1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID driver for Asus notebook built-in keyboard.
4 * Fixes small logical maximum to match usage maximum.
6 * Currently supported devices are:
10 * Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
12 * This module based on hid-ortek by
13 * Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
14 * Copyright (c) 2011 Jiri Kosina
16 * This module has been updated to add support for Asus i2c touchpad.
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>
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>
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");
42 #define T100_TPAD_INTF 2
43 #define MEDION_E1239T_TPAD_INTF 1
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
54 #define SUPPORT_KBD_BACKLIGHT BIT(0)
56 #define MAX_TOUCH_MAJOR 8
57 #define MAX_PRESSURE 128
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
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)
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)
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 | \
94 #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
96 struct asus_kbd_leds {
97 struct led_classdev cdev;
98 struct hid_device *hdev;
99 struct work_struct work;
100 unsigned int brightness;
104 struct asus_touchpad_info {
114 struct asus_drvdata {
115 unsigned long quirks;
116 struct hid_device *hdev;
117 struct input_dev *input;
118 struct input_dev *tp_kbd_input;
119 struct asus_kbd_leds *kbd_backlight;
120 const struct asus_touchpad_info *tp;
121 bool enable_backlight;
122 struct power_supply *battery;
123 struct power_supply_desc battery_desc;
124 int battery_capacity;
126 bool battery_in_query;
127 unsigned long battery_next_query;
130 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
132 static const struct asus_touchpad_info asus_i2c_tp = {
137 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
140 static const struct asus_touchpad_info asus_t100ta_tp = {
143 .res_x = 30, /* units/mm */
144 .res_y = 27, /* units/mm */
147 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
150 static const struct asus_touchpad_info asus_t100ha_tp = {
153 .res_x = 30, /* units/mm */
154 .res_y = 29, /* units/mm */
157 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
160 static const struct asus_touchpad_info asus_t200ta_tp = {
163 .res_x = 30, /* units/mm */
164 .res_y = 28, /* units/mm */
167 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
170 static const struct asus_touchpad_info asus_t100chi_tp = {
173 .res_x = 31, /* units/mm */
174 .res_y = 29, /* units/mm */
177 .report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
180 static const struct asus_touchpad_info medion_e1239t_tp = {
183 .res_x = 29, /* units/mm */
184 .res_y = 28, /* units/mm */
187 .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
190 static void asus_report_contact_down(struct asus_drvdata *drvdat,
191 int toolType, u8 *data)
193 struct input_dev *input = drvdat->input;
194 int touch_major, pressure, x, y;
196 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
197 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
199 input_report_abs(input, ABS_MT_POSITION_X, x);
200 input_report_abs(input, ABS_MT_POSITION_Y, y);
202 if (drvdat->tp->contact_size < 5)
205 if (toolType == MT_TOOL_PALM) {
206 touch_major = MAX_TOUCH_MAJOR;
207 pressure = MAX_PRESSURE;
209 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
210 pressure = data[4] & CONTACT_PRESSURE_MASK;
213 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
214 input_report_abs(input, ABS_MT_PRESSURE, pressure);
217 /* Required for Synaptics Palm Detection */
218 static void asus_report_tool_width(struct asus_drvdata *drvdat)
220 struct input_mt *mt = drvdat->input->mt;
221 struct input_mt_slot *oldest;
224 if (drvdat->tp->contact_size < 5)
230 for (i = 0; i < mt->num_slots; ++i) {
231 struct input_mt_slot *ps = &mt->slots[i];
232 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
236 if ((id - oldid) & TRKID_SGN) {
243 input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
244 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
248 static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
250 int i, toolType = MT_TOOL_FINGER;
251 u8 *contactData = data + 2;
253 if (size != drvdat->tp->report_size)
256 for (i = 0; i < drvdat->tp->max_contacts; i++) {
257 bool down = !!(data[1] & BIT(i+3));
259 if (drvdat->tp->contact_size >= 5)
260 toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
261 MT_TOOL_PALM : MT_TOOL_FINGER;
263 input_mt_slot(drvdat->input, i);
264 input_mt_report_slot_state(drvdat->input, toolType, down);
267 asus_report_contact_down(drvdat, toolType, contactData);
268 contactData += drvdat->tp->contact_size;
272 input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
273 asus_report_tool_width(drvdat);
275 input_mt_sync_frame(drvdat->input);
276 input_sync(drvdat->input);
281 static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size)
286 /* Handle broken mute key which only sends press events */
288 data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) {
289 input_report_key(drvdat->input, KEY_MUTE, 1);
290 input_sync(drvdat->input);
291 input_report_key(drvdat->input, KEY_MUTE, 0);
292 input_sync(drvdat->input);
296 /* Handle custom touchpad toggle key which only sends press events */
297 if (drvdat->tp_kbd_input &&
298 data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) {
299 input_report_key(drvdat->tp_kbd_input, KEY_F21, 1);
300 input_sync(drvdat->tp_kbd_input);
301 input_report_key(drvdat->tp_kbd_input, KEY_F21, 0);
302 input_sync(drvdat->tp_kbd_input);
309 static int asus_event(struct hid_device *hdev, struct hid_field *field,
310 struct hid_usage *usage, __s32 value)
312 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
313 (usage->hid & HID_USAGE) != 0x00 &&
314 (usage->hid & HID_USAGE) != 0xff && !usage->type) {
315 hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
316 usage->hid & HID_USAGE);
322 static int asus_raw_event(struct hid_device *hdev,
323 struct hid_report *report, u8 *data, int size)
325 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
327 if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
328 return asus_report_battery(drvdata, data, size);
330 if (drvdata->tp && data[0] == INPUT_REPORT_ID)
331 return asus_report_input(drvdata, data, size);
333 if (drvdata->quirks & QUIRK_MEDION_E1239T)
334 return asus_e1239t_event(drvdata, data, size);
336 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) {
338 * Skip these report ID, the device emits a continuous stream associated
339 * with the AURA mode it is in which looks like an 'echo'.
341 if (report->id == FEATURE_KBD_LED_REPORT_ID1 ||
342 report->id == FEATURE_KBD_LED_REPORT_ID2) {
344 /* Additional report filtering */
345 } else if (report->id == FEATURE_KBD_REPORT_ID) {
347 * G14 and G15 send these codes on some keypresses with no
348 * discernable reason for doing so. We'll filter them out to avoid
349 * unmapped warning messages later.
351 if (data[1] == 0xea || data[1] == 0xec || data[1] == 0x02 ||
352 data[1] == 0x8a || data[1] == 0x9e) {
356 if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
358 * G713 and G733 send these codes on some keypresses, depending on
359 * the key pressed it can trigger a shutdown event if not caught.
361 if(data[0] == 0x02 && data[1] == 0x30) {
368 if (drvdata->quirks & QUIRK_ROG_CLAYMORE_II_KEYBOARD) {
370 * CLAYMORE II keyboard sends this packet when it goes to sleep
371 * this causes the whole system to go into suspend.
374 if(size == 2 && data[0] == 0x02 && data[1] == 0x00) {
382 static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
384 unsigned char *dmabuf;
387 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
392 * The report ID should be set from the incoming buffer due to LED and key
393 * interfaces having different pages
395 ret = hid_hw_raw_request(hdev, buf[0], dmabuf,
396 buf_size, HID_FEATURE_REPORT,
403 static int asus_kbd_init(struct hid_device *hdev)
405 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
406 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
409 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
411 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
416 static int asus_kbd_get_functions(struct hid_device *hdev,
417 unsigned char *kbd_func)
419 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
423 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
425 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
429 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
433 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
434 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
437 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
442 *kbd_func = readbuf[6];
448 static int rog_nkey_led_init(struct hid_device *hdev)
450 u8 buf_init_start[] = { FEATURE_KBD_LED_REPORT_ID1, 0xB9 };
451 u8 buf_init2[] = { FEATURE_KBD_LED_REPORT_ID1, 0x41, 0x53, 0x55, 0x53, 0x20,
452 0x54, 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
453 u8 buf_init3[] = { FEATURE_KBD_LED_REPORT_ID1,
454 0x05, 0x20, 0x31, 0x00, 0x08 };
457 hid_info(hdev, "Asus initialise N-KEY Device");
458 /* The first message is an init start */
459 ret = asus_kbd_set_report(hdev, buf_init_start, sizeof(buf_init_start));
461 hid_warn(hdev, "Asus failed to send init start command: %d\n", ret);
464 /* Followed by a string */
465 ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
467 hid_warn(hdev, "Asus failed to send init command 1.0: %d\n", ret);
470 /* Followed by a string */
471 ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
473 hid_warn(hdev, "Asus failed to send init command 1.1: %d\n", ret);
477 /* begin second report ID with same data */
478 buf_init2[0] = FEATURE_KBD_LED_REPORT_ID2;
479 buf_init3[0] = FEATURE_KBD_LED_REPORT_ID2;
481 ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
483 hid_warn(hdev, "Asus failed to send init command 2.0: %d\n", ret);
486 ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
488 hid_warn(hdev, "Asus failed to send init command 2.1: %d\n", ret);
493 static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
494 enum led_brightness brightness)
496 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
498 led->brightness = brightness;
499 schedule_work(&led->work);
502 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
504 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
507 return led->brightness;
510 static void asus_kbd_backlight_work(struct work_struct *work)
512 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
513 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
519 buf[4] = led->brightness;
521 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
523 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
526 /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
527 * precedence. We only activate HID-based backlight control when the
528 * WMI control is not available.
530 static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
535 if (!IS_ENABLED(CONFIG_ASUS_WMI))
538 ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
539 ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
540 hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
544 return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
547 static int asus_kbd_register_leds(struct hid_device *hdev)
549 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
550 unsigned char kbd_func;
553 if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
554 ret = rog_nkey_led_init(hdev);
558 /* Initialize keyboard */
559 ret = asus_kbd_init(hdev);
563 /* Get keyboard functions */
564 ret = asus_kbd_get_functions(hdev, &kbd_func);
568 /* Check for backlight support */
569 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
573 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
574 sizeof(struct asus_kbd_leds),
576 if (!drvdata->kbd_backlight)
579 drvdata->kbd_backlight->removed = false;
580 drvdata->kbd_backlight->brightness = 0;
581 drvdata->kbd_backlight->hdev = hdev;
582 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
583 drvdata->kbd_backlight->cdev.max_brightness = 3;
584 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
585 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
586 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
588 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
590 /* No need to have this still around */
591 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
598 * [0] REPORT_ID (same value defined in report descriptor)
599 * [1] rest battery level. range [0..255]
600 * [2]..[7] Bluetooth hardware address (MAC address)
601 * [8] charging status
602 * = 0 : AC offline / discharging
603 * = 1 : AC online / charging
604 * = 2 : AC online / fully charged
606 static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
615 drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
618 case BATTERY_STAT_CHARGING:
619 val = POWER_SUPPLY_STATUS_CHARGING;
621 case BATTERY_STAT_FULL:
622 val = POWER_SUPPLY_STATUS_FULL;
624 case BATTERY_STAT_DISCONNECT:
626 val = POWER_SUPPLY_STATUS_DISCHARGING;
629 drvdata->battery_stat = val;
634 static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
636 /* notify only the autonomous event by device */
637 if ((drvdata->battery_in_query == false) &&
638 (size == BATTERY_REPORT_SIZE))
639 power_supply_changed(drvdata->battery);
644 static int asus_battery_query(struct asus_drvdata *drvdata)
649 buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
653 drvdata->battery_in_query = true;
654 ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
655 buf, BATTERY_REPORT_SIZE,
656 HID_INPUT_REPORT, HID_REQ_GET_REPORT);
657 drvdata->battery_in_query = false;
658 if (ret == BATTERY_REPORT_SIZE)
659 ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
668 static enum power_supply_property asus_battery_props[] = {
669 POWER_SUPPLY_PROP_STATUS,
670 POWER_SUPPLY_PROP_PRESENT,
671 POWER_SUPPLY_PROP_CAPACITY,
672 POWER_SUPPLY_PROP_SCOPE,
673 POWER_SUPPLY_PROP_MODEL_NAME,
676 #define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */
678 static int asus_battery_get_property(struct power_supply *psy,
679 enum power_supply_property psp,
680 union power_supply_propval *val)
682 struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
686 case POWER_SUPPLY_PROP_STATUS:
687 case POWER_SUPPLY_PROP_CAPACITY:
688 if (time_before(drvdata->battery_next_query, jiffies)) {
689 drvdata->battery_next_query =
690 jiffies + QUERY_MIN_INTERVAL;
691 ret = asus_battery_query(drvdata);
695 if (psp == POWER_SUPPLY_PROP_STATUS)
696 val->intval = drvdata->battery_stat;
698 val->intval = drvdata->battery_capacity;
700 case POWER_SUPPLY_PROP_PRESENT:
703 case POWER_SUPPLY_PROP_SCOPE:
704 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
706 case POWER_SUPPLY_PROP_MODEL_NAME:
707 val->strval = drvdata->hdev->name;
717 static int asus_battery_probe(struct hid_device *hdev)
719 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
720 struct power_supply_config pscfg = { .drv_data = drvdata };
723 drvdata->battery_capacity = 0;
724 drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
725 drvdata->battery_in_query = false;
727 drvdata->battery_desc.properties = asus_battery_props;
728 drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
729 drvdata->battery_desc.get_property = asus_battery_get_property;
730 drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
731 drvdata->battery_desc.use_for_apm = 0;
732 drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
733 "asus-keyboard-%s-battery",
735 hdev->uniq : dev_name(&hdev->dev));
736 if (!drvdata->battery_desc.name)
739 drvdata->battery_next_query = jiffies;
741 drvdata->battery = devm_power_supply_register(&hdev->dev,
742 &(drvdata->battery_desc), &pscfg);
743 if (IS_ERR(drvdata->battery)) {
744 ret = PTR_ERR(drvdata->battery);
745 drvdata->battery = NULL;
746 hid_err(hdev, "Unable to register battery device\n");
750 power_supply_powers(drvdata->battery, &hdev->dev);
755 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
757 struct input_dev *input = hi->input;
758 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
760 /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
761 if (drvdata->quirks & QUIRK_T100CHI &&
762 hi->report->id != T100CHI_MOUSE_REPORT_ID)
765 /* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
766 if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
767 switch (hi->report->id) {
768 case E1239T_TP_TOGGLE_REPORT_ID:
769 input_set_capability(input, EV_KEY, KEY_F21);
770 input->name = "Asus Touchpad Keys";
771 drvdata->tp_kbd_input = input;
773 case INPUT_REPORT_ID:
774 break; /* Touchpad report, handled below */
776 return 0; /* Ignore other reports */
783 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
784 drvdata->tp->max_x, 0, 0);
785 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
786 drvdata->tp->max_y, 0, 0);
787 input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
788 input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
790 if (drvdata->tp->contact_size >= 5) {
791 input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
792 MAX_TOUCH_MAJOR, 0, 0);
793 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
794 MAX_TOUCH_MAJOR, 0, 0);
795 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
799 __set_bit(BTN_LEFT, input->keybit);
800 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
802 ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
806 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
811 drvdata->input = input;
813 if (drvdata->enable_backlight &&
814 !asus_kbd_wmi_led_control_present(hdev) &&
815 asus_kbd_register_leds(hdev))
816 hid_warn(hdev, "Failed to initialize backlight.\n");
821 #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
823 static int asus_input_mapping(struct hid_device *hdev,
824 struct hid_input *hi, struct hid_field *field,
825 struct hid_usage *usage, unsigned long **bit,
828 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
830 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
831 /* Don't map anything from the HID report.
832 * We do it all manually in asus_input_configured
838 * Ignore a bunch of bogus collections in the T100CHI descriptor.
839 * This avoids a bunch of non-functional hid_input devices getting
840 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
842 if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
843 (field->application == (HID_UP_GENDESK | 0x0080) ||
844 field->application == HID_GD_MOUSE ||
845 usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
846 usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
847 usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
850 /* ASUS-specific keyboard hotkeys and led backlight */
851 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ASUSVENDOR) {
852 switch (usage->hid & HID_USAGE) {
853 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
854 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
855 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
856 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
857 case 0x7c: asus_map_key_clear(KEY_MICMUTE); break;
858 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
859 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
860 case 0xb5: asus_map_key_clear(KEY_CALC); break;
861 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
862 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
864 /* ASUS touchpad toggle */
865 case 0x6b: asus_map_key_clear(KEY_F21); break;
868 case 0x38: asus_map_key_clear(KEY_PROG1); break;
870 /* Fn+C ASUS Splendid */
871 case 0xba: asus_map_key_clear(KEY_PROG2); break;
873 /* Fn+Space Power4Gear Hybrid */
874 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
876 /* Fn+F5 "fan" symbol on FX503VD */
877 case 0x99: asus_map_key_clear(KEY_PROG4); break;
879 /* Fn+F5 "fan" symbol on N-Key keyboard */
880 case 0xae: asus_map_key_clear(KEY_PROG4); break;
882 /* Fn+Ret "Calc" symbol on N-Key keyboard */
883 case 0x92: asus_map_key_clear(KEY_CALC); break;
885 /* Fn+Left Aura mode previous on N-Key keyboard */
886 case 0xb2: asus_map_key_clear(KEY_PROG2); break;
888 /* Fn+Right Aura mode next on N-Key keyboard */
889 case 0xb3: asus_map_key_clear(KEY_PROG3); break;
892 /* ASUS lazily declares 256 usages, ignore the rest,
893 * as some make the keyboard appear as a pointer device. */
898 * Check and enable backlight only on devices with UsagePage ==
899 * 0xff31 to avoid initializing the keyboard firmware multiple
900 * times on devices with multiple HID descriptors but same
903 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
904 drvdata->enable_backlight = true;
906 set_bit(EV_REP, hi->input->evbit);
910 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
911 switch (usage->hid & HID_USAGE) {
912 case 0xff01: asus_map_key_clear(BTN_1); break;
913 case 0xff02: asus_map_key_clear(BTN_2); break;
914 case 0xff03: asus_map_key_clear(BTN_3); break;
915 case 0xff04: asus_map_key_clear(BTN_4); break;
916 case 0xff05: asus_map_key_clear(BTN_5); break;
917 case 0xff06: asus_map_key_clear(BTN_6); break;
918 case 0xff07: asus_map_key_clear(BTN_7); break;
919 case 0xff08: asus_map_key_clear(BTN_8); break;
920 case 0xff09: asus_map_key_clear(BTN_9); break;
921 case 0xff0a: asus_map_key_clear(BTN_A); break;
922 case 0xff0b: asus_map_key_clear(BTN_B); break;
923 case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
924 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
925 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
926 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
927 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
928 case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
933 set_bit(EV_REP, hi->input->evbit);
937 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
938 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
939 switch (usage->hid & HID_USAGE) {
940 case 0xe2: /* Mute */
941 case 0xe9: /* Volume up */
942 case 0xea: /* Volume down */
945 /* Ignore dummy Consumer usages which make the
946 * keyboard incorrectly appear as a pointer device.
953 * The mute button is broken and only sends press events, we
954 * deal with this in our raw_event handler, so do not map it.
956 if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
957 usage->hid == (HID_UP_CONSUMER | 0xe2)) {
958 input_set_capability(hi->input, EV_KEY, KEY_MUTE);
965 static int asus_start_multitouch(struct hid_device *hdev)
968 static const unsigned char buf[] = {
969 FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
971 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
975 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
979 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
980 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
984 if (ret != sizeof(buf)) {
985 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
992 static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
994 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
997 return asus_start_multitouch(hdev);
1002 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
1005 struct asus_drvdata *drvdata;
1007 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1008 if (drvdata == NULL) {
1009 hid_err(hdev, "Can't alloc Asus descriptor\n");
1013 hid_set_drvdata(hdev, drvdata);
1015 drvdata->quirks = id->driver_data;
1018 * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
1019 * Thus, identify T90CHI dock with product name string.
1021 if (strstr(hdev->name, "T90CHI")) {
1022 drvdata->quirks &= ~QUIRK_T100CHI;
1023 drvdata->quirks |= QUIRK_T90CHI;
1026 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
1027 drvdata->tp = &asus_i2c_tp;
1029 if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && hid_is_usb(hdev)) {
1030 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1032 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
1033 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
1035 * The T100HA uses the same USB-ids as the T100TAF and
1036 * the T200TA uses the same USB-ids as the T100TA, while
1037 * both have different max x/y values as the T100TA[F].
1039 if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
1040 drvdata->tp = &asus_t100ha_tp;
1041 else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
1042 drvdata->tp = &asus_t200ta_tp;
1044 drvdata->tp = &asus_t100ta_tp;
1048 if (drvdata->quirks & QUIRK_T100CHI) {
1050 * All functionality is on a single HID interface and for
1051 * userspace the touchpad must be a separate input_dev.
1053 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1054 drvdata->tp = &asus_t100chi_tp;
1057 if ((drvdata->quirks & QUIRK_MEDION_E1239T) && hid_is_usb(hdev)) {
1058 struct usb_host_interface *alt =
1059 to_usb_interface(hdev->dev.parent)->altsetting;
1061 if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
1062 /* For separate input-devs for tp and tp toggle key */
1063 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1064 drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
1065 drvdata->tp = &medion_e1239t_tp;
1069 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
1070 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1072 drvdata->hdev = hdev;
1074 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1075 ret = asus_battery_probe(hdev);
1078 "Asus hid battery_probe failed: %d\n", ret);
1083 ret = hid_parse(hdev);
1085 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
1089 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1091 hid_err(hdev, "Asus hw start failed: %d\n", ret);
1095 if (!drvdata->input) {
1096 hid_err(hdev, "Asus input not registered\n");
1102 drvdata->input->name = "Asus TouchPad";
1104 drvdata->input->name = "Asus Keyboard";
1108 ret = asus_start_multitouch(hdev);
1119 static void asus_remove(struct hid_device *hdev)
1121 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1123 if (drvdata->kbd_backlight) {
1124 drvdata->kbd_backlight->removed = true;
1125 cancel_work_sync(&drvdata->kbd_backlight->work);
1131 static const __u8 asus_g752_fixed_rdesc[] = {
1132 0x19, 0x00, /* Usage Minimum (0x00) */
1133 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
1136 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1137 unsigned int *rsize)
1139 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1141 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
1142 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
1143 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
1146 /* For the T100TA/T200TA keyboard dock */
1147 if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
1148 (*rsize == 76 || *rsize == 101) &&
1149 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
1150 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
1151 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
1153 /* For the T100CHI/T90CHI keyboard dock */
1154 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1158 if (drvdata->quirks & QUIRK_T100CHI) {
1167 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
1168 * (FFh) and clear the flags in the Input() byte.
1169 * Note the descriptor has a bogus 0 byte at the end so we
1170 * only need 1 extra byte.
1172 if (*rsize == rsize_orig &&
1173 rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
1174 *rsize = rsize_orig + 1;
1175 rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
1179 hid_info(hdev, "Fixing up %s keyb report descriptor\n",
1180 drvdata->quirks & QUIRK_T100CHI ?
1181 "T100CHI" : "T90CHI");
1182 memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1184 rdesc[offs + 1] = 0x00;
1185 rdesc[offs + 2] = 0x29;
1186 rdesc[offs + 3] = 0xff;
1187 rdesc[offs + 14] = 0x00;
1191 if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1192 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1193 /* report is missing usage mninum and maximum */
1195 size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1197 new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1198 if (new_rdesc == NULL)
1201 hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1202 /* copy the valid part */
1203 memcpy(new_rdesc, rdesc, 61);
1204 /* insert missing part */
1205 memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1206 /* copy remaining data */
1207 memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1213 if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD &&
1214 *rsize == 331 && rdesc[190] == 0x85 && rdesc[191] == 0x5a &&
1215 rdesc[204] == 0x95 && rdesc[205] == 0x05) {
1216 hid_info(hdev, "Fixing up Asus N-KEY keyb report descriptor\n");
1223 static const struct hid_device_id asus_devices[] = {
1224 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1225 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
1226 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1227 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1228 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1229 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1230 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1231 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
1232 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1233 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1234 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1235 USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1236 QUIRK_USE_KBD_BACKLIGHT },
1237 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1238 USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD),
1239 QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1240 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1241 USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2),
1242 QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1243 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1244 USB_DEVICE_ID_ASUSTEK_ROG_CLAYMORE_II_KEYBOARD),
1245 QUIRK_ROG_CLAYMORE_II_KEYBOARD },
1246 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1247 USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1248 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1249 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1250 USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
1251 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1252 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
1253 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
1254 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
1255 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1256 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1257 { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
1258 QUIRK_MEDION_E1239T },
1260 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1261 * part, while letting hid-multitouch.c handle the touchpad.
1263 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1264 USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
1267 MODULE_DEVICE_TABLE(hid, asus_devices);
1269 static struct hid_driver asus_driver = {
1271 .id_table = asus_devices,
1272 .report_fixup = asus_report_fixup,
1273 .probe = asus_probe,
1274 .remove = asus_remove,
1275 .input_mapping = asus_input_mapping,
1276 .input_configured = asus_input_configured,
1278 .reset_resume = asus_reset_resume,
1280 .event = asus_event,
1281 .raw_event = asus_raw_event
1283 module_hid_driver(asus_driver);
1285 MODULE_LICENSE("GPL");