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
27 #define ELAN_MUTE_LED_REPORT 0xBC
28 #define ELAN_LED_REPORT_SIZE 8
30 struct elan_touchpad_settings
{
37 int usb_bInterfaceNumber
;
41 struct input_dev
*input
;
42 u8 prev_report
[ELAN_INPUT_REPORT_SIZE
];
43 struct led_classdev mute_led
;
45 struct elan_touchpad_settings
*settings
;
48 static int is_not_elan_touchpad(struct hid_device
*hdev
)
50 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
51 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
53 return (intf
->altsetting
->desc
.bInterfaceNumber
!= drvdata
->settings
->usb_bInterfaceNumber
);
56 static int elan_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
57 struct hid_field
*field
, struct hid_usage
*usage
,
58 unsigned long **bit
, int *max
)
60 if (is_not_elan_touchpad(hdev
))
63 if (field
->report
->id
== ELAN_SINGLE_FINGER
||
64 field
->report
->id
== ELAN_MT_FIRST_FINGER
||
65 field
->report
->id
== ELAN_MT_SECOND_FINGER
)
71 static int elan_input_configured(struct hid_device
*hdev
, struct hid_input
*hi
)
74 struct input_dev
*input
;
75 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
77 if (is_not_elan_touchpad(hdev
))
80 input
= devm_input_allocate_device(&hdev
->dev
);
84 input
->name
= "Elan Touchpad";
85 input
->phys
= hdev
->phys
;
86 input
->uniq
= hdev
->uniq
;
87 input
->id
.bustype
= hdev
->bus
;
88 input
->id
.vendor
= hdev
->vendor
;
89 input
->id
.product
= hdev
->product
;
90 input
->id
.version
= hdev
->version
;
91 input
->dev
.parent
= &hdev
->dev
;
93 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0,
94 drvdata
->settings
->max_x
, 0, 0);
95 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0,
96 drvdata
->settings
->max_y
, 0, 0);
97 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0,
98 drvdata
->settings
->max_fingers
, 0, 0);
99 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0,
100 drvdata
->settings
->max_w
, 0, 0);
102 __set_bit(BTN_LEFT
, input
->keybit
);
103 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
105 ret
= input_mt_init_slots(input
, drvdata
->settings
->max_fingers
,
108 hid_err(hdev
, "Failed to init elan MT slots: %d\n", ret
);
112 ret
= input_register_device(input
);
114 hid_err(hdev
, "Failed to register elan input device: %d\n",
116 input_free_device(input
);
120 drvdata
->input
= input
;
125 static void elan_report_mt_slot(struct elan_drvdata
*drvdata
, u8
*data
,
126 unsigned int slot_num
)
128 struct input_dev
*input
= drvdata
->input
;
131 bool active
= !!data
;
133 input_mt_slot(input
, slot_num
);
134 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, active
);
136 x
= ((data
[0] & 0xF0) << 4) | data
[1];
137 y
= drvdata
->settings
->max_y
-
138 (((data
[0] & 0x07) << 8) | data
[2]);
141 input_report_abs(input
, ABS_MT_POSITION_X
, x
);
142 input_report_abs(input
, ABS_MT_POSITION_Y
, y
);
143 input_report_abs(input
, ABS_TOOL_WIDTH
, w
);
147 static void elan_report_input(struct elan_drvdata
*drvdata
, u8
*data
)
150 struct input_dev
*input
= drvdata
->input
;
153 * There is 3 types of reports: for single touch,
154 * for multitouch - first finger and for multitouch - second finger
156 * packet structure for ELAN_SINGLE_FINGER and ELAN_MT_FIRST_FINGER:
158 * byte 1: 1 0 0 0 0 0 0 1 // 0x81 or 0x82
159 * byte 2: 0 0 0 0 0 0 0 0 // looks like unused
160 * byte 3: f5 f4 f3 f2 f1 0 0 L
161 * byte 4: x12 x11 x10 x9 0? y11 y10 y9
162 * byte 5: x8 x7 x6 x5 x4 x3 x2 x1
163 * byte 6: y8 y7 y6 y5 y4 y3 y2 y1
164 * byte 7: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
165 * byte 8: w8 w7 w6 w5 w4 w3 w2 w1
167 * packet structure for ELAN_MT_SECOND_FINGER:
169 * byte 1: 1 0 0 0 0 0 1 1 // 0x83
170 * byte 2: x12 x11 x10 x9 0 y11 y10 y9
171 * byte 3: x8 x7 x6 x5 x4 x3 x2 x1
172 * byte 4: y8 y7 y6 y5 y4 y3 y2 y1
173 * byte 5: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
174 * byte 6: w8 w7 w6 w5 w4 w3 w2 w1
175 * byte 7: 0 0 0 0 0 0 0 0
176 * byte 8: 0 0 0 0 0 0 0 0
178 * f5-f1: finger touch bits
180 * sy / sx: not sure yet, but this looks like rectangular
182 * w: looks like finger width
185 if (data
[0] == ELAN_SINGLE_FINGER
) {
186 for (i
= 0; i
< drvdata
->settings
->max_fingers
; i
++) {
187 if (data
[2] & BIT(i
+ 3))
188 elan_report_mt_slot(drvdata
, data
+ 3, i
);
190 elan_report_mt_slot(drvdata
, NULL
, i
);
192 input_report_key(input
, BTN_LEFT
, data
[2] & 0x01);
195 * When touched with two fingers Elan touchpad will emit two HID reports
196 * first is ELAN_MT_FIRST_FINGER and second is ELAN_MT_SECOND_FINGER
197 * we will save ELAN_MT_FIRST_FINGER report and wait for
198 * ELAN_MT_SECOND_FINGER to finish multitouch
200 if (data
[0] == ELAN_MT_FIRST_FINGER
) {
201 memcpy(drvdata
->prev_report
, data
,
202 sizeof(drvdata
->prev_report
));
206 if (data
[0] == ELAN_MT_SECOND_FINGER
) {
208 u8
*prev_report
= drvdata
->prev_report
;
210 if (prev_report
[0] != ELAN_MT_FIRST_FINGER
)
213 for (i
= 0; i
< drvdata
->settings
->max_fingers
; i
++) {
214 if (prev_report
[2] & BIT(i
+ 3)) {
217 elan_report_mt_slot(drvdata
, prev_report
+ 3, i
);
219 elan_report_mt_slot(drvdata
, data
+ 1, i
);
222 elan_report_mt_slot(drvdata
, NULL
, i
);
225 input_report_key(input
, BTN_LEFT
, prev_report
[2] & 0x01);
228 input_mt_sync_frame(input
);
232 static int elan_raw_event(struct hid_device
*hdev
,
233 struct hid_report
*report
, u8
*data
, int size
)
235 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
237 if (is_not_elan_touchpad(hdev
))
240 if (data
[0] == ELAN_SINGLE_FINGER
||
241 data
[0] == ELAN_MT_FIRST_FINGER
||
242 data
[0] == ELAN_MT_SECOND_FINGER
) {
243 if (size
== ELAN_INPUT_REPORT_SIZE
) {
244 elan_report_input(drvdata
, data
);
252 static int elan_start_multitouch(struct hid_device
*hdev
)
257 * This byte sequence will enable multitouch mode and disable
260 const unsigned char buf
[] = { 0x0D, 0x00, 0x03, 0x21, 0x00 };
261 unsigned char *dmabuf
= kmemdup(buf
, sizeof(buf
), GFP_KERNEL
);
266 ret
= hid_hw_raw_request(hdev
, dmabuf
[0], dmabuf
, sizeof(buf
),
267 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
271 if (ret
!= sizeof(buf
)) {
272 hid_err(hdev
, "Failed to start multitouch: %d\n", ret
);
279 static enum led_brightness
elan_mute_led_get_brigtness(struct led_classdev
*led_cdev
)
281 struct device
*dev
= led_cdev
->dev
->parent
;
282 struct hid_device
*hdev
= to_hid_device(dev
);
283 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
285 return drvdata
->mute_led_state
;
288 static int elan_mute_led_set_brigtness(struct led_classdev
*led_cdev
,
289 enum led_brightness value
)
293 struct device
*dev
= led_cdev
->dev
->parent
;
294 struct hid_device
*hdev
= to_hid_device(dev
);
295 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
297 unsigned char *dmabuf
= kzalloc(ELAN_LED_REPORT_SIZE
, GFP_KERNEL
);
304 dmabuf
[0] = ELAN_MUTE_LED_REPORT
;
306 dmabuf
[2] = led_state
;
308 ret
= hid_hw_raw_request(hdev
, dmabuf
[0], dmabuf
, ELAN_LED_REPORT_SIZE
,
309 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
313 if (ret
!= ELAN_LED_REPORT_SIZE
) {
314 hid_err(hdev
, "Failed to set mute led brightness: %d\n", ret
);
318 drvdata
->mute_led_state
= led_state
;
322 static int elan_init_mute_led(struct hid_device
*hdev
)
324 struct elan_drvdata
*drvdata
= hid_get_drvdata(hdev
);
325 struct led_classdev
*mute_led
= &drvdata
->mute_led
;
327 mute_led
->name
= "elan:red:mute";
328 mute_led
->brightness_get
= elan_mute_led_get_brigtness
;
329 mute_led
->brightness_set_blocking
= elan_mute_led_set_brigtness
;
330 mute_led
->max_brightness
= LED_ON
;
331 mute_led
->dev
= &hdev
->dev
;
333 return devm_led_classdev_register(&hdev
->dev
, mute_led
);
336 static int elan_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
339 struct elan_drvdata
*drvdata
;
341 drvdata
= devm_kzalloc(&hdev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
346 drvdata
->settings
= (struct elan_touchpad_settings
*)id
->driver_data
;
347 hid_set_drvdata(hdev
, drvdata
);
349 ret
= hid_parse(hdev
);
351 hid_err(hdev
, "Hid Parse failed\n");
355 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
357 hid_err(hdev
, "Hid hw start failed\n");
361 if (is_not_elan_touchpad(hdev
))
364 if (!drvdata
->input
) {
365 hid_err(hdev
, "Input device is not registred\n");
370 ret
= elan_start_multitouch(hdev
);
374 ret
= elan_init_mute_led(hdev
);
384 static void elan_remove(struct hid_device
*hdev
)
389 static const struct elan_touchpad_settings hp_x2_10_touchpad_data
= {
396 .usb_bInterfaceNumber
= 1,
399 static const struct hid_device_id elan_devices
[] = {
400 { HID_USB_DEVICE(USB_VENDOR_ID_ELAN
, USB_DEVICE_ID_HP_X2_10_COVER
),
401 (kernel_ulong_t
)&hp_x2_10_touchpad_data
},
405 MODULE_DEVICE_TABLE(hid
, elan_devices
);
407 static struct hid_driver elan_driver
= {
409 .id_table
= elan_devices
,
410 .input_mapping
= elan_input_mapping
,
411 .input_configured
= elan_input_configured
,
412 .raw_event
= elan_raw_event
,
414 .remove
= elan_remove
,
417 module_hid_driver(elan_driver
);
419 MODULE_LICENSE("GPL");
420 MODULE_AUTHOR("Alexandrov Stanislav");
421 MODULE_DESCRIPTION("Driver for HID ELAN Touchpads");