]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/hid/hid-asus.c
HID: asus: Add event handler to catch unmapped Asus Vendor UsagePage codes
[mirror_ubuntu-hirsute-kernel.git] / drivers / hid / hid-asus.c
CommitLineData
eeb01a57 1/*
b94f7d5d 2 * HID driver for Asus notebook built-in keyboard.
eeb01a57
YF
3 * Fixes small logical maximum to match usage maximum.
4 *
b94f7d5d
YF
5 * Currently supported devices are:
6 * EeeBook X205TA
7 * VivoBook E200HA
8 *
eeb01a57
YF
9 * Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
10 *
11 * This module based on hid-ortek by
12 * Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
13 * Copyright (c) 2011 Jiri Kosina
9ce12d8b
BM
14 *
15 * This module has been updated to add support for Asus i2c touchpad.
16 *
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>
eeb01a57
YF
20 */
21
22/*
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)
26 * any later version.
27 */
28
762f948c 29#include <linux/dmi.h>
eeb01a57
YF
30#include <linux/hid.h>
31#include <linux/module.h>
3b692c55 32#include <linux/platform_data/x86/asus-wmi.h>
9ce12d8b 33#include <linux/input/mt.h>
57573c54 34#include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
eeb01a57
YF
35
36#include "hid-ids.h"
37
9ce12d8b
BM
38MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
39MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
40MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
41MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
42MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
43
57573c54
HG
44#define T100_TPAD_INTF 2
45
73c75d39 46#define T100CHI_MOUSE_REPORT_ID 0x06
9ce12d8b
BM
47#define FEATURE_REPORT_ID 0x0d
48#define INPUT_REPORT_ID 0x5d
af22a610 49#define FEATURE_KBD_REPORT_ID 0x5a
af22a610
CC
50#define FEATURE_KBD_REPORT_SIZE 16
51
52#define SUPPORT_KBD_BACKLIGHT BIT(0)
9ce12d8b 53
9ce12d8b
BM
54#define MAX_TOUCH_MAJOR 8
55#define MAX_PRESSURE 128
56
9ce12d8b
BM
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
63
64#define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
65#define QUIRK_NO_INIT_REPORTS BIT(1)
66#define QUIRK_SKIP_INPUT_MAPPING BIT(2)
67#define QUIRK_IS_MULTITOUCH BIT(3)
0485b1ec 68#define QUIRK_NO_CONSUMER_USAGES BIT(4)
af22a610 69#define QUIRK_USE_KBD_BACKLIGHT BIT(5)
76dd1fbe 70#define QUIRK_T100_KEYBOARD BIT(6)
5703e52c 71#define QUIRK_T100CHI BIT(7)
832e1eee 72#define QUIRK_G752_KEYBOARD BIT(8)
9ce12d8b 73
a93913e1 74#define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
0485b1ec
MH
75 QUIRK_NO_INIT_REPORTS | \
76 QUIRK_NO_CONSUMER_USAGES)
c81760b9 77#define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
9ce12d8b
BM
78 QUIRK_SKIP_INPUT_MAPPING | \
79 QUIRK_IS_MULTITOUCH)
80
81#define TRKID_SGN ((TRKID_MAX + 1) >> 1)
82
af22a610
CC
83struct asus_kbd_leds {
84 struct led_classdev cdev;
85 struct hid_device *hdev;
86 struct work_struct work;
87 unsigned int brightness;
88 bool removed;
89};
90
c81760b9
HG
91struct asus_touchpad_info {
92 int max_x;
93 int max_y;
b61d43e6
HG
94 int res_x;
95 int res_y;
c81760b9
HG
96 int contact_size;
97 int max_contacts;
98};
99
9ce12d8b
BM
100struct asus_drvdata {
101 unsigned long quirks;
102 struct input_dev *input;
af22a610 103 struct asus_kbd_leds *kbd_backlight;
c81760b9 104 const struct asus_touchpad_info *tp;
af22a610 105 bool enable_backlight;
9ce12d8b
BM
106};
107
c81760b9
HG
108static const struct asus_touchpad_info asus_i2c_tp = {
109 .max_x = 2794,
110 .max_y = 1758,
111 .contact_size = 5,
112 .max_contacts = 5,
113};
114
115static const struct asus_touchpad_info asus_t100ta_tp = {
116 .max_x = 2240,
25cc2611 117 .max_y = 1120,
b61d43e6
HG
118 .res_x = 30, /* units/mm */
119 .res_y = 27, /* units/mm */
c81760b9
HG
120 .contact_size = 5,
121 .max_contacts = 5,
122};
123
762f948c
HG
124static const struct asus_touchpad_info asus_t100ha_tp = {
125 .max_x = 2640,
126 .max_y = 1320,
127 .res_x = 30, /* units/mm */
128 .res_y = 29, /* units/mm */
129 .contact_size = 5,
130 .max_contacts = 5,
131};
132
dbd3ef28
HG
133static const struct asus_touchpad_info asus_t200ta_tp = {
134 .max_x = 3120,
135 .max_y = 1716,
136 .res_x = 30, /* units/mm */
137 .res_y = 28, /* units/mm */
138 .contact_size = 5,
139 .max_contacts = 5,
140};
141
73c75d39
HG
142static const struct asus_touchpad_info asus_t100chi_tp = {
143 .max_x = 2640,
144 .max_y = 1320,
145 .res_x = 31, /* units/mm */
146 .res_y = 29, /* units/mm */
147 .contact_size = 3,
148 .max_contacts = 4,
149};
150
c81760b9 151static void asus_report_contact_down(struct asus_drvdata *drvdat,
9ce12d8b
BM
152 int toolType, u8 *data)
153{
c81760b9
HG
154 struct input_dev *input = drvdat->input;
155 int touch_major, pressure, x, y;
156
157 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
158 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
9ce12d8b 159
73c75d39
HG
160 input_report_abs(input, ABS_MT_POSITION_X, x);
161 input_report_abs(input, ABS_MT_POSITION_Y, y);
162
163 if (drvdat->tp->contact_size < 5)
164 return;
165
9ce12d8b
BM
166 if (toolType == MT_TOOL_PALM) {
167 touch_major = MAX_TOUCH_MAJOR;
168 pressure = MAX_PRESSURE;
169 } else {
170 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
171 pressure = data[4] & CONTACT_PRESSURE_MASK;
172 }
173
9ce12d8b
BM
174 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
175 input_report_abs(input, ABS_MT_PRESSURE, pressure);
176}
177
178/* Required for Synaptics Palm Detection */
c81760b9 179static void asus_report_tool_width(struct asus_drvdata *drvdat)
9ce12d8b 180{
c81760b9 181 struct input_mt *mt = drvdat->input->mt;
9ce12d8b
BM
182 struct input_mt_slot *oldest;
183 int oldid, count, i;
184
73c75d39
HG
185 if (drvdat->tp->contact_size < 5)
186 return;
187
9ce12d8b
BM
188 oldest = NULL;
189 oldid = mt->trkid;
190 count = 0;
191
192 for (i = 0; i < mt->num_slots; ++i) {
193 struct input_mt_slot *ps = &mt->slots[i];
194 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
195
196 if (id < 0)
197 continue;
198 if ((id - oldid) & TRKID_SGN) {
199 oldest = ps;
200 oldid = id;
201 }
202 count++;
203 }
204
205 if (oldest) {
c81760b9 206 input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
9ce12d8b
BM
207 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
208 }
209}
210
c81760b9 211static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
9ce12d8b 212{
73c75d39 213 int i, toolType = MT_TOOL_FINGER;
9ce12d8b
BM
214 u8 *contactData = data + 2;
215
c81760b9
HG
216 if (size != 3 + drvdat->tp->contact_size * drvdat->tp->max_contacts)
217 return 0;
218
219 for (i = 0; i < drvdat->tp->max_contacts; i++) {
9ce12d8b 220 bool down = !!(data[1] & BIT(i+3));
73c75d39
HG
221
222 if (drvdat->tp->contact_size >= 5)
223 toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
9ce12d8b
BM
224 MT_TOOL_PALM : MT_TOOL_FINGER;
225
c81760b9
HG
226 input_mt_slot(drvdat->input, i);
227 input_mt_report_slot_state(drvdat->input, toolType, down);
9ce12d8b
BM
228
229 if (down) {
c81760b9
HG
230 asus_report_contact_down(drvdat, toolType, contactData);
231 contactData += drvdat->tp->contact_size;
9ce12d8b
BM
232 }
233 }
234
c81760b9
HG
235 input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
236 asus_report_tool_width(drvdat);
237
238 input_mt_sync_frame(drvdat->input);
239 input_sync(drvdat->input);
9ce12d8b 240
c81760b9 241 return 1;
9ce12d8b
BM
242}
243
e98e3809
HG
244static int asus_event(struct hid_device *hdev, struct hid_field *field,
245 struct hid_usage *usage, __s32 value)
246{
247 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
248 (usage->hid & HID_USAGE) != 0x00 && !usage->type) {
249 hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
250 usage->hid & HID_USAGE);
251 }
252
253 return 0;
254}
255
9ce12d8b
BM
256static int asus_raw_event(struct hid_device *hdev,
257 struct hid_report *report, u8 *data, int size)
258{
259 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
260
c81760b9
HG
261 if (drvdata->tp && data[0] == INPUT_REPORT_ID)
262 return asus_report_input(drvdata, data, size);
9ce12d8b
BM
263
264 return 0;
265}
266
af22a610
CC
267static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
268{
269 unsigned char *dmabuf;
270 int ret;
271
272 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
273 if (!dmabuf)
274 return -ENOMEM;
275
276 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
277 buf_size, HID_FEATURE_REPORT,
278 HID_REQ_SET_REPORT);
279 kfree(dmabuf);
280
281 return ret;
282}
283
284static int asus_kbd_init(struct hid_device *hdev)
285{
286 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
287 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
288 int ret;
289
290 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
291 if (ret < 0)
292 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
293
294 return ret;
295}
296
297static int asus_kbd_get_functions(struct hid_device *hdev,
298 unsigned char *kbd_func)
299{
300 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
301 u8 *readbuf;
302 int ret;
303
304 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
305 if (ret < 0) {
306 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
307 return ret;
308 }
309
310 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
311 if (!readbuf)
312 return -ENOMEM;
313
314 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
315 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
316 HID_REQ_GET_REPORT);
317 if (ret < 0) {
318 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
319 kfree(readbuf);
320 return ret;
321 }
322
323 *kbd_func = readbuf[6];
324
325 kfree(readbuf);
326 return ret;
327}
328
329static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
330 enum led_brightness brightness)
331{
332 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
333 cdev);
334 if (led->brightness == brightness)
335 return;
336
337 led->brightness = brightness;
338 schedule_work(&led->work);
339}
340
341static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
342{
343 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
344 cdev);
345
346 return led->brightness;
347}
348
349static void asus_kbd_backlight_work(struct work_struct *work)
350{
351 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
352 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
353 int ret;
354
355 if (led->removed)
356 return;
357
358 buf[4] = led->brightness;
359
360 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
361 if (ret < 0)
362 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
363}
364
3b692c55
DD
365/* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
366 * precedence. We only activate HID-based backlight control when the
367 * WMI control is not available.
368 */
369static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
370{
371 u32 value;
372 int ret;
373
3fc202e8
AB
374 if (!IS_ENABLED(CONFIG_ASUS_WMI))
375 return false;
376
3b692c55
DD
377 ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS2,
378 ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
379 hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
380 if (ret)
381 return false;
382
383 return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
384}
385
af22a610
CC
386static int asus_kbd_register_leds(struct hid_device *hdev)
387{
388 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
389 unsigned char kbd_func;
390 int ret;
391
392 /* Initialize keyboard */
393 ret = asus_kbd_init(hdev);
394 if (ret < 0)
395 return ret;
396
397 /* Get keyboard functions */
398 ret = asus_kbd_get_functions(hdev, &kbd_func);
399 if (ret < 0)
400 return ret;
401
402 /* Check for backlight support */
403 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
404 return -ENODEV;
405
406 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
407 sizeof(struct asus_kbd_leds),
408 GFP_KERNEL);
409 if (!drvdata->kbd_backlight)
410 return -ENOMEM;
411
412 drvdata->kbd_backlight->removed = false;
413 drvdata->kbd_backlight->brightness = 0;
414 drvdata->kbd_backlight->hdev = hdev;
415 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
416 drvdata->kbd_backlight->cdev.max_brightness = 3;
417 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
418 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
419 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
420
421 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
422 if (ret < 0) {
423 /* No need to have this still around */
424 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
425 }
426
427 return ret;
428}
429
9ce12d8b
BM
430static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
431{
c8b1b3dd 432 struct input_dev *input = hi->input;
9ce12d8b
BM
433 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
434
73c75d39
HG
435 /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
436 if (drvdata->quirks & QUIRK_T100CHI &&
437 hi->report->id != T100CHI_MOUSE_REPORT_ID)
438 return 0;
439
c81760b9 440 if (drvdata->tp) {
9ce12d8b 441 int ret;
9ce12d8b 442
c81760b9
HG
443 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
444 drvdata->tp->max_x, 0, 0);
445 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
446 drvdata->tp->max_y, 0, 0);
b61d43e6
HG
447 input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
448 input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
73c75d39
HG
449
450 if (drvdata->tp->contact_size >= 5) {
451 input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
452 MAX_TOUCH_MAJOR, 0, 0);
453 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
454 MAX_TOUCH_MAJOR, 0, 0);
455 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
456 MAX_PRESSURE, 0, 0);
457 }
9ce12d8b
BM
458
459 __set_bit(BTN_LEFT, input->keybit);
460 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
461
c81760b9
HG
462 ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
463 INPUT_MT_POINTER);
9ce12d8b
BM
464
465 if (ret) {
466 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
467 return ret;
468 }
9ce12d8b
BM
469 }
470
c8b1b3dd
BM
471 drvdata->input = input;
472
3b692c55
DD
473 if (drvdata->enable_backlight &&
474 !asus_kbd_wmi_led_control_present(hdev) &&
475 asus_kbd_register_leds(hdev))
af22a610
CC
476 hid_warn(hdev, "Failed to initialize backlight.\n");
477
9ce12d8b
BM
478 return 0;
479}
480
a93913e1 481#define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
1caccc25 482 max, EV_KEY, (c))
9ce12d8b
BM
483static int asus_input_mapping(struct hid_device *hdev,
484 struct hid_input *hi, struct hid_field *field,
485 struct hid_usage *usage, unsigned long **bit,
486 int *max)
487{
488 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
489
490 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
491 /* Don't map anything from the HID report.
492 * We do it all manually in asus_input_configured
493 */
494 return -1;
495 }
496
73c75d39
HG
497 /*
498 * Ignore a bunch of bogus collections in the T100CHI descriptor.
499 * This avoids a bunch of non-functional hid_input devices getting
500 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
501 */
502 if (drvdata->quirks & QUIRK_T100CHI) {
503 if (field->application == (HID_UP_GENDESK | 0x0080) ||
504 usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
505 usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
506 usage->hid == (HID_UP_GENDEVCTRLS | 0x0026))
507 return -1;
508 /*
509 * We use the hid_input for the mouse report for the touchpad,
510 * keep the left button, to avoid the core removing it.
511 */
512 if (field->application == HID_GD_MOUSE &&
513 usage->hid != (HID_UP_BUTTON | 1))
514 return -1;
515 }
516
a93913e1 517 /* ASUS-specific keyboard hotkeys */
1caccc25
CC
518 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
519 set_bit(EV_REP, hi->input->evbit);
520 switch (usage->hid & HID_USAGE) {
a93913e1
MH
521 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
522 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
523 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
524 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
525 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
802b24b4 526 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
a93913e1
MH
527 case 0xb5: asus_map_key_clear(KEY_CALC); break;
528 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
529 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
1caccc25
CC
530
531 /* ASUS touchpad toggle */
a93913e1 532 case 0x6b: asus_map_key_clear(KEY_F21); break;
1caccc25
CC
533
534 /* ROG key */
a93913e1 535 case 0x38: asus_map_key_clear(KEY_PROG1); break;
1caccc25
CC
536
537 /* Fn+C ASUS Splendid */
a93913e1 538 case 0xba: asus_map_key_clear(KEY_PROG2); break;
1caccc25
CC
539
540 /* Fn+Space Power4Gear Hybrid */
a93913e1 541 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
1caccc25
CC
542
543 default:
0485b1ec
MH
544 /* ASUS lazily declares 256 usages, ignore the rest,
545 * as some make the keyboard appear as a pointer device. */
546 return -1;
1caccc25 547 }
af22a610
CC
548
549 /*
550 * Check and enable backlight only on devices with UsagePage ==
551 * 0xff31 to avoid initializing the keyboard firmware multiple
552 * times on devices with multiple HID descriptors but same
553 * PID/VID.
554 */
555 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
556 drvdata->enable_backlight = true;
557
1caccc25
CC
558 return 1;
559 }
560
5be91803
DD
561 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
562 set_bit(EV_REP, hi->input->evbit);
563 switch (usage->hid & HID_USAGE) {
564 case 0xff01: asus_map_key_clear(BTN_1); break;
565 case 0xff02: asus_map_key_clear(BTN_2); break;
566 case 0xff03: asus_map_key_clear(BTN_3); break;
567 case 0xff04: asus_map_key_clear(BTN_4); break;
568 case 0xff05: asus_map_key_clear(BTN_5); break;
569 case 0xff06: asus_map_key_clear(BTN_6); break;
570 case 0xff07: asus_map_key_clear(BTN_7); break;
571 case 0xff08: asus_map_key_clear(BTN_8); break;
572 case 0xff09: asus_map_key_clear(BTN_9); break;
573 case 0xff0a: asus_map_key_clear(BTN_A); break;
574 case 0xff0b: asus_map_key_clear(BTN_B); break;
575 case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
576 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
577 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
578 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
579 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
580 case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
581 default:
582 return 0;
583 }
584
585 return 1;
586 }
587
0485b1ec
MH
588 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
589 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
590 switch (usage->hid & HID_USAGE) {
591 case 0xe2: /* Mute */
592 case 0xe9: /* Volume up */
593 case 0xea: /* Volume down */
594 return 0;
595 default:
596 /* Ignore dummy Consumer usages which make the
597 * keyboard incorrectly appear as a pointer device.
598 */
599 return -1;
600 }
601 }
602
9ce12d8b
BM
603 return 0;
604}
605
606static int asus_start_multitouch(struct hid_device *hdev)
607{
608 int ret;
b9ec7009
CIK
609 static const unsigned char buf[] = {
610 FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
611 };
9ce12d8b
BM
612 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
613
614 if (!dmabuf) {
615 ret = -ENOMEM;
616 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
617 return ret;
618 }
619
620 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
621 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
622
623 kfree(dmabuf);
624
625 if (ret != sizeof(buf)) {
626 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
627 return ret;
628 }
629
630 return 0;
631}
632
633static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
634{
635 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
636
c81760b9 637 if (drvdata->tp)
9ce12d8b
BM
638 return asus_start_multitouch(hdev);
639
640 return 0;
641}
642
643static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
644{
645 int ret;
646 struct asus_drvdata *drvdata;
647
648 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
649 if (drvdata == NULL) {
650 hid_err(hdev, "Can't alloc Asus descriptor\n");
651 return -ENOMEM;
652 }
653
654 hid_set_drvdata(hdev, drvdata);
655
656 drvdata->quirks = id->driver_data;
657
c81760b9
HG
658 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
659 drvdata->tp = &asus_i2c_tp;
660
57573c54
HG
661 if (drvdata->quirks & QUIRK_T100_KEYBOARD) {
662 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
663
c81760b9
HG
664 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
665 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
762f948c 666 /*
dbd3ef28
HG
667 * The T100HA uses the same USB-ids as the T100TAF and
668 * the T200TA uses the same USB-ids as the T100TA, while
669 * both have different max x/y values as the T100TA[F].
762f948c
HG
670 */
671 if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
672 drvdata->tp = &asus_t100ha_tp;
dbd3ef28
HG
673 else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
674 drvdata->tp = &asus_t200ta_tp;
762f948c
HG
675 else
676 drvdata->tp = &asus_t100ta_tp;
c81760b9 677 }
57573c54
HG
678 }
679
73c75d39
HG
680 if (drvdata->quirks & QUIRK_T100CHI) {
681 /*
682 * All functionality is on a single HID interface and for
683 * userspace the touchpad must be a separate input_dev.
684 */
39335d1c 685 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
73c75d39
HG
686 drvdata->tp = &asus_t100chi_tp;
687 }
688
9ce12d8b
BM
689 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
690 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
691
692 ret = hid_parse(hdev);
693 if (ret) {
694 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
695 return ret;
696 }
697
698 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
699 if (ret) {
700 hid_err(hdev, "Asus hw start failed: %d\n", ret);
701 return ret;
702 }
703
704 if (!drvdata->input) {
705 hid_err(hdev, "Asus input not registered\n");
706 ret = -ENOMEM;
707 goto err_stop_hw;
708 }
709
c81760b9 710 if (drvdata->tp) {
c8b1b3dd
BM
711 drvdata->input->name = "Asus TouchPad";
712 } else {
713 drvdata->input->name = "Asus Keyboard";
714 }
9ce12d8b 715
c81760b9 716 if (drvdata->tp) {
9ce12d8b
BM
717 ret = asus_start_multitouch(hdev);
718 if (ret)
719 goto err_stop_hw;
720 }
721
722 return 0;
723err_stop_hw:
724 hid_hw_stop(hdev);
725 return ret;
726}
727
af22a610
CC
728static void asus_remove(struct hid_device *hdev)
729{
730 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
731
732 if (drvdata->kbd_backlight) {
733 drvdata->kbd_backlight->removed = true;
734 cancel_work_sync(&drvdata->kbd_backlight->work);
735 }
715e944f
CC
736
737 hid_hw_stop(hdev);
af22a610
CC
738}
739
832e1eee
MB
740static const __u8 asus_g752_fixed_rdesc[] = {
741 0x19, 0x00, /* Usage Minimum (0x00) */
742 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
743};
744
eeb01a57
YF
745static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
746 unsigned int *rsize)
747{
9ce12d8b
BM
748 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
749
750 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
751 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
b94f7d5d 752 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
eeb01a57
YF
753 rdesc[55] = 0xdd;
754 }
33edee4f 755 /* For the T100TA/T200TA keyboard dock */
76dd1fbe 756 if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
33edee4f
HG
757 (*rsize == 76 || *rsize == 101) &&
758 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
76dd1fbe
HG
759 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
760 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
761 }
5703e52c
HG
762 /* For the T100CHI keyboard dock */
763 if (drvdata->quirks & QUIRK_T100CHI &&
764 *rsize == 403 && rdesc[388] == 0x09 && rdesc[389] == 0x76) {
765 /*
766 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
767 * (FFh) and clear the flags in the Input() byte.
768 * Note the descriptor has a bogus 0 byte at the end so we
769 * only need 1 extra byte.
770 */
771 *rsize = 404;
772 rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
773 if (!rdesc)
774 return NULL;
775
776 hid_info(hdev, "Fixing up T100CHI keyb report descriptor\n");
777 memmove(rdesc + 392, rdesc + 390, 12);
778 rdesc[388] = 0x19;
779 rdesc[389] = 0x00;
780 rdesc[390] = 0x29;
781 rdesc[391] = 0xff;
782 rdesc[402] = 0x00;
783 }
832e1eee
MB
784 if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
785 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
786 /* report is missing usage mninum and maximum */
787 __u8 *new_rdesc;
788 size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
789
790 new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
791 if (new_rdesc == NULL)
792 return rdesc;
793
794 hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
795 /* copy the valid part */
796 memcpy(new_rdesc, rdesc, 61);
797 /* insert missing part */
798 memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
799 /* copy remaining data */
800 memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
801
802 *rsize = new_size;
803 rdesc = new_rdesc;
804 }
76dd1fbe 805
eeb01a57
YF
806 return rdesc;
807}
808
809static const struct hid_device_id asus_devices[] = {
9ce12d8b 810 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
a93913e1 811 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
9ce12d8b 812 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
c81760b9 813 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1caccc25 814 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
339ee3fc 815 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1caccc25 816 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
af22a610 817 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
832e1eee
MB
818 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
819 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
76dd1fbe 820 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
762f948c
HG
821 USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
822 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
823 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
824 USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
76dd1fbe 825 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
5be91803 826 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
38b2d78c 827 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
5be91803 828 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
5703e52c
HG
829 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
830 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
831
eeb01a57
YF
832 { }
833};
834MODULE_DEVICE_TABLE(hid, asus_devices);
835
836static struct hid_driver asus_driver = {
9ce12d8b
BM
837 .name = "asus",
838 .id_table = asus_devices,
839 .report_fixup = asus_report_fixup,
840 .probe = asus_probe,
af22a610 841 .remove = asus_remove,
9ce12d8b
BM
842 .input_mapping = asus_input_mapping,
843 .input_configured = asus_input_configured,
844#ifdef CONFIG_PM
845 .reset_resume = asus_reset_resume,
846#endif
e98e3809 847 .event = asus_event,
9ce12d8b 848 .raw_event = asus_raw_event
eeb01a57
YF
849};
850module_hid_driver(asus_driver);
851
852MODULE_LICENSE("GPL");