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