Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * HID driver for Lenovo:
4 * - ThinkPad USB Keyboard with TrackPoint (tpkbd)
5 * - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
6 * - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
7 *
8 * Copyright (c) 2012 Bernhard Seibold
9 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
10 *
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
18 *
19 * Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
20 * Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
21 */
22
23/*
24 */
25
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>
33
34#include "hid-ids.h"
35
36struct lenovo_drvdata {
37 u8 led_report[3]; /* Must be first for proper alignment */
38 int led_state;
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;
44 int press_to_select;
45 int dragging;
46 int release_to_select;
47 int select_right;
48 int sensitivity;
49 int press_speed;
50 u8 middlebutton_state; /* 0:Up, 1:Down (undecided), 2:Scrolling */
51 bool fn_lock;
52};
53
54#define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
55
56#define TP10UBKBD_LED_OUTPUT_REPORT 9
57
58#define TP10UBKBD_FN_LOCK_LED 0x54
59#define TP10UBKBD_MUTE_LED 0x64
60#define TP10UBKBD_MICMUTE_LED 0x74
61
62#define TP10UBKBD_LED_OFF 1
63#define TP10UBKBD_LED_ON 2
64
65static void lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
66 enum led_brightness value)
67{
68 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
69 int ret;
70
71 mutex_lock(&data->led_report_mutex);
72
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);
78 if (ret)
79 hid_err(hdev, "Set LED output report error: %d\n", ret);
80
81 mutex_unlock(&data->led_report_mutex);
82}
83
84static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
85{
86 struct lenovo_drvdata *data =
87 container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
88
89 lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
90 data->fn_lock);
91}
92
93static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
94 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */
95 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
96 0xa1, 0x01, /* Collection (Application) */
97 0x85, 0x04, /* Report ID (4) */
98 0x19, 0x00, /* Usage Minimum (0) */
99 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */
100};
101
102static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
103 unsigned int *rsize)
104{
105 switch (hdev->product) {
106 case USB_DEVICE_ID_LENOVO_TPPRODOCK:
107 /* the fixups that need to be done:
108 * - get a reasonable usage max for the vendor collection
109 * 0x8801 from the report ID 4
110 */
111 if (*rsize >= 153 &&
112 memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
113 sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
114 rdesc[151] = 0x01;
115 rdesc[152] = 0x00;
116 }
117 break;
118 }
119 return rdesc;
120}
121
122static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
123 struct hid_input *hi, struct hid_field *field,
124 struct hid_usage *usage, unsigned long **bit, int *max)
125{
126 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
127 /* This sub-device contains trackpoint, mark it */
128 hid_set_drvdata(hdev, (void *)1);
129 map_key_clear(KEY_MICMUTE);
130 return 1;
131 }
132 return 0;
133}
134
135static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
136 struct hid_input *hi, struct hid_field *field,
137 struct hid_usage *usage, unsigned long **bit, int *max)
138{
139 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
140 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
141 (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
142 switch (usage->hid & HID_USAGE) {
143 case 0x00f1: /* Fn-F4: Mic mute */
144 map_key_clear(KEY_MICMUTE);
145 return 1;
146 case 0x00f2: /* Fn-F5: Brightness down */
147 map_key_clear(KEY_BRIGHTNESSDOWN);
148 return 1;
149 case 0x00f3: /* Fn-F6: Brightness up */
150 map_key_clear(KEY_BRIGHTNESSUP);
151 return 1;
152 case 0x00f4: /* Fn-F7: External display (projector) */
153 map_key_clear(KEY_SWITCHVIDEOMODE);
154 return 1;
155 case 0x00f5: /* Fn-F8: Wireless */
156 map_key_clear(KEY_WLAN);
157 return 1;
158 case 0x00f6: /* Fn-F9: Control panel */
159 map_key_clear(KEY_CONFIG);
160 return 1;
161 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
162 map_key_clear(KEY_SCALE);
163 return 1;
164 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
165 /* NB: This mapping is invented in raw_event below */
166 map_key_clear(KEY_FILE);
167 return 1;
168 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
169 map_key_clear(KEY_FN_ESC);
170 return 1;
171 case 0x00fb: /* Middle mouse button (in native mode) */
172 map_key_clear(BTN_MIDDLE);
173 return 1;
174 }
175 }
176
177 /* Compatibility middle/wheel mappings should be ignored */
178 if (usage->hid == HID_GD_WHEEL)
179 return -1;
180 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
181 (usage->hid & HID_USAGE) == 0x003)
182 return -1;
183 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
184 (usage->hid & HID_USAGE) == 0x238)
185 return -1;
186
187 /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
188 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
189 (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
190 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
191 field->logical_minimum = -127;
192 field->logical_maximum = 127;
193
194 switch (usage->hid & HID_USAGE) {
195 case 0x0000:
196 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
197 return 1;
198 case 0x0001:
199 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
200 return 1;
201 default:
202 return -1;
203 }
204 }
205
206 return 0;
207}
208
209static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
210 struct hid_input *hi, struct hid_field *field,
211 struct hid_usage *usage, unsigned long **bit, int *max)
212{
213 if (usage->hid == HID_GD_Z) {
214 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
215 return 1;
216 }
217 return 0;
218}
219
220static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
221 struct hid_input *hi, struct hid_field *field,
222 struct hid_usage *usage, unsigned long **bit, int *max)
223{
224 /*
225 * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
226 * a bunch of keys which have no standard consumer page code.
227 */
228 if (usage->hid == 0x000c0001) {
229 switch (usage->usage_index) {
230 case 8: /* Fn-Esc: Fn-lock toggle */
231 map_key_clear(KEY_FN_ESC);
232 return 1;
233 case 9: /* Fn-F4: Mic mute */
234 map_key_clear(KEY_MICMUTE);
235 return 1;
236 case 10: /* Fn-F7: Control panel */
237 map_key_clear(KEY_CONFIG);
238 return 1;
239 case 11: /* Fn-F8: Search (magnifier glass) */
240 map_key_clear(KEY_SEARCH);
241 return 1;
242 case 12: /* Fn-F10: Open My computer (6 boxes) */
243 map_key_clear(KEY_FILE);
244 return 1;
245 }
246 }
247
248 /*
249 * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
250 * from suspend and it does not actually have a F23 key, ignore it.
251 */
252 if (usage->hid == 0x00070072)
253 return -1;
254
255 return 0;
256}
257
258static int lenovo_input_mapping(struct hid_device *hdev,
259 struct hid_input *hi, struct hid_field *field,
260 struct hid_usage *usage, unsigned long **bit, int *max)
261{
262 switch (hdev->product) {
263 case USB_DEVICE_ID_LENOVO_TPKBD:
264 return lenovo_input_mapping_tpkbd(hdev, hi, field,
265 usage, bit, max);
266 case USB_DEVICE_ID_LENOVO_CUSBKBD:
267 case USB_DEVICE_ID_LENOVO_CBTKBD:
268 return lenovo_input_mapping_cptkbd(hdev, hi, field,
269 usage, bit, max);
270 case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
271 case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
272 case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
273 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
274 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
275 case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
276 return lenovo_input_mapping_scrollpoint(hdev, hi, field,
277 usage, bit, max);
278 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
279 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
280 usage, bit, max);
281 default:
282 return 0;
283 }
284}
285
286#undef map_key_clear
287
288/* Send a config command to the keyboard */
289static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
290 unsigned char byte2, unsigned char byte3)
291{
292 int ret;
293 unsigned char *buf;
294
295 buf = kzalloc(3, GFP_KERNEL);
296 if (!buf)
297 return -ENOMEM;
298
299 buf[0] = 0x18;
300 buf[1] = byte2;
301 buf[2] = byte3;
302
303 switch (hdev->product) {
304 case USB_DEVICE_ID_LENOVO_CUSBKBD:
305 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
306 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
307 break;
308 case USB_DEVICE_ID_LENOVO_CBTKBD:
309 ret = hid_hw_output_report(hdev, buf, 3);
310 break;
311 default:
312 ret = -EINVAL;
313 break;
314 }
315
316 kfree(buf);
317
318 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
319}
320
321static void lenovo_features_set_cptkbd(struct hid_device *hdev)
322{
323 int ret;
324 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
325
326 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
327 if (ret)
328 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
329
330 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
331 if (ret)
332 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
333}
334
335static ssize_t attr_fn_lock_show(struct device *dev,
336 struct device_attribute *attr,
337 char *buf)
338{
339 struct hid_device *hdev = to_hid_device(dev);
340 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
341
342 return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
343}
344
345static ssize_t attr_fn_lock_store(struct device *dev,
346 struct device_attribute *attr,
347 const char *buf,
348 size_t count)
349{
350 struct hid_device *hdev = to_hid_device(dev);
351 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
352 int value;
353
354 if (kstrtoint(buf, 10, &value))
355 return -EINVAL;
356 if (value < 0 || value > 1)
357 return -EINVAL;
358
359 data->fn_lock = !!value;
360
361 switch (hdev->product) {
362 case USB_DEVICE_ID_LENOVO_CUSBKBD:
363 case USB_DEVICE_ID_LENOVO_CBTKBD:
364 lenovo_features_set_cptkbd(hdev);
365 break;
366 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
367 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
368 break;
369 }
370
371 return count;
372}
373
374static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
375 struct device_attribute *attr,
376 char *buf)
377{
378 struct hid_device *hdev = to_hid_device(dev);
379 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
380
381 return snprintf(buf, PAGE_SIZE, "%u\n",
382 cptkbd_data->sensitivity);
383}
384
385static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf,
388 size_t count)
389{
390 struct hid_device *hdev = to_hid_device(dev);
391 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
392 int value;
393
394 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
395 return -EINVAL;
396
397 cptkbd_data->sensitivity = value;
398 lenovo_features_set_cptkbd(hdev);
399
400 return count;
401}
402
403
404static struct device_attribute dev_attr_fn_lock =
405 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
406 attr_fn_lock_show,
407 attr_fn_lock_store);
408
409static struct device_attribute dev_attr_sensitivity_cptkbd =
410 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
411 attr_sensitivity_show_cptkbd,
412 attr_sensitivity_store_cptkbd);
413
414
415static struct attribute *lenovo_attributes_cptkbd[] = {
416 &dev_attr_fn_lock.attr,
417 &dev_attr_sensitivity_cptkbd.attr,
418 NULL
419};
420
421static const struct attribute_group lenovo_attr_group_cptkbd = {
422 .attrs = lenovo_attributes_cptkbd,
423};
424
425static int lenovo_raw_event(struct hid_device *hdev,
426 struct hid_report *report, u8 *data, int size)
427{
428 /*
429 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
430 * its own key is outside the usage page range. Remove extra
431 * keypresses and remap to inside usage page.
432 */
433 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
434 && size == 3
435 && data[0] == 0x15
436 && data[1] == 0x94
437 && data[2] == 0x01)) {
438 data[1] = 0x00;
439 data[2] = 0x01;
440 }
441
442 return 0;
443}
444
445static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
446 struct hid_field *field, struct hid_usage *usage, __s32 value)
447{
448 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
449
450 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
451 /*
452 * The user has toggled the Fn-lock state. Toggle our own
453 * cached value of it and sync our value to the keyboard to
454 * ensure things are in sync (the sycning should be a no-op).
455 */
456 data->fn_lock = !data->fn_lock;
457 schedule_work(&data->fn_lock_sync_work);
458 }
459
460 return 0;
461}
462
463static int lenovo_event_cptkbd(struct hid_device *hdev,
464 struct hid_field *field, struct hid_usage *usage, __s32 value)
465{
466 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
467
468 /* "wheel" scroll events */
469 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
470 usage->code == REL_HWHEEL)) {
471 /* Scroll events disable middle-click event */
472 cptkbd_data->middlebutton_state = 2;
473 return 0;
474 }
475
476 /* Middle click events */
477 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
478 if (value == 1) {
479 cptkbd_data->middlebutton_state = 1;
480 } else if (value == 0) {
481 if (cptkbd_data->middlebutton_state == 1) {
482 /* No scrolling inbetween, send middle-click */
483 input_event(field->hidinput->input,
484 EV_KEY, BTN_MIDDLE, 1);
485 input_sync(field->hidinput->input);
486 input_event(field->hidinput->input,
487 EV_KEY, BTN_MIDDLE, 0);
488 input_sync(field->hidinput->input);
489 }
490 cptkbd_data->middlebutton_state = 0;
491 }
492 return 1;
493 }
494
495 return 0;
496}
497
498static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
499 struct hid_usage *usage, __s32 value)
500{
501 switch (hdev->product) {
502 case USB_DEVICE_ID_LENOVO_CUSBKBD:
503 case USB_DEVICE_ID_LENOVO_CBTKBD:
504 return lenovo_event_cptkbd(hdev, field, usage, value);
505 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
506 return lenovo_event_tp10ubkbd(hdev, field, usage, value);
507 default:
508 return 0;
509 }
510}
511
512static int lenovo_features_set_tpkbd(struct hid_device *hdev)
513{
514 struct hid_report *report;
515 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
516
517 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
518
519 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
520 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
521 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
522 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
523 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
524 report->field[2]->value[0] = data_pointer->sensitivity;
525 report->field[3]->value[0] = data_pointer->press_speed;
526
527 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
528 return 0;
529}
530
531static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
532 struct device_attribute *attr,
533 char *buf)
534{
535 struct hid_device *hdev = to_hid_device(dev);
536 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
537
538 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
539}
540
541static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
542 struct device_attribute *attr,
543 const char *buf,
544 size_t count)
545{
546 struct hid_device *hdev = to_hid_device(dev);
547 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
548 int value;
549
550 if (kstrtoint(buf, 10, &value))
551 return -EINVAL;
552 if (value < 0 || value > 1)
553 return -EINVAL;
554
555 data_pointer->press_to_select = value;
556 lenovo_features_set_tpkbd(hdev);
557
558 return count;
559}
560
561static ssize_t attr_dragging_show_tpkbd(struct device *dev,
562 struct device_attribute *attr,
563 char *buf)
564{
565 struct hid_device *hdev = to_hid_device(dev);
566 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
567
568 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
569}
570
571static ssize_t attr_dragging_store_tpkbd(struct device *dev,
572 struct device_attribute *attr,
573 const char *buf,
574 size_t count)
575{
576 struct hid_device *hdev = to_hid_device(dev);
577 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
578 int value;
579
580 if (kstrtoint(buf, 10, &value))
581 return -EINVAL;
582 if (value < 0 || value > 1)
583 return -EINVAL;
584
585 data_pointer->dragging = value;
586 lenovo_features_set_tpkbd(hdev);
587
588 return count;
589}
590
591static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
592 struct device_attribute *attr,
593 char *buf)
594{
595 struct hid_device *hdev = to_hid_device(dev);
596 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
597
598 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
599}
600
601static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
602 struct device_attribute *attr,
603 const char *buf,
604 size_t count)
605{
606 struct hid_device *hdev = to_hid_device(dev);
607 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
608 int value;
609
610 if (kstrtoint(buf, 10, &value))
611 return -EINVAL;
612 if (value < 0 || value > 1)
613 return -EINVAL;
614
615 data_pointer->release_to_select = value;
616 lenovo_features_set_tpkbd(hdev);
617
618 return count;
619}
620
621static ssize_t attr_select_right_show_tpkbd(struct device *dev,
622 struct device_attribute *attr,
623 char *buf)
624{
625 struct hid_device *hdev = to_hid_device(dev);
626 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
627
628 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
629}
630
631static ssize_t attr_select_right_store_tpkbd(struct device *dev,
632 struct device_attribute *attr,
633 const char *buf,
634 size_t count)
635{
636 struct hid_device *hdev = to_hid_device(dev);
637 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
638 int value;
639
640 if (kstrtoint(buf, 10, &value))
641 return -EINVAL;
642 if (value < 0 || value > 1)
643 return -EINVAL;
644
645 data_pointer->select_right = value;
646 lenovo_features_set_tpkbd(hdev);
647
648 return count;
649}
650
651static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
652 struct device_attribute *attr,
653 char *buf)
654{
655 struct hid_device *hdev = to_hid_device(dev);
656 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
657
658 return snprintf(buf, PAGE_SIZE, "%u\n",
659 data_pointer->sensitivity);
660}
661
662static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
663 struct device_attribute *attr,
664 const char *buf,
665 size_t count)
666{
667 struct hid_device *hdev = to_hid_device(dev);
668 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
669 int value;
670
671 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
672 return -EINVAL;
673
674 data_pointer->sensitivity = value;
675 lenovo_features_set_tpkbd(hdev);
676
677 return count;
678}
679
680static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
681 struct device_attribute *attr,
682 char *buf)
683{
684 struct hid_device *hdev = to_hid_device(dev);
685 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
686
687 return snprintf(buf, PAGE_SIZE, "%u\n",
688 data_pointer->press_speed);
689}
690
691static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
692 struct device_attribute *attr,
693 const char *buf,
694 size_t count)
695{
696 struct hid_device *hdev = to_hid_device(dev);
697 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
698 int value;
699
700 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
701 return -EINVAL;
702
703 data_pointer->press_speed = value;
704 lenovo_features_set_tpkbd(hdev);
705
706 return count;
707}
708
709static struct device_attribute dev_attr_press_to_select_tpkbd =
710 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
711 attr_press_to_select_show_tpkbd,
712 attr_press_to_select_store_tpkbd);
713
714static struct device_attribute dev_attr_dragging_tpkbd =
715 __ATTR(dragging, S_IWUSR | S_IRUGO,
716 attr_dragging_show_tpkbd,
717 attr_dragging_store_tpkbd);
718
719static struct device_attribute dev_attr_release_to_select_tpkbd =
720 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
721 attr_release_to_select_show_tpkbd,
722 attr_release_to_select_store_tpkbd);
723
724static struct device_attribute dev_attr_select_right_tpkbd =
725 __ATTR(select_right, S_IWUSR | S_IRUGO,
726 attr_select_right_show_tpkbd,
727 attr_select_right_store_tpkbd);
728
729static struct device_attribute dev_attr_sensitivity_tpkbd =
730 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
731 attr_sensitivity_show_tpkbd,
732 attr_sensitivity_store_tpkbd);
733
734static struct device_attribute dev_attr_press_speed_tpkbd =
735 __ATTR(press_speed, S_IWUSR | S_IRUGO,
736 attr_press_speed_show_tpkbd,
737 attr_press_speed_store_tpkbd);
738
739static struct attribute *lenovo_attributes_tpkbd[] = {
740 &dev_attr_press_to_select_tpkbd.attr,
741 &dev_attr_dragging_tpkbd.attr,
742 &dev_attr_release_to_select_tpkbd.attr,
743 &dev_attr_select_right_tpkbd.attr,
744 &dev_attr_sensitivity_tpkbd.attr,
745 &dev_attr_press_speed_tpkbd.attr,
746 NULL
747};
748
749static const struct attribute_group lenovo_attr_group_tpkbd = {
750 .attrs = lenovo_attributes_tpkbd,
751};
752
753static void lenovo_led_set_tpkbd(struct hid_device *hdev)
754{
755 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
756 struct hid_report *report;
757
758 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
759 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
760 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
761 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
762}
763
764static enum led_brightness lenovo_led_brightness_get(
765 struct led_classdev *led_cdev)
766{
767 struct device *dev = led_cdev->dev->parent;
768 struct hid_device *hdev = to_hid_device(dev);
769 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
770 int led_nr = 0;
771
772 if (led_cdev == &data_pointer->led_micmute)
773 led_nr = 1;
774
775 return data_pointer->led_state & (1 << led_nr)
776 ? LED_FULL
777 : LED_OFF;
778}
779
780static void lenovo_led_brightness_set(struct led_classdev *led_cdev,
781 enum led_brightness value)
782{
783 struct device *dev = led_cdev->dev->parent;
784 struct hid_device *hdev = to_hid_device(dev);
785 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
786 u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
787 int led_nr = 0;
788
789 if (led_cdev == &data_pointer->led_micmute)
790 led_nr = 1;
791
792 if (value == LED_OFF)
793 data_pointer->led_state &= ~(1 << led_nr);
794 else
795 data_pointer->led_state |= 1 << led_nr;
796
797 switch (hdev->product) {
798 case USB_DEVICE_ID_LENOVO_TPKBD:
799 lenovo_led_set_tpkbd(hdev);
800 break;
801 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
802 lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
803 break;
804 }
805}
806
807static int lenovo_register_leds(struct hid_device *hdev)
808{
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;
812 int ret;
813
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");
818 return -ENOMEM;
819 }
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));
822
823 data->led_mute.name = name_mute;
824 data->led_mute.brightness_get = lenovo_led_brightness_get;
825 data->led_mute.brightness_set = lenovo_led_brightness_set;
826 data->led_mute.dev = &hdev->dev;
827 ret = led_classdev_register(&hdev->dev, &data->led_mute);
828 if (ret < 0)
829 return ret;
830
831 data->led_micmute.name = name_micm;
832 data->led_micmute.brightness_get = lenovo_led_brightness_get;
833 data->led_micmute.brightness_set = lenovo_led_brightness_set;
834 data->led_micmute.dev = &hdev->dev;
835 ret = led_classdev_register(&hdev->dev, &data->led_micmute);
836 if (ret < 0) {
837 led_classdev_unregister(&data->led_mute);
838 return ret;
839 }
840
841 return 0;
842}
843
844static int lenovo_probe_tpkbd(struct hid_device *hdev)
845{
846 struct lenovo_drvdata *data_pointer;
847 int i, ret;
848
849 /*
850 * Only register extra settings against subdevice where input_mapping
851 * set drvdata to 1, i.e. the trackpoint.
852 */
853 if (!hid_get_drvdata(hdev))
854 return 0;
855
856 hid_set_drvdata(hdev, NULL);
857
858 /* Validate required reports. */
859 for (i = 0; i < 4; i++) {
860 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
861 return -ENODEV;
862 }
863 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
864 return -ENODEV;
865
866 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
867 if (ret)
868 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
869
870 data_pointer = devm_kzalloc(&hdev->dev,
871 sizeof(struct lenovo_drvdata),
872 GFP_KERNEL);
873 if (data_pointer == NULL) {
874 hid_err(hdev, "Could not allocate memory for driver data\n");
875 ret = -ENOMEM;
876 goto err;
877 }
878
879 // set same default values as windows driver
880 data_pointer->sensitivity = 0xa0;
881 data_pointer->press_speed = 0x38;
882
883 hid_set_drvdata(hdev, data_pointer);
884
885 ret = lenovo_register_leds(hdev);
886 if (ret)
887 goto err;
888
889 lenovo_features_set_tpkbd(hdev);
890
891 return 0;
892err:
893 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
894 return ret;
895}
896
897static int lenovo_probe_cptkbd(struct hid_device *hdev)
898{
899 int ret;
900 struct lenovo_drvdata *cptkbd_data;
901
902 /* All the custom action happens on the USBMOUSE device for USB */
903 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
904 && hdev->type != HID_TYPE_USBMOUSE) {
905 hid_dbg(hdev, "Ignoring keyboard half of device\n");
906 return 0;
907 }
908
909 cptkbd_data = devm_kzalloc(&hdev->dev,
910 sizeof(*cptkbd_data),
911 GFP_KERNEL);
912 if (cptkbd_data == NULL) {
913 hid_err(hdev, "can't alloc keyboard descriptor\n");
914 return -ENOMEM;
915 }
916 hid_set_drvdata(hdev, cptkbd_data);
917
918 /*
919 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
920 * regular keys
921 */
922 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
923 if (ret)
924 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
925
926 /* Switch middle button to native mode */
927 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
928 if (ret)
929 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
930
931 /* Set keyboard settings to known state */
932 cptkbd_data->middlebutton_state = 0;
933 cptkbd_data->fn_lock = true;
934 cptkbd_data->sensitivity = 0x05;
935 lenovo_features_set_cptkbd(hdev);
936
937 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
938 if (ret)
939 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
940
941 return 0;
942}
943
944static struct attribute *lenovo_attributes_tp10ubkbd[] = {
945 &dev_attr_fn_lock.attr,
946 NULL
947};
948
949static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
950 .attrs = lenovo_attributes_tp10ubkbd,
951};
952
953static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
954{
955 struct lenovo_drvdata *data;
956 int ret;
957
958 /* All the custom action happens on the USBMOUSE device for USB */
959 if (hdev->type != HID_TYPE_USBMOUSE)
960 return 0;
961
962 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
963 if (!data)
964 return -ENOMEM;
965
966 mutex_init(&data->led_report_mutex);
967 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
968 data->hdev = hdev;
969
970 hid_set_drvdata(hdev, data);
971
972 /*
973 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
974 * We cannot read the state, only set it, so we force it to on here
975 * (which should be a no-op) to make sure that our state matches the
976 * keyboard's FN-lock state. This is the same as what Windows does.
977 */
978 data->fn_lock = true;
979 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
980
981 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
982 if (ret)
983 return ret;
984
985 ret = lenovo_register_leds(hdev);
986 if (ret)
987 goto err;
988
989 return 0;
990err:
991 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
992 return ret;
993}
994
995static int lenovo_probe(struct hid_device *hdev,
996 const struct hid_device_id *id)
997{
998 int ret;
999
1000 ret = hid_parse(hdev);
1001 if (ret) {
1002 hid_err(hdev, "hid_parse failed\n");
1003 goto err;
1004 }
1005
1006 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1007 if (ret) {
1008 hid_err(hdev, "hid_hw_start failed\n");
1009 goto err;
1010 }
1011
1012 switch (hdev->product) {
1013 case USB_DEVICE_ID_LENOVO_TPKBD:
1014 ret = lenovo_probe_tpkbd(hdev);
1015 break;
1016 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1017 case USB_DEVICE_ID_LENOVO_CBTKBD:
1018 ret = lenovo_probe_cptkbd(hdev);
1019 break;
1020 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1021 ret = lenovo_probe_tp10ubkbd(hdev);
1022 break;
1023 default:
1024 ret = 0;
1025 break;
1026 }
1027 if (ret)
1028 goto err_hid;
1029
1030 return 0;
1031err_hid:
1032 hid_hw_stop(hdev);
1033err:
1034 return ret;
1035}
1036
1037static void lenovo_remove_tpkbd(struct hid_device *hdev)
1038{
1039 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
1040
1041 /*
1042 * Only the trackpoint half of the keyboard has drvdata and stuff that
1043 * needs unregistering.
1044 */
1045 if (data_pointer == NULL)
1046 return;
1047
1048 sysfs_remove_group(&hdev->dev.kobj,
1049 &lenovo_attr_group_tpkbd);
1050
1051 led_classdev_unregister(&data_pointer->led_micmute);
1052 led_classdev_unregister(&data_pointer->led_mute);
1053}
1054
1055static void lenovo_remove_cptkbd(struct hid_device *hdev)
1056{
1057 sysfs_remove_group(&hdev->dev.kobj,
1058 &lenovo_attr_group_cptkbd);
1059}
1060
1061static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
1062{
1063 struct lenovo_drvdata *data = hid_get_drvdata(hdev);
1064
1065 if (data == NULL)
1066 return;
1067
1068 led_classdev_unregister(&data->led_micmute);
1069 led_classdev_unregister(&data->led_mute);
1070
1071 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
1072 cancel_work_sync(&data->fn_lock_sync_work);
1073}
1074
1075static void lenovo_remove(struct hid_device *hdev)
1076{
1077 switch (hdev->product) {
1078 case USB_DEVICE_ID_LENOVO_TPKBD:
1079 lenovo_remove_tpkbd(hdev);
1080 break;
1081 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1082 case USB_DEVICE_ID_LENOVO_CBTKBD:
1083 lenovo_remove_cptkbd(hdev);
1084 break;
1085 case USB_DEVICE_ID_LENOVO_TP10UBKBD:
1086 lenovo_remove_tp10ubkbd(hdev);
1087 break;
1088 }
1089
1090 hid_hw_stop(hdev);
1091}
1092
1093static int lenovo_input_configured(struct hid_device *hdev,
1094 struct hid_input *hi)
1095{
1096 switch (hdev->product) {
1097 case USB_DEVICE_ID_LENOVO_TPKBD:
1098 case USB_DEVICE_ID_LENOVO_CUSBKBD:
1099 case USB_DEVICE_ID_LENOVO_CBTKBD:
1100 if (test_bit(EV_REL, hi->input->evbit)) {
1101 /* set only for trackpoint device */
1102 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
1103 __set_bit(INPUT_PROP_POINTING_STICK,
1104 hi->input->propbit);
1105 }
1106 break;
1107 }
1108
1109 return 0;
1110}
1111
1112
1113static const struct hid_device_id lenovo_devices[] = {
1114 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1115 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
1116 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
1117 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
1118 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
1119 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
1120 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
1121 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
1122 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
1123 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
1124 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
1125 { }
1126};
1127
1128MODULE_DEVICE_TABLE(hid, lenovo_devices);
1129
1130static struct hid_driver lenovo_driver = {
1131 .name = "lenovo",
1132 .id_table = lenovo_devices,
1133 .input_configured = lenovo_input_configured,
1134 .input_mapping = lenovo_input_mapping,
1135 .probe = lenovo_probe,
1136 .remove = lenovo_remove,
1137 .raw_event = lenovo_raw_event,
1138 .event = lenovo_event,
1139 .report_fixup = lenovo_report_fixup,
1140};
1141module_hid_driver(lenovo_driver);
1142
1143MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * HID driver for Lenovo:
4 * - ThinkPad USB Keyboard with TrackPoint (tpkbd)
5 * - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
6 * - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
7 *
8 * Copyright (c) 2012 Bernhard Seibold
9 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
10 *
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
18 *
19 * Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
20 * Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
21 */
22
23/*
24 */
25
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
33#include "hid-ids.h"
34
35struct lenovo_drvdata_tpkbd {
36 int led_state;
37 struct led_classdev led_mute;
38 struct led_classdev led_micmute;
39 int press_to_select;
40 int dragging;
41 int release_to_select;
42 int select_right;
43 int sensitivity;
44 int press_speed;
45};
46
47struct lenovo_drvdata_cptkbd {
48 u8 middlebutton_state; /* 0:Up, 1:Down (undecided), 2:Scrolling */
49 bool fn_lock;
50 int sensitivity;
51};
52
53#define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
54
55static const __u8 lenovo_pro_dock_need_fixup_collection[] = {
56 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */
57 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
58 0xa1, 0x01, /* Collection (Application) */
59 0x85, 0x04, /* Report ID (4) */
60 0x19, 0x00, /* Usage Minimum (0) */
61 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */
62};
63
64static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
65 unsigned int *rsize)
66{
67 switch (hdev->product) {
68 case USB_DEVICE_ID_LENOVO_TPPRODOCK:
69 /* the fixups that need to be done:
70 * - get a reasonable usage max for the vendor collection
71 * 0x8801 from the report ID 4
72 */
73 if (*rsize >= 153 &&
74 memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
75 sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
76 rdesc[151] = 0x01;
77 rdesc[152] = 0x00;
78 }
79 break;
80 }
81 return rdesc;
82}
83
84static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
85 struct hid_input *hi, struct hid_field *field,
86 struct hid_usage *usage, unsigned long **bit, int *max)
87{
88 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
89 /* This sub-device contains trackpoint, mark it */
90 hid_set_drvdata(hdev, (void *)1);
91 map_key_clear(KEY_MICMUTE);
92 return 1;
93 }
94 return 0;
95}
96
97static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
98 struct hid_input *hi, struct hid_field *field,
99 struct hid_usage *usage, unsigned long **bit, int *max)
100{
101 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
102 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
103 (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
104 switch (usage->hid & HID_USAGE) {
105 case 0x00f1: /* Fn-F4: Mic mute */
106 map_key_clear(KEY_MICMUTE);
107 return 1;
108 case 0x00f2: /* Fn-F5: Brightness down */
109 map_key_clear(KEY_BRIGHTNESSDOWN);
110 return 1;
111 case 0x00f3: /* Fn-F6: Brightness up */
112 map_key_clear(KEY_BRIGHTNESSUP);
113 return 1;
114 case 0x00f4: /* Fn-F7: External display (projector) */
115 map_key_clear(KEY_SWITCHVIDEOMODE);
116 return 1;
117 case 0x00f5: /* Fn-F8: Wireless */
118 map_key_clear(KEY_WLAN);
119 return 1;
120 case 0x00f6: /* Fn-F9: Control panel */
121 map_key_clear(KEY_CONFIG);
122 return 1;
123 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
124 map_key_clear(KEY_SCALE);
125 return 1;
126 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
127 /* NB: This mapping is invented in raw_event below */
128 map_key_clear(KEY_FILE);
129 return 1;
130 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
131 map_key_clear(KEY_FN_ESC);
132 return 1;
133 case 0x00fb: /* Middle mouse button (in native mode) */
134 map_key_clear(BTN_MIDDLE);
135 return 1;
136 }
137 }
138
139 /* Compatibility middle/wheel mappings should be ignored */
140 if (usage->hid == HID_GD_WHEEL)
141 return -1;
142 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
143 (usage->hid & HID_USAGE) == 0x003)
144 return -1;
145 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
146 (usage->hid & HID_USAGE) == 0x238)
147 return -1;
148
149 /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
150 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
151 (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
152 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
153 field->logical_minimum = -127;
154 field->logical_maximum = 127;
155
156 switch (usage->hid & HID_USAGE) {
157 case 0x0000:
158 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
159 return 1;
160 case 0x0001:
161 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
162 return 1;
163 default:
164 return -1;
165 }
166 }
167
168 return 0;
169}
170
171static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
172 struct hid_input *hi, struct hid_field *field,
173 struct hid_usage *usage, unsigned long **bit, int *max)
174{
175 if (usage->hid == HID_GD_Z) {
176 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
177 return 1;
178 }
179 return 0;
180}
181
182static int lenovo_input_mapping(struct hid_device *hdev,
183 struct hid_input *hi, struct hid_field *field,
184 struct hid_usage *usage, unsigned long **bit, int *max)
185{
186 switch (hdev->product) {
187 case USB_DEVICE_ID_LENOVO_TPKBD:
188 return lenovo_input_mapping_tpkbd(hdev, hi, field,
189 usage, bit, max);
190 case USB_DEVICE_ID_LENOVO_CUSBKBD:
191 case USB_DEVICE_ID_LENOVO_CBTKBD:
192 return lenovo_input_mapping_cptkbd(hdev, hi, field,
193 usage, bit, max);
194 case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
195 case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
196 case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
197 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
198 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
199 case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
200 return lenovo_input_mapping_scrollpoint(hdev, hi, field,
201 usage, bit, max);
202 default:
203 return 0;
204 }
205}
206
207#undef map_key_clear
208
209/* Send a config command to the keyboard */
210static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
211 unsigned char byte2, unsigned char byte3)
212{
213 int ret;
214 unsigned char *buf;
215
216 buf = kzalloc(3, GFP_KERNEL);
217 if (!buf)
218 return -ENOMEM;
219
220 buf[0] = 0x18;
221 buf[1] = byte2;
222 buf[2] = byte3;
223
224 switch (hdev->product) {
225 case USB_DEVICE_ID_LENOVO_CUSBKBD:
226 ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
227 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
228 break;
229 case USB_DEVICE_ID_LENOVO_CBTKBD:
230 ret = hid_hw_output_report(hdev, buf, 3);
231 break;
232 default:
233 ret = -EINVAL;
234 break;
235 }
236
237 kfree(buf);
238
239 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
240}
241
242static void lenovo_features_set_cptkbd(struct hid_device *hdev)
243{
244 int ret;
245 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
246
247 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
248 if (ret)
249 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
250
251 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
252 if (ret)
253 hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
254}
255
256static ssize_t attr_fn_lock_show_cptkbd(struct device *dev,
257 struct device_attribute *attr,
258 char *buf)
259{
260 struct hid_device *hdev = to_hid_device(dev);
261 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
262
263 return snprintf(buf, PAGE_SIZE, "%u\n", cptkbd_data->fn_lock);
264}
265
266static ssize_t attr_fn_lock_store_cptkbd(struct device *dev,
267 struct device_attribute *attr,
268 const char *buf,
269 size_t count)
270{
271 struct hid_device *hdev = to_hid_device(dev);
272 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
273 int value;
274
275 if (kstrtoint(buf, 10, &value))
276 return -EINVAL;
277 if (value < 0 || value > 1)
278 return -EINVAL;
279
280 cptkbd_data->fn_lock = !!value;
281 lenovo_features_set_cptkbd(hdev);
282
283 return count;
284}
285
286static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
287 struct device_attribute *attr,
288 char *buf)
289{
290 struct hid_device *hdev = to_hid_device(dev);
291 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
292
293 return snprintf(buf, PAGE_SIZE, "%u\n",
294 cptkbd_data->sensitivity);
295}
296
297static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
298 struct device_attribute *attr,
299 const char *buf,
300 size_t count)
301{
302 struct hid_device *hdev = to_hid_device(dev);
303 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
304 int value;
305
306 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
307 return -EINVAL;
308
309 cptkbd_data->sensitivity = value;
310 lenovo_features_set_cptkbd(hdev);
311
312 return count;
313}
314
315
316static struct device_attribute dev_attr_fn_lock_cptkbd =
317 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
318 attr_fn_lock_show_cptkbd,
319 attr_fn_lock_store_cptkbd);
320
321static struct device_attribute dev_attr_sensitivity_cptkbd =
322 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
323 attr_sensitivity_show_cptkbd,
324 attr_sensitivity_store_cptkbd);
325
326
327static struct attribute *lenovo_attributes_cptkbd[] = {
328 &dev_attr_fn_lock_cptkbd.attr,
329 &dev_attr_sensitivity_cptkbd.attr,
330 NULL
331};
332
333static const struct attribute_group lenovo_attr_group_cptkbd = {
334 .attrs = lenovo_attributes_cptkbd,
335};
336
337static int lenovo_raw_event(struct hid_device *hdev,
338 struct hid_report *report, u8 *data, int size)
339{
340 /*
341 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
342 * its own key is outside the usage page range. Remove extra
343 * keypresses and remap to inside usage page.
344 */
345 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
346 && size == 3
347 && data[0] == 0x15
348 && data[1] == 0x94
349 && data[2] == 0x01)) {
350 data[1] = 0x00;
351 data[2] = 0x01;
352 }
353
354 return 0;
355}
356
357static int lenovo_event_cptkbd(struct hid_device *hdev,
358 struct hid_field *field, struct hid_usage *usage, __s32 value)
359{
360 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
361
362 /* "wheel" scroll events */
363 if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
364 usage->code == REL_HWHEEL)) {
365 /* Scroll events disable middle-click event */
366 cptkbd_data->middlebutton_state = 2;
367 return 0;
368 }
369
370 /* Middle click events */
371 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
372 if (value == 1) {
373 cptkbd_data->middlebutton_state = 1;
374 } else if (value == 0) {
375 if (cptkbd_data->middlebutton_state == 1) {
376 /* No scrolling inbetween, send middle-click */
377 input_event(field->hidinput->input,
378 EV_KEY, BTN_MIDDLE, 1);
379 input_sync(field->hidinput->input);
380 input_event(field->hidinput->input,
381 EV_KEY, BTN_MIDDLE, 0);
382 input_sync(field->hidinput->input);
383 }
384 cptkbd_data->middlebutton_state = 0;
385 }
386 return 1;
387 }
388
389 return 0;
390}
391
392static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
393 struct hid_usage *usage, __s32 value)
394{
395 switch (hdev->product) {
396 case USB_DEVICE_ID_LENOVO_CUSBKBD:
397 case USB_DEVICE_ID_LENOVO_CBTKBD:
398 return lenovo_event_cptkbd(hdev, field, usage, value);
399 default:
400 return 0;
401 }
402}
403
404static int lenovo_features_set_tpkbd(struct hid_device *hdev)
405{
406 struct hid_report *report;
407 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
408
409 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
410
411 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
412 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
413 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
414 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
415 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
416 report->field[2]->value[0] = data_pointer->sensitivity;
417 report->field[3]->value[0] = data_pointer->press_speed;
418
419 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
420 return 0;
421}
422
423static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
424 struct device_attribute *attr,
425 char *buf)
426{
427 struct hid_device *hdev = to_hid_device(dev);
428 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
429
430 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
431}
432
433static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
434 struct device_attribute *attr,
435 const char *buf,
436 size_t count)
437{
438 struct hid_device *hdev = to_hid_device(dev);
439 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
440 int value;
441
442 if (kstrtoint(buf, 10, &value))
443 return -EINVAL;
444 if (value < 0 || value > 1)
445 return -EINVAL;
446
447 data_pointer->press_to_select = value;
448 lenovo_features_set_tpkbd(hdev);
449
450 return count;
451}
452
453static ssize_t attr_dragging_show_tpkbd(struct device *dev,
454 struct device_attribute *attr,
455 char *buf)
456{
457 struct hid_device *hdev = to_hid_device(dev);
458 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
459
460 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
461}
462
463static ssize_t attr_dragging_store_tpkbd(struct device *dev,
464 struct device_attribute *attr,
465 const char *buf,
466 size_t count)
467{
468 struct hid_device *hdev = to_hid_device(dev);
469 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
470 int value;
471
472 if (kstrtoint(buf, 10, &value))
473 return -EINVAL;
474 if (value < 0 || value > 1)
475 return -EINVAL;
476
477 data_pointer->dragging = value;
478 lenovo_features_set_tpkbd(hdev);
479
480 return count;
481}
482
483static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
484 struct device_attribute *attr,
485 char *buf)
486{
487 struct hid_device *hdev = to_hid_device(dev);
488 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
489
490 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
491}
492
493static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
494 struct device_attribute *attr,
495 const char *buf,
496 size_t count)
497{
498 struct hid_device *hdev = to_hid_device(dev);
499 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
500 int value;
501
502 if (kstrtoint(buf, 10, &value))
503 return -EINVAL;
504 if (value < 0 || value > 1)
505 return -EINVAL;
506
507 data_pointer->release_to_select = value;
508 lenovo_features_set_tpkbd(hdev);
509
510 return count;
511}
512
513static ssize_t attr_select_right_show_tpkbd(struct device *dev,
514 struct device_attribute *attr,
515 char *buf)
516{
517 struct hid_device *hdev = to_hid_device(dev);
518 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
519
520 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
521}
522
523static ssize_t attr_select_right_store_tpkbd(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf,
526 size_t count)
527{
528 struct hid_device *hdev = to_hid_device(dev);
529 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
530 int value;
531
532 if (kstrtoint(buf, 10, &value))
533 return -EINVAL;
534 if (value < 0 || value > 1)
535 return -EINVAL;
536
537 data_pointer->select_right = value;
538 lenovo_features_set_tpkbd(hdev);
539
540 return count;
541}
542
543static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
544 struct device_attribute *attr,
545 char *buf)
546{
547 struct hid_device *hdev = to_hid_device(dev);
548 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
549
550 return snprintf(buf, PAGE_SIZE, "%u\n",
551 data_pointer->sensitivity);
552}
553
554static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
555 struct device_attribute *attr,
556 const char *buf,
557 size_t count)
558{
559 struct hid_device *hdev = to_hid_device(dev);
560 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
561 int value;
562
563 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
564 return -EINVAL;
565
566 data_pointer->sensitivity = value;
567 lenovo_features_set_tpkbd(hdev);
568
569 return count;
570}
571
572static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
573 struct device_attribute *attr,
574 char *buf)
575{
576 struct hid_device *hdev = to_hid_device(dev);
577 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
578
579 return snprintf(buf, PAGE_SIZE, "%u\n",
580 data_pointer->press_speed);
581}
582
583static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
584 struct device_attribute *attr,
585 const char *buf,
586 size_t count)
587{
588 struct hid_device *hdev = to_hid_device(dev);
589 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
590 int value;
591
592 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
593 return -EINVAL;
594
595 data_pointer->press_speed = value;
596 lenovo_features_set_tpkbd(hdev);
597
598 return count;
599}
600
601static struct device_attribute dev_attr_press_to_select_tpkbd =
602 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
603 attr_press_to_select_show_tpkbd,
604 attr_press_to_select_store_tpkbd);
605
606static struct device_attribute dev_attr_dragging_tpkbd =
607 __ATTR(dragging, S_IWUSR | S_IRUGO,
608 attr_dragging_show_tpkbd,
609 attr_dragging_store_tpkbd);
610
611static struct device_attribute dev_attr_release_to_select_tpkbd =
612 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
613 attr_release_to_select_show_tpkbd,
614 attr_release_to_select_store_tpkbd);
615
616static struct device_attribute dev_attr_select_right_tpkbd =
617 __ATTR(select_right, S_IWUSR | S_IRUGO,
618 attr_select_right_show_tpkbd,
619 attr_select_right_store_tpkbd);
620
621static struct device_attribute dev_attr_sensitivity_tpkbd =
622 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
623 attr_sensitivity_show_tpkbd,
624 attr_sensitivity_store_tpkbd);
625
626static struct device_attribute dev_attr_press_speed_tpkbd =
627 __ATTR(press_speed, S_IWUSR | S_IRUGO,
628 attr_press_speed_show_tpkbd,
629 attr_press_speed_store_tpkbd);
630
631static struct attribute *lenovo_attributes_tpkbd[] = {
632 &dev_attr_press_to_select_tpkbd.attr,
633 &dev_attr_dragging_tpkbd.attr,
634 &dev_attr_release_to_select_tpkbd.attr,
635 &dev_attr_select_right_tpkbd.attr,
636 &dev_attr_sensitivity_tpkbd.attr,
637 &dev_attr_press_speed_tpkbd.attr,
638 NULL
639};
640
641static const struct attribute_group lenovo_attr_group_tpkbd = {
642 .attrs = lenovo_attributes_tpkbd,
643};
644
645static enum led_brightness lenovo_led_brightness_get_tpkbd(
646 struct led_classdev *led_cdev)
647{
648 struct device *dev = led_cdev->dev->parent;
649 struct hid_device *hdev = to_hid_device(dev);
650 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
651 int led_nr = 0;
652
653 if (led_cdev == &data_pointer->led_micmute)
654 led_nr = 1;
655
656 return data_pointer->led_state & (1 << led_nr)
657 ? LED_FULL
658 : LED_OFF;
659}
660
661static void lenovo_led_brightness_set_tpkbd(struct led_classdev *led_cdev,
662 enum led_brightness value)
663{
664 struct device *dev = led_cdev->dev->parent;
665 struct hid_device *hdev = to_hid_device(dev);
666 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
667 struct hid_report *report;
668 int led_nr = 0;
669
670 if (led_cdev == &data_pointer->led_micmute)
671 led_nr = 1;
672
673 if (value == LED_OFF)
674 data_pointer->led_state &= ~(1 << led_nr);
675 else
676 data_pointer->led_state |= 1 << led_nr;
677
678 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
679 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
680 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
681 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
682}
683
684static int lenovo_probe_tpkbd(struct hid_device *hdev)
685{
686 struct device *dev = &hdev->dev;
687 struct lenovo_drvdata_tpkbd *data_pointer;
688 size_t name_sz = strlen(dev_name(dev)) + 16;
689 char *name_mute, *name_micmute;
690 int i;
691 int ret;
692
693 /*
694 * Only register extra settings against subdevice where input_mapping
695 * set drvdata to 1, i.e. the trackpoint.
696 */
697 if (!hid_get_drvdata(hdev))
698 return 0;
699
700 hid_set_drvdata(hdev, NULL);
701
702 /* Validate required reports. */
703 for (i = 0; i < 4; i++) {
704 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
705 return -ENODEV;
706 }
707 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
708 return -ENODEV;
709
710 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
711 if (ret)
712 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
713
714 data_pointer = devm_kzalloc(&hdev->dev,
715 sizeof(struct lenovo_drvdata_tpkbd),
716 GFP_KERNEL);
717 if (data_pointer == NULL) {
718 hid_err(hdev, "Could not allocate memory for driver data\n");
719 ret = -ENOMEM;
720 goto err;
721 }
722
723 // set same default values as windows driver
724 data_pointer->sensitivity = 0xa0;
725 data_pointer->press_speed = 0x38;
726
727 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
728 name_micmute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
729 if (name_mute == NULL || name_micmute == NULL) {
730 hid_err(hdev, "Could not allocate memory for led data\n");
731 ret = -ENOMEM;
732 goto err;
733 }
734 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(dev));
735 snprintf(name_micmute, name_sz, "%s:amber:micmute", dev_name(dev));
736
737 hid_set_drvdata(hdev, data_pointer);
738
739 data_pointer->led_mute.name = name_mute;
740 data_pointer->led_mute.brightness_get = lenovo_led_brightness_get_tpkbd;
741 data_pointer->led_mute.brightness_set = lenovo_led_brightness_set_tpkbd;
742 data_pointer->led_mute.dev = dev;
743 ret = led_classdev_register(dev, &data_pointer->led_mute);
744 if (ret < 0)
745 goto err;
746
747 data_pointer->led_micmute.name = name_micmute;
748 data_pointer->led_micmute.brightness_get =
749 lenovo_led_brightness_get_tpkbd;
750 data_pointer->led_micmute.brightness_set =
751 lenovo_led_brightness_set_tpkbd;
752 data_pointer->led_micmute.dev = dev;
753 ret = led_classdev_register(dev, &data_pointer->led_micmute);
754 if (ret < 0) {
755 led_classdev_unregister(&data_pointer->led_mute);
756 goto err;
757 }
758
759 lenovo_features_set_tpkbd(hdev);
760
761 return 0;
762err:
763 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
764 return ret;
765}
766
767static int lenovo_probe_cptkbd(struct hid_device *hdev)
768{
769 int ret;
770 struct lenovo_drvdata_cptkbd *cptkbd_data;
771
772 /* All the custom action happens on the USBMOUSE device for USB */
773 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
774 && hdev->type != HID_TYPE_USBMOUSE) {
775 hid_dbg(hdev, "Ignoring keyboard half of device\n");
776 return 0;
777 }
778
779 cptkbd_data = devm_kzalloc(&hdev->dev,
780 sizeof(*cptkbd_data),
781 GFP_KERNEL);
782 if (cptkbd_data == NULL) {
783 hid_err(hdev, "can't alloc keyboard descriptor\n");
784 return -ENOMEM;
785 }
786 hid_set_drvdata(hdev, cptkbd_data);
787
788 /*
789 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
790 * regular keys
791 */
792 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
793 if (ret)
794 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
795
796 /* Switch middle button to native mode */
797 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
798 if (ret)
799 hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
800
801 /* Set keyboard settings to known state */
802 cptkbd_data->middlebutton_state = 0;
803 cptkbd_data->fn_lock = true;
804 cptkbd_data->sensitivity = 0x05;
805 lenovo_features_set_cptkbd(hdev);
806
807 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
808 if (ret)
809 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
810
811 return 0;
812}
813
814static int lenovo_probe(struct hid_device *hdev,
815 const struct hid_device_id *id)
816{
817 int ret;
818
819 ret = hid_parse(hdev);
820 if (ret) {
821 hid_err(hdev, "hid_parse failed\n");
822 goto err;
823 }
824
825 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
826 if (ret) {
827 hid_err(hdev, "hid_hw_start failed\n");
828 goto err;
829 }
830
831 switch (hdev->product) {
832 case USB_DEVICE_ID_LENOVO_TPKBD:
833 ret = lenovo_probe_tpkbd(hdev);
834 break;
835 case USB_DEVICE_ID_LENOVO_CUSBKBD:
836 case USB_DEVICE_ID_LENOVO_CBTKBD:
837 ret = lenovo_probe_cptkbd(hdev);
838 break;
839 default:
840 ret = 0;
841 break;
842 }
843 if (ret)
844 goto err_hid;
845
846 return 0;
847err_hid:
848 hid_hw_stop(hdev);
849err:
850 return ret;
851}
852
853static void lenovo_remove_tpkbd(struct hid_device *hdev)
854{
855 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
856
857 /*
858 * Only the trackpoint half of the keyboard has drvdata and stuff that
859 * needs unregistering.
860 */
861 if (data_pointer == NULL)
862 return;
863
864 sysfs_remove_group(&hdev->dev.kobj,
865 &lenovo_attr_group_tpkbd);
866
867 led_classdev_unregister(&data_pointer->led_micmute);
868 led_classdev_unregister(&data_pointer->led_mute);
869}
870
871static void lenovo_remove_cptkbd(struct hid_device *hdev)
872{
873 sysfs_remove_group(&hdev->dev.kobj,
874 &lenovo_attr_group_cptkbd);
875}
876
877static void lenovo_remove(struct hid_device *hdev)
878{
879 switch (hdev->product) {
880 case USB_DEVICE_ID_LENOVO_TPKBD:
881 lenovo_remove_tpkbd(hdev);
882 break;
883 case USB_DEVICE_ID_LENOVO_CUSBKBD:
884 case USB_DEVICE_ID_LENOVO_CBTKBD:
885 lenovo_remove_cptkbd(hdev);
886 break;
887 }
888
889 hid_hw_stop(hdev);
890}
891
892static int lenovo_input_configured(struct hid_device *hdev,
893 struct hid_input *hi)
894{
895 switch (hdev->product) {
896 case USB_DEVICE_ID_LENOVO_TPKBD:
897 case USB_DEVICE_ID_LENOVO_CUSBKBD:
898 case USB_DEVICE_ID_LENOVO_CBTKBD:
899 if (test_bit(EV_REL, hi->input->evbit)) {
900 /* set only for trackpoint device */
901 __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
902 __set_bit(INPUT_PROP_POINTING_STICK,
903 hi->input->propbit);
904 }
905 break;
906 }
907
908 return 0;
909}
910
911
912static const struct hid_device_id lenovo_devices[] = {
913 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
914 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
915 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
916 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
917 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
918 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
919 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
920 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
921 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
922 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
923 { }
924};
925
926MODULE_DEVICE_TABLE(hid, lenovo_devices);
927
928static struct hid_driver lenovo_driver = {
929 .name = "lenovo",
930 .id_table = lenovo_devices,
931 .input_configured = lenovo_input_configured,
932 .input_mapping = lenovo_input_mapping,
933 .probe = lenovo_probe,
934 .remove = lenovo_remove,
935 .raw_event = lenovo_raw_event,
936 .event = lenovo_event,
937 .report_fixup = lenovo_report_fixup,
938};
939module_hid_driver(lenovo_driver);
940
941MODULE_LICENSE("GPL");