hid-asus.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * HID driver for Asus notebook built-in keyboard.
  3. * Fixes small logical maximum to match usage maximum.
  4. *
  5. * Currently supported devices are:
  6. * EeeBook X205TA
  7. * VivoBook E200HA
  8. *
  9. * Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
  10. *
  11. * This module based on hid-ortek by
  12. * Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
  13. * Copyright (c) 2011 Jiri Kosina
  14. *
  15. * This module has been updated to add support for Asus i2c touchpad.
  16. *
  17. * Copyright (c) 2016 Brendan McGrath <redmcg@redmandi.dyndns.org>
  18. * Copyright (c) 2016 Victor Vlasenko <victor.vlasenko@sysgears.com>
  19. * Copyright (c) 2016 Frederik Wenigwieser <frederik.wenigwieser@gmail.com>
  20. */
  21. /*
  22. * This program is free software; you can redistribute it and/or modify it
  23. * under the terms of the GNU General Public License as published by the Free
  24. * Software Foundation; either version 2 of the License, or (at your option)
  25. * any later version.
  26. */
  27. #include <linux/hid.h>
  28. #include <linux/module.h>
  29. #include <linux/input/mt.h>
  30. #include "hid-ids.h"
  31. MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
  32. MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
  33. MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
  34. MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
  35. MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
  36. #define FEATURE_REPORT_ID 0x0d
  37. #define INPUT_REPORT_ID 0x5d
  38. #define INPUT_REPORT_SIZE 28
  39. #define MAX_CONTACTS 5
  40. #define MAX_X 2794
  41. #define MAX_Y 1758
  42. #define MAX_TOUCH_MAJOR 8
  43. #define MAX_PRESSURE 128
  44. #define CONTACT_DATA_SIZE 5
  45. #define BTN_LEFT_MASK 0x01
  46. #define CONTACT_TOOL_TYPE_MASK 0x80
  47. #define CONTACT_X_MSB_MASK 0xf0
  48. #define CONTACT_Y_MSB_MASK 0x0f
  49. #define CONTACT_TOUCH_MAJOR_MASK 0x07
  50. #define CONTACT_PRESSURE_MASK 0x7f
  51. #define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
  52. #define QUIRK_NO_INIT_REPORTS BIT(1)
  53. #define QUIRK_SKIP_INPUT_MAPPING BIT(2)
  54. #define QUIRK_IS_MULTITOUCH BIT(3)
  55. #define KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
  56. QUIRK_NO_INIT_REPORTS)
  57. #define TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
  58. QUIRK_SKIP_INPUT_MAPPING | \
  59. QUIRK_IS_MULTITOUCH)
  60. #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
  61. struct asus_drvdata {
  62. unsigned long quirks;
  63. struct input_dev *input;
  64. };
  65. static void asus_report_contact_down(struct input_dev *input,
  66. int toolType, u8 *data)
  67. {
  68. int touch_major, pressure;
  69. int x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
  70. int y = MAX_Y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
  71. if (toolType == MT_TOOL_PALM) {
  72. touch_major = MAX_TOUCH_MAJOR;
  73. pressure = MAX_PRESSURE;
  74. } else {
  75. touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
  76. pressure = data[4] & CONTACT_PRESSURE_MASK;
  77. }
  78. input_report_abs(input, ABS_MT_POSITION_X, x);
  79. input_report_abs(input, ABS_MT_POSITION_Y, y);
  80. input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
  81. input_report_abs(input, ABS_MT_PRESSURE, pressure);
  82. }
  83. /* Required for Synaptics Palm Detection */
  84. static void asus_report_tool_width(struct input_dev *input)
  85. {
  86. struct input_mt *mt = input->mt;
  87. struct input_mt_slot *oldest;
  88. int oldid, count, i;
  89. oldest = NULL;
  90. oldid = mt->trkid;
  91. count = 0;
  92. for (i = 0; i < mt->num_slots; ++i) {
  93. struct input_mt_slot *ps = &mt->slots[i];
  94. int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
  95. if (id < 0)
  96. continue;
  97. if ((id - oldid) & TRKID_SGN) {
  98. oldest = ps;
  99. oldid = id;
  100. }
  101. count++;
  102. }
  103. if (oldest) {
  104. input_report_abs(input, ABS_TOOL_WIDTH,
  105. input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
  106. }
  107. }
  108. static void asus_report_input(struct input_dev *input, u8 *data)
  109. {
  110. int i;
  111. u8 *contactData = data + 2;
  112. for (i = 0; i < MAX_CONTACTS; i++) {
  113. bool down = !!(data[1] & BIT(i+3));
  114. int toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
  115. MT_TOOL_PALM : MT_TOOL_FINGER;
  116. input_mt_slot(input, i);
  117. input_mt_report_slot_state(input, toolType, down);
  118. if (down) {
  119. asus_report_contact_down(input, toolType, contactData);
  120. contactData += CONTACT_DATA_SIZE;
  121. }
  122. }
  123. input_report_key(input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
  124. asus_report_tool_width(input);
  125. input_mt_sync_frame(input);
  126. input_sync(input);
  127. }
  128. static int asus_raw_event(struct hid_device *hdev,
  129. struct hid_report *report, u8 *data, int size)
  130. {
  131. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  132. if (drvdata->quirks & QUIRK_IS_MULTITOUCH &&
  133. data[0] == INPUT_REPORT_ID &&
  134. size == INPUT_REPORT_SIZE) {
  135. asus_report_input(drvdata->input, data);
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
  141. {
  142. struct input_dev *input = hi->input;
  143. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  144. if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
  145. int ret;
  146. input_set_abs_params(input, ABS_MT_POSITION_X, 0, MAX_X, 0, 0);
  147. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, MAX_Y, 0, 0);
  148. input_set_abs_params(input, ABS_TOOL_WIDTH, 0, MAX_TOUCH_MAJOR, 0, 0);
  149. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, MAX_TOUCH_MAJOR, 0, 0);
  150. input_set_abs_params(input, ABS_MT_PRESSURE, 0, MAX_PRESSURE, 0, 0);
  151. __set_bit(BTN_LEFT, input->keybit);
  152. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  153. ret = input_mt_init_slots(input, MAX_CONTACTS, INPUT_MT_POINTER);
  154. if (ret) {
  155. hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
  156. return ret;
  157. }
  158. }
  159. drvdata->input = input;
  160. return 0;
  161. }
  162. static int asus_input_mapping(struct hid_device *hdev,
  163. struct hid_input *hi, struct hid_field *field,
  164. struct hid_usage *usage, unsigned long **bit,
  165. int *max)
  166. {
  167. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  168. if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
  169. /* Don't map anything from the HID report.
  170. * We do it all manually in asus_input_configured
  171. */
  172. return -1;
  173. }
  174. return 0;
  175. }
  176. static int asus_start_multitouch(struct hid_device *hdev)
  177. {
  178. int ret;
  179. const unsigned char buf[] = { FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00 };
  180. unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
  181. if (!dmabuf) {
  182. ret = -ENOMEM;
  183. hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
  184. return ret;
  185. }
  186. ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
  187. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  188. kfree(dmabuf);
  189. if (ret != sizeof(buf)) {
  190. hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
  196. {
  197. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  198. if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
  199. return asus_start_multitouch(hdev);
  200. return 0;
  201. }
  202. static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
  203. {
  204. int ret;
  205. struct asus_drvdata *drvdata;
  206. drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
  207. if (drvdata == NULL) {
  208. hid_err(hdev, "Can't alloc Asus descriptor\n");
  209. return -ENOMEM;
  210. }
  211. hid_set_drvdata(hdev, drvdata);
  212. drvdata->quirks = id->driver_data;
  213. if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
  214. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  215. ret = hid_parse(hdev);
  216. if (ret) {
  217. hid_err(hdev, "Asus hid parse failed: %d\n", ret);
  218. return ret;
  219. }
  220. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  221. if (ret) {
  222. hid_err(hdev, "Asus hw start failed: %d\n", ret);
  223. return ret;
  224. }
  225. if (!drvdata->input) {
  226. hid_err(hdev, "Asus input not registered\n");
  227. ret = -ENOMEM;
  228. goto err_stop_hw;
  229. }
  230. if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
  231. drvdata->input->name = "Asus TouchPad";
  232. } else {
  233. drvdata->input->name = "Asus Keyboard";
  234. }
  235. if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
  236. ret = asus_start_multitouch(hdev);
  237. if (ret)
  238. goto err_stop_hw;
  239. }
  240. return 0;
  241. err_stop_hw:
  242. hid_hw_stop(hdev);
  243. return ret;
  244. }
  245. static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  246. unsigned int *rsize)
  247. {
  248. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  249. if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
  250. *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
  251. hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
  252. rdesc[55] = 0xdd;
  253. }
  254. return rdesc;
  255. }
  256. static const struct hid_device_id asus_devices[] = {
  257. { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
  258. USB_DEVICE_ID_ASUSTEK_NOTEBOOK_KEYBOARD), KEYBOARD_QUIRKS},
  259. { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
  260. USB_DEVICE_ID_ASUSTEK_TOUCHPAD), TOUCHPAD_QUIRKS },
  261. { }
  262. };
  263. MODULE_DEVICE_TABLE(hid, asus_devices);
  264. static struct hid_driver asus_driver = {
  265. .name = "asus",
  266. .id_table = asus_devices,
  267. .report_fixup = asus_report_fixup,
  268. .probe = asus_probe,
  269. .input_mapping = asus_input_mapping,
  270. .input_configured = asus_input_configured,
  271. #ifdef CONFIG_PM
  272. .reset_resume = asus_reset_resume,
  273. #endif
  274. .raw_event = asus_raw_event
  275. };
  276. module_hid_driver(asus_driver);
  277. MODULE_LICENSE("GPL");