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