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)
8 * Copyright (c) 2012 Bernhard Seibold
9 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
11 * Linux IBM/Lenovo Scrollpoint mouse driver:
12 * - IBM Scrollpoint III
13 * - IBM Scrollpoint Pro
14 * - IBM Scrollpoint Optical
15 * - IBM Scrollpoint Optical 800dpi
16 * - IBM Scrollpoint Optical 800dpi Pro
17 * - Lenovo Scrollpoint Optical
19 * Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
20 * Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
26 #include <linux/module.h>
27 #include <linux/sysfs.h>
28 #include <linux/device.h>
29 #include <linux/hid.h>
30 #include <linux/input.h>
31 #include <linux/leds.h>
32 #include <linux/workqueue.h>
36 struct lenovo_drvdata {
37 u8 led_report[3]; /* Must be first for proper alignment */
39 struct mutex led_report_mutex;
40 struct led_classdev led_mute;
41 struct led_classdev led_micmute;
42 struct work_struct fn_lock_sync_work;
43 struct hid_device *hdev;
46 int release_to_select;
50 u8 middlebutton_state; /* 0:Up, 1:Down (undecided), 2:Scrolling */
54 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
56 #define TP10UBKBD_LED_OUTPUT_REPORT 9
58 #define TP10UBKBD_FN_LOCK_LED 0x54
59 #define TP10UBKBD_MUTE_LED 0x64
60 #define TP10UBKBD_MICMUTE_LED 0x74
62 #define TP10UBKBD_LED_OFF 1
63 #define TP10UBKBD_LED_ON 2
65 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
66 enum led_brightness value)
68 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
71 mutex_lock(&data->led_report_mutex);
73 data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT;
74 data->led_report[1] = led_code;
75 data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF;
76 ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3,
77 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
80 hid_err(hdev, "Set LED output report error: %d\n", ret);
82 ret = ret < 0 ? ret : -EIO;
87 mutex_unlock(&data->led_report_mutex);
92 static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
94 struct lenovo_drvdata *data =
95 container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
97 lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
101 static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
102 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */
103 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
104 0xa1, 0x01, /* Collection (Application) */
105 0x85, 0x04, /* Report ID (4) */
106 0x19, 0x00, /* Usage Minimum (0) */
107 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */
110 static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
113 switch (hdev->product) {
114 case USB_DEVICE_ID_LENOVO_TPPRODOCK:
115 /* the fixups that need to be done:
116 * - get a reasonable usage max for the vendor collection
117 * 0x8801 from the report ID 4
120 memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
121 sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
130 static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
131 struct hid_input *hi, struct hid_field *field,
132 struct hid_usage *usage, unsigned long **bit, int *max)
134 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
135 /* This sub-device contains trackpoint, mark it */
136 hid_set_drvdata(hdev, (void *)1);
137 map_key_clear(KEY_MICMUTE);
143 static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
144 struct hid_input *hi, struct hid_field *field,
145 struct hid_usage *usage, unsigned long **bit, int *max)
147 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
148 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
149 (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
150 switch (usage->hid & HID_USAGE) {
151 case 0x00f1: /* Fn-F4: Mic mute */
152 map_key_clear(KEY_MICMUTE);
154 case 0x00f2: /* Fn-F5: Brightness down */
155 map_key_clear(KEY_BRIGHTNESSDOWN);
157 case 0x00f3: /* Fn-F6: Brightness up */
158 map_key_clear(KEY_BRIGHTNESSUP);
160 case 0x00f4: /* Fn-F7: External display (projector) */
161 map_key_clear(KEY_SWITCHVIDEOMODE);
163 case 0x00f5: /* Fn-F8: Wireless */
164 map_key_clear(KEY_WLAN);
166 case 0x00f6: /* Fn-F9: Control panel */
167 map_key_clear(KEY_CONFIG);
169 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
170 map_key_clear(KEY_SCALE);
172 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
173 /* NB: This mapping is invented in raw_event below */
174 map_key_clear(KEY_FILE);
176 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
177 map_key_clear(KEY_FN_ESC);
179 case 0x00fb: /* Middle mouse button (in native mode) */
180 map_key_clear(BTN_MIDDLE);
185 /* Compatibility middle/wheel mappings should be ignored */
186 if (usage->hid == HID_GD_WHEEL)
188 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
189 (usage->hid & HID_USAGE) == 0x003)
191 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
192 (usage->hid & HID_USAGE) == 0x238)
195 /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
196 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
197 (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
198 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
199 field->logical_minimum = -127;
200 field->logical_maximum = 127;
202 switch (usage->hid & HID_USAGE) {
204 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
207 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
217 static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
218 struct hid_input *hi, struct hid_field *field,
219 struct hid_usage *usage, unsigned long **bit, int *max)
221 if (usage->hid == HID_GD_Z) {
222 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
228 static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
229 struct hid_input *hi, struct hid_field *field,
230 struct hid_usage *usage, unsigned long **bit, int *max)
233 * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
234 * a bunch of keys which have no standard consumer page code.
236 if (usage->hid == 0x000c0001) {
237 switch (usage->usage_index) {
238 case 8: /* Fn-Esc: Fn-lock toggle */
239 map_key_clear(KEY_FN_ESC);
241 case 9: /* Fn-F4: Mic mute */
242 map_key_clear(KEY_MICMUTE);
244 case 10: /* Fn-F7: Control panel */
245 map_key_clear(KEY_CONFIG);
247 case 11: /* Fn-F8: Search (magnifier glass) */
248 map_key_clear(KEY_SEARCH);
250 case 12: /* Fn-F10: Open My computer (6 boxes) */
251 map_key_clear(KEY_FILE);
257 * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
258 * from suspend and it does not actually have a F23 key, ignore it.
260 if (usage->hid == 0x00070072)
266 static int lenovo_input_mapping(struct hid_device *hdev,
267 struct hid_input *hi, struct hid_field *field,
268 struct hid_usage *usage, unsigned long **bit, int *max)
270 switch (hdev->product) {
271 case USB_DEVICE_ID_LENOVO_TPKBD:
272 return lenovo_input_mapping_tpkbd(hdev, hi, field,
274 case USB_DEVICE_ID_LENOVO_CUSBKBD:
275 case USB_DEVICE_ID_LENOVO_CBTKBD:
276 return lenovo_input_mapping_cptkbd(hdev, hi, field,
278 case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
279 case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
280 case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
281 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
282 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
283 case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
284 return lenovo_input_mapping_scrollpoint(hdev, hi, field,
286 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
287 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
296 /* Send a config command to the keyboard */
297 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
298 unsigned char byte2, unsigned char byte3)
303 buf = kzalloc(3, GFP_KERNEL);
311 switch (hdev->product) {
312 case USB_DEVICE_ID_LENOVO_CUSBKBD:
313 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
314 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
316 case USB_DEVICE_ID_LENOVO_CBTKBD:
317 ret = hid_hw_output_report(hdev, buf, 3);
326 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
329 static void lenovo_features_set_cptkbd(struct hid_device *hdev)
332 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
334 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
336 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
338 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
340 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
343 static ssize_t attr_fn_lock_show(struct device *dev,
344 struct device_attribute *attr,
347 struct hid_device *hdev = to_hid_device(dev);
348 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
350 return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
353 static ssize_t attr_fn_lock_store(struct device *dev,
354 struct device_attribute *attr,
358 struct hid_device *hdev = to_hid_device(dev);
359 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
362 if (kstrtoint(buf, 10, &value))
364 if (value < 0 || value > 1)
367 data->fn_lock = !!value;
369 switch (hdev->product) {
370 case USB_DEVICE_ID_LENOVO_CUSBKBD:
371 case USB_DEVICE_ID_LENOVO_CBTKBD:
372 lenovo_features_set_cptkbd(hdev);
374 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
375 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
384 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
385 struct device_attribute *attr,
388 struct hid_device *hdev = to_hid_device(dev);
389 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
391 return snprintf(buf, PAGE_SIZE, "%u\n",
392 cptkbd_data->sensitivity);
395 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
396 struct device_attribute *attr,
400 struct hid_device *hdev = to_hid_device(dev);
401 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
404 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
407 cptkbd_data->sensitivity = value;
408 lenovo_features_set_cptkbd(hdev);
414 static struct device_attribute dev_attr_fn_lock =
415 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
419 static struct device_attribute dev_attr_sensitivity_cptkbd =
420 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
421 attr_sensitivity_show_cptkbd,
422 attr_sensitivity_store_cptkbd);
425 static struct attribute *lenovo_attributes_cptkbd[] = {
426 &dev_attr_fn_lock.attr,
427 &dev_attr_sensitivity_cptkbd.attr,
431 static const struct attribute_group lenovo_attr_group_cptkbd = {
432 .attrs = lenovo_attributes_cptkbd,
435 static int lenovo_raw_event(struct hid_device *hdev,
436 struct hid_report *report, u8 *data, int size)
439 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
440 * its own key is outside the usage page range. Remove extra
441 * keypresses and remap to inside usage page.
443 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
447 && data[2] == 0x01)) {
455 static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
456 struct hid_field *field, struct hid_usage *usage, __s32 value)
458 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
460 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
462 * The user has toggled the Fn-lock state. Toggle our own
463 * cached value of it and sync our value to the keyboard to
464 * ensure things are in sync (the sycning should be a no-op).
466 data->fn_lock = !data->fn_lock;
467 schedule_work(&data->fn_lock_sync_work);
473 static int lenovo_event_cptkbd(struct hid_device *hdev,
474 struct hid_field *field, struct hid_usage *usage, __s32 value)
476 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
478 /* "wheel" scroll events */
479 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
480 usage->code == REL_HWHEEL)) {
481 /* Scroll events disable middle-click event */
482 cptkbd_data->middlebutton_state = 2;
486 /* Middle click events */
487 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
489 cptkbd_data->middlebutton_state = 1;
490 } else if (value == 0) {
491 if (cptkbd_data->middlebutton_state == 1) {
492 /* No scrolling inbetween, send middle-click */
493 input_event(field->hidinput->input,
494 EV_KEY, BTN_MIDDLE, 1);
495 input_sync(field->hidinput->input);
496 input_event(field->hidinput->input,
497 EV_KEY, BTN_MIDDLE, 0);
498 input_sync(field->hidinput->input);
500 cptkbd_data->middlebutton_state = 0;
508 static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
509 struct hid_usage *usage, __s32 value)
511 if (!hid_get_drvdata(hdev))
514 switch (hdev->product) {
515 case USB_DEVICE_ID_LENOVO_CUSBKBD:
516 case USB_DEVICE_ID_LENOVO_CBTKBD:
517 return lenovo_event_cptkbd(hdev, field, usage, value);
518 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
519 return lenovo_event_tp10ubkbd(hdev, field, usage, value);
525 static int lenovo_features_set_tpkbd(struct hid_device *hdev)
527 struct hid_report *report;
528 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
530 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
532 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
533 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
534 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
535 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
536 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
537 report->field[2]->value[0] = data_pointer->sensitivity;
538 report->field[3]->value[0] = data_pointer->press_speed;
540 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
544 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
545 struct device_attribute *attr,
548 struct hid_device *hdev = to_hid_device(dev);
549 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
551 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
554 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
555 struct device_attribute *attr,
559 struct hid_device *hdev = to_hid_device(dev);
560 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
563 if (kstrtoint(buf, 10, &value))
565 if (value < 0 || value > 1)
568 data_pointer->press_to_select = value;
569 lenovo_features_set_tpkbd(hdev);
574 static ssize_t attr_dragging_show_tpkbd(struct device *dev,
575 struct device_attribute *attr,
578 struct hid_device *hdev = to_hid_device(dev);
579 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
581 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
584 static ssize_t attr_dragging_store_tpkbd(struct device *dev,
585 struct device_attribute *attr,
589 struct hid_device *hdev = to_hid_device(dev);
590 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
593 if (kstrtoint(buf, 10, &value))
595 if (value < 0 || value > 1)
598 data_pointer->dragging = value;
599 lenovo_features_set_tpkbd(hdev);
604 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
605 struct device_attribute *attr,
608 struct hid_device *hdev = to_hid_device(dev);
609 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
611 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
614 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
615 struct device_attribute *attr,
619 struct hid_device *hdev = to_hid_device(dev);
620 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
623 if (kstrtoint(buf, 10, &value))
625 if (value < 0 || value > 1)
628 data_pointer->release_to_select = value;
629 lenovo_features_set_tpkbd(hdev);
634 static ssize_t attr_select_right_show_tpkbd(struct device *dev,
635 struct device_attribute *attr,
638 struct hid_device *hdev = to_hid_device(dev);
639 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
641 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
644 static ssize_t attr_select_right_store_tpkbd(struct device *dev,
645 struct device_attribute *attr,
649 struct hid_device *hdev = to_hid_device(dev);
650 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
653 if (kstrtoint(buf, 10, &value))
655 if (value < 0 || value > 1)
658 data_pointer->select_right = value;
659 lenovo_features_set_tpkbd(hdev);
664 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
665 struct device_attribute *attr,
668 struct hid_device *hdev = to_hid_device(dev);
669 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
671 return snprintf(buf, PAGE_SIZE, "%u\n",
672 data_pointer->sensitivity);
675 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
676 struct device_attribute *attr,
680 struct hid_device *hdev = to_hid_device(dev);
681 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
684 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
687 data_pointer->sensitivity = value;
688 lenovo_features_set_tpkbd(hdev);
693 static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
694 struct device_attribute *attr,
697 struct hid_device *hdev = to_hid_device(dev);
698 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
700 return snprintf(buf, PAGE_SIZE, "%u\n",
701 data_pointer->press_speed);
704 static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
705 struct device_attribute *attr,
709 struct hid_device *hdev = to_hid_device(dev);
710 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
713 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
716 data_pointer->press_speed = value;
717 lenovo_features_set_tpkbd(hdev);
722 static struct device_attribute dev_attr_press_to_select_tpkbd =
723 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
724 attr_press_to_select_show_tpkbd,
725 attr_press_to_select_store_tpkbd);
727 static struct device_attribute dev_attr_dragging_tpkbd =
728 __ATTR(dragging, S_IWUSR | S_IRUGO,
729 attr_dragging_show_tpkbd,
730 attr_dragging_store_tpkbd);
732 static struct device_attribute dev_attr_release_to_select_tpkbd =
733 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
734 attr_release_to_select_show_tpkbd,
735 attr_release_to_select_store_tpkbd);
737 static struct device_attribute dev_attr_select_right_tpkbd =
738 __ATTR(select_right, S_IWUSR | S_IRUGO,
739 attr_select_right_show_tpkbd,
740 attr_select_right_store_tpkbd);
742 static struct device_attribute dev_attr_sensitivity_tpkbd =
743 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
744 attr_sensitivity_show_tpkbd,
745 attr_sensitivity_store_tpkbd);
747 static struct device_attribute dev_attr_press_speed_tpkbd =
748 __ATTR(press_speed, S_IWUSR | S_IRUGO,
749 attr_press_speed_show_tpkbd,
750 attr_press_speed_store_tpkbd);
752 static struct attribute *lenovo_attributes_tpkbd[] = {
753 &dev_attr_press_to_select_tpkbd.attr,
754 &dev_attr_dragging_tpkbd.attr,
755 &dev_attr_release_to_select_tpkbd.attr,
756 &dev_attr_select_right_tpkbd.attr,
757 &dev_attr_sensitivity_tpkbd.attr,
758 &dev_attr_press_speed_tpkbd.attr,
762 static const struct attribute_group lenovo_attr_group_tpkbd = {
763 .attrs = lenovo_attributes_tpkbd,
766 static void lenovo_led_set_tpkbd(struct hid_device *hdev)
768 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
769 struct hid_report *report;
771 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
772 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
773 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
774 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
777 static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
778 enum led_brightness value)
780 struct device *dev = led_cdev->dev->parent;
781 struct hid_device *hdev = to_hid_device(dev);
782 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
783 u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
787 if (led_cdev == &data_pointer->led_micmute)
790 if (value == LED_OFF)
791 data_pointer->led_state &= ~(1 << led_nr);
793 data_pointer->led_state |= 1 << led_nr;
795 switch (hdev->product) {
796 case USB_DEVICE_ID_LENOVO_TPKBD:
797 lenovo_led_set_tpkbd(hdev);
799 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
800 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
807 static int lenovo_register_leds(struct hid_device *hdev)
809 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
810 size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
811 char *name_mute, *name_micm;
814 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
815 name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
816 if (name_mute == NULL || name_micm == NULL) {
817 hid_err(hdev, "Could not allocate memory for led data\n");
820 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
821 snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
823 data->led_mute.name = name_mute;
824 data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
825 data->led_mute.max_brightness = 1;
826 data->led_mute.flags = LED_HW_PLUGGABLE;
827 data->led_mute.dev = &hdev->dev;
828 ret = led_classdev_register(&hdev->dev, &data->led_mute);
832 data->led_micmute.name = name_micm;
833 data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
834 data->led_micmute.max_brightness = 1;
835 data->led_micmute.flags = LED_HW_PLUGGABLE;
836 data->led_micmute.dev = &hdev->dev;
837 ret = led_classdev_register(&hdev->dev, &data->led_micmute);
839 led_classdev_unregister(&data->led_mute);
846 static int lenovo_probe_tpkbd(struct hid_device *hdev)
848 struct lenovo_drvdata *data_pointer;
852 * Only register extra settings against subdevice where input_mapping
853 * set drvdata to 1, i.e. the trackpoint.
855 if (!hid_get_drvdata(hdev))
858 hid_set_drvdata(hdev, NULL);
860 /* Validate required reports. */
861 for (i = 0; i < 4; i++) {
862 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
865 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
868 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
870 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
872 data_pointer = devm_kzalloc(&hdev->dev,
873 sizeof(struct lenovo_drvdata),
875 if (data_pointer == NULL) {
876 hid_err(hdev, "Could not allocate memory for driver data\n");
881 // set same default values as windows driver
882 data_pointer->sensitivity = 0xa0;
883 data_pointer->press_speed = 0x38;
885 hid_set_drvdata(hdev, data_pointer);
887 ret = lenovo_register_leds(hdev);
891 lenovo_features_set_tpkbd(hdev);
895 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
899 static int lenovo_probe_cptkbd(struct hid_device *hdev)
902 struct lenovo_drvdata *cptkbd_data;
904 /* All the custom action happens on the USBMOUSE device for USB */
905 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
906 && hdev->type != HID_TYPE_USBMOUSE) {
907 hid_dbg(hdev, "Ignoring keyboard half of device\n");
911 cptkbd_data = devm_kzalloc(&hdev->dev,
912 sizeof(*cptkbd_data),
914 if (cptkbd_data == NULL) {
915 hid_err(hdev, "can't alloc keyboard descriptor\n");
918 hid_set_drvdata(hdev, cptkbd_data);
921 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
924 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
926 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
928 /* Switch middle button to native mode */
929 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
931 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
933 /* Set keyboard settings to known state */
934 cptkbd_data->middlebutton_state = 0;
935 cptkbd_data->fn_lock = true;
936 cptkbd_data->sensitivity = 0x05;
937 lenovo_features_set_cptkbd(hdev);
939 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
941 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
946 static struct attribute *lenovo_attributes_tp10ubkbd[] = {
947 &dev_attr_fn_lock.attr,
951 static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
952 .attrs = lenovo_attributes_tp10ubkbd,
955 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
957 struct lenovo_drvdata *data;
960 /* All the custom action happens on the USBMOUSE device for USB */
961 if (hdev->type != HID_TYPE_USBMOUSE)
964 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
968 mutex_init(&data->led_report_mutex);
969 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
972 hid_set_drvdata(hdev, data);
975 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
976 * We cannot read the state, only set it, so we force it to on here
977 * (which should be a no-op) to make sure that our state matches the
978 * keyboard's FN-lock state. This is the same as what Windows does.
980 data->fn_lock = true;
981 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
983 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
987 ret = lenovo_register_leds(hdev);
993 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
997 static int lenovo_probe(struct hid_device *hdev,
998 const struct hid_device_id *id)
1002 ret = hid_parse(hdev);
1004 hid_err(hdev, "hid_parse failed\n");
1008 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1010 hid_err(hdev, "hid_hw_start failed\n");
1014 switch (hdev->product) {
1015 case USB_DEVICE_ID_LENOVO_TPKBD:
1016 ret = lenovo_probe_tpkbd(hdev);
1018 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1019 case USB_DEVICE_ID_LENOVO_CBTKBD:
1020 ret = lenovo_probe_cptkbd(hdev);
1022 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1023 ret = lenovo_probe_tp10ubkbd(hdev);
1039 static void lenovo_remove_tpkbd(struct hid_device *hdev)
1041 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1044 * Only the trackpoint half of the keyboard has drvdata and stuff that
1045 * needs unregistering.
1047 if (data_pointer == NULL)
1050 sysfs_remove_group(&hdev->dev.kobj,
1051 &lenovo_attr_group_tpkbd);
1053 led_classdev_unregister(&data_pointer->led_micmute);
1054 led_classdev_unregister(&data_pointer->led_mute);
1057 static void lenovo_remove_cptkbd(struct hid_device *hdev)
1059 sysfs_remove_group(&hdev->dev.kobj,
1060 &lenovo_attr_group_cptkbd);
1063 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1065 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1070 led_classdev_unregister(&data->led_micmute);
1071 led_classdev_unregister(&data->led_mute);
1073 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1074 cancel_work_sync(&data->fn_lock_sync_work);
1077 static void lenovo_remove(struct hid_device *hdev)
1079 switch (hdev->product) {
1080 case USB_DEVICE_ID_LENOVO_TPKBD:
1081 lenovo_remove_tpkbd(hdev);
1083 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1084 case USB_DEVICE_ID_LENOVO_CBTKBD:
1085 lenovo_remove_cptkbd(hdev);
1087 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1088 lenovo_remove_tp10ubkbd(hdev);
1095 static int lenovo_input_configured(struct hid_device *hdev,
1096 struct hid_input *hi)
1098 switch (hdev->product) {
1099 case USB_DEVICE_ID_LENOVO_TPKBD:
1100 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1101 case USB_DEVICE_ID_LENOVO_CBTKBD:
1102 if (test_bit(EV_REL, hi->input->evbit)) {
1103 /* set only for trackpoint device */
1104 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1105 __set_bit(INPUT_PROP_POINTING_STICK,
1106 hi->input->propbit);
1115 static const struct hid_device_id lenovo_devices[] = {
1116 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1117 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1118 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1119 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1120 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1121 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1122 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1123 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1124 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1125 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1126 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1130 MODULE_DEVICE_TABLE(hid, lenovo_devices);
1132 static struct hid_driver lenovo_driver = {
1134 .id_table = lenovo_devices,
1135 .input_configured = lenovo_input_configured,
1136 .input_mapping = lenovo_input_mapping,
1137 .probe = lenovo_probe,
1138 .remove = lenovo_remove,
1139 .raw_event = lenovo_raw_event,
1140 .event = lenovo_event,
1141 .report_fixup = lenovo_report_fixup,
1143 module_hid_driver(lenovo_driver);
1145 MODULE_LICENSE("GPL");