]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/hid/hid-elan.c
HID: elan: Add USB-id for HP x2 10-n000nd touchpad
[thirdparty/kernel/stable.git] / drivers / hid / hid-elan.c
1 /*
2 * HID Driver for ELAN Touchpad
3 *
4 * Currently only supports touchpad found on HP Pavilion X2 10
5 *
6 * Copyright (c) 2016 Alexandrov Stanislav <neko@nya.ai>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 */
13
14 #include <linux/hid.h>
15 #include <linux/input/mt.h>
16 #include <linux/leds.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19
20 #include "hid-ids.h"
21
22 #define ELAN_SINGLE_FINGER 0x81
23 #define ELAN_MT_FIRST_FINGER 0x82
24 #define ELAN_MT_SECOND_FINGER 0x83
25 #define ELAN_INPUT_REPORT_SIZE 8
26 #define ELAN_MAX_FINGERS 5
27 #define ELAN_MAX_PRESSURE 255
28 #define ELAN_TP_USB_INTF 1
29
30 #define ELAN_FEATURE_REPORT 0x0d
31 #define ELAN_FEATURE_SIZE 5
32 #define ELAN_PARAM_MAX_X 6
33 #define ELAN_PARAM_MAX_Y 7
34 #define ELAN_PARAM_RES 8
35
36 #define ELAN_MUTE_LED_REPORT 0xBC
37 #define ELAN_LED_REPORT_SIZE 8
38
39 #define ELAN_HAS_LED BIT(0)
40
41 struct elan_drvdata {
42 struct input_dev *input;
43 u8 prev_report[ELAN_INPUT_REPORT_SIZE];
44 struct led_classdev mute_led;
45 u8 mute_led_state;
46 u16 max_x;
47 u16 max_y;
48 u16 res_x;
49 u16 res_y;
50 };
51
52 static int is_not_elan_touchpad(struct hid_device *hdev)
53 {
54 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
55
56 return (intf->altsetting->desc.bInterfaceNumber != ELAN_TP_USB_INTF);
57 }
58
59 static int elan_input_mapping(struct hid_device *hdev, struct hid_input *hi,
60 struct hid_field *field, struct hid_usage *usage,
61 unsigned long **bit, int *max)
62 {
63 if (is_not_elan_touchpad(hdev))
64 return 0;
65
66 if (field->report->id == ELAN_SINGLE_FINGER ||
67 field->report->id == ELAN_MT_FIRST_FINGER ||
68 field->report->id == ELAN_MT_SECOND_FINGER)
69 return -1;
70
71 return 0;
72 }
73
74 static int elan_get_device_param(struct hid_device *hdev,
75 unsigned char *dmabuf, unsigned char param)
76 {
77 int ret;
78
79 dmabuf[0] = ELAN_FEATURE_REPORT;
80 dmabuf[1] = 0x05;
81 dmabuf[2] = 0x03;
82 dmabuf[3] = param;
83 dmabuf[4] = 0x01;
84
85 ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
86 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
87 HID_REQ_SET_REPORT);
88 if (ret != ELAN_FEATURE_SIZE) {
89 hid_err(hdev, "Set report error for parm %d: %d\n", param, ret);
90 return ret;
91 }
92
93 ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
94 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
95 HID_REQ_GET_REPORT);
96 if (ret != ELAN_FEATURE_SIZE) {
97 hid_err(hdev, "Get report error for parm %d: %d\n", param, ret);
98 return ret;
99 }
100
101 return 0;
102 }
103
104 static unsigned int elan_convert_res(char val)
105 {
106 /*
107 * (value from firmware) * 10 + 790 = dpi
108 * dpi * 10 / 254 = dots/mm
109 */
110 return (val * 10 + 790) * 10 / 254;
111 }
112
113 static int elan_get_device_params(struct hid_device *hdev)
114 {
115 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
116 unsigned char *dmabuf;
117 int ret;
118
119 dmabuf = kmalloc(ELAN_FEATURE_SIZE, GFP_KERNEL);
120 if (!dmabuf)
121 return -ENOMEM;
122
123 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_X);
124 if (ret)
125 goto err;
126
127 drvdata->max_x = (dmabuf[4] << 8) | dmabuf[3];
128
129 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_Y);
130 if (ret)
131 goto err;
132
133 drvdata->max_y = (dmabuf[4] << 8) | dmabuf[3];
134
135 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_RES);
136 if (ret)
137 goto err;
138
139 drvdata->res_x = elan_convert_res(dmabuf[3]);
140 drvdata->res_y = elan_convert_res(dmabuf[4]);
141
142 err:
143 kfree(dmabuf);
144 return ret;
145 }
146
147 static int elan_input_configured(struct hid_device *hdev, struct hid_input *hi)
148 {
149 int ret;
150 struct input_dev *input;
151 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
152
153 if (is_not_elan_touchpad(hdev))
154 return 0;
155
156 ret = elan_get_device_params(hdev);
157 if (ret)
158 return ret;
159
160 input = devm_input_allocate_device(&hdev->dev);
161 if (!input)
162 return -ENOMEM;
163
164 input->name = "Elan Touchpad";
165 input->phys = hdev->phys;
166 input->uniq = hdev->uniq;
167 input->id.bustype = hdev->bus;
168 input->id.vendor = hdev->vendor;
169 input->id.product = hdev->product;
170 input->id.version = hdev->version;
171 input->dev.parent = &hdev->dev;
172
173 input_set_abs_params(input, ABS_MT_POSITION_X, 0, drvdata->max_x,
174 0, 0);
175 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, drvdata->max_y,
176 0, 0);
177 input_set_abs_params(input, ABS_MT_PRESSURE, 0, ELAN_MAX_PRESSURE,
178 0, 0);
179
180 __set_bit(BTN_LEFT, input->keybit);
181 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
182
183 ret = input_mt_init_slots(input, ELAN_MAX_FINGERS, INPUT_MT_POINTER);
184 if (ret) {
185 hid_err(hdev, "Failed to init elan MT slots: %d\n", ret);
186 return ret;
187 }
188
189 input_abs_set_res(input, ABS_X, drvdata->res_x);
190 input_abs_set_res(input, ABS_Y, drvdata->res_y);
191
192 ret = input_register_device(input);
193 if (ret) {
194 hid_err(hdev, "Failed to register elan input device: %d\n",
195 ret);
196 input_free_device(input);
197 return ret;
198 }
199
200 drvdata->input = input;
201
202 return 0;
203 }
204
205 static void elan_report_mt_slot(struct elan_drvdata *drvdata, u8 *data,
206 unsigned int slot_num)
207 {
208 struct input_dev *input = drvdata->input;
209 int x, y, p;
210
211 bool active = !!data;
212
213 input_mt_slot(input, slot_num);
214 input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
215 if (active) {
216 x = ((data[0] & 0xF0) << 4) | data[1];
217 y = drvdata->max_y -
218 (((data[0] & 0x07) << 8) | data[2]);
219 p = data[4];
220
221 input_report_abs(input, ABS_MT_POSITION_X, x);
222 input_report_abs(input, ABS_MT_POSITION_Y, y);
223 input_report_abs(input, ABS_MT_PRESSURE, p);
224 }
225 }
226
227 static void elan_report_input(struct elan_drvdata *drvdata, u8 *data)
228 {
229 int i;
230 struct input_dev *input = drvdata->input;
231
232 /*
233 * There is 3 types of reports: for single touch,
234 * for multitouch - first finger and for multitouch - second finger
235 *
236 * packet structure for ELAN_SINGLE_FINGER and ELAN_MT_FIRST_FINGER:
237 *
238 * byte 1: 1 0 0 0 0 0 0 1 // 0x81 or 0x82
239 * byte 2: 0 0 0 0 0 0 0 0 // looks like unused
240 * byte 3: f5 f4 f3 f2 f1 0 0 L
241 * byte 4: x12 x11 x10 x9 0? y11 y10 y9
242 * byte 5: x8 x7 x6 x5 x4 x3 x2 x1
243 * byte 6: y8 y7 y6 y5 y4 y3 y2 y1
244 * byte 7: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
245 * byte 8: p8 p7 p6 p5 p4 p3 p2 p1
246 *
247 * packet structure for ELAN_MT_SECOND_FINGER:
248 *
249 * byte 1: 1 0 0 0 0 0 1 1 // 0x83
250 * byte 2: x12 x11 x10 x9 0 y11 y10 y9
251 * byte 3: x8 x7 x6 x5 x4 x3 x2 x1
252 * byte 4: y8 y7 y6 y5 y4 y3 y2 y1
253 * byte 5: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
254 * byte 6: p8 p7 p6 p5 p4 p3 p2 p1
255 * byte 7: 0 0 0 0 0 0 0 0
256 * byte 8: 0 0 0 0 0 0 0 0
257 *
258 * f5-f1: finger touch bits
259 * L: clickpad button
260 * sy / sx: finger width / height expressed in traces, the total number
261 * of traces can be queried by doing a HID_REQ_SET_REPORT
262 * { 0x0d, 0x05, 0x03, 0x05, 0x01 } followed by a GET, in the
263 * returned buf, buf[3]=no-x-traces, buf[4]=no-y-traces.
264 * p: pressure
265 */
266
267 if (data[0] == ELAN_SINGLE_FINGER) {
268 for (i = 0; i < ELAN_MAX_FINGERS; i++) {
269 if (data[2] & BIT(i + 3))
270 elan_report_mt_slot(drvdata, data + 3, i);
271 else
272 elan_report_mt_slot(drvdata, NULL, i);
273 }
274 input_report_key(input, BTN_LEFT, data[2] & 0x01);
275 }
276 /*
277 * When touched with two fingers Elan touchpad will emit two HID reports
278 * first is ELAN_MT_FIRST_FINGER and second is ELAN_MT_SECOND_FINGER
279 * we will save ELAN_MT_FIRST_FINGER report and wait for
280 * ELAN_MT_SECOND_FINGER to finish multitouch
281 */
282 if (data[0] == ELAN_MT_FIRST_FINGER) {
283 memcpy(drvdata->prev_report, data,
284 sizeof(drvdata->prev_report));
285 return;
286 }
287
288 if (data[0] == ELAN_MT_SECOND_FINGER) {
289 int first = 0;
290 u8 *prev_report = drvdata->prev_report;
291
292 if (prev_report[0] != ELAN_MT_FIRST_FINGER)
293 return;
294
295 for (i = 0; i < ELAN_MAX_FINGERS; i++) {
296 if (prev_report[2] & BIT(i + 3)) {
297 if (!first) {
298 first = 1;
299 elan_report_mt_slot(drvdata, prev_report + 3, i);
300 } else {
301 elan_report_mt_slot(drvdata, data + 1, i);
302 }
303 } else {
304 elan_report_mt_slot(drvdata, NULL, i);
305 }
306 }
307 input_report_key(input, BTN_LEFT, prev_report[2] & 0x01);
308 }
309
310 input_mt_sync_frame(input);
311 input_sync(input);
312 }
313
314 static int elan_raw_event(struct hid_device *hdev,
315 struct hid_report *report, u8 *data, int size)
316 {
317 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
318
319 if (is_not_elan_touchpad(hdev))
320 return 0;
321
322 if (data[0] == ELAN_SINGLE_FINGER ||
323 data[0] == ELAN_MT_FIRST_FINGER ||
324 data[0] == ELAN_MT_SECOND_FINGER) {
325 if (size == ELAN_INPUT_REPORT_SIZE) {
326 elan_report_input(drvdata, data);
327 return 1;
328 }
329 }
330
331 return 0;
332 }
333
334 static int elan_start_multitouch(struct hid_device *hdev)
335 {
336 int ret;
337
338 /*
339 * This byte sequence will enable multitouch mode and disable
340 * mouse emulation
341 */
342 const unsigned char buf[] = { 0x0D, 0x00, 0x03, 0x21, 0x00 };
343 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
344
345 if (!dmabuf)
346 return -ENOMEM;
347
348 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
349 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
350
351 kfree(dmabuf);
352
353 if (ret != sizeof(buf)) {
354 hid_err(hdev, "Failed to start multitouch: %d\n", ret);
355 return ret;
356 }
357
358 return 0;
359 }
360
361 static enum led_brightness elan_mute_led_get_brigtness(struct led_classdev *led_cdev)
362 {
363 struct device *dev = led_cdev->dev->parent;
364 struct hid_device *hdev = to_hid_device(dev);
365 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
366
367 return drvdata->mute_led_state;
368 }
369
370 static int elan_mute_led_set_brigtness(struct led_classdev *led_cdev,
371 enum led_brightness value)
372 {
373 int ret;
374 u8 led_state;
375 struct device *dev = led_cdev->dev->parent;
376 struct hid_device *hdev = to_hid_device(dev);
377 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
378
379 unsigned char *dmabuf = kzalloc(ELAN_LED_REPORT_SIZE, GFP_KERNEL);
380
381 if (!dmabuf)
382 return -ENOMEM;
383
384 led_state = !!value;
385
386 dmabuf[0] = ELAN_MUTE_LED_REPORT;
387 dmabuf[1] = 0x02;
388 dmabuf[2] = led_state;
389
390 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, ELAN_LED_REPORT_SIZE,
391 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
392
393 kfree(dmabuf);
394
395 if (ret != ELAN_LED_REPORT_SIZE) {
396 hid_err(hdev, "Failed to set mute led brightness: %d\n", ret);
397 return ret;
398 }
399
400 drvdata->mute_led_state = led_state;
401 return 0;
402 }
403
404 static int elan_init_mute_led(struct hid_device *hdev)
405 {
406 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
407 struct led_classdev *mute_led = &drvdata->mute_led;
408
409 mute_led->name = "elan:red:mute";
410 mute_led->brightness_get = elan_mute_led_get_brigtness;
411 mute_led->brightness_set_blocking = elan_mute_led_set_brigtness;
412 mute_led->max_brightness = LED_ON;
413 mute_led->dev = &hdev->dev;
414
415 return devm_led_classdev_register(&hdev->dev, mute_led);
416 }
417
418 static int elan_probe(struct hid_device *hdev, const struct hid_device_id *id)
419 {
420 int ret;
421 struct elan_drvdata *drvdata;
422
423 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
424
425 if (!drvdata)
426 return -ENOMEM;
427
428 hid_set_drvdata(hdev, drvdata);
429
430 ret = hid_parse(hdev);
431 if (ret) {
432 hid_err(hdev, "Hid Parse failed\n");
433 return ret;
434 }
435
436 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
437 if (ret) {
438 hid_err(hdev, "Hid hw start failed\n");
439 return ret;
440 }
441
442 if (is_not_elan_touchpad(hdev))
443 return 0;
444
445 if (!drvdata->input) {
446 hid_err(hdev, "Input device is not registred\n");
447 ret = -ENAVAIL;
448 goto err;
449 }
450
451 ret = elan_start_multitouch(hdev);
452 if (ret)
453 goto err;
454
455 if (id->driver_data & ELAN_HAS_LED) {
456 ret = elan_init_mute_led(hdev);
457 if (ret)
458 goto err;
459 }
460
461 return 0;
462 err:
463 hid_hw_stop(hdev);
464 return ret;
465 }
466
467 static void elan_remove(struct hid_device *hdev)
468 {
469 hid_hw_stop(hdev);
470 }
471
472 static const struct hid_device_id elan_devices[] = {
473 { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2),
474 .driver_data = ELAN_HAS_LED },
475 { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2_10_COVER),
476 .driver_data = ELAN_HAS_LED },
477 { }
478 };
479 MODULE_DEVICE_TABLE(hid, elan_devices);
480
481 static struct hid_driver elan_driver = {
482 .name = "elan",
483 .id_table = elan_devices,
484 .input_mapping = elan_input_mapping,
485 .input_configured = elan_input_configured,
486 .raw_event = elan_raw_event,
487 .probe = elan_probe,
488 .remove = elan_remove,
489 };
490
491 module_hid_driver(elan_driver);
492
493 MODULE_LICENSE("GPL");
494 MODULE_AUTHOR("Alexandrov Stanislav");
495 MODULE_DESCRIPTION("Driver for HID ELAN Touchpads");