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