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