hid-elan.c 14 KB

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