Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[deliverable/linux.git] / drivers / hid / hid-lenovo.c
CommitLineData
c1dcad2d 1/*
6a5b414b
JL
2 * HID driver for Lenovo:
3 * - ThinkPad USB Keyboard with TrackPoint (tpkbd)
f3d4ff0e
JL
4 * - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
5 * - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
c1dcad2d
BS
6 *
7 * Copyright (c) 2012 Bernhard Seibold
f3d4ff0e 8 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
c1dcad2d
BS
9 */
10
11/*
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)
15 * any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/sysfs.h>
20#include <linux/device.h>
c1dcad2d
BS
21#include <linux/hid.h>
22#include <linux/input.h>
23#include <linux/leds.h>
c1dcad2d
BS
24
25#include "hid-ids.h"
26
94723bfa 27struct lenovo_drvdata_tpkbd {
c1dcad2d
BS
28 int led_state;
29 struct led_classdev led_mute;
30 struct led_classdev led_micmute;
31 int press_to_select;
32 int dragging;
33 int release_to_select;
34 int select_right;
35 int sensitivity;
36 int press_speed;
37};
38
f3d4ff0e
JL
39struct lenovo_drvdata_cptkbd {
40 bool fn_lock;
41};
42
c1dcad2d
BS
43#define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
44
94723bfa 45static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
c1dcad2d
BS
46 struct hid_input *hi, struct hid_field *field,
47 struct hid_usage *usage, unsigned long **bit, int *max)
48{
0c521836 49 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
6a5b414b 50 /* This sub-device contains trackpoint, mark it */
0c521836 51 hid_set_drvdata(hdev, (void *)1);
c1dcad2d
BS
52 map_key_clear(KEY_MICMUTE);
53 return 1;
54 }
55 return 0;
56}
57
f3d4ff0e
JL
58static 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)
61{
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) {
f3d4ff0e
JL
65 switch (usage->hid & HID_USAGE) {
66 case 0x00f1: /* Fn-F4: Mic mute */
67 map_key_clear(KEY_MICMUTE);
68 return 1;
69 case 0x00f2: /* Fn-F5: Brightness down */
70 map_key_clear(KEY_BRIGHTNESSDOWN);
71 return 1;
72 case 0x00f3: /* Fn-F6: Brightness up */
73 map_key_clear(KEY_BRIGHTNESSUP);
74 return 1;
75 case 0x00f4: /* Fn-F7: External display (projector) */
76 map_key_clear(KEY_SWITCHVIDEOMODE);
77 return 1;
78 case 0x00f5: /* Fn-F8: Wireless */
79 map_key_clear(KEY_WLAN);
80 return 1;
81 case 0x00f6: /* Fn-F9: Control panel */
82 map_key_clear(KEY_CONFIG);
83 return 1;
84 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
85 map_key_clear(KEY_SCALE);
86 return 1;
5556eb14 87 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
f3d4ff0e
JL
88 /* NB: This mapping is invented in raw_event below */
89 map_key_clear(KEY_FILE);
90 return 1;
5556eb14
JL
91 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
92 map_key_clear(KEY_FN_ESC);
93 return 1;
f3d4ff0e
JL
94 }
95 }
96
97 return 0;
98}
99
6a5b414b
JL
100static 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)
103{
104 switch (hdev->product) {
105 case USB_DEVICE_ID_LENOVO_TPKBD:
106 return lenovo_input_mapping_tpkbd(hdev, hi, field,
107 usage, bit, max);
f3d4ff0e
JL
108 case USB_DEVICE_ID_LENOVO_CUSBKBD:
109 case USB_DEVICE_ID_LENOVO_CBTKBD:
110 return lenovo_input_mapping_cptkbd(hdev, hi, field,
111 usage, bit, max);
6a5b414b
JL
112 default:
113 return 0;
114 }
115}
116
c1dcad2d
BS
117#undef map_key_clear
118
f3d4ff0e
JL
119/* Send a config command to the keyboard */
120static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
121 unsigned char byte2, unsigned char byte3)
122{
123 int ret;
124 unsigned char buf[] = {0x18, byte2, byte3};
125
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);
130 break;
131 case USB_DEVICE_ID_LENOVO_CBTKBD:
132 ret = hid_hw_output_report(hdev, buf, sizeof(buf));
133 break;
134 default:
135 ret = -EINVAL;
136 break;
137 }
138
139 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
140}
141
142static void lenovo_features_set_cptkbd(struct hid_device *hdev)
143{
144 int ret;
145 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
146
147 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
148 if (ret)
149 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
150}
151
152static ssize_t attr_fn_lock_show_cptkbd(struct device *dev,
153 struct device_attribute *attr,
154 char *buf)
155{
156 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
157 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
158
159 return snprintf(buf, PAGE_SIZE, "%u\n", cptkbd_data->fn_lock);
160}
161
162static ssize_t attr_fn_lock_store_cptkbd(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf,
165 size_t count)
166{
167 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
168 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
169 int value;
170
171 if (kstrtoint(buf, 10, &value))
172 return -EINVAL;
173 if (value < 0 || value > 1)
174 return -EINVAL;
175
176 cptkbd_data->fn_lock = !!value;
177 lenovo_features_set_cptkbd(hdev);
178
179 return count;
180}
181
182static 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);
186
187static struct attribute *lenovo_attributes_cptkbd[] = {
188 &dev_attr_fn_lock_cptkbd.attr,
189 NULL
190};
191
192static const struct attribute_group lenovo_attr_group_cptkbd = {
193 .attrs = lenovo_attributes_cptkbd,
194};
195
196static int lenovo_raw_event(struct hid_device *hdev,
197 struct hid_report *report, u8 *data, int size)
198{
199 /*
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.
203 */
204 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
205 && size == 3
206 && data[0] == 0x15
207 && data[1] == 0x94
208 && data[2] == 0x01)) {
5556eb14
JL
209 data[1] = 0x00;
210 data[2] = 0x01;
f3d4ff0e
JL
211 }
212
213 return 0;
214}
215
94723bfa 216static int lenovo_features_set_tpkbd(struct hid_device *hdev)
c1dcad2d
BS
217{
218 struct hid_report *report;
94723bfa 219 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d 220
c1dcad2d
BS
221 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
222
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;
230
d8814272 231 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
c1dcad2d
BS
232 return 0;
233}
234
94723bfa 235static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
c1dcad2d
BS
236 struct device_attribute *attr,
237 char *buf)
238{
832fbeae 239 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 240 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
241
242 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
243}
244
94723bfa 245static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
c1dcad2d
BS
246 struct device_attribute *attr,
247 const char *buf,
248 size_t count)
249{
832fbeae 250 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 251 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
252 int value;
253
c1dcad2d
BS
254 if (kstrtoint(buf, 10, &value))
255 return -EINVAL;
256 if (value < 0 || value > 1)
257 return -EINVAL;
258
259 data_pointer->press_to_select = value;
94723bfa 260 lenovo_features_set_tpkbd(hdev);
c1dcad2d
BS
261
262 return count;
263}
264
94723bfa 265static ssize_t attr_dragging_show_tpkbd(struct device *dev,
c1dcad2d
BS
266 struct device_attribute *attr,
267 char *buf)
268{
832fbeae 269 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 270 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
271
272 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
273}
274
94723bfa 275static ssize_t attr_dragging_store_tpkbd(struct device *dev,
c1dcad2d
BS
276 struct device_attribute *attr,
277 const char *buf,
278 size_t count)
279{
832fbeae 280 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 281 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
282 int value;
283
c1dcad2d
BS
284 if (kstrtoint(buf, 10, &value))
285 return -EINVAL;
286 if (value < 0 || value > 1)
287 return -EINVAL;
288
289 data_pointer->dragging = value;
94723bfa 290 lenovo_features_set_tpkbd(hdev);
c1dcad2d
BS
291
292 return count;
293}
294
94723bfa 295static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
c1dcad2d
BS
296 struct device_attribute *attr,
297 char *buf)
298{
832fbeae 299 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 300 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
301
302 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
303}
304
94723bfa 305static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
c1dcad2d
BS
306 struct device_attribute *attr,
307 const char *buf,
308 size_t count)
309{
832fbeae 310 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 311 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
312 int value;
313
c1dcad2d
BS
314 if (kstrtoint(buf, 10, &value))
315 return -EINVAL;
316 if (value < 0 || value > 1)
317 return -EINVAL;
318
319 data_pointer->release_to_select = value;
94723bfa 320 lenovo_features_set_tpkbd(hdev);
c1dcad2d
BS
321
322 return count;
323}
324
94723bfa 325static ssize_t attr_select_right_show_tpkbd(struct device *dev,
c1dcad2d
BS
326 struct device_attribute *attr,
327 char *buf)
328{
832fbeae 329 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 330 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
331
332 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
333}
334
94723bfa 335static ssize_t attr_select_right_store_tpkbd(struct device *dev,
c1dcad2d
BS
336 struct device_attribute *attr,
337 const char *buf,
338 size_t count)
339{
832fbeae 340 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 341 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
342 int value;
343
c1dcad2d
BS
344 if (kstrtoint(buf, 10, &value))
345 return -EINVAL;
346 if (value < 0 || value > 1)
347 return -EINVAL;
348
349 data_pointer->select_right = value;
94723bfa 350 lenovo_features_set_tpkbd(hdev);
c1dcad2d
BS
351
352 return count;
353}
354
94723bfa 355static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
c1dcad2d
BS
356 struct device_attribute *attr,
357 char *buf)
358{
832fbeae 359 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 360 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
361
362 return snprintf(buf, PAGE_SIZE, "%u\n",
363 data_pointer->sensitivity);
364}
365
94723bfa 366static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
c1dcad2d
BS
367 struct device_attribute *attr,
368 const char *buf,
369 size_t count)
370{
832fbeae 371 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 372 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
373 int value;
374
c1dcad2d
BS
375 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
376 return -EINVAL;
377
378 data_pointer->sensitivity = value;
94723bfa 379 lenovo_features_set_tpkbd(hdev);
c1dcad2d
BS
380
381 return count;
382}
383
94723bfa 384static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
c1dcad2d
BS
385 struct device_attribute *attr,
386 char *buf)
387{
832fbeae 388 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 389 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d 390
c1dcad2d
BS
391 return snprintf(buf, PAGE_SIZE, "%u\n",
392 data_pointer->press_speed);
393}
394
94723bfa 395static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
c1dcad2d
BS
396 struct device_attribute *attr,
397 const char *buf,
398 size_t count)
399{
832fbeae 400 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 401 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
402 int value;
403
c1dcad2d
BS
404 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
405 return -EINVAL;
406
407 data_pointer->press_speed = value;
94723bfa 408 lenovo_features_set_tpkbd(hdev);
c1dcad2d
BS
409
410 return count;
411}
412
94723bfa 413static struct device_attribute dev_attr_press_to_select_tpkbd =
c1dcad2d 414 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
94723bfa
JL
415 attr_press_to_select_show_tpkbd,
416 attr_press_to_select_store_tpkbd);
c1dcad2d 417
94723bfa 418static struct device_attribute dev_attr_dragging_tpkbd =
c1dcad2d 419 __ATTR(dragging, S_IWUSR | S_IRUGO,
94723bfa
JL
420 attr_dragging_show_tpkbd,
421 attr_dragging_store_tpkbd);
c1dcad2d 422
94723bfa 423static struct device_attribute dev_attr_release_to_select_tpkbd =
c1dcad2d 424 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
94723bfa
JL
425 attr_release_to_select_show_tpkbd,
426 attr_release_to_select_store_tpkbd);
c1dcad2d 427
94723bfa 428static struct device_attribute dev_attr_select_right_tpkbd =
c1dcad2d 429 __ATTR(select_right, S_IWUSR | S_IRUGO,
94723bfa
JL
430 attr_select_right_show_tpkbd,
431 attr_select_right_store_tpkbd);
c1dcad2d 432
94723bfa 433static struct device_attribute dev_attr_sensitivity_tpkbd =
c1dcad2d 434 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
94723bfa
JL
435 attr_sensitivity_show_tpkbd,
436 attr_sensitivity_store_tpkbd);
c1dcad2d 437
94723bfa 438static struct device_attribute dev_attr_press_speed_tpkbd =
c1dcad2d 439 __ATTR(press_speed, S_IWUSR | S_IRUGO,
94723bfa
JL
440 attr_press_speed_show_tpkbd,
441 attr_press_speed_store_tpkbd);
442
443static 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,
c1dcad2d
BS
450 NULL
451};
452
94723bfa
JL
453static const struct attribute_group lenovo_attr_group_tpkbd = {
454 .attrs = lenovo_attributes_tpkbd,
c1dcad2d
BS
455};
456
94723bfa 457static enum led_brightness lenovo_led_brightness_get_tpkbd(
c1dcad2d
BS
458 struct led_classdev *led_cdev)
459{
832fbeae
AL
460 struct device *dev = led_cdev->dev->parent;
461 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 462 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d
BS
463 int led_nr = 0;
464
c1dcad2d
BS
465 if (led_cdev == &data_pointer->led_micmute)
466 led_nr = 1;
467
468 return data_pointer->led_state & (1 << led_nr)
469 ? LED_FULL
470 : LED_OFF;
471}
472
94723bfa 473static void lenovo_led_brightness_set_tpkbd(struct led_classdev *led_cdev,
c1dcad2d
BS
474 enum led_brightness value)
475{
832fbeae
AL
476 struct device *dev = led_cdev->dev->parent;
477 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
94723bfa 478 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d 479 struct hid_report *report;
c1dcad2d
BS
480 int led_nr = 0;
481
c1dcad2d
BS
482 if (led_cdev == &data_pointer->led_micmute)
483 led_nr = 1;
484
485 if (value == LED_OFF)
486 data_pointer->led_state &= ~(1 << led_nr);
487 else
488 data_pointer->led_state |= 1 << led_nr;
489
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;
d8814272 493 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
c1dcad2d
BS
494}
495
94723bfa 496static int lenovo_probe_tpkbd(struct hid_device *hdev)
c1dcad2d
BS
497{
498 struct device *dev = &hdev->dev;
94723bfa 499 struct lenovo_drvdata_tpkbd *data_pointer;
c1dcad2d
BS
500 size_t name_sz = strlen(dev_name(dev)) + 16;
501 char *name_mute, *name_micmute;
01ab35f1 502 int i;
e0a6aad6 503 int ret;
0a9cd0a8 504
6a5b414b
JL
505 /*
506 * Only register extra settings against subdevice where input_mapping
507 * set drvdata to 1, i.e. the trackpoint.
508 */
509 if (!hid_get_drvdata(hdev))
510 return 0;
511
512 hid_set_drvdata(hdev, NULL);
513
0a9cd0a8
KC
514 /* Validate required reports. */
515 for (i = 0; i < 4; i++) {
516 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
517 return -ENODEV;
518 }
519 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
520 return -ENODEV;
c1dcad2d 521
e0a6aad6
JL
522 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
523 if (ret)
524 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
c1dcad2d 525
01ab35f1 526 data_pointer = devm_kzalloc(&hdev->dev,
94723bfa 527 sizeof(struct lenovo_drvdata_tpkbd),
01ab35f1 528 GFP_KERNEL);
c1dcad2d
BS
529 if (data_pointer == NULL) {
530 hid_err(hdev, "Could not allocate memory for driver data\n");
531 return -ENOMEM;
532 }
533
534 // set same default values as windows driver
535 data_pointer->sensitivity = 0xa0;
536 data_pointer->press_speed = 0x38;
537
01ab35f1
BT
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) {
c1dcad2d 541 hid_err(hdev, "Could not allocate memory for led data\n");
01ab35f1 542 return -ENOMEM;
c1dcad2d
BS
543 }
544 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(dev));
c1dcad2d
BS
545 snprintf(name_micmute, name_sz, "%s:amber:micmute", dev_name(dev));
546
547 hid_set_drvdata(hdev, data_pointer);
548
549 data_pointer->led_mute.name = name_mute;
94723bfa
JL
550 data_pointer->led_mute.brightness_get = lenovo_led_brightness_get_tpkbd;
551 data_pointer->led_mute.brightness_set = lenovo_led_brightness_set_tpkbd;
c1dcad2d
BS
552 data_pointer->led_mute.dev = dev;
553 led_classdev_register(dev, &data_pointer->led_mute);
554
555 data_pointer->led_micmute.name = name_micmute;
94723bfa
JL
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;
c1dcad2d
BS
560 data_pointer->led_micmute.dev = dev;
561 led_classdev_register(dev, &data_pointer->led_micmute);
562
94723bfa 563 lenovo_features_set_tpkbd(hdev);
c1dcad2d
BS
564
565 return 0;
c1dcad2d
BS
566}
567
f3d4ff0e
JL
568static int lenovo_probe_cptkbd(struct hid_device *hdev)
569{
570 int ret;
571 struct lenovo_drvdata_cptkbd *cptkbd_data;
572
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");
577 return 0;
578 }
579
580 cptkbd_data = devm_kzalloc(&hdev->dev,
581 sizeof(*cptkbd_data),
582 GFP_KERNEL);
583 if (cptkbd_data == NULL) {
584 hid_err(hdev, "can't alloc keyboard descriptor\n");
585 return -ENOMEM;
586 }
587 hid_set_drvdata(hdev, cptkbd_data);
588
589 /*
590 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
591 * regular keys
592 */
593 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
594 if (ret)
595 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
596
597 /* Turn Fn-Lock on by default */
598 cptkbd_data->fn_lock = true;
599 lenovo_features_set_cptkbd(hdev);
600
601 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
602 if (ret)
603 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
604
605 return 0;
606}
607
94723bfa 608static int lenovo_probe(struct hid_device *hdev,
c1dcad2d
BS
609 const struct hid_device_id *id)
610{
611 int ret;
c1dcad2d
BS
612
613 ret = hid_parse(hdev);
614 if (ret) {
615 hid_err(hdev, "hid_parse failed\n");
0ccdd9e7 616 goto err;
c1dcad2d
BS
617 }
618
619 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
620 if (ret) {
621 hid_err(hdev, "hid_hw_start failed\n");
0ccdd9e7 622 goto err;
c1dcad2d
BS
623 }
624
6a5b414b
JL
625 switch (hdev->product) {
626 case USB_DEVICE_ID_LENOVO_TPKBD:
94723bfa 627 ret = lenovo_probe_tpkbd(hdev);
6a5b414b 628 break;
f3d4ff0e
JL
629 case USB_DEVICE_ID_LENOVO_CUSBKBD:
630 case USB_DEVICE_ID_LENOVO_CBTKBD:
631 ret = lenovo_probe_cptkbd(hdev);
632 break;
6a5b414b
JL
633 default:
634 ret = 0;
635 break;
0ccdd9e7 636 }
6a5b414b
JL
637 if (ret)
638 goto err_hid;
c1dcad2d
BS
639
640 return 0;
0ccdd9e7
BT
641err_hid:
642 hid_hw_stop(hdev);
643err:
c1dcad2d
BS
644 return ret;
645}
646
94723bfa 647static void lenovo_remove_tpkbd(struct hid_device *hdev)
c1dcad2d 648{
94723bfa 649 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
c1dcad2d 650
6a5b414b
JL
651 /*
652 * Only the trackpoint half of the keyboard has drvdata and stuff that
653 * needs unregistering.
654 */
655 if (data_pointer == NULL)
656 return;
657
c1dcad2d 658 sysfs_remove_group(&hdev->dev.kobj,
94723bfa 659 &lenovo_attr_group_tpkbd);
c1dcad2d 660
c1dcad2d
BS
661 led_classdev_unregister(&data_pointer->led_micmute);
662 led_classdev_unregister(&data_pointer->led_mute);
663
664 hid_set_drvdata(hdev, NULL);
c1dcad2d
BS
665}
666
f3d4ff0e
JL
667static void lenovo_remove_cptkbd(struct hid_device *hdev)
668{
669 sysfs_remove_group(&hdev->dev.kobj,
670 &lenovo_attr_group_cptkbd);
671}
672
94723bfa 673static void lenovo_remove(struct hid_device *hdev)
c1dcad2d 674{
6a5b414b
JL
675 switch (hdev->product) {
676 case USB_DEVICE_ID_LENOVO_TPKBD:
94723bfa 677 lenovo_remove_tpkbd(hdev);
6a5b414b 678 break;
f3d4ff0e
JL
679 case USB_DEVICE_ID_LENOVO_CUSBKBD:
680 case USB_DEVICE_ID_LENOVO_CBTKBD:
681 lenovo_remove_cptkbd(hdev);
682 break;
6a5b414b 683 }
c1dcad2d
BS
684
685 hid_hw_stop(hdev);
686}
687
94723bfa 688static const struct hid_device_id lenovo_devices[] = {
c1dcad2d 689 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
f3d4ff0e
JL
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) },
c1dcad2d
BS
692 { }
693};
694
94723bfa 695MODULE_DEVICE_TABLE(hid, lenovo_devices);
c1dcad2d 696
94723bfa
JL
697static struct hid_driver lenovo_driver = {
698 .name = "lenovo",
699 .id_table = lenovo_devices,
6a5b414b 700 .input_mapping = lenovo_input_mapping,
94723bfa
JL
701 .probe = lenovo_probe,
702 .remove = lenovo_remove,
f3d4ff0e 703 .raw_event = lenovo_raw_event,
c1dcad2d 704};
94723bfa 705module_hid_driver(lenovo_driver);
c1dcad2d
BS
706
707MODULE_LICENSE("GPL");
This page took 0.184413 seconds and 5 git commands to generate.