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