GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / hid / hid-lenovo.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  HID driver for Lenovo:
4  *  - ThinkPad USB Keyboard with TrackPoint (tpkbd)
5  *  - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
6  *  - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
7  *
8  *  Copyright (c) 2012 Bernhard Seibold
9  *  Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
10  *
11  * Linux IBM/Lenovo Scrollpoint mouse driver:
12  * - IBM Scrollpoint III
13  * - IBM Scrollpoint Pro
14  * - IBM Scrollpoint Optical
15  * - IBM Scrollpoint Optical 800dpi
16  * - IBM Scrollpoint Optical 800dpi Pro
17  * - Lenovo Scrollpoint Optical
18  *
19  *  Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
20  *  Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
21  */
22
23 /*
24  */
25
26 #include <linux/module.h>
27 #include <linux/sysfs.h>
28 #include <linux/device.h>
29 #include <linux/hid.h>
30 #include <linux/input.h>
31 #include <linux/leds.h>
32 #include <linux/workqueue.h>
33
34 #include "hid-ids.h"
35
36 /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */
37 #define LENOVO_KEY_MICMUTE KEY_F20
38
39 struct lenovo_drvdata {
40         u8 led_report[3]; /* Must be first for proper alignment */
41         int led_state;
42         struct mutex led_report_mutex;
43         struct led_classdev led_mute;
44         struct led_classdev led_micmute;
45         struct work_struct fn_lock_sync_work;
46         struct hid_device *hdev;
47         int press_to_select;
48         int dragging;
49         int release_to_select;
50         int select_right;
51         int sensitivity;
52         int press_speed;
53         /* 0: Up
54          * 1: Down (undecided)
55          * 2: Scrolling
56          */
57         u8 middlebutton_state;
58         bool fn_lock;
59         bool middleclick_workaround_cptkbd;
60 };
61
62 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
63
64 #define TP10UBKBD_LED_OUTPUT_REPORT     9
65
66 #define TP10UBKBD_FN_LOCK_LED           0x54
67 #define TP10UBKBD_MUTE_LED              0x64
68 #define TP10UBKBD_MICMUTE_LED           0x74
69
70 #define TP10UBKBD_LED_OFF               1
71 #define TP10UBKBD_LED_ON                2
72
73 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
74                                     enum led_brightness value)
75 {
76         struct lenovo_drvdata *data = hid_get_drvdata(hdev);
77         int ret;
78
79         mutex_lock(&data->led_report_mutex);
80
81         data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT;
82         data->led_report[1] = led_code;
83         data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF;
84         ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3,
85                                  HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
86         if (ret != 3) {
87                 if (ret != -ENODEV)
88                         hid_err(hdev, "Set LED output report error: %d\n", ret);
89
90                 ret = ret < 0 ? ret : -EIO;
91         } else {
92                 ret = 0;
93         }
94
95         mutex_unlock(&data->led_report_mutex);
96
97         return ret;
98 }
99
100 static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
101 {
102         struct lenovo_drvdata *data =
103                 container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
104
105         lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
106                                  data->fn_lock);
107 }
108
109 static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
110         0x05, 0x88,             /* Usage Page (Vendor Usage Page 0x88)  */
111         0x09, 0x01,             /* Usage (Vendor Usage 0x01)            */
112         0xa1, 0x01,             /* Collection (Application)             */
113         0x85, 0x04,             /*  Report ID (4)                       */
114         0x19, 0x00,             /*  Usage Minimum (0)                   */
115         0x2a, 0xff, 0xff,       /*  Usage Maximum (65535)               */
116 };
117
118 static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
119                 unsigned int *rsize)
120 {
121         switch (hdev->product) {
122         case USB_DEVICE_ID_LENOVO_TPPRODOCK:
123                 /* the fixups that need to be done:
124                  *   - get a reasonable usage max for the vendor collection
125                  *     0x8801 from the report ID 4
126                  */
127                 if (*rsize >= 153 &&
128                     memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
129                           sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
130                         rdesc[151] = 0x01;
131                         rdesc[152] = 0x00;
132                 }
133                 break;
134         }
135         return rdesc;
136 }
137
138 static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
139                 struct hid_input *hi, struct hid_field *field,
140                 struct hid_usage *usage, unsigned long **bit, int *max)
141 {
142         if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
143                 /* This sub-device contains trackpoint, mark it */
144                 hid_set_drvdata(hdev, (void *)1);
145                 map_key_clear(LENOVO_KEY_MICMUTE);
146                 return 1;
147         }
148         return 0;
149 }
150
151 static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
152                 struct hid_input *hi, struct hid_field *field,
153                 struct hid_usage *usage, unsigned long **bit, int *max)
154 {
155         /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
156         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
157             (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
158                 switch (usage->hid & HID_USAGE) {
159                 case 0x00f1: /* Fn-F4: Mic mute */
160                         map_key_clear(LENOVO_KEY_MICMUTE);
161                         return 1;
162                 case 0x00f2: /* Fn-F5: Brightness down */
163                         map_key_clear(KEY_BRIGHTNESSDOWN);
164                         return 1;
165                 case 0x00f3: /* Fn-F6: Brightness up */
166                         map_key_clear(KEY_BRIGHTNESSUP);
167                         return 1;
168                 case 0x00f4: /* Fn-F7: External display (projector) */
169                         map_key_clear(KEY_SWITCHVIDEOMODE);
170                         return 1;
171                 case 0x00f5: /* Fn-F8: Wireless */
172                         map_key_clear(KEY_WLAN);
173                         return 1;
174                 case 0x00f6: /* Fn-F9: Control panel */
175                         map_key_clear(KEY_CONFIG);
176                         return 1;
177                 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
178                         map_key_clear(KEY_SCALE);
179                         return 1;
180                 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
181                         /* NB: This mapping is invented in raw_event below */
182                         map_key_clear(KEY_FILE);
183                         return 1;
184                 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
185                         map_key_clear(KEY_FN_ESC);
186                         return 1;
187                 case 0x00fb: /* Middle mouse button (in native mode) */
188                         map_key_clear(BTN_MIDDLE);
189                         return 1;
190                 }
191         }
192
193         /* Compatibility middle/wheel mappings should be ignored */
194         if (usage->hid == HID_GD_WHEEL)
195                 return -1;
196         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
197                         (usage->hid & HID_USAGE) == 0x003)
198                 return -1;
199         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
200                         (usage->hid & HID_USAGE) == 0x238)
201                 return -1;
202
203         /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
204         if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
205             (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
206                 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
207                 field->logical_minimum = -127;
208                 field->logical_maximum = 127;
209
210                 switch (usage->hid & HID_USAGE) {
211                 case 0x0000:
212                         hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
213                         return 1;
214                 case 0x0001:
215                         hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
216                         return 1;
217                 default:
218                         return -1;
219                 }
220         }
221
222         return 0;
223 }
224
225 static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
226                 struct hid_input *hi, struct hid_field *field,
227                 struct hid_usage *usage, unsigned long **bit, int *max)
228 {
229         if (usage->hid == HID_GD_Z) {
230                 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
231                 return 1;
232         }
233         return 0;
234 }
235
236 static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
237                 struct hid_input *hi, struct hid_field *field,
238                 struct hid_usage *usage, unsigned long **bit, int *max)
239 {
240         /*
241          * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
242          * a bunch of keys which have no standard consumer page code.
243          */
244         if (usage->hid == 0x000c0001) {
245                 switch (usage->usage_index) {
246                 case 8: /* Fn-Esc: Fn-lock toggle */
247                         map_key_clear(KEY_FN_ESC);
248                         return 1;
249                 case 9: /* Fn-F4: Mic mute */
250                         map_key_clear(LENOVO_KEY_MICMUTE);
251                         return 1;
252                 case 10: /* Fn-F7: Control panel */
253                         map_key_clear(KEY_CONFIG);
254                         return 1;
255                 case 11: /* Fn-F8: Search (magnifier glass) */
256                         map_key_clear(KEY_SEARCH);
257                         return 1;
258                 case 12: /* Fn-F10: Open My computer (6 boxes) */
259                         map_key_clear(KEY_FILE);
260                         return 1;
261                 }
262         }
263
264         /*
265          * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
266          * from suspend and it does not actually have a F23 key, ignore it.
267          */
268         if (usage->hid == 0x00070072)
269                 return -1;
270
271         return 0;
272 }
273
274 static int lenovo_input_mapping(struct hid_device *hdev,
275                 struct hid_input *hi, struct hid_field *field,
276                 struct hid_usage *usage, unsigned long **bit, int *max)
277 {
278         switch (hdev->product) {
279         case USB_DEVICE_ID_LENOVO_TPKBD:
280                 return lenovo_input_mapping_tpkbd(hdev, hi, field,
281                                                         usage, bit, max);
282         case USB_DEVICE_ID_LENOVO_CUSBKBD:
283         case USB_DEVICE_ID_LENOVO_CBTKBD:
284                 return lenovo_input_mapping_cptkbd(hdev, hi, field,
285                                                         usage, bit, max);
286         case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
287         case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
288         case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
289         case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
290         case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
291         case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
292                 return lenovo_input_mapping_scrollpoint(hdev, hi, field,
293                                                         usage, bit, max);
294         case USB_DEVICE_ID_LENOVO_TP10UBKBD:
295                 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
296                                                                usage, bit, max);
297         default:
298                 return 0;
299         }
300 }
301
302 #undef map_key_clear
303
304 /* Send a config command to the keyboard */
305 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
306                         unsigned char byte2, unsigned char byte3)
307 {
308         int ret;
309         unsigned char *buf;
310
311         buf = kzalloc(3, GFP_KERNEL);
312         if (!buf)
313                 return -ENOMEM;
314
315         buf[0] = 0x18;
316         buf[1] = byte2;
317         buf[2] = byte3;
318
319         switch (hdev->product) {
320         case USB_DEVICE_ID_LENOVO_CUSBKBD:
321                 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
322                                         HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
323                 break;
324         case USB_DEVICE_ID_LENOVO_CBTKBD:
325                 ret = hid_hw_output_report(hdev, buf, 3);
326                 break;
327         default:
328                 ret = -EINVAL;
329                 break;
330         }
331
332         kfree(buf);
333
334         return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
335 }
336
337 static void lenovo_features_set_cptkbd(struct hid_device *hdev)
338 {
339         int ret;
340         struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
341
342         ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
343         if (ret)
344                 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
345
346         ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
347         if (ret)
348                 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
349 }
350
351 static ssize_t attr_fn_lock_show(struct device *dev,
352                 struct device_attribute *attr,
353                 char *buf)
354 {
355         struct hid_device *hdev = to_hid_device(dev);
356         struct lenovo_drvdata *data = hid_get_drvdata(hdev);
357
358         return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
359 }
360
361 static ssize_t attr_fn_lock_store(struct device *dev,
362                 struct device_attribute *attr,
363                 const char *buf,
364                 size_t count)
365 {
366         struct hid_device *hdev = to_hid_device(dev);
367         struct lenovo_drvdata *data = hid_get_drvdata(hdev);
368         int value, ret;
369
370         if (kstrtoint(buf, 10, &value))
371                 return -EINVAL;
372         if (value < 0 || value > 1)
373                 return -EINVAL;
374
375         data->fn_lock = !!value;
376
377         switch (hdev->product) {
378         case USB_DEVICE_ID_LENOVO_CUSBKBD:
379         case USB_DEVICE_ID_LENOVO_CBTKBD:
380                 lenovo_features_set_cptkbd(hdev);
381                 break;
382         case USB_DEVICE_ID_LENOVO_TP10UBKBD:
383                 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
384                 if (ret)
385                         return ret;
386                 break;
387         }
388
389         return count;
390 }
391
392 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
393                 struct device_attribute *attr,
394                 char *buf)
395 {
396         struct hid_device *hdev = to_hid_device(dev);
397         struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
398
399         return snprintf(buf, PAGE_SIZE, "%u\n",
400                 cptkbd_data->sensitivity);
401 }
402
403 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
404                 struct device_attribute *attr,
405                 const char *buf,
406                 size_t count)
407 {
408         struct hid_device *hdev = to_hid_device(dev);
409         struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
410         int value;
411
412         if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
413                 return -EINVAL;
414
415         cptkbd_data->sensitivity = value;
416         lenovo_features_set_cptkbd(hdev);
417
418         return count;
419 }
420
421 static ssize_t attr_middleclick_workaround_show_cptkbd(struct device *dev,
422                 struct device_attribute *attr,
423                 char *buf)
424 {
425         struct hid_device *hdev = to_hid_device(dev);
426         struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
427
428         return snprintf(buf, PAGE_SIZE, "%u\n",
429                 cptkbd_data->middleclick_workaround_cptkbd);
430 }
431
432 static ssize_t attr_middleclick_workaround_store_cptkbd(struct device *dev,
433                 struct device_attribute *attr,
434                 const char *buf,
435                 size_t count)
436 {
437         struct hid_device *hdev = to_hid_device(dev);
438         struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
439         int value;
440
441         if (kstrtoint(buf, 10, &value))
442                 return -EINVAL;
443         if (value < 0 || value > 1)
444                 return -EINVAL;
445
446         cptkbd_data->middleclick_workaround_cptkbd = !!value;
447
448         return count;
449 }
450
451
452 static struct device_attribute dev_attr_fn_lock =
453         __ATTR(fn_lock, S_IWUSR | S_IRUGO,
454                         attr_fn_lock_show,
455                         attr_fn_lock_store);
456
457 static struct device_attribute dev_attr_sensitivity_cptkbd =
458         __ATTR(sensitivity, S_IWUSR | S_IRUGO,
459                         attr_sensitivity_show_cptkbd,
460                         attr_sensitivity_store_cptkbd);
461
462 static struct device_attribute dev_attr_middleclick_workaround_cptkbd =
463         __ATTR(middleclick_workaround, S_IWUSR | S_IRUGO,
464                         attr_middleclick_workaround_show_cptkbd,
465                         attr_middleclick_workaround_store_cptkbd);
466
467
468 static struct attribute *lenovo_attributes_cptkbd[] = {
469         &dev_attr_fn_lock.attr,
470         &dev_attr_sensitivity_cptkbd.attr,
471         &dev_attr_middleclick_workaround_cptkbd.attr,
472         NULL
473 };
474
475 static const struct attribute_group lenovo_attr_group_cptkbd = {
476         .attrs = lenovo_attributes_cptkbd,
477 };
478
479 static int lenovo_raw_event(struct hid_device *hdev,
480                         struct hid_report *report, u8 *data, int size)
481 {
482         /*
483          * Compact USB keyboard's Fn-F12 report holds down many other keys, and
484          * its own key is outside the usage page range. Remove extra
485          * keypresses and remap to inside usage page.
486          */
487         if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
488                         && size == 3
489                         && data[0] == 0x15
490                         && data[1] == 0x94
491                         && data[2] == 0x01)) {
492                 data[1] = 0x00;
493                 data[2] = 0x01;
494         }
495
496         return 0;
497 }
498
499 static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
500                 struct hid_field *field, struct hid_usage *usage, __s32 value)
501 {
502         struct lenovo_drvdata *data = hid_get_drvdata(hdev);
503
504         if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
505                 /*
506                  * The user has toggled the Fn-lock state. Toggle our own
507                  * cached value of it and sync our value to the keyboard to
508                  * ensure things are in sync (the sycning should be a no-op).
509                  */
510                 data->fn_lock = !data->fn_lock;
511                 schedule_work(&data->fn_lock_sync_work);
512         }
513
514         return 0;
515 }
516
517 static int lenovo_event_cptkbd(struct hid_device *hdev,
518                 struct hid_field *field, struct hid_usage *usage, __s32 value)
519 {
520         struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
521
522         if (cptkbd_data->middleclick_workaround_cptkbd) {
523                 /* "wheel" scroll events */
524                 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
525                                 usage->code == REL_HWHEEL)) {
526                         /* Scroll events disable middle-click event */
527                         cptkbd_data->middlebutton_state = 2;
528                         return 0;
529                 }
530
531                 /* Middle click events */
532                 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
533                         if (value == 1) {
534                                 cptkbd_data->middlebutton_state = 1;
535                         } else if (value == 0) {
536                                 if (cptkbd_data->middlebutton_state == 1) {
537                                         /* No scrolling inbetween, send middle-click */
538                                         input_event(field->hidinput->input,
539                                                 EV_KEY, BTN_MIDDLE, 1);
540                                         input_sync(field->hidinput->input);
541                                         input_event(field->hidinput->input,
542                                                 EV_KEY, BTN_MIDDLE, 0);
543                                         input_sync(field->hidinput->input);
544                                 }
545                                 cptkbd_data->middlebutton_state = 0;
546                         }
547                         return 1;
548                 }
549         }
550
551         return 0;
552 }
553
554 static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
555                 struct hid_usage *usage, __s32 value)
556 {
557         if (!hid_get_drvdata(hdev))
558                 return 0;
559
560         switch (hdev->product) {
561         case USB_DEVICE_ID_LENOVO_CUSBKBD:
562         case USB_DEVICE_ID_LENOVO_CBTKBD:
563                 return lenovo_event_cptkbd(hdev, field, usage, value);
564         case USB_DEVICE_ID_LENOVO_TP10UBKBD:
565                 return lenovo_event_tp10ubkbd(hdev, field, usage, value);
566         default:
567                 return 0;
568         }
569 }
570
571 static int lenovo_features_set_tpkbd(struct hid_device *hdev)
572 {
573         struct hid_report *report;
574         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
575
576         report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
577
578         report->field[0]->value[0]  = data_pointer->press_to_select   ? 0x01 : 0x02;
579         report->field[0]->value[0] |= data_pointer->dragging          ? 0x04 : 0x08;
580         report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
581         report->field[0]->value[0] |= data_pointer->select_right      ? 0x80 : 0x40;
582         report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
583         report->field[2]->value[0] = data_pointer->sensitivity;
584         report->field[3]->value[0] = data_pointer->press_speed;
585
586         hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
587         return 0;
588 }
589
590 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
591                 struct device_attribute *attr,
592                 char *buf)
593 {
594         struct hid_device *hdev = to_hid_device(dev);
595         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
596
597         return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
598 }
599
600 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
601                 struct device_attribute *attr,
602                 const char *buf,
603                 size_t count)
604 {
605         struct hid_device *hdev = to_hid_device(dev);
606         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
607         int value;
608
609         if (kstrtoint(buf, 10, &value))
610                 return -EINVAL;
611         if (value < 0 || value > 1)
612                 return -EINVAL;
613
614         data_pointer->press_to_select = value;
615         lenovo_features_set_tpkbd(hdev);
616
617         return count;
618 }
619
620 static ssize_t attr_dragging_show_tpkbd(struct device *dev,
621                 struct device_attribute *attr,
622                 char *buf)
623 {
624         struct hid_device *hdev = to_hid_device(dev);
625         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
626
627         return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
628 }
629
630 static ssize_t attr_dragging_store_tpkbd(struct device *dev,
631                 struct device_attribute *attr,
632                 const char *buf,
633                 size_t count)
634 {
635         struct hid_device *hdev = to_hid_device(dev);
636         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
637         int value;
638
639         if (kstrtoint(buf, 10, &value))
640                 return -EINVAL;
641         if (value < 0 || value > 1)
642                 return -EINVAL;
643
644         data_pointer->dragging = value;
645         lenovo_features_set_tpkbd(hdev);
646
647         return count;
648 }
649
650 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
651                 struct device_attribute *attr,
652                 char *buf)
653 {
654         struct hid_device *hdev = to_hid_device(dev);
655         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
656
657         return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
658 }
659
660 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
661                 struct device_attribute *attr,
662                 const char *buf,
663                 size_t count)
664 {
665         struct hid_device *hdev = to_hid_device(dev);
666         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
667         int value;
668
669         if (kstrtoint(buf, 10, &value))
670                 return -EINVAL;
671         if (value < 0 || value > 1)
672                 return -EINVAL;
673
674         data_pointer->release_to_select = value;
675         lenovo_features_set_tpkbd(hdev);
676
677         return count;
678 }
679
680 static ssize_t attr_select_right_show_tpkbd(struct device *dev,
681                 struct device_attribute *attr,
682                 char *buf)
683 {
684         struct hid_device *hdev = to_hid_device(dev);
685         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
686
687         return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
688 }
689
690 static ssize_t attr_select_right_store_tpkbd(struct device *dev,
691                 struct device_attribute *attr,
692                 const char *buf,
693                 size_t count)
694 {
695         struct hid_device *hdev = to_hid_device(dev);
696         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
697         int value;
698
699         if (kstrtoint(buf, 10, &value))
700                 return -EINVAL;
701         if (value < 0 || value > 1)
702                 return -EINVAL;
703
704         data_pointer->select_right = value;
705         lenovo_features_set_tpkbd(hdev);
706
707         return count;
708 }
709
710 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
711                 struct device_attribute *attr,
712                 char *buf)
713 {
714         struct hid_device *hdev = to_hid_device(dev);
715         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
716
717         return snprintf(buf, PAGE_SIZE, "%u\n",
718                 data_pointer->sensitivity);
719 }
720
721 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
722                 struct device_attribute *attr,
723                 const char *buf,
724                 size_t count)
725 {
726         struct hid_device *hdev = to_hid_device(dev);
727         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
728         int value;
729
730         if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
731                 return -EINVAL;
732
733         data_pointer->sensitivity = value;
734         lenovo_features_set_tpkbd(hdev);
735
736         return count;
737 }
738
739 static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
740                 struct device_attribute *attr,
741                 char *buf)
742 {
743         struct hid_device *hdev = to_hid_device(dev);
744         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
745
746         return snprintf(buf, PAGE_SIZE, "%u\n",
747                 data_pointer->press_speed);
748 }
749
750 static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
751                 struct device_attribute *attr,
752                 const char *buf,
753                 size_t count)
754 {
755         struct hid_device *hdev = to_hid_device(dev);
756         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
757         int value;
758
759         if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
760                 return -EINVAL;
761
762         data_pointer->press_speed = value;
763         lenovo_features_set_tpkbd(hdev);
764
765         return count;
766 }
767
768 static struct device_attribute dev_attr_press_to_select_tpkbd =
769         __ATTR(press_to_select, S_IWUSR | S_IRUGO,
770                         attr_press_to_select_show_tpkbd,
771                         attr_press_to_select_store_tpkbd);
772
773 static struct device_attribute dev_attr_dragging_tpkbd =
774         __ATTR(dragging, S_IWUSR | S_IRUGO,
775                         attr_dragging_show_tpkbd,
776                         attr_dragging_store_tpkbd);
777
778 static struct device_attribute dev_attr_release_to_select_tpkbd =
779         __ATTR(release_to_select, S_IWUSR | S_IRUGO,
780                         attr_release_to_select_show_tpkbd,
781                         attr_release_to_select_store_tpkbd);
782
783 static struct device_attribute dev_attr_select_right_tpkbd =
784         __ATTR(select_right, S_IWUSR | S_IRUGO,
785                         attr_select_right_show_tpkbd,
786                         attr_select_right_store_tpkbd);
787
788 static struct device_attribute dev_attr_sensitivity_tpkbd =
789         __ATTR(sensitivity, S_IWUSR | S_IRUGO,
790                         attr_sensitivity_show_tpkbd,
791                         attr_sensitivity_store_tpkbd);
792
793 static struct device_attribute dev_attr_press_speed_tpkbd =
794         __ATTR(press_speed, S_IWUSR | S_IRUGO,
795                         attr_press_speed_show_tpkbd,
796                         attr_press_speed_store_tpkbd);
797
798 static struct attribute *lenovo_attributes_tpkbd[] = {
799         &dev_attr_press_to_select_tpkbd.attr,
800         &dev_attr_dragging_tpkbd.attr,
801         &dev_attr_release_to_select_tpkbd.attr,
802         &dev_attr_select_right_tpkbd.attr,
803         &dev_attr_sensitivity_tpkbd.attr,
804         &dev_attr_press_speed_tpkbd.attr,
805         NULL
806 };
807
808 static const struct attribute_group lenovo_attr_group_tpkbd = {
809         .attrs = lenovo_attributes_tpkbd,
810 };
811
812 static void lenovo_led_set_tpkbd(struct hid_device *hdev)
813 {
814         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
815         struct hid_report *report;
816
817         report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
818         report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
819         report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
820         hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
821 }
822
823 static enum led_brightness lenovo_led_brightness_get(
824                         struct led_classdev *led_cdev)
825 {
826         struct device *dev = led_cdev->dev->parent;
827         struct hid_device *hdev = to_hid_device(dev);
828         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
829         int led_nr = 0;
830
831         if (led_cdev == &data_pointer->led_micmute)
832                 led_nr = 1;
833
834         return data_pointer->led_state & (1 << led_nr)
835                                 ? LED_FULL
836                                 : LED_OFF;
837 }
838
839 static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
840                         enum led_brightness value)
841 {
842         struct device *dev = led_cdev->dev->parent;
843         struct hid_device *hdev = to_hid_device(dev);
844         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
845         u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
846         int led_nr = 0;
847         int ret = 0;
848
849         if (led_cdev == &data_pointer->led_micmute)
850                 led_nr = 1;
851
852         if (value == LED_OFF)
853                 data_pointer->led_state &= ~(1 << led_nr);
854         else
855                 data_pointer->led_state |= 1 << led_nr;
856
857         switch (hdev->product) {
858         case USB_DEVICE_ID_LENOVO_TPKBD:
859                 lenovo_led_set_tpkbd(hdev);
860                 break;
861         case USB_DEVICE_ID_LENOVO_TP10UBKBD:
862                 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
863                 break;
864         }
865
866         return ret;
867 }
868
869 static int lenovo_register_leds(struct hid_device *hdev)
870 {
871         struct lenovo_drvdata *data = hid_get_drvdata(hdev);
872         size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
873         char *name_mute, *name_micm;
874         int ret;
875
876         name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
877         name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
878         if (name_mute == NULL || name_micm == NULL) {
879                 hid_err(hdev, "Could not allocate memory for led data\n");
880                 return -ENOMEM;
881         }
882         snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
883         snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
884
885         data->led_mute.name = name_mute;
886         data->led_mute.brightness_get = lenovo_led_brightness_get;
887         data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
888         data->led_mute.flags = LED_HW_PLUGGABLE;
889         data->led_mute.dev = &hdev->dev;
890         ret = led_classdev_register(&hdev->dev, &data->led_mute);
891         if (ret < 0)
892                 return ret;
893
894         data->led_micmute.name = name_micm;
895         data->led_micmute.brightness_get = lenovo_led_brightness_get;
896         data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
897         data->led_micmute.flags = LED_HW_PLUGGABLE;
898         data->led_micmute.dev = &hdev->dev;
899         ret = led_classdev_register(&hdev->dev, &data->led_micmute);
900         if (ret < 0) {
901                 led_classdev_unregister(&data->led_mute);
902                 return ret;
903         }
904
905         return 0;
906 }
907
908 static int lenovo_probe_tpkbd(struct hid_device *hdev)
909 {
910         struct lenovo_drvdata *data_pointer;
911         int i, ret;
912
913         /*
914          * Only register extra settings against subdevice where input_mapping
915          * set drvdata to 1, i.e. the trackpoint.
916          */
917         if (!hid_get_drvdata(hdev))
918                 return 0;
919
920         hid_set_drvdata(hdev, NULL);
921
922         /* Validate required reports. */
923         for (i = 0; i < 4; i++) {
924                 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
925                         return -ENODEV;
926         }
927         if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
928                 return -ENODEV;
929
930         ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
931         if (ret)
932                 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
933
934         data_pointer = devm_kzalloc(&hdev->dev,
935                                     sizeof(struct lenovo_drvdata),
936                                     GFP_KERNEL);
937         if (data_pointer == NULL) {
938                 hid_err(hdev, "Could not allocate memory for driver data\n");
939                 ret = -ENOMEM;
940                 goto err;
941         }
942
943         // set same default values as windows driver
944         data_pointer->sensitivity = 0xa0;
945         data_pointer->press_speed = 0x38;
946
947         hid_set_drvdata(hdev, data_pointer);
948
949         ret = lenovo_register_leds(hdev);
950         if (ret)
951                 goto err;
952
953         lenovo_features_set_tpkbd(hdev);
954
955         return 0;
956 err:
957         sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
958         return ret;
959 }
960
961 static int lenovo_probe_cptkbd(struct hid_device *hdev)
962 {
963         int ret;
964         struct lenovo_drvdata *cptkbd_data;
965
966         /* All the custom action happens on the USBMOUSE device for USB */
967         if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
968                         && hdev->type != HID_TYPE_USBMOUSE) {
969                 hid_dbg(hdev, "Ignoring keyboard half of device\n");
970                 return 0;
971         }
972
973         cptkbd_data = devm_kzalloc(&hdev->dev,
974                                         sizeof(*cptkbd_data),
975                                         GFP_KERNEL);
976         if (cptkbd_data == NULL) {
977                 hid_err(hdev, "can't alloc keyboard descriptor\n");
978                 return -ENOMEM;
979         }
980         hid_set_drvdata(hdev, cptkbd_data);
981
982         /*
983          * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
984          * regular keys
985          */
986         ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
987         if (ret)
988                 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
989
990         /* Switch middle button to native mode */
991         ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
992         if (ret)
993                 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
994
995         /* Set keyboard settings to known state */
996         cptkbd_data->middlebutton_state = 0;
997         cptkbd_data->fn_lock = true;
998         cptkbd_data->sensitivity = 0x05;
999         cptkbd_data->middleclick_workaround_cptkbd = true;
1000         lenovo_features_set_cptkbd(hdev);
1001
1002         ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
1003         if (ret)
1004                 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1005
1006         return 0;
1007 }
1008
1009 static struct attribute *lenovo_attributes_tp10ubkbd[] = {
1010         &dev_attr_fn_lock.attr,
1011         NULL
1012 };
1013
1014 static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
1015         .attrs = lenovo_attributes_tp10ubkbd,
1016 };
1017
1018 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
1019 {
1020         struct lenovo_drvdata *data;
1021         int ret;
1022
1023         /* All the custom action happens on the USBMOUSE device for USB */
1024         if (hdev->type != HID_TYPE_USBMOUSE)
1025                 return 0;
1026
1027         data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
1028         if (!data)
1029                 return -ENOMEM;
1030
1031         mutex_init(&data->led_report_mutex);
1032         INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
1033         data->hdev = hdev;
1034
1035         hid_set_drvdata(hdev, data);
1036
1037         /*
1038          * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
1039          * We cannot read the state, only set it, so we force it to on here
1040          * (which should be a no-op) to make sure that our state matches the
1041          * keyboard's FN-lock state. This is the same as what Windows does.
1042          */
1043         data->fn_lock = true;
1044         lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
1045
1046         ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1047         if (ret)
1048                 return ret;
1049
1050         ret = lenovo_register_leds(hdev);
1051         if (ret)
1052                 goto err;
1053
1054         return 0;
1055 err:
1056         sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1057         return ret;
1058 }
1059
1060 static int lenovo_probe(struct hid_device *hdev,
1061                 const struct hid_device_id *id)
1062 {
1063         int ret;
1064
1065         ret = hid_parse(hdev);
1066         if (ret) {
1067                 hid_err(hdev, "hid_parse failed\n");
1068                 goto err;
1069         }
1070
1071         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1072         if (ret) {
1073                 hid_err(hdev, "hid_hw_start failed\n");
1074                 goto err;
1075         }
1076
1077         switch (hdev->product) {
1078         case USB_DEVICE_ID_LENOVO_TPKBD:
1079                 ret = lenovo_probe_tpkbd(hdev);
1080                 break;
1081         case USB_DEVICE_ID_LENOVO_CUSBKBD:
1082         case USB_DEVICE_ID_LENOVO_CBTKBD:
1083                 ret = lenovo_probe_cptkbd(hdev);
1084                 break;
1085         case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1086                 ret = lenovo_probe_tp10ubkbd(hdev);
1087                 break;
1088         default:
1089                 ret = 0;
1090                 break;
1091         }
1092         if (ret)
1093                 goto err_hid;
1094
1095         return 0;
1096 err_hid:
1097         hid_hw_stop(hdev);
1098 err:
1099         return ret;
1100 }
1101
1102 static void lenovo_remove_tpkbd(struct hid_device *hdev)
1103 {
1104         struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1105
1106         /*
1107          * Only the trackpoint half of the keyboard has drvdata and stuff that
1108          * needs unregistering.
1109          */
1110         if (data_pointer == NULL)
1111                 return;
1112
1113         sysfs_remove_group(&hdev->dev.kobj,
1114                         &lenovo_attr_group_tpkbd);
1115
1116         led_classdev_unregister(&data_pointer->led_micmute);
1117         led_classdev_unregister(&data_pointer->led_mute);
1118 }
1119
1120 static void lenovo_remove_cptkbd(struct hid_device *hdev)
1121 {
1122         sysfs_remove_group(&hdev->dev.kobj,
1123                         &lenovo_attr_group_cptkbd);
1124 }
1125
1126 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1127 {
1128         struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1129
1130         if (data == NULL)
1131                 return;
1132
1133         led_classdev_unregister(&data->led_micmute);
1134         led_classdev_unregister(&data->led_mute);
1135
1136         sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1137         cancel_work_sync(&data->fn_lock_sync_work);
1138 }
1139
1140 static void lenovo_remove(struct hid_device *hdev)
1141 {
1142         switch (hdev->product) {
1143         case USB_DEVICE_ID_LENOVO_TPKBD:
1144                 lenovo_remove_tpkbd(hdev);
1145                 break;
1146         case USB_DEVICE_ID_LENOVO_CUSBKBD:
1147         case USB_DEVICE_ID_LENOVO_CBTKBD:
1148                 lenovo_remove_cptkbd(hdev);
1149                 break;
1150         case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1151                 lenovo_remove_tp10ubkbd(hdev);
1152                 break;
1153         }
1154
1155         hid_hw_stop(hdev);
1156 }
1157
1158 static int lenovo_input_configured(struct hid_device *hdev,
1159                 struct hid_input *hi)
1160 {
1161         switch (hdev->product) {
1162                 case USB_DEVICE_ID_LENOVO_TPKBD:
1163                 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1164                 case USB_DEVICE_ID_LENOVO_CBTKBD:
1165                         if (test_bit(EV_REL, hi->input->evbit)) {
1166                                 /* set only for trackpoint device */
1167                                 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1168                                 __set_bit(INPUT_PROP_POINTING_STICK,
1169                                                 hi->input->propbit);
1170                         }
1171                         break;
1172         }
1173
1174         return 0;
1175 }
1176
1177
1178 static const struct hid_device_id lenovo_devices[] = {
1179         { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1180         { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1181         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1182         { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1183         { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1184         { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1185         { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1186         { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1187         { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1188         { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1189         { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1190         { }
1191 };
1192
1193 MODULE_DEVICE_TABLE(hid, lenovo_devices);
1194
1195 static struct hid_driver lenovo_driver = {
1196         .name = "lenovo",
1197         .id_table = lenovo_devices,
1198         .input_configured = lenovo_input_configured,
1199         .input_mapping = lenovo_input_mapping,
1200         .probe = lenovo_probe,
1201         .remove = lenovo_remove,
1202         .raw_event = lenovo_raw_event,
1203         .event = lenovo_event,
1204         .report_fixup = lenovo_report_fixup,
1205 };
1206 module_hid_driver(lenovo_driver);
1207
1208 MODULE_LICENSE("GPL");