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;
101 struct asus_touchpad_info {
111 struct asus_drvdata {
112 unsigned long quirks;
113 struct hid_device *hdev;
114 struct input_dev *input;
115 struct input_dev *tp_kbd_input;
116 struct asus_kbd_leds *kbd_backlight;
117 const struct asus_touchpad_info *tp;
118 bool enable_backlight;
119 struct power_supply *battery;
120 struct power_supply_desc battery_desc;
121 int battery_capacity;
123 bool battery_in_query;
124 unsigned long battery_next_query;
127 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
129 static const struct asus_touchpad_info asus_i2c_tp = {
134 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
137 static const struct asus_touchpad_info asus_t100ta_tp = {
140 .res_x = 30, /* units/mm */
141 .res_y = 27, /* units/mm */
144 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
147 static const struct asus_touchpad_info asus_t100ha_tp = {
150 .res_x = 30, /* units/mm */
151 .res_y = 29, /* units/mm */
154 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
157 static const struct asus_touchpad_info asus_t200ta_tp = {
160 .res_x = 30, /* units/mm */
161 .res_y = 28, /* units/mm */
164 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
167 static const struct asus_touchpad_info asus_t100chi_tp = {
170 .res_x = 31, /* units/mm */
171 .res_y = 29, /* units/mm */
174 .report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
177 static const struct asus_touchpad_info medion_e1239t_tp = {
180 .res_x = 29, /* units/mm */
181 .res_y = 28, /* units/mm */
184 .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
187 static void asus_report_contact_down(struct asus_drvdata *drvdat,
188 int toolType, u8 *data)
190 struct input_dev *input = drvdat->input;
191 int touch_major, pressure, x, y;
193 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
194 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
196 input_report_abs(input, ABS_MT_POSITION_X, x);
197 input_report_abs(input, ABS_MT_POSITION_Y, y);
199 if (drvdat->tp->contact_size < 5)
202 if (toolType == MT_TOOL_PALM) {
203 touch_major = MAX_TOUCH_MAJOR;
204 pressure = MAX_PRESSURE;
206 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
207 pressure = data[4] & CONTACT_PRESSURE_MASK;
210 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
211 input_report_abs(input, ABS_MT_PRESSURE, pressure);
214 /* Required for Synaptics Palm Detection */
215 static void asus_report_tool_width(struct asus_drvdata *drvdat)
217 struct input_mt *mt = drvdat->input->mt;
218 struct input_mt_slot *oldest;
221 if (drvdat->tp->contact_size < 5)
228 for (i = 0; i < mt->num_slots; ++i) {
229 struct input_mt_slot *ps = &mt->slots[i];
230 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
234 if ((id - oldid) & TRKID_SGN) {
242 input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
243 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
247 static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
249 int i, toolType = MT_TOOL_FINGER;
250 u8 *contactData = data + 2;
252 if (size != drvdat->tp->report_size)
255 for (i = 0; i < drvdat->tp->max_contacts; i++) {
256 bool down = !!(data[1] & BIT(i+3));
258 if (drvdat->tp->contact_size >= 5)
259 toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
260 MT_TOOL_PALM : MT_TOOL_FINGER;
262 input_mt_slot(drvdat->input, i);
263 input_mt_report_slot_state(drvdat->input, toolType, down);
266 asus_report_contact_down(drvdat, toolType, contactData);
267 contactData += drvdat->tp->contact_size;
271 input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
272 asus_report_tool_width(drvdat);
274 input_mt_sync_frame(drvdat->input);
275 input_sync(drvdat->input);
280 static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size)
285 /* Handle broken mute key which only sends press events */
287 data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) {
288 input_report_key(drvdat->input, KEY_MUTE, 1);
289 input_sync(drvdat->input);
290 input_report_key(drvdat->input, KEY_MUTE, 0);
291 input_sync(drvdat->input);
295 /* Handle custom touchpad toggle key which only sends press events */
296 if (drvdat->tp_kbd_input &&
297 data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) {
298 input_report_key(drvdat->tp_kbd_input, KEY_F21, 1);
299 input_sync(drvdat->tp_kbd_input);
300 input_report_key(drvdat->tp_kbd_input, KEY_F21, 0);
301 input_sync(drvdat->tp_kbd_input);
308 static int asus_event(struct hid_device *hdev, struct hid_field *field,
309 struct hid_usage *usage, __s32 value)
311 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
312 (usage->hid & HID_USAGE) != 0x00 &&
313 (usage->hid & HID_USAGE) != 0xff && !usage->type) {
314 hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
315 usage->hid & HID_USAGE);
321 static int asus_raw_event(struct hid_device *hdev,
322 struct hid_report *report, u8 *data, int size)
324 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
326 if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
327 return asus_report_battery(drvdata, data, size);
329 if (drvdata->tp && data[0] == INPUT_REPORT_ID)
330 return asus_report_input(drvdata, data, size);
332 if (drvdata->quirks & QUIRK_MEDION_E1239T)
333 return asus_e1239t_event(drvdata, data, size);
338 static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
340 unsigned char *dmabuf;
343 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
347 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
348 buf_size, HID_FEATURE_REPORT,
355 static int asus_kbd_init(struct hid_device *hdev)
357 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
358 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
361 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
363 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
368 static int asus_kbd_get_functions(struct hid_device *hdev,
369 unsigned char *kbd_func)
371 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
375 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
377 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
381 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
385 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
386 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
389 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
394 *kbd_func = readbuf[6];
400 static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
401 enum led_brightness brightness)
403 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
405 if (led->brightness == brightness)
408 led->brightness = brightness;
409 schedule_work(&led->work);
412 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
414 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
417 return led->brightness;
420 static void asus_kbd_backlight_work(struct work_struct *work)
422 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
423 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
429 buf[4] = led->brightness;
431 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
433 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
436 /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
437 * precedence. We only activate HID-based backlight control when the
438 * WMI control is not available.
440 static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
445 if (!IS_ENABLED(CONFIG_ASUS_WMI))
448 ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
449 ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
450 hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
454 return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
457 static int asus_kbd_register_leds(struct hid_device *hdev)
459 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
460 unsigned char kbd_func;
463 /* Initialize keyboard */
464 ret = asus_kbd_init(hdev);
468 /* Get keyboard functions */
469 ret = asus_kbd_get_functions(hdev, &kbd_func);
473 /* Check for backlight support */
474 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
477 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
478 sizeof(struct asus_kbd_leds),
480 if (!drvdata->kbd_backlight)
483 drvdata->kbd_backlight->removed = false;
484 drvdata->kbd_backlight->brightness = 0;
485 drvdata->kbd_backlight->hdev = hdev;
486 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
487 drvdata->kbd_backlight->cdev.max_brightness = 3;
488 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
489 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
490 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
492 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
494 /* No need to have this still around */
495 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
502 * [0] REPORT_ID (same value defined in report descriptor)
503 * [1] rest battery level. range [0..255]
504 * [2]..[7] Bluetooth hardware address (MAC address)
505 * [8] charging status
506 * = 0 : AC offline / discharging
507 * = 1 : AC online / charging
508 * = 2 : AC online / fully charged
510 static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
519 drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
522 case BATTERY_STAT_CHARGING:
523 val = POWER_SUPPLY_STATUS_CHARGING;
525 case BATTERY_STAT_FULL:
526 val = POWER_SUPPLY_STATUS_FULL;
528 case BATTERY_STAT_DISCONNECT:
530 val = POWER_SUPPLY_STATUS_DISCHARGING;
533 drvdata->battery_stat = val;
538 static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
540 /* notify only the autonomous event by device */
541 if ((drvdata->battery_in_query == false) &&
542 (size == BATTERY_REPORT_SIZE))
543 power_supply_changed(drvdata->battery);
548 static int asus_battery_query(struct asus_drvdata *drvdata)
553 buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
557 drvdata->battery_in_query = true;
558 ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
559 buf, BATTERY_REPORT_SIZE,
560 HID_INPUT_REPORT, HID_REQ_GET_REPORT);
561 drvdata->battery_in_query = false;
562 if (ret == BATTERY_REPORT_SIZE)
563 ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
572 static enum power_supply_property asus_battery_props[] = {
573 POWER_SUPPLY_PROP_STATUS,
574 POWER_SUPPLY_PROP_PRESENT,
575 POWER_SUPPLY_PROP_CAPACITY,
576 POWER_SUPPLY_PROP_SCOPE,
577 POWER_SUPPLY_PROP_MODEL_NAME,
580 #define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */
582 static int asus_battery_get_property(struct power_supply *psy,
583 enum power_supply_property psp,
584 union power_supply_propval *val)
586 struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
590 case POWER_SUPPLY_PROP_STATUS:
591 case POWER_SUPPLY_PROP_CAPACITY:
592 if (time_before(drvdata->battery_next_query, jiffies)) {
593 drvdata->battery_next_query =
594 jiffies + QUERY_MIN_INTERVAL;
595 ret = asus_battery_query(drvdata);
599 if (psp == POWER_SUPPLY_PROP_STATUS)
600 val->intval = drvdata->battery_stat;
602 val->intval = drvdata->battery_capacity;
604 case POWER_SUPPLY_PROP_PRESENT:
607 case POWER_SUPPLY_PROP_SCOPE:
608 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
610 case POWER_SUPPLY_PROP_MODEL_NAME:
611 val->strval = drvdata->hdev->name;
621 static int asus_battery_probe(struct hid_device *hdev)
623 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
624 struct power_supply_config pscfg = { .drv_data = drvdata };
627 drvdata->battery_capacity = 0;
628 drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
629 drvdata->battery_in_query = false;
631 drvdata->battery_desc.properties = asus_battery_props;
632 drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
633 drvdata->battery_desc.get_property = asus_battery_get_property;
634 drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
635 drvdata->battery_desc.use_for_apm = 0;
636 drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
637 "asus-keyboard-%s-battery",
639 hdev->uniq : dev_name(&hdev->dev));
640 if (!drvdata->battery_desc.name)
643 drvdata->battery_next_query = jiffies;
645 drvdata->battery = devm_power_supply_register(&hdev->dev,
646 &(drvdata->battery_desc), &pscfg);
647 if (IS_ERR(drvdata->battery)) {
648 ret = PTR_ERR(drvdata->battery);
649 drvdata->battery = NULL;
650 hid_err(hdev, "Unable to register battery device\n");
654 power_supply_powers(drvdata->battery, &hdev->dev);
659 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
661 struct input_dev *input = hi->input;
662 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
664 /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
665 if (drvdata->quirks & QUIRK_T100CHI &&
666 hi->report->id != T100CHI_MOUSE_REPORT_ID)
669 /* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
670 if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
671 switch (hi->report->id) {
672 case E1239T_TP_TOGGLE_REPORT_ID:
673 input_set_capability(input, EV_KEY, KEY_F21);
674 input->name = "Asus Touchpad Keys";
675 drvdata->tp_kbd_input = input;
677 case INPUT_REPORT_ID:
678 break; /* Touchpad report, handled below */
680 return 0; /* Ignore other reports */
687 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
688 drvdata->tp->max_x, 0, 0);
689 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
690 drvdata->tp->max_y, 0, 0);
691 input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
692 input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
694 if (drvdata->tp->contact_size >= 5) {
695 input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
696 MAX_TOUCH_MAJOR, 0, 0);
697 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
698 MAX_TOUCH_MAJOR, 0, 0);
699 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
703 __set_bit(BTN_LEFT, input->keybit);
704 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
706 ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
710 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
715 drvdata->input = input;
717 if (drvdata->enable_backlight &&
718 !asus_kbd_wmi_led_control_present(hdev) &&
719 asus_kbd_register_leds(hdev))
720 hid_warn(hdev, "Failed to initialize backlight.\n");
725 #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
727 static int asus_input_mapping(struct hid_device *hdev,
728 struct hid_input *hi, struct hid_field *field,
729 struct hid_usage *usage, unsigned long **bit,
732 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
734 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
735 /* Don't map anything from the HID report.
736 * We do it all manually in asus_input_configured
742 * Ignore a bunch of bogus collections in the T100CHI descriptor.
743 * This avoids a bunch of non-functional hid_input devices getting
744 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
746 if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
747 (field->application == (HID_UP_GENDESK | 0x0080) ||
748 field->application == HID_GD_MOUSE ||
749 usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
750 usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
751 usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
754 /* ASUS-specific keyboard hotkeys */
755 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
756 switch (usage->hid & HID_USAGE) {
757 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
758 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
759 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
760 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
761 case 0x7c: asus_map_key_clear(KEY_MICMUTE); break;
762 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
763 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
764 case 0xb5: asus_map_key_clear(KEY_CALC); break;
765 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
766 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
768 /* ASUS touchpad toggle */
769 case 0x6b: asus_map_key_clear(KEY_F21); break;
772 case 0x38: asus_map_key_clear(KEY_PROG1); break;
774 /* Fn+C ASUS Splendid */
775 case 0xba: asus_map_key_clear(KEY_PROG2); break;
777 /* Fn+Space Power4Gear Hybrid */
778 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
780 /* Fn+F5 "fan" symbol on FX503VD */
781 case 0x99: asus_map_key_clear(KEY_PROG4); break;
784 /* ASUS lazily declares 256 usages, ignore the rest,
785 * as some make the keyboard appear as a pointer device. */
790 * Check and enable backlight only on devices with UsagePage ==
791 * 0xff31 to avoid initializing the keyboard firmware multiple
792 * times on devices with multiple HID descriptors but same
795 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
796 drvdata->enable_backlight = true;
798 set_bit(EV_REP, hi->input->evbit);
802 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
803 switch (usage->hid & HID_USAGE) {
804 case 0xff01: asus_map_key_clear(BTN_1); break;
805 case 0xff02: asus_map_key_clear(BTN_2); break;
806 case 0xff03: asus_map_key_clear(BTN_3); break;
807 case 0xff04: asus_map_key_clear(BTN_4); break;
808 case 0xff05: asus_map_key_clear(BTN_5); break;
809 case 0xff06: asus_map_key_clear(BTN_6); break;
810 case 0xff07: asus_map_key_clear(BTN_7); break;
811 case 0xff08: asus_map_key_clear(BTN_8); break;
812 case 0xff09: asus_map_key_clear(BTN_9); break;
813 case 0xff0a: asus_map_key_clear(BTN_A); break;
814 case 0xff0b: asus_map_key_clear(BTN_B); break;
815 case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
816 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
817 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
818 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
819 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
820 case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
825 set_bit(EV_REP, hi->input->evbit);
829 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
830 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
831 switch (usage->hid & HID_USAGE) {
832 case 0xe2: /* Mute */
833 case 0xe9: /* Volume up */
834 case 0xea: /* Volume down */
837 /* Ignore dummy Consumer usages which make the
838 * keyboard incorrectly appear as a pointer device.
845 * The mute button is broken and only sends press events, we
846 * deal with this in our raw_event handler, so do not map it.
848 if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
849 usage->hid == (HID_UP_CONSUMER | 0xe2)) {
850 input_set_capability(hi->input, EV_KEY, KEY_MUTE);
857 static int asus_start_multitouch(struct hid_device *hdev)
860 static const unsigned char buf[] = {
861 FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
863 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
867 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
871 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
872 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
876 if (ret != sizeof(buf)) {
877 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
884 static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
886 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
889 return asus_start_multitouch(hdev);
894 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
897 struct asus_drvdata *drvdata;
899 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
900 if (drvdata == NULL) {
901 hid_err(hdev, "Can't alloc Asus descriptor\n");
905 hid_set_drvdata(hdev, drvdata);
907 drvdata->quirks = id->driver_data;
910 * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
911 * Thus, identify T90CHI dock with product name string.
913 if (strstr(hdev->name, "T90CHI")) {
914 drvdata->quirks &= ~QUIRK_T100CHI;
915 drvdata->quirks |= QUIRK_T90CHI;
918 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
919 drvdata->tp = &asus_i2c_tp;
921 if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && hid_is_usb(hdev)) {
922 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
924 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
925 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
927 * The T100HA uses the same USB-ids as the T100TAF and
928 * the T200TA uses the same USB-ids as the T100TA, while
929 * both have different max x/y values as the T100TA[F].
931 if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
932 drvdata->tp = &asus_t100ha_tp;
933 else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
934 drvdata->tp = &asus_t200ta_tp;
936 drvdata->tp = &asus_t100ta_tp;
940 if (drvdata->quirks & QUIRK_T100CHI) {
942 * All functionality is on a single HID interface and for
943 * userspace the touchpad must be a separate input_dev.
945 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
946 drvdata->tp = &asus_t100chi_tp;
949 if ((drvdata->quirks & QUIRK_MEDION_E1239T) && hid_is_usb(hdev)) {
950 struct usb_host_interface *alt =
951 to_usb_interface(hdev->dev.parent)->altsetting;
953 if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
954 /* For separate input-devs for tp and tp toggle key */
955 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
956 drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
957 drvdata->tp = &medion_e1239t_tp;
961 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
962 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
964 drvdata->hdev = hdev;
966 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
967 ret = asus_battery_probe(hdev);
970 "Asus hid battery_probe failed: %d\n", ret);
975 ret = hid_parse(hdev);
977 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
981 /* use hid-multitouch for T101HA touchpad */
982 if (id->driver_data & QUIRK_T101HA_DOCK &&
983 hdev->collection->usage == HID_GD_MOUSE)
986 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
988 hid_err(hdev, "Asus hw start failed: %d\n", ret);
992 if (!drvdata->input) {
993 hid_err(hdev, "Asus input not registered\n");
999 drvdata->input->name = "Asus TouchPad";
1001 drvdata->input->name = "Asus Keyboard";
1005 ret = asus_start_multitouch(hdev);
1016 static void asus_remove(struct hid_device *hdev)
1018 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1020 if (drvdata->kbd_backlight) {
1021 drvdata->kbd_backlight->removed = true;
1022 cancel_work_sync(&drvdata->kbd_backlight->work);
1028 static const __u8 asus_g752_fixed_rdesc[] = {
1029 0x19, 0x00, /* Usage Minimum (0x00) */
1030 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
1033 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1034 unsigned int *rsize)
1036 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1038 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
1039 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
1040 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
1043 /* For the T100TA/T200TA keyboard dock */
1044 if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
1045 (*rsize == 76 || *rsize == 101) &&
1046 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
1047 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
1048 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
1050 /* For the T100CHI/T90CHI keyboard dock */
1051 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1055 if (drvdata->quirks & QUIRK_T100CHI) {
1064 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
1065 * (FFh) and clear the flags in the Input() byte.
1066 * Note the descriptor has a bogus 0 byte at the end so we
1067 * only need 1 extra byte.
1069 if (*rsize == rsize_orig &&
1070 rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
1071 *rsize = rsize_orig + 1;
1072 rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
1076 hid_info(hdev, "Fixing up %s keyb report descriptor\n",
1077 drvdata->quirks & QUIRK_T100CHI ?
1078 "T100CHI" : "T90CHI");
1079 memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1081 rdesc[offs + 1] = 0x00;
1082 rdesc[offs + 2] = 0x29;
1083 rdesc[offs + 3] = 0xff;
1084 rdesc[offs + 14] = 0x00;
1088 if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1089 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1090 /* report is missing usage mninum and maximum */
1092 size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1094 new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1095 if (new_rdesc == NULL)
1098 hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1099 /* copy the valid part */
1100 memcpy(new_rdesc, rdesc, 61);
1101 /* insert missing part */
1102 memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1103 /* copy remaining data */
1104 memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1113 static const struct hid_device_id asus_devices[] = {
1114 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1115 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
1116 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1117 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1118 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1119 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1120 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1121 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
1122 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1123 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1124 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1125 USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1126 QUIRK_USE_KBD_BACKLIGHT },
1127 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1128 USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1129 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1130 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1131 USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
1132 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1133 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1134 USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD), QUIRK_T101HA_DOCK },
1135 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
1136 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
1137 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
1138 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1139 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1140 { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
1141 QUIRK_MEDION_E1239T },
1144 MODULE_DEVICE_TABLE(hid, asus_devices);
1146 static struct hid_driver asus_driver = {
1148 .id_table = asus_devices,
1149 .report_fixup = asus_report_fixup,
1150 .probe = asus_probe,
1151 .remove = asus_remove,
1152 .input_mapping = asus_input_mapping,
1153 .input_configured = asus_input_configured,
1155 .reset_resume = asus_reset_resume,
1157 .event = asus_event,
1158 .raw_event = asus_raw_event
1160 module_hid_driver(asus_driver);
1162 MODULE_LICENSE("GPL");