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 set_bit(EV_REP
, hi
->input
->evbit
);
66 switch (usage
->hid
& HID_USAGE
) {
67 case 0x00f1: /* Fn-F4: Mic mute */
68 map_key_clear(KEY_MICMUTE
);
70 case 0x00f2: /* Fn-F5: Brightness down */
71 map_key_clear(KEY_BRIGHTNESSDOWN
);
73 case 0x00f3: /* Fn-F6: Brightness up */
74 map_key_clear(KEY_BRIGHTNESSUP
);
76 case 0x00f4: /* Fn-F7: External display (projector) */
77 map_key_clear(KEY_SWITCHVIDEOMODE
);
79 case 0x00f5: /* Fn-F8: Wireless */
80 map_key_clear(KEY_WLAN
);
82 case 0x00f6: /* Fn-F9: Control panel */
83 map_key_clear(KEY_CONFIG
);
85 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
86 map_key_clear(KEY_SCALE
);
88 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
89 map_key_clear(KEY_FN_ESC
);
91 case 0x00fb: /* Fn-F12: Open My computer (6 boxes) USB-only */
92 /* NB: This mapping is invented in raw_event below */
93 map_key_clear(KEY_FILE
);
101 static int lenovo_input_mapping(struct hid_device
*hdev
,
102 struct hid_input
*hi
, struct hid_field
*field
,
103 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
105 switch (hdev
->product
) {
106 case USB_DEVICE_ID_LENOVO_TPKBD
:
107 return lenovo_input_mapping_tpkbd(hdev
, hi
, field
,
109 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
110 case USB_DEVICE_ID_LENOVO_CBTKBD
:
111 return lenovo_input_mapping_cptkbd(hdev
, hi
, field
,
120 /* Send a config command to the keyboard */
121 static int lenovo_send_cmd_cptkbd(struct hid_device
*hdev
,
122 unsigned char byte2
, unsigned char byte3
)
125 unsigned char buf
[] = {0x18, byte2
, byte3
};
127 switch (hdev
->product
) {
128 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
129 ret
= hid_hw_raw_request(hdev
, 0x13, buf
, sizeof(buf
),
130 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
132 case USB_DEVICE_ID_LENOVO_CBTKBD
:
133 ret
= hid_hw_output_report(hdev
, buf
, sizeof(buf
));
140 return ret
< 0 ? ret
: 0; /* BT returns 0, USB returns sizeof(buf) */
143 static void lenovo_features_set_cptkbd(struct hid_device
*hdev
)
146 struct lenovo_drvdata_cptkbd
*cptkbd_data
= hid_get_drvdata(hdev
);
148 ret
= lenovo_send_cmd_cptkbd(hdev
, 0x05, cptkbd_data
->fn_lock
);
150 hid_err(hdev
, "Fn-lock setting failed: %d\n", ret
);
153 static ssize_t
attr_fn_lock_show_cptkbd(struct device
*dev
,
154 struct device_attribute
*attr
,
157 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
158 struct lenovo_drvdata_cptkbd
*cptkbd_data
= hid_get_drvdata(hdev
);
160 return snprintf(buf
, PAGE_SIZE
, "%u\n", cptkbd_data
->fn_lock
);
163 static ssize_t
attr_fn_lock_store_cptkbd(struct device
*dev
,
164 struct device_attribute
*attr
,
168 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
169 struct lenovo_drvdata_cptkbd
*cptkbd_data
= hid_get_drvdata(hdev
);
172 if (kstrtoint(buf
, 10, &value
))
174 if (value
< 0 || value
> 1)
177 cptkbd_data
->fn_lock
= !!value
;
178 lenovo_features_set_cptkbd(hdev
);
183 static struct device_attribute dev_attr_fn_lock_cptkbd
=
184 __ATTR(fn_lock
, S_IWUSR
| S_IRUGO
,
185 attr_fn_lock_show_cptkbd
,
186 attr_fn_lock_store_cptkbd
);
188 static struct attribute
*lenovo_attributes_cptkbd
[] = {
189 &dev_attr_fn_lock_cptkbd
.attr
,
193 static const struct attribute_group lenovo_attr_group_cptkbd
= {
194 .attrs
= lenovo_attributes_cptkbd
,
197 static int lenovo_raw_event(struct hid_device
*hdev
,
198 struct hid_report
*report
, u8
*data
, int size
)
201 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
202 * its own key is outside the usage page range. Remove extra
203 * keypresses and remap to inside usage page.
205 if (unlikely(hdev
->product
== USB_DEVICE_ID_LENOVO_CUSBKBD
209 && data
[2] == 0x01)) {
217 static int lenovo_features_set_tpkbd(struct hid_device
*hdev
)
219 struct hid_report
*report
;
220 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
222 report
= hdev
->report_enum
[HID_FEATURE_REPORT
].report_id_hash
[4];
224 report
->field
[0]->value
[0] = data_pointer
->press_to_select
? 0x01 : 0x02;
225 report
->field
[0]->value
[0] |= data_pointer
->dragging
? 0x04 : 0x08;
226 report
->field
[0]->value
[0] |= data_pointer
->release_to_select
? 0x10 : 0x20;
227 report
->field
[0]->value
[0] |= data_pointer
->select_right
? 0x80 : 0x40;
228 report
->field
[1]->value
[0] = 0x03; // unknown setting, imitate windows driver
229 report
->field
[2]->value
[0] = data_pointer
->sensitivity
;
230 report
->field
[3]->value
[0] = data_pointer
->press_speed
;
232 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
236 static ssize_t
attr_press_to_select_show_tpkbd(struct device
*dev
,
237 struct device_attribute
*attr
,
240 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
241 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
243 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->press_to_select
);
246 static ssize_t
attr_press_to_select_store_tpkbd(struct device
*dev
,
247 struct device_attribute
*attr
,
251 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
252 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
255 if (kstrtoint(buf
, 10, &value
))
257 if (value
< 0 || value
> 1)
260 data_pointer
->press_to_select
= value
;
261 lenovo_features_set_tpkbd(hdev
);
266 static ssize_t
attr_dragging_show_tpkbd(struct device
*dev
,
267 struct device_attribute
*attr
,
270 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
271 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
273 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->dragging
);
276 static ssize_t
attr_dragging_store_tpkbd(struct device
*dev
,
277 struct device_attribute
*attr
,
281 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
282 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
285 if (kstrtoint(buf
, 10, &value
))
287 if (value
< 0 || value
> 1)
290 data_pointer
->dragging
= value
;
291 lenovo_features_set_tpkbd(hdev
);
296 static ssize_t
attr_release_to_select_show_tpkbd(struct device
*dev
,
297 struct device_attribute
*attr
,
300 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
301 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
303 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->release_to_select
);
306 static ssize_t
attr_release_to_select_store_tpkbd(struct device
*dev
,
307 struct device_attribute
*attr
,
311 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
312 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
315 if (kstrtoint(buf
, 10, &value
))
317 if (value
< 0 || value
> 1)
320 data_pointer
->release_to_select
= value
;
321 lenovo_features_set_tpkbd(hdev
);
326 static ssize_t
attr_select_right_show_tpkbd(struct device
*dev
,
327 struct device_attribute
*attr
,
330 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
331 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
333 return snprintf(buf
, PAGE_SIZE
, "%u\n", data_pointer
->select_right
);
336 static ssize_t
attr_select_right_store_tpkbd(struct device
*dev
,
337 struct device_attribute
*attr
,
341 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
342 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
345 if (kstrtoint(buf
, 10, &value
))
347 if (value
< 0 || value
> 1)
350 data_pointer
->select_right
= value
;
351 lenovo_features_set_tpkbd(hdev
);
356 static ssize_t
attr_sensitivity_show_tpkbd(struct device
*dev
,
357 struct device_attribute
*attr
,
360 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
361 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
363 return snprintf(buf
, PAGE_SIZE
, "%u\n",
364 data_pointer
->sensitivity
);
367 static ssize_t
attr_sensitivity_store_tpkbd(struct device
*dev
,
368 struct device_attribute
*attr
,
372 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
373 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
376 if (kstrtoint(buf
, 10, &value
) || value
< 1 || value
> 255)
379 data_pointer
->sensitivity
= value
;
380 lenovo_features_set_tpkbd(hdev
);
385 static ssize_t
attr_press_speed_show_tpkbd(struct device
*dev
,
386 struct device_attribute
*attr
,
389 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
390 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
392 return snprintf(buf
, PAGE_SIZE
, "%u\n",
393 data_pointer
->press_speed
);
396 static ssize_t
attr_press_speed_store_tpkbd(struct device
*dev
,
397 struct device_attribute
*attr
,
401 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
402 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
405 if (kstrtoint(buf
, 10, &value
) || value
< 1 || value
> 255)
408 data_pointer
->press_speed
= value
;
409 lenovo_features_set_tpkbd(hdev
);
414 static struct device_attribute dev_attr_press_to_select_tpkbd
=
415 __ATTR(press_to_select
, S_IWUSR
| S_IRUGO
,
416 attr_press_to_select_show_tpkbd
,
417 attr_press_to_select_store_tpkbd
);
419 static struct device_attribute dev_attr_dragging_tpkbd
=
420 __ATTR(dragging
, S_IWUSR
| S_IRUGO
,
421 attr_dragging_show_tpkbd
,
422 attr_dragging_store_tpkbd
);
424 static struct device_attribute dev_attr_release_to_select_tpkbd
=
425 __ATTR(release_to_select
, S_IWUSR
| S_IRUGO
,
426 attr_release_to_select_show_tpkbd
,
427 attr_release_to_select_store_tpkbd
);
429 static struct device_attribute dev_attr_select_right_tpkbd
=
430 __ATTR(select_right
, S_IWUSR
| S_IRUGO
,
431 attr_select_right_show_tpkbd
,
432 attr_select_right_store_tpkbd
);
434 static struct device_attribute dev_attr_sensitivity_tpkbd
=
435 __ATTR(sensitivity
, S_IWUSR
| S_IRUGO
,
436 attr_sensitivity_show_tpkbd
,
437 attr_sensitivity_store_tpkbd
);
439 static struct device_attribute dev_attr_press_speed_tpkbd
=
440 __ATTR(press_speed
, S_IWUSR
| S_IRUGO
,
441 attr_press_speed_show_tpkbd
,
442 attr_press_speed_store_tpkbd
);
444 static struct attribute
*lenovo_attributes_tpkbd
[] = {
445 &dev_attr_press_to_select_tpkbd
.attr
,
446 &dev_attr_dragging_tpkbd
.attr
,
447 &dev_attr_release_to_select_tpkbd
.attr
,
448 &dev_attr_select_right_tpkbd
.attr
,
449 &dev_attr_sensitivity_tpkbd
.attr
,
450 &dev_attr_press_speed_tpkbd
.attr
,
454 static const struct attribute_group lenovo_attr_group_tpkbd
= {
455 .attrs
= lenovo_attributes_tpkbd
,
458 static enum led_brightness
lenovo_led_brightness_get_tpkbd(
459 struct led_classdev
*led_cdev
)
461 struct device
*dev
= led_cdev
->dev
->parent
;
462 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
463 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
466 if (led_cdev
== &data_pointer
->led_micmute
)
469 return data_pointer
->led_state
& (1 << led_nr
)
474 static void lenovo_led_brightness_set_tpkbd(struct led_classdev
*led_cdev
,
475 enum led_brightness value
)
477 struct device
*dev
= led_cdev
->dev
->parent
;
478 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
479 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
480 struct hid_report
*report
;
483 if (led_cdev
== &data_pointer
->led_micmute
)
486 if (value
== LED_OFF
)
487 data_pointer
->led_state
&= ~(1 << led_nr
);
489 data_pointer
->led_state
|= 1 << led_nr
;
491 report
= hdev
->report_enum
[HID_OUTPUT_REPORT
].report_id_hash
[3];
492 report
->field
[0]->value
[0] = (data_pointer
->led_state
>> 0) & 1;
493 report
->field
[0]->value
[1] = (data_pointer
->led_state
>> 1) & 1;
494 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
497 static int lenovo_probe_tpkbd(struct hid_device
*hdev
)
499 struct device
*dev
= &hdev
->dev
;
500 struct lenovo_drvdata_tpkbd
*data_pointer
;
501 size_t name_sz
= strlen(dev_name(dev
)) + 16;
502 char *name_mute
, *name_micmute
;
507 * Only register extra settings against subdevice where input_mapping
508 * set drvdata to 1, i.e. the trackpoint.
510 if (!hid_get_drvdata(hdev
))
513 hid_set_drvdata(hdev
, NULL
);
515 /* Validate required reports. */
516 for (i
= 0; i
< 4; i
++) {
517 if (!hid_validate_values(hdev
, HID_FEATURE_REPORT
, 4, i
, 1))
520 if (!hid_validate_values(hdev
, HID_OUTPUT_REPORT
, 3, 0, 2))
523 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &lenovo_attr_group_tpkbd
);
525 hid_warn(hdev
, "Could not create sysfs group: %d\n", ret
);
527 data_pointer
= devm_kzalloc(&hdev
->dev
,
528 sizeof(struct lenovo_drvdata_tpkbd
),
530 if (data_pointer
== NULL
) {
531 hid_err(hdev
, "Could not allocate memory for driver data\n");
535 // set same default values as windows driver
536 data_pointer
->sensitivity
= 0xa0;
537 data_pointer
->press_speed
= 0x38;
539 name_mute
= devm_kzalloc(&hdev
->dev
, name_sz
, GFP_KERNEL
);
540 name_micmute
= devm_kzalloc(&hdev
->dev
, name_sz
, GFP_KERNEL
);
541 if (name_mute
== NULL
|| name_micmute
== NULL
) {
542 hid_err(hdev
, "Could not allocate memory for led data\n");
545 snprintf(name_mute
, name_sz
, "%s:amber:mute", dev_name(dev
));
546 snprintf(name_micmute
, name_sz
, "%s:amber:micmute", dev_name(dev
));
548 hid_set_drvdata(hdev
, data_pointer
);
550 data_pointer
->led_mute
.name
= name_mute
;
551 data_pointer
->led_mute
.brightness_get
= lenovo_led_brightness_get_tpkbd
;
552 data_pointer
->led_mute
.brightness_set
= lenovo_led_brightness_set_tpkbd
;
553 data_pointer
->led_mute
.dev
= dev
;
554 led_classdev_register(dev
, &data_pointer
->led_mute
);
556 data_pointer
->led_micmute
.name
= name_micmute
;
557 data_pointer
->led_micmute
.brightness_get
=
558 lenovo_led_brightness_get_tpkbd
;
559 data_pointer
->led_micmute
.brightness_set
=
560 lenovo_led_brightness_set_tpkbd
;
561 data_pointer
->led_micmute
.dev
= dev
;
562 led_classdev_register(dev
, &data_pointer
->led_micmute
);
564 lenovo_features_set_tpkbd(hdev
);
569 static int lenovo_probe_cptkbd(struct hid_device
*hdev
)
572 struct lenovo_drvdata_cptkbd
*cptkbd_data
;
574 /* All the custom action happens on the USBMOUSE device for USB */
575 if (hdev
->product
== USB_DEVICE_ID_LENOVO_CUSBKBD
576 && hdev
->type
!= HID_TYPE_USBMOUSE
) {
577 hid_dbg(hdev
, "Ignoring keyboard half of device\n");
581 cptkbd_data
= devm_kzalloc(&hdev
->dev
,
582 sizeof(*cptkbd_data
),
584 if (cptkbd_data
== NULL
) {
585 hid_err(hdev
, "can't alloc keyboard descriptor\n");
588 hid_set_drvdata(hdev
, cptkbd_data
);
591 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
594 ret
= lenovo_send_cmd_cptkbd(hdev
, 0x01, 0x03);
596 hid_warn(hdev
, "Failed to switch F7/9/11 mode: %d\n", ret
);
598 /* Turn Fn-Lock on by default */
599 cptkbd_data
->fn_lock
= true;
600 lenovo_features_set_cptkbd(hdev
);
602 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &lenovo_attr_group_cptkbd
);
604 hid_warn(hdev
, "Could not create sysfs group: %d\n", ret
);
609 static int lenovo_probe(struct hid_device
*hdev
,
610 const struct hid_device_id
*id
)
614 ret
= hid_parse(hdev
);
616 hid_err(hdev
, "hid_parse failed\n");
620 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
622 hid_err(hdev
, "hid_hw_start failed\n");
626 switch (hdev
->product
) {
627 case USB_DEVICE_ID_LENOVO_TPKBD
:
628 ret
= lenovo_probe_tpkbd(hdev
);
630 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
631 case USB_DEVICE_ID_LENOVO_CBTKBD
:
632 ret
= lenovo_probe_cptkbd(hdev
);
648 static void lenovo_remove_tpkbd(struct hid_device
*hdev
)
650 struct lenovo_drvdata_tpkbd
*data_pointer
= hid_get_drvdata(hdev
);
653 * Only the trackpoint half of the keyboard has drvdata and stuff that
654 * needs unregistering.
656 if (data_pointer
== NULL
)
659 sysfs_remove_group(&hdev
->dev
.kobj
,
660 &lenovo_attr_group_tpkbd
);
662 led_classdev_unregister(&data_pointer
->led_micmute
);
663 led_classdev_unregister(&data_pointer
->led_mute
);
665 hid_set_drvdata(hdev
, NULL
);
668 static void lenovo_remove_cptkbd(struct hid_device
*hdev
)
670 sysfs_remove_group(&hdev
->dev
.kobj
,
671 &lenovo_attr_group_cptkbd
);
674 static void lenovo_remove(struct hid_device
*hdev
)
676 switch (hdev
->product
) {
677 case USB_DEVICE_ID_LENOVO_TPKBD
:
678 lenovo_remove_tpkbd(hdev
);
680 case USB_DEVICE_ID_LENOVO_CUSBKBD
:
681 case USB_DEVICE_ID_LENOVO_CBTKBD
:
682 lenovo_remove_cptkbd(hdev
);
689 static const struct hid_device_id lenovo_devices
[] = {
690 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO
, USB_DEVICE_ID_LENOVO_TPKBD
) },
691 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO
, USB_DEVICE_ID_LENOVO_CUSBKBD
) },
692 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO
, USB_DEVICE_ID_LENOVO_CBTKBD
) },
696 MODULE_DEVICE_TABLE(hid
, lenovo_devices
);
698 static struct hid_driver lenovo_driver
= {
700 .id_table
= lenovo_devices
,
701 .input_mapping
= lenovo_input_mapping
,
702 .probe
= lenovo_probe
,
703 .remove
= lenovo_remove
,
704 .raw_event
= lenovo_raw_event
,
706 module_hid_driver(lenovo_driver
);
708 MODULE_LICENSE("GPL");