1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID Driver for ELAN Touchpad
5 * Currently only supports touchpad found on HP Pavilion X2 10
7 * Copyright (c) 2016 Alexandrov Stanislav <neko@nya.ai>
10 #include <linux/hid.h>
11 #include <linux/input/mt.h>
12 #include <linux/leds.h>
13 #include <linux/module.h>
14 #include <linux/usb.h>
18 #define ELAN_MT_I2C 0x5d
19 #define ELAN_SINGLE_FINGER 0x81
20 #define ELAN_MT_FIRST_FINGER 0x82
21 #define ELAN_MT_SECOND_FINGER 0x83
22 #define ELAN_INPUT_REPORT_SIZE 8
23 #define ELAN_I2C_REPORT_SIZE 32
24 #define ELAN_FINGER_DATA_LEN 5
25 #define ELAN_MAX_FINGERS 5
26 #define ELAN_MAX_PRESSURE 255
27 #define ELAN_TP_USB_INTF 1
29 #define ELAN_FEATURE_REPORT 0x0d
30 #define ELAN_FEATURE_SIZE 5
31 #define ELAN_PARAM_MAX_X 6
32 #define ELAN_PARAM_MAX_Y 7
33 #define ELAN_PARAM_RES 8
35 #define ELAN_MUTE_LED_REPORT 0xBC
36 #define ELAN_LED_REPORT_SIZE 8
38 #define ELAN_HAS_LED BIT(0)
41 struct input_dev *input;
42 u8 prev_report[ELAN_INPUT_REPORT_SIZE];
43 struct led_classdev mute_led;
51 static int is_not_elan_touchpad(struct hid_device *hdev)
53 if (hid_is_usb(hdev)) {
54 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
56 return (intf->altsetting->desc.bInterfaceNumber !=
63 static int elan_input_mapping(struct hid_device *hdev, struct hid_input *hi,
64 struct hid_field *field, struct hid_usage *usage,
65 unsigned long **bit, int *max)
67 if (is_not_elan_touchpad(hdev))
70 if (field->report->id == ELAN_SINGLE_FINGER ||
71 field->report->id == ELAN_MT_FIRST_FINGER ||
72 field->report->id == ELAN_MT_SECOND_FINGER ||
73 field->report->id == ELAN_MT_I2C)
79 static int elan_get_device_param(struct hid_device *hdev,
80 unsigned char *dmabuf, unsigned char param)
84 dmabuf[0] = ELAN_FEATURE_REPORT;
90 ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
91 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
93 if (ret != ELAN_FEATURE_SIZE) {
94 hid_err(hdev, "Set report error for parm %d: %d\n", param, ret);
98 ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
99 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
101 if (ret != ELAN_FEATURE_SIZE) {
102 hid_err(hdev, "Get report error for parm %d: %d\n", param, ret);
109 static unsigned int elan_convert_res(char val)
112 * (value from firmware) * 10 + 790 = dpi
113 * dpi * 10 / 254 = dots/mm
115 return (val * 10 + 790) * 10 / 254;
118 static int elan_get_device_params(struct hid_device *hdev)
120 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
121 unsigned char *dmabuf;
124 dmabuf = kmalloc(ELAN_FEATURE_SIZE, GFP_KERNEL);
128 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_X);
132 drvdata->max_x = (dmabuf[4] << 8) | dmabuf[3];
134 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_Y);
138 drvdata->max_y = (dmabuf[4] << 8) | dmabuf[3];
140 ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_RES);
144 drvdata->res_x = elan_convert_res(dmabuf[3]);
145 drvdata->res_y = elan_convert_res(dmabuf[4]);
152 static int elan_input_configured(struct hid_device *hdev, struct hid_input *hi)
155 struct input_dev *input;
156 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
158 if (is_not_elan_touchpad(hdev))
161 ret = elan_get_device_params(hdev);
165 input = devm_input_allocate_device(&hdev->dev);
169 input->name = "Elan Touchpad";
170 input->phys = hdev->phys;
171 input->uniq = hdev->uniq;
172 input->id.bustype = hdev->bus;
173 input->id.vendor = hdev->vendor;
174 input->id.product = hdev->product;
175 input->id.version = hdev->version;
176 input->dev.parent = &hdev->dev;
178 input_set_abs_params(input, ABS_MT_POSITION_X, 0, drvdata->max_x,
180 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, drvdata->max_y,
182 input_set_abs_params(input, ABS_MT_PRESSURE, 0, ELAN_MAX_PRESSURE,
185 __set_bit(BTN_LEFT, input->keybit);
186 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
188 ret = input_mt_init_slots(input, ELAN_MAX_FINGERS, INPUT_MT_POINTER);
190 hid_err(hdev, "Failed to init elan MT slots: %d\n", ret);
194 input_abs_set_res(input, ABS_X, drvdata->res_x);
195 input_abs_set_res(input, ABS_Y, drvdata->res_y);
197 ret = input_register_device(input);
199 hid_err(hdev, "Failed to register elan input device: %d\n",
201 input_mt_destroy_slots(input);
205 drvdata->input = input;
210 static void elan_report_mt_slot(struct elan_drvdata *drvdata, u8 *data,
211 unsigned int slot_num)
213 struct input_dev *input = drvdata->input;
216 bool active = !!data;
218 input_mt_slot(input, slot_num);
219 input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
221 x = ((data[0] & 0xF0) << 4) | data[1];
223 (((data[0] & 0x07) << 8) | data[2]);
226 input_report_abs(input, ABS_MT_POSITION_X, x);
227 input_report_abs(input, ABS_MT_POSITION_Y, y);
228 input_report_abs(input, ABS_MT_PRESSURE, p);
232 static void elan_usb_report_input(struct elan_drvdata *drvdata, u8 *data)
235 struct input_dev *input = drvdata->input;
238 * There is 3 types of reports: for single touch,
239 * for multitouch - first finger and for multitouch - second finger
241 * packet structure for ELAN_SINGLE_FINGER and ELAN_MT_FIRST_FINGER:
243 * byte 1: 1 0 0 0 0 0 0 1 // 0x81 or 0x82
244 * byte 2: 0 0 0 0 0 0 0 0 // looks like unused
245 * byte 3: f5 f4 f3 f2 f1 0 0 L
246 * byte 4: x12 x11 x10 x9 0? y11 y10 y9
247 * byte 5: x8 x7 x6 x5 x4 x3 x2 x1
248 * byte 6: y8 y7 y6 y5 y4 y3 y2 y1
249 * byte 7: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
250 * byte 8: p8 p7 p6 p5 p4 p3 p2 p1
252 * packet structure for ELAN_MT_SECOND_FINGER:
254 * byte 1: 1 0 0 0 0 0 1 1 // 0x83
255 * byte 2: x12 x11 x10 x9 0 y11 y10 y9
256 * byte 3: x8 x7 x6 x5 x4 x3 x2 x1
257 * byte 4: y8 y7 y6 y5 y4 y3 y2 y1
258 * byte 5: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
259 * byte 6: p8 p7 p6 p5 p4 p3 p2 p1
260 * byte 7: 0 0 0 0 0 0 0 0
261 * byte 8: 0 0 0 0 0 0 0 0
263 * f5-f1: finger touch bits
265 * sy / sx: finger width / height expressed in traces, the total number
266 * of traces can be queried by doing a HID_REQ_SET_REPORT
267 * { 0x0d, 0x05, 0x03, 0x05, 0x01 } followed by a GET, in the
268 * returned buf, buf[3]=no-x-traces, buf[4]=no-y-traces.
272 if (data[0] == ELAN_SINGLE_FINGER) {
273 for (i = 0; i < ELAN_MAX_FINGERS; i++) {
274 if (data[2] & BIT(i + 3))
275 elan_report_mt_slot(drvdata, data + 3, i);
277 elan_report_mt_slot(drvdata, NULL, i);
279 input_report_key(input, BTN_LEFT, data[2] & 0x01);
282 * When touched with two fingers Elan touchpad will emit two HID reports
283 * first is ELAN_MT_FIRST_FINGER and second is ELAN_MT_SECOND_FINGER
284 * we will save ELAN_MT_FIRST_FINGER report and wait for
285 * ELAN_MT_SECOND_FINGER to finish multitouch
287 if (data[0] == ELAN_MT_FIRST_FINGER) {
288 memcpy(drvdata->prev_report, data,
289 sizeof(drvdata->prev_report));
293 if (data[0] == ELAN_MT_SECOND_FINGER) {
295 u8 *prev_report = drvdata->prev_report;
297 if (prev_report[0] != ELAN_MT_FIRST_FINGER)
300 for (i = 0; i < ELAN_MAX_FINGERS; i++) {
301 if (prev_report[2] & BIT(i + 3)) {
304 elan_report_mt_slot(drvdata, prev_report + 3, i);
306 elan_report_mt_slot(drvdata, data + 1, i);
309 elan_report_mt_slot(drvdata, NULL, i);
312 input_report_key(input, BTN_LEFT, prev_report[2] & 0x01);
315 input_mt_sync_frame(input);
319 static void elan_i2c_report_input(struct elan_drvdata *drvdata, u8 *data)
321 struct input_dev *input = drvdata->input;
326 * Elan MT touchpads in i2c mode send finger data in the same format
327 * as in USB mode, but then with all fingers in a single packet.
329 * packet structure for ELAN_MT_I2C:
331 * byte 1: 1 0 0 1 1 1 0 1 // 0x5d
332 * byte 2: f5 f4 f3 f2 f1 0 0 L
333 * byte 3: x12 x11 x10 x9 0? y11 y10 y9
334 * byte 4: x8 x7 x6 x5 x4 x3 x2 x1
335 * byte 5: y8 y7 y6 y5 y4 y3 y2 y1
336 * byte 6: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
337 * byte 7: p8 p7 p6 p5 p4 p3 p2 p1
338 * byte 8-12: Same as byte 3-7 for second finger down
339 * byte 13-17: Same as byte 3-7 for third finger down
340 * byte 18-22: Same as byte 3-7 for fourth finger down
341 * byte 23-27: Same as byte 3-7 for fifth finger down
344 finger_data = data + 2;
345 for (i = 0; i < ELAN_MAX_FINGERS; i++) {
346 if (data[1] & BIT(i + 3)) {
347 elan_report_mt_slot(drvdata, finger_data, i);
348 finger_data += ELAN_FINGER_DATA_LEN;
350 elan_report_mt_slot(drvdata, NULL, i);
354 input_report_key(input, BTN_LEFT, data[1] & 0x01);
355 input_mt_sync_frame(input);
359 static int elan_raw_event(struct hid_device *hdev,
360 struct hid_report *report, u8 *data, int size)
362 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
364 if (is_not_elan_touchpad(hdev))
367 if (data[0] == ELAN_SINGLE_FINGER ||
368 data[0] == ELAN_MT_FIRST_FINGER ||
369 data[0] == ELAN_MT_SECOND_FINGER) {
370 if (size == ELAN_INPUT_REPORT_SIZE) {
371 elan_usb_report_input(drvdata, data);
376 if (data[0] == ELAN_MT_I2C && size == ELAN_I2C_REPORT_SIZE) {
377 elan_i2c_report_input(drvdata, data);
384 static int elan_start_multitouch(struct hid_device *hdev)
389 * This byte sequence will enable multitouch mode and disable
392 static const unsigned char buf[] = { 0x0D, 0x00, 0x03, 0x21, 0x00 };
393 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
398 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
399 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
403 if (ret != sizeof(buf)) {
404 hid_err(hdev, "Failed to start multitouch: %d\n", ret);
411 static int elan_mute_led_set_brigtness(struct led_classdev *led_cdev,
412 enum led_brightness value)
416 struct device *dev = led_cdev->dev->parent;
417 struct hid_device *hdev = to_hid_device(dev);
418 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
420 unsigned char *dmabuf = kzalloc(ELAN_LED_REPORT_SIZE, GFP_KERNEL);
427 dmabuf[0] = ELAN_MUTE_LED_REPORT;
429 dmabuf[2] = led_state;
431 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, ELAN_LED_REPORT_SIZE,
432 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
436 if (ret != ELAN_LED_REPORT_SIZE) {
438 hid_err(hdev, "Failed to set mute led brightness: %d\n", ret);
439 return ret < 0 ? ret : -EIO;
442 drvdata->mute_led_state = led_state;
446 static int elan_init_mute_led(struct hid_device *hdev)
448 struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
449 struct led_classdev *mute_led = &drvdata->mute_led;
451 mute_led->name = "elan:red:mute";
452 mute_led->default_trigger = "audio-mute";
453 mute_led->brightness_set_blocking = elan_mute_led_set_brigtness;
454 mute_led->max_brightness = LED_ON;
455 mute_led->flags = LED_HW_PLUGGABLE;
456 mute_led->dev = &hdev->dev;
458 return devm_led_classdev_register(&hdev->dev, mute_led);
461 static int elan_probe(struct hid_device *hdev, const struct hid_device_id *id)
464 struct elan_drvdata *drvdata;
466 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
471 hid_set_drvdata(hdev, drvdata);
473 ret = hid_parse(hdev);
475 hid_err(hdev, "Hid Parse failed\n");
479 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
481 hid_err(hdev, "Hid hw start failed\n");
485 if (is_not_elan_touchpad(hdev))
488 if (!drvdata->input) {
489 hid_err(hdev, "Input device is not registered\n");
494 ret = elan_start_multitouch(hdev);
498 if (id->driver_data & ELAN_HAS_LED) {
499 ret = elan_init_mute_led(hdev);
510 static void elan_remove(struct hid_device *hdev)
515 static const struct hid_device_id elan_devices[] = {
516 { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2),
517 .driver_data = ELAN_HAS_LED },
518 { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2_10_COVER),
519 .driver_data = ELAN_HAS_LED },
520 { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_TOSHIBA_CLICK_L9W) },
523 MODULE_DEVICE_TABLE(hid, elan_devices);
525 static struct hid_driver elan_driver = {
527 .id_table = elan_devices,
528 .input_mapping = elan_input_mapping,
529 .input_configured = elan_input_configured,
530 .raw_event = elan_raw_event,
532 .remove = elan_remove,
535 module_hid_driver(elan_driver);
537 MODULE_LICENSE("GPL");
538 MODULE_AUTHOR("Alexandrov Stanislav");
539 MODULE_DESCRIPTION("Driver for HID ELAN Touchpads");