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