hid-asus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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/dmi.h>
  28. #include <linux/hid.h>
  29. #include <linux/module.h>
  30. #include <linux/input/mt.h>
  31. #include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
  32. #include "hid-ids.h"
  33. MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
  34. MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
  35. MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
  36. MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
  37. MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
  38. #define T100_TPAD_INTF 2
  39. #define T100CHI_MOUSE_REPORT_ID 0x06
  40. #define FEATURE_REPORT_ID 0x0d
  41. #define INPUT_REPORT_ID 0x5d
  42. #define FEATURE_KBD_REPORT_ID 0x5a
  43. #define FEATURE_KBD_REPORT_SIZE 16
  44. #define SUPPORT_KBD_BACKLIGHT BIT(0)
  45. #define MAX_TOUCH_MAJOR 8
  46. #define MAX_PRESSURE 128
  47. #define BTN_LEFT_MASK 0x01
  48. #define CONTACT_TOOL_TYPE_MASK 0x80
  49. #define CONTACT_X_MSB_MASK 0xf0
  50. #define CONTACT_Y_MSB_MASK 0x0f
  51. #define CONTACT_TOUCH_MAJOR_MASK 0x07
  52. #define CONTACT_PRESSURE_MASK 0x7f
  53. #define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
  54. #define QUIRK_NO_INIT_REPORTS BIT(1)
  55. #define QUIRK_SKIP_INPUT_MAPPING BIT(2)
  56. #define QUIRK_IS_MULTITOUCH BIT(3)
  57. #define QUIRK_NO_CONSUMER_USAGES BIT(4)
  58. #define QUIRK_USE_KBD_BACKLIGHT BIT(5)
  59. #define QUIRK_T100_KEYBOARD BIT(6)
  60. #define QUIRK_T100CHI BIT(7)
  61. #define QUIRK_G752_KEYBOARD BIT(8)
  62. #define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
  63. QUIRK_NO_INIT_REPORTS | \
  64. QUIRK_NO_CONSUMER_USAGES)
  65. #define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
  66. QUIRK_SKIP_INPUT_MAPPING | \
  67. QUIRK_IS_MULTITOUCH)
  68. #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
  69. struct asus_kbd_leds {
  70. struct led_classdev cdev;
  71. struct hid_device *hdev;
  72. struct work_struct work;
  73. unsigned int brightness;
  74. bool removed;
  75. };
  76. struct asus_touchpad_info {
  77. int max_x;
  78. int max_y;
  79. int res_x;
  80. int res_y;
  81. int contact_size;
  82. int max_contacts;
  83. };
  84. struct asus_drvdata {
  85. unsigned long quirks;
  86. struct input_dev *input;
  87. struct asus_kbd_leds *kbd_backlight;
  88. const struct asus_touchpad_info *tp;
  89. bool enable_backlight;
  90. };
  91. static const struct asus_touchpad_info asus_i2c_tp = {
  92. .max_x = 2794,
  93. .max_y = 1758,
  94. .contact_size = 5,
  95. .max_contacts = 5,
  96. };
  97. static const struct asus_touchpad_info asus_t100ta_tp = {
  98. .max_x = 2240,
  99. .max_y = 1120,
  100. .res_x = 30, /* units/mm */
  101. .res_y = 27, /* units/mm */
  102. .contact_size = 5,
  103. .max_contacts = 5,
  104. };
  105. static const struct asus_touchpad_info asus_t100ha_tp = {
  106. .max_x = 2640,
  107. .max_y = 1320,
  108. .res_x = 30, /* units/mm */
  109. .res_y = 29, /* units/mm */
  110. .contact_size = 5,
  111. .max_contacts = 5,
  112. };
  113. static const struct asus_touchpad_info asus_t200ta_tp = {
  114. .max_x = 3120,
  115. .max_y = 1716,
  116. .res_x = 30, /* units/mm */
  117. .res_y = 28, /* units/mm */
  118. .contact_size = 5,
  119. .max_contacts = 5,
  120. };
  121. static const struct asus_touchpad_info asus_t100chi_tp = {
  122. .max_x = 2640,
  123. .max_y = 1320,
  124. .res_x = 31, /* units/mm */
  125. .res_y = 29, /* units/mm */
  126. .contact_size = 3,
  127. .max_contacts = 4,
  128. };
  129. static void asus_report_contact_down(struct asus_drvdata *drvdat,
  130. int toolType, u8 *data)
  131. {
  132. struct input_dev *input = drvdat->input;
  133. int touch_major, pressure, x, y;
  134. x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
  135. y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
  136. input_report_abs(input, ABS_MT_POSITION_X, x);
  137. input_report_abs(input, ABS_MT_POSITION_Y, y);
  138. if (drvdat->tp->contact_size < 5)
  139. return;
  140. if (toolType == MT_TOOL_PALM) {
  141. touch_major = MAX_TOUCH_MAJOR;
  142. pressure = MAX_PRESSURE;
  143. } else {
  144. touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
  145. pressure = data[4] & CONTACT_PRESSURE_MASK;
  146. }
  147. input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
  148. input_report_abs(input, ABS_MT_PRESSURE, pressure);
  149. }
  150. /* Required for Synaptics Palm Detection */
  151. static void asus_report_tool_width(struct asus_drvdata *drvdat)
  152. {
  153. struct input_mt *mt = drvdat->input->mt;
  154. struct input_mt_slot *oldest;
  155. int oldid, count, i;
  156. if (drvdat->tp->contact_size < 5)
  157. return;
  158. oldest = NULL;
  159. oldid = mt->trkid;
  160. count = 0;
  161. for (i = 0; i < mt->num_slots; ++i) {
  162. struct input_mt_slot *ps = &mt->slots[i];
  163. int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
  164. if (id < 0)
  165. continue;
  166. if ((id - oldid) & TRKID_SGN) {
  167. oldest = ps;
  168. oldid = id;
  169. }
  170. count++;
  171. }
  172. if (oldest) {
  173. input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
  174. input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
  175. }
  176. }
  177. static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
  178. {
  179. int i, toolType = MT_TOOL_FINGER;
  180. u8 *contactData = data + 2;
  181. if (size != 3 + drvdat->tp->contact_size * drvdat->tp->max_contacts)
  182. return 0;
  183. for (i = 0; i < drvdat->tp->max_contacts; i++) {
  184. bool down = !!(data[1] & BIT(i+3));
  185. if (drvdat->tp->contact_size >= 5)
  186. toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
  187. MT_TOOL_PALM : MT_TOOL_FINGER;
  188. input_mt_slot(drvdat->input, i);
  189. input_mt_report_slot_state(drvdat->input, toolType, down);
  190. if (down) {
  191. asus_report_contact_down(drvdat, toolType, contactData);
  192. contactData += drvdat->tp->contact_size;
  193. }
  194. }
  195. input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
  196. asus_report_tool_width(drvdat);
  197. input_mt_sync_frame(drvdat->input);
  198. input_sync(drvdat->input);
  199. return 1;
  200. }
  201. static int asus_raw_event(struct hid_device *hdev,
  202. struct hid_report *report, u8 *data, int size)
  203. {
  204. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  205. if (drvdata->tp && data[0] == INPUT_REPORT_ID)
  206. return asus_report_input(drvdata, data, size);
  207. return 0;
  208. }
  209. static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
  210. {
  211. unsigned char *dmabuf;
  212. int ret;
  213. dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
  214. if (!dmabuf)
  215. return -ENOMEM;
  216. ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
  217. buf_size, HID_FEATURE_REPORT,
  218. HID_REQ_SET_REPORT);
  219. kfree(dmabuf);
  220. return ret;
  221. }
  222. static int asus_kbd_init(struct hid_device *hdev)
  223. {
  224. u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
  225. 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
  226. int ret;
  227. ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
  228. if (ret < 0)
  229. hid_err(hdev, "Asus failed to send init command: %d\n", ret);
  230. return ret;
  231. }
  232. static int asus_kbd_get_functions(struct hid_device *hdev,
  233. unsigned char *kbd_func)
  234. {
  235. u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
  236. u8 *readbuf;
  237. int ret;
  238. ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
  239. if (ret < 0) {
  240. hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
  241. return ret;
  242. }
  243. readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
  244. if (!readbuf)
  245. return -ENOMEM;
  246. ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
  247. FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
  248. HID_REQ_GET_REPORT);
  249. if (ret < 0) {
  250. hid_err(hdev, "Asus failed to request functions: %d\n", ret);
  251. kfree(readbuf);
  252. return ret;
  253. }
  254. *kbd_func = readbuf[6];
  255. kfree(readbuf);
  256. return ret;
  257. }
  258. static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
  259. enum led_brightness brightness)
  260. {
  261. struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
  262. cdev);
  263. if (led->brightness == brightness)
  264. return;
  265. led->brightness = brightness;
  266. schedule_work(&led->work);
  267. }
  268. static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
  269. {
  270. struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
  271. cdev);
  272. return led->brightness;
  273. }
  274. static void asus_kbd_backlight_work(struct work_struct *work)
  275. {
  276. struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
  277. u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
  278. int ret;
  279. if (led->removed)
  280. return;
  281. buf[4] = led->brightness;
  282. ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
  283. if (ret < 0)
  284. hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
  285. }
  286. static int asus_kbd_register_leds(struct hid_device *hdev)
  287. {
  288. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  289. unsigned char kbd_func;
  290. int ret;
  291. /* Initialize keyboard */
  292. ret = asus_kbd_init(hdev);
  293. if (ret < 0)
  294. return ret;
  295. /* Get keyboard functions */
  296. ret = asus_kbd_get_functions(hdev, &kbd_func);
  297. if (ret < 0)
  298. return ret;
  299. /* Check for backlight support */
  300. if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
  301. return -ENODEV;
  302. drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
  303. sizeof(struct asus_kbd_leds),
  304. GFP_KERNEL);
  305. if (!drvdata->kbd_backlight)
  306. return -ENOMEM;
  307. drvdata->kbd_backlight->removed = false;
  308. drvdata->kbd_backlight->brightness = 0;
  309. drvdata->kbd_backlight->hdev = hdev;
  310. drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
  311. drvdata->kbd_backlight->cdev.max_brightness = 3;
  312. drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
  313. drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
  314. INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
  315. ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
  316. if (ret < 0) {
  317. /* No need to have this still around */
  318. devm_kfree(&hdev->dev, drvdata->kbd_backlight);
  319. }
  320. return ret;
  321. }
  322. static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
  323. {
  324. struct input_dev *input = hi->input;
  325. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  326. /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
  327. if (drvdata->quirks & QUIRK_T100CHI &&
  328. hi->report->id != T100CHI_MOUSE_REPORT_ID)
  329. return 0;
  330. if (drvdata->tp) {
  331. int ret;
  332. input_set_abs_params(input, ABS_MT_POSITION_X, 0,
  333. drvdata->tp->max_x, 0, 0);
  334. input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
  335. drvdata->tp->max_y, 0, 0);
  336. input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
  337. input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
  338. if (drvdata->tp->contact_size >= 5) {
  339. input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
  340. MAX_TOUCH_MAJOR, 0, 0);
  341. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
  342. MAX_TOUCH_MAJOR, 0, 0);
  343. input_set_abs_params(input, ABS_MT_PRESSURE, 0,
  344. MAX_PRESSURE, 0, 0);
  345. }
  346. __set_bit(BTN_LEFT, input->keybit);
  347. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  348. ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
  349. INPUT_MT_POINTER);
  350. if (ret) {
  351. hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
  352. return ret;
  353. }
  354. }
  355. drvdata->input = input;
  356. if (drvdata->enable_backlight && asus_kbd_register_leds(hdev))
  357. hid_warn(hdev, "Failed to initialize backlight.\n");
  358. return 0;
  359. }
  360. #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
  361. max, EV_KEY, (c))
  362. static int asus_input_mapping(struct hid_device *hdev,
  363. struct hid_input *hi, struct hid_field *field,
  364. struct hid_usage *usage, unsigned long **bit,
  365. int *max)
  366. {
  367. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  368. if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
  369. /* Don't map anything from the HID report.
  370. * We do it all manually in asus_input_configured
  371. */
  372. return -1;
  373. }
  374. /*
  375. * Ignore a bunch of bogus collections in the T100CHI descriptor.
  376. * This avoids a bunch of non-functional hid_input devices getting
  377. * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
  378. */
  379. if (drvdata->quirks & QUIRK_T100CHI) {
  380. if (field->application == (HID_UP_GENDESK | 0x0080) ||
  381. usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
  382. usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
  383. usage->hid == (HID_UP_GENDEVCTRLS | 0x0026))
  384. return -1;
  385. /*
  386. * We use the hid_input for the mouse report for the touchpad,
  387. * keep the left button, to avoid the core removing it.
  388. */
  389. if (field->application == HID_GD_MOUSE &&
  390. usage->hid != (HID_UP_BUTTON | 1))
  391. return -1;
  392. }
  393. /* ASUS-specific keyboard hotkeys */
  394. if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
  395. set_bit(EV_REP, hi->input->evbit);
  396. switch (usage->hid & HID_USAGE) {
  397. case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
  398. case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
  399. case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
  400. case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
  401. case 0x82: asus_map_key_clear(KEY_CAMERA); break;
  402. case 0x88: asus_map_key_clear(KEY_RFKILL); break;
  403. case 0xb5: asus_map_key_clear(KEY_CALC); break;
  404. case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
  405. case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
  406. /* ASUS touchpad toggle */
  407. case 0x6b: asus_map_key_clear(KEY_F21); break;
  408. /* ROG key */
  409. case 0x38: asus_map_key_clear(KEY_PROG1); break;
  410. /* Fn+C ASUS Splendid */
  411. case 0xba: asus_map_key_clear(KEY_PROG2); break;
  412. /* Fn+Space Power4Gear Hybrid */
  413. case 0x5c: asus_map_key_clear(KEY_PROG3); break;
  414. default:
  415. /* ASUS lazily declares 256 usages, ignore the rest,
  416. * as some make the keyboard appear as a pointer device. */
  417. return -1;
  418. }
  419. /*
  420. * Check and enable backlight only on devices with UsagePage ==
  421. * 0xff31 to avoid initializing the keyboard firmware multiple
  422. * times on devices with multiple HID descriptors but same
  423. * PID/VID.
  424. */
  425. if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
  426. drvdata->enable_backlight = true;
  427. return 1;
  428. }
  429. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
  430. set_bit(EV_REP, hi->input->evbit);
  431. switch (usage->hid & HID_USAGE) {
  432. case 0xff01: asus_map_key_clear(BTN_1); break;
  433. case 0xff02: asus_map_key_clear(BTN_2); break;
  434. case 0xff03: asus_map_key_clear(BTN_3); break;
  435. case 0xff04: asus_map_key_clear(BTN_4); break;
  436. case 0xff05: asus_map_key_clear(BTN_5); break;
  437. case 0xff06: asus_map_key_clear(BTN_6); break;
  438. case 0xff07: asus_map_key_clear(BTN_7); break;
  439. case 0xff08: asus_map_key_clear(BTN_8); break;
  440. case 0xff09: asus_map_key_clear(BTN_9); break;
  441. case 0xff0a: asus_map_key_clear(BTN_A); break;
  442. case 0xff0b: asus_map_key_clear(BTN_B); break;
  443. case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
  444. case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
  445. case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
  446. case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
  447. case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
  448. case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
  449. default:
  450. return 0;
  451. }
  452. return 1;
  453. }
  454. if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
  455. (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
  456. switch (usage->hid & HID_USAGE) {
  457. case 0xe2: /* Mute */
  458. case 0xe9: /* Volume up */
  459. case 0xea: /* Volume down */
  460. return 0;
  461. default:
  462. /* Ignore dummy Consumer usages which make the
  463. * keyboard incorrectly appear as a pointer device.
  464. */
  465. return -1;
  466. }
  467. }
  468. return 0;
  469. }
  470. static int asus_start_multitouch(struct hid_device *hdev)
  471. {
  472. int ret;
  473. static const unsigned char buf[] = {
  474. FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
  475. };
  476. unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
  477. if (!dmabuf) {
  478. ret = -ENOMEM;
  479. hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
  480. return ret;
  481. }
  482. ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
  483. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  484. kfree(dmabuf);
  485. if (ret != sizeof(buf)) {
  486. hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
  487. return ret;
  488. }
  489. return 0;
  490. }
  491. static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
  492. {
  493. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  494. if (drvdata->tp)
  495. return asus_start_multitouch(hdev);
  496. return 0;
  497. }
  498. static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
  499. {
  500. int ret;
  501. struct asus_drvdata *drvdata;
  502. drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
  503. if (drvdata == NULL) {
  504. hid_err(hdev, "Can't alloc Asus descriptor\n");
  505. return -ENOMEM;
  506. }
  507. hid_set_drvdata(hdev, drvdata);
  508. drvdata->quirks = id->driver_data;
  509. if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
  510. drvdata->tp = &asus_i2c_tp;
  511. if (drvdata->quirks & QUIRK_T100_KEYBOARD) {
  512. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  513. if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
  514. drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
  515. /*
  516. * The T100HA uses the same USB-ids as the T100TAF and
  517. * the T200TA uses the same USB-ids as the T100TA, while
  518. * both have different max x/y values as the T100TA[F].
  519. */
  520. if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
  521. drvdata->tp = &asus_t100ha_tp;
  522. else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
  523. drvdata->tp = &asus_t200ta_tp;
  524. else
  525. drvdata->tp = &asus_t100ta_tp;
  526. }
  527. }
  528. if (drvdata->quirks & QUIRK_T100CHI) {
  529. /*
  530. * All functionality is on a single HID interface and for
  531. * userspace the touchpad must be a separate input_dev.
  532. */
  533. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  534. drvdata->tp = &asus_t100chi_tp;
  535. }
  536. if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
  537. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  538. ret = hid_parse(hdev);
  539. if (ret) {
  540. hid_err(hdev, "Asus hid parse failed: %d\n", ret);
  541. return ret;
  542. }
  543. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  544. if (ret) {
  545. hid_err(hdev, "Asus hw start failed: %d\n", ret);
  546. return ret;
  547. }
  548. if (!drvdata->input) {
  549. hid_err(hdev, "Asus input not registered\n");
  550. ret = -ENOMEM;
  551. goto err_stop_hw;
  552. }
  553. if (drvdata->tp) {
  554. drvdata->input->name = "Asus TouchPad";
  555. } else {
  556. drvdata->input->name = "Asus Keyboard";
  557. }
  558. if (drvdata->tp) {
  559. ret = asus_start_multitouch(hdev);
  560. if (ret)
  561. goto err_stop_hw;
  562. }
  563. return 0;
  564. err_stop_hw:
  565. hid_hw_stop(hdev);
  566. return ret;
  567. }
  568. static void asus_remove(struct hid_device *hdev)
  569. {
  570. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  571. if (drvdata->kbd_backlight) {
  572. drvdata->kbd_backlight->removed = true;
  573. cancel_work_sync(&drvdata->kbd_backlight->work);
  574. }
  575. hid_hw_stop(hdev);
  576. }
  577. static const __u8 asus_g752_fixed_rdesc[] = {
  578. 0x19, 0x00, /* Usage Minimum (0x00) */
  579. 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
  580. };
  581. static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  582. unsigned int *rsize)
  583. {
  584. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  585. if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
  586. *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
  587. hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
  588. rdesc[55] = 0xdd;
  589. }
  590. /* For the T100TA/T200TA keyboard dock */
  591. if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
  592. (*rsize == 76 || *rsize == 101) &&
  593. rdesc[73] == 0x81 && rdesc[74] == 0x01) {
  594. hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
  595. rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
  596. }
  597. /* For the T100CHI keyboard dock */
  598. if (drvdata->quirks & QUIRK_T100CHI &&
  599. *rsize == 403 && rdesc[388] == 0x09 && rdesc[389] == 0x76) {
  600. /*
  601. * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
  602. * (FFh) and clear the flags in the Input() byte.
  603. * Note the descriptor has a bogus 0 byte at the end so we
  604. * only need 1 extra byte.
  605. */
  606. *rsize = 404;
  607. rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
  608. if (!rdesc)
  609. return NULL;
  610. hid_info(hdev, "Fixing up T100CHI keyb report descriptor\n");
  611. memmove(rdesc + 392, rdesc + 390, 12);
  612. rdesc[388] = 0x19;
  613. rdesc[389] = 0x00;
  614. rdesc[390] = 0x29;
  615. rdesc[391] = 0xff;
  616. rdesc[402] = 0x00;
  617. }
  618. if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
  619. *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
  620. /* report is missing usage mninum and maximum */
  621. __u8 *new_rdesc;
  622. size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
  623. new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
  624. if (new_rdesc == NULL)
  625. return rdesc;
  626. hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
  627. /* copy the valid part */
  628. memcpy(new_rdesc, rdesc, 61);
  629. /* insert missing part */
  630. memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
  631. /* copy remaining data */
  632. memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
  633. *rsize = new_size;
  634. rdesc = new_rdesc;
  635. }
  636. return rdesc;
  637. }
  638. static const struct hid_device_id asus_devices[] = {
  639. { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
  640. USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
  641. { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
  642. USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
  643. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  644. USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
  645. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  646. USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
  647. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  648. USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
  649. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  650. USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
  651. QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
  652. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  653. USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
  654. QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
  655. { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
  656. { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
  657. { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
  658. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
  659. USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
  660. { }
  661. };
  662. MODULE_DEVICE_TABLE(hid, asus_devices);
  663. static struct hid_driver asus_driver = {
  664. .name = "asus",
  665. .id_table = asus_devices,
  666. .report_fixup = asus_report_fixup,
  667. .probe = asus_probe,
  668. .remove = asus_remove,
  669. .input_mapping = asus_input_mapping,
  670. .input_configured = asus_input_configured,
  671. #ifdef CONFIG_PM
  672. .reset_resume = asus_reset_resume,
  673. #endif
  674. .raw_event = asus_raw_event
  675. };
  676. module_hid_driver(asus_driver);
  677. MODULE_LICENSE("GPL");