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