blob: 4716f1e59ecac3c953de5b4287223da141e2b749 [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) {
65 set_bit(EV_REP, hi->input->evbit);
66 switch (usage->hid & HID_USAGE) {
67 case 0x00f1: /* Fn-F4: Mic mute */
68 map_key_clear(KEY_MICMUTE);
69 return 1;
70 case 0x00f2: /* Fn-F5: Brightness down */
71 map_key_clear(KEY_BRIGHTNESSDOWN);
72 return 1;
73 case 0x00f3: /* Fn-F6: Brightness up */
74 map_key_clear(KEY_BRIGHTNESSUP);
75 return 1;
76 case 0x00f4: /* Fn-F7: External display (projector) */
77 map_key_clear(KEY_SWITCHVIDEOMODE);
78 return 1;
79 case 0x00f5: /* Fn-F8: Wireless */
80 map_key_clear(KEY_WLAN);
81 return 1;
82 case 0x00f6: /* Fn-F9: Control panel */
83 map_key_clear(KEY_CONFIG);
84 return 1;
85 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
86 map_key_clear(KEY_SCALE);
87 return 1;
Jamie Lentin5556eb12014-11-09 07:54:29 +000088 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
Jamie Lentinf3d4ff02014-07-23 23:30:48 +010089 /* NB: This mapping is invented in raw_event below */
90 map_key_clear(KEY_FILE);
91 return 1;
Jamie Lentin5556eb12014-11-09 07:54:29 +000092 case 0x00fa: /* Fn-Esc: Fn-lock toggle */
93 map_key_clear(KEY_FN_ESC);
94 return 1;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +010095 }
96 }
97
98 return 0;
99}
100
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100101static int lenovo_input_mapping(struct hid_device *hdev,
102 struct hid_input *hi, struct hid_field *field,
103 struct hid_usage *usage, unsigned long **bit, int *max)
104{
105 switch (hdev->product) {
106 case USB_DEVICE_ID_LENOVO_TPKBD:
107 return lenovo_input_mapping_tpkbd(hdev, hi, field,
108 usage, bit, max);
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100109 case USB_DEVICE_ID_LENOVO_CUSBKBD:
110 case USB_DEVICE_ID_LENOVO_CBTKBD:
111 return lenovo_input_mapping_cptkbd(hdev, hi, field,
112 usage, bit, max);
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100113 default:
114 return 0;
115 }
116}
117
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100118#undef map_key_clear
119
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100120/* Send a config command to the keyboard */
121static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
122 unsigned char byte2, unsigned char byte3)
123{
124 int ret;
125 unsigned char buf[] = {0x18, byte2, byte3};
126
127 switch (hdev->product) {
128 case USB_DEVICE_ID_LENOVO_CUSBKBD:
129 ret = hid_hw_raw_request(hdev, 0x13, buf, sizeof(buf),
130 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
131 break;
132 case USB_DEVICE_ID_LENOVO_CBTKBD:
133 ret = hid_hw_output_report(hdev, buf, sizeof(buf));
134 break;
135 default:
136 ret = -EINVAL;
137 break;
138 }
139
140 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
141}
142
143static void lenovo_features_set_cptkbd(struct hid_device *hdev)
144{
145 int ret;
146 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
147
148 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
149 if (ret)
150 hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
151}
152
153static ssize_t attr_fn_lock_show_cptkbd(struct device *dev,
154 struct device_attribute *attr,
155 char *buf)
156{
157 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
158 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
159
160 return snprintf(buf, PAGE_SIZE, "%u\n", cptkbd_data->fn_lock);
161}
162
163static ssize_t attr_fn_lock_store_cptkbd(struct device *dev,
164 struct device_attribute *attr,
165 const char *buf,
166 size_t count)
167{
168 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
169 struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
170 int value;
171
172 if (kstrtoint(buf, 10, &value))
173 return -EINVAL;
174 if (value < 0 || value > 1)
175 return -EINVAL;
176
177 cptkbd_data->fn_lock = !!value;
178 lenovo_features_set_cptkbd(hdev);
179
180 return count;
181}
182
183static struct device_attribute dev_attr_fn_lock_cptkbd =
184 __ATTR(fn_lock, S_IWUSR | S_IRUGO,
185 attr_fn_lock_show_cptkbd,
186 attr_fn_lock_store_cptkbd);
187
188static struct attribute *lenovo_attributes_cptkbd[] = {
189 &dev_attr_fn_lock_cptkbd.attr,
190 NULL
191};
192
193static const struct attribute_group lenovo_attr_group_cptkbd = {
194 .attrs = lenovo_attributes_cptkbd,
195};
196
197static int lenovo_raw_event(struct hid_device *hdev,
198 struct hid_report *report, u8 *data, int size)
199{
200 /*
201 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
202 * its own key is outside the usage page range. Remove extra
203 * keypresses and remap to inside usage page.
204 */
205 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
206 && size == 3
207 && data[0] == 0x15
208 && data[1] == 0x94
209 && data[2] == 0x01)) {
Jamie Lentin5556eb12014-11-09 07:54:29 +0000210 data[1] = 0x00;
211 data[2] = 0x01;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100212 }
213
214 return 0;
215}
216
Jamie Lentin94723bf2014-07-23 23:30:45 +0100217static int lenovo_features_set_tpkbd(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100218{
219 struct hid_report *report;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100220 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100221
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100222 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
223
224 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
225 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
226 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
227 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
228 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
229 report->field[2]->value[0] = data_pointer->sensitivity;
230 report->field[3]->value[0] = data_pointer->press_speed;
231
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100232 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100233 return 0;
234}
235
Jamie Lentin94723bf2014-07-23 23:30:45 +0100236static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100237 struct device_attribute *attr,
238 char *buf)
239{
Axel Lin832fbea2012-09-13 14:12:08 +0800240 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100241 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100242
243 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
244}
245
Jamie Lentin94723bf2014-07-23 23:30:45 +0100246static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100247 struct device_attribute *attr,
248 const char *buf,
249 size_t count)
250{
Axel Lin832fbea2012-09-13 14:12:08 +0800251 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100252 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100253 int value;
254
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100255 if (kstrtoint(buf, 10, &value))
256 return -EINVAL;
257 if (value < 0 || value > 1)
258 return -EINVAL;
259
260 data_pointer->press_to_select = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100261 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100262
263 return count;
264}
265
Jamie Lentin94723bf2014-07-23 23:30:45 +0100266static ssize_t attr_dragging_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100267 struct device_attribute *attr,
268 char *buf)
269{
Axel Lin832fbea2012-09-13 14:12:08 +0800270 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100271 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100272
273 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
274}
275
Jamie Lentin94723bf2014-07-23 23:30:45 +0100276static ssize_t attr_dragging_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100277 struct device_attribute *attr,
278 const char *buf,
279 size_t count)
280{
Axel Lin832fbea2012-09-13 14:12:08 +0800281 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100282 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100283 int value;
284
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100285 if (kstrtoint(buf, 10, &value))
286 return -EINVAL;
287 if (value < 0 || value > 1)
288 return -EINVAL;
289
290 data_pointer->dragging = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100291 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100292
293 return count;
294}
295
Jamie Lentin94723bf2014-07-23 23:30:45 +0100296static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100297 struct device_attribute *attr,
298 char *buf)
299{
Axel Lin832fbea2012-09-13 14:12:08 +0800300 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100301 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100302
303 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
304}
305
Jamie Lentin94723bf2014-07-23 23:30:45 +0100306static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100307 struct device_attribute *attr,
308 const char *buf,
309 size_t count)
310{
Axel Lin832fbea2012-09-13 14:12:08 +0800311 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100312 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100313 int value;
314
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100315 if (kstrtoint(buf, 10, &value))
316 return -EINVAL;
317 if (value < 0 || value > 1)
318 return -EINVAL;
319
320 data_pointer->release_to_select = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100321 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100322
323 return count;
324}
325
Jamie Lentin94723bf2014-07-23 23:30:45 +0100326static ssize_t attr_select_right_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100327 struct device_attribute *attr,
328 char *buf)
329{
Axel Lin832fbea2012-09-13 14:12:08 +0800330 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100331 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100332
333 return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
334}
335
Jamie Lentin94723bf2014-07-23 23:30:45 +0100336static ssize_t attr_select_right_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100337 struct device_attribute *attr,
338 const char *buf,
339 size_t count)
340{
Axel Lin832fbea2012-09-13 14:12:08 +0800341 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100342 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100343 int value;
344
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100345 if (kstrtoint(buf, 10, &value))
346 return -EINVAL;
347 if (value < 0 || value > 1)
348 return -EINVAL;
349
350 data_pointer->select_right = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100351 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100352
353 return count;
354}
355
Jamie Lentin94723bf2014-07-23 23:30:45 +0100356static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100357 struct device_attribute *attr,
358 char *buf)
359{
Axel Lin832fbea2012-09-13 14:12:08 +0800360 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100361 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100362
363 return snprintf(buf, PAGE_SIZE, "%u\n",
364 data_pointer->sensitivity);
365}
366
Jamie Lentin94723bf2014-07-23 23:30:45 +0100367static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100368 struct device_attribute *attr,
369 const char *buf,
370 size_t count)
371{
Axel Lin832fbea2012-09-13 14:12:08 +0800372 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100373 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100374 int value;
375
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100376 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
377 return -EINVAL;
378
379 data_pointer->sensitivity = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100380 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100381
382 return count;
383}
384
Jamie Lentin94723bf2014-07-23 23:30:45 +0100385static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100386 struct device_attribute *attr,
387 char *buf)
388{
Axel Lin832fbea2012-09-13 14:12:08 +0800389 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100390 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100391
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100392 return snprintf(buf, PAGE_SIZE, "%u\n",
393 data_pointer->press_speed);
394}
395
Jamie Lentin94723bf2014-07-23 23:30:45 +0100396static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100397 struct device_attribute *attr,
398 const char *buf,
399 size_t count)
400{
Axel Lin832fbea2012-09-13 14:12:08 +0800401 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100402 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100403 int value;
404
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100405 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
406 return -EINVAL;
407
408 data_pointer->press_speed = value;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100409 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100410
411 return count;
412}
413
Jamie Lentin94723bf2014-07-23 23:30:45 +0100414static struct device_attribute dev_attr_press_to_select_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100415 __ATTR(press_to_select, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100416 attr_press_to_select_show_tpkbd,
417 attr_press_to_select_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100418
Jamie Lentin94723bf2014-07-23 23:30:45 +0100419static struct device_attribute dev_attr_dragging_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100420 __ATTR(dragging, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100421 attr_dragging_show_tpkbd,
422 attr_dragging_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100423
Jamie Lentin94723bf2014-07-23 23:30:45 +0100424static struct device_attribute dev_attr_release_to_select_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100425 __ATTR(release_to_select, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100426 attr_release_to_select_show_tpkbd,
427 attr_release_to_select_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100428
Jamie Lentin94723bf2014-07-23 23:30:45 +0100429static struct device_attribute dev_attr_select_right_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100430 __ATTR(select_right, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100431 attr_select_right_show_tpkbd,
432 attr_select_right_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100433
Jamie Lentin94723bf2014-07-23 23:30:45 +0100434static struct device_attribute dev_attr_sensitivity_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100435 __ATTR(sensitivity, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100436 attr_sensitivity_show_tpkbd,
437 attr_sensitivity_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100438
Jamie Lentin94723bf2014-07-23 23:30:45 +0100439static struct device_attribute dev_attr_press_speed_tpkbd =
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100440 __ATTR(press_speed, S_IWUSR | S_IRUGO,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100441 attr_press_speed_show_tpkbd,
442 attr_press_speed_store_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100443
Jamie Lentin94723bf2014-07-23 23:30:45 +0100444static struct attribute *lenovo_attributes_tpkbd[] = {
445 &dev_attr_press_to_select_tpkbd.attr,
446 &dev_attr_dragging_tpkbd.attr,
447 &dev_attr_release_to_select_tpkbd.attr,
448 &dev_attr_select_right_tpkbd.attr,
449 &dev_attr_sensitivity_tpkbd.attr,
450 &dev_attr_press_speed_tpkbd.attr,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100451 NULL
452};
453
Jamie Lentin94723bf2014-07-23 23:30:45 +0100454static const struct attribute_group lenovo_attr_group_tpkbd = {
455 .attrs = lenovo_attributes_tpkbd,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100456};
457
Jamie Lentin94723bf2014-07-23 23:30:45 +0100458static enum led_brightness lenovo_led_brightness_get_tpkbd(
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100459 struct led_classdev *led_cdev)
460{
Axel Lin832fbea2012-09-13 14:12:08 +0800461 struct device *dev = led_cdev->dev->parent;
462 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100463 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100464 int led_nr = 0;
465
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100466 if (led_cdev == &data_pointer->led_micmute)
467 led_nr = 1;
468
469 return data_pointer->led_state & (1 << led_nr)
470 ? LED_FULL
471 : LED_OFF;
472}
473
Jamie Lentin94723bf2014-07-23 23:30:45 +0100474static void lenovo_led_brightness_set_tpkbd(struct led_classdev *led_cdev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100475 enum led_brightness value)
476{
Axel Lin832fbea2012-09-13 14:12:08 +0800477 struct device *dev = led_cdev->dev->parent;
478 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Jamie Lentin94723bf2014-07-23 23:30:45 +0100479 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100480 struct hid_report *report;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100481 int led_nr = 0;
482
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100483 if (led_cdev == &data_pointer->led_micmute)
484 led_nr = 1;
485
486 if (value == LED_OFF)
487 data_pointer->led_state &= ~(1 << led_nr);
488 else
489 data_pointer->led_state |= 1 << led_nr;
490
491 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
492 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
493 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100494 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100495}
496
Jamie Lentin94723bf2014-07-23 23:30:45 +0100497static int lenovo_probe_tpkbd(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100498{
499 struct device *dev = &hdev->dev;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100500 struct lenovo_drvdata_tpkbd *data_pointer;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100501 size_t name_sz = strlen(dev_name(dev)) + 16;
502 char *name_mute, *name_micmute;
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200503 int i;
Jamie Lentine0a6aad2014-07-26 15:42:27 +0100504 int ret;
Kees Cook0a9cd0a2013-09-11 21:56:55 +0200505
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100506 /*
507 * Only register extra settings against subdevice where input_mapping
508 * set drvdata to 1, i.e. the trackpoint.
509 */
510 if (!hid_get_drvdata(hdev))
511 return 0;
512
513 hid_set_drvdata(hdev, NULL);
514
Kees Cook0a9cd0a2013-09-11 21:56:55 +0200515 /* Validate required reports. */
516 for (i = 0; i < 4; i++) {
517 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
518 return -ENODEV;
519 }
520 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
521 return -ENODEV;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100522
Jamie Lentine0a6aad2014-07-26 15:42:27 +0100523 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
524 if (ret)
525 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100526
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200527 data_pointer = devm_kzalloc(&hdev->dev,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100528 sizeof(struct lenovo_drvdata_tpkbd),
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200529 GFP_KERNEL);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100530 if (data_pointer == NULL) {
531 hid_err(hdev, "Could not allocate memory for driver data\n");
532 return -ENOMEM;
533 }
534
535 // set same default values as windows driver
536 data_pointer->sensitivity = 0xa0;
537 data_pointer->press_speed = 0x38;
538
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200539 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
540 name_micmute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
541 if (name_mute == NULL || name_micmute == NULL) {
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100542 hid_err(hdev, "Could not allocate memory for led data\n");
Benjamin Tissoires01ab35f2013-09-11 22:12:23 +0200543 return -ENOMEM;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100544 }
545 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(dev));
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100546 snprintf(name_micmute, name_sz, "%s:amber:micmute", dev_name(dev));
547
548 hid_set_drvdata(hdev, data_pointer);
549
550 data_pointer->led_mute.name = name_mute;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100551 data_pointer->led_mute.brightness_get = lenovo_led_brightness_get_tpkbd;
552 data_pointer->led_mute.brightness_set = lenovo_led_brightness_set_tpkbd;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100553 data_pointer->led_mute.dev = dev;
554 led_classdev_register(dev, &data_pointer->led_mute);
555
556 data_pointer->led_micmute.name = name_micmute;
Jamie Lentin94723bf2014-07-23 23:30:45 +0100557 data_pointer->led_micmute.brightness_get =
558 lenovo_led_brightness_get_tpkbd;
559 data_pointer->led_micmute.brightness_set =
560 lenovo_led_brightness_set_tpkbd;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100561 data_pointer->led_micmute.dev = dev;
562 led_classdev_register(dev, &data_pointer->led_micmute);
563
Jamie Lentin94723bf2014-07-23 23:30:45 +0100564 lenovo_features_set_tpkbd(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100565
566 return 0;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100567}
568
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100569static int lenovo_probe_cptkbd(struct hid_device *hdev)
570{
571 int ret;
572 struct lenovo_drvdata_cptkbd *cptkbd_data;
573
574 /* All the custom action happens on the USBMOUSE device for USB */
575 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
576 && hdev->type != HID_TYPE_USBMOUSE) {
577 hid_dbg(hdev, "Ignoring keyboard half of device\n");
578 return 0;
579 }
580
581 cptkbd_data = devm_kzalloc(&hdev->dev,
582 sizeof(*cptkbd_data),
583 GFP_KERNEL);
584 if (cptkbd_data == NULL) {
585 hid_err(hdev, "can't alloc keyboard descriptor\n");
586 return -ENOMEM;
587 }
588 hid_set_drvdata(hdev, cptkbd_data);
589
590 /*
591 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
592 * regular keys
593 */
594 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
595 if (ret)
596 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
597
598 /* Turn Fn-Lock on by default */
599 cptkbd_data->fn_lock = true;
600 lenovo_features_set_cptkbd(hdev);
601
602 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
603 if (ret)
604 hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
605
606 return 0;
607}
608
Jamie Lentin94723bf2014-07-23 23:30:45 +0100609static int lenovo_probe(struct hid_device *hdev,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100610 const struct hid_device_id *id)
611{
612 int ret;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100613
614 ret = hid_parse(hdev);
615 if (ret) {
616 hid_err(hdev, "hid_parse failed\n");
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200617 goto err;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100618 }
619
620 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
621 if (ret) {
622 hid_err(hdev, "hid_hw_start failed\n");
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200623 goto err;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100624 }
625
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100626 switch (hdev->product) {
627 case USB_DEVICE_ID_LENOVO_TPKBD:
Jamie Lentin94723bf2014-07-23 23:30:45 +0100628 ret = lenovo_probe_tpkbd(hdev);
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100629 break;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100630 case USB_DEVICE_ID_LENOVO_CUSBKBD:
631 case USB_DEVICE_ID_LENOVO_CBTKBD:
632 ret = lenovo_probe_cptkbd(hdev);
633 break;
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100634 default:
635 ret = 0;
636 break;
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200637 }
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100638 if (ret)
639 goto err_hid;
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100640
641 return 0;
Benjamin Tissoires0ccdd9e2013-09-11 21:56:59 +0200642err_hid:
643 hid_hw_stop(hdev);
644err:
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100645 return ret;
646}
647
Jamie Lentin94723bf2014-07-23 23:30:45 +0100648static void lenovo_remove_tpkbd(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100649{
Jamie Lentin94723bf2014-07-23 23:30:45 +0100650 struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100651
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100652 /*
653 * Only the trackpoint half of the keyboard has drvdata and stuff that
654 * needs unregistering.
655 */
656 if (data_pointer == NULL)
657 return;
658
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100659 sysfs_remove_group(&hdev->dev.kobj,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100660 &lenovo_attr_group_tpkbd);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100661
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100662 led_classdev_unregister(&data_pointer->led_micmute);
663 led_classdev_unregister(&data_pointer->led_mute);
664
665 hid_set_drvdata(hdev, NULL);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100666}
667
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100668static void lenovo_remove_cptkbd(struct hid_device *hdev)
669{
670 sysfs_remove_group(&hdev->dev.kobj,
671 &lenovo_attr_group_cptkbd);
672}
673
Jamie Lentin94723bf2014-07-23 23:30:45 +0100674static void lenovo_remove(struct hid_device *hdev)
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100675{
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100676 switch (hdev->product) {
677 case USB_DEVICE_ID_LENOVO_TPKBD:
Jamie Lentin94723bf2014-07-23 23:30:45 +0100678 lenovo_remove_tpkbd(hdev);
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100679 break;
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100680 case USB_DEVICE_ID_LENOVO_CUSBKBD:
681 case USB_DEVICE_ID_LENOVO_CBTKBD:
682 lenovo_remove_cptkbd(hdev);
683 break;
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100684 }
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100685
686 hid_hw_stop(hdev);
687}
688
Jamie Lentin94723bf2014-07-23 23:30:45 +0100689static const struct hid_device_id lenovo_devices[] = {
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100690 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100691 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
692 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100693 { }
694};
695
Jamie Lentin94723bf2014-07-23 23:30:45 +0100696MODULE_DEVICE_TABLE(hid, lenovo_devices);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100697
Jamie Lentin94723bf2014-07-23 23:30:45 +0100698static struct hid_driver lenovo_driver = {
699 .name = "lenovo",
700 .id_table = lenovo_devices,
Jamie Lentin6a5b4142014-07-23 23:30:46 +0100701 .input_mapping = lenovo_input_mapping,
Jamie Lentin94723bf2014-07-23 23:30:45 +0100702 .probe = lenovo_probe,
703 .remove = lenovo_remove,
Jamie Lentinf3d4ff02014-07-23 23:30:48 +0100704 .raw_event = lenovo_raw_event,
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100705};
Jamie Lentin94723bf2014-07-23 23:30:45 +0100706module_hid_driver(lenovo_driver);
Bernhard Seiboldc1dcad2d2012-02-15 13:40:43 +0100707
708MODULE_LICENSE("GPL");