blob: b6d02ad980a3d9515a5cb1b4cf6c831d0de90141 [file] [log] [blame]
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +09001/*
Yusuke Fujimakib94f7d52016-04-03 23:15:16 +09002 * HID driver for Asus notebook built-in keyboard.
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +09003 * Fixes small logical maximum to match usage maximum.
4 *
Yusuke Fujimakib94f7d52016-04-03 23:15:16 +09005 * Currently supported devices are:
6 * EeeBook X205TA
7 * VivoBook E200HA
8 *
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +09009 * 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
Brendan McGrath9ce12d82016-11-29 18:59:25 +110014 *
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>
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +090020 */
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
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +090029#include <linux/hid.h>
30#include <linux/module.h>
Brendan McGrath9ce12d82016-11-29 18:59:25 +110031#include <linux/input/mt.h>
Hans de Goede57573c52017-05-25 16:49:21 +020032#include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +090033
34#include "hid-ids.h"
35
Brendan McGrath9ce12d82016-11-29 18:59:25 +110036MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
37MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
38MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
39MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
40MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
41
Hans de Goede57573c52017-05-25 16:49:21 +020042#define T100_TPAD_INTF 2
43
Brendan McGrath9ce12d82016-11-29 18:59:25 +110044#define FEATURE_REPORT_ID 0x0d
45#define INPUT_REPORT_ID 0x5d
Carlo Caioneaf22a612017-04-06 12:18:17 +020046#define FEATURE_KBD_REPORT_ID 0x5a
Carlo Caioneaf22a612017-04-06 12:18:17 +020047#define FEATURE_KBD_REPORT_SIZE 16
48
49#define SUPPORT_KBD_BACKLIGHT BIT(0)
Brendan McGrath9ce12d82016-11-29 18:59:25 +110050
Brendan McGrath9ce12d82016-11-29 18:59:25 +110051#define MAX_TOUCH_MAJOR 8
52#define MAX_PRESSURE 128
53
Brendan McGrath9ce12d82016-11-29 18:59:25 +110054#define BTN_LEFT_MASK 0x01
55#define CONTACT_TOOL_TYPE_MASK 0x80
56#define CONTACT_X_MSB_MASK 0xf0
57#define CONTACT_Y_MSB_MASK 0x0f
58#define CONTACT_TOUCH_MAJOR_MASK 0x07
59#define CONTACT_PRESSURE_MASK 0x7f
60
61#define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
62#define QUIRK_NO_INIT_REPORTS BIT(1)
63#define QUIRK_SKIP_INPUT_MAPPING BIT(2)
64#define QUIRK_IS_MULTITOUCH BIT(3)
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +010065#define QUIRK_NO_CONSUMER_USAGES BIT(4)
Carlo Caioneaf22a612017-04-06 12:18:17 +020066#define QUIRK_USE_KBD_BACKLIGHT BIT(5)
Hans de Goede76dd1fb2017-05-15 09:31:41 +020067#define QUIRK_T100_KEYBOARD BIT(6)
Brendan McGrath9ce12d82016-11-29 18:59:25 +110068
Matjaz Hegedica93913e2017-03-09 00:31:13 +010069#define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +010070 QUIRK_NO_INIT_REPORTS | \
71 QUIRK_NO_CONSUMER_USAGES)
Hans de Goedec81760b2017-07-02 16:34:12 +020072#define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
Brendan McGrath9ce12d82016-11-29 18:59:25 +110073 QUIRK_SKIP_INPUT_MAPPING | \
74 QUIRK_IS_MULTITOUCH)
75
76#define TRKID_SGN ((TRKID_MAX + 1) >> 1)
77
Carlo Caioneaf22a612017-04-06 12:18:17 +020078struct asus_kbd_leds {
79 struct led_classdev cdev;
80 struct hid_device *hdev;
81 struct work_struct work;
82 unsigned int brightness;
83 bool removed;
84};
85
Hans de Goedec81760b2017-07-02 16:34:12 +020086struct asus_touchpad_info {
87 int max_x;
88 int max_y;
89 int contact_size;
90 int max_contacts;
91};
92
Brendan McGrath9ce12d82016-11-29 18:59:25 +110093struct asus_drvdata {
94 unsigned long quirks;
95 struct input_dev *input;
Carlo Caioneaf22a612017-04-06 12:18:17 +020096 struct asus_kbd_leds *kbd_backlight;
Hans de Goedec81760b2017-07-02 16:34:12 +020097 const struct asus_touchpad_info *tp;
Carlo Caioneaf22a612017-04-06 12:18:17 +020098 bool enable_backlight;
Brendan McGrath9ce12d82016-11-29 18:59:25 +110099};
100
Hans de Goedec81760b2017-07-02 16:34:12 +0200101static const struct asus_touchpad_info asus_i2c_tp = {
102 .max_x = 2794,
103 .max_y = 1758,
104 .contact_size = 5,
105 .max_contacts = 5,
106};
107
108static const struct asus_touchpad_info asus_t100ta_tp = {
109 .max_x = 2240,
110 .max_y = 1758,
111 .contact_size = 5,
112 .max_contacts = 5,
113};
114
115static void asus_report_contact_down(struct asus_drvdata *drvdat,
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100116 int toolType, u8 *data)
117{
Hans de Goedec81760b2017-07-02 16:34:12 +0200118 struct input_dev *input = drvdat->input;
119 int touch_major, pressure, x, y;
120
121 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
122 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100123
124 if (toolType == MT_TOOL_PALM) {
125 touch_major = MAX_TOUCH_MAJOR;
126 pressure = MAX_PRESSURE;
127 } else {
128 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
129 pressure = data[4] & CONTACT_PRESSURE_MASK;
130 }
131
132 input_report_abs(input, ABS_MT_POSITION_X, x);
133 input_report_abs(input, ABS_MT_POSITION_Y, y);
134 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
135 input_report_abs(input, ABS_MT_PRESSURE, pressure);
136}
137
138/* Required for Synaptics Palm Detection */
Hans de Goedec81760b2017-07-02 16:34:12 +0200139static void asus_report_tool_width(struct asus_drvdata *drvdat)
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100140{
Hans de Goedec81760b2017-07-02 16:34:12 +0200141 struct input_mt *mt = drvdat->input->mt;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100142 struct input_mt_slot *oldest;
143 int oldid, count, i;
144
145 oldest = NULL;
146 oldid = mt->trkid;
147 count = 0;
148
149 for (i = 0; i < mt->num_slots; ++i) {
150 struct input_mt_slot *ps = &mt->slots[i];
151 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
152
153 if (id < 0)
154 continue;
155 if ((id - oldid) & TRKID_SGN) {
156 oldest = ps;
157 oldid = id;
158 }
159 count++;
160 }
161
162 if (oldest) {
Hans de Goedec81760b2017-07-02 16:34:12 +0200163 input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100164 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
165 }
166}
167
Hans de Goedec81760b2017-07-02 16:34:12 +0200168static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100169{
170 int i;
171 u8 *contactData = data + 2;
172
Hans de Goedec81760b2017-07-02 16:34:12 +0200173 if (size != 3 + drvdat->tp->contact_size * drvdat->tp->max_contacts)
174 return 0;
175
176 for (i = 0; i < drvdat->tp->max_contacts; i++) {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100177 bool down = !!(data[1] & BIT(i+3));
178 int toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
179 MT_TOOL_PALM : MT_TOOL_FINGER;
180
Hans de Goedec81760b2017-07-02 16:34:12 +0200181 input_mt_slot(drvdat->input, i);
182 input_mt_report_slot_state(drvdat->input, toolType, down);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100183
184 if (down) {
Hans de Goedec81760b2017-07-02 16:34:12 +0200185 asus_report_contact_down(drvdat, toolType, contactData);
186 contactData += drvdat->tp->contact_size;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100187 }
188 }
189
Hans de Goedec81760b2017-07-02 16:34:12 +0200190 input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
191 asus_report_tool_width(drvdat);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100192
Hans de Goedec81760b2017-07-02 16:34:12 +0200193 input_mt_sync_frame(drvdat->input);
194 input_sync(drvdat->input);
195
196 return 1;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100197}
198
199static int asus_raw_event(struct hid_device *hdev,
200 struct hid_report *report, u8 *data, int size)
201{
202 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
203
Hans de Goedec81760b2017-07-02 16:34:12 +0200204 if (drvdata->tp && data[0] == INPUT_REPORT_ID)
205 return asus_report_input(drvdata, data, size);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100206
207 return 0;
208}
209
Carlo Caioneaf22a612017-04-06 12:18:17 +0200210static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
211{
212 unsigned char *dmabuf;
213 int ret;
214
215 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
216 if (!dmabuf)
217 return -ENOMEM;
218
219 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
220 buf_size, HID_FEATURE_REPORT,
221 HID_REQ_SET_REPORT);
222 kfree(dmabuf);
223
224 return ret;
225}
226
227static int asus_kbd_init(struct hid_device *hdev)
228{
229 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
230 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
231 int ret;
232
233 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
234 if (ret < 0)
235 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
236
237 return ret;
238}
239
240static int asus_kbd_get_functions(struct hid_device *hdev,
241 unsigned char *kbd_func)
242{
243 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
244 u8 *readbuf;
245 int ret;
246
247 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
248 if (ret < 0) {
249 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
250 return ret;
251 }
252
253 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
254 if (!readbuf)
255 return -ENOMEM;
256
257 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
258 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
259 HID_REQ_GET_REPORT);
260 if (ret < 0) {
261 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
262 kfree(readbuf);
263 return ret;
264 }
265
266 *kbd_func = readbuf[6];
267
268 kfree(readbuf);
269 return ret;
270}
271
272static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
273 enum led_brightness brightness)
274{
275 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
276 cdev);
277 if (led->brightness == brightness)
278 return;
279
280 led->brightness = brightness;
281 schedule_work(&led->work);
282}
283
284static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
285{
286 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
287 cdev);
288
289 return led->brightness;
290}
291
292static void asus_kbd_backlight_work(struct work_struct *work)
293{
294 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
295 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
296 int ret;
297
298 if (led->removed)
299 return;
300
301 buf[4] = led->brightness;
302
303 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
304 if (ret < 0)
305 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
306}
307
308static int asus_kbd_register_leds(struct hid_device *hdev)
309{
310 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
311 unsigned char kbd_func;
312 int ret;
313
314 /* Initialize keyboard */
315 ret = asus_kbd_init(hdev);
316 if (ret < 0)
317 return ret;
318
319 /* Get keyboard functions */
320 ret = asus_kbd_get_functions(hdev, &kbd_func);
321 if (ret < 0)
322 return ret;
323
324 /* Check for backlight support */
325 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
326 return -ENODEV;
327
328 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
329 sizeof(struct asus_kbd_leds),
330 GFP_KERNEL);
331 if (!drvdata->kbd_backlight)
332 return -ENOMEM;
333
334 drvdata->kbd_backlight->removed = false;
335 drvdata->kbd_backlight->brightness = 0;
336 drvdata->kbd_backlight->hdev = hdev;
337 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
338 drvdata->kbd_backlight->cdev.max_brightness = 3;
339 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
340 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
341 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
342
343 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
344 if (ret < 0) {
345 /* No need to have this still around */
346 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
347 }
348
349 return ret;
350}
351
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100352static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
353{
Brendan McGrathc8b1b3d2016-12-10 21:20:42 +1100354 struct input_dev *input = hi->input;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100355 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
356
Hans de Goedec81760b2017-07-02 16:34:12 +0200357 if (drvdata->tp) {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100358 int ret;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100359
Hans de Goedec81760b2017-07-02 16:34:12 +0200360 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
361 drvdata->tp->max_x, 0, 0);
362 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
363 drvdata->tp->max_y, 0, 0);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100364 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, MAX_TOUCH_MAJOR, 0, 0);
365 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, MAX_TOUCH_MAJOR, 0, 0);
366 input_set_abs_params(input, ABS_MT_PRESSURE, 0, MAX_PRESSURE, 0, 0);
367
368 __set_bit(BTN_LEFT, input->keybit);
369 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
370
Hans de Goedec81760b2017-07-02 16:34:12 +0200371 ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
372 INPUT_MT_POINTER);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100373
374 if (ret) {
375 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
376 return ret;
377 }
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100378 }
379
Brendan McGrathc8b1b3d2016-12-10 21:20:42 +1100380 drvdata->input = input;
381
Carlo Caioneaf22a612017-04-06 12:18:17 +0200382 if (drvdata->enable_backlight && asus_kbd_register_leds(hdev))
383 hid_warn(hdev, "Failed to initialize backlight.\n");
384
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100385 return 0;
386}
387
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100388#define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
Chris Chiu1caccc22017-03-01 15:48:51 -0600389 max, EV_KEY, (c))
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100390static int asus_input_mapping(struct hid_device *hdev,
391 struct hid_input *hi, struct hid_field *field,
392 struct hid_usage *usage, unsigned long **bit,
393 int *max)
394{
395 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
396
397 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
398 /* Don't map anything from the HID report.
399 * We do it all manually in asus_input_configured
400 */
401 return -1;
402 }
403
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100404 /* ASUS-specific keyboard hotkeys */
Chris Chiu1caccc22017-03-01 15:48:51 -0600405 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
406 set_bit(EV_REP, hi->input->evbit);
407 switch (usage->hid & HID_USAGE) {
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100408 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
409 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
410 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
411 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
412 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
Matjaz Hegedic802b24b2017-03-09 00:31:15 +0100413 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100414 case 0xb5: asus_map_key_clear(KEY_CALC); break;
415 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
416 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600417
418 /* ASUS touchpad toggle */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100419 case 0x6b: asus_map_key_clear(KEY_F21); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600420
421 /* ROG key */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100422 case 0x38: asus_map_key_clear(KEY_PROG1); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600423
424 /* Fn+C ASUS Splendid */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100425 case 0xba: asus_map_key_clear(KEY_PROG2); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600426
427 /* Fn+Space Power4Gear Hybrid */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100428 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600429
430 default:
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +0100431 /* ASUS lazily declares 256 usages, ignore the rest,
432 * as some make the keyboard appear as a pointer device. */
433 return -1;
Chris Chiu1caccc22017-03-01 15:48:51 -0600434 }
Carlo Caioneaf22a612017-04-06 12:18:17 +0200435
436 /*
437 * Check and enable backlight only on devices with UsagePage ==
438 * 0xff31 to avoid initializing the keyboard firmware multiple
439 * times on devices with multiple HID descriptors but same
440 * PID/VID.
441 */
442 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
443 drvdata->enable_backlight = true;
444
Chris Chiu1caccc22017-03-01 15:48:51 -0600445 return 1;
446 }
447
Daniel Drake5be91802017-06-05 14:58:56 -0600448 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
449 set_bit(EV_REP, hi->input->evbit);
450 switch (usage->hid & HID_USAGE) {
451 case 0xff01: asus_map_key_clear(BTN_1); break;
452 case 0xff02: asus_map_key_clear(BTN_2); break;
453 case 0xff03: asus_map_key_clear(BTN_3); break;
454 case 0xff04: asus_map_key_clear(BTN_4); break;
455 case 0xff05: asus_map_key_clear(BTN_5); break;
456 case 0xff06: asus_map_key_clear(BTN_6); break;
457 case 0xff07: asus_map_key_clear(BTN_7); break;
458 case 0xff08: asus_map_key_clear(BTN_8); break;
459 case 0xff09: asus_map_key_clear(BTN_9); break;
460 case 0xff0a: asus_map_key_clear(BTN_A); break;
461 case 0xff0b: asus_map_key_clear(BTN_B); break;
462 case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
463 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
464 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
465 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
466 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
467 case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
468 default:
469 return 0;
470 }
471
472 return 1;
473 }
474
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +0100475 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
476 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
477 switch (usage->hid & HID_USAGE) {
478 case 0xe2: /* Mute */
479 case 0xe9: /* Volume up */
480 case 0xea: /* Volume down */
481 return 0;
482 default:
483 /* Ignore dummy Consumer usages which make the
484 * keyboard incorrectly appear as a pointer device.
485 */
486 return -1;
487 }
488 }
489
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100490 return 0;
491}
492
493static int asus_start_multitouch(struct hid_device *hdev)
494{
495 int ret;
496 const unsigned char buf[] = { FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00 };
497 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
498
499 if (!dmabuf) {
500 ret = -ENOMEM;
501 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
502 return ret;
503 }
504
505 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
506 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
507
508 kfree(dmabuf);
509
510 if (ret != sizeof(buf)) {
511 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
512 return ret;
513 }
514
515 return 0;
516}
517
518static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
519{
520 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
521
Hans de Goedec81760b2017-07-02 16:34:12 +0200522 if (drvdata->tp)
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100523 return asus_start_multitouch(hdev);
524
525 return 0;
526}
527
528static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
529{
530 int ret;
531 struct asus_drvdata *drvdata;
532
533 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
534 if (drvdata == NULL) {
535 hid_err(hdev, "Can't alloc Asus descriptor\n");
536 return -ENOMEM;
537 }
538
539 hid_set_drvdata(hdev, drvdata);
540
541 drvdata->quirks = id->driver_data;
542
Hans de Goedec81760b2017-07-02 16:34:12 +0200543 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
544 drvdata->tp = &asus_i2c_tp;
545
Hans de Goede57573c52017-05-25 16:49:21 +0200546 if (drvdata->quirks & QUIRK_T100_KEYBOARD) {
547 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
548
Hans de Goedec81760b2017-07-02 16:34:12 +0200549 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
550 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
551 drvdata->tp = &asus_t100ta_tp;
552 }
Hans de Goede57573c52017-05-25 16:49:21 +0200553 }
554
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100555 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
556 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
557
558 ret = hid_parse(hdev);
559 if (ret) {
560 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
561 return ret;
562 }
563
564 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
565 if (ret) {
566 hid_err(hdev, "Asus hw start failed: %d\n", ret);
567 return ret;
568 }
569
570 if (!drvdata->input) {
571 hid_err(hdev, "Asus input not registered\n");
572 ret = -ENOMEM;
573 goto err_stop_hw;
574 }
575
Hans de Goedec81760b2017-07-02 16:34:12 +0200576 if (drvdata->tp) {
Brendan McGrathc8b1b3d2016-12-10 21:20:42 +1100577 drvdata->input->name = "Asus TouchPad";
578 } else {
579 drvdata->input->name = "Asus Keyboard";
580 }
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100581
Hans de Goedec81760b2017-07-02 16:34:12 +0200582 if (drvdata->tp) {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100583 ret = asus_start_multitouch(hdev);
584 if (ret)
585 goto err_stop_hw;
586 }
587
588 return 0;
589err_stop_hw:
590 hid_hw_stop(hdev);
591 return ret;
592}
593
Carlo Caioneaf22a612017-04-06 12:18:17 +0200594static void asus_remove(struct hid_device *hdev)
595{
596 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
597
598 if (drvdata->kbd_backlight) {
599 drvdata->kbd_backlight->removed = true;
600 cancel_work_sync(&drvdata->kbd_backlight->work);
601 }
Carlo Caione715e9442017-05-30 22:39:46 +0200602
603 hid_hw_stop(hdev);
Carlo Caioneaf22a612017-04-06 12:18:17 +0200604}
605
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +0900606static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
607 unsigned int *rsize)
608{
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100609 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
610
611 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
612 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
Yusuke Fujimakib94f7d52016-04-03 23:15:16 +0900613 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +0900614 rdesc[55] = 0xdd;
615 }
Hans de Goede76dd1fb2017-05-15 09:31:41 +0200616 if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
617 *rsize == 76 && rdesc[73] == 0x81 && rdesc[74] == 0x01) {
618 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
619 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
620 }
621
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +0900622 return rdesc;
623}
624
625static const struct hid_device_id asus_devices[] = {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100626 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100627 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100628 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
Hans de Goedec81760b2017-07-02 16:34:12 +0200629 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
Chris Chiu1caccc22017-03-01 15:48:51 -0600630 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
631 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1) },
632 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
Carlo Caioneaf22a612017-04-06 12:18:17 +0200633 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
Hans de Goede76dd1fb2017-05-15 09:31:41 +0200634 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
635 USB_DEVICE_ID_ASUSTEK_T100_KEYBOARD),
636 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
Daniel Drake5be91802017-06-05 14:58:56 -0600637 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
Daniel Drake38b2d782017-06-05 14:58:57 -0600638 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
Daniel Drake5be91802017-06-05 14:58:56 -0600639 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +0900640 { }
641};
642MODULE_DEVICE_TABLE(hid, asus_devices);
643
644static struct hid_driver asus_driver = {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100645 .name = "asus",
646 .id_table = asus_devices,
647 .report_fixup = asus_report_fixup,
648 .probe = asus_probe,
Carlo Caioneaf22a612017-04-06 12:18:17 +0200649 .remove = asus_remove,
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100650 .input_mapping = asus_input_mapping,
651 .input_configured = asus_input_configured,
652#ifdef CONFIG_PM
653 .reset_resume = asus_reset_resume,
654#endif
655 .raw_event = asus_raw_event
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +0900656};
657module_hid_driver(asus_driver);
658
659MODULE_LICENSE("GPL");