blob: 4c55f4d95798794eed986c638c11b51d2e9d8375 [file] [log] [blame]
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +01001/*
Jamie Lentin6a5b4142014-07-23 23:30:46 +01002 * HID driver for Lenovo:
3 * - ThinkPad USB Keyboard with TrackPoint (tpkbd)
Jamie Lentinf3d4ff02014-07-23 23:30:48 +01004 * - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
5 * - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +01006 *
7 * Copyright (c) 2012 Bernhard Seibold
Jamie Lentinf3d4ff02014-07-23 23:30:48 +01008 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +01009 */
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>
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +010021#include <linux/hid.h>
22#include <linux/input.h>
23#include <linux/leds.h>
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +010024
25#include "hid-ids.h"
26
Jamie Lentin94723bf2014-07-23 23:30:45 +010027struct lenovo_drvdata_tpkbd {
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +010028 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
Jamie Lentinf3d4ff02014-07-23 23:30:48 +010039struct lenovo_drvdata_cptkbd {
40 bool fn_lock;
41};
42
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +010043#define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
44
Jamie Lentin94723bf2014-07-23 23:30:45 +010045static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +010046 struct hid_input *hi, struct hid_field *field,
47 struct hid_usage *usage, unsigned long **bit, int *max)
48{
Benjamin Tissoires0c521832013-09-11 22:12:24 +020049 if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
Jamie Lentin6a5b4142014-07-23 23:30:46 +010050 /* This sub-device contains trackpoint, mark it */
Benjamin Tissoires0c521832013-09-11 22:12:24 +020051 hid_set_drvdata(hdev, (void *)1);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +010052 map_key_clear(KEY_MICMUTE);
53 return 1;
54 }
55 return 0;
56}
57
Jamie Lentinf3d4ff02014-07-23 23:30:48 +010058static 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) {
Jamie Lentinf3d4ff02014-07-23 23:30:48 +010065 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;
Jamie Lentin5556eb12014-11-09 07:54:29 +000087 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
Jamie Lentinf3d4ff02014-07-23 23:30:48 +010088 /* NB: This mapping is invented in raw_event below */
89 map_key_clear(KEY_FILE);
90 return 1;
Jamie Lentin5556eb12014-11-09 07:54:29 +000091 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
92 map_key_clear(KEY_FN_ESC);
93 return 1;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +010094 }
95 }
96
97 return 0;
98}
99
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100100static 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);
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100108 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);
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100112 default:
113 return 0;
114 }
115}
116
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100117#undef map_key_clear
118
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100119/* 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)) {
Jamie Lentin5556eb12014-11-09 07:54:29 +0000209 data[1] = 0x00;
210 data[2] = 0x01;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100211 }
212
213 return 0;
214}
215
Jamie Lentin94723bf2014-07-23 23:30:45 +0100216static int lenovo_features_set_tpkbd(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100217{
218 struct hid_report *report;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100219 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100220
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100221 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
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100231 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100232 return 0;
233}
234
Jamie Lentin94723bf2014-07-23 23:30:45 +0100235static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100236 struct device_attribute *attr,
237 char *buf)
238{
Axel Lin832fbea2012-09-13 14:12:08 +0800239 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100240 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100241
242 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
243}
244
Jamie Lentin94723bf2014-07-23 23:30:45 +0100245static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100246 struct device_attribute *attr,
247 const char *buf,
248 size_t count)
249{
Axel Lin832fbea2012-09-13 14:12:08 +0800250 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100251 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100252 int value;
253
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100254 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;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100260 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100261
262 return count;
263}
264
Jamie Lentin94723bf2014-07-23 23:30:45 +0100265static ssize_t attr_dragging_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100266 struct device_attribute *attr,
267 char *buf)
268{
Axel Lin832fbea2012-09-13 14:12:08 +0800269 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100270 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100271
272 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
273}
274
Jamie Lentin94723bf2014-07-23 23:30:45 +0100275static ssize_t attr_dragging_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100276 struct device_attribute *attr,
277 const char *buf,
278 size_t count)
279{
Axel Lin832fbea2012-09-13 14:12:08 +0800280 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100281 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100282 int value;
283
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100284 if (kstrtoint(buf, 10, &value))
285 return -EINVAL;
286 if (value < 0 || value > 1)
287 return -EINVAL;
288
289 data_pointer->dragging = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100290 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100291
292 return count;
293}
294
Jamie Lentin94723bf2014-07-23 23:30:45 +0100295static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100296 struct device_attribute *attr,
297 char *buf)
298{
Axel Lin832fbea2012-09-13 14:12:08 +0800299 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100300 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100301
302 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
303}
304
Jamie Lentin94723bf2014-07-23 23:30:45 +0100305static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100306 struct device_attribute *attr,
307 const char *buf,
308 size_t count)
309{
Axel Lin832fbea2012-09-13 14:12:08 +0800310 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100311 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100312 int value;
313
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100314 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;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100320 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100321
322 return count;
323}
324
Jamie Lentin94723bf2014-07-23 23:30:45 +0100325static ssize_t attr_select_right_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100326 struct device_attribute *attr,
327 char *buf)
328{
Axel Lin832fbea2012-09-13 14:12:08 +0800329 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100330 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100331
332 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
333}
334
Jamie Lentin94723bf2014-07-23 23:30:45 +0100335static ssize_t attr_select_right_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100336 struct device_attribute *attr,
337 const char *buf,
338 size_t count)
339{
Axel Lin832fbea2012-09-13 14:12:08 +0800340 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100341 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100342 int value;
343
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100344 if (kstrtoint(buf, 10, &value))
345 return -EINVAL;
346 if (value < 0 || value > 1)
347 return -EINVAL;
348
349 data_pointer->select_right = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100350 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100351
352 return count;
353}
354
Jamie Lentin94723bf2014-07-23 23:30:45 +0100355static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100356 struct device_attribute *attr,
357 char *buf)
358{
Axel Lin832fbea2012-09-13 14:12:08 +0800359 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100360 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100361
362 return snprintf(buf, PAGE_SIZE, "%u\n",
363 data_pointer->sensitivity);
364}
365
Jamie Lentin94723bf2014-07-23 23:30:45 +0100366static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100367 struct device_attribute *attr,
368 const char *buf,
369 size_t count)
370{
Axel Lin832fbea2012-09-13 14:12:08 +0800371 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100372 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100373 int value;
374
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100375 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
376 return -EINVAL;
377
378 data_pointer->sensitivity = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100379 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100380
381 return count;
382}
383
Jamie Lentin94723bf2014-07-23 23:30:45 +0100384static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100385 struct device_attribute *attr,
386 char *buf)
387{
Axel Lin832fbea2012-09-13 14:12:08 +0800388 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100389 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100390
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100391 return snprintf(buf, PAGE_SIZE, "%u\n",
392 data_pointer->press_speed);
393}
394
Jamie Lentin94723bf2014-07-23 23:30:45 +0100395static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100396 struct device_attribute *attr,
397 const char *buf,
398 size_t count)
399{
Axel Lin832fbea2012-09-13 14:12:08 +0800400 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100401 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100402 int value;
403
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100404 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
405 return -EINVAL;
406
407 data_pointer->press_speed = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100408 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100409
410 return count;
411}
412
Jamie Lentin94723bf2014-07-23 23:30:45 +0100413static struct device_attribute dev_attr_press_to_select_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100414 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100415 attr_press_to_select_show_tpkbd,
416 attr_press_to_select_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100417
Jamie Lentin94723bf2014-07-23 23:30:45 +0100418static struct device_attribute dev_attr_dragging_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100419 __ATTR(dragging, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100420 attr_dragging_show_tpkbd,
421 attr_dragging_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100422
Jamie Lentin94723bf2014-07-23 23:30:45 +0100423static struct device_attribute dev_attr_release_to_select_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100424 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100425 attr_release_to_select_show_tpkbd,
426 attr_release_to_select_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100427
Jamie Lentin94723bf2014-07-23 23:30:45 +0100428static struct device_attribute dev_attr_select_right_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100429 __ATTR(select_right, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100430 attr_select_right_show_tpkbd,
431 attr_select_right_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100432
Jamie Lentin94723bf2014-07-23 23:30:45 +0100433static struct device_attribute dev_attr_sensitivity_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100434 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100435 attr_sensitivity_show_tpkbd,
436 attr_sensitivity_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100437
Jamie Lentin94723bf2014-07-23 23:30:45 +0100438static struct device_attribute dev_attr_press_speed_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100439 __ATTR(press_speed, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100440 attr_press_speed_show_tpkbd,
441 attr_press_speed_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100442
Jamie Lentin94723bf2014-07-23 23:30:45 +0100443static 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,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100450 NULL
451};
452
Jamie Lentin94723bf2014-07-23 23:30:45 +0100453static const struct attribute_group lenovo_attr_group_tpkbd = {
454 .attrs = lenovo_attributes_tpkbd,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100455};
456
Jamie Lentin94723bf2014-07-23 23:30:45 +0100457static enum led_brightness lenovo_led_brightness_get_tpkbd(
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100458 struct led_classdev *led_cdev)
459{
Axel Lin832fbea2012-09-13 14:12:08 +0800460 struct device *dev = led_cdev->dev->parent;
461 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100462 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100463 int led_nr = 0;
464
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100465 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
Jamie Lentin94723bf2014-07-23 23:30:45 +0100473static void lenovo_led_brightness_set_tpkbd(struct led_classdev *led_cdev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100474 enum led_brightness value)
475{
Axel Lin832fbea2012-09-13 14:12:08 +0800476 struct device *dev = led_cdev->dev->parent;
477 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100478 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100479 struct hid_report *report;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100480 int led_nr = 0;
481
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100482 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;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100493 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100494}
495
Jamie Lentin94723bf2014-07-23 23:30:45 +0100496static int lenovo_probe_tpkbd(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100497{
498 struct device *dev = &hdev->dev;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100499 struct lenovo_drvdata_tpkbd *data_pointer;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100500 size_t name_sz = strlen(dev_name(dev)) + 16;
501 char *name_mute, *name_micmute;
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200502 int i;
Jamie Lentine0a6aad2014-07-26 15:42:27 +0100503 int ret;
Kees Cook0a9cd0a2013-09-11 21:56:55 +0200504
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100505 /*
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
Kees Cook0a9cd0a2013-09-11 21:56:55 +0200514 /* 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;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100521
Jamie Lentine0a6aad2014-07-26 15:42:27 +0100522 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);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100525
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200526 data_pointer = devm_kzalloc(&hdev->dev,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100527 sizeof(struct lenovo_drvdata_tpkbd),
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200528 GFP_KERNEL);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100529 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
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200538 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) {
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100541 hid_err(hdev, "Could not allocate memory for led data\n");
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200542 return -ENOMEM;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100543 }
544 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(dev));
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100545 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;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100550 data_pointer->led_mute.brightness_get = lenovo_led_brightness_get_tpkbd;
551 data_pointer->led_mute.brightness_set = lenovo_led_brightness_set_tpkbd;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100552 data_pointer->led_mute.dev = dev;
553 led_classdev_register(dev, &data_pointer->led_mute);
554
555 data_pointer->led_micmute.name = name_micmute;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100556 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;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100560 data_pointer->led_micmute.dev = dev;
561 led_classdev_register(dev, &data_pointer->led_micmute);
562
Jamie Lentin94723bf2014-07-23 23:30:45 +0100563 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100564
565 return 0;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100566}
567
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100568static 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
Jamie Lentin94723bf2014-07-23 23:30:45 +0100608static int lenovo_probe(struct hid_device *hdev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100609 const struct hid_device_id *id)
610{
611 int ret;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100612
613 ret = hid_parse(hdev);
614 if (ret) {
615 hid_err(hdev, "hid_parse failed\n");
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200616 goto err;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100617 }
618
619 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
620 if (ret) {
621 hid_err(hdev, "hid_hw_start failed\n");
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200622 goto err;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100623 }
624
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100625 switch (hdev->product) {
626 case USB_DEVICE_ID_LENOVO_TPKBD:
Jamie Lentin94723bf2014-07-23 23:30:45 +0100627 ret = lenovo_probe_tpkbd(hdev);
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100628 break;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100629 case USB_DEVICE_ID_LENOVO_CUSBKBD:
630 case USB_DEVICE_ID_LENOVO_CBTKBD:
631 ret = lenovo_probe_cptkbd(hdev);
632 break;
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100633 default:
634 ret = 0;
635 break;
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200636 }
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100637 if (ret)
638 goto err_hid;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100639
640 return 0;
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200641err_hid:
642 hid_hw_stop(hdev);
643err:
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100644 return ret;
645}
646
Jamie Lentin94723bf2014-07-23 23:30:45 +0100647static void lenovo_remove_tpkbd(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100648{
Jamie Lentin94723bf2014-07-23 23:30:45 +0100649 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100650
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100651 /*
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
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100658 sysfs_remove_group(&hdev->dev.kobj,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100659 &lenovo_attr_group_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100660
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100661 led_classdev_unregister(&data_pointer->led_micmute);
662 led_classdev_unregister(&data_pointer->led_mute);
663
664 hid_set_drvdata(hdev, NULL);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100665}
666
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100667static void lenovo_remove_cptkbd(struct hid_device *hdev)
668{
669 sysfs_remove_group(&hdev->dev.kobj,
670 &lenovo_attr_group_cptkbd);
671}
672
Jamie Lentin94723bf2014-07-23 23:30:45 +0100673static void lenovo_remove(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100674{
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100675 switch (hdev->product) {
676 case USB_DEVICE_ID_LENOVO_TPKBD:
Jamie Lentin94723bf2014-07-23 23:30:45 +0100677 lenovo_remove_tpkbd(hdev);
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100678 break;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100679 case USB_DEVICE_ID_LENOVO_CUSBKBD:
680 case USB_DEVICE_ID_LENOVO_CBTKBD:
681 lenovo_remove_cptkbd(hdev);
682 break;
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100683 }
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100684
685 hid_hw_stop(hdev);
686}
687
Jamie Lentin94723bf2014-07-23 23:30:45 +0100688static const struct hid_device_id lenovo_devices[] = {
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100689 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100690 { 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) },
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100692 { }
693};
694
Jamie Lentin94723bf2014-07-23 23:30:45 +0100695MODULE_DEVICE_TABLE(hid, lenovo_devices);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100696
Jamie Lentin94723bf2014-07-23 23:30:45 +0100697static struct hid_driver lenovo_driver = {
698 .name = "lenovo",
699 .id_table = lenovo_devices,
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100700 .input_mapping = lenovo_input_mapping,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100701 .probe = lenovo_probe,
702 .remove = lenovo_remove,
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100703 .raw_event = lenovo_raw_event,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100704};
Jamie Lentin94723bf2014-07-23 23:30:45 +0100705module_hid_driver(lenovo_driver);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100706
707MODULE_LICENSE("GPL");