hid-asus.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for Asus notebook built-in keyboard.
  4. * Fixes small logical maximum to match usage maximum.
  5. *
  6. * Currently supported devices are:
  7. * EeeBook X205TA
  8. * VivoBook E200HA
  9. *
  10. * Copyright (c) 2016 Yusuke Fujimaki <[email protected]>
  11. *
  12. * This module based on hid-ortek by
  13. * Copyright (c) 2010 Johnathon Harris <[email protected]>
  14. * Copyright (c) 2011 Jiri Kosina
  15. *
  16. * This module has been updated to add support for Asus i2c touchpad.
  17. *
  18. * Copyright (c) 2016 Brendan McGrath <[email protected]>
  19. * Copyright (c) 2016 Victor Vlasenko <[email protected]>
  20. * Copyright (c) 2016 Frederik Wenigwieser <[email protected]>
  21. */
  22. /*
  23. */
  24. #include <linux/dmi.h>
  25. #include <linux/hid.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_data/x86/asus-wmi.h>
  28. #include <linux/input/mt.h>
  29. #include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
  30. #include <linux/power_supply.h>
  31. #include "hid-ids.h"
  32. MODULE_AUTHOR("Yusuke Fujimaki <[email protected]>");
  33. MODULE_AUTHOR("Brendan McGrath <[email protected]>");
  34. MODULE_AUTHOR("Victor Vlasenko <[email protected]>");
  35. MODULE_AUTHOR("Frederik Wenigwieser <[email protected]>");
  36. MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
  37. #define T100_TPAD_INTF 2
  38. #define MEDION_E1239T_TPAD_INTF 1
  39. #define E1239T_TP_TOGGLE_REPORT_ID 0x05
  40. #define T100CHI_MOUSE_REPORT_ID 0x06
  41. #define FEATURE_REPORT_ID 0x0d
  42. #define INPUT_REPORT_ID 0x5d
  43. #define FEATURE_KBD_REPORT_ID 0x5a
  44. #define FEATURE_KBD_REPORT_SIZE 16
  45. #define FEATURE_KBD_LED_REPORT_ID1 0x5d
  46. #define FEATURE_KBD_LED_REPORT_ID2 0x5e
  47. #define SUPPORT_KBD_BACKLIGHT BIT(0)
  48. #define MAX_TOUCH_MAJOR 8
  49. #define MAX_PRESSURE 128
  50. #define BTN_LEFT_MASK 0x01
  51. #define CONTACT_TOOL_TYPE_MASK 0x80
  52. #define CONTACT_X_MSB_MASK 0xf0
  53. #define CONTACT_Y_MSB_MASK 0x0f
  54. #define CONTACT_TOUCH_MAJOR_MASK 0x07
  55. #define CONTACT_PRESSURE_MASK 0x7f
  56. #define BATTERY_REPORT_ID (0x03)
  57. #define BATTERY_REPORT_SIZE (1 + 8)
  58. #define BATTERY_LEVEL_MAX ((u8)255)
  59. #define BATTERY_STAT_DISCONNECT (0)
  60. #define BATTERY_STAT_CHARGING (1)
  61. #define BATTERY_STAT_FULL (2)
  62. #define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
  63. #define QUIRK_NO_INIT_REPORTS BIT(1)
  64. #define QUIRK_SKIP_INPUT_MAPPING BIT(2)
  65. #define QUIRK_IS_MULTITOUCH BIT(3)
  66. #define QUIRK_NO_CONSUMER_USAGES BIT(4)
  67. #define QUIRK_USE_KBD_BACKLIGHT BIT(5)
  68. #define QUIRK_T100_KEYBOARD BIT(6)
  69. #define QUIRK_T100CHI BIT(7)
  70. #define QUIRK_G752_KEYBOARD BIT(8)
  71. #define QUIRK_T90CHI BIT(9)
  72. #define QUIRK_MEDION_E1239T BIT(10)
  73. #define QUIRK_ROG_NKEY_KEYBOARD BIT(11)
  74. #define QUIRK_ROG_CLAYMORE_II_KEYBOARD BIT(12)
  75. #define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
  76. QUIRK_NO_INIT_REPORTS | \
  77. QUIRK_NO_CONSUMER_USAGES)
  78. #define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
  79. QUIRK_SKIP_INPUT_MAPPING | \
  80. QUIRK_IS_MULTITOUCH)
  81. #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
  82. struct asus_kbd_leds {
  83. struct led_classdev cdev;
  84. struct hid_device *hdev;
  85. struct work_struct work;
  86. unsigned int brightness;
  87. spinlock_t lock;
  88. bool removed;
  89. };
  90. struct asus_touchpad_info {
  91. int max_x;
  92. int max_y;
  93. int res_x;
  94. int res_y;
  95. int contact_size;
  96. int max_contacts;
  97. int report_size;
  98. };
  99. struct asus_drvdata {
  100. unsigned long quirks;
  101. struct hid_device *hdev;
  102. struct input_dev *input;
  103. struct input_dev *tp_kbd_input;
  104. struct asus_kbd_leds *kbd_backlight;
  105. const struct asus_touchpad_info *tp;
  106. bool enable_backlight;
  107. struct power_supply *battery;
  108. struct power_supply_desc battery_desc;
  109. int battery_capacity;
  110. int battery_stat;
  111. bool battery_in_query;
  112. unsigned long battery_next_query;
  113. };
  114. static int asus_report_battery(struct asus_drvdata *, u8 *, int);
  115. static const struct asus_touchpad_info asus_i2c_tp = {
  116. .max_x = 2794,
  117. .max_y = 1758,
  118. .contact_size = 5,
  119. .max_contacts = 5,
  120. .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
  121. };
  122. static const struct asus_touchpad_info asus_t100ta_tp = {
  123. .max_x = 2240,
  124. .max_y = 1120,
  125. .res_x = 30, /* units/mm */
  126. .res_y = 27, /* units/mm */
  127. .contact_size = 5,
  128. .max_contacts = 5,
  129. .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
  130. };
  131. static const struct asus_touchpad_info asus_t100ha_tp = {
  132. .max_x = 2640,
  133. .max_y = 1320,
  134. .res_x = 30, /* units/mm */
  135. .res_y = 29, /* units/mm */
  136. .contact_size = 5,
  137. .max_contacts = 5,
  138. .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
  139. };
  140. static const struct asus_touchpad_info asus_t200ta_tp = {
  141. .max_x = 3120,
  142. .max_y = 1716,
  143. .res_x = 30, /* units/mm */
  144. .res_y = 28, /* units/mm */
  145. .contact_size = 5,
  146. .max_contacts = 5,
  147. .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
  148. };
  149. static const struct asus_touchpad_info asus_t100chi_tp = {
  150. .max_x = 2640,
  151. .max_y = 1320,
  152. .res_x = 31, /* units/mm */
  153. .res_y = 29, /* units/mm */
  154. .contact_size = 3,
  155. .max_contacts = 4,
  156. .report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
  157. };
  158. static const struct asus_touchpad_info medion_e1239t_tp = {
  159. .max_x = 2640,
  160. .max_y = 1380,
  161. .res_x = 29, /* units/mm */
  162. .res_y = 28, /* units/mm */
  163. .contact_size = 5,
  164. .max_contacts = 5,
  165. .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
  166. };
  167. static void asus_report_contact_down(struct asus_drvdata *drvdat,
  168. int toolType, u8 *data)
  169. {
  170. struct input_dev *input = drvdat->input;
  171. int touch_major, pressure, x, y;
  172. x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
  173. y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
  174. input_report_abs(input, ABS_MT_POSITION_X, x);
  175. input_report_abs(input, ABS_MT_POSITION_Y, y);
  176. if (drvdat->tp->contact_size < 5)
  177. return;
  178. if (toolType == MT_TOOL_PALM) {
  179. touch_major = MAX_TOUCH_MAJOR;
  180. pressure = MAX_PRESSURE;
  181. } else {
  182. touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
  183. pressure = data[4] & CONTACT_PRESSURE_MASK;
  184. }
  185. input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
  186. input_report_abs(input, ABS_MT_PRESSURE, pressure);
  187. }
  188. /* Required for Synaptics Palm Detection */
  189. static void asus_report_tool_width(struct asus_drvdata *drvdat)
  190. {
  191. struct input_mt *mt = drvdat->input->mt;
  192. struct input_mt_slot *oldest;
  193. int oldid, i;
  194. if (drvdat->tp->contact_size < 5)
  195. return;
  196. oldest = NULL;
  197. oldid = mt->trkid;
  198. for (i = 0; i < mt->num_slots; ++i) {
  199. struct input_mt_slot *ps = &mt->slots[i];
  200. int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
  201. if (id < 0)
  202. continue;
  203. if ((id - oldid) & TRKID_SGN) {
  204. oldest = ps;
  205. oldid = id;
  206. }
  207. }
  208. if (oldest) {
  209. input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
  210. input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
  211. }
  212. }
  213. static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
  214. {
  215. int i, toolType = MT_TOOL_FINGER;
  216. u8 *contactData = data + 2;
  217. if (size != drvdat->tp->report_size)
  218. return 0;
  219. for (i = 0; i < drvdat->tp->max_contacts; i++) {
  220. bool down = !!(data[1] & BIT(i+3));
  221. if (drvdat->tp->contact_size >= 5)
  222. toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
  223. MT_TOOL_PALM : MT_TOOL_FINGER;
  224. input_mt_slot(drvdat->input, i);
  225. input_mt_report_slot_state(drvdat->input, toolType, down);
  226. if (down) {
  227. asus_report_contact_down(drvdat, toolType, contactData);
  228. contactData += drvdat->tp->contact_size;
  229. }
  230. }
  231. input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
  232. asus_report_tool_width(drvdat);
  233. input_mt_sync_frame(drvdat->input);
  234. input_sync(drvdat->input);
  235. return 1;
  236. }
  237. static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size)
  238. {
  239. if (size != 3)
  240. return 0;
  241. /* Handle broken mute key which only sends press events */
  242. if (!drvdat->tp &&
  243. data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) {
  244. input_report_key(drvdat->input, KEY_MUTE, 1);
  245. input_sync(drvdat->input);
  246. input_report_key(drvdat->input, KEY_MUTE, 0);
  247. input_sync(drvdat->input);
  248. return 1;
  249. }
  250. /* Handle custom touchpad toggle key which only sends press events */
  251. if (drvdat->tp_kbd_input &&
  252. data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) {
  253. input_report_key(drvdat->tp_kbd_input, KEY_F21, 1);
  254. input_sync(drvdat->tp_kbd_input);
  255. input_report_key(drvdat->tp_kbd_input, KEY_F21, 0);
  256. input_sync(drvdat->tp_kbd_input);
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. static int asus_event(struct hid_device *hdev, struct hid_field *field,
  262. struct hid_usage *usage, __s32 value)
  263. {
  264. if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
  265. (usage->hid & HID_USAGE) != 0x00 &&
  266. (usage->hid & HID_USAGE) != 0xff && !usage->type) {
  267. hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
  268. usage->hid & HID_USAGE);
  269. }
  270. return 0;
  271. }
  272. static int asus_raw_event(struct hid_device *hdev,
  273. struct hid_report *report, u8 *data, int size)
  274. {
  275. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  276. if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
  277. return asus_report_battery(drvdata, data, size);
  278. if (drvdata->tp && data[0] == INPUT_REPORT_ID)
  279. return asus_report_input(drvdata, data, size);
  280. if (drvdata->quirks & QUIRK_MEDION_E1239T)
  281. return asus_e1239t_event(drvdata, data, size);
  282. if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) {
  283. /*
  284. * Skip these report ID, the device emits a continuous stream associated
  285. * with the AURA mode it is in which looks like an 'echo'.
  286. */
  287. if (report->id == FEATURE_KBD_LED_REPORT_ID1 ||
  288. report->id == FEATURE_KBD_LED_REPORT_ID2) {
  289. return -1;
  290. /* Additional report filtering */
  291. } else if (report->id == FEATURE_KBD_REPORT_ID) {
  292. /*
  293. * G14 and G15 send these codes on some keypresses with no
  294. * discernable reason for doing so. We'll filter them out to avoid
  295. * unmapped warning messages later.
  296. */
  297. if (data[1] == 0xea || data[1] == 0xec || data[1] == 0x02 ||
  298. data[1] == 0x8a || data[1] == 0x9e) {
  299. return -1;
  300. }
  301. }
  302. if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
  303. /*
  304. * G713 and G733 send these codes on some keypresses, depending on
  305. * the key pressed it can trigger a shutdown event if not caught.
  306. */
  307. if(data[0] == 0x02 && data[1] == 0x30) {
  308. return -1;
  309. }
  310. }
  311. }
  312. if (drvdata->quirks & QUIRK_ROG_CLAYMORE_II_KEYBOARD) {
  313. /*
  314. * CLAYMORE II keyboard sends this packet when it goes to sleep
  315. * this causes the whole system to go into suspend.
  316. */
  317. if(size == 2 && data[0] == 0x02 && data[1] == 0x00) {
  318. return -1;
  319. }
  320. }
  321. return 0;
  322. }
  323. static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
  324. {
  325. unsigned char *dmabuf;
  326. int ret;
  327. dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
  328. if (!dmabuf)
  329. return -ENOMEM;
  330. /*
  331. * The report ID should be set from the incoming buffer due to LED and key
  332. * interfaces having different pages
  333. */
  334. ret = hid_hw_raw_request(hdev, buf[0], dmabuf,
  335. buf_size, HID_FEATURE_REPORT,
  336. HID_REQ_SET_REPORT);
  337. kfree(dmabuf);
  338. return ret;
  339. }
  340. static int asus_kbd_init(struct hid_device *hdev)
  341. {
  342. u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
  343. 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
  344. int ret;
  345. ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
  346. if (ret < 0)
  347. hid_err(hdev, "Asus failed to send init command: %d\n", ret);
  348. return ret;
  349. }
  350. static int asus_kbd_get_functions(struct hid_device *hdev,
  351. unsigned char *kbd_func)
  352. {
  353. u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
  354. u8 *readbuf;
  355. int ret;
  356. ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
  357. if (ret < 0) {
  358. hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
  359. return ret;
  360. }
  361. readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
  362. if (!readbuf)
  363. return -ENOMEM;
  364. ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
  365. FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
  366. HID_REQ_GET_REPORT);
  367. if (ret < 0) {
  368. hid_err(hdev, "Asus failed to request functions: %d\n", ret);
  369. kfree(readbuf);
  370. return ret;
  371. }
  372. *kbd_func = readbuf[6];
  373. kfree(readbuf);
  374. return ret;
  375. }
  376. static int rog_nkey_led_init(struct hid_device *hdev)
  377. {
  378. u8 buf_init_start[] = { FEATURE_KBD_LED_REPORT_ID1, 0xB9 };
  379. u8 buf_init2[] = { FEATURE_KBD_LED_REPORT_ID1, 0x41, 0x53, 0x55, 0x53, 0x20,
  380. 0x54, 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
  381. u8 buf_init3[] = { FEATURE_KBD_LED_REPORT_ID1,
  382. 0x05, 0x20, 0x31, 0x00, 0x08 };
  383. int ret;
  384. hid_info(hdev, "Asus initialise N-KEY Device");
  385. /* The first message is an init start */
  386. ret = asus_kbd_set_report(hdev, buf_init_start, sizeof(buf_init_start));
  387. if (ret < 0) {
  388. hid_warn(hdev, "Asus failed to send init start command: %d\n", ret);
  389. return ret;
  390. }
  391. /* Followed by a string */
  392. ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
  393. if (ret < 0) {
  394. hid_warn(hdev, "Asus failed to send init command 1.0: %d\n", ret);
  395. return ret;
  396. }
  397. /* Followed by a string */
  398. ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
  399. if (ret < 0) {
  400. hid_warn(hdev, "Asus failed to send init command 1.1: %d\n", ret);
  401. return ret;
  402. }
  403. /* begin second report ID with same data */
  404. buf_init2[0] = FEATURE_KBD_LED_REPORT_ID2;
  405. buf_init3[0] = FEATURE_KBD_LED_REPORT_ID2;
  406. ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
  407. if (ret < 0) {
  408. hid_warn(hdev, "Asus failed to send init command 2.0: %d\n", ret);
  409. return ret;
  410. }
  411. ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
  412. if (ret < 0)
  413. hid_warn(hdev, "Asus failed to send init command 2.1: %d\n", ret);
  414. return ret;
  415. }
  416. static void asus_schedule_work(struct asus_kbd_leds *led)
  417. {
  418. unsigned long flags;
  419. spin_lock_irqsave(&led->lock, flags);
  420. if (!led->removed)
  421. schedule_work(&led->work);
  422. spin_unlock_irqrestore(&led->lock, flags);
  423. }
  424. static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
  425. enum led_brightness brightness)
  426. {
  427. struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
  428. cdev);
  429. unsigned long flags;
  430. spin_lock_irqsave(&led->lock, flags);
  431. led->brightness = brightness;
  432. spin_unlock_irqrestore(&led->lock, flags);
  433. asus_schedule_work(led);
  434. }
  435. static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
  436. {
  437. struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
  438. cdev);
  439. enum led_brightness brightness;
  440. unsigned long flags;
  441. spin_lock_irqsave(&led->lock, flags);
  442. brightness = led->brightness;
  443. spin_unlock_irqrestore(&led->lock, flags);
  444. return brightness;
  445. }
  446. static void asus_kbd_backlight_work(struct work_struct *work)
  447. {
  448. struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
  449. u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
  450. int ret;
  451. unsigned long flags;
  452. spin_lock_irqsave(&led->lock, flags);
  453. buf[4] = led->brightness;
  454. spin_unlock_irqrestore(&led->lock, flags);
  455. ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
  456. if (ret < 0)
  457. hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
  458. }
  459. /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
  460. * precedence. We only activate HID-based backlight control when the
  461. * WMI control is not available.
  462. */
  463. static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
  464. {
  465. u32 value;
  466. int ret;
  467. if (!IS_ENABLED(CONFIG_ASUS_WMI))
  468. return false;
  469. ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
  470. ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
  471. hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
  472. if (ret)
  473. return false;
  474. return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
  475. }
  476. static int asus_kbd_register_leds(struct hid_device *hdev)
  477. {
  478. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  479. unsigned char kbd_func;
  480. int ret;
  481. if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
  482. ret = rog_nkey_led_init(hdev);
  483. if (ret < 0)
  484. return ret;
  485. } else {
  486. /* Initialize keyboard */
  487. ret = asus_kbd_init(hdev);
  488. if (ret < 0)
  489. return ret;
  490. /* Get keyboard functions */
  491. ret = asus_kbd_get_functions(hdev, &kbd_func);
  492. if (ret < 0)
  493. return ret;
  494. /* Check for backlight support */
  495. if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
  496. return -ENODEV;
  497. }
  498. drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
  499. sizeof(struct asus_kbd_leds),
  500. GFP_KERNEL);
  501. if (!drvdata->kbd_backlight)
  502. return -ENOMEM;
  503. drvdata->kbd_backlight->removed = false;
  504. drvdata->kbd_backlight->brightness = 0;
  505. drvdata->kbd_backlight->hdev = hdev;
  506. drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
  507. drvdata->kbd_backlight->cdev.max_brightness = 3;
  508. drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
  509. drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
  510. INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
  511. spin_lock_init(&drvdata->kbd_backlight->lock);
  512. ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
  513. if (ret < 0) {
  514. /* No need to have this still around */
  515. devm_kfree(&hdev->dev, drvdata->kbd_backlight);
  516. }
  517. return ret;
  518. }
  519. /*
  520. * [0] REPORT_ID (same value defined in report descriptor)
  521. * [1] rest battery level. range [0..255]
  522. * [2]..[7] Bluetooth hardware address (MAC address)
  523. * [8] charging status
  524. * = 0 : AC offline / discharging
  525. * = 1 : AC online / charging
  526. * = 2 : AC online / fully charged
  527. */
  528. static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
  529. {
  530. u8 sts;
  531. u8 lvl;
  532. int val;
  533. lvl = data[1];
  534. sts = data[8];
  535. drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
  536. switch (sts) {
  537. case BATTERY_STAT_CHARGING:
  538. val = POWER_SUPPLY_STATUS_CHARGING;
  539. break;
  540. case BATTERY_STAT_FULL:
  541. val = POWER_SUPPLY_STATUS_FULL;
  542. break;
  543. case BATTERY_STAT_DISCONNECT:
  544. default:
  545. val = POWER_SUPPLY_STATUS_DISCHARGING;
  546. break;
  547. }
  548. drvdata->battery_stat = val;
  549. return 0;
  550. }
  551. static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
  552. {
  553. /* notify only the autonomous event by device */
  554. if ((drvdata->battery_in_query == false) &&
  555. (size == BATTERY_REPORT_SIZE))
  556. power_supply_changed(drvdata->battery);
  557. return 0;
  558. }
  559. static int asus_battery_query(struct asus_drvdata *drvdata)
  560. {
  561. u8 *buf;
  562. int ret = 0;
  563. buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
  564. if (!buf)
  565. return -ENOMEM;
  566. drvdata->battery_in_query = true;
  567. ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
  568. buf, BATTERY_REPORT_SIZE,
  569. HID_INPUT_REPORT, HID_REQ_GET_REPORT);
  570. drvdata->battery_in_query = false;
  571. if (ret == BATTERY_REPORT_SIZE)
  572. ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
  573. else
  574. ret = -ENODATA;
  575. kfree(buf);
  576. return ret;
  577. }
  578. static enum power_supply_property asus_battery_props[] = {
  579. POWER_SUPPLY_PROP_STATUS,
  580. POWER_SUPPLY_PROP_PRESENT,
  581. POWER_SUPPLY_PROP_CAPACITY,
  582. POWER_SUPPLY_PROP_SCOPE,
  583. POWER_SUPPLY_PROP_MODEL_NAME,
  584. };
  585. #define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */
  586. static int asus_battery_get_property(struct power_supply *psy,
  587. enum power_supply_property psp,
  588. union power_supply_propval *val)
  589. {
  590. struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
  591. int ret = 0;
  592. switch (psp) {
  593. case POWER_SUPPLY_PROP_STATUS:
  594. case POWER_SUPPLY_PROP_CAPACITY:
  595. if (time_before(drvdata->battery_next_query, jiffies)) {
  596. drvdata->battery_next_query =
  597. jiffies + QUERY_MIN_INTERVAL;
  598. ret = asus_battery_query(drvdata);
  599. if (ret)
  600. return ret;
  601. }
  602. if (psp == POWER_SUPPLY_PROP_STATUS)
  603. val->intval = drvdata->battery_stat;
  604. else
  605. val->intval = drvdata->battery_capacity;
  606. break;
  607. case POWER_SUPPLY_PROP_PRESENT:
  608. val->intval = 1;
  609. break;
  610. case POWER_SUPPLY_PROP_SCOPE:
  611. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  612. break;
  613. case POWER_SUPPLY_PROP_MODEL_NAME:
  614. val->strval = drvdata->hdev->name;
  615. break;
  616. default:
  617. ret = -EINVAL;
  618. break;
  619. }
  620. return ret;
  621. }
  622. static int asus_battery_probe(struct hid_device *hdev)
  623. {
  624. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  625. struct power_supply_config pscfg = { .drv_data = drvdata };
  626. int ret = 0;
  627. drvdata->battery_capacity = 0;
  628. drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
  629. drvdata->battery_in_query = false;
  630. drvdata->battery_desc.properties = asus_battery_props;
  631. drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
  632. drvdata->battery_desc.get_property = asus_battery_get_property;
  633. drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  634. drvdata->battery_desc.use_for_apm = 0;
  635. drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  636. "asus-keyboard-%s-battery",
  637. strlen(hdev->uniq) ?
  638. hdev->uniq : dev_name(&hdev->dev));
  639. if (!drvdata->battery_desc.name)
  640. return -ENOMEM;
  641. drvdata->battery_next_query = jiffies;
  642. drvdata->battery = devm_power_supply_register(&hdev->dev,
  643. &(drvdata->battery_desc), &pscfg);
  644. if (IS_ERR(drvdata->battery)) {
  645. ret = PTR_ERR(drvdata->battery);
  646. drvdata->battery = NULL;
  647. hid_err(hdev, "Unable to register battery device\n");
  648. return ret;
  649. }
  650. power_supply_powers(drvdata->battery, &hdev->dev);
  651. return ret;
  652. }
  653. static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
  654. {
  655. struct input_dev *input = hi->input;
  656. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  657. /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
  658. if (drvdata->quirks & QUIRK_T100CHI &&
  659. hi->report->id != T100CHI_MOUSE_REPORT_ID)
  660. return 0;
  661. /* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
  662. if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
  663. switch (hi->report->id) {
  664. case E1239T_TP_TOGGLE_REPORT_ID:
  665. input_set_capability(input, EV_KEY, KEY_F21);
  666. input->name = "Asus Touchpad Keys";
  667. drvdata->tp_kbd_input = input;
  668. return 0;
  669. case INPUT_REPORT_ID:
  670. break; /* Touchpad report, handled below */
  671. default:
  672. return 0; /* Ignore other reports */
  673. }
  674. }
  675. if (drvdata->tp) {
  676. int ret;
  677. input_set_abs_params(input, ABS_MT_POSITION_X, 0,
  678. drvdata->tp->max_x, 0, 0);
  679. input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
  680. drvdata->tp->max_y, 0, 0);
  681. input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
  682. input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
  683. if (drvdata->tp->contact_size >= 5) {
  684. input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
  685. MAX_TOUCH_MAJOR, 0, 0);
  686. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
  687. MAX_TOUCH_MAJOR, 0, 0);
  688. input_set_abs_params(input, ABS_MT_PRESSURE, 0,
  689. MAX_PRESSURE, 0, 0);
  690. }
  691. __set_bit(BTN_LEFT, input->keybit);
  692. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  693. ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
  694. INPUT_MT_POINTER);
  695. if (ret) {
  696. hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
  697. return ret;
  698. }
  699. }
  700. drvdata->input = input;
  701. if (drvdata->enable_backlight &&
  702. !asus_kbd_wmi_led_control_present(hdev) &&
  703. asus_kbd_register_leds(hdev))
  704. hid_warn(hdev, "Failed to initialize backlight.\n");
  705. return 0;
  706. }
  707. #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
  708. max, EV_KEY, (c))
  709. static int asus_input_mapping(struct hid_device *hdev,
  710. struct hid_input *hi, struct hid_field *field,
  711. struct hid_usage *usage, unsigned long **bit,
  712. int *max)
  713. {
  714. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  715. if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
  716. /* Don't map anything from the HID report.
  717. * We do it all manually in asus_input_configured
  718. */
  719. return -1;
  720. }
  721. /*
  722. * Ignore a bunch of bogus collections in the T100CHI descriptor.
  723. * This avoids a bunch of non-functional hid_input devices getting
  724. * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
  725. */
  726. if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
  727. (field->application == (HID_UP_GENDESK | 0x0080) ||
  728. field->application == HID_GD_MOUSE ||
  729. usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
  730. usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
  731. usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
  732. return -1;
  733. /* ASUS-specific keyboard hotkeys and led backlight */
  734. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ASUSVENDOR) {
  735. switch (usage->hid & HID_USAGE) {
  736. case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
  737. case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
  738. case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
  739. case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
  740. case 0x7c: asus_map_key_clear(KEY_MICMUTE); break;
  741. case 0x82: asus_map_key_clear(KEY_CAMERA); break;
  742. case 0x88: asus_map_key_clear(KEY_RFKILL); break;
  743. case 0xb5: asus_map_key_clear(KEY_CALC); break;
  744. case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
  745. case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
  746. /* ASUS touchpad toggle */
  747. case 0x6b: asus_map_key_clear(KEY_F21); break;
  748. /* ROG key */
  749. case 0x38: asus_map_key_clear(KEY_PROG1); break;
  750. /* Fn+C ASUS Splendid */
  751. case 0xba: asus_map_key_clear(KEY_PROG2); break;
  752. /* Fn+Space Power4Gear Hybrid */
  753. case 0x5c: asus_map_key_clear(KEY_PROG3); break;
  754. /* Fn+F5 "fan" symbol on FX503VD */
  755. case 0x99: asus_map_key_clear(KEY_PROG4); break;
  756. /* Fn+F5 "fan" symbol on N-Key keyboard */
  757. case 0xae: asus_map_key_clear(KEY_PROG4); break;
  758. /* Fn+Ret "Calc" symbol on N-Key keyboard */
  759. case 0x92: asus_map_key_clear(KEY_CALC); break;
  760. /* Fn+Left Aura mode previous on N-Key keyboard */
  761. case 0xb2: asus_map_key_clear(KEY_PROG2); break;
  762. /* Fn+Right Aura mode next on N-Key keyboard */
  763. case 0xb3: asus_map_key_clear(KEY_PROG3); break;
  764. default:
  765. /* ASUS lazily declares 256 usages, ignore the rest,
  766. * as some make the keyboard appear as a pointer device. */
  767. return -1;
  768. }
  769. /*
  770. * Check and enable backlight only on devices with UsagePage ==
  771. * 0xff31 to avoid initializing the keyboard firmware multiple
  772. * times on devices with multiple HID descriptors but same
  773. * PID/VID.
  774. */
  775. if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
  776. drvdata->enable_backlight = true;
  777. set_bit(EV_REP, hi->input->evbit);
  778. return 1;
  779. }
  780. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
  781. switch (usage->hid & HID_USAGE) {
  782. case 0xff01: asus_map_key_clear(BTN_1); break;
  783. case 0xff02: asus_map_key_clear(BTN_2); break;
  784. case 0xff03: asus_map_key_clear(BTN_3); break;
  785. case 0xff04: asus_map_key_clear(BTN_4); break;
  786. case 0xff05: asus_map_key_clear(BTN_5); break;
  787. case 0xff06: asus_map_key_clear(BTN_6); break;
  788. case 0xff07: asus_map_key_clear(BTN_7); break;
  789. case 0xff08: asus_map_key_clear(BTN_8); break;
  790. case 0xff09: asus_map_key_clear(BTN_9); break;
  791. case 0xff0a: asus_map_key_clear(BTN_A); break;
  792. case 0xff0b: asus_map_key_clear(BTN_B); break;
  793. case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
  794. case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
  795. case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
  796. case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
  797. case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
  798. case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
  799. default:
  800. return 0;
  801. }
  802. set_bit(EV_REP, hi->input->evbit);
  803. return 1;
  804. }
  805. if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
  806. (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
  807. switch (usage->hid & HID_USAGE) {
  808. case 0xe2: /* Mute */
  809. case 0xe9: /* Volume up */
  810. case 0xea: /* Volume down */
  811. return 0;
  812. default:
  813. /* Ignore dummy Consumer usages which make the
  814. * keyboard incorrectly appear as a pointer device.
  815. */
  816. return -1;
  817. }
  818. }
  819. /*
  820. * The mute button is broken and only sends press events, we
  821. * deal with this in our raw_event handler, so do not map it.
  822. */
  823. if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
  824. usage->hid == (HID_UP_CONSUMER | 0xe2)) {
  825. input_set_capability(hi->input, EV_KEY, KEY_MUTE);
  826. return -1;
  827. }
  828. return 0;
  829. }
  830. static int asus_start_multitouch(struct hid_device *hdev)
  831. {
  832. int ret;
  833. static const unsigned char buf[] = {
  834. FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
  835. };
  836. unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
  837. if (!dmabuf) {
  838. ret = -ENOMEM;
  839. hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
  840. return ret;
  841. }
  842. ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
  843. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  844. kfree(dmabuf);
  845. if (ret != sizeof(buf)) {
  846. hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
  847. return ret;
  848. }
  849. return 0;
  850. }
  851. static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
  852. {
  853. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  854. if (drvdata->tp)
  855. return asus_start_multitouch(hdev);
  856. return 0;
  857. }
  858. static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
  859. {
  860. int ret;
  861. struct asus_drvdata *drvdata;
  862. drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
  863. if (drvdata == NULL) {
  864. hid_err(hdev, "Can't alloc Asus descriptor\n");
  865. return -ENOMEM;
  866. }
  867. hid_set_drvdata(hdev, drvdata);
  868. drvdata->quirks = id->driver_data;
  869. /*
  870. * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
  871. * Thus, identify T90CHI dock with product name string.
  872. */
  873. if (strstr(hdev->name, "T90CHI")) {
  874. drvdata->quirks &= ~QUIRK_T100CHI;
  875. drvdata->quirks |= QUIRK_T90CHI;
  876. }
  877. if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
  878. drvdata->tp = &asus_i2c_tp;
  879. if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && hid_is_usb(hdev)) {
  880. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  881. if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
  882. drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
  883. /*
  884. * The T100HA uses the same USB-ids as the T100TAF and
  885. * the T200TA uses the same USB-ids as the T100TA, while
  886. * both have different max x/y values as the T100TA[F].
  887. */
  888. if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
  889. drvdata->tp = &asus_t100ha_tp;
  890. else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
  891. drvdata->tp = &asus_t200ta_tp;
  892. else
  893. drvdata->tp = &asus_t100ta_tp;
  894. }
  895. }
  896. if (drvdata->quirks & QUIRK_T100CHI) {
  897. /*
  898. * All functionality is on a single HID interface and for
  899. * userspace the touchpad must be a separate input_dev.
  900. */
  901. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  902. drvdata->tp = &asus_t100chi_tp;
  903. }
  904. if ((drvdata->quirks & QUIRK_MEDION_E1239T) && hid_is_usb(hdev)) {
  905. struct usb_host_interface *alt =
  906. to_usb_interface(hdev->dev.parent)->altsetting;
  907. if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
  908. /* For separate input-devs for tp and tp toggle key */
  909. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  910. drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
  911. drvdata->tp = &medion_e1239t_tp;
  912. }
  913. }
  914. if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
  915. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  916. drvdata->hdev = hdev;
  917. if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
  918. ret = asus_battery_probe(hdev);
  919. if (ret) {
  920. hid_err(hdev,
  921. "Asus hid battery_probe failed: %d\n", ret);
  922. return ret;
  923. }
  924. }
  925. ret = hid_parse(hdev);
  926. if (ret) {
  927. hid_err(hdev, "Asus hid parse failed: %d\n", ret);
  928. return ret;
  929. }
  930. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  931. if (ret) {
  932. hid_err(hdev, "Asus hw start failed: %d\n", ret);
  933. return ret;
  934. }
  935. if (!drvdata->input) {
  936. hid_err(hdev, "Asus input not registered\n");
  937. ret = -ENOMEM;
  938. goto err_stop_hw;
  939. }
  940. if (drvdata->tp) {
  941. drvdata->input->name = "Asus TouchPad";
  942. } else {
  943. drvdata->input->name = "Asus Keyboard";
  944. }
  945. if (drvdata->tp) {
  946. ret = asus_start_multitouch(hdev);
  947. if (ret)
  948. goto err_stop_hw;
  949. }
  950. return 0;
  951. err_stop_hw:
  952. hid_hw_stop(hdev);
  953. return ret;
  954. }
  955. static void asus_remove(struct hid_device *hdev)
  956. {
  957. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  958. unsigned long flags;
  959. if (drvdata->kbd_backlight) {
  960. spin_lock_irqsave(&drvdata->kbd_backlight->lock, flags);
  961. drvdata->kbd_backlight->removed = true;
  962. spin_unlock_irqrestore(&drvdata->kbd_backlight->lock, flags);
  963. cancel_work_sync(&drvdata->kbd_backlight->work);
  964. }
  965. hid_hw_stop(hdev);
  966. }
  967. static const __u8 asus_g752_fixed_rdesc[] = {
  968. 0x19, 0x00, /* Usage Minimum (0x00) */
  969. 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
  970. };
  971. static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  972. unsigned int *rsize)
  973. {
  974. struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
  975. if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
  976. *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
  977. hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
  978. rdesc[55] = 0xdd;
  979. }
  980. /* For the T100TA/T200TA keyboard dock */
  981. if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
  982. (*rsize == 76 || *rsize == 101) &&
  983. rdesc[73] == 0x81 && rdesc[74] == 0x01) {
  984. hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
  985. rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
  986. }
  987. /* For the T100CHI/T90CHI keyboard dock */
  988. if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
  989. int rsize_orig;
  990. int offs;
  991. if (drvdata->quirks & QUIRK_T100CHI) {
  992. rsize_orig = 403;
  993. offs = 388;
  994. } else {
  995. rsize_orig = 306;
  996. offs = 291;
  997. }
  998. /*
  999. * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
  1000. * (FFh) and clear the flags in the Input() byte.
  1001. * Note the descriptor has a bogus 0 byte at the end so we
  1002. * only need 1 extra byte.
  1003. */
  1004. if (*rsize == rsize_orig &&
  1005. rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
  1006. *rsize = rsize_orig + 1;
  1007. rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
  1008. if (!rdesc)
  1009. return NULL;
  1010. hid_info(hdev, "Fixing up %s keyb report descriptor\n",
  1011. drvdata->quirks & QUIRK_T100CHI ?
  1012. "T100CHI" : "T90CHI");
  1013. memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
  1014. rdesc[offs] = 0x19;
  1015. rdesc[offs + 1] = 0x00;
  1016. rdesc[offs + 2] = 0x29;
  1017. rdesc[offs + 3] = 0xff;
  1018. rdesc[offs + 14] = 0x00;
  1019. }
  1020. }
  1021. if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
  1022. *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
  1023. /* report is missing usage mninum and maximum */
  1024. __u8 *new_rdesc;
  1025. size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
  1026. new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
  1027. if (new_rdesc == NULL)
  1028. return rdesc;
  1029. hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
  1030. /* copy the valid part */
  1031. memcpy(new_rdesc, rdesc, 61);
  1032. /* insert missing part */
  1033. memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
  1034. /* copy remaining data */
  1035. memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
  1036. *rsize = new_size;
  1037. rdesc = new_rdesc;
  1038. }
  1039. if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD &&
  1040. *rsize == 331 && rdesc[190] == 0x85 && rdesc[191] == 0x5a &&
  1041. rdesc[204] == 0x95 && rdesc[205] == 0x05) {
  1042. hid_info(hdev, "Fixing up Asus N-KEY keyb report descriptor\n");
  1043. rdesc[205] = 0x01;
  1044. }
  1045. return rdesc;
  1046. }
  1047. static const struct hid_device_id asus_devices[] = {
  1048. { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1049. USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
  1050. { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1051. USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
  1052. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1053. USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
  1054. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1055. USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
  1056. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1057. USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
  1058. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1059. USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
  1060. QUIRK_USE_KBD_BACKLIGHT },
  1061. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1062. USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD),
  1063. QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
  1064. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1065. USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2),
  1066. QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
  1067. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1068. USB_DEVICE_ID_ASUSTEK_ROG_CLAYMORE_II_KEYBOARD),
  1069. QUIRK_ROG_CLAYMORE_II_KEYBOARD },
  1070. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1071. USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
  1072. QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
  1073. { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1074. USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
  1075. QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
  1076. { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
  1077. { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
  1078. { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
  1079. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
  1080. USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
  1081. { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
  1082. QUIRK_MEDION_E1239T },
  1083. /*
  1084. * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
  1085. * part, while letting hid-multitouch.c handle the touchpad.
  1086. */
  1087. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  1088. USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
  1089. { }
  1090. };
  1091. MODULE_DEVICE_TABLE(hid, asus_devices);
  1092. static struct hid_driver asus_driver = {
  1093. .name = "asus",
  1094. .id_table = asus_devices,
  1095. .report_fixup = asus_report_fixup,
  1096. .probe = asus_probe,
  1097. .remove = asus_remove,
  1098. .input_mapping = asus_input_mapping,
  1099. .input_configured = asus_input_configured,
  1100. #ifdef CONFIG_PM
  1101. .reset_resume = asus_reset_resume,
  1102. #endif
  1103. .event = asus_event,
  1104. .raw_event = asus_raw_event
  1105. };
  1106. module_hid_driver(asus_driver);
  1107. MODULE_LICENSE("GPL");