hid-asus.c 40 KB

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