2 * HID driver for Lenovo:
3 * - ThinkPad USB Keyboard with TrackPoint (tpkbd)
4 * - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
5 * - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
7 * Copyright (c) 2012 Bernhard Seibold
8 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
18 #include <linux/module.h>
19 #include <linux/sysfs.h>
20 #include <linux/device.h>
21 #include <linux/hid.h>
22 #include <linux/input.h>
23 #include <linux/leds.h>
27 struct lenovo_drvdata_tpkbd
{
29 struct led_classdev led_mute
;
30 struct led_classdev led_micmute
;
33 int release_to_select
;
39 struct lenovo_drvdata_cptkbd
{
43 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
45 static int lenovo_input_mapping_tpkbd(struct hid_device
*hdev
,
46 struct hid_input
*hi
, struct hid_field
*field
,
47 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
49 if (usage
->hid
== (HID_UP_BUTTON
| 0x0010)) {
50 /* This sub-device contains trackpoint, mark it */
51 hid_set_drvdata(hdev
, (void *)1);
52 map_key_clear(KEY_MICMUTE
);
58 static int lenovo_input_mapping_cptkbd(struct hid_device
*hdev
,
59 struct hid_input
*hi
, struct hid_field
*field
,
60 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
62 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
63 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_MSVENDOR
||
64 (usage
->hid
& HID_USAGE_PAGE
) == HID_UP_LNVENDOR
) {
65 switch (usage
->hid
& HID_USAGE
) {
66 case 0x00f1: /* Fn-F4: Mic mute */
67 map_key_clear(KEY_MICMUTE
);
69 case 0x00f2: /* Fn-F5: Brightness down */
70 map_key_clear(KEY_BRIGHTNESSDOWN
);
72 case 0x00f3: /* Fn-F6: Brightness up */
73 map_key_clear(KEY_BRIGHTNESSUP
);
75 case 0x00f4: /* Fn-F7: External display (projector) */
76 map_key_clear(KEY_SWITCHVIDEOMODE
);
78 case 0x00f5: /* Fn-F8: Wireless */
79 map_key_clear(KEY_WLAN
);
81 case 0x00f6: /* Fn-F9: Control panel */
82 map_key_clear(KEY_CONFIG
);
84 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
85 map_key_clear(KEY_SCALE
);
87 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
88 /* NB: This mapping is invented in raw_event below */
89 map_key_clear(KEY_FILE
);
91 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
92 map_key_clear(KEY_FN_ESC
);
100 static int lenovo_input_mapping(struct hid_device
*hdev
,
101 struct hid_input
*hi
, struct hid_field
*field
,
102 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
104 switch (hdev
->product
) {
105 case USB_DEVICE_ID_LENOVO_TPKBD
:
106 return lenovo_input_mapping_tpkbd(hdev
, hi
, field
,
108 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
109 case USB_DEVICE_ID_LENOVO_CBTKBD
:
110 return lenovo_input_mapping_cptkbd(hdev
, hi
, field
,
119 /* Send a config command to the keyboard */
120 static int lenovo_send_cmd_cptkbd(struct hid_device
*hdev
,
121 unsigned char byte2
, unsigned char byte3
)
124 unsigned char buf
[] = {0x18, byte2
, byte3
};
126 switch (hdev
->product
) {
127 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
128 ret
= hid_hw_raw_request(hdev
, 0x13, buf
, sizeof(buf
),
129 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
131 case USB_DEVICE_ID_LENOVO_CBTKBD
:
132 ret
= hid_hw_output_report(hdev
, buf
, sizeof(buf
));
139 return ret
< 0 ? ret
: 0; /* BT returns 0, USB returns sizeof(buf) */
142 static void lenovo_features_set_cptkbd(struct hid_device
*hdev
)
145 struct lenovo_drvdata_cptkbd
*cptkbd_data
= hid_get_drvdata(hdev
);
147 ret
= lenovo_send_cmd_cptkbd(hdev
, 0x05, cptkbd_data
->fn_lock
);
149 hid_err(hdev
, "Fn-lock setting failed: %d\n", ret
);
152 static ssize_t
attr_fn_lock_show_cptkbd(struct device
*dev
,
153 struct device_attribute
*attr
,
156 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
157 struct lenovo_drvdata_cptkbd
*cptkbd_data
= hid_get_drvdata(hdev
);
159 return snprintf(buf
, PAGE_SIZE
, "%u\n", cptkbd_data
->fn_lock
);
162 static ssize_t
attr_fn_lock_store_cptkbd(struct device
*dev
,
163 struct device_attribute
*attr
,
167 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
168 struct lenovo_drvdata_cptkbd
*cptkbd_data
= hid_get_drvdata(hdev
);
171 if (kstrtoint(buf
, 10, &value
))
173 if (value
< 0 || value
> 1)
176 cptkbd_data
->fn_lock
= !!value
;
177 lenovo_features_set_cptkbd(hdev
);
182 static struct device_attribute dev_attr_fn_lock_cptkbd
=
183 __ATTR(fn_lock
, S_IWUSR
| S_IRUGO
,
184 attr_fn_lock_show_cptkbd
,
185 attr_fn_lock_store_cptkbd
);
187 static struct attribute
*lenovo_attributes_cptkbd
[] = {
188 &dev_attr_fn_lock_cptkbd
.attr
,
192 static const struct attribute_group lenovo_attr_group_cptkbd
= {
193 .attrs
= lenovo_attributes_cptkbd
,
196 static int lenovo_raw_event(struct hid_device
*hdev
,
197 struct hid_report
*report
, u8
*data
, int size
)
200 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
201 * its own key is outside the usage page range. Remove extra
202 * keypresses and remap to inside usage page.
204 if (unlikely(hdev
->product
== USB_DEVICE_ID_LENOVO_CUSBKBD
208 && data
[2] == 0x01)) {
216 static int lenovo_features_set_tpkbd(struct hid_device
*hdev
)
218 struct hid_report
*report
;
219 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
221 report
= hdev
->report_enum
[HID_FEATURE_REPORT
].report_id_hash
[4];
223 report
->field
[0]->value
[0] = data_pointer
->press_to_select
? 0x01 : 0x02;
224 report
->field
[0]->value
[0] |= data_pointer
->dragging
? 0x04 : 0x08;
225 report
->field
[0]->value
[0] |= data_pointer
->release_to_select
? 0x10 : 0x20;
226 report
->field
[0]->value
[0] |= data_pointer
->select_right
? 0x80 : 0x40;
227 report
->field
[1]->value
[0] = 0x03; // unknown setting, imitate windows driver
228 report
->field
[2]->value
[0] = data_pointer
->sensitivity
;
229 report
->field
[3]->value
[0] = data_pointer
->press_speed
;
231 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
235 static ssize_t
attr_press_to_select_show_tpkbd(struct device
*dev
,
236 struct device_attribute
*attr
,
239 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
240 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
242 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->press_to_select
);
245 static ssize_t
attr_press_to_select_store_tpkbd(struct device
*dev
,
246 struct device_attribute
*attr
,
250 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
251 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
254 if (kstrtoint(buf
, 10, &value
))
256 if (value
< 0 || value
> 1)
259 data_pointer
->press_to_select
= value
;
260 lenovo_features_set_tpkbd(hdev
);
265 static ssize_t
attr_dragging_show_tpkbd(struct device
*dev
,
266 struct device_attribute
*attr
,
269 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
270 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
272 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->dragging
);
275 static ssize_t
attr_dragging_store_tpkbd(struct device
*dev
,
276 struct device_attribute
*attr
,
280 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
281 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
284 if (kstrtoint(buf
, 10, &value
))
286 if (value
< 0 || value
> 1)
289 data_pointer
->dragging
= value
;
290 lenovo_features_set_tpkbd(hdev
);
295 static ssize_t
attr_release_to_select_show_tpkbd(struct device
*dev
,
296 struct device_attribute
*attr
,
299 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
300 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
302 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->release_to_select
);
305 static ssize_t
attr_release_to_select_store_tpkbd(struct device
*dev
,
306 struct device_attribute
*attr
,
310 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
311 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
314 if (kstrtoint(buf
, 10, &value
))
316 if (value
< 0 || value
> 1)
319 data_pointer
->release_to_select
= value
;
320 lenovo_features_set_tpkbd(hdev
);
325 static ssize_t
attr_select_right_show_tpkbd(struct device
*dev
,
326 struct device_attribute
*attr
,
329 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
330 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
332 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->select_right
);
335 static ssize_t
attr_select_right_store_tpkbd(struct device
*dev
,
336 struct device_attribute
*attr
,
340 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
341 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
344 if (kstrtoint(buf
, 10, &value
))
346 if (value
< 0 || value
> 1)
349 data_pointer
->select_right
= value
;
350 lenovo_features_set_tpkbd(hdev
);
355 static ssize_t
attr_sensitivity_show_tpkbd(struct device
*dev
,
356 struct device_attribute
*attr
,
359 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
360 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
362 return snprintf(buf
, PAGE_SIZE
, "%u\n",
363 data_pointer
->sensitivity
);
366 static ssize_t
attr_sensitivity_store_tpkbd(struct device
*dev
,
367 struct device_attribute
*attr
,
371 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
372 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
375 if (kstrtoint(buf
, 10, &value
) || value
< 1 || value
> 255)
378 data_pointer
->sensitivity
= value
;
379 lenovo_features_set_tpkbd(hdev
);
384 static ssize_t
attr_press_speed_show_tpkbd(struct device
*dev
,
385 struct device_attribute
*attr
,
388 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
389 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
391 return snprintf(buf
, PAGE_SIZE
, "%u\n",
392 data_pointer
->press_speed
);
395 static ssize_t
attr_press_speed_store_tpkbd(struct device
*dev
,
396 struct device_attribute
*attr
,
400 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
401 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
404 if (kstrtoint(buf
, 10, &value
) || value
< 1 || value
> 255)
407 data_pointer
->press_speed
= value
;
408 lenovo_features_set_tpkbd(hdev
);
413 static struct device_attribute dev_attr_press_to_select_tpkbd
=
414 __ATTR(press_to_select
, S_IWUSR
| S_IRUGO
,
415 attr_press_to_select_show_tpkbd
,
416 attr_press_to_select_store_tpkbd
);
418 static struct device_attribute dev_attr_dragging_tpkbd
=
419 __ATTR(dragging
, S_IWUSR
| S_IRUGO
,
420 attr_dragging_show_tpkbd
,
421 attr_dragging_store_tpkbd
);
423 static struct device_attribute dev_attr_release_to_select_tpkbd
=
424 __ATTR(release_to_select
, S_IWUSR
| S_IRUGO
,
425 attr_release_to_select_show_tpkbd
,
426 attr_release_to_select_store_tpkbd
);
428 static struct device_attribute dev_attr_select_right_tpkbd
=
429 __ATTR(select_right
, S_IWUSR
| S_IRUGO
,
430 attr_select_right_show_tpkbd
,
431 attr_select_right_store_tpkbd
);
433 static struct device_attribute dev_attr_sensitivity_tpkbd
=
434 __ATTR(sensitivity
, S_IWUSR
| S_IRUGO
,
435 attr_sensitivity_show_tpkbd
,
436 attr_sensitivity_store_tpkbd
);
438 static struct device_attribute dev_attr_press_speed_tpkbd
=
439 __ATTR(press_speed
, S_IWUSR
| S_IRUGO
,
440 attr_press_speed_show_tpkbd
,
441 attr_press_speed_store_tpkbd
);
443 static struct attribute
*lenovo_attributes_tpkbd
[] = {
444 &dev_attr_press_to_select_tpkbd
.attr
,
445 &dev_attr_dragging_tpkbd
.attr
,
446 &dev_attr_release_to_select_tpkbd
.attr
,
447 &dev_attr_select_right_tpkbd
.attr
,
448 &dev_attr_sensitivity_tpkbd
.attr
,
449 &dev_attr_press_speed_tpkbd
.attr
,
453 static const struct attribute_group lenovo_attr_group_tpkbd
= {
454 .attrs
= lenovo_attributes_tpkbd
,
457 static enum led_brightness
lenovo_led_brightness_get_tpkbd(
458 struct led_classdev
*led_cdev
)
460 struct device
*dev
= led_cdev
->dev
->parent
;
461 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
462 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
465 if (led_cdev
== &data_pointer
->led_micmute
)
468 return data_pointer
->led_state
& (1 << led_nr
)
473 static void lenovo_led_brightness_set_tpkbd(struct led_classdev
*led_cdev
,
474 enum led_brightness value
)
476 struct device
*dev
= led_cdev
->dev
->parent
;
477 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
478 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
479 struct hid_report
*report
;
482 if (led_cdev
== &data_pointer
->led_micmute
)
485 if (value
== LED_OFF
)
486 data_pointer
->led_state
&= ~(1 << led_nr
);
488 data_pointer
->led_state
|= 1 << led_nr
;
490 report
= hdev
->report_enum
[HID_OUTPUT_REPORT
].report_id_hash
[3];
491 report
->field
[0]->value
[0] = (data_pointer
->led_state
>> 0) & 1;
492 report
->field
[0]->value
[1] = (data_pointer
->led_state
>> 1) & 1;
493 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
496 static int lenovo_probe_tpkbd(struct hid_device
*hdev
)
498 struct device
*dev
= &hdev
->dev
;
499 struct lenovo_drvdata_tpkbd
*data_pointer
;
500 size_t name_sz
= strlen(dev_name(dev
)) + 16;
501 char *name_mute
, *name_micmute
;
506 * Only register extra settings against subdevice where input_mapping
507 * set drvdata to 1, i.e. the trackpoint.
509 if (!hid_get_drvdata(hdev
))
512 hid_set_drvdata(hdev
, NULL
);
514 /* Validate required reports. */
515 for (i
= 0; i
< 4; i
++) {
516 if (!hid_validate_values(hdev
, HID_FEATURE_REPORT
, 4, i
, 1))
519 if (!hid_validate_values(hdev
, HID_OUTPUT_REPORT
, 3, 0, 2))
522 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &lenovo_attr_group_tpkbd
);
524 hid_warn(hdev
, "Could not create sysfs group: %d\n", ret
);
526 data_pointer
= devm_kzalloc(&hdev
->dev
,
527 sizeof(struct lenovo_drvdata_tpkbd
),
529 if (data_pointer
== NULL
) {
530 hid_err(hdev
, "Could not allocate memory for driver data\n");
534 // set same default values as windows driver
535 data_pointer
->sensitivity
= 0xa0;
536 data_pointer
->press_speed
= 0x38;
538 name_mute
= devm_kzalloc(&hdev
->dev
, name_sz
, GFP_KERNEL
);
539 name_micmute
= devm_kzalloc(&hdev
->dev
, name_sz
, GFP_KERNEL
);
540 if (name_mute
== NULL
|| name_micmute
== NULL
) {
541 hid_err(hdev
, "Could not allocate memory for led data\n");
544 snprintf(name_mute
, name_sz
, "%s:amber:mute", dev_name(dev
));
545 snprintf(name_micmute
, name_sz
, "%s:amber:micmute", dev_name(dev
));
547 hid_set_drvdata(hdev
, data_pointer
);
549 data_pointer
->led_mute
.name
= name_mute
;
550 data_pointer
->led_mute
.brightness_get
= lenovo_led_brightness_get_tpkbd
;
551 data_pointer
->led_mute
.brightness_set
= lenovo_led_brightness_set_tpkbd
;
552 data_pointer
->led_mute
.dev
= dev
;
553 led_classdev_register(dev
, &data_pointer
->led_mute
);
555 data_pointer
->led_micmute
.name
= name_micmute
;
556 data_pointer
->led_micmute
.brightness_get
=
557 lenovo_led_brightness_get_tpkbd
;
558 data_pointer
->led_micmute
.brightness_set
=
559 lenovo_led_brightness_set_tpkbd
;
560 data_pointer
->led_micmute
.dev
= dev
;
561 led_classdev_register(dev
, &data_pointer
->led_micmute
);
563 lenovo_features_set_tpkbd(hdev
);
568 static int lenovo_probe_cptkbd(struct hid_device
*hdev
)
571 struct lenovo_drvdata_cptkbd
*cptkbd_data
;
573 /* All the custom action happens on the USBMOUSE device for USB */
574 if (hdev
->product
== USB_DEVICE_ID_LENOVO_CUSBKBD
575 && hdev
->type
!= HID_TYPE_USBMOUSE
) {
576 hid_dbg(hdev
, "Ignoring keyboard half of device\n");
580 cptkbd_data
= devm_kzalloc(&hdev
->dev
,
581 sizeof(*cptkbd_data
),
583 if (cptkbd_data
== NULL
) {
584 hid_err(hdev
, "can't alloc keyboard descriptor\n");
587 hid_set_drvdata(hdev
, cptkbd_data
);
590 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
593 ret
= lenovo_send_cmd_cptkbd(hdev
, 0x01, 0x03);
595 hid_warn(hdev
, "Failed to switch F7/9/11 mode: %d\n", ret
);
597 /* Turn Fn-Lock on by default */
598 cptkbd_data
->fn_lock
= true;
599 lenovo_features_set_cptkbd(hdev
);
601 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &lenovo_attr_group_cptkbd
);
603 hid_warn(hdev
, "Could not create sysfs group: %d\n", ret
);
608 static int lenovo_probe(struct hid_device
*hdev
,
609 const struct hid_device_id
*id
)
613 ret
= hid_parse(hdev
);
615 hid_err(hdev
, "hid_parse failed\n");
619 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
621 hid_err(hdev
, "hid_hw_start failed\n");
625 switch (hdev
->product
) {
626 case USB_DEVICE_ID_LENOVO_TPKBD
:
627 ret
= lenovo_probe_tpkbd(hdev
);
629 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
630 case USB_DEVICE_ID_LENOVO_CBTKBD
:
631 ret
= lenovo_probe_cptkbd(hdev
);
647 static void lenovo_remove_tpkbd(struct hid_device
*hdev
)
649 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
652 * Only the trackpoint half of the keyboard has drvdata and stuff that
653 * needs unregistering.
655 if (data_pointer
== NULL
)
658 sysfs_remove_group(&hdev
->dev
.kobj
,
659 &lenovo_attr_group_tpkbd
);
661 led_classdev_unregister(&data_pointer
->led_micmute
);
662 led_classdev_unregister(&data_pointer
->led_mute
);
664 hid_set_drvdata(hdev
, NULL
);
667 static void lenovo_remove_cptkbd(struct hid_device
*hdev
)
669 sysfs_remove_group(&hdev
->dev
.kobj
,
670 &lenovo_attr_group_cptkbd
);
673 static void lenovo_remove(struct hid_device
*hdev
)
675 switch (hdev
->product
) {
676 case USB_DEVICE_ID_LENOVO_TPKBD
:
677 lenovo_remove_tpkbd(hdev
);
679 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
680 case USB_DEVICE_ID_LENOVO_CBTKBD
:
681 lenovo_remove_cptkbd(hdev
);
688 static const struct hid_device_id lenovo_devices
[] = {
689 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO
, USB_DEVICE_ID_LENOVO_TPKBD
) },
690 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO
, USB_DEVICE_ID_LENOVO_CUSBKBD
) },
691 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO
, USB_DEVICE_ID_LENOVO_CBTKBD
) },
695 MODULE_DEVICE_TABLE(hid
, lenovo_devices
);
697 static struct hid_driver lenovo_driver
= {
699 .id_table
= lenovo_devices
,
700 .input_mapping
= lenovo_input_mapping
,
701 .probe
= lenovo_probe
,
702 .remove
= lenovo_remove
,
703 .raw_event
= lenovo_raw_event
,
705 module_hid_driver(lenovo_driver
);
707 MODULE_LICENSE("GPL");