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 /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */
37 #define LENOVO_KEY_MICMUTE KEY_F20
39 struct lenovo_drvdata {
40 u8 led_report[3]; /* Must be first for proper alignment */
42 struct mutex led_report_mutex;
43 struct led_classdev led_mute;
44 struct led_classdev led_micmute;
45 struct work_struct fn_lock_sync_work;
46 struct hid_device *hdev;
49 int release_to_select;
57 u8 middlebutton_state;
59 bool middleclick_workaround_cptkbd;
62 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
64 #define TP10UBKBD_LED_OUTPUT_REPORT 9
66 #define TP10UBKBD_FN_LOCK_LED 0x54
67 #define TP10UBKBD_MUTE_LED 0x64
68 #define TP10UBKBD_MICMUTE_LED 0x74
70 #define TP10UBKBD_LED_OFF 1
71 #define TP10UBKBD_LED_ON 2
73 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
74 enum led_brightness value)
76 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
79 mutex_lock(&data->led_report_mutex);
81 data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT;
82 data->led_report[1] = led_code;
83 data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF;
84 ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3,
85 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
88 hid_err(hdev, "Set LED output report error: %d\n", ret);
90 ret = ret < 0 ? ret : -EIO;
95 mutex_unlock(&data->led_report_mutex);
100 static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
102 struct lenovo_drvdata *data =
103 container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
105 lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
109 static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
110 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */
111 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
112 0xa1, 0x01, /* Collection (Application) */
113 0x85, 0x04, /* Report ID (4) */
114 0x19, 0x00, /* Usage Minimum (0) */
115 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */
118 static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
121 switch (hdev->product) {
122 case USB_DEVICE_ID_LENOVO_TPPRODOCK:
123 /* the fixups that need to be done:
124 * - get a reasonable usage max for the vendor collection
125 * 0x8801 from the report ID 4
128 memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
129 sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
138 static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
139 struct hid_input *hi, struct hid_field *field,
140 struct hid_usage *usage, unsigned long **bit, int *max)
142 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
143 /* This sub-device contains trackpoint, mark it */
144 hid_set_drvdata(hdev, (void *)1);
145 map_key_clear(LENOVO_KEY_MICMUTE);
151 static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
152 struct hid_input *hi, struct hid_field *field,
153 struct hid_usage *usage, unsigned long **bit, int *max)
155 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
156 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
157 (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
158 switch (usage->hid & HID_USAGE) {
159 case 0x00f1: /* Fn-F4: Mic mute */
160 map_key_clear(LENOVO_KEY_MICMUTE);
162 case 0x00f2: /* Fn-F5: Brightness down */
163 map_key_clear(KEY_BRIGHTNESSDOWN);
165 case 0x00f3: /* Fn-F6: Brightness up */
166 map_key_clear(KEY_BRIGHTNESSUP);
168 case 0x00f4: /* Fn-F7: External display (projector) */
169 map_key_clear(KEY_SWITCHVIDEOMODE);
171 case 0x00f5: /* Fn-F8: Wireless */
172 map_key_clear(KEY_WLAN);
174 case 0x00f6: /* Fn-F9: Control panel */
175 map_key_clear(KEY_CONFIG);
177 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
178 map_key_clear(KEY_SCALE);
180 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
181 /* NB: This mapping is invented in raw_event below */
182 map_key_clear(KEY_FILE);
184 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
185 map_key_clear(KEY_FN_ESC);
187 case 0x00fb: /* Middle mouse button (in native mode) */
188 map_key_clear(BTN_MIDDLE);
193 /* Compatibility middle/wheel mappings should be ignored */
194 if (usage->hid == HID_GD_WHEEL)
196 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
197 (usage->hid & HID_USAGE) == 0x003)
199 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
200 (usage->hid & HID_USAGE) == 0x238)
203 /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
204 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
205 (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
206 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
207 field->logical_minimum = -127;
208 field->logical_maximum = 127;
210 switch (usage->hid & HID_USAGE) {
212 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
215 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
225 static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
226 struct hid_input *hi, struct hid_field *field,
227 struct hid_usage *usage, unsigned long **bit, int *max)
229 if (usage->hid == HID_GD_Z) {
230 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
236 static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
237 struct hid_input *hi, struct hid_field *field,
238 struct hid_usage *usage, unsigned long **bit, int *max)
241 * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
242 * a bunch of keys which have no standard consumer page code.
244 if (usage->hid == 0x000c0001) {
245 switch (usage->usage_index) {
246 case 8: /* Fn-Esc: Fn-lock toggle */
247 map_key_clear(KEY_FN_ESC);
249 case 9: /* Fn-F4: Mic mute */
250 map_key_clear(LENOVO_KEY_MICMUTE);
252 case 10: /* Fn-F7: Control panel */
253 map_key_clear(KEY_CONFIG);
255 case 11: /* Fn-F8: Search (magnifier glass) */
256 map_key_clear(KEY_SEARCH);
258 case 12: /* Fn-F10: Open My computer (6 boxes) */
259 map_key_clear(KEY_FILE);
265 * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
266 * from suspend and it does not actually have a F23 key, ignore it.
268 if (usage->hid == 0x00070072)
274 static int lenovo_input_mapping(struct hid_device *hdev,
275 struct hid_input *hi, struct hid_field *field,
276 struct hid_usage *usage, unsigned long **bit, int *max)
278 switch (hdev->product) {
279 case USB_DEVICE_ID_LENOVO_TPKBD:
280 return lenovo_input_mapping_tpkbd(hdev, hi, field,
282 case USB_DEVICE_ID_LENOVO_CUSBKBD:
283 case USB_DEVICE_ID_LENOVO_CBTKBD:
284 return lenovo_input_mapping_cptkbd(hdev, hi, field,
286 case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
287 case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
288 case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
289 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
290 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
291 case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
292 return lenovo_input_mapping_scrollpoint(hdev, hi, field,
294 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
295 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
304 /* Send a config command to the keyboard */
305 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
306 unsigned char byte2, unsigned char byte3)
311 buf = kzalloc(3, GFP_KERNEL);
319 switch (hdev->product) {
320 case USB_DEVICE_ID_LENOVO_CUSBKBD:
321 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
322 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
324 case USB_DEVICE_ID_LENOVO_CBTKBD:
325 ret = hid_hw_output_report(hdev, buf, 3);
334 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
337 static void lenovo_features_set_cptkbd(struct hid_device *hdev)
340 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
342 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
344 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
346 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
348 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
351 static ssize_t attr_fn_lock_show(struct device *dev,
352 struct device_attribute *attr,
355 struct hid_device *hdev = to_hid_device(dev);
356 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
358 return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
361 static ssize_t attr_fn_lock_store(struct device *dev,
362 struct device_attribute *attr,
366 struct hid_device *hdev = to_hid_device(dev);
367 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
370 if (kstrtoint(buf, 10, &value))
372 if (value < 0 || value > 1)
375 data->fn_lock = !!value;
377 switch (hdev->product) {
378 case USB_DEVICE_ID_LENOVO_CUSBKBD:
379 case USB_DEVICE_ID_LENOVO_CBTKBD:
380 lenovo_features_set_cptkbd(hdev);
382 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
383 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
392 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
393 struct device_attribute *attr,
396 struct hid_device *hdev = to_hid_device(dev);
397 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
399 return snprintf(buf, PAGE_SIZE, "%u\n",
400 cptkbd_data->sensitivity);
403 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
404 struct device_attribute *attr,
408 struct hid_device *hdev = to_hid_device(dev);
409 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
412 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
415 cptkbd_data->sensitivity = value;
416 lenovo_features_set_cptkbd(hdev);
421 static ssize_t attr_middleclick_workaround_show_cptkbd(struct device *dev,
422 struct device_attribute *attr,
425 struct hid_device *hdev = to_hid_device(dev);
426 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
428 return snprintf(buf, PAGE_SIZE, "%u\n",
429 cptkbd_data->middleclick_workaround_cptkbd);
432 static ssize_t attr_middleclick_workaround_store_cptkbd(struct device *dev,
433 struct device_attribute *attr,
437 struct hid_device *hdev = to_hid_device(dev);
438 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
441 if (kstrtoint(buf, 10, &value))
443 if (value < 0 || value > 1)
446 cptkbd_data->middleclick_workaround_cptkbd = !!value;
452 static struct device_attribute dev_attr_fn_lock =
453 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
457 static struct device_attribute dev_attr_sensitivity_cptkbd =
458 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
459 attr_sensitivity_show_cptkbd,
460 attr_sensitivity_store_cptkbd);
462 static struct device_attribute dev_attr_middleclick_workaround_cptkbd =
463 __ATTR(middleclick_workaround, S_IWUSR | S_IRUGO,
464 attr_middleclick_workaround_show_cptkbd,
465 attr_middleclick_workaround_store_cptkbd);
468 static struct attribute *lenovo_attributes_cptkbd[] = {
469 &dev_attr_fn_lock.attr,
470 &dev_attr_sensitivity_cptkbd.attr,
471 &dev_attr_middleclick_workaround_cptkbd.attr,
475 static const struct attribute_group lenovo_attr_group_cptkbd = {
476 .attrs = lenovo_attributes_cptkbd,
479 static int lenovo_raw_event(struct hid_device *hdev,
480 struct hid_report *report, u8 *data, int size)
483 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
484 * its own key is outside the usage page range. Remove extra
485 * keypresses and remap to inside usage page.
487 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
491 && data[2] == 0x01)) {
499 static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
500 struct hid_field *field, struct hid_usage *usage, __s32 value)
502 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
504 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
506 * The user has toggled the Fn-lock state. Toggle our own
507 * cached value of it and sync our value to the keyboard to
508 * ensure things are in sync (the sycning should be a no-op).
510 data->fn_lock = !data->fn_lock;
511 schedule_work(&data->fn_lock_sync_work);
517 static int lenovo_event_cptkbd(struct hid_device *hdev,
518 struct hid_field *field, struct hid_usage *usage, __s32 value)
520 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
522 if (cptkbd_data->middleclick_workaround_cptkbd) {
523 /* "wheel" scroll events */
524 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
525 usage->code == REL_HWHEEL)) {
526 /* Scroll events disable middle-click event */
527 cptkbd_data->middlebutton_state = 2;
531 /* Middle click events */
532 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
534 cptkbd_data->middlebutton_state = 1;
535 } else if (value == 0) {
536 if (cptkbd_data->middlebutton_state == 1) {
537 /* No scrolling inbetween, send middle-click */
538 input_event(field->hidinput->input,
539 EV_KEY, BTN_MIDDLE, 1);
540 input_sync(field->hidinput->input);
541 input_event(field->hidinput->input,
542 EV_KEY, BTN_MIDDLE, 0);
543 input_sync(field->hidinput->input);
545 cptkbd_data->middlebutton_state = 0;
554 static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
555 struct hid_usage *usage, __s32 value)
557 if (!hid_get_drvdata(hdev))
560 switch (hdev->product) {
561 case USB_DEVICE_ID_LENOVO_CUSBKBD:
562 case USB_DEVICE_ID_LENOVO_CBTKBD:
563 return lenovo_event_cptkbd(hdev, field, usage, value);
564 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
565 return lenovo_event_tp10ubkbd(hdev, field, usage, value);
571 static int lenovo_features_set_tpkbd(struct hid_device *hdev)
573 struct hid_report *report;
574 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
576 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
578 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
579 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
580 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
581 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
582 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
583 report->field[2]->value[0] = data_pointer->sensitivity;
584 report->field[3]->value[0] = data_pointer->press_speed;
586 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
590 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
591 struct device_attribute *attr,
594 struct hid_device *hdev = to_hid_device(dev);
595 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
597 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
600 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
601 struct device_attribute *attr,
605 struct hid_device *hdev = to_hid_device(dev);
606 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
609 if (kstrtoint(buf, 10, &value))
611 if (value < 0 || value > 1)
614 data_pointer->press_to_select = value;
615 lenovo_features_set_tpkbd(hdev);
620 static ssize_t attr_dragging_show_tpkbd(struct device *dev,
621 struct device_attribute *attr,
624 struct hid_device *hdev = to_hid_device(dev);
625 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
627 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
630 static ssize_t attr_dragging_store_tpkbd(struct device *dev,
631 struct device_attribute *attr,
635 struct hid_device *hdev = to_hid_device(dev);
636 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
639 if (kstrtoint(buf, 10, &value))
641 if (value < 0 || value > 1)
644 data_pointer->dragging = value;
645 lenovo_features_set_tpkbd(hdev);
650 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
651 struct device_attribute *attr,
654 struct hid_device *hdev = to_hid_device(dev);
655 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
657 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
660 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
661 struct device_attribute *attr,
665 struct hid_device *hdev = to_hid_device(dev);
666 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
669 if (kstrtoint(buf, 10, &value))
671 if (value < 0 || value > 1)
674 data_pointer->release_to_select = value;
675 lenovo_features_set_tpkbd(hdev);
680 static ssize_t attr_select_right_show_tpkbd(struct device *dev,
681 struct device_attribute *attr,
684 struct hid_device *hdev = to_hid_device(dev);
685 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
687 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
690 static ssize_t attr_select_right_store_tpkbd(struct device *dev,
691 struct device_attribute *attr,
695 struct hid_device *hdev = to_hid_device(dev);
696 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
699 if (kstrtoint(buf, 10, &value))
701 if (value < 0 || value > 1)
704 data_pointer->select_right = value;
705 lenovo_features_set_tpkbd(hdev);
710 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
711 struct device_attribute *attr,
714 struct hid_device *hdev = to_hid_device(dev);
715 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
717 return snprintf(buf, PAGE_SIZE, "%u\n",
718 data_pointer->sensitivity);
721 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
722 struct device_attribute *attr,
726 struct hid_device *hdev = to_hid_device(dev);
727 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
730 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
733 data_pointer->sensitivity = value;
734 lenovo_features_set_tpkbd(hdev);
739 static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
740 struct device_attribute *attr,
743 struct hid_device *hdev = to_hid_device(dev);
744 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
746 return snprintf(buf, PAGE_SIZE, "%u\n",
747 data_pointer->press_speed);
750 static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
751 struct device_attribute *attr,
755 struct hid_device *hdev = to_hid_device(dev);
756 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
759 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
762 data_pointer->press_speed = value;
763 lenovo_features_set_tpkbd(hdev);
768 static struct device_attribute dev_attr_press_to_select_tpkbd =
769 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
770 attr_press_to_select_show_tpkbd,
771 attr_press_to_select_store_tpkbd);
773 static struct device_attribute dev_attr_dragging_tpkbd =
774 __ATTR(dragging, S_IWUSR | S_IRUGO,
775 attr_dragging_show_tpkbd,
776 attr_dragging_store_tpkbd);
778 static struct device_attribute dev_attr_release_to_select_tpkbd =
779 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
780 attr_release_to_select_show_tpkbd,
781 attr_release_to_select_store_tpkbd);
783 static struct device_attribute dev_attr_select_right_tpkbd =
784 __ATTR(select_right, S_IWUSR | S_IRUGO,
785 attr_select_right_show_tpkbd,
786 attr_select_right_store_tpkbd);
788 static struct device_attribute dev_attr_sensitivity_tpkbd =
789 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
790 attr_sensitivity_show_tpkbd,
791 attr_sensitivity_store_tpkbd);
793 static struct device_attribute dev_attr_press_speed_tpkbd =
794 __ATTR(press_speed, S_IWUSR | S_IRUGO,
795 attr_press_speed_show_tpkbd,
796 attr_press_speed_store_tpkbd);
798 static struct attribute *lenovo_attributes_tpkbd[] = {
799 &dev_attr_press_to_select_tpkbd.attr,
800 &dev_attr_dragging_tpkbd.attr,
801 &dev_attr_release_to_select_tpkbd.attr,
802 &dev_attr_select_right_tpkbd.attr,
803 &dev_attr_sensitivity_tpkbd.attr,
804 &dev_attr_press_speed_tpkbd.attr,
808 static const struct attribute_group lenovo_attr_group_tpkbd = {
809 .attrs = lenovo_attributes_tpkbd,
812 static void lenovo_led_set_tpkbd(struct hid_device *hdev)
814 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
815 struct hid_report *report;
817 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
818 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
819 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
820 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
823 static enum led_brightness lenovo_led_brightness_get(
824 struct led_classdev *led_cdev)
826 struct device *dev = led_cdev->dev->parent;
827 struct hid_device *hdev = to_hid_device(dev);
828 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
831 if (led_cdev == &data_pointer->led_micmute)
834 return data_pointer->led_state & (1 << led_nr)
839 static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
840 enum led_brightness value)
842 struct device *dev = led_cdev->dev->parent;
843 struct hid_device *hdev = to_hid_device(dev);
844 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
845 u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
849 if (led_cdev == &data_pointer->led_micmute)
852 if (value == LED_OFF)
853 data_pointer->led_state &= ~(1 << led_nr);
855 data_pointer->led_state |= 1 << led_nr;
857 switch (hdev->product) {
858 case USB_DEVICE_ID_LENOVO_TPKBD:
859 lenovo_led_set_tpkbd(hdev);
861 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
862 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
869 static int lenovo_register_leds(struct hid_device *hdev)
871 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
872 size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
873 char *name_mute, *name_micm;
876 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
877 name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
878 if (name_mute == NULL || name_micm == NULL) {
879 hid_err(hdev, "Could not allocate memory for led data\n");
882 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
883 snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
885 data->led_mute.name = name_mute;
886 data->led_mute.brightness_get = lenovo_led_brightness_get;
887 data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
888 data->led_mute.flags = LED_HW_PLUGGABLE;
889 data->led_mute.dev = &hdev->dev;
890 ret = led_classdev_register(&hdev->dev, &data->led_mute);
894 data->led_micmute.name = name_micm;
895 data->led_micmute.brightness_get = lenovo_led_brightness_get;
896 data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
897 data->led_micmute.flags = LED_HW_PLUGGABLE;
898 data->led_micmute.dev = &hdev->dev;
899 ret = led_classdev_register(&hdev->dev, &data->led_micmute);
901 led_classdev_unregister(&data->led_mute);
908 static int lenovo_probe_tpkbd(struct hid_device *hdev)
910 struct lenovo_drvdata *data_pointer;
914 * Only register extra settings against subdevice where input_mapping
915 * set drvdata to 1, i.e. the trackpoint.
917 if (!hid_get_drvdata(hdev))
920 hid_set_drvdata(hdev, NULL);
922 /* Validate required reports. */
923 for (i = 0; i < 4; i++) {
924 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
927 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
930 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
932 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
934 data_pointer = devm_kzalloc(&hdev->dev,
935 sizeof(struct lenovo_drvdata),
937 if (data_pointer == NULL) {
938 hid_err(hdev, "Could not allocate memory for driver data\n");
943 // set same default values as windows driver
944 data_pointer->sensitivity = 0xa0;
945 data_pointer->press_speed = 0x38;
947 hid_set_drvdata(hdev, data_pointer);
949 ret = lenovo_register_leds(hdev);
953 lenovo_features_set_tpkbd(hdev);
957 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
961 static int lenovo_probe_cptkbd(struct hid_device *hdev)
964 struct lenovo_drvdata *cptkbd_data;
966 /* All the custom action happens on the USBMOUSE device for USB */
967 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
968 && hdev->type != HID_TYPE_USBMOUSE) {
969 hid_dbg(hdev, "Ignoring keyboard half of device\n");
973 cptkbd_data = devm_kzalloc(&hdev->dev,
974 sizeof(*cptkbd_data),
976 if (cptkbd_data == NULL) {
977 hid_err(hdev, "can't alloc keyboard descriptor\n");
980 hid_set_drvdata(hdev, cptkbd_data);
983 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
986 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
988 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
990 /* Switch middle button to native mode */
991 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
993 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
995 /* Set keyboard settings to known state */
996 cptkbd_data->middlebutton_state = 0;
997 cptkbd_data->fn_lock = true;
998 cptkbd_data->sensitivity = 0x05;
999 cptkbd_data->middleclick_workaround_cptkbd = true;
1000 lenovo_features_set_cptkbd(hdev);
1002 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
1004 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
1009 static struct attribute *lenovo_attributes_tp10ubkbd[] = {
1010 &dev_attr_fn_lock.attr,
1014 static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
1015 .attrs = lenovo_attributes_tp10ubkbd,
1018 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
1020 struct lenovo_drvdata *data;
1023 /* All the custom action happens on the USBMOUSE device for USB */
1024 if (hdev->type != HID_TYPE_USBMOUSE)
1027 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
1031 mutex_init(&data->led_report_mutex);
1032 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
1035 hid_set_drvdata(hdev, data);
1038 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
1039 * We cannot read the state, only set it, so we force it to on here
1040 * (which should be a no-op) to make sure that our state matches the
1041 * keyboard's FN-lock state. This is the same as what Windows does.
1043 data->fn_lock = true;
1044 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
1046 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1050 ret = lenovo_register_leds(hdev);
1056 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1060 static int lenovo_probe(struct hid_device *hdev,
1061 const struct hid_device_id *id)
1065 ret = hid_parse(hdev);
1067 hid_err(hdev, "hid_parse failed\n");
1071 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1073 hid_err(hdev, "hid_hw_start failed\n");
1077 switch (hdev->product) {
1078 case USB_DEVICE_ID_LENOVO_TPKBD:
1079 ret = lenovo_probe_tpkbd(hdev);
1081 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1082 case USB_DEVICE_ID_LENOVO_CBTKBD:
1083 ret = lenovo_probe_cptkbd(hdev);
1085 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1086 ret = lenovo_probe_tp10ubkbd(hdev);
1102 static void lenovo_remove_tpkbd(struct hid_device *hdev)
1104 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1107 * Only the trackpoint half of the keyboard has drvdata and stuff that
1108 * needs unregistering.
1110 if (data_pointer == NULL)
1113 sysfs_remove_group(&hdev->dev.kobj,
1114 &lenovo_attr_group_tpkbd);
1116 led_classdev_unregister(&data_pointer->led_micmute);
1117 led_classdev_unregister(&data_pointer->led_mute);
1120 static void lenovo_remove_cptkbd(struct hid_device *hdev)
1122 sysfs_remove_group(&hdev->dev.kobj,
1123 &lenovo_attr_group_cptkbd);
1126 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1128 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1133 led_classdev_unregister(&data->led_micmute);
1134 led_classdev_unregister(&data->led_mute);
1136 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1137 cancel_work_sync(&data->fn_lock_sync_work);
1140 static void lenovo_remove(struct hid_device *hdev)
1142 switch (hdev->product) {
1143 case USB_DEVICE_ID_LENOVO_TPKBD:
1144 lenovo_remove_tpkbd(hdev);
1146 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1147 case USB_DEVICE_ID_LENOVO_CBTKBD:
1148 lenovo_remove_cptkbd(hdev);
1150 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1151 lenovo_remove_tp10ubkbd(hdev);
1158 static int lenovo_input_configured(struct hid_device *hdev,
1159 struct hid_input *hi)
1161 switch (hdev->product) {
1162 case USB_DEVICE_ID_LENOVO_TPKBD:
1163 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1164 case USB_DEVICE_ID_LENOVO_CBTKBD:
1165 if (test_bit(EV_REL, hi->input->evbit)) {
1166 /* set only for trackpoint device */
1167 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1168 __set_bit(INPUT_PROP_POINTING_STICK,
1169 hi->input->propbit);
1178 static const struct hid_device_id lenovo_devices[] = {
1179 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1180 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1181 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1182 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1183 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1184 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1185 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1186 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1187 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1188 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1189 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1193 MODULE_DEVICE_TABLE(hid, lenovo_devices);
1195 static struct hid_driver lenovo_driver = {
1197 .id_table = lenovo_devices,
1198 .input_configured = lenovo_input_configured,
1199 .input_mapping = lenovo_input_mapping,
1200 .probe = lenovo_probe,
1201 .remove = lenovo_remove,
1202 .raw_event = lenovo_raw_event,
1203 .event = lenovo_event,
1204 .report_fixup = lenovo_report_fixup,
1206 module_hid_driver(lenovo_driver);
1208 MODULE_LICENSE("GPL");