blob: 6969fb5344a58c76565c2e4e06b48c3b9649c1be [file] [log] [blame]
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +03001/*
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
Hans de Goede314f04e2018-07-11 12:38:32 +020026#define ELAN_MAX_FINGERS 5
Hans de Goede2f612de2018-07-11 12:38:31 +020027#define ELAN_MAX_PRESSURE 255
Hans de Goede314f04e2018-07-11 12:38:32 +020028#define ELAN_TP_USB_INTF 1
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +030029
Hans de Goede79d11f22018-07-11 12:38:33 +020030#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
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +030036#define ELAN_MUTE_LED_REPORT 0xBC
37#define ELAN_LED_REPORT_SIZE 8
38
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +030039struct elan_drvdata {
40 struct input_dev *input;
41 u8 prev_report[ELAN_INPUT_REPORT_SIZE];
42 struct led_classdev mute_led;
43 u8 mute_led_state;
Hans de Goede79d11f22018-07-11 12:38:33 +020044 u16 max_x;
45 u16 max_y;
Hans de Goede19588be2018-07-11 12:38:34 +020046 u16 res_x;
47 u16 res_y;
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +030048};
49
50static int is_not_elan_touchpad(struct hid_device *hdev)
51{
52 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +030053
Hans de Goede314f04e2018-07-11 12:38:32 +020054 return (intf->altsetting->desc.bInterfaceNumber != ELAN_TP_USB_INTF);
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +030055}
56
57static int elan_input_mapping(struct hid_device *hdev, struct hid_input *hi,
58 struct hid_field *field, struct hid_usage *usage,
59 unsigned long **bit, int *max)
60{
61 if (is_not_elan_touchpad(hdev))
62 return 0;
63
64 if (field->report->id == ELAN_SINGLE_FINGER ||
65 field->report->id == ELAN_MT_FIRST_FINGER ||
66 field->report->id == ELAN_MT_SECOND_FINGER)
67 return -1;
68
69 return 0;
70}
71
Hans de Goede79d11f22018-07-11 12:38:33 +020072static int elan_get_device_param(struct hid_device *hdev,
73 unsigned char *dmabuf, unsigned char param)
74{
75 int ret;
76
77 dmabuf[0] = ELAN_FEATURE_REPORT;
78 dmabuf[1] = 0x05;
79 dmabuf[2] = 0x03;
80 dmabuf[3] = param;
81 dmabuf[4] = 0x01;
82
83 ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
84 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
85 HID_REQ_SET_REPORT);
86 if (ret != ELAN_FEATURE_SIZE) {
87 hid_err(hdev, "Set report error for parm %d: %d\n", param, ret);
88 return ret;
89 }
90
91 ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
92 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
93 HID_REQ_GET_REPORT);
94 if (ret != ELAN_FEATURE_SIZE) {
95 hid_err(hdev, "Get report error for parm %d: %d\n", param, ret);
96 return ret;
97 }
98
99 return 0;
100}
101
Hans de Goede19588be2018-07-11 12:38:34 +0200102static unsigned int elan_convert_res(char val)
103{
104 /*
105 * (value from firmware) * 10 + 790 = dpi
106 * dpi * 10 / 254 = dots/mm
107 */
108 return (val * 10 + 790) * 10 / 254;
109}
110
Hans de Goede79d11f22018-07-11 12:38:33 +0200111static int elan_get_device_params(struct hid_device *hdev)
112{
113 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
114 unsigned char *dmabuf;
115 int ret;
116
117 dmabuf = kmalloc(ELAN_FEATURE_SIZE, GFP_KERNEL);
118 if (!dmabuf)
119 return -ENOMEM;
120
121 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_X);
122 if (ret)
123 goto err;
124
125 drvdata->max_x = (dmabuf[4] << 8) | dmabuf[3];
126
127 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_Y);
128 if (ret)
129 goto err;
130
131 drvdata->max_y = (dmabuf[4] << 8) | dmabuf[3];
132
Hans de Goede19588be2018-07-11 12:38:34 +0200133 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_RES);
134 if (ret)
135 goto err;
136
137 drvdata->res_x = elan_convert_res(dmabuf[3]);
138 drvdata->res_y = elan_convert_res(dmabuf[4]);
139
Hans de Goede79d11f22018-07-11 12:38:33 +0200140err:
141 kfree(dmabuf);
142 return ret;
143}
144
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300145static int elan_input_configured(struct hid_device *hdev, struct hid_input *hi)
146{
147 int ret;
148 struct input_dev *input;
149 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
150
151 if (is_not_elan_touchpad(hdev))
152 return 0;
153
Hans de Goede79d11f22018-07-11 12:38:33 +0200154 ret = elan_get_device_params(hdev);
155 if (ret)
156 return ret;
157
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300158 input = devm_input_allocate_device(&hdev->dev);
159 if (!input)
160 return -ENOMEM;
161
162 input->name = "Elan Touchpad";
163 input->phys = hdev->phys;
164 input->uniq = hdev->uniq;
165 input->id.bustype = hdev->bus;
166 input->id.vendor = hdev->vendor;
167 input->id.product = hdev->product;
168 input->id.version = hdev->version;
169 input->dev.parent = &hdev->dev;
170
Hans de Goede79d11f22018-07-11 12:38:33 +0200171 input_set_abs_params(input, ABS_MT_POSITION_X, 0, drvdata->max_x,
172 0, 0);
173 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, drvdata->max_y,
174 0, 0);
Hans de Goede2f612de2018-07-11 12:38:31 +0200175 input_set_abs_params(input, ABS_MT_PRESSURE, 0, ELAN_MAX_PRESSURE,
176 0, 0);
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300177
178 __set_bit(BTN_LEFT, input->keybit);
179 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
180
Hans de Goede314f04e2018-07-11 12:38:32 +0200181 ret = input_mt_init_slots(input, ELAN_MAX_FINGERS, INPUT_MT_POINTER);
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300182 if (ret) {
183 hid_err(hdev, "Failed to init elan MT slots: %d\n", ret);
184 return ret;
185 }
186
Hans de Goede19588be2018-07-11 12:38:34 +0200187 input_abs_set_res(input, ABS_X, drvdata->res_x);
188 input_abs_set_res(input, ABS_Y, drvdata->res_y);
189
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300190 ret = input_register_device(input);
191 if (ret) {
192 hid_err(hdev, "Failed to register elan input device: %d\n",
193 ret);
194 input_free_device(input);
195 return ret;
196 }
197
198 drvdata->input = input;
199
200 return 0;
201}
202
203static void elan_report_mt_slot(struct elan_drvdata *drvdata, u8 *data,
204 unsigned int slot_num)
205{
206 struct input_dev *input = drvdata->input;
Hans de Goede2f612de2018-07-11 12:38:31 +0200207 int x, y, p;
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300208
209 bool active = !!data;
210
211 input_mt_slot(input, slot_num);
212 input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
213 if (active) {
214 x = ((data[0] & 0xF0) << 4) | data[1];
Hans de Goede79d11f22018-07-11 12:38:33 +0200215 y = drvdata->max_y -
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300216 (((data[0] & 0x07) << 8) | data[2]);
Hans de Goede2f612de2018-07-11 12:38:31 +0200217 p = data[4];
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300218
219 input_report_abs(input, ABS_MT_POSITION_X, x);
220 input_report_abs(input, ABS_MT_POSITION_Y, y);
Hans de Goede2f612de2018-07-11 12:38:31 +0200221 input_report_abs(input, ABS_MT_PRESSURE, p);
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300222 }
223}
224
225static void elan_report_input(struct elan_drvdata *drvdata, u8 *data)
226{
227 int i;
228 struct input_dev *input = drvdata->input;
229
230 /*
231 * There is 3 types of reports: for single touch,
232 * for multitouch - first finger and for multitouch - second finger
233 *
234 * packet structure for ELAN_SINGLE_FINGER and ELAN_MT_FIRST_FINGER:
235 *
236 * byte 1: 1 0 0 0 0 0 0 1 // 0x81 or 0x82
237 * byte 2: 0 0 0 0 0 0 0 0 // looks like unused
238 * byte 3: f5 f4 f3 f2 f1 0 0 L
239 * byte 4: x12 x11 x10 x9 0? y11 y10 y9
240 * byte 5: x8 x7 x6 x5 x4 x3 x2 x1
241 * byte 6: y8 y7 y6 y5 y4 y3 y2 y1
242 * byte 7: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
Hans de Goede2f612de2018-07-11 12:38:31 +0200243 * byte 8: p8 p7 p6 p5 p4 p3 p2 p1
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300244 *
245 * packet structure for ELAN_MT_SECOND_FINGER:
246 *
247 * byte 1: 1 0 0 0 0 0 1 1 // 0x83
248 * byte 2: x12 x11 x10 x9 0 y11 y10 y9
249 * byte 3: x8 x7 x6 x5 x4 x3 x2 x1
250 * byte 4: y8 y7 y6 y5 y4 y3 y2 y1
251 * byte 5: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
Hans de Goede2f612de2018-07-11 12:38:31 +0200252 * byte 6: p8 p7 p6 p5 p4 p3 p2 p1
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300253 * byte 7: 0 0 0 0 0 0 0 0
254 * byte 8: 0 0 0 0 0 0 0 0
255 *
256 * f5-f1: finger touch bits
257 * L: clickpad button
Hans de Goede2f612de2018-07-11 12:38:31 +0200258 * sy / sx: finger width / height expressed in traces, the total number
259 * of traces can be queried by doing a HID_REQ_SET_REPORT
260 * { 0x0d, 0x05, 0x03, 0x05, 0x01 } followed by a GET, in the
261 * returned buf, buf[3]=no-x-traces, buf[4]=no-y-traces.
262 * p: pressure
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300263 */
264
265 if (data[0] == ELAN_SINGLE_FINGER) {
Hans de Goede314f04e2018-07-11 12:38:32 +0200266 for (i = 0; i < ELAN_MAX_FINGERS; i++) {
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300267 if (data[2] & BIT(i + 3))
268 elan_report_mt_slot(drvdata, data + 3, i);
269 else
270 elan_report_mt_slot(drvdata, NULL, i);
271 }
272 input_report_key(input, BTN_LEFT, data[2] & 0x01);
273 }
274 /*
275 * When touched with two fingers Elan touchpad will emit two HID reports
276 * first is ELAN_MT_FIRST_FINGER and second is ELAN_MT_SECOND_FINGER
277 * we will save ELAN_MT_FIRST_FINGER report and wait for
278 * ELAN_MT_SECOND_FINGER to finish multitouch
279 */
280 if (data[0] == ELAN_MT_FIRST_FINGER) {
281 memcpy(drvdata->prev_report, data,
282 sizeof(drvdata->prev_report));
283 return;
284 }
285
286 if (data[0] == ELAN_MT_SECOND_FINGER) {
287 int first = 0;
288 u8 *prev_report = drvdata->prev_report;
289
290 if (prev_report[0] != ELAN_MT_FIRST_FINGER)
291 return;
292
Hans de Goede314f04e2018-07-11 12:38:32 +0200293 for (i = 0; i < ELAN_MAX_FINGERS; i++) {
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300294 if (prev_report[2] & BIT(i + 3)) {
295 if (!first) {
296 first = 1;
297 elan_report_mt_slot(drvdata, prev_report + 3, i);
298 } else {
299 elan_report_mt_slot(drvdata, data + 1, i);
300 }
301 } else {
302 elan_report_mt_slot(drvdata, NULL, i);
303 }
304 }
305 input_report_key(input, BTN_LEFT, prev_report[2] & 0x01);
306 }
307
308 input_mt_sync_frame(input);
309 input_sync(input);
310}
311
312static int elan_raw_event(struct hid_device *hdev,
313 struct hid_report *report, u8 *data, int size)
314{
315 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
316
317 if (is_not_elan_touchpad(hdev))
318 return 0;
319
320 if (data[0] == ELAN_SINGLE_FINGER ||
321 data[0] == ELAN_MT_FIRST_FINGER ||
322 data[0] == ELAN_MT_SECOND_FINGER) {
323 if (size == ELAN_INPUT_REPORT_SIZE) {
324 elan_report_input(drvdata, data);
325 return 1;
326 }
327 }
328
329 return 0;
330}
331
332static int elan_start_multitouch(struct hid_device *hdev)
333{
334 int ret;
335
336 /*
337 * This byte sequence will enable multitouch mode and disable
338 * mouse emulation
339 */
340 const unsigned char buf[] = { 0x0D, 0x00, 0x03, 0x21, 0x00 };
341 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
342
343 if (!dmabuf)
344 return -ENOMEM;
345
346 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
347 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
348
349 kfree(dmabuf);
350
351 if (ret != sizeof(buf)) {
352 hid_err(hdev, "Failed to start multitouch: %d\n", ret);
353 return ret;
354 }
355
356 return 0;
357}
358
359static enum led_brightness elan_mute_led_get_brigtness(struct led_classdev *led_cdev)
360{
361 struct device *dev = led_cdev->dev->parent;
362 struct hid_device *hdev = to_hid_device(dev);
363 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
364
365 return drvdata->mute_led_state;
366}
367
368static int elan_mute_led_set_brigtness(struct led_classdev *led_cdev,
369 enum led_brightness value)
370{
371 int ret;
372 u8 led_state;
373 struct device *dev = led_cdev->dev->parent;
374 struct hid_device *hdev = to_hid_device(dev);
375 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
376
377 unsigned char *dmabuf = kzalloc(ELAN_LED_REPORT_SIZE, GFP_KERNEL);
378
379 if (!dmabuf)
380 return -ENOMEM;
381
382 led_state = !!value;
383
384 dmabuf[0] = ELAN_MUTE_LED_REPORT;
385 dmabuf[1] = 0x02;
386 dmabuf[2] = led_state;
387
388 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, ELAN_LED_REPORT_SIZE,
389 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
390
391 kfree(dmabuf);
392
393 if (ret != ELAN_LED_REPORT_SIZE) {
394 hid_err(hdev, "Failed to set mute led brightness: %d\n", ret);
395 return ret;
396 }
397
398 drvdata->mute_led_state = led_state;
399 return 0;
400}
401
402static int elan_init_mute_led(struct hid_device *hdev)
403{
404 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
405 struct led_classdev *mute_led = &drvdata->mute_led;
406
407 mute_led->name = "elan:red:mute";
408 mute_led->brightness_get = elan_mute_led_get_brigtness;
409 mute_led->brightness_set_blocking = elan_mute_led_set_brigtness;
410 mute_led->max_brightness = LED_ON;
411 mute_led->dev = &hdev->dev;
412
413 return devm_led_classdev_register(&hdev->dev, mute_led);
414}
415
416static int elan_probe(struct hid_device *hdev, const struct hid_device_id *id)
417{
418 int ret;
419 struct elan_drvdata *drvdata;
420
421 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
422
423 if (!drvdata)
424 return -ENOMEM;
425
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300426 hid_set_drvdata(hdev, drvdata);
427
428 ret = hid_parse(hdev);
429 if (ret) {
430 hid_err(hdev, "Hid Parse failed\n");
431 return ret;
432 }
433
434 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
435 if (ret) {
436 hid_err(hdev, "Hid hw start failed\n");
437 return ret;
438 }
439
440 if (is_not_elan_touchpad(hdev))
441 return 0;
442
443 if (!drvdata->input) {
444 hid_err(hdev, "Input device is not registred\n");
445 ret = -ENAVAIL;
446 goto err;
447 }
448
449 ret = elan_start_multitouch(hdev);
450 if (ret)
451 goto err;
452
453 ret = elan_init_mute_led(hdev);
454 if (ret)
455 goto err;
456
457 return 0;
458err:
459 hid_hw_stop(hdev);
460 return ret;
461}
462
463static void elan_remove(struct hid_device *hdev)
464{
465 hid_hw_stop(hdev);
466}
467
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300468static const struct hid_device_id elan_devices[] = {
Hans de Goede79d11f22018-07-11 12:38:33 +0200469 { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2_10_COVER) },
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300470 { }
471};
Alexandrov Stansilav9a6a4192018-01-14 02:33:49 +0300472MODULE_DEVICE_TABLE(hid, elan_devices);
473
474static struct hid_driver elan_driver = {
475 .name = "elan",
476 .id_table = elan_devices,
477 .input_mapping = elan_input_mapping,
478 .input_configured = elan_input_configured,
479 .raw_event = elan_raw_event,
480 .probe = elan_probe,
481 .remove = elan_remove,
482};
483
484module_hid_driver(elan_driver);
485
486MODULE_LICENSE("GPL");
487MODULE_AUTHOR("Alexandrov Stanislav");
488MODULE_DESCRIPTION("Driver for HID ELAN Touchpads");