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