hid-elan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. return ret;
  160. }
  161. input_abs_set_res(input, ABS_X, drvdata->res_x);
  162. input_abs_set_res(input, ABS_Y, drvdata->res_y);
  163. ret = input_register_device(input);
  164. if (ret) {
  165. hid_err(hdev, "Failed to register elan input device: %d\n",
  166. ret);
  167. input_free_device(input);
  168. return ret;
  169. }
  170. drvdata->input = input;
  171. return 0;
  172. }
  173. static void elan_report_mt_slot(struct elan_drvdata *drvdata, u8 *data,
  174. unsigned int slot_num)
  175. {
  176. struct input_dev *input = drvdata->input;
  177. int x, y, p;
  178. bool active = !!data;
  179. input_mt_slot(input, slot_num);
  180. input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
  181. if (active) {
  182. x = ((data[0] & 0xF0) << 4) | data[1];
  183. y = drvdata->max_y -
  184. (((data[0] & 0x07) << 8) | data[2]);
  185. p = data[4];
  186. input_report_abs(input, ABS_MT_POSITION_X, x);
  187. input_report_abs(input, ABS_MT_POSITION_Y, y);
  188. input_report_abs(input, ABS_MT_PRESSURE, p);
  189. }
  190. }
  191. static void elan_usb_report_input(struct elan_drvdata *drvdata, u8 *data)
  192. {
  193. int i;
  194. struct input_dev *input = drvdata->input;
  195. /*
  196. * There is 3 types of reports: for single touch,
  197. * for multitouch - first finger and for multitouch - second finger
  198. *
  199. * packet structure for ELAN_SINGLE_FINGER and ELAN_MT_FIRST_FINGER:
  200. *
  201. * byte 1: 1 0 0 0 0 0 0 1 // 0x81 or 0x82
  202. * byte 2: 0 0 0 0 0 0 0 0 // looks like unused
  203. * byte 3: f5 f4 f3 f2 f1 0 0 L
  204. * byte 4: x12 x11 x10 x9 0? y11 y10 y9
  205. * byte 5: x8 x7 x6 x5 x4 x3 x2 x1
  206. * byte 6: y8 y7 y6 y5 y4 y3 y2 y1
  207. * byte 7: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
  208. * byte 8: p8 p7 p6 p5 p4 p3 p2 p1
  209. *
  210. * packet structure for ELAN_MT_SECOND_FINGER:
  211. *
  212. * byte 1: 1 0 0 0 0 0 1 1 // 0x83
  213. * byte 2: x12 x11 x10 x9 0 y11 y10 y9
  214. * byte 3: x8 x7 x6 x5 x4 x3 x2 x1
  215. * byte 4: y8 y7 y6 y5 y4 y3 y2 y1
  216. * byte 5: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
  217. * byte 6: p8 p7 p6 p5 p4 p3 p2 p1
  218. * byte 7: 0 0 0 0 0 0 0 0
  219. * byte 8: 0 0 0 0 0 0 0 0
  220. *
  221. * f5-f1: finger touch bits
  222. * L: clickpad button
  223. * sy / sx: finger width / height expressed in traces, the total number
  224. * of traces can be queried by doing a HID_REQ_SET_REPORT
  225. * { 0x0d, 0x05, 0x03, 0x05, 0x01 } followed by a GET, in the
  226. * returned buf, buf[3]=no-x-traces, buf[4]=no-y-traces.
  227. * p: pressure
  228. */
  229. if (data[0] == ELAN_SINGLE_FINGER) {
  230. for (i = 0; i < ELAN_MAX_FINGERS; i++) {
  231. if (data[2] & BIT(i + 3))
  232. elan_report_mt_slot(drvdata, data + 3, i);
  233. else
  234. elan_report_mt_slot(drvdata, NULL, i);
  235. }
  236. input_report_key(input, BTN_LEFT, data[2] & 0x01);
  237. }
  238. /*
  239. * When touched with two fingers Elan touchpad will emit two HID reports
  240. * first is ELAN_MT_FIRST_FINGER and second is ELAN_MT_SECOND_FINGER
  241. * we will save ELAN_MT_FIRST_FINGER report and wait for
  242. * ELAN_MT_SECOND_FINGER to finish multitouch
  243. */
  244. if (data[0] == ELAN_MT_FIRST_FINGER) {
  245. memcpy(drvdata->prev_report, data,
  246. sizeof(drvdata->prev_report));
  247. return;
  248. }
  249. if (data[0] == ELAN_MT_SECOND_FINGER) {
  250. int first = 0;
  251. u8 *prev_report = drvdata->prev_report;
  252. if (prev_report[0] != ELAN_MT_FIRST_FINGER)
  253. return;
  254. for (i = 0; i < ELAN_MAX_FINGERS; i++) {
  255. if (prev_report[2] & BIT(i + 3)) {
  256. if (!first) {
  257. first = 1;
  258. elan_report_mt_slot(drvdata, prev_report + 3, i);
  259. } else {
  260. elan_report_mt_slot(drvdata, data + 1, i);
  261. }
  262. } else {
  263. elan_report_mt_slot(drvdata, NULL, i);
  264. }
  265. }
  266. input_report_key(input, BTN_LEFT, prev_report[2] & 0x01);
  267. }
  268. input_mt_sync_frame(input);
  269. input_sync(input);
  270. }
  271. static void elan_i2c_report_input(struct elan_drvdata *drvdata, u8 *data)
  272. {
  273. struct input_dev *input = drvdata->input;
  274. u8 *finger_data;
  275. int i;
  276. /*
  277. * Elan MT touchpads in i2c mode send finger data in the same format
  278. * as in USB mode, but then with all fingers in a single packet.
  279. *
  280. * packet structure for ELAN_MT_I2C:
  281. *
  282. * byte 1: 1 0 0 1 1 1 0 1 // 0x5d
  283. * byte 2: f5 f4 f3 f2 f1 0 0 L
  284. * byte 3: x12 x11 x10 x9 0? y11 y10 y9
  285. * byte 4: x8 x7 x6 x5 x4 x3 x2 x1
  286. * byte 5: y8 y7 y6 y5 y4 y3 y2 y1
  287. * byte 6: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
  288. * byte 7: p8 p7 p6 p5 p4 p3 p2 p1
  289. * byte 8-12: Same as byte 3-7 for second finger down
  290. * byte 13-17: Same as byte 3-7 for third finger down
  291. * byte 18-22: Same as byte 3-7 for fourth finger down
  292. * byte 23-27: Same as byte 3-7 for fifth finger down
  293. */
  294. finger_data = data + 2;
  295. for (i = 0; i < ELAN_MAX_FINGERS; i++) {
  296. if (data[1] & BIT(i + 3)) {
  297. elan_report_mt_slot(drvdata, finger_data, i);
  298. finger_data += ELAN_FINGER_DATA_LEN;
  299. } else {
  300. elan_report_mt_slot(drvdata, NULL, i);
  301. }
  302. }
  303. input_report_key(input, BTN_LEFT, data[1] & 0x01);
  304. input_mt_sync_frame(input);
  305. input_sync(input);
  306. }
  307. static int elan_raw_event(struct hid_device *hdev,
  308. struct hid_report *report, u8 *data, int size)
  309. {
  310. struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
  311. if (is_not_elan_touchpad(hdev))
  312. return 0;
  313. if (data[0] == ELAN_SINGLE_FINGER ||
  314. data[0] == ELAN_MT_FIRST_FINGER ||
  315. data[0] == ELAN_MT_SECOND_FINGER) {
  316. if (size == ELAN_INPUT_REPORT_SIZE) {
  317. elan_usb_report_input(drvdata, data);
  318. return 1;
  319. }
  320. }
  321. if (data[0] == ELAN_MT_I2C && size == ELAN_I2C_REPORT_SIZE) {
  322. elan_i2c_report_input(drvdata, data);
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. static int elan_start_multitouch(struct hid_device *hdev)
  328. {
  329. int ret;
  330. /*
  331. * This byte sequence will enable multitouch mode and disable
  332. * mouse emulation
  333. */
  334. const unsigned char buf[] = { 0x0D, 0x00, 0x03, 0x21, 0x00 };
  335. unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
  336. if (!dmabuf)
  337. return -ENOMEM;
  338. ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
  339. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  340. kfree(dmabuf);
  341. if (ret != sizeof(buf)) {
  342. hid_err(hdev, "Failed to start multitouch: %d\n", ret);
  343. return ret;
  344. }
  345. return 0;
  346. }
  347. static enum led_brightness elan_mute_led_get_brigtness(struct led_classdev *led_cdev)
  348. {
  349. struct device *dev = led_cdev->dev->parent;
  350. struct hid_device *hdev = to_hid_device(dev);
  351. struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
  352. return drvdata->mute_led_state;
  353. }
  354. static int elan_mute_led_set_brigtness(struct led_classdev *led_cdev,
  355. enum led_brightness value)
  356. {
  357. int ret;
  358. u8 led_state;
  359. struct device *dev = led_cdev->dev->parent;
  360. struct hid_device *hdev = to_hid_device(dev);
  361. struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
  362. unsigned char *dmabuf = kzalloc(ELAN_LED_REPORT_SIZE, GFP_KERNEL);
  363. if (!dmabuf)
  364. return -ENOMEM;
  365. led_state = !!value;
  366. dmabuf[0] = ELAN_MUTE_LED_REPORT;
  367. dmabuf[1] = 0x02;
  368. dmabuf[2] = led_state;
  369. ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, ELAN_LED_REPORT_SIZE,
  370. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  371. kfree(dmabuf);
  372. if (ret != ELAN_LED_REPORT_SIZE) {
  373. hid_err(hdev, "Failed to set mute led brightness: %d\n", ret);
  374. return ret;
  375. }
  376. drvdata->mute_led_state = led_state;
  377. return 0;
  378. }
  379. static int elan_init_mute_led(struct hid_device *hdev)
  380. {
  381. struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
  382. struct led_classdev *mute_led = &drvdata->mute_led;
  383. mute_led->name = "elan:red:mute";
  384. mute_led->brightness_get = elan_mute_led_get_brigtness;
  385. mute_led->brightness_set_blocking = elan_mute_led_set_brigtness;
  386. mute_led->max_brightness = LED_ON;
  387. mute_led->dev = &hdev->dev;
  388. return devm_led_classdev_register(&hdev->dev, mute_led);
  389. }
  390. static int elan_probe(struct hid_device *hdev, const struct hid_device_id *id)
  391. {
  392. int ret;
  393. struct elan_drvdata *drvdata;
  394. drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
  395. if (!drvdata)
  396. return -ENOMEM;
  397. hid_set_drvdata(hdev, drvdata);
  398. ret = hid_parse(hdev);
  399. if (ret) {
  400. hid_err(hdev, "Hid Parse failed\n");
  401. return ret;
  402. }
  403. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  404. if (ret) {
  405. hid_err(hdev, "Hid hw start failed\n");
  406. return ret;
  407. }
  408. if (is_not_elan_touchpad(hdev))
  409. return 0;
  410. if (!drvdata->input) {
  411. hid_err(hdev, "Input device is not registred\n");
  412. ret = -ENAVAIL;
  413. goto err;
  414. }
  415. ret = elan_start_multitouch(hdev);
  416. if (ret)
  417. goto err;
  418. if (id->driver_data & ELAN_HAS_LED) {
  419. ret = elan_init_mute_led(hdev);
  420. if (ret)
  421. goto err;
  422. }
  423. return 0;
  424. err:
  425. hid_hw_stop(hdev);
  426. return ret;
  427. }
  428. static void elan_remove(struct hid_device *hdev)
  429. {
  430. hid_hw_stop(hdev);
  431. }
  432. static const struct hid_device_id elan_devices[] = {
  433. { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2),
  434. .driver_data = ELAN_HAS_LED },
  435. { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2_10_COVER),
  436. .driver_data = ELAN_HAS_LED },
  437. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_TOSHIBA_CLICK_L9W) },
  438. { }
  439. };
  440. MODULE_DEVICE_TABLE(hid, elan_devices);
  441. static struct hid_driver elan_driver = {
  442. .name = "elan",
  443. .id_table = elan_devices,
  444. .input_mapping = elan_input_mapping,
  445. .input_configured = elan_input_configured,
  446. .raw_event = elan_raw_event,
  447. .probe = elan_probe,
  448. .remove = elan_remove,
  449. };
  450. module_hid_driver(elan_driver);
  451. MODULE_LICENSE("GPL");
  452. MODULE_AUTHOR("Alexandrov Stanislav");
  453. MODULE_DESCRIPTION("Driver for HID ELAN Touchpads");