Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  HID driver for Lenovo:
0004  *  - ThinkPad USB Keyboard with TrackPoint (tpkbd)
0005  *  - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
0006  *  - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
0007  *  - ThinkPad TrackPoint Keyboard II USB/Bluetooth (cptkbd/tpIIkbd)
0008  *
0009  *  Copyright (c) 2012 Bernhard Seibold
0010  *  Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
0011  *
0012  * Linux IBM/Lenovo Scrollpoint mouse driver:
0013  * - IBM Scrollpoint III
0014  * - IBM Scrollpoint Pro
0015  * - IBM Scrollpoint Optical
0016  * - IBM Scrollpoint Optical 800dpi
0017  * - IBM Scrollpoint Optical 800dpi Pro
0018  * - Lenovo Scrollpoint Optical
0019  *
0020  *  Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
0021  *  Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
0022  */
0023 
0024 /*
0025  */
0026 
0027 #include <linux/module.h>
0028 #include <linux/sysfs.h>
0029 #include <linux/device.h>
0030 #include <linux/hid.h>
0031 #include <linux/input.h>
0032 #include <linux/leds.h>
0033 #include <linux/workqueue.h>
0034 
0035 #include "hid-ids.h"
0036 
0037 /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */
0038 #define LENOVO_KEY_MICMUTE KEY_F20
0039 
0040 struct lenovo_drvdata {
0041     u8 led_report[3]; /* Must be first for proper alignment */
0042     int led_state;
0043     struct mutex led_report_mutex;
0044     struct led_classdev led_mute;
0045     struct led_classdev led_micmute;
0046     struct work_struct fn_lock_sync_work;
0047     struct hid_device *hdev;
0048     int press_to_select;
0049     int dragging;
0050     int release_to_select;
0051     int select_right;
0052     int sensitivity;
0053     int press_speed;
0054     u8 middlebutton_state; /* 0:Up, 1:Down (undecided), 2:Scrolling */
0055     bool fn_lock;
0056 };
0057 
0058 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
0059 
0060 #define TP10UBKBD_LED_OUTPUT_REPORT 9
0061 
0062 #define TP10UBKBD_FN_LOCK_LED       0x54
0063 #define TP10UBKBD_MUTE_LED      0x64
0064 #define TP10UBKBD_MICMUTE_LED       0x74
0065 
0066 #define TP10UBKBD_LED_OFF       1
0067 #define TP10UBKBD_LED_ON        2
0068 
0069 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
0070                     enum led_brightness value)
0071 {
0072     struct lenovo_drvdata *data = hid_get_drvdata(hdev);
0073     int ret;
0074 
0075     mutex_lock(&data->led_report_mutex);
0076 
0077     data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT;
0078     data->led_report[1] = led_code;
0079     data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF;
0080     ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3,
0081                  HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
0082     if (ret != 3) {
0083         if (ret != -ENODEV)
0084             hid_err(hdev, "Set LED output report error: %d\n", ret);
0085 
0086         ret = ret < 0 ? ret : -EIO;
0087     } else {
0088         ret = 0;
0089     }
0090 
0091     mutex_unlock(&data->led_report_mutex);
0092 
0093     return ret;
0094 }
0095 
0096 static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
0097 {
0098     struct lenovo_drvdata *data =
0099         container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
0100 
0101     lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
0102                  data->fn_lock);
0103 }
0104 
0105 static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
0106     0x05, 0x88,     /* Usage Page (Vendor Usage Page 0x88)  */
0107     0x09, 0x01,     /* Usage (Vendor Usage 0x01)        */
0108     0xa1, 0x01,     /* Collection (Application)     */
0109     0x85, 0x04,     /*  Report ID (4)           */
0110     0x19, 0x00,     /*  Usage Minimum (0)           */
0111     0x2a, 0xff, 0xff,   /*  Usage Maximum (65535)       */
0112 };
0113 
0114 /* Broken ThinkPad TrackPoint II collection (Bluetooth mode) */
0115 static const __u8 lenovo_tpIIbtkbd_need_fixup_collection[] = {
0116     0x06, 0x00, 0xFF,   /* Usage Page (Vendor Defined 0xFF00) */
0117     0x09, 0x01,     /* Usage (0x01) */
0118     0xA1, 0x01,     /* Collection (Application) */
0119     0x85, 0x05,     /*   Report ID (5) */
0120     0x1A, 0xF1, 0x00,   /*   Usage Minimum (0xF1) */
0121     0x2A, 0xFC, 0x00,   /*   Usage Maximum (0xFC) */
0122     0x15, 0x00,     /*   Logical Minimum (0) */
0123     0x25, 0x01,     /*   Logical Maximum (1) */
0124     0x75, 0x01,     /*   Report Size (1) */
0125     0x95, 0x0D,     /*   Report Count (13) */
0126     0x81, 0x02,     /*   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */
0127     0x95, 0x03,     /*   Report Count (3) */
0128     0x81, 0x01,     /*   Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */
0129 };
0130 
0131 static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
0132         unsigned int *rsize)
0133 {
0134     switch (hdev->product) {
0135     case USB_DEVICE_ID_LENOVO_TPPRODOCK:
0136         /* the fixups that need to be done:
0137          *   - get a reasonable usage max for the vendor collection
0138          *     0x8801 from the report ID 4
0139          */
0140         if (*rsize >= 153 &&
0141             memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
0142               sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
0143             rdesc[151] = 0x01;
0144             rdesc[152] = 0x00;
0145         }
0146         break;
0147     case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
0148         if (*rsize >= 263 &&
0149             memcmp(&rdesc[234], lenovo_tpIIbtkbd_need_fixup_collection,
0150               sizeof(lenovo_tpIIbtkbd_need_fixup_collection)) == 0) {
0151             rdesc[244] = 0x00; /* usage minimum = 0x00 */
0152             rdesc[247] = 0xff; /* usage maximum = 0xff */
0153             rdesc[252] = 0xff; /* logical maximum = 0xff */
0154             rdesc[254] = 0x08; /* report size = 0x08 */
0155             rdesc[256] = 0x01; /* report count = 0x01 */
0156             rdesc[258] = 0x00; /* input = 0x00 */
0157             rdesc[260] = 0x01; /* report count (2) = 0x01 */
0158         }
0159         break;
0160     }
0161     return rdesc;
0162 }
0163 
0164 static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
0165         struct hid_input *hi, struct hid_field *field,
0166         struct hid_usage *usage, unsigned long **bit, int *max)
0167 {
0168     if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
0169         /* This sub-device contains trackpoint, mark it */
0170         hid_set_drvdata(hdev, (void *)1);
0171         map_key_clear(LENOVO_KEY_MICMUTE);
0172         return 1;
0173     }
0174     return 0;
0175 }
0176 
0177 static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
0178         struct hid_input *hi, struct hid_field *field,
0179         struct hid_usage *usage, unsigned long **bit, int *max)
0180 {
0181     /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
0182     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
0183         (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
0184         switch (usage->hid & HID_USAGE) {
0185         case 0x00f1: /* Fn-F4: Mic mute */
0186             map_key_clear(LENOVO_KEY_MICMUTE);
0187             return 1;
0188         case 0x00f2: /* Fn-F5: Brightness down */
0189             map_key_clear(KEY_BRIGHTNESSDOWN);
0190             return 1;
0191         case 0x00f3: /* Fn-F6: Brightness up */
0192             map_key_clear(KEY_BRIGHTNESSUP);
0193             return 1;
0194         case 0x00f4: /* Fn-F7: External display (projector) */
0195             map_key_clear(KEY_SWITCHVIDEOMODE);
0196             return 1;
0197         case 0x00f5: /* Fn-F8: Wireless */
0198             map_key_clear(KEY_WLAN);
0199             return 1;
0200         case 0x00f6: /* Fn-F9: Control panel */
0201             map_key_clear(KEY_CONFIG);
0202             return 1;
0203         case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
0204             map_key_clear(KEY_SCALE);
0205             return 1;
0206         case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
0207             /* NB: This mapping is invented in raw_event below */
0208             map_key_clear(KEY_FILE);
0209             return 1;
0210         case 0x00fa: /* Fn-Esc: Fn-lock toggle */
0211             map_key_clear(KEY_FN_ESC);
0212             return 1;
0213         case 0x00fb: /* Middle mouse button (in native mode) */
0214             map_key_clear(BTN_MIDDLE);
0215             return 1;
0216         }
0217     }
0218 
0219     /* Compatibility middle/wheel mappings should be ignored */
0220     if (usage->hid == HID_GD_WHEEL)
0221         return -1;
0222     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
0223             (usage->hid & HID_USAGE) == 0x003)
0224         return -1;
0225     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
0226             (usage->hid & HID_USAGE) == 0x238)
0227         return -1;
0228 
0229     /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
0230     if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
0231         (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
0232         field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
0233         field->logical_minimum = -127;
0234         field->logical_maximum = 127;
0235 
0236         switch (usage->hid & HID_USAGE) {
0237         case 0x0000:
0238             hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
0239             return 1;
0240         case 0x0001:
0241             hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
0242             return 1;
0243         default:
0244             return -1;
0245         }
0246     }
0247 
0248     return 0;
0249 }
0250 
0251 static int lenovo_input_mapping_tpIIkbd(struct hid_device *hdev,
0252         struct hid_input *hi, struct hid_field *field,
0253         struct hid_usage *usage, unsigned long **bit, int *max)
0254 {
0255     /*
0256      * 0xff0a0000 = USB, HID_UP_MSVENDOR = BT.
0257      *
0258      * In BT mode, there are two HID_UP_MSVENDOR pages.
0259      * Use only the page that contains report ID == 5.
0260      */
0261     if (((usage->hid & HID_USAGE_PAGE) == 0xff0a0000 ||
0262         (usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) &&
0263         field->report->id == 5) {
0264         switch (usage->hid & HID_USAGE) {
0265         case 0x00bb: /* Fn-F4: Mic mute */
0266             map_key_clear(LENOVO_KEY_MICMUTE);
0267             return 1;
0268         case 0x00c3: /* Fn-F5: Brightness down */
0269             map_key_clear(KEY_BRIGHTNESSDOWN);
0270             return 1;
0271         case 0x00c4: /* Fn-F6: Brightness up */
0272             map_key_clear(KEY_BRIGHTNESSUP);
0273             return 1;
0274         case 0x00c1: /* Fn-F8: Notification center */
0275             map_key_clear(KEY_NOTIFICATION_CENTER);
0276             return 1;
0277         case 0x00bc: /* Fn-F9: Control panel */
0278             map_key_clear(KEY_CONFIG);
0279             return 1;
0280         case 0x00b6: /* Fn-F10: Bluetooth */
0281             map_key_clear(KEY_BLUETOOTH);
0282             return 1;
0283         case 0x00b7: /* Fn-F11: Keyboard config */
0284             map_key_clear(KEY_KEYBOARD);
0285             return 1;
0286         case 0x00b8: /* Fn-F12: User function */
0287             map_key_clear(KEY_PROG1);
0288             return 1;
0289         case 0x00b9: /* Fn-PrtSc: Snipping tool */
0290             map_key_clear(KEY_SELECTIVE_SCREENSHOT);
0291             return 1;
0292         case 0x00b5: /* Fn-Esc: Fn-lock toggle */
0293             map_key_clear(KEY_FN_ESC);
0294             return 1;
0295         }
0296     }
0297 
0298     if ((usage->hid & HID_USAGE_PAGE) == 0xffa00000) {
0299         switch (usage->hid & HID_USAGE) {
0300         case 0x00fb: /* Middle mouse (in native USB mode) */
0301             map_key_clear(BTN_MIDDLE);
0302             return 1;
0303         }
0304     }
0305 
0306     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR &&
0307         field->report->id == 21) {
0308         switch (usage->hid & HID_USAGE) {
0309         case 0x0004: /* Middle mouse (in native Bluetooth mode) */
0310             map_key_clear(BTN_MIDDLE);
0311             return 1;
0312         }
0313     }
0314 
0315     /* Compatibility middle/wheel mappings should be ignored */
0316     if (usage->hid == HID_GD_WHEEL)
0317         return -1;
0318     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
0319             (usage->hid & HID_USAGE) == 0x003)
0320         return -1;
0321     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
0322             (usage->hid & HID_USAGE) == 0x238)
0323         return -1;
0324 
0325     /* Map wheel emulation reports: 0xff10 */
0326     if ((usage->hid & HID_USAGE_PAGE) == 0xff100000) {
0327         field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
0328         field->logical_minimum = -127;
0329         field->logical_maximum = 127;
0330 
0331         switch (usage->hid & HID_USAGE) {
0332         case 0x0000:
0333             hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
0334             return 1;
0335         case 0x0001:
0336             hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
0337             return 1;
0338         default:
0339             return -1;
0340         }
0341     }
0342 
0343     return 0;
0344 }
0345 
0346 static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
0347         struct hid_input *hi, struct hid_field *field,
0348         struct hid_usage *usage, unsigned long **bit, int *max)
0349 {
0350     if (usage->hid == HID_GD_Z) {
0351         hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
0352         return 1;
0353     }
0354     return 0;
0355 }
0356 
0357 static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
0358         struct hid_input *hi, struct hid_field *field,
0359         struct hid_usage *usage, unsigned long **bit, int *max)
0360 {
0361     /*
0362      * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
0363      * a bunch of keys which have no standard consumer page code.
0364      */
0365     if (usage->hid == 0x000c0001) {
0366         switch (usage->usage_index) {
0367         case 8: /* Fn-Esc: Fn-lock toggle */
0368             map_key_clear(KEY_FN_ESC);
0369             return 1;
0370         case 9: /* Fn-F4: Mic mute */
0371             map_key_clear(LENOVO_KEY_MICMUTE);
0372             return 1;
0373         case 10: /* Fn-F7: Control panel */
0374             map_key_clear(KEY_CONFIG);
0375             return 1;
0376         case 11: /* Fn-F8: Search (magnifier glass) */
0377             map_key_clear(KEY_SEARCH);
0378             return 1;
0379         case 12: /* Fn-F10: Open My computer (6 boxes) */
0380             map_key_clear(KEY_FILE);
0381             return 1;
0382         }
0383     }
0384 
0385     /*
0386      * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
0387      * from suspend and it does not actually have a F23 key, ignore it.
0388      */
0389     if (usage->hid == 0x00070072)
0390         return -1;
0391 
0392     return 0;
0393 }
0394 
0395 static int lenovo_input_mapping_x1_tab_kbd(struct hid_device *hdev,
0396         struct hid_input *hi, struct hid_field *field,
0397         struct hid_usage *usage, unsigned long **bit, int *max)
0398 {
0399     /*
0400      * The ThinkPad X1 Tablet Thin Keyboard uses 0x000c0001 usage for
0401      * a bunch of keys which have no standard consumer page code.
0402      */
0403     if (usage->hid == 0x000c0001) {
0404         switch (usage->usage_index) {
0405         case 0: /* Fn-F10: Enable/disable bluetooth */
0406             map_key_clear(KEY_BLUETOOTH);
0407             return 1;
0408         case 1: /* Fn-F11: Keyboard settings */
0409             map_key_clear(KEY_KEYBOARD);
0410             return 1;
0411         case 2: /* Fn-F12: User function / Cortana */
0412             map_key_clear(KEY_MACRO1);
0413             return 1;
0414         case 3: /* Fn-PrtSc: Snipping tool */
0415             map_key_clear(KEY_SELECTIVE_SCREENSHOT);
0416             return 1;
0417         case 8: /* Fn-Esc: Fn-lock toggle */
0418             map_key_clear(KEY_FN_ESC);
0419             return 1;
0420         case 9: /* Fn-F4: Mute/unmute microphone */
0421             map_key_clear(KEY_MICMUTE);
0422             return 1;
0423         case 10: /* Fn-F9: Settings */
0424             map_key_clear(KEY_CONFIG);
0425             return 1;
0426         case 13: /* Fn-F7: Manage external displays */
0427             map_key_clear(KEY_SWITCHVIDEOMODE);
0428             return 1;
0429         case 14: /* Fn-F8: Enable/disable wifi */
0430             map_key_clear(KEY_WLAN);
0431             return 1;
0432         }
0433     }
0434 
0435     if (usage->hid == (HID_UP_KEYBOARD | 0x009a)) {
0436         map_key_clear(KEY_SYSRQ);
0437         return 1;
0438     }
0439 
0440     return 0;
0441 }
0442 
0443 static int lenovo_input_mapping(struct hid_device *hdev,
0444         struct hid_input *hi, struct hid_field *field,
0445         struct hid_usage *usage, unsigned long **bit, int *max)
0446 {
0447     switch (hdev->product) {
0448     case USB_DEVICE_ID_LENOVO_TPKBD:
0449         return lenovo_input_mapping_tpkbd(hdev, hi, field,
0450                             usage, bit, max);
0451     case USB_DEVICE_ID_LENOVO_CUSBKBD:
0452     case USB_DEVICE_ID_LENOVO_CBTKBD:
0453         return lenovo_input_mapping_cptkbd(hdev, hi, field,
0454                             usage, bit, max);
0455     case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
0456     case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
0457         return lenovo_input_mapping_tpIIkbd(hdev, hi, field,
0458                             usage, bit, max);
0459     case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
0460     case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
0461     case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
0462     case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
0463     case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
0464     case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
0465         return lenovo_input_mapping_scrollpoint(hdev, hi, field,
0466                             usage, bit, max);
0467     case USB_DEVICE_ID_LENOVO_TP10UBKBD:
0468         return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
0469                                    usage, bit, max);
0470     case USB_DEVICE_ID_LENOVO_X1_TAB:
0471         return lenovo_input_mapping_x1_tab_kbd(hdev, hi, field, usage, bit, max);
0472     default:
0473         return 0;
0474     }
0475 }
0476 
0477 #undef map_key_clear
0478 
0479 /* Send a config command to the keyboard */
0480 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
0481             unsigned char byte2, unsigned char byte3)
0482 {
0483     int ret;
0484     unsigned char *buf;
0485 
0486     buf = kzalloc(3, GFP_KERNEL);
0487     if (!buf)
0488         return -ENOMEM;
0489 
0490     /*
0491      * Feature report 0x13 is used for USB,
0492      * output report 0x18 is used for Bluetooth.
0493      * buf[0] is ignored by hid_hw_raw_request.
0494      */
0495     buf[0] = 0x18;
0496     buf[1] = byte2;
0497     buf[2] = byte3;
0498 
0499     switch (hdev->product) {
0500     case USB_DEVICE_ID_LENOVO_CUSBKBD:
0501     case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
0502         ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
0503                     HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
0504         break;
0505     case USB_DEVICE_ID_LENOVO_CBTKBD:
0506     case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
0507         ret = hid_hw_output_report(hdev, buf, 3);
0508         break;
0509     default:
0510         ret = -EINVAL;
0511         break;
0512     }
0513 
0514     kfree(buf);
0515 
0516     return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
0517 }
0518 
0519 static void lenovo_features_set_cptkbd(struct hid_device *hdev)
0520 {
0521     int ret;
0522     struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
0523 
0524     ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
0525     if (ret)
0526         hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
0527 
0528     ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
0529     if (ret)
0530         hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
0531 }
0532 
0533 static ssize_t attr_fn_lock_show(struct device *dev,
0534         struct device_attribute *attr,
0535         char *buf)
0536 {
0537     struct hid_device *hdev = to_hid_device(dev);
0538     struct lenovo_drvdata *data = hid_get_drvdata(hdev);
0539 
0540     return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
0541 }
0542 
0543 static ssize_t attr_fn_lock_store(struct device *dev,
0544         struct device_attribute *attr,
0545         const char *buf,
0546         size_t count)
0547 {
0548     struct hid_device *hdev = to_hid_device(dev);
0549     struct lenovo_drvdata *data = hid_get_drvdata(hdev);
0550     int value, ret;
0551 
0552     if (kstrtoint(buf, 10, &value))
0553         return -EINVAL;
0554     if (value < 0 || value > 1)
0555         return -EINVAL;
0556 
0557     data->fn_lock = !!value;
0558 
0559     switch (hdev->product) {
0560     case USB_DEVICE_ID_LENOVO_CUSBKBD:
0561     case USB_DEVICE_ID_LENOVO_CBTKBD:
0562     case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
0563     case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
0564         lenovo_features_set_cptkbd(hdev);
0565         break;
0566     case USB_DEVICE_ID_LENOVO_TP10UBKBD:
0567     case USB_DEVICE_ID_LENOVO_X1_TAB:
0568         ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
0569         if (ret)
0570             return ret;
0571         break;
0572     }
0573 
0574     return count;
0575 }
0576 
0577 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
0578         struct device_attribute *attr,
0579         char *buf)
0580 {
0581     struct hid_device *hdev = to_hid_device(dev);
0582     struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
0583 
0584     return snprintf(buf, PAGE_SIZE, "%u\n",
0585         cptkbd_data->sensitivity);
0586 }
0587 
0588 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
0589         struct device_attribute *attr,
0590         const char *buf,
0591         size_t count)
0592 {
0593     struct hid_device *hdev = to_hid_device(dev);
0594     struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
0595     int value;
0596 
0597     if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
0598         return -EINVAL;
0599 
0600     cptkbd_data->sensitivity = value;
0601     lenovo_features_set_cptkbd(hdev);
0602 
0603     return count;
0604 }
0605 
0606 
0607 static struct device_attribute dev_attr_fn_lock =
0608     __ATTR(fn_lock, S_IWUSR | S_IRUGO,
0609             attr_fn_lock_show,
0610             attr_fn_lock_store);
0611 
0612 static struct device_attribute dev_attr_sensitivity_cptkbd =
0613     __ATTR(sensitivity, S_IWUSR | S_IRUGO,
0614             attr_sensitivity_show_cptkbd,
0615             attr_sensitivity_store_cptkbd);
0616 
0617 
0618 static struct attribute *lenovo_attributes_cptkbd[] = {
0619     &dev_attr_fn_lock.attr,
0620     &dev_attr_sensitivity_cptkbd.attr,
0621     NULL
0622 };
0623 
0624 static const struct attribute_group lenovo_attr_group_cptkbd = {
0625     .attrs = lenovo_attributes_cptkbd,
0626 };
0627 
0628 static int lenovo_raw_event(struct hid_device *hdev,
0629             struct hid_report *report, u8 *data, int size)
0630 {
0631     /*
0632      * Compact USB keyboard's Fn-F12 report holds down many other keys, and
0633      * its own key is outside the usage page range. Remove extra
0634      * keypresses and remap to inside usage page.
0635      */
0636     if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
0637             && size == 3
0638             && data[0] == 0x15
0639             && data[1] == 0x94
0640             && data[2] == 0x01)) {
0641         data[1] = 0x00;
0642         data[2] = 0x01;
0643     }
0644 
0645     return 0;
0646 }
0647 
0648 static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
0649         struct hid_field *field, struct hid_usage *usage, __s32 value)
0650 {
0651     struct lenovo_drvdata *data = hid_get_drvdata(hdev);
0652 
0653     if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
0654         /*
0655          * The user has toggled the Fn-lock state. Toggle our own
0656          * cached value of it and sync our value to the keyboard to
0657          * ensure things are in sync (the sycning should be a no-op).
0658          */
0659         data->fn_lock = !data->fn_lock;
0660         schedule_work(&data->fn_lock_sync_work);
0661     }
0662 
0663     return 0;
0664 }
0665 
0666 static int lenovo_event_cptkbd(struct hid_device *hdev,
0667         struct hid_field *field, struct hid_usage *usage, __s32 value)
0668 {
0669     struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
0670 
0671     /* "wheel" scroll events */
0672     if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
0673             usage->code == REL_HWHEEL)) {
0674         /* Scroll events disable middle-click event */
0675         cptkbd_data->middlebutton_state = 2;
0676         return 0;
0677     }
0678 
0679     /* Middle click events */
0680     if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
0681         if (value == 1) {
0682             cptkbd_data->middlebutton_state = 1;
0683         } else if (value == 0) {
0684             if (cptkbd_data->middlebutton_state == 1) {
0685                 /* No scrolling inbetween, send middle-click */
0686                 input_event(field->hidinput->input,
0687                     EV_KEY, BTN_MIDDLE, 1);
0688                 input_sync(field->hidinput->input);
0689                 input_event(field->hidinput->input,
0690                     EV_KEY, BTN_MIDDLE, 0);
0691                 input_sync(field->hidinput->input);
0692             }
0693             cptkbd_data->middlebutton_state = 0;
0694         }
0695         return 1;
0696     }
0697 
0698     if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
0699         /*
0700          * The user has toggled the Fn-lock state. Toggle our own
0701          * cached value of it and sync our value to the keyboard to
0702          * ensure things are in sync (the syncing should be a no-op).
0703          */
0704         cptkbd_data->fn_lock = !cptkbd_data->fn_lock;
0705     }
0706 
0707     return 0;
0708 }
0709 
0710 static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
0711         struct hid_usage *usage, __s32 value)
0712 {
0713     if (!hid_get_drvdata(hdev))
0714         return 0;
0715 
0716     switch (hdev->product) {
0717     case USB_DEVICE_ID_LENOVO_CUSBKBD:
0718     case USB_DEVICE_ID_LENOVO_CBTKBD:
0719     case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
0720     case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
0721         return lenovo_event_cptkbd(hdev, field, usage, value);
0722     case USB_DEVICE_ID_LENOVO_TP10UBKBD:
0723     case USB_DEVICE_ID_LENOVO_X1_TAB:
0724         return lenovo_event_tp10ubkbd(hdev, field, usage, value);
0725     default:
0726         return 0;
0727     }
0728 }
0729 
0730 static int lenovo_features_set_tpkbd(struct hid_device *hdev)
0731 {
0732     struct hid_report *report;
0733     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0734 
0735     report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
0736 
0737     report->field[0]->value[0]  = data_pointer->press_to_select   ? 0x01 : 0x02;
0738     report->field[0]->value[0] |= data_pointer->dragging          ? 0x04 : 0x08;
0739     report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
0740     report->field[0]->value[0] |= data_pointer->select_right      ? 0x80 : 0x40;
0741     report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
0742     report->field[2]->value[0] = data_pointer->sensitivity;
0743     report->field[3]->value[0] = data_pointer->press_speed;
0744 
0745     hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
0746     return 0;
0747 }
0748 
0749 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
0750         struct device_attribute *attr,
0751         char *buf)
0752 {
0753     struct hid_device *hdev = to_hid_device(dev);
0754     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0755 
0756     return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
0757 }
0758 
0759 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
0760         struct device_attribute *attr,
0761         const char *buf,
0762         size_t count)
0763 {
0764     struct hid_device *hdev = to_hid_device(dev);
0765     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0766     int value;
0767 
0768     if (kstrtoint(buf, 10, &value))
0769         return -EINVAL;
0770     if (value < 0 || value > 1)
0771         return -EINVAL;
0772 
0773     data_pointer->press_to_select = value;
0774     lenovo_features_set_tpkbd(hdev);
0775 
0776     return count;
0777 }
0778 
0779 static ssize_t attr_dragging_show_tpkbd(struct device *dev,
0780         struct device_attribute *attr,
0781         char *buf)
0782 {
0783     struct hid_device *hdev = to_hid_device(dev);
0784     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0785 
0786     return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
0787 }
0788 
0789 static ssize_t attr_dragging_store_tpkbd(struct device *dev,
0790         struct device_attribute *attr,
0791         const char *buf,
0792         size_t count)
0793 {
0794     struct hid_device *hdev = to_hid_device(dev);
0795     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0796     int value;
0797 
0798     if (kstrtoint(buf, 10, &value))
0799         return -EINVAL;
0800     if (value < 0 || value > 1)
0801         return -EINVAL;
0802 
0803     data_pointer->dragging = value;
0804     lenovo_features_set_tpkbd(hdev);
0805 
0806     return count;
0807 }
0808 
0809 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
0810         struct device_attribute *attr,
0811         char *buf)
0812 {
0813     struct hid_device *hdev = to_hid_device(dev);
0814     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0815 
0816     return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
0817 }
0818 
0819 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
0820         struct device_attribute *attr,
0821         const char *buf,
0822         size_t count)
0823 {
0824     struct hid_device *hdev = to_hid_device(dev);
0825     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0826     int value;
0827 
0828     if (kstrtoint(buf, 10, &value))
0829         return -EINVAL;
0830     if (value < 0 || value > 1)
0831         return -EINVAL;
0832 
0833     data_pointer->release_to_select = value;
0834     lenovo_features_set_tpkbd(hdev);
0835 
0836     return count;
0837 }
0838 
0839 static ssize_t attr_select_right_show_tpkbd(struct device *dev,
0840         struct device_attribute *attr,
0841         char *buf)
0842 {
0843     struct hid_device *hdev = to_hid_device(dev);
0844     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0845 
0846     return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
0847 }
0848 
0849 static ssize_t attr_select_right_store_tpkbd(struct device *dev,
0850         struct device_attribute *attr,
0851         const char *buf,
0852         size_t count)
0853 {
0854     struct hid_device *hdev = to_hid_device(dev);
0855     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0856     int value;
0857 
0858     if (kstrtoint(buf, 10, &value))
0859         return -EINVAL;
0860     if (value < 0 || value > 1)
0861         return -EINVAL;
0862 
0863     data_pointer->select_right = value;
0864     lenovo_features_set_tpkbd(hdev);
0865 
0866     return count;
0867 }
0868 
0869 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
0870         struct device_attribute *attr,
0871         char *buf)
0872 {
0873     struct hid_device *hdev = to_hid_device(dev);
0874     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0875 
0876     return snprintf(buf, PAGE_SIZE, "%u\n",
0877         data_pointer->sensitivity);
0878 }
0879 
0880 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
0881         struct device_attribute *attr,
0882         const char *buf,
0883         size_t count)
0884 {
0885     struct hid_device *hdev = to_hid_device(dev);
0886     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0887     int value;
0888 
0889     if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
0890         return -EINVAL;
0891 
0892     data_pointer->sensitivity = value;
0893     lenovo_features_set_tpkbd(hdev);
0894 
0895     return count;
0896 }
0897 
0898 static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
0899         struct device_attribute *attr,
0900         char *buf)
0901 {
0902     struct hid_device *hdev = to_hid_device(dev);
0903     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0904 
0905     return snprintf(buf, PAGE_SIZE, "%u\n",
0906         data_pointer->press_speed);
0907 }
0908 
0909 static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
0910         struct device_attribute *attr,
0911         const char *buf,
0912         size_t count)
0913 {
0914     struct hid_device *hdev = to_hid_device(dev);
0915     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0916     int value;
0917 
0918     if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
0919         return -EINVAL;
0920 
0921     data_pointer->press_speed = value;
0922     lenovo_features_set_tpkbd(hdev);
0923 
0924     return count;
0925 }
0926 
0927 static struct device_attribute dev_attr_press_to_select_tpkbd =
0928     __ATTR(press_to_select, S_IWUSR | S_IRUGO,
0929             attr_press_to_select_show_tpkbd,
0930             attr_press_to_select_store_tpkbd);
0931 
0932 static struct device_attribute dev_attr_dragging_tpkbd =
0933     __ATTR(dragging, S_IWUSR | S_IRUGO,
0934             attr_dragging_show_tpkbd,
0935             attr_dragging_store_tpkbd);
0936 
0937 static struct device_attribute dev_attr_release_to_select_tpkbd =
0938     __ATTR(release_to_select, S_IWUSR | S_IRUGO,
0939             attr_release_to_select_show_tpkbd,
0940             attr_release_to_select_store_tpkbd);
0941 
0942 static struct device_attribute dev_attr_select_right_tpkbd =
0943     __ATTR(select_right, S_IWUSR | S_IRUGO,
0944             attr_select_right_show_tpkbd,
0945             attr_select_right_store_tpkbd);
0946 
0947 static struct device_attribute dev_attr_sensitivity_tpkbd =
0948     __ATTR(sensitivity, S_IWUSR | S_IRUGO,
0949             attr_sensitivity_show_tpkbd,
0950             attr_sensitivity_store_tpkbd);
0951 
0952 static struct device_attribute dev_attr_press_speed_tpkbd =
0953     __ATTR(press_speed, S_IWUSR | S_IRUGO,
0954             attr_press_speed_show_tpkbd,
0955             attr_press_speed_store_tpkbd);
0956 
0957 static struct attribute *lenovo_attributes_tpkbd[] = {
0958     &dev_attr_press_to_select_tpkbd.attr,
0959     &dev_attr_dragging_tpkbd.attr,
0960     &dev_attr_release_to_select_tpkbd.attr,
0961     &dev_attr_select_right_tpkbd.attr,
0962     &dev_attr_sensitivity_tpkbd.attr,
0963     &dev_attr_press_speed_tpkbd.attr,
0964     NULL
0965 };
0966 
0967 static const struct attribute_group lenovo_attr_group_tpkbd = {
0968     .attrs = lenovo_attributes_tpkbd,
0969 };
0970 
0971 static void lenovo_led_set_tpkbd(struct hid_device *hdev)
0972 {
0973     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0974     struct hid_report *report;
0975 
0976     report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
0977     report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
0978     report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
0979     hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
0980 }
0981 
0982 static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
0983             enum led_brightness value)
0984 {
0985     struct device *dev = led_cdev->dev->parent;
0986     struct hid_device *hdev = to_hid_device(dev);
0987     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
0988     u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
0989     int led_nr = 0;
0990     int ret = 0;
0991 
0992     if (led_cdev == &data_pointer->led_micmute)
0993         led_nr = 1;
0994 
0995     if (value == LED_OFF)
0996         data_pointer->led_state &= ~(1 << led_nr);
0997     else
0998         data_pointer->led_state |= 1 << led_nr;
0999 
1000     switch (hdev->product) {
1001     case USB_DEVICE_ID_LENOVO_TPKBD:
1002         lenovo_led_set_tpkbd(hdev);
1003         break;
1004     case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1005     case USB_DEVICE_ID_LENOVO_X1_TAB:
1006         ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
1007         break;
1008     }
1009 
1010     return ret;
1011 }
1012 
1013 static int lenovo_register_leds(struct hid_device *hdev)
1014 {
1015     struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1016     size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
1017     char *name_mute, *name_micm;
1018     int ret;
1019 
1020     name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1021     name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1022     if (name_mute == NULL || name_micm == NULL) {
1023         hid_err(hdev, "Could not allocate memory for led data\n");
1024         return -ENOMEM;
1025     }
1026     snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
1027     snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
1028 
1029     data->led_mute.name = name_mute;
1030     data->led_mute.default_trigger = "audio-mute";
1031     data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
1032     data->led_mute.max_brightness = 1;
1033     data->led_mute.flags = LED_HW_PLUGGABLE;
1034     data->led_mute.dev = &hdev->dev;
1035     ret = led_classdev_register(&hdev->dev, &data->led_mute);
1036     if (ret < 0)
1037         return ret;
1038 
1039     data->led_micmute.name = name_micm;
1040     data->led_micmute.default_trigger = "audio-micmute";
1041     data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
1042     data->led_micmute.max_brightness = 1;
1043     data->led_micmute.flags = LED_HW_PLUGGABLE;
1044     data->led_micmute.dev = &hdev->dev;
1045     ret = led_classdev_register(&hdev->dev, &data->led_micmute);
1046     if (ret < 0) {
1047         led_classdev_unregister(&data->led_mute);
1048         return ret;
1049     }
1050 
1051     return 0;
1052 }
1053 
1054 static int lenovo_probe_tpkbd(struct hid_device *hdev)
1055 {
1056     struct lenovo_drvdata *data_pointer;
1057     int i, ret;
1058 
1059     /*
1060      * Only register extra settings against subdevice where input_mapping
1061      * set drvdata to 1, i.e. the trackpoint.
1062      */
1063     if (!hid_get_drvdata(hdev))
1064         return 0;
1065 
1066     hid_set_drvdata(hdev, NULL);
1067 
1068     /* Validate required reports. */
1069     for (i = 0; i < 4; i++) {
1070         if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
1071             return -ENODEV;
1072     }
1073     if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
1074         return -ENODEV;
1075 
1076     ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1077     if (ret)
1078         hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1079 
1080     data_pointer = devm_kzalloc(&hdev->dev,
1081                     sizeof(struct lenovo_drvdata),
1082                     GFP_KERNEL);
1083     if (data_pointer == NULL) {
1084         hid_err(hdev, "Could not allocate memory for driver data\n");
1085         ret = -ENOMEM;
1086         goto err;
1087     }
1088 
1089     // set same default values as windows driver
1090     data_pointer->sensitivity = 0xa0;
1091     data_pointer->press_speed = 0x38;
1092 
1093     hid_set_drvdata(hdev, data_pointer);
1094 
1095     ret = lenovo_register_leds(hdev);
1096     if (ret)
1097         goto err;
1098 
1099     lenovo_features_set_tpkbd(hdev);
1100 
1101     return 0;
1102 err:
1103     sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1104     return ret;
1105 }
1106 
1107 static int lenovo_probe_cptkbd(struct hid_device *hdev)
1108 {
1109     int ret;
1110     struct lenovo_drvdata *cptkbd_data;
1111 
1112     /* All the custom action happens on the USBMOUSE device for USB */
1113     if (((hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD) ||
1114         (hdev->product == USB_DEVICE_ID_LENOVO_TPIIUSBKBD)) &&
1115         hdev->type != HID_TYPE_USBMOUSE) {
1116         hid_dbg(hdev, "Ignoring keyboard half of device\n");
1117         return 0;
1118     }
1119 
1120     cptkbd_data = devm_kzalloc(&hdev->dev,
1121                     sizeof(*cptkbd_data),
1122                     GFP_KERNEL);
1123     if (cptkbd_data == NULL) {
1124         hid_err(hdev, "can't alloc keyboard descriptor\n");
1125         return -ENOMEM;
1126     }
1127     hid_set_drvdata(hdev, cptkbd_data);
1128 
1129     /*
1130      * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
1131      * regular keys (Compact only)
1132      */
1133     if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD ||
1134         hdev->product == USB_DEVICE_ID_LENOVO_CBTKBD) {
1135         ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
1136         if (ret)
1137             hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
1138     }
1139 
1140     /* Switch middle button to native mode */
1141     ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
1142     if (ret)
1143         hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
1144 
1145     /* Set keyboard settings to known state */
1146     cptkbd_data->middlebutton_state = 0;
1147     cptkbd_data->fn_lock = true;
1148     cptkbd_data->sensitivity = 0x05;
1149     lenovo_features_set_cptkbd(hdev);
1150 
1151     ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
1152     if (ret)
1153         hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1154 
1155     return 0;
1156 }
1157 
1158 static struct attribute *lenovo_attributes_tp10ubkbd[] = {
1159     &dev_attr_fn_lock.attr,
1160     NULL
1161 };
1162 
1163 static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
1164     .attrs = lenovo_attributes_tp10ubkbd,
1165 };
1166 
1167 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
1168 {
1169     struct hid_report_enum *rep_enum;
1170     struct lenovo_drvdata *data;
1171     struct hid_report *rep;
1172     bool found;
1173     int ret;
1174 
1175     /*
1176      * The LEDs and the Fn-lock functionality use output report 9,
1177      * with an application of 0xffa0001, add the LEDs on the interface
1178      * with this output report.
1179      */
1180     found = false;
1181     rep_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1182     list_for_each_entry(rep, &rep_enum->report_list, list) {
1183         if (rep->application == 0xffa00001)
1184             found = true;
1185     }
1186     if (!found)
1187         return 0;
1188 
1189     data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
1190     if (!data)
1191         return -ENOMEM;
1192 
1193     mutex_init(&data->led_report_mutex);
1194     INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
1195     data->hdev = hdev;
1196 
1197     hid_set_drvdata(hdev, data);
1198 
1199     /*
1200      * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
1201      * We cannot read the state, only set it, so we force it to on here
1202      * (which should be a no-op) to make sure that our state matches the
1203      * keyboard's FN-lock state. This is the same as what Windows does.
1204      */
1205     data->fn_lock = true;
1206     lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
1207 
1208     ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1209     if (ret)
1210         return ret;
1211 
1212     ret = lenovo_register_leds(hdev);
1213     if (ret)
1214         goto err;
1215 
1216     return 0;
1217 err:
1218     sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1219     return ret;
1220 }
1221 
1222 static int lenovo_probe(struct hid_device *hdev,
1223         const struct hid_device_id *id)
1224 {
1225     int ret;
1226 
1227     ret = hid_parse(hdev);
1228     if (ret) {
1229         hid_err(hdev, "hid_parse failed\n");
1230         goto err;
1231     }
1232 
1233     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1234     if (ret) {
1235         hid_err(hdev, "hid_hw_start failed\n");
1236         goto err;
1237     }
1238 
1239     switch (hdev->product) {
1240     case USB_DEVICE_ID_LENOVO_TPKBD:
1241         ret = lenovo_probe_tpkbd(hdev);
1242         break;
1243     case USB_DEVICE_ID_LENOVO_CUSBKBD:
1244     case USB_DEVICE_ID_LENOVO_CBTKBD:
1245     case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1246     case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1247         ret = lenovo_probe_cptkbd(hdev);
1248         break;
1249     case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1250     case USB_DEVICE_ID_LENOVO_X1_TAB:
1251         ret = lenovo_probe_tp10ubkbd(hdev);
1252         break;
1253     default:
1254         ret = 0;
1255         break;
1256     }
1257     if (ret)
1258         goto err_hid;
1259 
1260     return 0;
1261 err_hid:
1262     hid_hw_stop(hdev);
1263 err:
1264     return ret;
1265 }
1266 
1267 static void lenovo_remove_tpkbd(struct hid_device *hdev)
1268 {
1269     struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1270 
1271     /*
1272      * Only the trackpoint half of the keyboard has drvdata and stuff that
1273      * needs unregistering.
1274      */
1275     if (data_pointer == NULL)
1276         return;
1277 
1278     sysfs_remove_group(&hdev->dev.kobj,
1279             &lenovo_attr_group_tpkbd);
1280 
1281     led_classdev_unregister(&data_pointer->led_micmute);
1282     led_classdev_unregister(&data_pointer->led_mute);
1283 }
1284 
1285 static void lenovo_remove_cptkbd(struct hid_device *hdev)
1286 {
1287     sysfs_remove_group(&hdev->dev.kobj,
1288             &lenovo_attr_group_cptkbd);
1289 }
1290 
1291 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1292 {
1293     struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1294 
1295     if (data == NULL)
1296         return;
1297 
1298     led_classdev_unregister(&data->led_micmute);
1299     led_classdev_unregister(&data->led_mute);
1300 
1301     sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1302     cancel_work_sync(&data->fn_lock_sync_work);
1303 }
1304 
1305 static void lenovo_remove(struct hid_device *hdev)
1306 {
1307     switch (hdev->product) {
1308     case USB_DEVICE_ID_LENOVO_TPKBD:
1309         lenovo_remove_tpkbd(hdev);
1310         break;
1311     case USB_DEVICE_ID_LENOVO_CUSBKBD:
1312     case USB_DEVICE_ID_LENOVO_CBTKBD:
1313     case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1314     case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1315         lenovo_remove_cptkbd(hdev);
1316         break;
1317     case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1318     case USB_DEVICE_ID_LENOVO_X1_TAB:
1319         lenovo_remove_tp10ubkbd(hdev);
1320         break;
1321     }
1322 
1323     hid_hw_stop(hdev);
1324 }
1325 
1326 static int lenovo_input_configured(struct hid_device *hdev,
1327         struct hid_input *hi)
1328 {
1329     switch (hdev->product) {
1330         case USB_DEVICE_ID_LENOVO_TPKBD:
1331         case USB_DEVICE_ID_LENOVO_CUSBKBD:
1332         case USB_DEVICE_ID_LENOVO_CBTKBD:
1333         case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1334         case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1335             if (test_bit(EV_REL, hi->input->evbit)) {
1336                 /* set only for trackpoint device */
1337                 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1338                 __set_bit(INPUT_PROP_POINTING_STICK,
1339                         hi->input->propbit);
1340             }
1341             break;
1342     }
1343 
1344     return 0;
1345 }
1346 
1347 
1348 static const struct hid_device_id lenovo_devices[] = {
1349     { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1350     { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1351     { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIUSBKBD) },
1352     { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1353     { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIBTKBD) },
1354     { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1355     { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1356     { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1357     { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1358     { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1359     { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1360     { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1361     { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1362     /*
1363      * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1364      * part, while letting hid-multitouch.c handle the touchpad and trackpoint.
1365      */
1366     { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1367              USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB) },
1368     { }
1369 };
1370 
1371 MODULE_DEVICE_TABLE(hid, lenovo_devices);
1372 
1373 static struct hid_driver lenovo_driver = {
1374     .name = "lenovo",
1375     .id_table = lenovo_devices,
1376     .input_configured = lenovo_input_configured,
1377     .input_mapping = lenovo_input_mapping,
1378     .probe = lenovo_probe,
1379     .remove = lenovo_remove,
1380     .raw_event = lenovo_raw_event,
1381     .event = lenovo_event,
1382     .report_fixup = lenovo_report_fixup,
1383 };
1384 module_hid_driver(lenovo_driver);
1385 
1386 MODULE_LICENSE("GPL");