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