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 * - ThinkPad TrackPoint Keyboard II USB/Bluetooth (cptkbd/tpIIkbd)
8 *
9 * Copyright (c) 2012 Bernhard Seibold
10 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
11 *
12 * Linux IBM/Lenovo Scrollpoint mouse driver:
13 * - IBM Scrollpoint III
14 * - IBM Scrollpoint Pro
15 * - IBM Scrollpoint Optical
16 * - IBM Scrollpoint Optical 800dpi
17 * - IBM Scrollpoint Optical 800dpi Pro
18 * - Lenovo Scrollpoint Optical
19 *
20 * Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
21 * Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
22 */
23
24 /*
25 */
26
27 #include <linux/module.h>
28 #include <linux/sysfs.h>
29 #include <linux/device.h>
30 #include <linux/hid.h>
31 #include <linux/input.h>
32 #include <linux/leds.h>
33 #include <linux/workqueue.h>
34
35 #include "hid-ids.h"
36
37 /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */
38 #define LENOVO_KEY_MICMUTE KEY_F20
39
40 /* HID raw events for ThinkPad X12 Tabs*/
41 #define TP_X12_RAW_HOTKEY_FN_F4 0x00020003
42 #define TP_X12_RAW_HOTKEY_FN_F8 0x38001003
43 #define TP_X12_RAW_HOTKEY_FN_F10 0x00000803
44 #define TP_X12_RAW_HOTKEY_FN_F12 0x00000403
45 #define TP_X12_RAW_HOTKEY_FN_SPACE 0x18001003
46
47 struct lenovo_drvdata {
48 u8 led_report[3]; /* Must be first for proper alignment */
49 int led_state;
50 struct mutex led_report_mutex;
51 struct led_classdev led_mute;
52 struct led_classdev led_micmute;
53 struct work_struct fn_lock_sync_work;
54 struct hid_device *hdev;
55 int press_to_select;
56 int dragging;
57 int release_to_select;
58 int select_right;
59 int sensitivity;
60 int press_speed;
61 /* 0: Up
62 * 1: Down (undecided)
63 * 2: Scrolling
64 */
65 u8 middlebutton_state;
66 bool fn_lock;
67 bool middleclick_workaround_cptkbd;
68 };
69
70 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
71
72 #define TP10UBKBD_LED_OUTPUT_REPORT 9
73
74 #define TP10UBKBD_FN_LOCK_LED 0x54
75 #define TP10UBKBD_MUTE_LED 0x64
76 #define TP10UBKBD_MICMUTE_LED 0x74
77
78 #define TP10UBKBD_LED_OFF 1
79 #define TP10UBKBD_LED_ON 2
80
81 /* Function to report raw_events as key events*/
report_key_event(struct input_dev * input,int keycode)82 static inline void report_key_event(struct input_dev *input, int keycode)
83 {
84 input_report_key(input, keycode, 1);
85 input_report_key(input, keycode, 0);
86 input_sync(input);
87 }
88
lenovo_led_set_tp10ubkbd(struct hid_device * hdev,u8 led_code,enum led_brightness value)89 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
90 enum led_brightness value)
91 {
92 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
93 int ret;
94
95 mutex_lock(&data->led_report_mutex);
96
97 data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT;
98 data->led_report[1] = led_code;
99 data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF;
100 ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3,
101 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
102 if (ret != 3) {
103 if (ret != -ENODEV)
104 hid_err(hdev, "Set LED output report error: %d\n", ret);
105
106 ret = ret < 0 ? ret : -EIO;
107 } else {
108 ret = 0;
109 }
110
111 mutex_unlock(&data->led_report_mutex);
112
113 return ret;
114 }
115
lenovo_tp10ubkbd_sync_fn_lock(struct work_struct * work)116 static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
117 {
118 struct lenovo_drvdata *data =
119 container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
120
121 lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
122 data->fn_lock);
123 }
124
125 static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
126 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */
127 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
128 0xa1, 0x01, /* Collection (Application) */
129 0x85, 0x04, /* Report ID (4) */
130 0x19, 0x00, /* Usage Minimum (0) */
131 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */
132 };
133
134 /* Broken ThinkPad TrackPoint II collection (Bluetooth mode) */
135 static const __u8 lenovo_tpIIbtkbd_need_fixup_collection[] = {
136 0x06, 0x00, 0xFF, /* Usage Page (Vendor Defined 0xFF00) */
137 0x09, 0x01, /* Usage (0x01) */
138 0xA1, 0x01, /* Collection (Application) */
139 0x85, 0x05, /* Report ID (5) */
140 0x1A, 0xF1, 0x00, /* Usage Minimum (0xF1) */
141 0x2A, 0xFC, 0x00, /* Usage Maximum (0xFC) */
142 0x15, 0x00, /* Logical Minimum (0) */
143 0x25, 0x01, /* Logical Maximum (1) */
144 0x75, 0x01, /* Report Size (1) */
145 0x95, 0x0D, /* Report Count (13) */
146 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */
147 0x95, 0x03, /* Report Count (3) */
148 0x81, 0x01, /* Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */
149 };
150
151 static const __u8 lenovo_yoga7x_kbd_need_fixup_collection[] = {
152 0x15, 0x00, // Logical Minimum (0)
153 0x25, 0x65, // Logical Maximum (101)
154 0x05, 0x07, // Usage Page (Keyboard)
155 0x19, 0x00, // Usage Minimum (0)
156 0x29, 0xDD, // Usage Maximum (221)
157 };
158
lenovo_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)159 static const __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
160 unsigned int *rsize)
161 {
162 switch (hdev->product) {
163 case USB_DEVICE_ID_LENOVO_TPPRODOCK:
164 /* the fixups that need to be done:
165 * - get a reasonable usage max for the vendor collection
166 * 0x8801 from the report ID 4
167 */
168 if (*rsize >= 153 &&
169 memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
170 sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
171 rdesc[151] = 0x01;
172 rdesc[152] = 0x00;
173 }
174 break;
175 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
176 if (*rsize >= 263 &&
177 memcmp(&rdesc[234], lenovo_tpIIbtkbd_need_fixup_collection,
178 sizeof(lenovo_tpIIbtkbd_need_fixup_collection)) == 0) {
179 rdesc[244] = 0x00; /* usage minimum = 0x00 */
180 rdesc[247] = 0xff; /* usage maximum = 0xff */
181 rdesc[252] = 0xff; /* logical maximum = 0xff */
182 rdesc[254] = 0x08; /* report size = 0x08 */
183 rdesc[256] = 0x01; /* report count = 0x01 */
184 rdesc[258] = 0x00; /* input = 0x00 */
185 rdesc[260] = 0x01; /* report count (2) = 0x01 */
186 }
187 break;
188 case I2C_DEVICE_ID_ITE_LENOVO_YOGA_SLIM_7X_KEYBOARD:
189 if (*rsize == 176 &&
190 memcmp(&rdesc[52], lenovo_yoga7x_kbd_need_fixup_collection,
191 sizeof(lenovo_yoga7x_kbd_need_fixup_collection)) == 0) {
192 rdesc[55] = rdesc[61]; // logical maximum = usage maximum
193 }
194 break;
195 }
196 return rdesc;
197 }
198
lenovo_input_mapping_tpkbd(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)199 static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
200 struct hid_input *hi, struct hid_field *field,
201 struct hid_usage *usage, unsigned long **bit, int *max)
202 {
203 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
204 /* This sub-device contains trackpoint, mark it */
205 hid_set_drvdata(hdev, (void *)1);
206 map_key_clear(LENOVO_KEY_MICMUTE);
207 return 1;
208 }
209 return 0;
210 }
211
lenovo_input_mapping_cptkbd(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)212 static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
213 struct hid_input *hi, struct hid_field *field,
214 struct hid_usage *usage, unsigned long **bit, int *max)
215 {
216 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
217 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
218 (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
219 switch (usage->hid & HID_USAGE) {
220 case 0x00f1: /* Fn-F4: Mic mute */
221 map_key_clear(LENOVO_KEY_MICMUTE);
222 return 1;
223 case 0x00f2: /* Fn-F5: Brightness down */
224 map_key_clear(KEY_BRIGHTNESSDOWN);
225 return 1;
226 case 0x00f3: /* Fn-F6: Brightness up */
227 map_key_clear(KEY_BRIGHTNESSUP);
228 return 1;
229 case 0x00f4: /* Fn-F7: External display (projector) */
230 map_key_clear(KEY_SWITCHVIDEOMODE);
231 return 1;
232 case 0x00f5: /* Fn-F8: Wireless */
233 map_key_clear(KEY_WLAN);
234 return 1;
235 case 0x00f6: /* Fn-F9: Control panel */
236 map_key_clear(KEY_CONFIG);
237 return 1;
238 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
239 map_key_clear(KEY_SCALE);
240 return 1;
241 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
242 /* NB: This mapping is invented in raw_event below */
243 map_key_clear(KEY_FILE);
244 return 1;
245 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
246 map_key_clear(KEY_FN_ESC);
247 return 1;
248 case 0x00fb: /* Middle mouse button (in native mode) */
249 map_key_clear(BTN_MIDDLE);
250 return 1;
251 }
252 }
253
254 /* Compatibility middle/wheel mappings should be ignored */
255 if (usage->hid == HID_GD_WHEEL)
256 return -1;
257 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
258 (usage->hid & HID_USAGE) == 0x003)
259 return -1;
260 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
261 (usage->hid & HID_USAGE) == 0x238)
262 return -1;
263
264 /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
265 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
266 (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
267 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
268 field->logical_minimum = -127;
269 field->logical_maximum = 127;
270
271 switch (usage->hid & HID_USAGE) {
272 case 0x0000:
273 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
274 return 1;
275 case 0x0001:
276 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
277 return 1;
278 default:
279 return -1;
280 }
281 }
282
283 return 0;
284 }
285
lenovo_input_mapping_tpIIkbd(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)286 static int lenovo_input_mapping_tpIIkbd(struct hid_device *hdev,
287 struct hid_input *hi, struct hid_field *field,
288 struct hid_usage *usage, unsigned long **bit, int *max)
289 {
290 /*
291 * 0xff0a0000 = USB, HID_UP_MSVENDOR = BT.
292 *
293 * In BT mode, there are two HID_UP_MSVENDOR pages.
294 * Use only the page that contains report ID == 5.
295 */
296 if (((usage->hid & HID_USAGE_PAGE) == 0xff0a0000 ||
297 (usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) &&
298 field->report->id == 5) {
299 switch (usage->hid & HID_USAGE) {
300 case 0x00bb: /* Fn-F4: Mic mute */
301 map_key_clear(LENOVO_KEY_MICMUTE);
302 return 1;
303 case 0x00c3: /* Fn-F5: Brightness down */
304 map_key_clear(KEY_BRIGHTNESSDOWN);
305 return 1;
306 case 0x00c4: /* Fn-F6: Brightness up */
307 map_key_clear(KEY_BRIGHTNESSUP);
308 return 1;
309 case 0x00c1: /* Fn-F8: Notification center */
310 map_key_clear(KEY_NOTIFICATION_CENTER);
311 return 1;
312 case 0x00bc: /* Fn-F9: Control panel */
313 map_key_clear(KEY_CONFIG);
314 return 1;
315 case 0x00b6: /* Fn-F10: Bluetooth */
316 map_key_clear(KEY_BLUETOOTH);
317 return 1;
318 case 0x00b7: /* Fn-F11: Keyboard config */
319 map_key_clear(KEY_KEYBOARD);
320 return 1;
321 case 0x00b8: /* Fn-F12: User function */
322 map_key_clear(KEY_PROG1);
323 return 1;
324 case 0x00b9: /* Fn-PrtSc: Snipping tool */
325 map_key_clear(KEY_SELECTIVE_SCREENSHOT);
326 return 1;
327 case 0x00b5: /* Fn-Esc: Fn-lock toggle */
328 map_key_clear(KEY_FN_ESC);
329 return 1;
330 }
331 }
332
333 if ((usage->hid & HID_USAGE_PAGE) == 0xffa00000) {
334 switch (usage->hid & HID_USAGE) {
335 case 0x00fb: /* Middle mouse (in native USB mode) */
336 map_key_clear(BTN_MIDDLE);
337 return 1;
338 }
339 }
340
341 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR &&
342 field->report->id == 21) {
343 switch (usage->hid & HID_USAGE) {
344 case 0x0004: /* Middle mouse (in native Bluetooth mode) */
345 map_key_clear(BTN_MIDDLE);
346 return 1;
347 }
348 }
349
350 /* Compatibility middle/wheel mappings should be ignored */
351 if (usage->hid == HID_GD_WHEEL)
352 return -1;
353 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
354 (usage->hid & HID_USAGE) == 0x003)
355 return -1;
356 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
357 (usage->hid & HID_USAGE) == 0x238)
358 return -1;
359
360 /* Map wheel emulation reports: 0xff10 */
361 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000) {
362 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
363 field->logical_minimum = -127;
364 field->logical_maximum = 127;
365
366 switch (usage->hid & HID_USAGE) {
367 case 0x0000:
368 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
369 return 1;
370 case 0x0001:
371 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
372 return 1;
373 default:
374 return -1;
375 }
376 }
377
378 return 0;
379 }
380
lenovo_input_mapping_scrollpoint(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)381 static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
382 struct hid_input *hi, struct hid_field *field,
383 struct hid_usage *usage, unsigned long **bit, int *max)
384 {
385 if (usage->hid == HID_GD_Z) {
386 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
387 return 1;
388 }
389 return 0;
390 }
391
lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)392 static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
393 struct hid_input *hi, struct hid_field *field,
394 struct hid_usage *usage, unsigned long **bit, int *max)
395 {
396 /*
397 * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
398 * a bunch of keys which have no standard consumer page code.
399 */
400 if (usage->hid == 0x000c0001) {
401 switch (usage->usage_index) {
402 case 8: /* Fn-Esc: Fn-lock toggle */
403 map_key_clear(KEY_FN_ESC);
404 return 1;
405 case 9: /* Fn-F4: Mic mute */
406 map_key_clear(LENOVO_KEY_MICMUTE);
407 return 1;
408 case 10: /* Fn-F7: Control panel */
409 map_key_clear(KEY_CONFIG);
410 return 1;
411 case 11: /* Fn-F8: Search (magnifier glass) */
412 map_key_clear(KEY_SEARCH);
413 return 1;
414 case 12: /* Fn-F10: Open My computer (6 boxes) */
415 map_key_clear(KEY_FILE);
416 return 1;
417 }
418 }
419
420 /*
421 * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
422 * from suspend and it does not actually have a F23 key, ignore it.
423 */
424 if (usage->hid == 0x00070072)
425 return -1;
426
427 return 0;
428 }
429
lenovo_input_mapping_x1_tab_kbd(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)430 static int lenovo_input_mapping_x1_tab_kbd(struct hid_device *hdev,
431 struct hid_input *hi, struct hid_field *field,
432 struct hid_usage *usage, unsigned long **bit, int *max)
433 {
434 /*
435 * The ThinkPad X1 Tablet Thin Keyboard uses 0x000c0001 usage for
436 * a bunch of keys which have no standard consumer page code.
437 */
438 if (usage->hid == 0x000c0001) {
439 switch (usage->usage_index) {
440 case 0: /* Fn-F10: Enable/disable bluetooth */
441 map_key_clear(KEY_BLUETOOTH);
442 return 1;
443 case 1: /* Fn-F11: Keyboard settings */
444 map_key_clear(KEY_KEYBOARD);
445 return 1;
446 case 2: /* Fn-F12: User function / Cortana */
447 map_key_clear(KEY_MACRO1);
448 return 1;
449 case 3: /* Fn-PrtSc: Snipping tool */
450 map_key_clear(KEY_SELECTIVE_SCREENSHOT);
451 return 1;
452 case 8: /* Fn-Esc: Fn-lock toggle */
453 map_key_clear(KEY_FN_ESC);
454 return 1;
455 case 9: /* Fn-F4: Mute/unmute microphone */
456 map_key_clear(KEY_MICMUTE);
457 return 1;
458 case 10: /* Fn-F9: Settings */
459 map_key_clear(KEY_CONFIG);
460 return 1;
461 case 13: /* Fn-F7: Manage external displays */
462 map_key_clear(KEY_SWITCHVIDEOMODE);
463 return 1;
464 case 14: /* Fn-F8: Enable/disable wifi */
465 map_key_clear(KEY_WLAN);
466 return 1;
467 }
468 }
469
470 if (usage->hid == (HID_UP_KEYBOARD | 0x009a)) {
471 map_key_clear(KEY_SYSRQ);
472 return 1;
473 }
474
475 return 0;
476 }
477
lenovo_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)478 static int lenovo_input_mapping(struct hid_device *hdev,
479 struct hid_input *hi, struct hid_field *field,
480 struct hid_usage *usage, unsigned long **bit, int *max)
481 {
482 switch (hdev->product) {
483 case USB_DEVICE_ID_LENOVO_TPKBD:
484 return lenovo_input_mapping_tpkbd(hdev, hi, field,
485 usage, bit, max);
486 case USB_DEVICE_ID_LENOVO_CUSBKBD:
487 case USB_DEVICE_ID_LENOVO_CBTKBD:
488 return lenovo_input_mapping_cptkbd(hdev, hi, field,
489 usage, bit, max);
490 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
491 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
492 return lenovo_input_mapping_tpIIkbd(hdev, hi, field,
493 usage, bit, max);
494 case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
495 case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
496 case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
497 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
498 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
499 case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
500 return lenovo_input_mapping_scrollpoint(hdev, hi, field,
501 usage, bit, max);
502 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
503 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
504 usage, bit, max);
505 case USB_DEVICE_ID_LENOVO_X12_TAB:
506 case USB_DEVICE_ID_LENOVO_X12_TAB2:
507 case USB_DEVICE_ID_LENOVO_X1_TAB:
508 case USB_DEVICE_ID_LENOVO_X1_TAB2:
509 case USB_DEVICE_ID_LENOVO_X1_TAB3:
510 return lenovo_input_mapping_x1_tab_kbd(hdev, hi, field, usage, bit, max);
511 default:
512 return 0;
513 }
514 }
515
516 #undef map_key_clear
517
518 /* Send a config command to the keyboard */
lenovo_send_cmd_cptkbd(struct hid_device * hdev,unsigned char byte2,unsigned char byte3)519 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
520 unsigned char byte2, unsigned char byte3)
521 {
522 int ret;
523 unsigned char *buf;
524
525 buf = kzalloc(3, GFP_KERNEL);
526 if (!buf)
527 return -ENOMEM;
528
529 /*
530 * Feature report 0x13 is used for USB,
531 * output report 0x18 is used for Bluetooth.
532 * buf[0] is ignored by hid_hw_raw_request.
533 */
534 buf[0] = 0x18;
535 buf[1] = byte2;
536 buf[2] = byte3;
537
538 switch (hdev->product) {
539 case USB_DEVICE_ID_LENOVO_CUSBKBD:
540 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
541 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
542 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
543 break;
544 case USB_DEVICE_ID_LENOVO_CBTKBD:
545 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
546 ret = hid_hw_output_report(hdev, buf, 3);
547 break;
548 default:
549 ret = -EINVAL;
550 break;
551 }
552
553 kfree(buf);
554
555 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
556 }
557
lenovo_features_set_cptkbd(struct hid_device * hdev)558 static void lenovo_features_set_cptkbd(struct hid_device *hdev)
559 {
560 int ret;
561 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
562
563 /*
564 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
565 * regular keys (Compact only)
566 */
567 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD ||
568 hdev->product == USB_DEVICE_ID_LENOVO_CBTKBD) {
569 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
570 if (ret)
571 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
572 }
573
574 /* Switch middle button to native mode */
575 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
576 if (ret)
577 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
578
579 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
580 if (ret)
581 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
582
583 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
584 if (ret)
585 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
586 }
587
attr_fn_lock_show(struct device * dev,struct device_attribute * attr,char * buf)588 static ssize_t attr_fn_lock_show(struct device *dev,
589 struct device_attribute *attr,
590 char *buf)
591 {
592 struct hid_device *hdev = to_hid_device(dev);
593 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
594
595 return sysfs_emit(buf, "%u\n", data->fn_lock);
596 }
597
attr_fn_lock_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)598 static ssize_t attr_fn_lock_store(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf,
601 size_t count)
602 {
603 struct hid_device *hdev = to_hid_device(dev);
604 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
605 int value, ret;
606
607 if (kstrtoint(buf, 10, &value))
608 return -EINVAL;
609 if (value < 0 || value > 1)
610 return -EINVAL;
611
612 data->fn_lock = !!value;
613
614 switch (hdev->product) {
615 case USB_DEVICE_ID_LENOVO_CUSBKBD:
616 case USB_DEVICE_ID_LENOVO_CBTKBD:
617 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
618 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
619 lenovo_features_set_cptkbd(hdev);
620 break;
621 case USB_DEVICE_ID_LENOVO_X12_TAB:
622 case USB_DEVICE_ID_LENOVO_X12_TAB2:
623 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
624 case USB_DEVICE_ID_LENOVO_X1_TAB:
625 case USB_DEVICE_ID_LENOVO_X1_TAB2:
626 case USB_DEVICE_ID_LENOVO_X1_TAB3:
627 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
628 if (ret)
629 return ret;
630 break;
631 }
632
633 return count;
634 }
635
attr_sensitivity_show_cptkbd(struct device * dev,struct device_attribute * attr,char * buf)636 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
637 struct device_attribute *attr,
638 char *buf)
639 {
640 struct hid_device *hdev = to_hid_device(dev);
641 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
642
643 return sysfs_emit(buf, "%u\n", cptkbd_data->sensitivity);
644 }
645
attr_sensitivity_store_cptkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)646 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
647 struct device_attribute *attr,
648 const char *buf,
649 size_t count)
650 {
651 struct hid_device *hdev = to_hid_device(dev);
652 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
653 int value;
654
655 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
656 return -EINVAL;
657
658 cptkbd_data->sensitivity = value;
659 lenovo_features_set_cptkbd(hdev);
660
661 return count;
662 }
663
attr_middleclick_workaround_show_cptkbd(struct device * dev,struct device_attribute * attr,char * buf)664 static ssize_t attr_middleclick_workaround_show_cptkbd(struct device *dev,
665 struct device_attribute *attr,
666 char *buf)
667 {
668 struct hid_device *hdev = to_hid_device(dev);
669 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
670
671 return sysfs_emit(buf, "%u\n",
672 cptkbd_data->middleclick_workaround_cptkbd);
673 }
674
attr_middleclick_workaround_store_cptkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)675 static ssize_t attr_middleclick_workaround_store_cptkbd(struct device *dev,
676 struct device_attribute *attr,
677 const char *buf,
678 size_t count)
679 {
680 struct hid_device *hdev = to_hid_device(dev);
681 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
682 int value;
683
684 if (kstrtoint(buf, 10, &value))
685 return -EINVAL;
686 if (value < 0 || value > 1)
687 return -EINVAL;
688
689 cptkbd_data->middleclick_workaround_cptkbd = !!value;
690
691 return count;
692 }
693
694
695 static struct device_attribute dev_attr_fn_lock =
696 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
697 attr_fn_lock_show,
698 attr_fn_lock_store);
699
700 static struct device_attribute dev_attr_sensitivity_cptkbd =
701 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
702 attr_sensitivity_show_cptkbd,
703 attr_sensitivity_store_cptkbd);
704
705 static struct device_attribute dev_attr_middleclick_workaround_cptkbd =
706 __ATTR(middleclick_workaround, S_IWUSR | S_IRUGO,
707 attr_middleclick_workaround_show_cptkbd,
708 attr_middleclick_workaround_store_cptkbd);
709
710
711 static struct attribute *lenovo_attributes_cptkbd[] = {
712 &dev_attr_fn_lock.attr,
713 &dev_attr_sensitivity_cptkbd.attr,
714 &dev_attr_middleclick_workaround_cptkbd.attr,
715 NULL
716 };
717
718 static const struct attribute_group lenovo_attr_group_cptkbd = {
719 .attrs = lenovo_attributes_cptkbd,
720 };
721
722 /* Function to handle Lenovo Thinkpad TAB X12's HID raw inputs for fn keys*/
lenovo_raw_event_TP_X12_tab(struct hid_device * hdev,u32 raw_data)723 static int lenovo_raw_event_TP_X12_tab(struct hid_device *hdev, u32 raw_data)
724 {
725 struct hid_input *hidinput;
726 struct input_dev *input = NULL;
727
728 /* Iterate through all associated input devices */
729 list_for_each_entry(hidinput, &hdev->inputs, list) {
730 input = hidinput->input;
731 if (!input)
732 continue;
733
734 switch (raw_data) {
735 /* fn-F20 being used here for MIC mute*/
736 case TP_X12_RAW_HOTKEY_FN_F4:
737 report_key_event(input, LENOVO_KEY_MICMUTE);
738 return 1;
739 /* Power-mode or Airplane mode will be called based on the device*/
740 case TP_X12_RAW_HOTKEY_FN_F8:
741 /*
742 * TP X12 TAB uses Fn-F8 calls Airplanemode
743 * Whereas TP X12 TAB2 uses Fn-F8 for toggling
744 * Power modes
745 */
746 if (hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) {
747 report_key_event(input, KEY_RFKILL);
748 return 1;
749 }
750 report_key_event(input, KEY_PERFORMANCE);
751 return 1;
752 case TP_X12_RAW_HOTKEY_FN_F10:
753 /* TAB1 has PICKUP Phone and TAB2 use Snipping tool*/
754 (hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) ?
755 report_key_event(input, KEY_PICKUP_PHONE) :
756 report_key_event(input, KEY_SELECTIVE_SCREENSHOT);
757 return 1;
758 case TP_X12_RAW_HOTKEY_FN_F12:
759 /* BookMarks/STAR key*/
760 report_key_event(input, KEY_BOOKMARKS);
761 return 1;
762 case TP_X12_RAW_HOTKEY_FN_SPACE:
763 /* Keyboard LED backlight toggle*/
764 report_key_event(input, KEY_KBDILLUMTOGGLE);
765 return 1;
766 default:
767 break;
768 }
769 }
770 return 0;
771 }
772
lenovo_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)773 static int lenovo_raw_event(struct hid_device *hdev,
774 struct hid_report *report, u8 *data, int size)
775 {
776 /*
777 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
778 * its own key is outside the usage page range. Remove extra
779 * keypresses and remap to inside usage page.
780 */
781 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
782 && size == 3
783 && data[0] == 0x15
784 && data[1] == 0x94
785 && data[2] == 0x01)) {
786 data[1] = 0x00;
787 data[2] = 0x01;
788 }
789
790 /*
791 * Lenovo TP X12 Tab KBD's Fn+XX is HID raw data defined. Report ID is 0x03
792 * e.g.: Raw data received for MIC mute is 0x00020003.
793 */
794 if (unlikely((hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB
795 || hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2)
796 && size >= 3 && report->id == 0x03))
797 return lenovo_raw_event_TP_X12_tab(hdev, le32_to_cpu(*(__le32 *)data));
798
799 return 0;
800 }
801
lenovo_event_tp10ubkbd(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)802 static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
803 struct hid_field *field, struct hid_usage *usage, __s32 value)
804 {
805 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
806
807 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
808 /*
809 * The user has toggled the Fn-lock state. Toggle our own
810 * cached value of it and sync our value to the keyboard to
811 * ensure things are in sync (the sycning should be a no-op).
812 */
813 data->fn_lock = !data->fn_lock;
814 schedule_work(&data->fn_lock_sync_work);
815 }
816
817 return 0;
818 }
819
lenovo_event_cptkbd(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)820 static int lenovo_event_cptkbd(struct hid_device *hdev,
821 struct hid_field *field, struct hid_usage *usage, __s32 value)
822 {
823 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
824
825 if (cptkbd_data->middleclick_workaround_cptkbd) {
826 /* "wheel" scroll events */
827 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
828 usage->code == REL_HWHEEL)) {
829 /* Scroll events disable middle-click event */
830 cptkbd_data->middlebutton_state = 2;
831 return 0;
832 }
833
834 /* Middle click events */
835 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
836 if (value == 1) {
837 cptkbd_data->middlebutton_state = 1;
838 } else if (value == 0) {
839 if (cptkbd_data->middlebutton_state == 1) {
840 /* No scrolling inbetween, send middle-click */
841 input_event(field->hidinput->input,
842 EV_KEY, BTN_MIDDLE, 1);
843 input_sync(field->hidinput->input);
844 input_event(field->hidinput->input,
845 EV_KEY, BTN_MIDDLE, 0);
846 input_sync(field->hidinput->input);
847 }
848 cptkbd_data->middlebutton_state = 0;
849 }
850 return 1;
851 }
852 }
853
854 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
855 /*
856 * The user has toggled the Fn-lock state. Toggle our own
857 * cached value of it and sync our value to the keyboard to
858 * ensure things are in sync (the syncing should be a no-op).
859 */
860 cptkbd_data->fn_lock = !cptkbd_data->fn_lock;
861 }
862
863 return 0;
864 }
865
lenovo_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)866 static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
867 struct hid_usage *usage, __s32 value)
868 {
869 if (!hid_get_drvdata(hdev))
870 return 0;
871
872 switch (hdev->product) {
873 case USB_DEVICE_ID_LENOVO_CUSBKBD:
874 case USB_DEVICE_ID_LENOVO_CBTKBD:
875 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
876 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
877 return lenovo_event_cptkbd(hdev, field, usage, value);
878 case USB_DEVICE_ID_LENOVO_X12_TAB:
879 case USB_DEVICE_ID_LENOVO_X12_TAB2:
880 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
881 case USB_DEVICE_ID_LENOVO_X1_TAB:
882 case USB_DEVICE_ID_LENOVO_X1_TAB2:
883 case USB_DEVICE_ID_LENOVO_X1_TAB3:
884 return lenovo_event_tp10ubkbd(hdev, field, usage, value);
885 default:
886 return 0;
887 }
888 }
889
lenovo_features_set_tpkbd(struct hid_device * hdev)890 static int lenovo_features_set_tpkbd(struct hid_device *hdev)
891 {
892 struct hid_report *report;
893 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
894
895 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
896
897 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
898 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
899 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
900 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
901 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
902 report->field[2]->value[0] = data_pointer->sensitivity;
903 report->field[3]->value[0] = data_pointer->press_speed;
904
905 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
906 return 0;
907 }
908
attr_press_to_select_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)909 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
910 struct device_attribute *attr,
911 char *buf)
912 {
913 struct hid_device *hdev = to_hid_device(dev);
914 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
915
916 return sysfs_emit(buf, "%u\n", data_pointer->press_to_select);
917 }
918
attr_press_to_select_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)919 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
920 struct device_attribute *attr,
921 const char *buf,
922 size_t count)
923 {
924 struct hid_device *hdev = to_hid_device(dev);
925 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
926 int value;
927
928 if (kstrtoint(buf, 10, &value))
929 return -EINVAL;
930 if (value < 0 || value > 1)
931 return -EINVAL;
932
933 data_pointer->press_to_select = value;
934 lenovo_features_set_tpkbd(hdev);
935
936 return count;
937 }
938
attr_dragging_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)939 static ssize_t attr_dragging_show_tpkbd(struct device *dev,
940 struct device_attribute *attr,
941 char *buf)
942 {
943 struct hid_device *hdev = to_hid_device(dev);
944 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
945
946 return sysfs_emit(buf, "%u\n", data_pointer->dragging);
947 }
948
attr_dragging_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)949 static ssize_t attr_dragging_store_tpkbd(struct device *dev,
950 struct device_attribute *attr,
951 const char *buf,
952 size_t count)
953 {
954 struct hid_device *hdev = to_hid_device(dev);
955 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
956 int value;
957
958 if (kstrtoint(buf, 10, &value))
959 return -EINVAL;
960 if (value < 0 || value > 1)
961 return -EINVAL;
962
963 data_pointer->dragging = value;
964 lenovo_features_set_tpkbd(hdev);
965
966 return count;
967 }
968
attr_release_to_select_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)969 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
970 struct device_attribute *attr,
971 char *buf)
972 {
973 struct hid_device *hdev = to_hid_device(dev);
974 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
975
976 return sysfs_emit(buf, "%u\n", data_pointer->release_to_select);
977 }
978
attr_release_to_select_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)979 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
980 struct device_attribute *attr,
981 const char *buf,
982 size_t count)
983 {
984 struct hid_device *hdev = to_hid_device(dev);
985 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
986 int value;
987
988 if (kstrtoint(buf, 10, &value))
989 return -EINVAL;
990 if (value < 0 || value > 1)
991 return -EINVAL;
992
993 data_pointer->release_to_select = value;
994 lenovo_features_set_tpkbd(hdev);
995
996 return count;
997 }
998
attr_select_right_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)999 static ssize_t attr_select_right_show_tpkbd(struct device *dev,
1000 struct device_attribute *attr,
1001 char *buf)
1002 {
1003 struct hid_device *hdev = to_hid_device(dev);
1004 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1005
1006 return sysfs_emit(buf, "%u\n", data_pointer->select_right);
1007 }
1008
attr_select_right_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1009 static ssize_t attr_select_right_store_tpkbd(struct device *dev,
1010 struct device_attribute *attr,
1011 const char *buf,
1012 size_t count)
1013 {
1014 struct hid_device *hdev = to_hid_device(dev);
1015 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1016 int value;
1017
1018 if (kstrtoint(buf, 10, &value))
1019 return -EINVAL;
1020 if (value < 0 || value > 1)
1021 return -EINVAL;
1022
1023 data_pointer->select_right = value;
1024 lenovo_features_set_tpkbd(hdev);
1025
1026 return count;
1027 }
1028
attr_sensitivity_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)1029 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
1030 struct device_attribute *attr,
1031 char *buf)
1032 {
1033 struct hid_device *hdev = to_hid_device(dev);
1034 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1035
1036 return sysfs_emit(buf, "%u\n", data_pointer->sensitivity);
1037 }
1038
attr_sensitivity_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1039 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
1040 struct device_attribute *attr,
1041 const char *buf,
1042 size_t count)
1043 {
1044 struct hid_device *hdev = to_hid_device(dev);
1045 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1046 int value;
1047
1048 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
1049 return -EINVAL;
1050
1051 data_pointer->sensitivity = value;
1052 lenovo_features_set_tpkbd(hdev);
1053
1054 return count;
1055 }
1056
attr_press_speed_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)1057 static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
1058 struct device_attribute *attr,
1059 char *buf)
1060 {
1061 struct hid_device *hdev = to_hid_device(dev);
1062 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1063
1064 return sysfs_emit(buf, "%u\n", data_pointer->press_speed);
1065 }
1066
attr_press_speed_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1067 static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
1068 struct device_attribute *attr,
1069 const char *buf,
1070 size_t count)
1071 {
1072 struct hid_device *hdev = to_hid_device(dev);
1073 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1074 int value;
1075
1076 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
1077 return -EINVAL;
1078
1079 data_pointer->press_speed = value;
1080 lenovo_features_set_tpkbd(hdev);
1081
1082 return count;
1083 }
1084
1085 static struct device_attribute dev_attr_press_to_select_tpkbd =
1086 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
1087 attr_press_to_select_show_tpkbd,
1088 attr_press_to_select_store_tpkbd);
1089
1090 static struct device_attribute dev_attr_dragging_tpkbd =
1091 __ATTR(dragging, S_IWUSR | S_IRUGO,
1092 attr_dragging_show_tpkbd,
1093 attr_dragging_store_tpkbd);
1094
1095 static struct device_attribute dev_attr_release_to_select_tpkbd =
1096 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
1097 attr_release_to_select_show_tpkbd,
1098 attr_release_to_select_store_tpkbd);
1099
1100 static struct device_attribute dev_attr_select_right_tpkbd =
1101 __ATTR(select_right, S_IWUSR | S_IRUGO,
1102 attr_select_right_show_tpkbd,
1103 attr_select_right_store_tpkbd);
1104
1105 static struct device_attribute dev_attr_sensitivity_tpkbd =
1106 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
1107 attr_sensitivity_show_tpkbd,
1108 attr_sensitivity_store_tpkbd);
1109
1110 static struct device_attribute dev_attr_press_speed_tpkbd =
1111 __ATTR(press_speed, S_IWUSR | S_IRUGO,
1112 attr_press_speed_show_tpkbd,
1113 attr_press_speed_store_tpkbd);
1114
1115 static struct attribute *lenovo_attributes_tpkbd[] = {
1116 &dev_attr_press_to_select_tpkbd.attr,
1117 &dev_attr_dragging_tpkbd.attr,
1118 &dev_attr_release_to_select_tpkbd.attr,
1119 &dev_attr_select_right_tpkbd.attr,
1120 &dev_attr_sensitivity_tpkbd.attr,
1121 &dev_attr_press_speed_tpkbd.attr,
1122 NULL
1123 };
1124
1125 static const struct attribute_group lenovo_attr_group_tpkbd = {
1126 .attrs = lenovo_attributes_tpkbd,
1127 };
1128
lenovo_led_set_tpkbd(struct hid_device * hdev)1129 static void lenovo_led_set_tpkbd(struct hid_device *hdev)
1130 {
1131 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1132 struct hid_report *report;
1133
1134 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
1135 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
1136 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
1137 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1138 }
1139
lenovo_led_brightness_set(struct led_classdev * led_cdev,enum led_brightness value)1140 static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
1141 enum led_brightness value)
1142 {
1143 struct device *dev = led_cdev->dev->parent;
1144 struct hid_device *hdev = to_hid_device(dev);
1145 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1146 static const u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
1147 int led_nr = 0;
1148 int ret = 0;
1149
1150 if (led_cdev == &data_pointer->led_micmute)
1151 led_nr = 1;
1152
1153 if (value == LED_OFF)
1154 data_pointer->led_state &= ~(1 << led_nr);
1155 else
1156 data_pointer->led_state |= 1 << led_nr;
1157
1158 switch (hdev->product) {
1159 case USB_DEVICE_ID_LENOVO_TPKBD:
1160 lenovo_led_set_tpkbd(hdev);
1161 break;
1162 case USB_DEVICE_ID_LENOVO_X12_TAB:
1163 case USB_DEVICE_ID_LENOVO_X12_TAB2:
1164 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1165 case USB_DEVICE_ID_LENOVO_X1_TAB:
1166 case USB_DEVICE_ID_LENOVO_X1_TAB2:
1167 case USB_DEVICE_ID_LENOVO_X1_TAB3:
1168 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
1169 break;
1170 }
1171
1172 return ret;
1173 }
1174
lenovo_register_leds(struct hid_device * hdev)1175 static int lenovo_register_leds(struct hid_device *hdev)
1176 {
1177 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1178 size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
1179 char *name_mute, *name_micm;
1180 int ret;
1181
1182 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1183 name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1184 if (name_mute == NULL || name_micm == NULL) {
1185 hid_err(hdev, "Could not allocate memory for led data\n");
1186 return -ENOMEM;
1187 }
1188 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
1189 snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
1190
1191 data->led_mute.name = name_mute;
1192 data->led_mute.default_trigger = "audio-mute";
1193 data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
1194 data->led_mute.max_brightness = 1;
1195 data->led_mute.flags = LED_HW_PLUGGABLE;
1196 data->led_mute.dev = &hdev->dev;
1197 ret = led_classdev_register(&hdev->dev, &data->led_mute);
1198 if (ret < 0)
1199 return ret;
1200
1201 data->led_micmute.name = name_micm;
1202 data->led_micmute.default_trigger = "audio-micmute";
1203 data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
1204 data->led_micmute.max_brightness = 1;
1205 data->led_micmute.flags = LED_HW_PLUGGABLE;
1206 data->led_micmute.dev = &hdev->dev;
1207 ret = led_classdev_register(&hdev->dev, &data->led_micmute);
1208 if (ret < 0) {
1209 led_classdev_unregister(&data->led_mute);
1210 return ret;
1211 }
1212
1213 return 0;
1214 }
1215
lenovo_probe_tpkbd(struct hid_device * hdev)1216 static int lenovo_probe_tpkbd(struct hid_device *hdev)
1217 {
1218 struct lenovo_drvdata *data_pointer;
1219 int i, ret;
1220
1221 /*
1222 * Only register extra settings against subdevice where input_mapping
1223 * set drvdata to 1, i.e. the trackpoint.
1224 */
1225 if (!hid_get_drvdata(hdev))
1226 return 0;
1227
1228 hid_set_drvdata(hdev, NULL);
1229
1230 /* Validate required reports. */
1231 for (i = 0; i < 4; i++) {
1232 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
1233 return -ENODEV;
1234 }
1235 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
1236 return -ENODEV;
1237
1238 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1239 if (ret)
1240 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1241
1242 data_pointer = devm_kzalloc(&hdev->dev,
1243 sizeof(struct lenovo_drvdata),
1244 GFP_KERNEL);
1245 if (data_pointer == NULL) {
1246 hid_err(hdev, "Could not allocate memory for driver data\n");
1247 ret = -ENOMEM;
1248 goto err;
1249 }
1250
1251 // set same default values as windows driver
1252 data_pointer->sensitivity = 0xa0;
1253 data_pointer->press_speed = 0x38;
1254
1255 hid_set_drvdata(hdev, data_pointer);
1256
1257 ret = lenovo_register_leds(hdev);
1258 if (ret)
1259 goto err;
1260
1261 lenovo_features_set_tpkbd(hdev);
1262
1263 return 0;
1264 err:
1265 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1266 return ret;
1267 }
1268
lenovo_probe_cptkbd(struct hid_device * hdev)1269 static int lenovo_probe_cptkbd(struct hid_device *hdev)
1270 {
1271 int ret;
1272 struct lenovo_drvdata *cptkbd_data;
1273
1274 /* All the custom action happens on the USBMOUSE device for USB */
1275 if (((hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD) ||
1276 (hdev->product == USB_DEVICE_ID_LENOVO_TPIIUSBKBD)) &&
1277 hdev->type != HID_TYPE_USBMOUSE) {
1278 hid_dbg(hdev, "Ignoring keyboard half of device\n");
1279 return 0;
1280 }
1281
1282 cptkbd_data = devm_kzalloc(&hdev->dev,
1283 sizeof(*cptkbd_data),
1284 GFP_KERNEL);
1285 if (cptkbd_data == NULL) {
1286 hid_err(hdev, "can't alloc keyboard descriptor\n");
1287 return -ENOMEM;
1288 }
1289 hid_set_drvdata(hdev, cptkbd_data);
1290
1291 /* Set keyboard settings to known state */
1292 cptkbd_data->middlebutton_state = 0;
1293 cptkbd_data->fn_lock = true;
1294 cptkbd_data->sensitivity = 0x05;
1295 cptkbd_data->middleclick_workaround_cptkbd = true;
1296 lenovo_features_set_cptkbd(hdev);
1297
1298 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
1299 if (ret)
1300 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1301
1302 return 0;
1303 }
1304
1305 static struct attribute *lenovo_attributes_tp10ubkbd[] = {
1306 &dev_attr_fn_lock.attr,
1307 NULL
1308 };
1309
1310 static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
1311 .attrs = lenovo_attributes_tp10ubkbd,
1312 };
1313
lenovo_probe_tp10ubkbd(struct hid_device * hdev)1314 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
1315 {
1316 struct hid_report_enum *rep_enum;
1317 struct lenovo_drvdata *data;
1318 struct hid_report *rep;
1319 bool found;
1320 int ret;
1321
1322 /*
1323 * The LEDs and the Fn-lock functionality use output report 9,
1324 * with an application of 0xffa0001, add the LEDs on the interface
1325 * with this output report.
1326 */
1327 found = false;
1328 rep_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1329 list_for_each_entry(rep, &rep_enum->report_list, list) {
1330 if (rep->application == 0xffa00001)
1331 found = true;
1332 }
1333 if (!found)
1334 return 0;
1335
1336 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
1337 if (!data)
1338 return -ENOMEM;
1339
1340 mutex_init(&data->led_report_mutex);
1341 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
1342 data->hdev = hdev;
1343
1344 hid_set_drvdata(hdev, data);
1345
1346 /*
1347 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
1348 * We cannot read the state, only set it, so we force it to on here
1349 * (which should be a no-op) to make sure that our state matches the
1350 * keyboard's FN-lock state. This is the same as what Windows does.
1351 *
1352 * For X12 TAB and TAB2, the default windows behaviour Fn-lock Off.
1353 * Adding additional check to ensure the behaviour in case of
1354 * Thinkpad X12 Tabs.
1355 */
1356
1357 data->fn_lock = !(hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB ||
1358 hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2);
1359
1360 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
1361
1362 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1363 if (ret)
1364 return ret;
1365
1366 ret = lenovo_register_leds(hdev);
1367 if (ret)
1368 goto err;
1369
1370 return 0;
1371 err:
1372 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1373 return ret;
1374 }
1375
lenovo_probe(struct hid_device * hdev,const struct hid_device_id * id)1376 static int lenovo_probe(struct hid_device *hdev,
1377 const struct hid_device_id *id)
1378 {
1379 int ret;
1380
1381 ret = hid_parse(hdev);
1382 if (ret) {
1383 hid_err(hdev, "hid_parse failed\n");
1384 goto err;
1385 }
1386
1387 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1388 if (ret) {
1389 hid_err(hdev, "hid_hw_start failed\n");
1390 goto err;
1391 }
1392
1393 switch (hdev->product) {
1394 case USB_DEVICE_ID_LENOVO_TPKBD:
1395 ret = lenovo_probe_tpkbd(hdev);
1396 break;
1397 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1398 case USB_DEVICE_ID_LENOVO_CBTKBD:
1399 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1400 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1401 ret = lenovo_probe_cptkbd(hdev);
1402 break;
1403 case USB_DEVICE_ID_LENOVO_X12_TAB:
1404 case USB_DEVICE_ID_LENOVO_X12_TAB2:
1405 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1406 case USB_DEVICE_ID_LENOVO_X1_TAB:
1407 case USB_DEVICE_ID_LENOVO_X1_TAB2:
1408 case USB_DEVICE_ID_LENOVO_X1_TAB3:
1409 ret = lenovo_probe_tp10ubkbd(hdev);
1410 break;
1411 default:
1412 ret = 0;
1413 break;
1414 }
1415 if (ret)
1416 goto err_hid;
1417
1418 return 0;
1419 err_hid:
1420 hid_hw_stop(hdev);
1421 err:
1422 return ret;
1423 }
1424
1425 #ifdef CONFIG_PM
lenovo_reset_resume(struct hid_device * hdev)1426 static int lenovo_reset_resume(struct hid_device *hdev)
1427 {
1428 switch (hdev->product) {
1429 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1430 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1431 if (hdev->type == HID_TYPE_USBMOUSE)
1432 lenovo_features_set_cptkbd(hdev);
1433
1434 break;
1435 default:
1436 break;
1437 }
1438
1439 return 0;
1440 }
1441 #endif
1442
lenovo_remove_tpkbd(struct hid_device * hdev)1443 static void lenovo_remove_tpkbd(struct hid_device *hdev)
1444 {
1445 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1446
1447 /*
1448 * Only the trackpoint half of the keyboard has drvdata and stuff that
1449 * needs unregistering.
1450 */
1451 if (data_pointer == NULL)
1452 return;
1453
1454 sysfs_remove_group(&hdev->dev.kobj,
1455 &lenovo_attr_group_tpkbd);
1456
1457 led_classdev_unregister(&data_pointer->led_micmute);
1458 led_classdev_unregister(&data_pointer->led_mute);
1459 }
1460
lenovo_remove_cptkbd(struct hid_device * hdev)1461 static void lenovo_remove_cptkbd(struct hid_device *hdev)
1462 {
1463 sysfs_remove_group(&hdev->dev.kobj,
1464 &lenovo_attr_group_cptkbd);
1465 }
1466
lenovo_remove_tp10ubkbd(struct hid_device * hdev)1467 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1468 {
1469 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1470
1471 if (data == NULL)
1472 return;
1473
1474 led_classdev_unregister(&data->led_micmute);
1475 led_classdev_unregister(&data->led_mute);
1476
1477 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1478 cancel_work_sync(&data->fn_lock_sync_work);
1479 }
1480
lenovo_remove(struct hid_device * hdev)1481 static void lenovo_remove(struct hid_device *hdev)
1482 {
1483 switch (hdev->product) {
1484 case USB_DEVICE_ID_LENOVO_TPKBD:
1485 lenovo_remove_tpkbd(hdev);
1486 break;
1487 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1488 case USB_DEVICE_ID_LENOVO_CBTKBD:
1489 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1490 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1491 lenovo_remove_cptkbd(hdev);
1492 break;
1493 case USB_DEVICE_ID_LENOVO_X12_TAB:
1494 case USB_DEVICE_ID_LENOVO_X12_TAB2:
1495 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1496 case USB_DEVICE_ID_LENOVO_X1_TAB:
1497 case USB_DEVICE_ID_LENOVO_X1_TAB2:
1498 case USB_DEVICE_ID_LENOVO_X1_TAB3:
1499 lenovo_remove_tp10ubkbd(hdev);
1500 break;
1501 }
1502
1503 hid_hw_stop(hdev);
1504 }
1505
lenovo_input_configured(struct hid_device * hdev,struct hid_input * hi)1506 static int lenovo_input_configured(struct hid_device *hdev,
1507 struct hid_input *hi)
1508 {
1509 switch (hdev->product) {
1510 case USB_DEVICE_ID_LENOVO_TPKBD:
1511 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1512 case USB_DEVICE_ID_LENOVO_CBTKBD:
1513 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1514 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1515 if (test_bit(EV_REL, hi->input->evbit)) {
1516 /* set only for trackpoint device */
1517 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1518 __set_bit(INPUT_PROP_POINTING_STICK,
1519 hi->input->propbit);
1520 }
1521 break;
1522 }
1523
1524 return 0;
1525 }
1526
1527
1528 static const struct hid_device_id lenovo_devices[] = {
1529 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1530 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1531 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIUSBKBD) },
1532 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1533 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIBTKBD) },
1534 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1535 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1536 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1537 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1538 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1539 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1540 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1541 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1542 /*
1543 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1544 * part, while letting hid-multitouch.c handle the touchpad and trackpoint.
1545 */
1546 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1547 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB) },
1548 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1549 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB2) },
1550 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1551 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB3) },
1552 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1553 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB) },
1554 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1555 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB2) },
1556 { HID_DEVICE(BUS_I2C, HID_GROUP_GENERIC,
1557 USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_YOGA_SLIM_7X_KEYBOARD) },
1558 { }
1559 };
1560
1561 MODULE_DEVICE_TABLE(hid, lenovo_devices);
1562
1563 static struct hid_driver lenovo_driver = {
1564 .name = "lenovo",
1565 .id_table = lenovo_devices,
1566 .input_configured = lenovo_input_configured,
1567 .input_mapping = lenovo_input_mapping,
1568 .probe = lenovo_probe,
1569 .remove = lenovo_remove,
1570 .raw_event = lenovo_raw_event,
1571 .event = lenovo_event,
1572 .report_fixup = lenovo_report_fixup,
1573 #ifdef CONFIG_PM
1574 .reset_resume = lenovo_reset_resume,
1575 #endif
1576 };
1577 module_hid_driver(lenovo_driver);
1578
1579 MODULE_DESCRIPTION("HID driver for IBM/Lenovo");
1580 MODULE_LICENSE("GPL");
1581