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