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_TAB3:
477 return lenovo_input_mapping_x1_tab_kbd(hdev, hi, field, usage, bit, max);
478 default:
479 return 0;
480 }
481 }
482
483 #undef map_key_clear
484
485 /* Send a config command to the keyboard */
lenovo_send_cmd_cptkbd(struct hid_device * hdev,unsigned char byte2,unsigned char byte3)486 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
487 unsigned char byte2, unsigned char byte3)
488 {
489 int ret;
490 unsigned char *buf;
491
492 buf = kzalloc(3, GFP_KERNEL);
493 if (!buf)
494 return -ENOMEM;
495
496 /*
497 * Feature report 0x13 is used for USB,
498 * output report 0x18 is used for Bluetooth.
499 * buf[0] is ignored by hid_hw_raw_request.
500 */
501 buf[0] = 0x18;
502 buf[1] = byte2;
503 buf[2] = byte3;
504
505 switch (hdev->product) {
506 case USB_DEVICE_ID_LENOVO_CUSBKBD:
507 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
508 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
509 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
510 break;
511 case USB_DEVICE_ID_LENOVO_CBTKBD:
512 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
513 ret = hid_hw_output_report(hdev, buf, 3);
514 break;
515 default:
516 ret = -EINVAL;
517 break;
518 }
519
520 kfree(buf);
521
522 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
523 }
524
lenovo_features_set_cptkbd(struct hid_device * hdev)525 static void lenovo_features_set_cptkbd(struct hid_device *hdev)
526 {
527 int ret;
528 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
529
530 /*
531 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
532 * regular keys
533 */
534 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
535 if (ret)
536 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
537
538 /* Switch middle button to native mode */
539 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
540 if (ret)
541 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
542
543 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
544 if (ret)
545 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
546
547 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
548 if (ret)
549 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
550 }
551
attr_fn_lock_show(struct device * dev,struct device_attribute * attr,char * buf)552 static ssize_t attr_fn_lock_show(struct device *dev,
553 struct device_attribute *attr,
554 char *buf)
555 {
556 struct hid_device *hdev = to_hid_device(dev);
557 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
558
559 return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
560 }
561
attr_fn_lock_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)562 static ssize_t attr_fn_lock_store(struct device *dev,
563 struct device_attribute *attr,
564 const char *buf,
565 size_t count)
566 {
567 struct hid_device *hdev = to_hid_device(dev);
568 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
569 int value, ret;
570
571 if (kstrtoint(buf, 10, &value))
572 return -EINVAL;
573 if (value < 0 || value > 1)
574 return -EINVAL;
575
576 data->fn_lock = !!value;
577
578 switch (hdev->product) {
579 case USB_DEVICE_ID_LENOVO_CUSBKBD:
580 case USB_DEVICE_ID_LENOVO_CBTKBD:
581 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
582 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
583 lenovo_features_set_cptkbd(hdev);
584 break;
585 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
586 case USB_DEVICE_ID_LENOVO_X1_TAB:
587 case USB_DEVICE_ID_LENOVO_X1_TAB3:
588 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
589 if (ret)
590 return ret;
591 break;
592 }
593
594 return count;
595 }
596
attr_sensitivity_show_cptkbd(struct device * dev,struct device_attribute * attr,char * buf)597 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
598 struct device_attribute *attr,
599 char *buf)
600 {
601 struct hid_device *hdev = to_hid_device(dev);
602 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
603
604 return snprintf(buf, PAGE_SIZE, "%u\n",
605 cptkbd_data->sensitivity);
606 }
607
attr_sensitivity_store_cptkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)608 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
609 struct device_attribute *attr,
610 const char *buf,
611 size_t count)
612 {
613 struct hid_device *hdev = to_hid_device(dev);
614 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
615 int value;
616
617 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
618 return -EINVAL;
619
620 cptkbd_data->sensitivity = value;
621 lenovo_features_set_cptkbd(hdev);
622
623 return count;
624 }
625
attr_middleclick_workaround_show_cptkbd(struct device * dev,struct device_attribute * attr,char * buf)626 static ssize_t attr_middleclick_workaround_show_cptkbd(struct device *dev,
627 struct device_attribute *attr,
628 char *buf)
629 {
630 struct hid_device *hdev = to_hid_device(dev);
631 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
632
633 return snprintf(buf, PAGE_SIZE, "%u\n",
634 cptkbd_data->middleclick_workaround_cptkbd);
635 }
636
attr_middleclick_workaround_store_cptkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)637 static ssize_t attr_middleclick_workaround_store_cptkbd(struct device *dev,
638 struct device_attribute *attr,
639 const char *buf,
640 size_t count)
641 {
642 struct hid_device *hdev = to_hid_device(dev);
643 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
644 int value;
645
646 if (kstrtoint(buf, 10, &value))
647 return -EINVAL;
648 if (value < 0 || value > 1)
649 return -EINVAL;
650
651 cptkbd_data->middleclick_workaround_cptkbd = !!value;
652
653 return count;
654 }
655
656
657 static struct device_attribute dev_attr_fn_lock =
658 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
659 attr_fn_lock_show,
660 attr_fn_lock_store);
661
662 static struct device_attribute dev_attr_sensitivity_cptkbd =
663 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
664 attr_sensitivity_show_cptkbd,
665 attr_sensitivity_store_cptkbd);
666
667 static struct device_attribute dev_attr_middleclick_workaround_cptkbd =
668 __ATTR(middleclick_workaround, S_IWUSR | S_IRUGO,
669 attr_middleclick_workaround_show_cptkbd,
670 attr_middleclick_workaround_store_cptkbd);
671
672
673 static struct attribute *lenovo_attributes_cptkbd[] = {
674 &dev_attr_fn_lock.attr,
675 &dev_attr_sensitivity_cptkbd.attr,
676 &dev_attr_middleclick_workaround_cptkbd.attr,
677 NULL
678 };
679
680 static const struct attribute_group lenovo_attr_group_cptkbd = {
681 .attrs = lenovo_attributes_cptkbd,
682 };
683
lenovo_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)684 static int lenovo_raw_event(struct hid_device *hdev,
685 struct hid_report *report, u8 *data, int size)
686 {
687 /*
688 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
689 * its own key is outside the usage page range. Remove extra
690 * keypresses and remap to inside usage page.
691 */
692 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
693 && size == 3
694 && data[0] == 0x15
695 && data[1] == 0x94
696 && data[2] == 0x01)) {
697 data[1] = 0x00;
698 data[2] = 0x01;
699 }
700
701 return 0;
702 }
703
lenovo_event_tp10ubkbd(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)704 static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
705 struct hid_field *field, struct hid_usage *usage, __s32 value)
706 {
707 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
708
709 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
710 /*
711 * The user has toggled the Fn-lock state. Toggle our own
712 * cached value of it and sync our value to the keyboard to
713 * ensure things are in sync (the sycning should be a no-op).
714 */
715 data->fn_lock = !data->fn_lock;
716 schedule_work(&data->fn_lock_sync_work);
717 }
718
719 return 0;
720 }
721
lenovo_event_cptkbd(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)722 static int lenovo_event_cptkbd(struct hid_device *hdev,
723 struct hid_field *field, struct hid_usage *usage, __s32 value)
724 {
725 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
726
727 if (cptkbd_data->middleclick_workaround_cptkbd) {
728 /* "wheel" scroll events */
729 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
730 usage->code == REL_HWHEEL)) {
731 /* Scroll events disable middle-click event */
732 cptkbd_data->middlebutton_state = 2;
733 return 0;
734 }
735
736 /* Middle click events */
737 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
738 if (value == 1) {
739 cptkbd_data->middlebutton_state = 1;
740 } else if (value == 0) {
741 if (cptkbd_data->middlebutton_state == 1) {
742 /* No scrolling inbetween, send middle-click */
743 input_event(field->hidinput->input,
744 EV_KEY, BTN_MIDDLE, 1);
745 input_sync(field->hidinput->input);
746 input_event(field->hidinput->input,
747 EV_KEY, BTN_MIDDLE, 0);
748 input_sync(field->hidinput->input);
749 }
750 cptkbd_data->middlebutton_state = 0;
751 }
752 return 1;
753 }
754 }
755
756 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
757 /*
758 * The user has toggled the Fn-lock state. Toggle our own
759 * cached value of it and sync our value to the keyboard to
760 * ensure things are in sync (the syncing should be a no-op).
761 */
762 cptkbd_data->fn_lock = !cptkbd_data->fn_lock;
763 }
764
765 return 0;
766 }
767
lenovo_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)768 static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
769 struct hid_usage *usage, __s32 value)
770 {
771 if (!hid_get_drvdata(hdev))
772 return 0;
773
774 switch (hdev->product) {
775 case USB_DEVICE_ID_LENOVO_CUSBKBD:
776 case USB_DEVICE_ID_LENOVO_CBTKBD:
777 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
778 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
779 return lenovo_event_cptkbd(hdev, field, usage, value);
780 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
781 case USB_DEVICE_ID_LENOVO_X1_TAB:
782 case USB_DEVICE_ID_LENOVO_X1_TAB3:
783 return lenovo_event_tp10ubkbd(hdev, field, usage, value);
784 default:
785 return 0;
786 }
787 }
788
lenovo_features_set_tpkbd(struct hid_device * hdev)789 static int lenovo_features_set_tpkbd(struct hid_device *hdev)
790 {
791 struct hid_report *report;
792 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
793
794 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
795
796 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
797 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
798 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
799 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
800 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
801 report->field[2]->value[0] = data_pointer->sensitivity;
802 report->field[3]->value[0] = data_pointer->press_speed;
803
804 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
805 return 0;
806 }
807
attr_press_to_select_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)808 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
809 struct device_attribute *attr,
810 char *buf)
811 {
812 struct hid_device *hdev = to_hid_device(dev);
813 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
814
815 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
816 }
817
attr_press_to_select_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)818 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
819 struct device_attribute *attr,
820 const char *buf,
821 size_t count)
822 {
823 struct hid_device *hdev = to_hid_device(dev);
824 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
825 int value;
826
827 if (kstrtoint(buf, 10, &value))
828 return -EINVAL;
829 if (value < 0 || value > 1)
830 return -EINVAL;
831
832 data_pointer->press_to_select = value;
833 lenovo_features_set_tpkbd(hdev);
834
835 return count;
836 }
837
attr_dragging_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)838 static ssize_t attr_dragging_show_tpkbd(struct device *dev,
839 struct device_attribute *attr,
840 char *buf)
841 {
842 struct hid_device *hdev = to_hid_device(dev);
843 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
844
845 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
846 }
847
attr_dragging_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)848 static ssize_t attr_dragging_store_tpkbd(struct device *dev,
849 struct device_attribute *attr,
850 const char *buf,
851 size_t count)
852 {
853 struct hid_device *hdev = to_hid_device(dev);
854 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
855 int value;
856
857 if (kstrtoint(buf, 10, &value))
858 return -EINVAL;
859 if (value < 0 || value > 1)
860 return -EINVAL;
861
862 data_pointer->dragging = value;
863 lenovo_features_set_tpkbd(hdev);
864
865 return count;
866 }
867
attr_release_to_select_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)868 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
869 struct device_attribute *attr,
870 char *buf)
871 {
872 struct hid_device *hdev = to_hid_device(dev);
873 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
874
875 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
876 }
877
attr_release_to_select_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)878 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
879 struct device_attribute *attr,
880 const char *buf,
881 size_t count)
882 {
883 struct hid_device *hdev = to_hid_device(dev);
884 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
885 int value;
886
887 if (kstrtoint(buf, 10, &value))
888 return -EINVAL;
889 if (value < 0 || value > 1)
890 return -EINVAL;
891
892 data_pointer->release_to_select = value;
893 lenovo_features_set_tpkbd(hdev);
894
895 return count;
896 }
897
attr_select_right_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)898 static ssize_t attr_select_right_show_tpkbd(struct device *dev,
899 struct device_attribute *attr,
900 char *buf)
901 {
902 struct hid_device *hdev = to_hid_device(dev);
903 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
904
905 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
906 }
907
attr_select_right_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)908 static ssize_t attr_select_right_store_tpkbd(struct device *dev,
909 struct device_attribute *attr,
910 const char *buf,
911 size_t count)
912 {
913 struct hid_device *hdev = to_hid_device(dev);
914 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
915 int value;
916
917 if (kstrtoint(buf, 10, &value))
918 return -EINVAL;
919 if (value < 0 || value > 1)
920 return -EINVAL;
921
922 data_pointer->select_right = value;
923 lenovo_features_set_tpkbd(hdev);
924
925 return count;
926 }
927
attr_sensitivity_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)928 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
929 struct device_attribute *attr,
930 char *buf)
931 {
932 struct hid_device *hdev = to_hid_device(dev);
933 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
934
935 return snprintf(buf, PAGE_SIZE, "%u\n",
936 data_pointer->sensitivity);
937 }
938
attr_sensitivity_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)939 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
940 struct device_attribute *attr,
941 const char *buf,
942 size_t count)
943 {
944 struct hid_device *hdev = to_hid_device(dev);
945 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
946 int value;
947
948 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
949 return -EINVAL;
950
951 data_pointer->sensitivity = value;
952 lenovo_features_set_tpkbd(hdev);
953
954 return count;
955 }
956
attr_press_speed_show_tpkbd(struct device * dev,struct device_attribute * attr,char * buf)957 static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
958 struct device_attribute *attr,
959 char *buf)
960 {
961 struct hid_device *hdev = to_hid_device(dev);
962 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
963
964 return snprintf(buf, PAGE_SIZE, "%u\n",
965 data_pointer->press_speed);
966 }
967
attr_press_speed_store_tpkbd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)968 static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
969 struct device_attribute *attr,
970 const char *buf,
971 size_t count)
972 {
973 struct hid_device *hdev = to_hid_device(dev);
974 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
975 int value;
976
977 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
978 return -EINVAL;
979
980 data_pointer->press_speed = value;
981 lenovo_features_set_tpkbd(hdev);
982
983 return count;
984 }
985
986 static struct device_attribute dev_attr_press_to_select_tpkbd =
987 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
988 attr_press_to_select_show_tpkbd,
989 attr_press_to_select_store_tpkbd);
990
991 static struct device_attribute dev_attr_dragging_tpkbd =
992 __ATTR(dragging, S_IWUSR | S_IRUGO,
993 attr_dragging_show_tpkbd,
994 attr_dragging_store_tpkbd);
995
996 static struct device_attribute dev_attr_release_to_select_tpkbd =
997 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
998 attr_release_to_select_show_tpkbd,
999 attr_release_to_select_store_tpkbd);
1000
1001 static struct device_attribute dev_attr_select_right_tpkbd =
1002 __ATTR(select_right, S_IWUSR | S_IRUGO,
1003 attr_select_right_show_tpkbd,
1004 attr_select_right_store_tpkbd);
1005
1006 static struct device_attribute dev_attr_sensitivity_tpkbd =
1007 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
1008 attr_sensitivity_show_tpkbd,
1009 attr_sensitivity_store_tpkbd);
1010
1011 static struct device_attribute dev_attr_press_speed_tpkbd =
1012 __ATTR(press_speed, S_IWUSR | S_IRUGO,
1013 attr_press_speed_show_tpkbd,
1014 attr_press_speed_store_tpkbd);
1015
1016 static struct attribute *lenovo_attributes_tpkbd[] = {
1017 &dev_attr_press_to_select_tpkbd.attr,
1018 &dev_attr_dragging_tpkbd.attr,
1019 &dev_attr_release_to_select_tpkbd.attr,
1020 &dev_attr_select_right_tpkbd.attr,
1021 &dev_attr_sensitivity_tpkbd.attr,
1022 &dev_attr_press_speed_tpkbd.attr,
1023 NULL
1024 };
1025
1026 static const struct attribute_group lenovo_attr_group_tpkbd = {
1027 .attrs = lenovo_attributes_tpkbd,
1028 };
1029
lenovo_led_set_tpkbd(struct hid_device * hdev)1030 static void lenovo_led_set_tpkbd(struct hid_device *hdev)
1031 {
1032 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1033 struct hid_report *report;
1034
1035 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
1036 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
1037 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
1038 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1039 }
1040
lenovo_led_brightness_set(struct led_classdev * led_cdev,enum led_brightness value)1041 static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
1042 enum led_brightness value)
1043 {
1044 struct device *dev = led_cdev->dev->parent;
1045 struct hid_device *hdev = to_hid_device(dev);
1046 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1047 static const u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
1048 int led_nr = 0;
1049 int ret = 0;
1050
1051 if (led_cdev == &data_pointer->led_micmute)
1052 led_nr = 1;
1053
1054 if (value == LED_OFF)
1055 data_pointer->led_state &= ~(1 << led_nr);
1056 else
1057 data_pointer->led_state |= 1 << led_nr;
1058
1059 switch (hdev->product) {
1060 case USB_DEVICE_ID_LENOVO_TPKBD:
1061 lenovo_led_set_tpkbd(hdev);
1062 break;
1063 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1064 case USB_DEVICE_ID_LENOVO_X1_TAB:
1065 case USB_DEVICE_ID_LENOVO_X1_TAB3:
1066 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
1067 break;
1068 }
1069
1070 return ret;
1071 }
1072
lenovo_register_leds(struct hid_device * hdev)1073 static int lenovo_register_leds(struct hid_device *hdev)
1074 {
1075 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1076 size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
1077 char *name_mute, *name_micm;
1078 int ret;
1079
1080 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1081 name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1082 if (name_mute == NULL || name_micm == NULL) {
1083 hid_err(hdev, "Could not allocate memory for led data\n");
1084 return -ENOMEM;
1085 }
1086 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
1087 snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
1088
1089 data->led_mute.name = name_mute;
1090 data->led_mute.default_trigger = "audio-mute";
1091 data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
1092 data->led_mute.max_brightness = 1;
1093 data->led_mute.flags = LED_HW_PLUGGABLE;
1094 data->led_mute.dev = &hdev->dev;
1095 ret = led_classdev_register(&hdev->dev, &data->led_mute);
1096 if (ret < 0)
1097 return ret;
1098
1099 data->led_micmute.name = name_micm;
1100 data->led_micmute.default_trigger = "audio-micmute";
1101 data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
1102 data->led_micmute.max_brightness = 1;
1103 data->led_micmute.flags = LED_HW_PLUGGABLE;
1104 data->led_micmute.dev = &hdev->dev;
1105 ret = led_classdev_register(&hdev->dev, &data->led_micmute);
1106 if (ret < 0) {
1107 led_classdev_unregister(&data->led_mute);
1108 return ret;
1109 }
1110
1111 return 0;
1112 }
1113
lenovo_probe_tpkbd(struct hid_device * hdev)1114 static int lenovo_probe_tpkbd(struct hid_device *hdev)
1115 {
1116 struct lenovo_drvdata *data_pointer;
1117 int i, ret;
1118
1119 /*
1120 * Only register extra settings against subdevice where input_mapping
1121 * set drvdata to 1, i.e. the trackpoint.
1122 */
1123 if (!hid_get_drvdata(hdev))
1124 return 0;
1125
1126 hid_set_drvdata(hdev, NULL);
1127
1128 /* Validate required reports. */
1129 for (i = 0; i < 4; i++) {
1130 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
1131 return -ENODEV;
1132 }
1133 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
1134 return -ENODEV;
1135
1136 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1137 if (ret)
1138 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1139
1140 data_pointer = devm_kzalloc(&hdev->dev,
1141 sizeof(struct lenovo_drvdata),
1142 GFP_KERNEL);
1143 if (data_pointer == NULL) {
1144 hid_err(hdev, "Could not allocate memory for driver data\n");
1145 ret = -ENOMEM;
1146 goto err;
1147 }
1148
1149 // set same default values as windows driver
1150 data_pointer->sensitivity = 0xa0;
1151 data_pointer->press_speed = 0x38;
1152
1153 hid_set_drvdata(hdev, data_pointer);
1154
1155 ret = lenovo_register_leds(hdev);
1156 if (ret)
1157 goto err;
1158
1159 lenovo_features_set_tpkbd(hdev);
1160
1161 return 0;
1162 err:
1163 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1164 return ret;
1165 }
1166
lenovo_probe_cptkbd(struct hid_device * hdev)1167 static int lenovo_probe_cptkbd(struct hid_device *hdev)
1168 {
1169 int ret;
1170 struct lenovo_drvdata *cptkbd_data;
1171
1172 /* All the custom action happens on the USBMOUSE device for USB */
1173 if (((hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD) ||
1174 (hdev->product == USB_DEVICE_ID_LENOVO_TPIIUSBKBD)) &&
1175 hdev->type != HID_TYPE_USBMOUSE) {
1176 hid_dbg(hdev, "Ignoring keyboard half of device\n");
1177 return 0;
1178 }
1179
1180 cptkbd_data = devm_kzalloc(&hdev->dev,
1181 sizeof(*cptkbd_data),
1182 GFP_KERNEL);
1183 if (cptkbd_data == NULL) {
1184 hid_err(hdev, "can't alloc keyboard descriptor\n");
1185 return -ENOMEM;
1186 }
1187 hid_set_drvdata(hdev, cptkbd_data);
1188
1189 /* Set keyboard settings to known state */
1190 cptkbd_data->middlebutton_state = 0;
1191 cptkbd_data->fn_lock = true;
1192 cptkbd_data->sensitivity = 0x05;
1193 cptkbd_data->middleclick_workaround_cptkbd = true;
1194 lenovo_features_set_cptkbd(hdev);
1195
1196 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
1197 if (ret)
1198 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1199
1200 return 0;
1201 }
1202
1203 static struct attribute *lenovo_attributes_tp10ubkbd[] = {
1204 &dev_attr_fn_lock.attr,
1205 NULL
1206 };
1207
1208 static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
1209 .attrs = lenovo_attributes_tp10ubkbd,
1210 };
1211
lenovo_probe_tp10ubkbd(struct hid_device * hdev)1212 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
1213 {
1214 struct hid_report_enum *rep_enum;
1215 struct lenovo_drvdata *data;
1216 struct hid_report *rep;
1217 bool found;
1218 int ret;
1219
1220 /*
1221 * The LEDs and the Fn-lock functionality use output report 9,
1222 * with an application of 0xffa0001, add the LEDs on the interface
1223 * with this output report.
1224 */
1225 found = false;
1226 rep_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1227 list_for_each_entry(rep, &rep_enum->report_list, list) {
1228 if (rep->application == 0xffa00001)
1229 found = true;
1230 }
1231 if (!found)
1232 return 0;
1233
1234 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
1235 if (!data)
1236 return -ENOMEM;
1237
1238 mutex_init(&data->led_report_mutex);
1239 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
1240 data->hdev = hdev;
1241
1242 hid_set_drvdata(hdev, data);
1243
1244 /*
1245 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
1246 * We cannot read the state, only set it, so we force it to on here
1247 * (which should be a no-op) to make sure that our state matches the
1248 * keyboard's FN-lock state. This is the same as what Windows does.
1249 */
1250 data->fn_lock = true;
1251 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
1252
1253 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1254 if (ret)
1255 return ret;
1256
1257 ret = lenovo_register_leds(hdev);
1258 if (ret)
1259 goto err;
1260
1261 return 0;
1262 err:
1263 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1264 return ret;
1265 }
1266
lenovo_probe(struct hid_device * hdev,const struct hid_device_id * id)1267 static int lenovo_probe(struct hid_device *hdev,
1268 const struct hid_device_id *id)
1269 {
1270 int ret;
1271
1272 ret = hid_parse(hdev);
1273 if (ret) {
1274 hid_err(hdev, "hid_parse failed\n");
1275 goto err;
1276 }
1277
1278 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1279 if (ret) {
1280 hid_err(hdev, "hid_hw_start failed\n");
1281 goto err;
1282 }
1283
1284 switch (hdev->product) {
1285 case USB_DEVICE_ID_LENOVO_TPKBD:
1286 ret = lenovo_probe_tpkbd(hdev);
1287 break;
1288 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1289 case USB_DEVICE_ID_LENOVO_CBTKBD:
1290 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1291 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1292 ret = lenovo_probe_cptkbd(hdev);
1293 break;
1294 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1295 case USB_DEVICE_ID_LENOVO_X1_TAB:
1296 case USB_DEVICE_ID_LENOVO_X1_TAB3:
1297 ret = lenovo_probe_tp10ubkbd(hdev);
1298 break;
1299 default:
1300 ret = 0;
1301 break;
1302 }
1303 if (ret)
1304 goto err_hid;
1305
1306 return 0;
1307 err_hid:
1308 hid_hw_stop(hdev);
1309 err:
1310 return ret;
1311 }
1312
1313 #ifdef CONFIG_PM
lenovo_reset_resume(struct hid_device * hdev)1314 static int lenovo_reset_resume(struct hid_device *hdev)
1315 {
1316 switch (hdev->product) {
1317 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1318 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1319 if (hdev->type == HID_TYPE_USBMOUSE)
1320 lenovo_features_set_cptkbd(hdev);
1321
1322 break;
1323 default:
1324 break;
1325 }
1326
1327 return 0;
1328 }
1329 #endif
1330
lenovo_remove_tpkbd(struct hid_device * hdev)1331 static void lenovo_remove_tpkbd(struct hid_device *hdev)
1332 {
1333 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1334
1335 /*
1336 * Only the trackpoint half of the keyboard has drvdata and stuff that
1337 * needs unregistering.
1338 */
1339 if (data_pointer == NULL)
1340 return;
1341
1342 sysfs_remove_group(&hdev->dev.kobj,
1343 &lenovo_attr_group_tpkbd);
1344
1345 led_classdev_unregister(&data_pointer->led_micmute);
1346 led_classdev_unregister(&data_pointer->led_mute);
1347 }
1348
lenovo_remove_cptkbd(struct hid_device * hdev)1349 static void lenovo_remove_cptkbd(struct hid_device *hdev)
1350 {
1351 sysfs_remove_group(&hdev->dev.kobj,
1352 &lenovo_attr_group_cptkbd);
1353 }
1354
lenovo_remove_tp10ubkbd(struct hid_device * hdev)1355 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1356 {
1357 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1358
1359 if (data == NULL)
1360 return;
1361
1362 led_classdev_unregister(&data->led_micmute);
1363 led_classdev_unregister(&data->led_mute);
1364
1365 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1366 cancel_work_sync(&data->fn_lock_sync_work);
1367 }
1368
lenovo_remove(struct hid_device * hdev)1369 static void lenovo_remove(struct hid_device *hdev)
1370 {
1371 switch (hdev->product) {
1372 case USB_DEVICE_ID_LENOVO_TPKBD:
1373 lenovo_remove_tpkbd(hdev);
1374 break;
1375 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1376 case USB_DEVICE_ID_LENOVO_CBTKBD:
1377 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1378 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1379 lenovo_remove_cptkbd(hdev);
1380 break;
1381 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1382 case USB_DEVICE_ID_LENOVO_X1_TAB:
1383 case USB_DEVICE_ID_LENOVO_X1_TAB3:
1384 lenovo_remove_tp10ubkbd(hdev);
1385 break;
1386 }
1387
1388 hid_hw_stop(hdev);
1389 }
1390
lenovo_input_configured(struct hid_device * hdev,struct hid_input * hi)1391 static int lenovo_input_configured(struct hid_device *hdev,
1392 struct hid_input *hi)
1393 {
1394 switch (hdev->product) {
1395 case USB_DEVICE_ID_LENOVO_TPKBD:
1396 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1397 case USB_DEVICE_ID_LENOVO_CBTKBD:
1398 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1399 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1400 if (test_bit(EV_REL, hi->input->evbit)) {
1401 /* set only for trackpoint device */
1402 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1403 __set_bit(INPUT_PROP_POINTING_STICK,
1404 hi->input->propbit);
1405 }
1406 break;
1407 }
1408
1409 return 0;
1410 }
1411
1412
1413 static const struct hid_device_id lenovo_devices[] = {
1414 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1415 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1416 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIUSBKBD) },
1417 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1418 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIBTKBD) },
1419 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1420 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1421 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1422 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1423 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1424 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1425 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1426 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1427 /*
1428 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1429 * part, while letting hid-multitouch.c handle the touchpad and trackpoint.
1430 */
1431 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1432 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB) },
1433 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1434 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB3) },
1435 { }
1436 };
1437
1438 MODULE_DEVICE_TABLE(hid, lenovo_devices);
1439
1440 static struct hid_driver lenovo_driver = {
1441 .name = "lenovo",
1442 .id_table = lenovo_devices,
1443 .input_configured = lenovo_input_configured,
1444 .input_mapping = lenovo_input_mapping,
1445 .probe = lenovo_probe,
1446 .remove = lenovo_remove,
1447 .raw_event = lenovo_raw_event,
1448 .event = lenovo_event,
1449 .report_fixup = lenovo_report_fixup,
1450 #ifdef CONFIG_PM
1451 .reset_resume = lenovo_reset_resume,
1452 #endif
1453 };
1454 module_hid_driver(lenovo_driver);
1455
1456 MODULE_LICENSE("GPL");
1457