2 * HID Driver for ELAN Touchpad
4 * Currently only supports touchpad found on HP Pavilion X2 10
6 * Copyright (c) 2016 Alexandrov Stanislav <neko@nya.ai>
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)
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>
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
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
36 #define ELAN_MUTE_LED_REPORT 0xBC
37 #define ELAN_LED_REPORT_SIZE 8
39 #define ELAN_HAS_LED BIT(0)
42 struct input_dev
*input
;
43 u8 prev_report
[ELAN_INPUT_REPORT_SIZE
];
44 struct led_classdev mute_led
;
52 static int is_not_elan_touchpad(struct hid_device
*hdev
)
54 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
56 return (intf
->altsetting
->desc
.bInterfaceNumber
!= ELAN_TP_USB_INTF
);
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
)
63 if (is_not_elan_touchpad(hdev
))
66 if (field
->report
->id
== ELAN_SINGLE_FINGER
||
67 field
->report
->id
== ELAN_MT_FIRST_FINGER
||
68 field
->report
->id
== ELAN_MT_SECOND_FINGER
)
74 static int elan_get_device_param(struct hid_device
*hdev
,
75 unsigned char *dmabuf
, unsigned char param
)
79 dmabuf
[0] = ELAN_FEATURE_REPORT
;
85 ret
= hid_hw_raw_request(hdev
, ELAN_FEATURE_REPORT
, dmabuf
,
86 ELAN_FEATURE_SIZE
, HID_FEATURE_REPORT
,
88 if (ret
!= ELAN_FEATURE_SIZE
) {
89 hid_err(hdev
, "Set report error for parm %d: %d\n", param
, ret
);
93 ret
= hid_hw_raw_request(hdev
, ELAN_FEATURE_REPORT
, dmabuf
,
94 ELAN_FEATURE_SIZE
, HID_FEATURE_REPORT
,
96 if (ret
!= ELAN_FEATURE_SIZE
) {
97 hid_err(hdev
, "Get report error for parm %d: %d\n", param
, ret
);
104 static unsigned int elan_convert_res(char val
)
107 * (value from firmware) * 10 + 790 = dpi
108 * dpi * 10 / 254 = dots/mm
110 return (val
* 10 + 790) * 10 / 254;
113 static int elan_get_device_params(struct hid_device
*hdev
)
115 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
116 unsigned char *dmabuf
;
119 dmabuf
= kmalloc(ELAN_FEATURE_SIZE
, GFP_KERNEL
);
123 ret
= elan_get_device_param(hdev
, dmabuf
, ELAN_PARAM_MAX_X
);
127 drvdata
->max_x
= (dmabuf
[4] << 8) | dmabuf
[3];
129 ret
= elan_get_device_param(hdev
, dmabuf
, ELAN_PARAM_MAX_Y
);
133 drvdata
->max_y
= (dmabuf
[4] << 8) | dmabuf
[3];
135 ret
= elan_get_device_param(hdev
, dmabuf
, ELAN_PARAM_RES
);
139 drvdata
->res_x
= elan_convert_res(dmabuf
[3]);
140 drvdata
->res_y
= elan_convert_res(dmabuf
[4]);
147 static int elan_input_configured(struct hid_device
*hdev
, struct hid_input
*hi
)
150 struct input_dev
*input
;
151 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
153 if (is_not_elan_touchpad(hdev
))
156 ret
= elan_get_device_params(hdev
);
160 input
= devm_input_allocate_device(&hdev
->dev
);
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
;
173 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, drvdata
->max_x
,
175 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, drvdata
->max_y
,
177 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, ELAN_MAX_PRESSURE
,
180 __set_bit(BTN_LEFT
, input
->keybit
);
181 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
183 ret
= input_mt_init_slots(input
, ELAN_MAX_FINGERS
, INPUT_MT_POINTER
);
185 hid_err(hdev
, "Failed to init elan MT slots: %d\n", ret
);
189 input_abs_set_res(input
, ABS_X
, drvdata
->res_x
);
190 input_abs_set_res(input
, ABS_Y
, drvdata
->res_y
);
192 ret
= input_register_device(input
);
194 hid_err(hdev
, "Failed to register elan input device: %d\n",
196 input_free_device(input
);
200 drvdata
->input
= input
;
205 static void elan_report_mt_slot(struct elan_drvdata
*drvdata
, u8
*data
,
206 unsigned int slot_num
)
208 struct input_dev
*input
= drvdata
->input
;
211 bool active
= !!data
;
213 input_mt_slot(input
, slot_num
);
214 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, active
);
216 x
= ((data
[0] & 0xF0) << 4) | data
[1];
218 (((data
[0] & 0x07) << 8) | data
[2]);
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
);
227 static void elan_report_input(struct elan_drvdata
*drvdata
, u8
*data
)
230 struct input_dev
*input
= drvdata
->input
;
233 * There is 3 types of reports: for single touch,
234 * for multitouch - first finger and for multitouch - second finger
236 * packet structure for ELAN_SINGLE_FINGER and ELAN_MT_FIRST_FINGER:
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
247 * packet structure for ELAN_MT_SECOND_FINGER:
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
258 * f5-f1: finger touch bits
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.
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
);
272 elan_report_mt_slot(drvdata
, NULL
, i
);
274 input_report_key(input
, BTN_LEFT
, data
[2] & 0x01);
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
282 if (data
[0] == ELAN_MT_FIRST_FINGER
) {
283 memcpy(drvdata
->prev_report
, data
,
284 sizeof(drvdata
->prev_report
));
288 if (data
[0] == ELAN_MT_SECOND_FINGER
) {
290 u8
*prev_report
= drvdata
->prev_report
;
292 if (prev_report
[0] != ELAN_MT_FIRST_FINGER
)
295 for (i
= 0; i
< ELAN_MAX_FINGERS
; i
++) {
296 if (prev_report
[2] & BIT(i
+ 3)) {
299 elan_report_mt_slot(drvdata
, prev_report
+ 3, i
);
301 elan_report_mt_slot(drvdata
, data
+ 1, i
);
304 elan_report_mt_slot(drvdata
, NULL
, i
);
307 input_report_key(input
, BTN_LEFT
, prev_report
[2] & 0x01);
310 input_mt_sync_frame(input
);
314 static int elan_raw_event(struct hid_device
*hdev
,
315 struct hid_report
*report
, u8
*data
, int size
)
317 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
319 if (is_not_elan_touchpad(hdev
))
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
);
334 static int elan_start_multitouch(struct hid_device
*hdev
)
339 * This byte sequence will enable multitouch mode and disable
342 const unsigned char buf
[] = { 0x0D, 0x00, 0x03, 0x21, 0x00 };
343 unsigned char *dmabuf
= kmemdup(buf
, sizeof(buf
), GFP_KERNEL
);
348 ret
= hid_hw_raw_request(hdev
, dmabuf
[0], dmabuf
, sizeof(buf
),
349 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
353 if (ret
!= sizeof(buf
)) {
354 hid_err(hdev
, "Failed to start multitouch: %d\n", ret
);
361 static enum led_brightness
elan_mute_led_get_brigtness(struct led_classdev
*led_cdev
)
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
);
367 return drvdata
->mute_led_state
;
370 static int elan_mute_led_set_brigtness(struct led_classdev
*led_cdev
,
371 enum led_brightness value
)
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
);
379 unsigned char *dmabuf
= kzalloc(ELAN_LED_REPORT_SIZE
, GFP_KERNEL
);
386 dmabuf
[0] = ELAN_MUTE_LED_REPORT
;
388 dmabuf
[2] = led_state
;
390 ret
= hid_hw_raw_request(hdev
, dmabuf
[0], dmabuf
, ELAN_LED_REPORT_SIZE
,
391 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
395 if (ret
!= ELAN_LED_REPORT_SIZE
) {
396 hid_err(hdev
, "Failed to set mute led brightness: %d\n", ret
);
400 drvdata
->mute_led_state
= led_state
;
404 static int elan_init_mute_led(struct hid_device
*hdev
)
406 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
407 struct led_classdev
*mute_led
= &drvdata
->mute_led
;
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
;
415 return devm_led_classdev_register(&hdev
->dev
, mute_led
);
418 static int elan_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
421 struct elan_drvdata
*drvdata
;
423 drvdata
= devm_kzalloc(&hdev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
428 hid_set_drvdata(hdev
, drvdata
);
430 ret
= hid_parse(hdev
);
432 hid_err(hdev
, "Hid Parse failed\n");
436 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
438 hid_err(hdev
, "Hid hw start failed\n");
442 if (is_not_elan_touchpad(hdev
))
445 if (!drvdata
->input
) {
446 hid_err(hdev
, "Input device is not registred\n");
451 ret
= elan_start_multitouch(hdev
);
455 if (id
->driver_data
& ELAN_HAS_LED
) {
456 ret
= elan_init_mute_led(hdev
);
467 static void elan_remove(struct hid_device
*hdev
)
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
},
479 MODULE_DEVICE_TABLE(hid
, elan_devices
);
481 static struct hid_driver elan_driver
= {
483 .id_table
= elan_devices
,
484 .input_mapping
= elan_input_mapping
,
485 .input_configured
= elan_input_configured
,
486 .raw_event
= elan_raw_event
,
488 .remove
= elan_remove
,
491 module_hid_driver(elan_driver
);
493 MODULE_LICENSE("GPL");
494 MODULE_AUTHOR("Alexandrov Stanislav");
495 MODULE_DESCRIPTION("Driver for HID ELAN Touchpads");