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