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