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)
72 #define QUIRK_T100_KEYBOARD BIT(6)
74 #define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
75 QUIRK_NO_INIT_REPORTS | \
76 QUIRK_NO_CONSUMER_USAGES)
77 #define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
78 QUIRK_SKIP_INPUT_MAPPING | \
81 #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
83 struct asus_kbd_leds
{
84 struct led_classdev cdev
;
85 struct hid_device
*hdev
;
86 struct work_struct work
;
87 unsigned int brightness
;
93 struct input_dev
*input
;
94 struct asus_kbd_leds
*kbd_backlight
;
95 bool enable_backlight
;
98 static void asus_report_contact_down(struct input_dev
*input
,
99 int toolType
, u8
*data
)
101 int touch_major
, pressure
;
102 int x
= (data
[0] & CONTACT_X_MSB_MASK
) << 4 | data
[1];
103 int y
= MAX_Y
- ((data
[0] & CONTACT_Y_MSB_MASK
) << 8 | data
[2]);
105 if (toolType
== MT_TOOL_PALM
) {
106 touch_major
= MAX_TOUCH_MAJOR
;
107 pressure
= MAX_PRESSURE
;
109 touch_major
= (data
[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK
;
110 pressure
= data
[4] & CONTACT_PRESSURE_MASK
;
113 input_report_abs(input
, ABS_MT_POSITION_X
, x
);
114 input_report_abs(input
, ABS_MT_POSITION_Y
, y
);
115 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, touch_major
);
116 input_report_abs(input
, ABS_MT_PRESSURE
, pressure
);
119 /* Required for Synaptics Palm Detection */
120 static void asus_report_tool_width(struct input_dev
*input
)
122 struct input_mt
*mt
= input
->mt
;
123 struct input_mt_slot
*oldest
;
130 for (i
= 0; i
< mt
->num_slots
; ++i
) {
131 struct input_mt_slot
*ps
= &mt
->slots
[i
];
132 int id
= input_mt_get_value(ps
, ABS_MT_TRACKING_ID
);
136 if ((id
- oldid
) & TRKID_SGN
) {
144 input_report_abs(input
, ABS_TOOL_WIDTH
,
145 input_mt_get_value(oldest
, ABS_MT_TOUCH_MAJOR
));
149 static void asus_report_input(struct input_dev
*input
, u8
*data
)
152 u8
*contactData
= data
+ 2;
154 for (i
= 0; i
< MAX_CONTACTS
; i
++) {
155 bool down
= !!(data
[1] & BIT(i
+3));
156 int toolType
= contactData
[3] & CONTACT_TOOL_TYPE_MASK
?
157 MT_TOOL_PALM
: MT_TOOL_FINGER
;
159 input_mt_slot(input
, i
);
160 input_mt_report_slot_state(input
, toolType
, down
);
163 asus_report_contact_down(input
, toolType
, contactData
);
164 contactData
+= CONTACT_DATA_SIZE
;
168 input_report_key(input
, BTN_LEFT
, data
[1] & BTN_LEFT_MASK
);
169 asus_report_tool_width(input
);
171 input_mt_sync_frame(input
);
175 static int asus_raw_event(struct hid_device
*hdev
,
176 struct hid_report
*report
, u8
*data
, int size
)
178 struct asus_drvdata
*drvdata
= hid_get_drvdata(hdev
);
180 if (drvdata
->quirks
& QUIRK_IS_MULTITOUCH
&&
181 data
[0] == INPUT_REPORT_ID
&&
182 size
== INPUT_REPORT_SIZE
) {
183 asus_report_input(drvdata
->input
, data
);
190 static int asus_kbd_set_report(struct hid_device
*hdev
, u8
*buf
, size_t buf_size
)
192 unsigned char *dmabuf
;
195 dmabuf
= kmemdup(buf
, buf_size
, GFP_KERNEL
);
199 ret
= hid_hw_raw_request(hdev
, FEATURE_KBD_REPORT_ID
, dmabuf
,
200 buf_size
, HID_FEATURE_REPORT
,
207 static int asus_kbd_init(struct hid_device
*hdev
)
209 u8 buf
[] = { FEATURE_KBD_REPORT_ID
, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
210 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
213 ret
= asus_kbd_set_report(hdev
, buf
, sizeof(buf
));
215 hid_err(hdev
, "Asus failed to send init command: %d\n", ret
);
220 static int asus_kbd_get_functions(struct hid_device
*hdev
,
221 unsigned char *kbd_func
)
223 u8 buf
[] = { FEATURE_KBD_REPORT_ID
, 0x05, 0x20, 0x31, 0x00, 0x08 };
227 ret
= asus_kbd_set_report(hdev
, buf
, sizeof(buf
));
229 hid_err(hdev
, "Asus failed to send configuration command: %d\n", ret
);
233 readbuf
= kzalloc(FEATURE_KBD_REPORT_SIZE
, GFP_KERNEL
);
237 ret
= hid_hw_raw_request(hdev
, FEATURE_KBD_REPORT_ID
, readbuf
,
238 FEATURE_KBD_REPORT_SIZE
, HID_FEATURE_REPORT
,
241 hid_err(hdev
, "Asus failed to request functions: %d\n", ret
);
246 *kbd_func
= readbuf
[6];
252 static void asus_kbd_backlight_set(struct led_classdev
*led_cdev
,
253 enum led_brightness brightness
)
255 struct asus_kbd_leds
*led
= container_of(led_cdev
, struct asus_kbd_leds
,
257 if (led
->brightness
== brightness
)
260 led
->brightness
= brightness
;
261 schedule_work(&led
->work
);
264 static enum led_brightness
asus_kbd_backlight_get(struct led_classdev
*led_cdev
)
266 struct asus_kbd_leds
*led
= container_of(led_cdev
, struct asus_kbd_leds
,
269 return led
->brightness
;
272 static void asus_kbd_backlight_work(struct work_struct
*work
)
274 struct asus_kbd_leds
*led
= container_of(work
, struct asus_kbd_leds
, work
);
275 u8 buf
[] = { FEATURE_KBD_REPORT_ID
, 0xba, 0xc5, 0xc4, 0x00 };
281 buf
[4] = led
->brightness
;
283 ret
= asus_kbd_set_report(led
->hdev
, buf
, sizeof(buf
));
285 hid_err(led
->hdev
, "Asus failed to set keyboard backlight: %d\n", ret
);
288 static int asus_kbd_register_leds(struct hid_device
*hdev
)
290 struct asus_drvdata
*drvdata
= hid_get_drvdata(hdev
);
291 unsigned char kbd_func
;
294 /* Initialize keyboard */
295 ret
= asus_kbd_init(hdev
);
299 /* Get keyboard functions */
300 ret
= asus_kbd_get_functions(hdev
, &kbd_func
);
304 /* Check for backlight support */
305 if (!(kbd_func
& SUPPORT_KBD_BACKLIGHT
))
308 drvdata
->kbd_backlight
= devm_kzalloc(&hdev
->dev
,
309 sizeof(struct asus_kbd_leds
),
311 if (!drvdata
->kbd_backlight
)
314 drvdata
->kbd_backlight
->removed
= false;
315 drvdata
->kbd_backlight
->brightness
= 0;
316 drvdata
->kbd_backlight
->hdev
= hdev
;
317 drvdata
->kbd_backlight
->cdev
.name
= "asus::kbd_backlight";
318 drvdata
->kbd_backlight
->cdev
.max_brightness
= 3;
319 drvdata
->kbd_backlight
->cdev
.brightness_set
= asus_kbd_backlight_set
;
320 drvdata
->kbd_backlight
->cdev
.brightness_get
= asus_kbd_backlight_get
;
321 INIT_WORK(&drvdata
->kbd_backlight
->work
, asus_kbd_backlight_work
);
323 ret
= devm_led_classdev_register(&hdev
->dev
, &drvdata
->kbd_backlight
->cdev
);
325 /* No need to have this still around */
326 devm_kfree(&hdev
->dev
, drvdata
->kbd_backlight
);
332 static int asus_input_configured(struct hid_device
*hdev
, struct hid_input
*hi
)
334 struct input_dev
*input
= hi
->input
;
335 struct asus_drvdata
*drvdata
= hid_get_drvdata(hdev
);
337 if (drvdata
->quirks
& QUIRK_IS_MULTITOUCH
) {
340 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, MAX_X
, 0, 0);
341 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, MAX_Y
, 0, 0);
342 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0, MAX_TOUCH_MAJOR
, 0, 0);
343 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0, MAX_TOUCH_MAJOR
, 0, 0);
344 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, MAX_PRESSURE
, 0, 0);
346 __set_bit(BTN_LEFT
, input
->keybit
);
347 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
349 ret
= input_mt_init_slots(input
, MAX_CONTACTS
, INPUT_MT_POINTER
);
352 hid_err(hdev
, "Asus input mt init slots failed: %d\n", ret
);
357 drvdata
->input
= input
;
359 if (drvdata
->enable_backlight
&& asus_kbd_register_leds(hdev
))
360 hid_warn(hdev
, "Failed to initialize backlight.\n");
365 #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
367 static int asus_input_mapping(struct hid_device
*hdev
,
368 struct hid_input
*hi
, struct hid_field
*field
,
369 struct hid_usage
*usage
, unsigned long **bit
,
372 struct asus_drvdata
*drvdata
= hid_get_drvdata(hdev
);
374 if (drvdata
->quirks
& QUIRK_SKIP_INPUT_MAPPING
) {
375 /* Don't map anything from the HID report.
376 * We do it all manually in asus_input_configured
381 /* ASUS-specific keyboard hotkeys */
382 if ((usage
->hid
& HID_USAGE_PAGE
) == 0xff310000) {
383 set_bit(EV_REP
, hi
->input
->evbit
);
384 switch (usage
->hid
& HID_USAGE
) {
385 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN
); break;
386 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP
); break;
387 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF
); break;
388 case 0x6c: asus_map_key_clear(KEY_SLEEP
); break;
389 case 0x82: asus_map_key_clear(KEY_CAMERA
); break;
390 case 0x88: asus_map_key_clear(KEY_RFKILL
); break;
391 case 0xb5: asus_map_key_clear(KEY_CALC
); break;
392 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP
); break;
393 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN
); break;
395 /* ASUS touchpad toggle */
396 case 0x6b: asus_map_key_clear(KEY_F21
); break;
399 case 0x38: asus_map_key_clear(KEY_PROG1
); break;
401 /* Fn+C ASUS Splendid */
402 case 0xba: asus_map_key_clear(KEY_PROG2
); break;
404 /* Fn+Space Power4Gear Hybrid */
405 case 0x5c: asus_map_key_clear(KEY_PROG3
); break;
408 /* ASUS lazily declares 256 usages, ignore the rest,
409 * as some make the keyboard appear as a pointer device. */
414 * Check and enable backlight only on devices with UsagePage ==
415 * 0xff31 to avoid initializing the keyboard firmware multiple
416 * times on devices with multiple HID descriptors but same
419 if (drvdata
->quirks
& QUIRK_USE_KBD_BACKLIGHT
)
420 drvdata
->enable_backlight
= true;
425 if (drvdata
->quirks
& QUIRK_NO_CONSUMER_USAGES
&&
426 (usage
->hid
& HID_USAGE_PAGE
) == HID_UP_CONSUMER
) {
427 switch (usage
->hid
& HID_USAGE
) {
428 case 0xe2: /* Mute */
429 case 0xe9: /* Volume up */
430 case 0xea: /* Volume down */
433 /* Ignore dummy Consumer usages which make the
434 * keyboard incorrectly appear as a pointer device.
443 static int asus_start_multitouch(struct hid_device
*hdev
)
446 const unsigned char buf
[] = { FEATURE_REPORT_ID
, 0x00, 0x03, 0x01, 0x00 };
447 unsigned char *dmabuf
= kmemdup(buf
, sizeof(buf
), GFP_KERNEL
);
451 hid_err(hdev
, "Asus failed to alloc dma buf: %d\n", ret
);
455 ret
= hid_hw_raw_request(hdev
, dmabuf
[0], dmabuf
, sizeof(buf
),
456 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
460 if (ret
!= sizeof(buf
)) {
461 hid_err(hdev
, "Asus failed to start multitouch: %d\n", ret
);
468 static int __maybe_unused
asus_reset_resume(struct hid_device
*hdev
)
470 struct asus_drvdata
*drvdata
= hid_get_drvdata(hdev
);
472 if (drvdata
->quirks
& QUIRK_IS_MULTITOUCH
)
473 return asus_start_multitouch(hdev
);
478 static int asus_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
481 struct asus_drvdata
*drvdata
;
483 drvdata
= devm_kzalloc(&hdev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
484 if (drvdata
== NULL
) {
485 hid_err(hdev
, "Can't alloc Asus descriptor\n");
489 hid_set_drvdata(hdev
, drvdata
);
491 drvdata
->quirks
= id
->driver_data
;
493 if (drvdata
->quirks
& QUIRK_NO_INIT_REPORTS
)
494 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
496 ret
= hid_parse(hdev
);
498 hid_err(hdev
, "Asus hid parse failed: %d\n", ret
);
502 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
504 hid_err(hdev
, "Asus hw start failed: %d\n", ret
);
508 if (!drvdata
->input
) {
509 hid_err(hdev
, "Asus input not registered\n");
514 if (drvdata
->quirks
& QUIRK_IS_MULTITOUCH
) {
515 drvdata
->input
->name
= "Asus TouchPad";
517 drvdata
->input
->name
= "Asus Keyboard";
520 if (drvdata
->quirks
& QUIRK_IS_MULTITOUCH
) {
521 ret
= asus_start_multitouch(hdev
);
532 static void asus_remove(struct hid_device
*hdev
)
534 struct asus_drvdata
*drvdata
= hid_get_drvdata(hdev
);
536 if (drvdata
->kbd_backlight
) {
537 drvdata
->kbd_backlight
->removed
= true;
538 cancel_work_sync(&drvdata
->kbd_backlight
->work
);
544 static __u8
*asus_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
547 struct asus_drvdata
*drvdata
= hid_get_drvdata(hdev
);
549 if (drvdata
->quirks
& QUIRK_FIX_NOTEBOOK_REPORT
&&
550 *rsize
>= 56 && rdesc
[54] == 0x25 && rdesc
[55] == 0x65) {
551 hid_info(hdev
, "Fixing up Asus notebook report descriptor\n");
554 if (drvdata
->quirks
& QUIRK_T100_KEYBOARD
&&
555 *rsize
== 76 && rdesc
[73] == 0x81 && rdesc
[74] == 0x01) {
556 hid_info(hdev
, "Fixing up Asus T100 keyb report descriptor\n");
557 rdesc
[74] &= ~HID_MAIN_ITEM_CONSTANT
;
563 static const struct hid_device_id asus_devices
[] = {
564 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK
,
565 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD
), I2C_KEYBOARD_QUIRKS
},
566 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK
,
567 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD
), I2C_TOUCHPAD_QUIRKS
},
568 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK
,
569 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1
) },
570 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK
,
571 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2
), QUIRK_USE_KBD_BACKLIGHT
},
572 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK
,
573 USB_DEVICE_ID_ASUSTEK_T100_KEYBOARD
),
574 QUIRK_T100_KEYBOARD
| QUIRK_NO_CONSUMER_USAGES
},
577 MODULE_DEVICE_TABLE(hid
, asus_devices
);
579 static struct hid_driver asus_driver
= {
581 .id_table
= asus_devices
,
582 .report_fixup
= asus_report_fixup
,
584 .remove
= asus_remove
,
585 .input_mapping
= asus_input_mapping
,
586 .input_configured
= asus_input_configured
,
588 .reset_resume
= asus_reset_resume
,
590 .raw_event
= asus_raw_event
592 module_hid_driver(asus_driver
);
594 MODULE_LICENSE("GPL");