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