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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/drivers/hid/hid-asus.c