1 // SPDX-License-Identifier: GPL-2.0-or-later
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)
9 * Copyright (c) 2012 Bernhard Seibold
10 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
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
20 * Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
21 * Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
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>
37 /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */
38 #define LENOVO_KEY_MICMUTE KEY_F20
40 struct lenovo_drvdata {
41 u8 led_report[3]; /* Must be first for proper alignment */
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;
50 int release_to_select;
54 u8 middlebutton_state; /* 0:Up, 1:Down (undecided), 2:Scrolling */
58 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
60 #define TP10UBKBD_LED_OUTPUT_REPORT 9
62 #define TP10UBKBD_FN_LOCK_LED 0x54
63 #define TP10UBKBD_MUTE_LED 0x64
64 #define TP10UBKBD_MICMUTE_LED 0x74
66 #define TP10UBKBD_LED_OFF 1
67 #define TP10UBKBD_LED_ON 2
69 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
70 enum led_brightness value)
72 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
75 mutex_lock(&data->led_report_mutex);
77 data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT;
78 data->led_report[1] = led_code;
79 data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF;
80 ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3,
81 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
84 hid_err(hdev, "Set LED output report error: %d\n", ret);
86 ret = ret < 0 ? ret : -EIO;
91 mutex_unlock(&data->led_report_mutex);
96 static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
98 struct lenovo_drvdata *data =
99 container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
101 lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
105 static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
106 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */
107 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
108 0xa1, 0x01, /* Collection (Application) */
109 0x85, 0x04, /* Report ID (4) */
110 0x19, 0x00, /* Usage Minimum (0) */
111 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */
114 /* Broken ThinkPad TrackPoint II collection (Bluetooth mode) */
115 static const __u8 lenovo_tpIIbtkbd_need_fixup_collection[] = {
116 0x06, 0x00, 0xFF, /* Usage Page (Vendor Defined 0xFF00) */
117 0x09, 0x01, /* Usage (0x01) */
118 0xA1, 0x01, /* Collection (Application) */
119 0x85, 0x05, /* Report ID (5) */
120 0x1A, 0xF1, 0x00, /* Usage Minimum (0xF1) */
121 0x2A, 0xFC, 0x00, /* Usage Maximum (0xFC) */
122 0x15, 0x00, /* Logical Minimum (0) */
123 0x25, 0x01, /* Logical Maximum (1) */
124 0x75, 0x01, /* Report Size (1) */
125 0x95, 0x0D, /* Report Count (13) */
126 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */
127 0x95, 0x03, /* Report Count (3) */
128 0x81, 0x01, /* Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */
131 static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
134 switch (hdev->product) {
135 case USB_DEVICE_ID_LENOVO_TPPRODOCK:
136 /* the fixups that need to be done:
137 * - get a reasonable usage max for the vendor collection
138 * 0x8801 from the report ID 4
141 memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
142 sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
147 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
149 memcmp(&rdesc[234], lenovo_tpIIbtkbd_need_fixup_collection,
150 sizeof(lenovo_tpIIbtkbd_need_fixup_collection)) == 0) {
151 rdesc[244] = 0x00; /* usage minimum = 0x00 */
152 rdesc[247] = 0xff; /* usage maximum = 0xff */
153 rdesc[252] = 0xff; /* logical maximum = 0xff */
154 rdesc[254] = 0x08; /* report size = 0x08 */
155 rdesc[256] = 0x01; /* report count = 0x01 */
156 rdesc[258] = 0x00; /* input = 0x00 */
157 rdesc[260] = 0x01; /* report count (2) = 0x01 */
164 static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
165 struct hid_input *hi, struct hid_field *field,
166 struct hid_usage *usage, unsigned long **bit, int *max)
168 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
169 /* This sub-device contains trackpoint, mark it */
170 hid_set_drvdata(hdev, (void *)1);
171 map_key_clear(LENOVO_KEY_MICMUTE);
177 static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
178 struct hid_input *hi, struct hid_field *field,
179 struct hid_usage *usage, unsigned long **bit, int *max)
181 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
182 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
183 (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
184 switch (usage->hid & HID_USAGE) {
185 case 0x00f1: /* Fn-F4: Mic mute */
186 map_key_clear(LENOVO_KEY_MICMUTE);
188 case 0x00f2: /* Fn-F5: Brightness down */
189 map_key_clear(KEY_BRIGHTNESSDOWN);
191 case 0x00f3: /* Fn-F6: Brightness up */
192 map_key_clear(KEY_BRIGHTNESSUP);
194 case 0x00f4: /* Fn-F7: External display (projector) */
195 map_key_clear(KEY_SWITCHVIDEOMODE);
197 case 0x00f5: /* Fn-F8: Wireless */
198 map_key_clear(KEY_WLAN);
200 case 0x00f6: /* Fn-F9: Control panel */
201 map_key_clear(KEY_CONFIG);
203 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
204 map_key_clear(KEY_SCALE);
206 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
207 /* NB: This mapping is invented in raw_event below */
208 map_key_clear(KEY_FILE);
210 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
211 map_key_clear(KEY_FN_ESC);
213 case 0x00fb: /* Middle mouse button (in native mode) */
214 map_key_clear(BTN_MIDDLE);
219 /* Compatibility middle/wheel mappings should be ignored */
220 if (usage->hid == HID_GD_WHEEL)
222 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
223 (usage->hid & HID_USAGE) == 0x003)
225 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
226 (usage->hid & HID_USAGE) == 0x238)
229 /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
230 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
231 (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
232 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
233 field->logical_minimum = -127;
234 field->logical_maximum = 127;
236 switch (usage->hid & HID_USAGE) {
238 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
241 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
251 static int lenovo_input_mapping_tpIIkbd(struct hid_device *hdev,
252 struct hid_input *hi, struct hid_field *field,
253 struct hid_usage *usage, unsigned long **bit, int *max)
256 * 0xff0a0000 = USB, HID_UP_MSVENDOR = BT.
258 * In BT mode, there are two HID_UP_MSVENDOR pages.
259 * Use only the page that contains report ID == 5.
261 if (((usage->hid & HID_USAGE_PAGE) == 0xff0a0000 ||
262 (usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) &&
263 field->report->id == 5) {
264 switch (usage->hid & HID_USAGE) {
265 case 0x00bb: /* Fn-F4: Mic mute */
266 map_key_clear(LENOVO_KEY_MICMUTE);
268 case 0x00c3: /* Fn-F5: Brightness down */
269 map_key_clear(KEY_BRIGHTNESSDOWN);
271 case 0x00c4: /* Fn-F6: Brightness up */
272 map_key_clear(KEY_BRIGHTNESSUP);
274 case 0x00c1: /* Fn-F8: Notification center */
275 map_key_clear(KEY_NOTIFICATION_CENTER);
277 case 0x00bc: /* Fn-F9: Control panel */
278 map_key_clear(KEY_CONFIG);
280 case 0x00b6: /* Fn-F10: Bluetooth */
281 map_key_clear(KEY_BLUETOOTH);
283 case 0x00b7: /* Fn-F11: Keyboard config */
284 map_key_clear(KEY_KEYBOARD);
286 case 0x00b8: /* Fn-F12: User function */
287 map_key_clear(KEY_PROG1);
289 case 0x00b9: /* Fn-PrtSc: Snipping tool */
290 map_key_clear(KEY_SELECTIVE_SCREENSHOT);
292 case 0x00b5: /* Fn-Esc: Fn-lock toggle */
293 map_key_clear(KEY_FN_ESC);
298 if ((usage->hid & HID_USAGE_PAGE) == 0xffa00000) {
299 switch (usage->hid & HID_USAGE) {
300 case 0x00fb: /* Middle mouse (in native USB mode) */
301 map_key_clear(BTN_MIDDLE);
306 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR &&
307 field->report->id == 21) {
308 switch (usage->hid & HID_USAGE) {
309 case 0x0004: /* Middle mouse (in native Bluetooth mode) */
310 map_key_clear(BTN_MIDDLE);
315 /* Compatibility middle/wheel mappings should be ignored */
316 if (usage->hid == HID_GD_WHEEL)
318 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
319 (usage->hid & HID_USAGE) == 0x003)
321 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
322 (usage->hid & HID_USAGE) == 0x238)
325 /* Map wheel emulation reports: 0xff10 */
326 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000) {
327 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
328 field->logical_minimum = -127;
329 field->logical_maximum = 127;
331 switch (usage->hid & HID_USAGE) {
333 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
336 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
346 static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
347 struct hid_input *hi, struct hid_field *field,
348 struct hid_usage *usage, unsigned long **bit, int *max)
350 if (usage->hid == HID_GD_Z) {
351 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
357 static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
358 struct hid_input *hi, struct hid_field *field,
359 struct hid_usage *usage, unsigned long **bit, int *max)
362 * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
363 * a bunch of keys which have no standard consumer page code.
365 if (usage->hid == 0x000c0001) {
366 switch (usage->usage_index) {
367 case 8: /* Fn-Esc: Fn-lock toggle */
368 map_key_clear(KEY_FN_ESC);
370 case 9: /* Fn-F4: Mic mute */
371 map_key_clear(LENOVO_KEY_MICMUTE);
373 case 10: /* Fn-F7: Control panel */
374 map_key_clear(KEY_CONFIG);
376 case 11: /* Fn-F8: Search (magnifier glass) */
377 map_key_clear(KEY_SEARCH);
379 case 12: /* Fn-F10: Open My computer (6 boxes) */
380 map_key_clear(KEY_FILE);
386 * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
387 * from suspend and it does not actually have a F23 key, ignore it.
389 if (usage->hid == 0x00070072)
395 static int lenovo_input_mapping_x1_tab_kbd(struct hid_device *hdev,
396 struct hid_input *hi, struct hid_field *field,
397 struct hid_usage *usage, unsigned long **bit, int *max)
400 * The ThinkPad X1 Tablet Thin Keyboard uses 0x000c0001 usage for
401 * a bunch of keys which have no standard consumer page code.
403 if (usage->hid == 0x000c0001) {
404 switch (usage->usage_index) {
405 case 0: /* Fn-F10: Enable/disable bluetooth */
406 map_key_clear(KEY_BLUETOOTH);
408 case 1: /* Fn-F11: Keyboard settings */
409 map_key_clear(KEY_KEYBOARD);
411 case 2: /* Fn-F12: User function / Cortana */
412 map_key_clear(KEY_MACRO1);
414 case 3: /* Fn-PrtSc: Snipping tool */
415 map_key_clear(KEY_SELECTIVE_SCREENSHOT);
417 case 8: /* Fn-Esc: Fn-lock toggle */
418 map_key_clear(KEY_FN_ESC);
420 case 9: /* Fn-F4: Mute/unmute microphone */
421 map_key_clear(KEY_MICMUTE);
423 case 10: /* Fn-F9: Settings */
424 map_key_clear(KEY_CONFIG);
426 case 13: /* Fn-F7: Manage external displays */
427 map_key_clear(KEY_SWITCHVIDEOMODE);
429 case 14: /* Fn-F8: Enable/disable wifi */
430 map_key_clear(KEY_WLAN);
435 if (usage->hid == (HID_UP_KEYBOARD | 0x009a)) {
436 map_key_clear(KEY_SYSRQ);
443 static int lenovo_input_mapping(struct hid_device *hdev,
444 struct hid_input *hi, struct hid_field *field,
445 struct hid_usage *usage, unsigned long **bit, int *max)
447 switch (hdev->product) {
448 case USB_DEVICE_ID_LENOVO_TPKBD:
449 return lenovo_input_mapping_tpkbd(hdev, hi, field,
451 case USB_DEVICE_ID_LENOVO_CUSBKBD:
452 case USB_DEVICE_ID_LENOVO_CBTKBD:
453 return lenovo_input_mapping_cptkbd(hdev, hi, field,
455 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
456 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
457 return lenovo_input_mapping_tpIIkbd(hdev, hi, field,
459 case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
460 case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
461 case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
462 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
463 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
464 case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
465 return lenovo_input_mapping_scrollpoint(hdev, hi, field,
467 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
468 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
470 case USB_DEVICE_ID_LENOVO_X1_TAB:
471 return lenovo_input_mapping_x1_tab_kbd(hdev, hi, field, usage, bit, max);
479 /* Send a config command to the keyboard */
480 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
481 unsigned char byte2, unsigned char byte3)
486 buf = kzalloc(3, GFP_KERNEL);
491 * Feature report 0x13 is used for USB,
492 * output report 0x18 is used for Bluetooth.
493 * buf[0] is ignored by hid_hw_raw_request.
499 switch (hdev->product) {
500 case USB_DEVICE_ID_LENOVO_CUSBKBD:
501 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
502 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
503 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
505 case USB_DEVICE_ID_LENOVO_CBTKBD:
506 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
507 ret = hid_hw_output_report(hdev, buf, 3);
516 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
519 static void lenovo_features_set_cptkbd(struct hid_device *hdev)
522 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
524 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
526 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
528 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
530 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
533 static ssize_t attr_fn_lock_show(struct device *dev,
534 struct device_attribute *attr,
537 struct hid_device *hdev = to_hid_device(dev);
538 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
540 return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
543 static ssize_t attr_fn_lock_store(struct device *dev,
544 struct device_attribute *attr,
548 struct hid_device *hdev = to_hid_device(dev);
549 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
552 if (kstrtoint(buf, 10, &value))
554 if (value < 0 || value > 1)
557 data->fn_lock = !!value;
559 switch (hdev->product) {
560 case USB_DEVICE_ID_LENOVO_CUSBKBD:
561 case USB_DEVICE_ID_LENOVO_CBTKBD:
562 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
563 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
564 lenovo_features_set_cptkbd(hdev);
566 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
567 case USB_DEVICE_ID_LENOVO_X1_TAB:
568 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
577 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
578 struct device_attribute *attr,
581 struct hid_device *hdev = to_hid_device(dev);
582 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
584 return snprintf(buf, PAGE_SIZE, "%u\n",
585 cptkbd_data->sensitivity);
588 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
589 struct device_attribute *attr,
593 struct hid_device *hdev = to_hid_device(dev);
594 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
597 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
600 cptkbd_data->sensitivity = value;
601 lenovo_features_set_cptkbd(hdev);
607 static struct device_attribute dev_attr_fn_lock =
608 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
612 static struct device_attribute dev_attr_sensitivity_cptkbd =
613 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
614 attr_sensitivity_show_cptkbd,
615 attr_sensitivity_store_cptkbd);
618 static struct attribute *lenovo_attributes_cptkbd[] = {
619 &dev_attr_fn_lock.attr,
620 &dev_attr_sensitivity_cptkbd.attr,
624 static const struct attribute_group lenovo_attr_group_cptkbd = {
625 .attrs = lenovo_attributes_cptkbd,
628 static int lenovo_raw_event(struct hid_device *hdev,
629 struct hid_report *report, u8 *data, int size)
632 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
633 * its own key is outside the usage page range. Remove extra
634 * keypresses and remap to inside usage page.
636 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
640 && data[2] == 0x01)) {
648 static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
649 struct hid_field *field, struct hid_usage *usage, __s32 value)
651 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
653 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
655 * The user has toggled the Fn-lock state. Toggle our own
656 * cached value of it and sync our value to the keyboard to
657 * ensure things are in sync (the sycning should be a no-op).
659 data->fn_lock = !data->fn_lock;
660 schedule_work(&data->fn_lock_sync_work);
666 static int lenovo_event_cptkbd(struct hid_device *hdev,
667 struct hid_field *field, struct hid_usage *usage, __s32 value)
669 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
671 /* "wheel" scroll events */
672 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
673 usage->code == REL_HWHEEL)) {
674 /* Scroll events disable middle-click event */
675 cptkbd_data->middlebutton_state = 2;
679 /* Middle click events */
680 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
682 cptkbd_data->middlebutton_state = 1;
683 } else if (value == 0) {
684 if (cptkbd_data->middlebutton_state == 1) {
685 /* No scrolling inbetween, send middle-click */
686 input_event(field->hidinput->input,
687 EV_KEY, BTN_MIDDLE, 1);
688 input_sync(field->hidinput->input);
689 input_event(field->hidinput->input,
690 EV_KEY, BTN_MIDDLE, 0);
691 input_sync(field->hidinput->input);
693 cptkbd_data->middlebutton_state = 0;
698 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
700 * The user has toggled the Fn-lock state. Toggle our own
701 * cached value of it and sync our value to the keyboard to
702 * ensure things are in sync (the syncing should be a no-op).
704 cptkbd_data->fn_lock = !cptkbd_data->fn_lock;
710 static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
711 struct hid_usage *usage, __s32 value)
713 if (!hid_get_drvdata(hdev))
716 switch (hdev->product) {
717 case USB_DEVICE_ID_LENOVO_CUSBKBD:
718 case USB_DEVICE_ID_LENOVO_CBTKBD:
719 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
720 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
721 return lenovo_event_cptkbd(hdev, field, usage, value);
722 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
723 case USB_DEVICE_ID_LENOVO_X1_TAB:
724 return lenovo_event_tp10ubkbd(hdev, field, usage, value);
730 static int lenovo_features_set_tpkbd(struct hid_device *hdev)
732 struct hid_report *report;
733 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
735 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
737 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
738 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
739 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
740 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
741 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
742 report->field[2]->value[0] = data_pointer->sensitivity;
743 report->field[3]->value[0] = data_pointer->press_speed;
745 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
749 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
750 struct device_attribute *attr,
753 struct hid_device *hdev = to_hid_device(dev);
754 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
756 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
759 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
760 struct device_attribute *attr,
764 struct hid_device *hdev = to_hid_device(dev);
765 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
768 if (kstrtoint(buf, 10, &value))
770 if (value < 0 || value > 1)
773 data_pointer->press_to_select = value;
774 lenovo_features_set_tpkbd(hdev);
779 static ssize_t attr_dragging_show_tpkbd(struct device *dev,
780 struct device_attribute *attr,
783 struct hid_device *hdev = to_hid_device(dev);
784 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
786 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
789 static ssize_t attr_dragging_store_tpkbd(struct device *dev,
790 struct device_attribute *attr,
794 struct hid_device *hdev = to_hid_device(dev);
795 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
798 if (kstrtoint(buf, 10, &value))
800 if (value < 0 || value > 1)
803 data_pointer->dragging = value;
804 lenovo_features_set_tpkbd(hdev);
809 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
810 struct device_attribute *attr,
813 struct hid_device *hdev = to_hid_device(dev);
814 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
816 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
819 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
820 struct device_attribute *attr,
824 struct hid_device *hdev = to_hid_device(dev);
825 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
828 if (kstrtoint(buf, 10, &value))
830 if (value < 0 || value > 1)
833 data_pointer->release_to_select = value;
834 lenovo_features_set_tpkbd(hdev);
839 static ssize_t attr_select_right_show_tpkbd(struct device *dev,
840 struct device_attribute *attr,
843 struct hid_device *hdev = to_hid_device(dev);
844 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
846 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
849 static ssize_t attr_select_right_store_tpkbd(struct device *dev,
850 struct device_attribute *attr,
854 struct hid_device *hdev = to_hid_device(dev);
855 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
858 if (kstrtoint(buf, 10, &value))
860 if (value < 0 || value > 1)
863 data_pointer->select_right = value;
864 lenovo_features_set_tpkbd(hdev);
869 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
870 struct device_attribute *attr,
873 struct hid_device *hdev = to_hid_device(dev);
874 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
876 return snprintf(buf, PAGE_SIZE, "%u\n",
877 data_pointer->sensitivity);
880 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
881 struct device_attribute *attr,
885 struct hid_device *hdev = to_hid_device(dev);
886 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
889 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
892 data_pointer->sensitivity = value;
893 lenovo_features_set_tpkbd(hdev);
898 static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
899 struct device_attribute *attr,
902 struct hid_device *hdev = to_hid_device(dev);
903 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
905 return snprintf(buf, PAGE_SIZE, "%u\n",
906 data_pointer->press_speed);
909 static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
910 struct device_attribute *attr,
914 struct hid_device *hdev = to_hid_device(dev);
915 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
918 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
921 data_pointer->press_speed = value;
922 lenovo_features_set_tpkbd(hdev);
927 static struct device_attribute dev_attr_press_to_select_tpkbd =
928 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
929 attr_press_to_select_show_tpkbd,
930 attr_press_to_select_store_tpkbd);
932 static struct device_attribute dev_attr_dragging_tpkbd =
933 __ATTR(dragging, S_IWUSR | S_IRUGO,
934 attr_dragging_show_tpkbd,
935 attr_dragging_store_tpkbd);
937 static struct device_attribute dev_attr_release_to_select_tpkbd =
938 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
939 attr_release_to_select_show_tpkbd,
940 attr_release_to_select_store_tpkbd);
942 static struct device_attribute dev_attr_select_right_tpkbd =
943 __ATTR(select_right, S_IWUSR | S_IRUGO,
944 attr_select_right_show_tpkbd,
945 attr_select_right_store_tpkbd);
947 static struct device_attribute dev_attr_sensitivity_tpkbd =
948 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
949 attr_sensitivity_show_tpkbd,
950 attr_sensitivity_store_tpkbd);
952 static struct device_attribute dev_attr_press_speed_tpkbd =
953 __ATTR(press_speed, S_IWUSR | S_IRUGO,
954 attr_press_speed_show_tpkbd,
955 attr_press_speed_store_tpkbd);
957 static struct attribute *lenovo_attributes_tpkbd[] = {
958 &dev_attr_press_to_select_tpkbd.attr,
959 &dev_attr_dragging_tpkbd.attr,
960 &dev_attr_release_to_select_tpkbd.attr,
961 &dev_attr_select_right_tpkbd.attr,
962 &dev_attr_sensitivity_tpkbd.attr,
963 &dev_attr_press_speed_tpkbd.attr,
967 static const struct attribute_group lenovo_attr_group_tpkbd = {
968 .attrs = lenovo_attributes_tpkbd,
971 static void lenovo_led_set_tpkbd(struct hid_device *hdev)
973 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
974 struct hid_report *report;
976 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
977 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
978 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
979 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
982 static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
983 enum led_brightness value)
985 struct device *dev = led_cdev->dev->parent;
986 struct hid_device *hdev = to_hid_device(dev);
987 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
988 static const u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
992 if (led_cdev == &data_pointer->led_micmute)
995 if (value == LED_OFF)
996 data_pointer->led_state &= ~(1 << led_nr);
998 data_pointer->led_state |= 1 << led_nr;
1000 switch (hdev->product) {
1001 case USB_DEVICE_ID_LENOVO_TPKBD:
1002 lenovo_led_set_tpkbd(hdev);
1004 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1005 case USB_DEVICE_ID_LENOVO_X1_TAB:
1006 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
1013 static int lenovo_register_leds(struct hid_device *hdev)
1015 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1016 size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
1017 char *name_mute, *name_micm;
1020 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1021 name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
1022 if (name_mute == NULL || name_micm == NULL) {
1023 hid_err(hdev, "Could not allocate memory for led data\n");
1026 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
1027 snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
1029 data->led_mute.name = name_mute;
1030 data->led_mute.default_trigger = "audio-mute";
1031 data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
1032 data->led_mute.max_brightness = 1;
1033 data->led_mute.flags = LED_HW_PLUGGABLE;
1034 data->led_mute.dev = &hdev->dev;
1035 ret = led_classdev_register(&hdev->dev, &data->led_mute);
1039 data->led_micmute.name = name_micm;
1040 data->led_micmute.default_trigger = "audio-micmute";
1041 data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
1042 data->led_micmute.max_brightness = 1;
1043 data->led_micmute.flags = LED_HW_PLUGGABLE;
1044 data->led_micmute.dev = &hdev->dev;
1045 ret = led_classdev_register(&hdev->dev, &data->led_micmute);
1047 led_classdev_unregister(&data->led_mute);
1054 static int lenovo_probe_tpkbd(struct hid_device *hdev)
1056 struct lenovo_drvdata *data_pointer;
1060 * Only register extra settings against subdevice where input_mapping
1061 * set drvdata to 1, i.e. the trackpoint.
1063 if (!hid_get_drvdata(hdev))
1066 hid_set_drvdata(hdev, NULL);
1068 /* Validate required reports. */
1069 for (i = 0; i < 4; i++) {
1070 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
1073 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
1076 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1078 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1080 data_pointer = devm_kzalloc(&hdev->dev,
1081 sizeof(struct lenovo_drvdata),
1083 if (data_pointer == NULL) {
1084 hid_err(hdev, "Could not allocate memory for driver data\n");
1089 // set same default values as windows driver
1090 data_pointer->sensitivity = 0xa0;
1091 data_pointer->press_speed = 0x38;
1093 hid_set_drvdata(hdev, data_pointer);
1095 ret = lenovo_register_leds(hdev);
1099 lenovo_features_set_tpkbd(hdev);
1103 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
1107 static int lenovo_probe_cptkbd(struct hid_device *hdev)
1110 struct lenovo_drvdata *cptkbd_data;
1112 /* All the custom action happens on the USBMOUSE device for USB */
1113 if (((hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD) ||
1114 (hdev->product == USB_DEVICE_ID_LENOVO_TPIIUSBKBD)) &&
1115 hdev->type != HID_TYPE_USBMOUSE) {
1116 hid_dbg(hdev, "Ignoring keyboard half of device\n");
1120 cptkbd_data = devm_kzalloc(&hdev->dev,
1121 sizeof(*cptkbd_data),
1123 if (cptkbd_data == NULL) {
1124 hid_err(hdev, "can't alloc keyboard descriptor\n");
1127 hid_set_drvdata(hdev, cptkbd_data);
1130 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
1131 * regular keys (Compact only)
1133 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD ||
1134 hdev->product == USB_DEVICE_ID_LENOVO_CBTKBD) {
1135 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
1137 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
1140 /* Switch middle button to native mode */
1141 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
1143 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
1145 /* Set keyboard settings to known state */
1146 cptkbd_data->middlebutton_state = 0;
1147 cptkbd_data->fn_lock = true;
1148 cptkbd_data->sensitivity = 0x05;
1149 lenovo_features_set_cptkbd(hdev);
1151 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
1153 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1158 static struct attribute *lenovo_attributes_tp10ubkbd[] = {
1159 &dev_attr_fn_lock.attr,
1163 static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
1164 .attrs = lenovo_attributes_tp10ubkbd,
1167 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
1169 struct hid_report_enum *rep_enum;
1170 struct lenovo_drvdata *data;
1171 struct hid_report *rep;
1176 * The LEDs and the Fn-lock functionality use output report 9,
1177 * with an application of 0xffa0001, add the LEDs on the interface
1178 * with this output report.
1181 rep_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1182 list_for_each_entry(rep, &rep_enum->report_list, list) {
1183 if (rep->application == 0xffa00001)
1189 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
1193 mutex_init(&data->led_report_mutex);
1194 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
1197 hid_set_drvdata(hdev, data);
1200 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
1201 * We cannot read the state, only set it, so we force it to on here
1202 * (which should be a no-op) to make sure that our state matches the
1203 * keyboard's FN-lock state. This is the same as what Windows does.
1205 data->fn_lock = true;
1206 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
1208 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1212 ret = lenovo_register_leds(hdev);
1218 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1222 static int lenovo_probe(struct hid_device *hdev,
1223 const struct hid_device_id *id)
1227 ret = hid_parse(hdev);
1229 hid_err(hdev, "hid_parse failed\n");
1233 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1235 hid_err(hdev, "hid_hw_start failed\n");
1239 switch (hdev->product) {
1240 case USB_DEVICE_ID_LENOVO_TPKBD:
1241 ret = lenovo_probe_tpkbd(hdev);
1243 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1244 case USB_DEVICE_ID_LENOVO_CBTKBD:
1245 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1246 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1247 ret = lenovo_probe_cptkbd(hdev);
1249 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1250 case USB_DEVICE_ID_LENOVO_X1_TAB:
1251 ret = lenovo_probe_tp10ubkbd(hdev);
1267 static void lenovo_remove_tpkbd(struct hid_device *hdev)
1269 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1272 * Only the trackpoint half of the keyboard has drvdata and stuff that
1273 * needs unregistering.
1275 if (data_pointer == NULL)
1278 sysfs_remove_group(&hdev->dev.kobj,
1279 &lenovo_attr_group_tpkbd);
1281 led_classdev_unregister(&data_pointer->led_micmute);
1282 led_classdev_unregister(&data_pointer->led_mute);
1285 static void lenovo_remove_cptkbd(struct hid_device *hdev)
1287 sysfs_remove_group(&hdev->dev.kobj,
1288 &lenovo_attr_group_cptkbd);
1291 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1293 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1298 led_classdev_unregister(&data->led_micmute);
1299 led_classdev_unregister(&data->led_mute);
1301 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1302 cancel_work_sync(&data->fn_lock_sync_work);
1305 static void lenovo_remove(struct hid_device *hdev)
1307 switch (hdev->product) {
1308 case USB_DEVICE_ID_LENOVO_TPKBD:
1309 lenovo_remove_tpkbd(hdev);
1311 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1312 case USB_DEVICE_ID_LENOVO_CBTKBD:
1313 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1314 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1315 lenovo_remove_cptkbd(hdev);
1317 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1318 case USB_DEVICE_ID_LENOVO_X1_TAB:
1319 lenovo_remove_tp10ubkbd(hdev);
1326 static int lenovo_input_configured(struct hid_device *hdev,
1327 struct hid_input *hi)
1329 switch (hdev->product) {
1330 case USB_DEVICE_ID_LENOVO_TPKBD:
1331 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1332 case USB_DEVICE_ID_LENOVO_CBTKBD:
1333 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
1334 case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
1335 if (test_bit(EV_REL, hi->input->evbit)) {
1336 /* set only for trackpoint device */
1337 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1338 __set_bit(INPUT_PROP_POINTING_STICK,
1339 hi->input->propbit);
1348 static const struct hid_device_id lenovo_devices[] = {
1349 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1350 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1351 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIUSBKBD) },
1352 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1353 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIBTKBD) },
1354 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1355 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1356 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1357 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1358 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1359 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1360 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1361 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1363 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1364 * part, while letting hid-multitouch.c handle the touchpad and trackpoint.
1366 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1367 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB) },
1371 MODULE_DEVICE_TABLE(hid, lenovo_devices);
1373 static struct hid_driver lenovo_driver = {
1375 .id_table = lenovo_devices,
1376 .input_configured = lenovo_input_configured,
1377 .input_mapping = lenovo_input_mapping,
1378 .probe = lenovo_probe,
1379 .remove = lenovo_remove,
1380 .raw_event = lenovo_raw_event,
1381 .event = lenovo_event,
1382 .report_fixup = lenovo_report_fixup,
1384 module_hid_driver(lenovo_driver);
1386 MODULE_LICENSE("GPL");