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