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