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)
86 #define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
87 QUIRK_NO_INIT_REPORTS | \
88 QUIRK_NO_CONSUMER_USAGES)
89 #define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
90 QUIRK_SKIP_INPUT_MAPPING | \
93 #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
95 struct asus_kbd_leds {
96 struct led_classdev cdev;
97 struct hid_device *hdev;
98 struct work_struct work;
99 unsigned int brightness;
103 struct asus_touchpad_info {
113 struct asus_drvdata {
114 unsigned long quirks;
115 struct hid_device *hdev;
116 struct input_dev *input;
117 struct input_dev *tp_kbd_input;
118 struct asus_kbd_leds *kbd_backlight;
119 const struct asus_touchpad_info *tp;
120 bool enable_backlight;
121 struct power_supply *battery;
122 struct power_supply_desc battery_desc;
123 int battery_capacity;
125 bool battery_in_query;
126 unsigned long battery_next_query;
129 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
131 static const struct asus_touchpad_info asus_i2c_tp = {
136 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
139 static const struct asus_touchpad_info asus_t100ta_tp = {
142 .res_x = 30, /* units/mm */
143 .res_y = 27, /* units/mm */
146 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
149 static const struct asus_touchpad_info asus_t100ha_tp = {
152 .res_x = 30, /* units/mm */
153 .res_y = 29, /* units/mm */
156 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
159 static const struct asus_touchpad_info asus_t200ta_tp = {
162 .res_x = 30, /* units/mm */
163 .res_y = 28, /* units/mm */
166 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
169 static const struct asus_touchpad_info asus_t100chi_tp = {
172 .res_x = 31, /* units/mm */
173 .res_y = 29, /* units/mm */
176 .report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
179 static const struct asus_touchpad_info medion_e1239t_tp = {
182 .res_x = 29, /* units/mm */
183 .res_y = 28, /* units/mm */
186 .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
189 static void asus_report_contact_down(struct asus_drvdata *drvdat,
190 int toolType, u8 *data)
192 struct input_dev *input = drvdat->input;
193 int touch_major, pressure, x, y;
195 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
196 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
198 input_report_abs(input, ABS_MT_POSITION_X, x);
199 input_report_abs(input, ABS_MT_POSITION_Y, y);
201 if (drvdat->tp->contact_size < 5)
204 if (toolType == MT_TOOL_PALM) {
205 touch_major = MAX_TOUCH_MAJOR;
206 pressure = MAX_PRESSURE;
208 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
209 pressure = data[4] & CONTACT_PRESSURE_MASK;
212 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
213 input_report_abs(input, ABS_MT_PRESSURE, pressure);
216 /* Required for Synaptics Palm Detection */
217 static void asus_report_tool_width(struct asus_drvdata *drvdat)
219 struct input_mt *mt = drvdat->input->mt;
220 struct input_mt_slot *oldest;
223 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) {
244 input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
245 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
249 static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
251 int i, toolType = MT_TOOL_FINGER;
252 u8 *contactData = data + 2;
254 if (size != drvdat->tp->report_size)
257 for (i = 0; i < drvdat->tp->max_contacts; i++) {
258 bool down = !!(data[1] & BIT(i+3));
260 if (drvdat->tp->contact_size >= 5)
261 toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
262 MT_TOOL_PALM : MT_TOOL_FINGER;
264 input_mt_slot(drvdat->input, i);
265 input_mt_report_slot_state(drvdat->input, toolType, down);
268 asus_report_contact_down(drvdat, toolType, contactData);
269 contactData += drvdat->tp->contact_size;
273 input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
274 asus_report_tool_width(drvdat);
276 input_mt_sync_frame(drvdat->input);
277 input_sync(drvdat->input);
282 static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size)
287 /* Handle broken mute key which only sends press events */
289 data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) {
290 input_report_key(drvdat->input, KEY_MUTE, 1);
291 input_sync(drvdat->input);
292 input_report_key(drvdat->input, KEY_MUTE, 0);
293 input_sync(drvdat->input);
297 /* Handle custom touchpad toggle key which only sends press events */
298 if (drvdat->tp_kbd_input &&
299 data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) {
300 input_report_key(drvdat->tp_kbd_input, KEY_F21, 1);
301 input_sync(drvdat->tp_kbd_input);
302 input_report_key(drvdat->tp_kbd_input, KEY_F21, 0);
303 input_sync(drvdat->tp_kbd_input);
310 static int asus_event(struct hid_device *hdev, struct hid_field *field,
311 struct hid_usage *usage, __s32 value)
313 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
314 (usage->hid & HID_USAGE) != 0x00 &&
315 (usage->hid & HID_USAGE) != 0xff && !usage->type) {
316 hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
317 usage->hid & HID_USAGE);
323 static int asus_raw_event(struct hid_device *hdev,
324 struct hid_report *report, u8 *data, int size)
326 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
328 if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
329 return asus_report_battery(drvdata, data, size);
331 if (drvdata->tp && data[0] == INPUT_REPORT_ID)
332 return asus_report_input(drvdata, data, size);
334 if (drvdata->quirks & QUIRK_MEDION_E1239T)
335 return asus_e1239t_event(drvdata, data, size);
337 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) {
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'.
342 if (report->id == FEATURE_KBD_LED_REPORT_ID1 ||
343 report->id == FEATURE_KBD_LED_REPORT_ID2) {
345 /* Additional report filtering */
346 } else if (report->id == FEATURE_KBD_REPORT_ID) {
348 * G14 and G15 send these codes on some keypresses with no
349 * discernable reason for doing so. We'll filter them out to avoid
350 * unmapped warning messages later.
352 if (data[1] == 0xea || data[1] == 0xec || data[1] == 0x02 ||
353 data[1] == 0x8a || data[1] == 0x9e) {
357 if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
359 * G713 and G733 send these codes on some keypresses, depending on
360 * the key pressed it can trigger a shutdown event if not caught.
362 if(data[0] == 0x02 && data[1] == 0x30) {
372 static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
374 unsigned char *dmabuf;
377 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
382 * The report ID should be set from the incoming buffer due to LED and key
383 * interfaces having different pages
385 ret = hid_hw_raw_request(hdev, buf[0], dmabuf,
386 buf_size, HID_FEATURE_REPORT,
393 static int asus_kbd_init(struct hid_device *hdev)
395 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
396 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
399 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
401 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
406 static int asus_kbd_get_functions(struct hid_device *hdev,
407 unsigned char *kbd_func)
409 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
413 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
415 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
419 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
423 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
424 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
427 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
432 *kbd_func = readbuf[6];
438 static int rog_nkey_led_init(struct hid_device *hdev)
440 u8 buf_init_start[] = { FEATURE_KBD_LED_REPORT_ID1, 0xB9 };
441 u8 buf_init2[] = { FEATURE_KBD_LED_REPORT_ID1, 0x41, 0x53, 0x55, 0x53, 0x20,
442 0x54, 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
443 u8 buf_init3[] = { FEATURE_KBD_LED_REPORT_ID1,
444 0x05, 0x20, 0x31, 0x00, 0x08 };
447 hid_info(hdev, "Asus initialise N-KEY Device");
448 /* The first message is an init start */
449 ret = asus_kbd_set_report(hdev, buf_init_start, sizeof(buf_init_start));
451 hid_warn(hdev, "Asus failed to send init start command: %d\n", ret);
454 /* Followed by a string */
455 ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
457 hid_warn(hdev, "Asus failed to send init command 1.0: %d\n", ret);
460 /* Followed by a string */
461 ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
463 hid_warn(hdev, "Asus failed to send init command 1.1: %d\n", ret);
467 /* begin second report ID with same data */
468 buf_init2[0] = FEATURE_KBD_LED_REPORT_ID2;
469 buf_init3[0] = FEATURE_KBD_LED_REPORT_ID2;
471 ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
473 hid_warn(hdev, "Asus failed to send init command 2.0: %d\n", ret);
476 ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
478 hid_warn(hdev, "Asus failed to send init command 2.1: %d\n", ret);
483 static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
484 enum led_brightness brightness)
486 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
488 led->brightness = brightness;
489 schedule_work(&led->work);
492 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
494 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
497 return led->brightness;
500 static void asus_kbd_backlight_work(struct work_struct *work)
502 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
503 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
509 buf[4] = led->brightness;
511 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
513 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
516 /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
517 * precedence. We only activate HID-based backlight control when the
518 * WMI control is not available.
520 static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
525 if (!IS_ENABLED(CONFIG_ASUS_WMI))
528 ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
529 ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
530 hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
534 return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
537 static int asus_kbd_register_leds(struct hid_device *hdev)
539 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
540 unsigned char kbd_func;
543 if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
544 ret = rog_nkey_led_init(hdev);
548 /* Initialize keyboard */
549 ret = asus_kbd_init(hdev);
553 /* Get keyboard functions */
554 ret = asus_kbd_get_functions(hdev, &kbd_func);
558 /* Check for backlight support */
559 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
563 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
564 sizeof(struct asus_kbd_leds),
566 if (!drvdata->kbd_backlight)
569 drvdata->kbd_backlight->removed = false;
570 drvdata->kbd_backlight->brightness = 0;
571 drvdata->kbd_backlight->hdev = hdev;
572 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
573 drvdata->kbd_backlight->cdev.max_brightness = 3;
574 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
575 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
576 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
578 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
580 /* No need to have this still around */
581 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
588 * [0] REPORT_ID (same value defined in report descriptor)
589 * [1] rest battery level. range [0..255]
590 * [2]..[7] Bluetooth hardware address (MAC address)
591 * [8] charging status
592 * = 0 : AC offline / discharging
593 * = 1 : AC online / charging
594 * = 2 : AC online / fully charged
596 static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
605 drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
608 case BATTERY_STAT_CHARGING:
609 val = POWER_SUPPLY_STATUS_CHARGING;
611 case BATTERY_STAT_FULL:
612 val = POWER_SUPPLY_STATUS_FULL;
614 case BATTERY_STAT_DISCONNECT:
616 val = POWER_SUPPLY_STATUS_DISCHARGING;
619 drvdata->battery_stat = val;
624 static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
626 /* notify only the autonomous event by device */
627 if ((drvdata->battery_in_query == false) &&
628 (size == BATTERY_REPORT_SIZE))
629 power_supply_changed(drvdata->battery);
634 static int asus_battery_query(struct asus_drvdata *drvdata)
639 buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
643 drvdata->battery_in_query = true;
644 ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
645 buf, BATTERY_REPORT_SIZE,
646 HID_INPUT_REPORT, HID_REQ_GET_REPORT);
647 drvdata->battery_in_query = false;
648 if (ret == BATTERY_REPORT_SIZE)
649 ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
658 static enum power_supply_property asus_battery_props[] = {
659 POWER_SUPPLY_PROP_STATUS,
660 POWER_SUPPLY_PROP_PRESENT,
661 POWER_SUPPLY_PROP_CAPACITY,
662 POWER_SUPPLY_PROP_SCOPE,
663 POWER_SUPPLY_PROP_MODEL_NAME,
666 #define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */
668 static int asus_battery_get_property(struct power_supply *psy,
669 enum power_supply_property psp,
670 union power_supply_propval *val)
672 struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
676 case POWER_SUPPLY_PROP_STATUS:
677 case POWER_SUPPLY_PROP_CAPACITY:
678 if (time_before(drvdata->battery_next_query, jiffies)) {
679 drvdata->battery_next_query =
680 jiffies + QUERY_MIN_INTERVAL;
681 ret = asus_battery_query(drvdata);
685 if (psp == POWER_SUPPLY_PROP_STATUS)
686 val->intval = drvdata->battery_stat;
688 val->intval = drvdata->battery_capacity;
690 case POWER_SUPPLY_PROP_PRESENT:
693 case POWER_SUPPLY_PROP_SCOPE:
694 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
696 case POWER_SUPPLY_PROP_MODEL_NAME:
697 val->strval = drvdata->hdev->name;
707 static int asus_battery_probe(struct hid_device *hdev)
709 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
710 struct power_supply_config pscfg = { .drv_data = drvdata };
713 drvdata->battery_capacity = 0;
714 drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
715 drvdata->battery_in_query = false;
717 drvdata->battery_desc.properties = asus_battery_props;
718 drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
719 drvdata->battery_desc.get_property = asus_battery_get_property;
720 drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
721 drvdata->battery_desc.use_for_apm = 0;
722 drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
723 "asus-keyboard-%s-battery",
725 hdev->uniq : dev_name(&hdev->dev));
726 if (!drvdata->battery_desc.name)
729 drvdata->battery_next_query = jiffies;
731 drvdata->battery = devm_power_supply_register(&hdev->dev,
732 &(drvdata->battery_desc), &pscfg);
733 if (IS_ERR(drvdata->battery)) {
734 ret = PTR_ERR(drvdata->battery);
735 drvdata->battery = NULL;
736 hid_err(hdev, "Unable to register battery device\n");
740 power_supply_powers(drvdata->battery, &hdev->dev);
745 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
747 struct input_dev *input = hi->input;
748 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
750 /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
751 if (drvdata->quirks & QUIRK_T100CHI &&
752 hi->report->id != T100CHI_MOUSE_REPORT_ID)
755 /* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
756 if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
757 switch (hi->report->id) {
758 case E1239T_TP_TOGGLE_REPORT_ID:
759 input_set_capability(input, EV_KEY, KEY_F21);
760 input->name = "Asus Touchpad Keys";
761 drvdata->tp_kbd_input = input;
763 case INPUT_REPORT_ID:
764 break; /* Touchpad report, handled below */
766 return 0; /* Ignore other reports */
773 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
774 drvdata->tp->max_x, 0, 0);
775 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
776 drvdata->tp->max_y, 0, 0);
777 input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
778 input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
780 if (drvdata->tp->contact_size >= 5) {
781 input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
782 MAX_TOUCH_MAJOR, 0, 0);
783 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
784 MAX_TOUCH_MAJOR, 0, 0);
785 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
789 __set_bit(BTN_LEFT, input->keybit);
790 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
792 ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
796 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
801 drvdata->input = input;
803 if (drvdata->enable_backlight &&
804 !asus_kbd_wmi_led_control_present(hdev) &&
805 asus_kbd_register_leds(hdev))
806 hid_warn(hdev, "Failed to initialize backlight.\n");
811 #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
813 static int asus_input_mapping(struct hid_device *hdev,
814 struct hid_input *hi, struct hid_field *field,
815 struct hid_usage *usage, unsigned long **bit,
818 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
820 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
821 /* Don't map anything from the HID report.
822 * We do it all manually in asus_input_configured
828 * Ignore a bunch of bogus collections in the T100CHI descriptor.
829 * This avoids a bunch of non-functional hid_input devices getting
830 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
832 if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
833 (field->application == (HID_UP_GENDESK | 0x0080) ||
834 field->application == HID_GD_MOUSE ||
835 usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
836 usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
837 usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
840 /* ASUS-specific keyboard hotkeys and led backlight */
841 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ASUSVENDOR) {
842 switch (usage->hid & HID_USAGE) {
843 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
844 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
845 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
846 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
847 case 0x7c: asus_map_key_clear(KEY_MICMUTE); break;
848 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
849 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
850 case 0xb5: asus_map_key_clear(KEY_CALC); break;
851 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
852 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
854 /* ASUS touchpad toggle */
855 case 0x6b: asus_map_key_clear(KEY_F21); break;
858 case 0x38: asus_map_key_clear(KEY_PROG1); break;
860 /* Fn+C ASUS Splendid */
861 case 0xba: asus_map_key_clear(KEY_PROG2); break;
863 /* Fn+Space Power4Gear Hybrid */
864 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
866 /* Fn+F5 "fan" symbol on FX503VD */
867 case 0x99: asus_map_key_clear(KEY_PROG4); break;
869 /* Fn+F5 "fan" symbol on N-Key keyboard */
870 case 0xae: asus_map_key_clear(KEY_PROG4); break;
872 /* Fn+Ret "Calc" symbol on N-Key keyboard */
873 case 0x92: asus_map_key_clear(KEY_CALC); break;
875 /* Fn+Left Aura mode previous on N-Key keyboard */
876 case 0xb2: asus_map_key_clear(KEY_PROG2); break;
878 /* Fn+Right Aura mode next on N-Key keyboard */
879 case 0xb3: asus_map_key_clear(KEY_PROG3); break;
882 /* ASUS lazily declares 256 usages, ignore the rest,
883 * as some make the keyboard appear as a pointer device. */
888 * Check and enable backlight only on devices with UsagePage ==
889 * 0xff31 to avoid initializing the keyboard firmware multiple
890 * times on devices with multiple HID descriptors but same
893 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
894 drvdata->enable_backlight = true;
896 set_bit(EV_REP, hi->input->evbit);
900 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
901 switch (usage->hid & HID_USAGE) {
902 case 0xff01: asus_map_key_clear(BTN_1); break;
903 case 0xff02: asus_map_key_clear(BTN_2); break;
904 case 0xff03: asus_map_key_clear(BTN_3); break;
905 case 0xff04: asus_map_key_clear(BTN_4); break;
906 case 0xff05: asus_map_key_clear(BTN_5); break;
907 case 0xff06: asus_map_key_clear(BTN_6); break;
908 case 0xff07: asus_map_key_clear(BTN_7); break;
909 case 0xff08: asus_map_key_clear(BTN_8); break;
910 case 0xff09: asus_map_key_clear(BTN_9); break;
911 case 0xff0a: asus_map_key_clear(BTN_A); break;
912 case 0xff0b: asus_map_key_clear(BTN_B); break;
913 case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
914 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
915 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
916 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
917 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
918 case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
923 set_bit(EV_REP, hi->input->evbit);
927 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
928 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
929 switch (usage->hid & HID_USAGE) {
930 case 0xe2: /* Mute */
931 case 0xe9: /* Volume up */
932 case 0xea: /* Volume down */
935 /* Ignore dummy Consumer usages which make the
936 * keyboard incorrectly appear as a pointer device.
943 * The mute button is broken and only sends press events, we
944 * deal with this in our raw_event handler, so do not map it.
946 if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
947 usage->hid == (HID_UP_CONSUMER | 0xe2)) {
948 input_set_capability(hi->input, EV_KEY, KEY_MUTE);
955 static int asus_start_multitouch(struct hid_device *hdev)
958 static const unsigned char buf[] = {
959 FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
961 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
965 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
969 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
970 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
974 if (ret != sizeof(buf)) {
975 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
982 static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
984 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
987 return asus_start_multitouch(hdev);
992 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
995 struct asus_drvdata *drvdata;
997 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
998 if (drvdata == NULL) {
999 hid_err(hdev, "Can't alloc Asus descriptor\n");
1003 hid_set_drvdata(hdev, drvdata);
1005 drvdata->quirks = id->driver_data;
1008 * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
1009 * Thus, identify T90CHI dock with product name string.
1011 if (strstr(hdev->name, "T90CHI")) {
1012 drvdata->quirks &= ~QUIRK_T100CHI;
1013 drvdata->quirks |= QUIRK_T90CHI;
1016 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
1017 drvdata->tp = &asus_i2c_tp;
1019 if ((drvdata->quirks & QUIRK_T100_KEYBOARD) &&
1020 hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1021 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1023 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
1024 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
1026 * The T100HA uses the same USB-ids as the T100TAF and
1027 * the T200TA uses the same USB-ids as the T100TA, while
1028 * both have different max x/y values as the T100TA[F].
1030 if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
1031 drvdata->tp = &asus_t100ha_tp;
1032 else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
1033 drvdata->tp = &asus_t200ta_tp;
1035 drvdata->tp = &asus_t100ta_tp;
1039 if (drvdata->quirks & QUIRK_T100CHI) {
1041 * All functionality is on a single HID interface and for
1042 * userspace the touchpad must be a separate input_dev.
1044 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1045 drvdata->tp = &asus_t100chi_tp;
1048 if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
1049 hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1050 struct usb_host_interface *alt =
1051 to_usb_interface(hdev->dev.parent)->altsetting;
1053 if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
1054 /* For separate input-devs for tp and tp toggle key */
1055 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1056 drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
1057 drvdata->tp = &medion_e1239t_tp;
1061 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
1062 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1064 drvdata->hdev = hdev;
1066 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1067 ret = asus_battery_probe(hdev);
1070 "Asus hid battery_probe failed: %d\n", ret);
1075 ret = hid_parse(hdev);
1077 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
1081 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1083 hid_err(hdev, "Asus hw start failed: %d\n", ret);
1087 if (!drvdata->input) {
1088 hid_err(hdev, "Asus input not registered\n");
1094 drvdata->input->name = "Asus TouchPad";
1096 drvdata->input->name = "Asus Keyboard";
1100 ret = asus_start_multitouch(hdev);
1111 static void asus_remove(struct hid_device *hdev)
1113 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1115 if (drvdata->kbd_backlight) {
1116 drvdata->kbd_backlight->removed = true;
1117 cancel_work_sync(&drvdata->kbd_backlight->work);
1123 static const __u8 asus_g752_fixed_rdesc[] = {
1124 0x19, 0x00, /* Usage Minimum (0x00) */
1125 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
1128 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1129 unsigned int *rsize)
1131 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1133 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
1134 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
1135 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
1138 /* For the T100TA/T200TA keyboard dock */
1139 if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
1140 (*rsize == 76 || *rsize == 101) &&
1141 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
1142 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
1143 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
1145 /* For the T100CHI/T90CHI keyboard dock */
1146 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1150 if (drvdata->quirks & QUIRK_T100CHI) {
1159 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
1160 * (FFh) and clear the flags in the Input() byte.
1161 * Note the descriptor has a bogus 0 byte at the end so we
1162 * only need 1 extra byte.
1164 if (*rsize == rsize_orig &&
1165 rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
1166 *rsize = rsize_orig + 1;
1167 rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
1171 hid_info(hdev, "Fixing up %s keyb report descriptor\n",
1172 drvdata->quirks & QUIRK_T100CHI ?
1173 "T100CHI" : "T90CHI");
1174 memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1176 rdesc[offs + 1] = 0x00;
1177 rdesc[offs + 2] = 0x29;
1178 rdesc[offs + 3] = 0xff;
1179 rdesc[offs + 14] = 0x00;
1183 if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1184 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1185 /* report is missing usage mninum and maximum */
1187 size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1189 new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1190 if (new_rdesc == NULL)
1193 hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1194 /* copy the valid part */
1195 memcpy(new_rdesc, rdesc, 61);
1196 /* insert missing part */
1197 memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1198 /* copy remaining data */
1199 memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1208 static const struct hid_device_id asus_devices[] = {
1209 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1210 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
1211 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1212 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1213 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1214 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1215 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1216 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
1217 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1218 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1219 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1220 USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1221 QUIRK_USE_KBD_BACKLIGHT },
1222 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1223 USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD),
1224 QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1225 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1226 USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2),
1227 QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1228 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1229 USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1230 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1231 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1232 USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
1233 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1234 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
1235 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
1236 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
1237 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1238 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1239 { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
1240 QUIRK_MEDION_E1239T },
1242 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1243 * part, while letting hid-multitouch.c handle the touchpad.
1245 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1246 USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
1249 MODULE_DEVICE_TABLE(hid, asus_devices);
1251 static struct hid_driver asus_driver = {
1253 .id_table = asus_devices,
1254 .report_fixup = asus_report_fixup,
1255 .probe = asus_probe,
1256 .remove = asus_remove,
1257 .input_mapping = asus_input_mapping,
1258 .input_configured = asus_input_configured,
1260 .reset_resume = asus_reset_resume,
1262 .event = asus_event,
1263 .raw_event = asus_raw_event
1265 module_hid_driver(asus_driver);
1267 MODULE_LICENSE("GPL");