xref: /openbmc/linux/drivers/hid/hid-asus.c (revision 2fa49589)
1 /*
2  *  HID driver for Asus notebook built-in keyboard.
3  *  Fixes small logical maximum to match usage maximum.
4  *
5  *  Currently supported devices are:
6  *    EeeBook X205TA
7  *    VivoBook E200HA
8  *
9  *  Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
10  *
11  *  This module based on hid-ortek by
12  *  Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
13  *  Copyright (c) 2011 Jiri Kosina
14  *
15  *  This module has been updated to add support for Asus i2c touchpad.
16  *
17  *  Copyright (c) 2016 Brendan McGrath <redmcg@redmandi.dyndns.org>
18  *  Copyright (c) 2016 Victor Vlasenko <victor.vlasenko@sysgears.com>
19  *  Copyright (c) 2016 Frederik Wenigwieser <frederik.wenigwieser@gmail.com>
20  */
21 
22 /*
23  * This program is free software; you can redistribute it and/or modify it
24  * under the terms of the GNU General Public License as published by the Free
25  * Software Foundation; either version 2 of the License, or (at your option)
26  * any later version.
27  */
28 
29 #include <linux/dmi.h>
30 #include <linux/hid.h>
31 #include <linux/module.h>
32 #include <linux/platform_data/x86/asus-wmi.h>
33 #include <linux/input/mt.h>
34 #include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
35 
36 #include "hid-ids.h"
37 
38 MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
39 MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
40 MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
41 MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
42 MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
43 
44 #define T100_TPAD_INTF 2
45 
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 
52 #define SUPPORT_KBD_BACKLIGHT BIT(0)
53 
54 #define MAX_TOUCH_MAJOR 8
55 #define MAX_PRESSURE 128
56 
57 #define BTN_LEFT_MASK 0x01
58 #define CONTACT_TOOL_TYPE_MASK 0x80
59 #define CONTACT_X_MSB_MASK 0xf0
60 #define CONTACT_Y_MSB_MASK 0x0f
61 #define CONTACT_TOUCH_MAJOR_MASK 0x07
62 #define CONTACT_PRESSURE_MASK 0x7f
63 
64 #define QUIRK_FIX_NOTEBOOK_REPORT	BIT(0)
65 #define QUIRK_NO_INIT_REPORTS		BIT(1)
66 #define QUIRK_SKIP_INPUT_MAPPING	BIT(2)
67 #define QUIRK_IS_MULTITOUCH		BIT(3)
68 #define QUIRK_NO_CONSUMER_USAGES	BIT(4)
69 #define QUIRK_USE_KBD_BACKLIGHT		BIT(5)
70 #define QUIRK_T100_KEYBOARD		BIT(6)
71 #define QUIRK_T100CHI			BIT(7)
72 #define QUIRK_G752_KEYBOARD		BIT(8)
73 #define QUIRK_T101HA_DOCK		BIT(9)
74 
75 #define I2C_KEYBOARD_QUIRKS			(QUIRK_FIX_NOTEBOOK_REPORT | \
76 						 QUIRK_NO_INIT_REPORTS | \
77 						 QUIRK_NO_CONSUMER_USAGES)
78 #define I2C_TOUCHPAD_QUIRKS			(QUIRK_NO_INIT_REPORTS | \
79 						 QUIRK_SKIP_INPUT_MAPPING | \
80 						 QUIRK_IS_MULTITOUCH)
81 
82 #define TRKID_SGN       ((TRKID_MAX + 1) >> 1)
83 
84 struct asus_kbd_leds {
85 	struct led_classdev cdev;
86 	struct hid_device *hdev;
87 	struct work_struct work;
88 	unsigned int brightness;
89 	bool removed;
90 };
91 
92 struct asus_touchpad_info {
93 	int max_x;
94 	int max_y;
95 	int res_x;
96 	int res_y;
97 	int contact_size;
98 	int max_contacts;
99 };
100 
101 struct asus_drvdata {
102 	unsigned long quirks;
103 	struct input_dev *input;
104 	struct asus_kbd_leds *kbd_backlight;
105 	const struct asus_touchpad_info *tp;
106 	bool enable_backlight;
107 };
108 
109 static const struct asus_touchpad_info asus_i2c_tp = {
110 	.max_x = 2794,
111 	.max_y = 1758,
112 	.contact_size = 5,
113 	.max_contacts = 5,
114 };
115 
116 static const struct asus_touchpad_info asus_t100ta_tp = {
117 	.max_x = 2240,
118 	.max_y = 1120,
119 	.res_x = 30, /* units/mm */
120 	.res_y = 27, /* units/mm */
121 	.contact_size = 5,
122 	.max_contacts = 5,
123 };
124 
125 static const struct asus_touchpad_info asus_t100ha_tp = {
126 	.max_x = 2640,
127 	.max_y = 1320,
128 	.res_x = 30, /* units/mm */
129 	.res_y = 29, /* units/mm */
130 	.contact_size = 5,
131 	.max_contacts = 5,
132 };
133 
134 static const struct asus_touchpad_info asus_t200ta_tp = {
135 	.max_x = 3120,
136 	.max_y = 1716,
137 	.res_x = 30, /* units/mm */
138 	.res_y = 28, /* units/mm */
139 	.contact_size = 5,
140 	.max_contacts = 5,
141 };
142 
143 static const struct asus_touchpad_info asus_t100chi_tp = {
144 	.max_x = 2640,
145 	.max_y = 1320,
146 	.res_x = 31, /* units/mm */
147 	.res_y = 29, /* units/mm */
148 	.contact_size = 3,
149 	.max_contacts = 4,
150 };
151 
152 static void asus_report_contact_down(struct asus_drvdata *drvdat,
153 		int toolType, u8 *data)
154 {
155 	struct input_dev *input = drvdat->input;
156 	int touch_major, pressure, x, y;
157 
158 	x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
159 	y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
160 
161 	input_report_abs(input, ABS_MT_POSITION_X, x);
162 	input_report_abs(input, ABS_MT_POSITION_Y, y);
163 
164 	if (drvdat->tp->contact_size < 5)
165 		return;
166 
167 	if (toolType == MT_TOOL_PALM) {
168 		touch_major = MAX_TOUCH_MAJOR;
169 		pressure = MAX_PRESSURE;
170 	} else {
171 		touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
172 		pressure = data[4] & CONTACT_PRESSURE_MASK;
173 	}
174 
175 	input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
176 	input_report_abs(input, ABS_MT_PRESSURE, pressure);
177 }
178 
179 /* Required for Synaptics Palm Detection */
180 static void asus_report_tool_width(struct asus_drvdata *drvdat)
181 {
182 	struct input_mt *mt = drvdat->input->mt;
183 	struct input_mt_slot *oldest;
184 	int oldid, count, i;
185 
186 	if (drvdat->tp->contact_size < 5)
187 		return;
188 
189 	oldest = NULL;
190 	oldid = mt->trkid;
191 	count = 0;
192 
193 	for (i = 0; i < mt->num_slots; ++i) {
194 		struct input_mt_slot *ps = &mt->slots[i];
195 		int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
196 
197 		if (id < 0)
198 			continue;
199 		if ((id - oldid) & TRKID_SGN) {
200 			oldest = ps;
201 			oldid = id;
202 		}
203 		count++;
204 	}
205 
206 	if (oldest) {
207 		input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
208 			input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
209 	}
210 }
211 
212 static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
213 {
214 	int i, toolType = MT_TOOL_FINGER;
215 	u8 *contactData = data + 2;
216 
217 	if (size != 3 + drvdat->tp->contact_size * drvdat->tp->max_contacts)
218 		return 0;
219 
220 	for (i = 0; i < drvdat->tp->max_contacts; i++) {
221 		bool down = !!(data[1] & BIT(i+3));
222 
223 		if (drvdat->tp->contact_size >= 5)
224 			toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
225 						MT_TOOL_PALM : MT_TOOL_FINGER;
226 
227 		input_mt_slot(drvdat->input, i);
228 		input_mt_report_slot_state(drvdat->input, toolType, down);
229 
230 		if (down) {
231 			asus_report_contact_down(drvdat, toolType, contactData);
232 			contactData += drvdat->tp->contact_size;
233 		}
234 	}
235 
236 	input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
237 	asus_report_tool_width(drvdat);
238 
239 	input_mt_sync_frame(drvdat->input);
240 	input_sync(drvdat->input);
241 
242 	return 1;
243 }
244 
245 static int asus_event(struct hid_device *hdev, struct hid_field *field,
246 		      struct hid_usage *usage, __s32 value)
247 {
248 	if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
249 	    (usage->hid & HID_USAGE) != 0x00 && !usage->type) {
250 		hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
251 			 usage->hid & HID_USAGE);
252 	}
253 
254 	return 0;
255 }
256 
257 static int asus_raw_event(struct hid_device *hdev,
258 		struct hid_report *report, u8 *data, int size)
259 {
260 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
261 
262 	if (drvdata->tp && data[0] == INPUT_REPORT_ID)
263 		return asus_report_input(drvdata, data, size);
264 
265 	return 0;
266 }
267 
268 static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
269 {
270 	unsigned char *dmabuf;
271 	int ret;
272 
273 	dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
274 	if (!dmabuf)
275 		return -ENOMEM;
276 
277 	ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
278 				 buf_size, HID_FEATURE_REPORT,
279 				 HID_REQ_SET_REPORT);
280 	kfree(dmabuf);
281 
282 	return ret;
283 }
284 
285 static int asus_kbd_init(struct hid_device *hdev)
286 {
287 	u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
288 		     0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
289 	int ret;
290 
291 	ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
292 	if (ret < 0)
293 		hid_err(hdev, "Asus failed to send init command: %d\n", ret);
294 
295 	return ret;
296 }
297 
298 static int asus_kbd_get_functions(struct hid_device *hdev,
299 				  unsigned char *kbd_func)
300 {
301 	u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
302 	u8 *readbuf;
303 	int ret;
304 
305 	ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
306 	if (ret < 0) {
307 		hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
308 		return ret;
309 	}
310 
311 	readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
312 	if (!readbuf)
313 		return -ENOMEM;
314 
315 	ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
316 				 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
317 				 HID_REQ_GET_REPORT);
318 	if (ret < 0) {
319 		hid_err(hdev, "Asus failed to request functions: %d\n", ret);
320 		kfree(readbuf);
321 		return ret;
322 	}
323 
324 	*kbd_func = readbuf[6];
325 
326 	kfree(readbuf);
327 	return ret;
328 }
329 
330 static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
331 				   enum led_brightness brightness)
332 {
333 	struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
334 						 cdev);
335 	if (led->brightness == brightness)
336 		return;
337 
338 	led->brightness = brightness;
339 	schedule_work(&led->work);
340 }
341 
342 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
343 {
344 	struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
345 						 cdev);
346 
347 	return led->brightness;
348 }
349 
350 static void asus_kbd_backlight_work(struct work_struct *work)
351 {
352 	struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
353 	u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
354 	int ret;
355 
356 	if (led->removed)
357 		return;
358 
359 	buf[4] = led->brightness;
360 
361 	ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
362 	if (ret < 0)
363 		hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
364 }
365 
366 /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
367  * precedence. We only activate HID-based backlight control when the
368  * WMI control is not available.
369  */
370 static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
371 {
372 	u32 value;
373 	int ret;
374 
375 	if (!IS_ENABLED(CONFIG_ASUS_WMI))
376 		return false;
377 
378 	ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS2,
379 				       ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
380 	hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
381 	if (ret)
382 		return false;
383 
384 	return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
385 }
386 
387 static int asus_kbd_register_leds(struct hid_device *hdev)
388 {
389 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
390 	unsigned char kbd_func;
391 	int ret;
392 
393 	/* Initialize keyboard */
394 	ret = asus_kbd_init(hdev);
395 	if (ret < 0)
396 		return ret;
397 
398 	/* Get keyboard functions */
399 	ret = asus_kbd_get_functions(hdev, &kbd_func);
400 	if (ret < 0)
401 		return ret;
402 
403 	/* Check for backlight support */
404 	if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
405 		return -ENODEV;
406 
407 	drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
408 					      sizeof(struct asus_kbd_leds),
409 					      GFP_KERNEL);
410 	if (!drvdata->kbd_backlight)
411 		return -ENOMEM;
412 
413 	drvdata->kbd_backlight->removed = false;
414 	drvdata->kbd_backlight->brightness = 0;
415 	drvdata->kbd_backlight->hdev = hdev;
416 	drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
417 	drvdata->kbd_backlight->cdev.max_brightness = 3;
418 	drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
419 	drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
420 	INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
421 
422 	ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
423 	if (ret < 0) {
424 		/* No need to have this still around */
425 		devm_kfree(&hdev->dev, drvdata->kbd_backlight);
426 	}
427 
428 	return ret;
429 }
430 
431 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
432 {
433 	struct input_dev *input = hi->input;
434 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
435 
436 	/* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
437 	if (drvdata->quirks & QUIRK_T100CHI &&
438 	    hi->report->id != T100CHI_MOUSE_REPORT_ID)
439 		return 0;
440 
441 	if (drvdata->tp) {
442 		int ret;
443 
444 		input_set_abs_params(input, ABS_MT_POSITION_X, 0,
445 				     drvdata->tp->max_x, 0, 0);
446 		input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
447 				     drvdata->tp->max_y, 0, 0);
448 		input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
449 		input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
450 
451 		if (drvdata->tp->contact_size >= 5) {
452 			input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
453 					     MAX_TOUCH_MAJOR, 0, 0);
454 			input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
455 					     MAX_TOUCH_MAJOR, 0, 0);
456 			input_set_abs_params(input, ABS_MT_PRESSURE, 0,
457 					      MAX_PRESSURE, 0, 0);
458 		}
459 
460 		__set_bit(BTN_LEFT, input->keybit);
461 		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
462 
463 		ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
464 					  INPUT_MT_POINTER);
465 
466 		if (ret) {
467 			hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
468 			return ret;
469 		}
470 	}
471 
472 	drvdata->input = input;
473 
474 	if (drvdata->enable_backlight &&
475 	    !asus_kbd_wmi_led_control_present(hdev) &&
476 	    asus_kbd_register_leds(hdev))
477 		hid_warn(hdev, "Failed to initialize backlight.\n");
478 
479 	return 0;
480 }
481 
482 #define asus_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, \
483 						    max, EV_KEY, (c))
484 static int asus_input_mapping(struct hid_device *hdev,
485 		struct hid_input *hi, struct hid_field *field,
486 		struct hid_usage *usage, unsigned long **bit,
487 		int *max)
488 {
489 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
490 
491 	if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
492 		/* Don't map anything from the HID report.
493 		 * We do it all manually in asus_input_configured
494 		 */
495 		return -1;
496 	}
497 
498 	/*
499 	 * Ignore a bunch of bogus collections in the T100CHI descriptor.
500 	 * This avoids a bunch of non-functional hid_input devices getting
501 	 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
502 	 */
503 	if (drvdata->quirks & QUIRK_T100CHI) {
504 		if (field->application == (HID_UP_GENDESK | 0x0080) ||
505 		    usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
506 		    usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
507 		    usage->hid == (HID_UP_GENDEVCTRLS | 0x0026))
508 			return -1;
509 		/*
510 		 * We use the hid_input for the mouse report for the touchpad,
511 		 * keep the left button, to avoid the core removing it.
512 		 */
513 		if (field->application == HID_GD_MOUSE &&
514 		    usage->hid != (HID_UP_BUTTON | 1))
515 			return -1;
516 	}
517 
518 	/* ASUS-specific keyboard hotkeys */
519 	if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
520 		set_bit(EV_REP, hi->input->evbit);
521 		switch (usage->hid & HID_USAGE) {
522 		case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN);	break;
523 		case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP);		break;
524 		case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF);		break;
525 		case 0x6c: asus_map_key_clear(KEY_SLEEP);		break;
526 		case 0x7c: asus_map_key_clear(KEY_MICMUTE);		break;
527 		case 0x82: asus_map_key_clear(KEY_CAMERA);		break;
528 		case 0x88: asus_map_key_clear(KEY_RFKILL);			break;
529 		case 0xb5: asus_map_key_clear(KEY_CALC);			break;
530 		case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP);		break;
531 		case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN);		break;
532 
533 		/* ASUS touchpad toggle */
534 		case 0x6b: asus_map_key_clear(KEY_F21);			break;
535 
536 		/* ROG key */
537 		case 0x38: asus_map_key_clear(KEY_PROG1);		break;
538 
539 		/* Fn+C ASUS Splendid */
540 		case 0xba: asus_map_key_clear(KEY_PROG2);		break;
541 
542 		/* Fn+Space Power4Gear Hybrid */
543 		case 0x5c: asus_map_key_clear(KEY_PROG3);		break;
544 
545 		/* Fn+F5 "fan" symbol on FX503VD */
546 		case 0x99: asus_map_key_clear(KEY_PROG4);		break;
547 
548 		default:
549 			/* ASUS lazily declares 256 usages, ignore the rest,
550 			 * as some make the keyboard appear as a pointer device. */
551 			return -1;
552 		}
553 
554 		/*
555 		 * Check and enable backlight only on devices with UsagePage ==
556 		 * 0xff31 to avoid initializing the keyboard firmware multiple
557 		 * times on devices with multiple HID descriptors but same
558 		 * PID/VID.
559 		 */
560 		if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
561 			drvdata->enable_backlight = true;
562 
563 		return 1;
564 	}
565 
566 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
567 		set_bit(EV_REP, hi->input->evbit);
568 		switch (usage->hid & HID_USAGE) {
569 		case 0xff01: asus_map_key_clear(BTN_1);	break;
570 		case 0xff02: asus_map_key_clear(BTN_2);	break;
571 		case 0xff03: asus_map_key_clear(BTN_3);	break;
572 		case 0xff04: asus_map_key_clear(BTN_4);	break;
573 		case 0xff05: asus_map_key_clear(BTN_5);	break;
574 		case 0xff06: asus_map_key_clear(BTN_6);	break;
575 		case 0xff07: asus_map_key_clear(BTN_7);	break;
576 		case 0xff08: asus_map_key_clear(BTN_8);	break;
577 		case 0xff09: asus_map_key_clear(BTN_9);	break;
578 		case 0xff0a: asus_map_key_clear(BTN_A);	break;
579 		case 0xff0b: asus_map_key_clear(BTN_B);	break;
580 		case 0x00f1: asus_map_key_clear(KEY_WLAN);	break;
581 		case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN);	break;
582 		case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP);	break;
583 		case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF);	break;
584 		case 0x00f7: asus_map_key_clear(KEY_CAMERA);	break;
585 		case 0x00f8: asus_map_key_clear(KEY_PROG1);	break;
586 		default:
587 			return 0;
588 		}
589 
590 		return 1;
591 	}
592 
593 	if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
594 		(usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
595 		switch (usage->hid & HID_USAGE) {
596 		case 0xe2: /* Mute */
597 		case 0xe9: /* Volume up */
598 		case 0xea: /* Volume down */
599 			return 0;
600 		default:
601 			/* Ignore dummy Consumer usages which make the
602 			 * keyboard incorrectly appear as a pointer device.
603 			 */
604 			return -1;
605 		}
606 	}
607 
608 	return 0;
609 }
610 
611 static int asus_start_multitouch(struct hid_device *hdev)
612 {
613 	int ret;
614 	static const unsigned char buf[] = {
615 		FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
616 	};
617 	unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
618 
619 	if (!dmabuf) {
620 		ret = -ENOMEM;
621 		hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
622 		return ret;
623 	}
624 
625 	ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
626 					HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
627 
628 	kfree(dmabuf);
629 
630 	if (ret != sizeof(buf)) {
631 		hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
632 		return ret;
633 	}
634 
635 	return 0;
636 }
637 
638 static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
639 {
640 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
641 
642 	if (drvdata->tp)
643 		return asus_start_multitouch(hdev);
644 
645 	return 0;
646 }
647 
648 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
649 {
650 	int ret;
651 	struct asus_drvdata *drvdata;
652 
653 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
654 	if (drvdata == NULL) {
655 		hid_err(hdev, "Can't alloc Asus descriptor\n");
656 		return -ENOMEM;
657 	}
658 
659 	hid_set_drvdata(hdev, drvdata);
660 
661 	drvdata->quirks = id->driver_data;
662 
663 	if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
664 		drvdata->tp = &asus_i2c_tp;
665 
666 	if (drvdata->quirks & QUIRK_T100_KEYBOARD) {
667 		struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
668 
669 		if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
670 			drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
671 			/*
672 			 * The T100HA uses the same USB-ids as the T100TAF and
673 			 * the T200TA uses the same USB-ids as the T100TA, while
674 			 * both have different max x/y values as the T100TA[F].
675 			 */
676 			if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
677 				drvdata->tp = &asus_t100ha_tp;
678 			else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
679 				drvdata->tp = &asus_t200ta_tp;
680 			else
681 				drvdata->tp = &asus_t100ta_tp;
682 		}
683 	}
684 
685 	if (drvdata->quirks & QUIRK_T100CHI) {
686 		/*
687 		 * All functionality is on a single HID interface and for
688 		 * userspace the touchpad must be a separate input_dev.
689 		 */
690 		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
691 		drvdata->tp = &asus_t100chi_tp;
692 	}
693 
694 	if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
695 		hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
696 
697 	ret = hid_parse(hdev);
698 	if (ret) {
699 		hid_err(hdev, "Asus hid parse failed: %d\n", ret);
700 		return ret;
701 	}
702 
703 	/* use hid-multitouch for T101HA touchpad */
704 	if (id->driver_data & QUIRK_T101HA_DOCK &&
705 	    hdev->collection->usage == HID_GD_MOUSE)
706 		return -ENODEV;
707 
708 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
709 	if (ret) {
710 		hid_err(hdev, "Asus hw start failed: %d\n", ret);
711 		return ret;
712 	}
713 
714 	if (!drvdata->input) {
715 		hid_err(hdev, "Asus input not registered\n");
716 		ret = -ENOMEM;
717 		goto err_stop_hw;
718 	}
719 
720 	if (drvdata->tp) {
721 		drvdata->input->name = "Asus TouchPad";
722 	} else {
723 		drvdata->input->name = "Asus Keyboard";
724 	}
725 
726 	if (drvdata->tp) {
727 		ret = asus_start_multitouch(hdev);
728 		if (ret)
729 			goto err_stop_hw;
730 	}
731 
732 	return 0;
733 err_stop_hw:
734 	hid_hw_stop(hdev);
735 	return ret;
736 }
737 
738 static void asus_remove(struct hid_device *hdev)
739 {
740 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
741 
742 	if (drvdata->kbd_backlight) {
743 		drvdata->kbd_backlight->removed = true;
744 		cancel_work_sync(&drvdata->kbd_backlight->work);
745 	}
746 
747 	hid_hw_stop(hdev);
748 }
749 
750 static const __u8 asus_g752_fixed_rdesc[] = {
751         0x19, 0x00,			/*   Usage Minimum (0x00)       */
752         0x2A, 0xFF, 0x00,		/*   Usage Maximum (0xFF)       */
753 };
754 
755 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
756 		unsigned int *rsize)
757 {
758 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
759 
760 	if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
761 			*rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
762 		hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
763 		rdesc[55] = 0xdd;
764 	}
765 	/* For the T100TA/T200TA keyboard dock */
766 	if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
767 		 (*rsize == 76 || *rsize == 101) &&
768 		 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
769 		hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
770 		rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
771 	}
772 	/* For the T100CHI keyboard dock */
773 	if (drvdata->quirks & QUIRK_T100CHI &&
774 		 *rsize == 403 && rdesc[388] == 0x09 && rdesc[389] == 0x76) {
775 		/*
776 		 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
777 		 * (FFh) and clear the flags in the Input() byte.
778 		 * Note the descriptor has a bogus 0 byte at the end so we
779 		 * only need 1 extra byte.
780 		 */
781 		*rsize = 404;
782 		rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
783 		if (!rdesc)
784 			return NULL;
785 
786 		hid_info(hdev, "Fixing up T100CHI keyb report descriptor\n");
787 		memmove(rdesc + 392, rdesc + 390, 12);
788 		rdesc[388] = 0x19;
789 		rdesc[389] = 0x00;
790 		rdesc[390] = 0x29;
791 		rdesc[391] = 0xff;
792 		rdesc[402] = 0x00;
793 	}
794 	if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
795 		 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
796 		/* report is missing usage mninum and maximum */
797 		__u8 *new_rdesc;
798 		size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
799 
800 		new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
801 		if (new_rdesc == NULL)
802 			return rdesc;
803 
804 		hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
805 		/* copy the valid part */
806 		memcpy(new_rdesc, rdesc, 61);
807 		/* insert missing part */
808 		memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
809 		/* copy remaining data */
810 		memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
811 
812 		*rsize = new_size;
813 		rdesc = new_rdesc;
814 	}
815 
816 	return rdesc;
817 }
818 
819 static const struct hid_device_id asus_devices[] = {
820 	{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
821 		USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
822 	{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
823 		USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
824 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
825 		USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
826 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
827 		USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
828 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
829 		USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
830 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
831 		USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
832 	  QUIRK_USE_KBD_BACKLIGHT },
833 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
834 		USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
835 	  QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
836 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
837 		USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
838 	  QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
839 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
840 		USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD), QUIRK_T101HA_DOCK },
841 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
842 	{ HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
843 	{ HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
844 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
845 		USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
846 
847 	{ }
848 };
849 MODULE_DEVICE_TABLE(hid, asus_devices);
850 
851 static struct hid_driver asus_driver = {
852 	.name			= "asus",
853 	.id_table		= asus_devices,
854 	.report_fixup		= asus_report_fixup,
855 	.probe                  = asus_probe,
856 	.remove			= asus_remove,
857 	.input_mapping          = asus_input_mapping,
858 	.input_configured       = asus_input_configured,
859 #ifdef CONFIG_PM
860 	.reset_resume           = asus_reset_resume,
861 #endif
862 	.event			= asus_event,
863 	.raw_event		= asus_raw_event
864 };
865 module_hid_driver(asus_driver);
866 
867 MODULE_LICENSE("GPL");
868