2 * HID driver for Asus notebook built-in keyboard.
3 * Fixes small logical maximum to match usage maximum.
5 * Currently supported devices are:
9 * Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
11 * This module based on hid-ortek by
12 * Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
13 * Copyright (c) 2011 Jiri Kosina
15 * This module has been updated to add support for Asus i2c touchpad.
17 * Copyright (c) 2016 Brendan McGrath <redmcg@redmandi.dyndns.org>
18 * Copyright (c) 2016 Victor Vlasenko <victor.vlasenko@sysgears.com>
19 * Copyright (c) 2016 Frederik Wenigwieser <frederik.wenigwieser@gmail.com>
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the Free
25 * Software Foundation; either version 2 of the License, or (at your option)
29 #include <linux/hid.h>
30 #include <linux/module.h>
31 #include <linux/input/mt.h>
35 MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
36 MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
37 MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
38 MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
39 MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
41 #define FEATURE_REPORT_ID 0x0d
42 #define INPUT_REPORT_ID 0x5d
43 #define FEATURE_KBD_REPORT_ID 0x5a
45 #define INPUT_REPORT_SIZE 28
46 #define FEATURE_KBD_REPORT_SIZE 16
48 #define SUPPORT_KBD_BACKLIGHT BIT(0)
50 #define MAX_CONTACTS 5
54 #define MAX_TOUCH_MAJOR 8
55 #define MAX_PRESSURE 128
57 #define CONTACT_DATA_SIZE 5
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 QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
67 #define QUIRK_NO_INIT_REPORTS BIT(1)
68 #define QUIRK_SKIP_INPUT_MAPPING BIT(2)
69 #define QUIRK_IS_MULTITOUCH BIT(3)
70 #define QUIRK_NO_CONSUMER_USAGES BIT(4)
71 #define QUIRK_USE_KBD_BACKLIGHT BIT(5)
73 #define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
74 QUIRK_NO_INIT_REPORTS | \
75 QUIRK_NO_CONSUMER_USAGES)
76 #define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
77 QUIRK_SKIP_INPUT_MAPPING | \
80 #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
82 struct asus_kbd_leds {
83 struct led_classdev cdev;
84 struct hid_device *hdev;
85 struct work_struct work;
86 unsigned int brightness;
92 struct input_dev *input;
93 struct asus_kbd_leds *kbd_backlight;
94 bool enable_backlight;
97 static void asus_report_contact_down(struct input_dev *input,
98 int toolType, u8 *data)
100 int touch_major, pressure;
101 int x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
102 int y = MAX_Y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
104 if (toolType == MT_TOOL_PALM) {
105 touch_major = MAX_TOUCH_MAJOR;
106 pressure = MAX_PRESSURE;
108 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
109 pressure = data[4] & CONTACT_PRESSURE_MASK;
112 input_report_abs(input, ABS_MT_POSITION_X, x);
113 input_report_abs(input, ABS_MT_POSITION_Y, y);
114 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
115 input_report_abs(input, ABS_MT_PRESSURE, pressure);
118 /* Required for Synaptics Palm Detection */
119 static void asus_report_tool_width(struct input_dev *input)
121 struct input_mt *mt = input->mt;
122 struct input_mt_slot *oldest;
129 for (i = 0; i < mt->num_slots; ++i) {
130 struct input_mt_slot *ps = &mt->slots[i];
131 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
135 if ((id - oldid) & TRKID_SGN) {
143 input_report_abs(input, ABS_TOOL_WIDTH,
144 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
148 static void asus_report_input(struct input_dev *input, u8 *data)
151 u8 *contactData = data + 2;
153 for (i = 0; i < MAX_CONTACTS; i++) {
154 bool down = !!(data[1] & BIT(i+3));
155 int toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
156 MT_TOOL_PALM : MT_TOOL_FINGER;
158 input_mt_slot(input, i);
159 input_mt_report_slot_state(input, toolType, down);
162 asus_report_contact_down(input, toolType, contactData);
163 contactData += CONTACT_DATA_SIZE;
167 input_report_key(input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
168 asus_report_tool_width(input);
170 input_mt_sync_frame(input);
174 static int asus_raw_event(struct hid_device *hdev,
175 struct hid_report *report, u8 *data, int size)
177 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
179 if (drvdata->quirks & QUIRK_IS_MULTITOUCH &&
180 data[0] == INPUT_REPORT_ID &&
181 size == INPUT_REPORT_SIZE) {
182 asus_report_input(drvdata->input, data);
189 static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
191 unsigned char *dmabuf;
194 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
198 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
199 buf_size, HID_FEATURE_REPORT,
206 static int asus_kbd_init(struct hid_device *hdev)
208 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
209 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
212 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
214 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
219 static int asus_kbd_get_functions(struct hid_device *hdev,
220 unsigned char *kbd_func)
222 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
226 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
228 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
232 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
236 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
237 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
240 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
245 *kbd_func = readbuf[6];
251 static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
252 enum led_brightness brightness)
254 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
256 if (led->brightness == brightness)
259 led->brightness = brightness;
260 schedule_work(&led->work);
263 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
265 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
268 return led->brightness;
271 static void asus_kbd_backlight_work(struct work_struct *work)
273 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
274 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
280 buf[4] = led->brightness;
282 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
284 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
287 static int asus_kbd_register_leds(struct hid_device *hdev)
289 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
290 unsigned char kbd_func;
293 /* Initialize keyboard */
294 ret = asus_kbd_init(hdev);
298 /* Get keyboard functions */
299 ret = asus_kbd_get_functions(hdev, &kbd_func);
303 /* Check for backlight support */
304 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
307 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
308 sizeof(struct asus_kbd_leds),
310 if (!drvdata->kbd_backlight)
313 drvdata->kbd_backlight->removed = false;
314 drvdata->kbd_backlight->brightness = 0;
315 drvdata->kbd_backlight->hdev = hdev;
316 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
317 drvdata->kbd_backlight->cdev.max_brightness = 3;
318 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
319 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
320 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
322 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
324 /* No need to have this still around */
325 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
331 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
333 struct input_dev *input = hi->input;
334 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
336 if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
339 input_set_abs_params(input, ABS_MT_POSITION_X, 0, MAX_X, 0, 0);
340 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, MAX_Y, 0, 0);
341 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, MAX_TOUCH_MAJOR, 0, 0);
342 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, MAX_TOUCH_MAJOR, 0, 0);
343 input_set_abs_params(input, ABS_MT_PRESSURE, 0, MAX_PRESSURE, 0, 0);
345 __set_bit(BTN_LEFT, input->keybit);
346 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
348 ret = input_mt_init_slots(input, MAX_CONTACTS, INPUT_MT_POINTER);
351 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
356 drvdata->input = input;
358 if (drvdata->enable_backlight && asus_kbd_register_leds(hdev))
359 hid_warn(hdev, "Failed to initialize backlight.\n");
364 #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
366 static int asus_input_mapping(struct hid_device *hdev,
367 struct hid_input *hi, struct hid_field *field,
368 struct hid_usage *usage, unsigned long **bit,
371 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
373 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
374 /* Don't map anything from the HID report.
375 * We do it all manually in asus_input_configured
380 /* ASUS-specific keyboard hotkeys */
381 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
382 set_bit(EV_REP, hi->input->evbit);
383 switch (usage->hid & HID_USAGE) {
384 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
385 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
386 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
387 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
388 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
389 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
390 case 0xb5: asus_map_key_clear(KEY_CALC); break;
391 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
392 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
394 /* ASUS touchpad toggle */
395 case 0x6b: asus_map_key_clear(KEY_F21); break;
398 case 0x38: asus_map_key_clear(KEY_PROG1); break;
400 /* Fn+C ASUS Splendid */
401 case 0xba: asus_map_key_clear(KEY_PROG2); break;
403 /* Fn+Space Power4Gear Hybrid */
404 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
407 /* ASUS lazily declares 256 usages, ignore the rest,
408 * as some make the keyboard appear as a pointer device. */
413 * Check and enable backlight only on devices with UsagePage ==
414 * 0xff31 to avoid initializing the keyboard firmware multiple
415 * times on devices with multiple HID descriptors but same
418 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
419 drvdata->enable_backlight = true;
424 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
425 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
426 switch (usage->hid & HID_USAGE) {
427 case 0xe2: /* Mute */
428 case 0xe9: /* Volume up */
429 case 0xea: /* Volume down */
432 /* Ignore dummy Consumer usages which make the
433 * keyboard incorrectly appear as a pointer device.
442 static int asus_start_multitouch(struct hid_device *hdev)
445 const unsigned char buf[] = { FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00 };
446 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
450 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
454 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
455 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
459 if (ret != sizeof(buf)) {
460 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
467 static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
469 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
471 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
472 return asus_start_multitouch(hdev);
477 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
480 struct asus_drvdata *drvdata;
482 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
483 if (drvdata == NULL) {
484 hid_err(hdev, "Can't alloc Asus descriptor\n");
488 hid_set_drvdata(hdev, drvdata);
490 drvdata->quirks = id->driver_data;
492 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
493 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
495 ret = hid_parse(hdev);
497 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
501 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
503 hid_err(hdev, "Asus hw start failed: %d\n", ret);
507 if (!drvdata->input) {
508 hid_err(hdev, "Asus input not registered\n");
513 if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
514 drvdata->input->name = "Asus TouchPad";
516 drvdata->input->name = "Asus Keyboard";
519 if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
520 ret = asus_start_multitouch(hdev);
531 static void asus_remove(struct hid_device *hdev)
533 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
535 if (drvdata->kbd_backlight) {
536 drvdata->kbd_backlight->removed = true;
537 cancel_work_sync(&drvdata->kbd_backlight->work);
541 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
544 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
546 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
547 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
548 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
554 static const struct hid_device_id asus_devices[] = {
555 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
556 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
557 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
558 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
559 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
560 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1) },
561 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
562 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
565 MODULE_DEVICE_TABLE(hid, asus_devices);
567 static struct hid_driver asus_driver = {
569 .id_table = asus_devices,
570 .report_fixup = asus_report_fixup,
572 .remove = asus_remove,
573 .input_mapping = asus_input_mapping,
574 .input_configured = asus_input_configured,
576 .reset_resume = asus_reset_resume,
578 .raw_event = asus_raw_event
580 module_hid_driver(asus_driver);
582 MODULE_LICENSE("GPL");