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
52 #define SUPPORT_KBD_BACKLIGHT BIT(0)
54 #define MAX_TOUCH_MAJOR 8
55 #define MAX_PRESSURE 128
57 #define BTN_LEFT_MASK 0x01
58 #define CONTACT_TOOL_TYPE_MASK 0x80
59 #define CONTACT_X_MSB_MASK 0xf0
60 #define CONTACT_Y_MSB_MASK 0x0f
61 #define CONTACT_TOUCH_MAJOR_MASK 0x07
62 #define CONTACT_PRESSURE_MASK 0x7f
64 #define BATTERY_REPORT_ID (0x03)
65 #define BATTERY_REPORT_SIZE (1 + 8)
66 #define BATTERY_LEVEL_MAX ((u8)255)
67 #define BATTERY_STAT_DISCONNECT (0)
68 #define BATTERY_STAT_CHARGING (1)
69 #define BATTERY_STAT_FULL (2)
71 #define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
72 #define QUIRK_NO_INIT_REPORTS BIT(1)
73 #define QUIRK_SKIP_INPUT_MAPPING BIT(2)
74 #define QUIRK_IS_MULTITOUCH BIT(3)
75 #define QUIRK_NO_CONSUMER_USAGES BIT(4)
76 #define QUIRK_USE_KBD_BACKLIGHT BIT(5)
77 #define QUIRK_T100_KEYBOARD BIT(6)
78 #define QUIRK_T100CHI BIT(7)
79 #define QUIRK_G752_KEYBOARD BIT(8)
80 #define QUIRK_T101HA_DOCK BIT(9)
81 #define QUIRK_T90CHI BIT(10)
82 #define QUIRK_MEDION_E1239T BIT(11)
84 #define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
85 QUIRK_NO_INIT_REPORTS | \
86 QUIRK_NO_CONSUMER_USAGES)
87 #define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
88 QUIRK_SKIP_INPUT_MAPPING | \
91 #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
93 struct asus_kbd_leds {
94 struct led_classdev cdev;
95 struct hid_device *hdev;
96 struct work_struct work;
97 unsigned int brightness;
102 struct asus_touchpad_info {
112 struct asus_drvdata {
113 unsigned long quirks;
114 struct hid_device *hdev;
115 struct input_dev *input;
116 struct input_dev *tp_kbd_input;
117 struct asus_kbd_leds *kbd_backlight;
118 const struct asus_touchpad_info *tp;
119 bool enable_backlight;
120 struct power_supply *battery;
121 struct power_supply_desc battery_desc;
122 int battery_capacity;
124 bool battery_in_query;
125 unsigned long battery_next_query;
128 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
130 static const struct asus_touchpad_info asus_i2c_tp = {
135 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
138 static const struct asus_touchpad_info asus_t100ta_tp = {
141 .res_x = 30, /* units/mm */
142 .res_y = 27, /* units/mm */
145 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
148 static const struct asus_touchpad_info asus_t100ha_tp = {
151 .res_x = 30, /* units/mm */
152 .res_y = 29, /* units/mm */
155 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
158 static const struct asus_touchpad_info asus_t200ta_tp = {
161 .res_x = 30, /* units/mm */
162 .res_y = 28, /* units/mm */
165 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
168 static const struct asus_touchpad_info asus_t100chi_tp = {
171 .res_x = 31, /* units/mm */
172 .res_y = 29, /* units/mm */
175 .report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
178 static const struct asus_touchpad_info medion_e1239t_tp = {
181 .res_x = 29, /* units/mm */
182 .res_y = 28, /* units/mm */
185 .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
188 static void asus_report_contact_down(struct asus_drvdata *drvdat,
189 int toolType, u8 *data)
191 struct input_dev *input = drvdat->input;
192 int touch_major, pressure, x, y;
194 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
195 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
197 input_report_abs(input, ABS_MT_POSITION_X, x);
198 input_report_abs(input, ABS_MT_POSITION_Y, y);
200 if (drvdat->tp->contact_size < 5)
203 if (toolType == MT_TOOL_PALM) {
204 touch_major = MAX_TOUCH_MAJOR;
205 pressure = MAX_PRESSURE;
207 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
208 pressure = data[4] & CONTACT_PRESSURE_MASK;
211 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
212 input_report_abs(input, ABS_MT_PRESSURE, pressure);
215 /* Required for Synaptics Palm Detection */
216 static void asus_report_tool_width(struct asus_drvdata *drvdat)
218 struct input_mt *mt = drvdat->input->mt;
219 struct input_mt_slot *oldest;
222 if (drvdat->tp->contact_size < 5)
229 for (i = 0; i < mt->num_slots; ++i) {
230 struct input_mt_slot *ps = &mt->slots[i];
231 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
235 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);
339 static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t buf_size)
341 unsigned char *dmabuf;
344 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
348 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
349 buf_size, HID_FEATURE_REPORT,
356 static int asus_kbd_init(struct hid_device *hdev)
358 const u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
359 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
362 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
364 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
369 static int asus_kbd_get_functions(struct hid_device *hdev,
370 unsigned char *kbd_func)
372 const u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
376 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
378 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
382 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
386 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
387 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
390 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
395 *kbd_func = readbuf[6];
401 static void asus_schedule_work(struct asus_kbd_leds *led)
405 spin_lock_irqsave(&led->lock, flags);
407 schedule_work(&led->work);
408 spin_unlock_irqrestore(&led->lock, flags);
411 static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
412 enum led_brightness brightness)
414 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
418 spin_lock_irqsave(&led->lock, flags);
419 led->brightness = brightness;
420 spin_unlock_irqrestore(&led->lock, flags);
422 asus_schedule_work(led);
425 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
427 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
429 enum led_brightness brightness;
432 spin_lock_irqsave(&led->lock, flags);
433 brightness = led->brightness;
434 spin_unlock_irqrestore(&led->lock, flags);
439 static void asus_kbd_backlight_work(struct work_struct *work)
441 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
442 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
446 spin_lock_irqsave(&led->lock, flags);
447 buf[4] = led->brightness;
448 spin_unlock_irqrestore(&led->lock, flags);
450 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
452 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
455 /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
456 * precedence. We only activate HID-based backlight control when the
457 * WMI control is not available.
459 static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
464 if (!IS_ENABLED(CONFIG_ASUS_WMI))
467 ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
468 ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
469 hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
473 return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
476 static int asus_kbd_register_leds(struct hid_device *hdev)
478 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
479 unsigned char kbd_func;
482 /* Initialize keyboard */
483 ret = asus_kbd_init(hdev);
487 /* Get keyboard functions */
488 ret = asus_kbd_get_functions(hdev, &kbd_func);
492 /* Check for backlight support */
493 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
496 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
497 sizeof(struct asus_kbd_leds),
499 if (!drvdata->kbd_backlight)
502 drvdata->kbd_backlight->removed = false;
503 drvdata->kbd_backlight->brightness = 0;
504 drvdata->kbd_backlight->hdev = hdev;
505 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
506 drvdata->kbd_backlight->cdev.max_brightness = 3;
507 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
508 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
509 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
510 spin_lock_init(&drvdata->kbd_backlight->lock);
512 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
514 /* No need to have this still around */
515 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
522 * [0] REPORT_ID (same value defined in report descriptor)
523 * [1] rest battery level. range [0..255]
524 * [2]..[7] Bluetooth hardware address (MAC address)
525 * [8] charging status
526 * = 0 : AC offline / discharging
527 * = 1 : AC online / charging
528 * = 2 : AC online / fully charged
530 static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
539 drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
542 case BATTERY_STAT_CHARGING:
543 val = POWER_SUPPLY_STATUS_CHARGING;
545 case BATTERY_STAT_FULL:
546 val = POWER_SUPPLY_STATUS_FULL;
548 case BATTERY_STAT_DISCONNECT:
550 val = POWER_SUPPLY_STATUS_DISCHARGING;
553 drvdata->battery_stat = val;
558 static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
560 /* notify only the autonomous event by device */
561 if ((drvdata->battery_in_query == false) &&
562 (size == BATTERY_REPORT_SIZE))
563 power_supply_changed(drvdata->battery);
568 static int asus_battery_query(struct asus_drvdata *drvdata)
573 buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
577 drvdata->battery_in_query = true;
578 ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
579 buf, BATTERY_REPORT_SIZE,
580 HID_INPUT_REPORT, HID_REQ_GET_REPORT);
581 drvdata->battery_in_query = false;
582 if (ret == BATTERY_REPORT_SIZE)
583 ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
592 static enum power_supply_property asus_battery_props[] = {
593 POWER_SUPPLY_PROP_STATUS,
594 POWER_SUPPLY_PROP_PRESENT,
595 POWER_SUPPLY_PROP_CAPACITY,
596 POWER_SUPPLY_PROP_SCOPE,
597 POWER_SUPPLY_PROP_MODEL_NAME,
600 #define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */
602 static int asus_battery_get_property(struct power_supply *psy,
603 enum power_supply_property psp,
604 union power_supply_propval *val)
606 struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
610 case POWER_SUPPLY_PROP_STATUS:
611 case POWER_SUPPLY_PROP_CAPACITY:
612 if (time_before(drvdata->battery_next_query, jiffies)) {
613 drvdata->battery_next_query =
614 jiffies + QUERY_MIN_INTERVAL;
615 ret = asus_battery_query(drvdata);
619 if (psp == POWER_SUPPLY_PROP_STATUS)
620 val->intval = drvdata->battery_stat;
622 val->intval = drvdata->battery_capacity;
624 case POWER_SUPPLY_PROP_PRESENT:
627 case POWER_SUPPLY_PROP_SCOPE:
628 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
630 case POWER_SUPPLY_PROP_MODEL_NAME:
631 val->strval = drvdata->hdev->name;
641 static int asus_battery_probe(struct hid_device *hdev)
643 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
644 struct power_supply_config pscfg = { .drv_data = drvdata };
647 drvdata->battery_capacity = 0;
648 drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
649 drvdata->battery_in_query = false;
651 drvdata->battery_desc.properties = asus_battery_props;
652 drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
653 drvdata->battery_desc.get_property = asus_battery_get_property;
654 drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
655 drvdata->battery_desc.use_for_apm = 0;
656 drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
657 "asus-keyboard-%s-battery",
659 hdev->uniq : dev_name(&hdev->dev));
660 if (!drvdata->battery_desc.name)
663 drvdata->battery_next_query = jiffies;
665 drvdata->battery = devm_power_supply_register(&hdev->dev,
666 &(drvdata->battery_desc), &pscfg);
667 if (IS_ERR(drvdata->battery)) {
668 ret = PTR_ERR(drvdata->battery);
669 drvdata->battery = NULL;
670 hid_err(hdev, "Unable to register battery device\n");
674 power_supply_powers(drvdata->battery, &hdev->dev);
679 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
681 struct input_dev *input = hi->input;
682 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
684 /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
685 if (drvdata->quirks & QUIRK_T100CHI &&
686 hi->report->id != T100CHI_MOUSE_REPORT_ID)
689 /* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
690 if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
691 switch (hi->report->id) {
692 case E1239T_TP_TOGGLE_REPORT_ID:
693 input_set_capability(input, EV_KEY, KEY_F21);
694 input->name = "Asus Touchpad Keys";
695 drvdata->tp_kbd_input = input;
697 case INPUT_REPORT_ID:
698 break; /* Touchpad report, handled below */
700 return 0; /* Ignore other reports */
707 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
708 drvdata->tp->max_x, 0, 0);
709 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
710 drvdata->tp->max_y, 0, 0);
711 input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
712 input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
714 if (drvdata->tp->contact_size >= 5) {
715 input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
716 MAX_TOUCH_MAJOR, 0, 0);
717 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
718 MAX_TOUCH_MAJOR, 0, 0);
719 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
723 __set_bit(BTN_LEFT, input->keybit);
724 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
726 ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
730 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
735 drvdata->input = input;
737 if (drvdata->enable_backlight &&
738 !asus_kbd_wmi_led_control_present(hdev) &&
739 asus_kbd_register_leds(hdev))
740 hid_warn(hdev, "Failed to initialize backlight.\n");
745 #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
747 static int asus_input_mapping(struct hid_device *hdev,
748 struct hid_input *hi, struct hid_field *field,
749 struct hid_usage *usage, unsigned long **bit,
752 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
754 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
755 /* Don't map anything from the HID report.
756 * We do it all manually in asus_input_configured
762 * Ignore a bunch of bogus collections in the T100CHI descriptor.
763 * This avoids a bunch of non-functional hid_input devices getting
764 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
766 if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
767 (field->application == (HID_UP_GENDESK | 0x0080) ||
768 field->application == HID_GD_MOUSE ||
769 usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
770 usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
771 usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
774 /* ASUS-specific keyboard hotkeys */
775 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
776 switch (usage->hid & HID_USAGE) {
777 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
778 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
779 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
780 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
781 case 0x7c: asus_map_key_clear(KEY_MICMUTE); break;
782 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
783 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
784 case 0xb5: asus_map_key_clear(KEY_CALC); break;
785 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
786 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
788 /* ASUS touchpad toggle */
789 case 0x6b: asus_map_key_clear(KEY_F21); break;
792 case 0x38: asus_map_key_clear(KEY_PROG1); break;
794 /* Fn+C ASUS Splendid */
795 case 0xba: asus_map_key_clear(KEY_PROG2); break;
797 /* Fn+Space Power4Gear Hybrid */
798 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
800 /* Fn+F5 "fan" symbol on FX503VD */
801 case 0x99: asus_map_key_clear(KEY_PROG4); break;
804 /* ASUS lazily declares 256 usages, ignore the rest,
805 * as some make the keyboard appear as a pointer device. */
810 * Check and enable backlight only on devices with UsagePage ==
811 * 0xff31 to avoid initializing the keyboard firmware multiple
812 * times on devices with multiple HID descriptors but same
815 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
816 drvdata->enable_backlight = true;
818 set_bit(EV_REP, hi->input->evbit);
822 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
823 switch (usage->hid & HID_USAGE) {
824 case 0xff01: asus_map_key_clear(BTN_1); break;
825 case 0xff02: asus_map_key_clear(BTN_2); break;
826 case 0xff03: asus_map_key_clear(BTN_3); break;
827 case 0xff04: asus_map_key_clear(BTN_4); break;
828 case 0xff05: asus_map_key_clear(BTN_5); break;
829 case 0xff06: asus_map_key_clear(BTN_6); break;
830 case 0xff07: asus_map_key_clear(BTN_7); break;
831 case 0xff08: asus_map_key_clear(BTN_8); break;
832 case 0xff09: asus_map_key_clear(BTN_9); break;
833 case 0xff0a: asus_map_key_clear(BTN_A); break;
834 case 0xff0b: asus_map_key_clear(BTN_B); break;
835 case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
836 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
837 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
838 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
839 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
840 case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
845 set_bit(EV_REP, hi->input->evbit);
849 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
850 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
851 switch (usage->hid & HID_USAGE) {
852 case 0xe2: /* Mute */
853 case 0xe9: /* Volume up */
854 case 0xea: /* Volume down */
857 /* Ignore dummy Consumer usages which make the
858 * keyboard incorrectly appear as a pointer device.
865 * The mute button is broken and only sends press events, we
866 * deal with this in our raw_event handler, so do not map it.
868 if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
869 usage->hid == (HID_UP_CONSUMER | 0xe2)) {
870 input_set_capability(hi->input, EV_KEY, KEY_MUTE);
877 static int asus_start_multitouch(struct hid_device *hdev)
880 static const unsigned char buf[] = {
881 FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
883 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
887 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
891 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
892 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
896 if (ret != sizeof(buf)) {
897 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
904 static int __maybe_unused asus_resume(struct hid_device *hdev) {
905 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
908 if (drvdata->kbd_backlight) {
909 const u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4,
910 drvdata->kbd_backlight->cdev.brightness };
911 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
913 hid_err(hdev, "Asus failed to set keyboard backlight: %d\n", ret);
914 goto asus_resume_err;
922 static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
924 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
927 return asus_start_multitouch(hdev);
932 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
935 struct asus_drvdata *drvdata;
937 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
938 if (drvdata == NULL) {
939 hid_err(hdev, "Can't alloc Asus descriptor\n");
943 hid_set_drvdata(hdev, drvdata);
945 drvdata->quirks = id->driver_data;
948 * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
949 * Thus, identify T90CHI dock with product name string.
951 if (strstr(hdev->name, "T90CHI")) {
952 drvdata->quirks &= ~QUIRK_T100CHI;
953 drvdata->quirks |= QUIRK_T90CHI;
956 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
957 drvdata->tp = &asus_i2c_tp;
959 if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && hid_is_usb(hdev)) {
960 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
962 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
963 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
965 * The T100HA uses the same USB-ids as the T100TAF and
966 * the T200TA uses the same USB-ids as the T100TA, while
967 * both have different max x/y values as the T100TA[F].
969 if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
970 drvdata->tp = &asus_t100ha_tp;
971 else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
972 drvdata->tp = &asus_t200ta_tp;
974 drvdata->tp = &asus_t100ta_tp;
978 if (drvdata->quirks & QUIRK_T100CHI) {
980 * All functionality is on a single HID interface and for
981 * userspace the touchpad must be a separate input_dev.
983 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
984 drvdata->tp = &asus_t100chi_tp;
987 if ((drvdata->quirks & QUIRK_MEDION_E1239T) && hid_is_usb(hdev)) {
988 struct usb_host_interface *alt =
989 to_usb_interface(hdev->dev.parent)->altsetting;
991 if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
992 /* For separate input-devs for tp and tp toggle key */
993 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
994 drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
995 drvdata->tp = &medion_e1239t_tp;
999 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
1000 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1002 drvdata->hdev = hdev;
1004 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1005 ret = asus_battery_probe(hdev);
1008 "Asus hid battery_probe failed: %d\n", ret);
1013 ret = hid_parse(hdev);
1015 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
1019 /* use hid-multitouch for T101HA touchpad */
1020 if (id->driver_data & QUIRK_T101HA_DOCK &&
1021 hdev->collection->usage == HID_GD_MOUSE)
1024 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1026 hid_err(hdev, "Asus hw start failed: %d\n", ret);
1030 if (!drvdata->input) {
1031 hid_err(hdev, "Asus input not registered\n");
1037 drvdata->input->name = "Asus TouchPad";
1039 drvdata->input->name = "Asus Keyboard";
1043 ret = asus_start_multitouch(hdev);
1054 static void asus_remove(struct hid_device *hdev)
1056 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1057 unsigned long flags;
1059 if (drvdata->kbd_backlight) {
1060 spin_lock_irqsave(&drvdata->kbd_backlight->lock, flags);
1061 drvdata->kbd_backlight->removed = true;
1062 spin_unlock_irqrestore(&drvdata->kbd_backlight->lock, flags);
1064 cancel_work_sync(&drvdata->kbd_backlight->work);
1070 static const __u8 asus_g752_fixed_rdesc[] = {
1071 0x19, 0x00, /* Usage Minimum (0x00) */
1072 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
1075 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1076 unsigned int *rsize)
1078 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1080 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
1081 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
1082 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
1085 /* For the T100TA/T200TA keyboard dock */
1086 if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
1087 (*rsize == 76 || *rsize == 101) &&
1088 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
1089 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
1090 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
1092 /* For the T100CHI/T90CHI keyboard dock */
1093 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1097 if (drvdata->quirks & QUIRK_T100CHI) {
1106 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
1107 * (FFh) and clear the flags in the Input() byte.
1108 * Note the descriptor has a bogus 0 byte at the end so we
1109 * only need 1 extra byte.
1111 if (*rsize == rsize_orig &&
1112 rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
1113 *rsize = rsize_orig + 1;
1114 rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
1118 hid_info(hdev, "Fixing up %s keyb report descriptor\n",
1119 drvdata->quirks & QUIRK_T100CHI ?
1120 "T100CHI" : "T90CHI");
1121 memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1123 rdesc[offs + 1] = 0x00;
1124 rdesc[offs + 2] = 0x29;
1125 rdesc[offs + 3] = 0xff;
1126 rdesc[offs + 14] = 0x00;
1130 if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1131 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1132 /* report is missing usage mninum and maximum */
1134 size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1136 new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1137 if (new_rdesc == NULL)
1140 hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1141 /* copy the valid part */
1142 memcpy(new_rdesc, rdesc, 61);
1143 /* insert missing part */
1144 memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1145 /* copy remaining data */
1146 memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1155 static const struct hid_device_id asus_devices[] = {
1156 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1157 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
1158 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1159 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1160 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1161 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1162 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1163 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
1164 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1165 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1166 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1167 USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1168 QUIRK_USE_KBD_BACKLIGHT },
1169 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1170 USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1171 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1172 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1173 USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
1174 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1175 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1176 USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD), QUIRK_T101HA_DOCK },
1177 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
1178 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
1179 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
1180 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1181 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1182 { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
1183 QUIRK_MEDION_E1239T },
1186 MODULE_DEVICE_TABLE(hid, asus_devices);
1188 static struct hid_driver asus_driver = {
1190 .id_table = asus_devices,
1191 .report_fixup = asus_report_fixup,
1192 .probe = asus_probe,
1193 .remove = asus_remove,
1194 .input_mapping = asus_input_mapping,
1195 .input_configured = asus_input_configured,
1197 .reset_resume = asus_reset_resume,
1198 .resume = asus_resume,
1200 .event = asus_event,
1201 .raw_event = asus_raw_event
1203 module_hid_driver(asus_driver);
1205 MODULE_LICENSE("GPL");