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