blob: 336aeaed1159adfde7abf3dfbb78ff0335f56546 [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
Hans de Goede762f9482017-11-26 16:40:10 +010029#include <linux/dmi.h>
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +090030#include <linux/hid.h>
31#include <linux/module.h>
Daniel Drake3b692c52018-10-09 14:40:56 +080032#include <linux/platform_data/x86/asus-wmi.h>
Brendan McGrath9ce12d82016-11-29 18:59:25 +110033#include <linux/input/mt.h>
Hans de Goede57573c52017-05-25 16:49:21 +020034#include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +090035#include <linux/power_supply.h>
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +090036
37#include "hid-ids.h"
38
Brendan McGrath9ce12d82016-11-29 18:59:25 +110039MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
40MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
41MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
42MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
43MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
44
Hans de Goede57573c52017-05-25 16:49:21 +020045#define T100_TPAD_INTF 2
46
Hans de Goede73c75d32017-08-04 15:31:04 +020047#define T100CHI_MOUSE_REPORT_ID 0x06
Brendan McGrath9ce12d82016-11-29 18:59:25 +110048#define FEATURE_REPORT_ID 0x0d
49#define INPUT_REPORT_ID 0x5d
Carlo Caioneaf22a612017-04-06 12:18:17 +020050#define FEATURE_KBD_REPORT_ID 0x5a
Carlo Caioneaf22a612017-04-06 12:18:17 +020051#define FEATURE_KBD_REPORT_SIZE 16
52
53#define SUPPORT_KBD_BACKLIGHT BIT(0)
Brendan McGrath9ce12d82016-11-29 18:59:25 +110054
Brendan McGrath9ce12d82016-11-29 18:59:25 +110055#define MAX_TOUCH_MAJOR 8
56#define MAX_PRESSURE 128
57
Brendan McGrath9ce12d82016-11-29 18:59:25 +110058#define BTN_LEFT_MASK 0x01
59#define CONTACT_TOOL_TYPE_MASK 0x80
60#define CONTACT_X_MSB_MASK 0xf0
61#define CONTACT_Y_MSB_MASK 0x0f
62#define CONTACT_TOUCH_MAJOR_MASK 0x07
63#define CONTACT_PRESSURE_MASK 0x7f
64
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +090065#define BATTERY_REPORT_ID (0x03)
66#define BATTERY_REPORT_SIZE (1 + 8)
67#define BATTERY_LEVEL_MAX ((u8)255)
68#define BATTERY_STAT_DISCONNECT (0)
69#define BATTERY_STAT_CHARGING (1)
70#define BATTERY_STAT_FULL (2)
71
Brendan McGrath9ce12d82016-11-29 18:59:25 +110072#define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
73#define QUIRK_NO_INIT_REPORTS BIT(1)
74#define QUIRK_SKIP_INPUT_MAPPING BIT(2)
75#define QUIRK_IS_MULTITOUCH BIT(3)
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +010076#define QUIRK_NO_CONSUMER_USAGES BIT(4)
Carlo Caioneaf22a612017-04-06 12:18:17 +020077#define QUIRK_USE_KBD_BACKLIGHT BIT(5)
Hans de Goede76dd1fb2017-05-15 09:31:41 +020078#define QUIRK_T100_KEYBOARD BIT(6)
Hans de Goede5703e522017-07-02 16:34:15 +020079#define QUIRK_T100CHI BIT(7)
Maxime Bellengé832e1ee2017-09-07 21:17:28 +020080#define QUIRK_G752_KEYBOARD BIT(8)
Aleix Roca Nonell0e1f37d2018-12-16 16:53:19 +010081#define QUIRK_T101HA_DOCK BIT(9)
NOGUCHI Hiroshi300c64d2019-01-29 13:23:20 +090082#define QUIRK_T90CHI BIT(10)
Brendan McGrath9ce12d82016-11-29 18:59:25 +110083
Matjaz Hegedica93913e2017-03-09 00:31:13 +010084#define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +010085 QUIRK_NO_INIT_REPORTS | \
86 QUIRK_NO_CONSUMER_USAGES)
Hans de Goedec81760b2017-07-02 16:34:12 +020087#define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
Brendan McGrath9ce12d82016-11-29 18:59:25 +110088 QUIRK_SKIP_INPUT_MAPPING | \
89 QUIRK_IS_MULTITOUCH)
90
91#define TRKID_SGN ((TRKID_MAX + 1) >> 1)
92
Carlo Caioneaf22a612017-04-06 12:18:17 +020093struct asus_kbd_leds {
94 struct led_classdev cdev;
95 struct hid_device *hdev;
96 struct work_struct work;
97 unsigned int brightness;
98 bool removed;
99};
100
Hans de Goedec81760b2017-07-02 16:34:12 +0200101struct asus_touchpad_info {
102 int max_x;
103 int max_y;
Hans de Goedeb61d43e62017-07-02 16:34:14 +0200104 int res_x;
105 int res_y;
Hans de Goedec81760b2017-07-02 16:34:12 +0200106 int contact_size;
107 int max_contacts;
108};
109
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100110struct asus_drvdata {
111 unsigned long quirks;
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +0900112 struct hid_device *hdev;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100113 struct input_dev *input;
Carlo Caioneaf22a612017-04-06 12:18:17 +0200114 struct asus_kbd_leds *kbd_backlight;
Hans de Goedec81760b2017-07-02 16:34:12 +0200115 const struct asus_touchpad_info *tp;
Carlo Caioneaf22a612017-04-06 12:18:17 +0200116 bool enable_backlight;
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +0900117 struct power_supply *battery;
118 struct power_supply_desc battery_desc;
119 int battery_capacity;
120 int battery_stat;
121 bool battery_in_query;
122 unsigned long battery_next_query;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100123};
124
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +0900125static int asus_report_battery(struct asus_drvdata *, u8 *, int);
126
Hans de Goedec81760b2017-07-02 16:34:12 +0200127static const struct asus_touchpad_info asus_i2c_tp = {
128 .max_x = 2794,
129 .max_y = 1758,
130 .contact_size = 5,
131 .max_contacts = 5,
132};
133
134static const struct asus_touchpad_info asus_t100ta_tp = {
135 .max_x = 2240,
Hans de Goede25cc2612017-07-02 16:34:13 +0200136 .max_y = 1120,
Hans de Goedeb61d43e62017-07-02 16:34:14 +0200137 .res_x = 30, /* units/mm */
138 .res_y = 27, /* units/mm */
Hans de Goedec81760b2017-07-02 16:34:12 +0200139 .contact_size = 5,
140 .max_contacts = 5,
141};
142
Hans de Goede762f9482017-11-26 16:40:10 +0100143static const struct asus_touchpad_info asus_t100ha_tp = {
144 .max_x = 2640,
145 .max_y = 1320,
146 .res_x = 30, /* units/mm */
147 .res_y = 29, /* units/mm */
148 .contact_size = 5,
149 .max_contacts = 5,
150};
151
Hans de Goededbd3ef22018-01-05 12:09:18 +0100152static const struct asus_touchpad_info asus_t200ta_tp = {
153 .max_x = 3120,
154 .max_y = 1716,
155 .res_x = 30, /* units/mm */
156 .res_y = 28, /* units/mm */
157 .contact_size = 5,
158 .max_contacts = 5,
159};
160
Hans de Goede73c75d32017-08-04 15:31:04 +0200161static const struct asus_touchpad_info asus_t100chi_tp = {
162 .max_x = 2640,
163 .max_y = 1320,
164 .res_x = 31, /* units/mm */
165 .res_y = 29, /* units/mm */
166 .contact_size = 3,
167 .max_contacts = 4,
168};
169
Hans de Goedec81760b2017-07-02 16:34:12 +0200170static void asus_report_contact_down(struct asus_drvdata *drvdat,
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100171 int toolType, u8 *data)
172{
Hans de Goedec81760b2017-07-02 16:34:12 +0200173 struct input_dev *input = drvdat->input;
174 int touch_major, pressure, x, y;
175
176 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
177 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100178
Hans de Goede73c75d32017-08-04 15:31:04 +0200179 input_report_abs(input, ABS_MT_POSITION_X, x);
180 input_report_abs(input, ABS_MT_POSITION_Y, y);
181
182 if (drvdat->tp->contact_size < 5)
183 return;
184
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100185 if (toolType == MT_TOOL_PALM) {
186 touch_major = MAX_TOUCH_MAJOR;
187 pressure = MAX_PRESSURE;
188 } else {
189 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
190 pressure = data[4] & CONTACT_PRESSURE_MASK;
191 }
192
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100193 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
194 input_report_abs(input, ABS_MT_PRESSURE, pressure);
195}
196
197/* Required for Synaptics Palm Detection */
Hans de Goedec81760b2017-07-02 16:34:12 +0200198static void asus_report_tool_width(struct asus_drvdata *drvdat)
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100199{
Hans de Goedec81760b2017-07-02 16:34:12 +0200200 struct input_mt *mt = drvdat->input->mt;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100201 struct input_mt_slot *oldest;
202 int oldid, count, i;
203
Hans de Goede73c75d32017-08-04 15:31:04 +0200204 if (drvdat->tp->contact_size < 5)
205 return;
206
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100207 oldest = NULL;
208 oldid = mt->trkid;
209 count = 0;
210
211 for (i = 0; i < mt->num_slots; ++i) {
212 struct input_mt_slot *ps = &mt->slots[i];
213 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
214
215 if (id < 0)
216 continue;
217 if ((id - oldid) & TRKID_SGN) {
218 oldest = ps;
219 oldid = id;
220 }
221 count++;
222 }
223
224 if (oldest) {
Hans de Goedec81760b2017-07-02 16:34:12 +0200225 input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100226 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
227 }
228}
229
Hans de Goedec81760b2017-07-02 16:34:12 +0200230static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100231{
Hans de Goede73c75d32017-08-04 15:31:04 +0200232 int i, toolType = MT_TOOL_FINGER;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100233 u8 *contactData = data + 2;
234
Hans de Goedec81760b2017-07-02 16:34:12 +0200235 if (size != 3 + drvdat->tp->contact_size * drvdat->tp->max_contacts)
236 return 0;
237
238 for (i = 0; i < drvdat->tp->max_contacts; i++) {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100239 bool down = !!(data[1] & BIT(i+3));
Hans de Goede73c75d32017-08-04 15:31:04 +0200240
241 if (drvdat->tp->contact_size >= 5)
242 toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100243 MT_TOOL_PALM : MT_TOOL_FINGER;
244
Hans de Goedec81760b2017-07-02 16:34:12 +0200245 input_mt_slot(drvdat->input, i);
246 input_mt_report_slot_state(drvdat->input, toolType, down);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100247
248 if (down) {
Hans de Goedec81760b2017-07-02 16:34:12 +0200249 asus_report_contact_down(drvdat, toolType, contactData);
250 contactData += drvdat->tp->contact_size;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100251 }
252 }
253
Hans de Goedec81760b2017-07-02 16:34:12 +0200254 input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
255 asus_report_tool_width(drvdat);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100256
Hans de Goedec81760b2017-07-02 16:34:12 +0200257 input_mt_sync_frame(drvdat->input);
258 input_sync(drvdat->input);
259
260 return 1;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100261}
262
Hans de Goedee98e3802018-11-26 11:52:16 +0100263static int asus_event(struct hid_device *hdev, struct hid_field *field,
264 struct hid_usage *usage, __s32 value)
265{
266 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
267 (usage->hid & HID_USAGE) != 0x00 && !usage->type) {
268 hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
269 usage->hid & HID_USAGE);
270 }
271
272 return 0;
273}
274
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100275static int asus_raw_event(struct hid_device *hdev,
276 struct hid_report *report, u8 *data, int size)
277{
278 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
279
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +0900280 if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
281 return asus_report_battery(drvdata, data, size);
282
Hans de Goedec81760b2017-07-02 16:34:12 +0200283 if (drvdata->tp && data[0] == INPUT_REPORT_ID)
284 return asus_report_input(drvdata, data, size);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100285
286 return 0;
287}
288
Carlo Caioneaf22a612017-04-06 12:18:17 +0200289static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
290{
291 unsigned char *dmabuf;
292 int ret;
293
294 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
295 if (!dmabuf)
296 return -ENOMEM;
297
298 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf,
299 buf_size, HID_FEATURE_REPORT,
300 HID_REQ_SET_REPORT);
301 kfree(dmabuf);
302
303 return ret;
304}
305
306static int asus_kbd_init(struct hid_device *hdev)
307{
308 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
309 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
310 int ret;
311
312 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
313 if (ret < 0)
314 hid_err(hdev, "Asus failed to send init command: %d\n", ret);
315
316 return ret;
317}
318
319static int asus_kbd_get_functions(struct hid_device *hdev,
320 unsigned char *kbd_func)
321{
322 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
323 u8 *readbuf;
324 int ret;
325
326 ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
327 if (ret < 0) {
328 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
329 return ret;
330 }
331
332 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
333 if (!readbuf)
334 return -ENOMEM;
335
336 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
337 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
338 HID_REQ_GET_REPORT);
339 if (ret < 0) {
340 hid_err(hdev, "Asus failed to request functions: %d\n", ret);
341 kfree(readbuf);
342 return ret;
343 }
344
345 *kbd_func = readbuf[6];
346
347 kfree(readbuf);
348 return ret;
349}
350
351static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
352 enum led_brightness brightness)
353{
354 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
355 cdev);
356 if (led->brightness == brightness)
357 return;
358
359 led->brightness = brightness;
360 schedule_work(&led->work);
361}
362
363static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
364{
365 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
366 cdev);
367
368 return led->brightness;
369}
370
371static void asus_kbd_backlight_work(struct work_struct *work)
372{
373 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
374 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
375 int ret;
376
377 if (led->removed)
378 return;
379
380 buf[4] = led->brightness;
381
382 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
383 if (ret < 0)
384 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
385}
386
Daniel Drake3b692c52018-10-09 14:40:56 +0800387/* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
388 * precedence. We only activate HID-based backlight control when the
389 * WMI control is not available.
390 */
391static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
392{
393 u32 value;
394 int ret;
395
Arnd Bergmann3fc202e2018-11-02 16:14:32 +0100396 if (!IS_ENABLED(CONFIG_ASUS_WMI))
397 return false;
398
Daniel Drake3b692c52018-10-09 14:40:56 +0800399 ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS2,
400 ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
401 hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
402 if (ret)
403 return false;
404
405 return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
406}
407
Carlo Caioneaf22a612017-04-06 12:18:17 +0200408static int asus_kbd_register_leds(struct hid_device *hdev)
409{
410 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
411 unsigned char kbd_func;
412 int ret;
413
414 /* Initialize keyboard */
415 ret = asus_kbd_init(hdev);
416 if (ret < 0)
417 return ret;
418
419 /* Get keyboard functions */
420 ret = asus_kbd_get_functions(hdev, &kbd_func);
421 if (ret < 0)
422 return ret;
423
424 /* Check for backlight support */
425 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
426 return -ENODEV;
427
428 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
429 sizeof(struct asus_kbd_leds),
430 GFP_KERNEL);
431 if (!drvdata->kbd_backlight)
432 return -ENOMEM;
433
434 drvdata->kbd_backlight->removed = false;
435 drvdata->kbd_backlight->brightness = 0;
436 drvdata->kbd_backlight->hdev = hdev;
437 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
438 drvdata->kbd_backlight->cdev.max_brightness = 3;
439 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
440 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
441 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
442
443 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
444 if (ret < 0) {
445 /* No need to have this still around */
446 devm_kfree(&hdev->dev, drvdata->kbd_backlight);
447 }
448
449 return ret;
450}
451
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +0900452/*
453 * [0] REPORT_ID (same value defined in report descriptor)
454 * [1] rest battery level. range [0..255]
455 * [2]..[7] Bluetooth hardware address (MAC address)
456 * [8] charging status
457 * = 0 : AC offline / discharging
458 * = 1 : AC online / charging
459 * = 2 : AC online / fully charged
460 */
461static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
462{
463 u8 sts;
464 u8 lvl;
465 int val;
466
467 lvl = data[1];
468 sts = data[8];
469
470 drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
471
472 switch (sts) {
473 case BATTERY_STAT_CHARGING:
474 val = POWER_SUPPLY_STATUS_CHARGING;
475 break;
476 case BATTERY_STAT_FULL:
477 val = POWER_SUPPLY_STATUS_FULL;
478 break;
479 case BATTERY_STAT_DISCONNECT:
480 default:
481 val = POWER_SUPPLY_STATUS_DISCHARGING;
482 break;
483 }
484 drvdata->battery_stat = val;
485
486 return 0;
487}
488
489static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
490{
491 /* notify only the autonomous event by device */
492 if ((drvdata->battery_in_query == false) &&
493 (size == BATTERY_REPORT_SIZE))
494 power_supply_changed(drvdata->battery);
495
496 return 0;
497}
498
499static int asus_battery_query(struct asus_drvdata *drvdata)
500{
501 u8 *buf;
502 int ret = 0;
503
504 buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
505 if (!buf)
506 return -ENOMEM;
507
508 drvdata->battery_in_query = true;
509 ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
510 buf, BATTERY_REPORT_SIZE,
511 HID_INPUT_REPORT, HID_REQ_GET_REPORT);
512 drvdata->battery_in_query = false;
513 if (ret == BATTERY_REPORT_SIZE)
514 ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
515 else
516 ret = -ENODATA;
517
518 kfree(buf);
519
520 return ret;
521}
522
523static enum power_supply_property asus_battery_props[] = {
524 POWER_SUPPLY_PROP_STATUS,
525 POWER_SUPPLY_PROP_PRESENT,
526 POWER_SUPPLY_PROP_CAPACITY,
527 POWER_SUPPLY_PROP_SCOPE,
528 POWER_SUPPLY_PROP_MODEL_NAME,
529};
530
531#define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */
532
533static int asus_battery_get_property(struct power_supply *psy,
534 enum power_supply_property psp,
535 union power_supply_propval *val)
536{
537 struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
538 int ret = 0;
539
540 switch (psp) {
541 case POWER_SUPPLY_PROP_STATUS:
542 case POWER_SUPPLY_PROP_CAPACITY:
543 if (time_before(drvdata->battery_next_query, jiffies)) {
544 drvdata->battery_next_query =
545 jiffies + QUERY_MIN_INTERVAL;
546 ret = asus_battery_query(drvdata);
547 if (ret)
548 return ret;
549 }
550 if (psp == POWER_SUPPLY_PROP_STATUS)
551 val->intval = drvdata->battery_stat;
552 else
553 val->intval = drvdata->battery_capacity;
554 break;
555 case POWER_SUPPLY_PROP_PRESENT:
556 val->intval = 1;
557 break;
558 case POWER_SUPPLY_PROP_SCOPE:
559 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
560 break;
561 case POWER_SUPPLY_PROP_MODEL_NAME:
562 val->strval = drvdata->hdev->name;
563 break;
564 default:
565 ret = -EINVAL;
566 break;
567 }
568
569 return ret;
570}
571
572static int asus_battery_probe(struct hid_device *hdev)
573{
574 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
575 struct power_supply_config pscfg = { .drv_data = drvdata };
576 int ret = 0;
577
578 drvdata->battery_capacity = 0;
579 drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
580 drvdata->battery_in_query = false;
581
582 drvdata->battery_desc.properties = asus_battery_props;
583 drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
584 drvdata->battery_desc.get_property = asus_battery_get_property;
585 drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
586 drvdata->battery_desc.use_for_apm = 0;
587 drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
588 "asus-keyboard-%s-battery",
589 strlen(hdev->uniq) ?
590 hdev->uniq : dev_name(&hdev->dev));
591 if (!drvdata->battery_desc.name)
592 return -ENOMEM;
593
594 drvdata->battery_next_query = jiffies;
595
596 drvdata->battery = devm_power_supply_register(&hdev->dev,
597 &(drvdata->battery_desc), &pscfg);
598 if (IS_ERR(drvdata->battery)) {
599 ret = PTR_ERR(drvdata->battery);
600 drvdata->battery = NULL;
601 hid_err(hdev, "Unable to register battery device\n");
602 return ret;
603 }
604
605 power_supply_powers(drvdata->battery, &hdev->dev);
606
607 return ret;
608}
609
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100610static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
611{
Brendan McGrathc8b1b3d2016-12-10 21:20:42 +1100612 struct input_dev *input = hi->input;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100613 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
614
Hans de Goede73c75d32017-08-04 15:31:04 +0200615 /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
616 if (drvdata->quirks & QUIRK_T100CHI &&
617 hi->report->id != T100CHI_MOUSE_REPORT_ID)
618 return 0;
619
Hans de Goedec81760b2017-07-02 16:34:12 +0200620 if (drvdata->tp) {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100621 int ret;
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100622
Hans de Goedec81760b2017-07-02 16:34:12 +0200623 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
624 drvdata->tp->max_x, 0, 0);
625 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
626 drvdata->tp->max_y, 0, 0);
Hans de Goedeb61d43e62017-07-02 16:34:14 +0200627 input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
628 input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
Hans de Goede73c75d32017-08-04 15:31:04 +0200629
630 if (drvdata->tp->contact_size >= 5) {
631 input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
632 MAX_TOUCH_MAJOR, 0, 0);
633 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
634 MAX_TOUCH_MAJOR, 0, 0);
635 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
636 MAX_PRESSURE, 0, 0);
637 }
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100638
639 __set_bit(BTN_LEFT, input->keybit);
640 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
641
Hans de Goedec81760b2017-07-02 16:34:12 +0200642 ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
643 INPUT_MT_POINTER);
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100644
645 if (ret) {
646 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
647 return ret;
648 }
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100649 }
650
Brendan McGrathc8b1b3d2016-12-10 21:20:42 +1100651 drvdata->input = input;
652
Daniel Drake3b692c52018-10-09 14:40:56 +0800653 if (drvdata->enable_backlight &&
654 !asus_kbd_wmi_led_control_present(hdev) &&
655 asus_kbd_register_leds(hdev))
Carlo Caioneaf22a612017-04-06 12:18:17 +0200656 hid_warn(hdev, "Failed to initialize backlight.\n");
657
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100658 return 0;
659}
660
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100661#define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
Chris Chiu1caccc22017-03-01 15:48:51 -0600662 max, EV_KEY, (c))
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100663static int asus_input_mapping(struct hid_device *hdev,
664 struct hid_input *hi, struct hid_field *field,
665 struct hid_usage *usage, unsigned long **bit,
666 int *max)
667{
668 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
669
670 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
671 /* Don't map anything from the HID report.
672 * We do it all manually in asus_input_configured
673 */
674 return -1;
675 }
676
Hans de Goede73c75d32017-08-04 15:31:04 +0200677 /*
678 * Ignore a bunch of bogus collections in the T100CHI descriptor.
679 * This avoids a bunch of non-functional hid_input devices getting
680 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
681 */
NOGUCHI Hiroshi300c64d2019-01-29 13:23:20 +0900682 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
Hans de Goede73c75d32017-08-04 15:31:04 +0200683 if (field->application == (HID_UP_GENDESK | 0x0080) ||
684 usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
685 usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
686 usage->hid == (HID_UP_GENDEVCTRLS | 0x0026))
687 return -1;
688 /*
689 * We use the hid_input for the mouse report for the touchpad,
690 * keep the left button, to avoid the core removing it.
691 */
692 if (field->application == HID_GD_MOUSE &&
693 usage->hid != (HID_UP_BUTTON | 1))
694 return -1;
695 }
696
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100697 /* ASUS-specific keyboard hotkeys */
Chris Chiu1caccc22017-03-01 15:48:51 -0600698 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
699 set_bit(EV_REP, hi->input->evbit);
700 switch (usage->hid & HID_USAGE) {
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100701 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
702 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
703 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
704 case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
Hans de Goede2340bad2018-11-26 11:52:17 +0100705 case 0x7c: asus_map_key_clear(KEY_MICMUTE); break;
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100706 case 0x82: asus_map_key_clear(KEY_CAMERA); break;
Matjaz Hegedic802b24b2017-03-09 00:31:15 +0100707 case 0x88: asus_map_key_clear(KEY_RFKILL); break;
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100708 case 0xb5: asus_map_key_clear(KEY_CALC); break;
709 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
710 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600711
712 /* ASUS touchpad toggle */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100713 case 0x6b: asus_map_key_clear(KEY_F21); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600714
715 /* ROG key */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100716 case 0x38: asus_map_key_clear(KEY_PROG1); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600717
718 /* Fn+C ASUS Splendid */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100719 case 0xba: asus_map_key_clear(KEY_PROG2); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600720
721 /* Fn+Space Power4Gear Hybrid */
Matjaz Hegedica93913e2017-03-09 00:31:13 +0100722 case 0x5c: asus_map_key_clear(KEY_PROG3); break;
Chris Chiu1caccc22017-03-01 15:48:51 -0600723
Hans de Goede2340bad2018-11-26 11:52:17 +0100724 /* Fn+F5 "fan" symbol on FX503VD */
725 case 0x99: asus_map_key_clear(KEY_PROG4); break;
726
Chris Chiu1caccc22017-03-01 15:48:51 -0600727 default:
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +0100728 /* ASUS lazily declares 256 usages, ignore the rest,
729 * as some make the keyboard appear as a pointer device. */
730 return -1;
Chris Chiu1caccc22017-03-01 15:48:51 -0600731 }
Carlo Caioneaf22a612017-04-06 12:18:17 +0200732
733 /*
734 * Check and enable backlight only on devices with UsagePage ==
735 * 0xff31 to avoid initializing the keyboard firmware multiple
736 * times on devices with multiple HID descriptors but same
737 * PID/VID.
738 */
739 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
740 drvdata->enable_backlight = true;
741
Chris Chiu1caccc22017-03-01 15:48:51 -0600742 return 1;
743 }
744
Daniel Drake5be91802017-06-05 14:58:56 -0600745 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
746 set_bit(EV_REP, hi->input->evbit);
747 switch (usage->hid & HID_USAGE) {
748 case 0xff01: asus_map_key_clear(BTN_1); break;
749 case 0xff02: asus_map_key_clear(BTN_2); break;
750 case 0xff03: asus_map_key_clear(BTN_3); break;
751 case 0xff04: asus_map_key_clear(BTN_4); break;
752 case 0xff05: asus_map_key_clear(BTN_5); break;
753 case 0xff06: asus_map_key_clear(BTN_6); break;
754 case 0xff07: asus_map_key_clear(BTN_7); break;
755 case 0xff08: asus_map_key_clear(BTN_8); break;
756 case 0xff09: asus_map_key_clear(BTN_9); break;
757 case 0xff0a: asus_map_key_clear(BTN_A); break;
758 case 0xff0b: asus_map_key_clear(BTN_B); break;
759 case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
760 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
761 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
762 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
763 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
764 case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
765 default:
766 return 0;
767 }
768
769 return 1;
770 }
771
Matjaz Hegedic0485b1e2017-03-09 00:31:14 +0100772 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
773 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
774 switch (usage->hid & HID_USAGE) {
775 case 0xe2: /* Mute */
776 case 0xe9: /* Volume up */
777 case 0xea: /* Volume down */
778 return 0;
779 default:
780 /* Ignore dummy Consumer usages which make the
781 * keyboard incorrectly appear as a pointer device.
782 */
783 return -1;
784 }
785 }
786
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100787 return 0;
788}
789
790static int asus_start_multitouch(struct hid_device *hdev)
791{
792 int ret;
Colin Ian Kingb9ec7002018-01-06 15:50:27 +0000793 static const unsigned char buf[] = {
794 FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
795 };
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100796 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
797
798 if (!dmabuf) {
799 ret = -ENOMEM;
800 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
801 return ret;
802 }
803
804 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
805 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
806
807 kfree(dmabuf);
808
809 if (ret != sizeof(buf)) {
810 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
811 return ret;
812 }
813
814 return 0;
815}
816
817static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
818{
819 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
820
Hans de Goedec81760b2017-07-02 16:34:12 +0200821 if (drvdata->tp)
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100822 return asus_start_multitouch(hdev);
823
824 return 0;
825}
826
827static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
828{
829 int ret;
830 struct asus_drvdata *drvdata;
831
832 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
833 if (drvdata == NULL) {
834 hid_err(hdev, "Can't alloc Asus descriptor\n");
835 return -ENOMEM;
836 }
837
838 hid_set_drvdata(hdev, drvdata);
839
840 drvdata->quirks = id->driver_data;
841
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +0900842 /*
843 * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
844 * Thus, identify T90CHI dock with product name string.
845 */
NOGUCHI Hiroshi300c64d2019-01-29 13:23:20 +0900846 if (strstr(hdev->name, "T90CHI")) {
847 drvdata->quirks &= ~QUIRK_T100CHI;
848 drvdata->quirks |= QUIRK_T90CHI;
849 }
850
Hans de Goedec81760b2017-07-02 16:34:12 +0200851 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
852 drvdata->tp = &asus_i2c_tp;
853
Hans de Goede57573c52017-05-25 16:49:21 +0200854 if (drvdata->quirks & QUIRK_T100_KEYBOARD) {
855 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
856
Hans de Goedec81760b2017-07-02 16:34:12 +0200857 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
858 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
Hans de Goede762f9482017-11-26 16:40:10 +0100859 /*
Hans de Goededbd3ef22018-01-05 12:09:18 +0100860 * The T100HA uses the same USB-ids as the T100TAF and
861 * the T200TA uses the same USB-ids as the T100TA, while
862 * both have different max x/y values as the T100TA[F].
Hans de Goede762f9482017-11-26 16:40:10 +0100863 */
864 if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
865 drvdata->tp = &asus_t100ha_tp;
Hans de Goededbd3ef22018-01-05 12:09:18 +0100866 else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
867 drvdata->tp = &asus_t200ta_tp;
Hans de Goede762f9482017-11-26 16:40:10 +0100868 else
869 drvdata->tp = &asus_t100ta_tp;
Hans de Goedec81760b2017-07-02 16:34:12 +0200870 }
Hans de Goede57573c52017-05-25 16:49:21 +0200871 }
872
Hans de Goede73c75d32017-08-04 15:31:04 +0200873 if (drvdata->quirks & QUIRK_T100CHI) {
874 /*
875 * All functionality is on a single HID interface and for
876 * userspace the touchpad must be a separate input_dev.
877 */
Benjamin Tissoires39335d12018-03-20 12:04:49 +0100878 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
Hans de Goede73c75d32017-08-04 15:31:04 +0200879 drvdata->tp = &asus_t100chi_tp;
880 }
881
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100882 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
883 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
884
NOGUCHI Hiroshi6311d322019-01-29 13:31:29 +0900885 drvdata->hdev = hdev;
886
887 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
888 ret = asus_battery_probe(hdev);
889 if (ret) {
890 hid_err(hdev,
891 "Asus hid battery_probe failed: %d\n", ret);
892 return ret;
893 }
894 }
895
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100896 ret = hid_parse(hdev);
897 if (ret) {
898 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
899 return ret;
900 }
901
Aleix Roca Nonell0e1f37d2018-12-16 16:53:19 +0100902 /* use hid-multitouch for T101HA touchpad */
903 if (id->driver_data & QUIRK_T101HA_DOCK &&
904 hdev->collection->usage == HID_GD_MOUSE)
905 return -ENODEV;
906
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100907 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
908 if (ret) {
909 hid_err(hdev, "Asus hw start failed: %d\n", ret);
910 return ret;
911 }
912
913 if (!drvdata->input) {
914 hid_err(hdev, "Asus input not registered\n");
915 ret = -ENOMEM;
916 goto err_stop_hw;
917 }
918
Hans de Goedec81760b2017-07-02 16:34:12 +0200919 if (drvdata->tp) {
Brendan McGrathc8b1b3d2016-12-10 21:20:42 +1100920 drvdata->input->name = "Asus TouchPad";
921 } else {
922 drvdata->input->name = "Asus Keyboard";
923 }
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100924
Hans de Goedec81760b2017-07-02 16:34:12 +0200925 if (drvdata->tp) {
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100926 ret = asus_start_multitouch(hdev);
927 if (ret)
928 goto err_stop_hw;
929 }
930
931 return 0;
932err_stop_hw:
933 hid_hw_stop(hdev);
934 return ret;
935}
936
Carlo Caioneaf22a612017-04-06 12:18:17 +0200937static void asus_remove(struct hid_device *hdev)
938{
939 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
940
941 if (drvdata->kbd_backlight) {
942 drvdata->kbd_backlight->removed = true;
943 cancel_work_sync(&drvdata->kbd_backlight->work);
944 }
Carlo Caione715e9442017-05-30 22:39:46 +0200945
946 hid_hw_stop(hdev);
Carlo Caioneaf22a612017-04-06 12:18:17 +0200947}
948
Maxime Bellengé832e1ee2017-09-07 21:17:28 +0200949static const __u8 asus_g752_fixed_rdesc[] = {
950 0x19, 0x00, /* Usage Minimum (0x00) */
951 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
952};
953
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +0900954static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
955 unsigned int *rsize)
956{
Brendan McGrath9ce12d82016-11-29 18:59:25 +1100957 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
958
959 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
960 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
Yusuke Fujimakib94f7d52016-04-03 23:15:16 +0900961 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +0900962 rdesc[55] = 0xdd;
963 }
Hans de Goede33edee42018-01-05 12:09:19 +0100964 /* For the T100TA/T200TA keyboard dock */
Hans de Goede76dd1fb2017-05-15 09:31:41 +0200965 if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
Hans de Goede33edee42018-01-05 12:09:19 +0100966 (*rsize == 76 || *rsize == 101) &&
967 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
Hans de Goede76dd1fb2017-05-15 09:31:41 +0200968 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
969 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
970 }
NOGUCHI Hiroshi300c64d2019-01-29 13:23:20 +0900971 /* For the T100CHI/T90CHI keyboard dock */
972 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
973 int rsize_orig;
974 int offs;
975
976 if (drvdata->quirks & QUIRK_T100CHI) {
977 rsize_orig = 403;
978 offs = 388;
979 } else {
980 rsize_orig = 306;
981 offs = 291;
982 }
983
Hans de Goede5703e522017-07-02 16:34:15 +0200984 /*
985 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
986 * (FFh) and clear the flags in the Input() byte.
987 * Note the descriptor has a bogus 0 byte at the end so we
988 * only need 1 extra byte.
989 */
NOGUCHI Hiroshi300c64d2019-01-29 13:23:20 +0900990 if (*rsize == rsize_orig &&
991 rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
992 *rsize = rsize_orig + 1;
993 rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
994 if (!rdesc)
995 return NULL;
Hans de Goede5703e522017-07-02 16:34:15 +0200996
NOGUCHI Hiroshi300c64d2019-01-29 13:23:20 +0900997 hid_info(hdev, "Fixing up %s keyb report descriptor\n",
998 drvdata->quirks & QUIRK_T100CHI ?
999 "T100CHI" : "T90CHI");
1000 memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1001 rdesc[offs] = 0x19;
1002 rdesc[offs + 1] = 0x00;
1003 rdesc[offs + 2] = 0x29;
1004 rdesc[offs + 3] = 0xff;
1005 rdesc[offs + 14] = 0x00;
1006 }
Hans de Goede5703e522017-07-02 16:34:15 +02001007 }
NOGUCHI Hiroshi300c64d2019-01-29 13:23:20 +09001008
Maxime Bellengé832e1ee2017-09-07 21:17:28 +02001009 if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1010 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1011 /* report is missing usage mninum and maximum */
1012 __u8 *new_rdesc;
1013 size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1014
1015 new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1016 if (new_rdesc == NULL)
1017 return rdesc;
1018
1019 hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1020 /* copy the valid part */
1021 memcpy(new_rdesc, rdesc, 61);
1022 /* insert missing part */
1023 memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1024 /* copy remaining data */
1025 memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1026
1027 *rsize = new_size;
1028 rdesc = new_rdesc;
1029 }
Hans de Goede76dd1fb2017-05-15 09:31:41 +02001030
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +09001031 return rdesc;
1032}
1033
1034static const struct hid_device_id asus_devices[] = {
Brendan McGrath9ce12d82016-11-29 18:59:25 +11001035 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
Matjaz Hegedica93913e2017-03-09 00:31:13 +01001036 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
Brendan McGrath9ce12d82016-11-29 18:59:25 +11001037 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
Hans de Goedec81760b2017-07-02 16:34:12 +02001038 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
Chris Chiu1caccc22017-03-01 15:48:51 -06001039 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
Mustafa Kuscu339ee3f2017-10-28 10:31:40 +03001040 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
Chris Chiu1caccc22017-03-01 15:48:51 -06001041 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
Carlo Caioneaf22a612017-04-06 12:18:17 +02001042 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
Hans de Goede76dd1fb2017-05-15 09:31:41 +02001043 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
Maxime Bellengé832e1ee2017-09-07 21:17:28 +02001044 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1045 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
Hans de Goede2340bad2018-11-26 11:52:17 +01001046 USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1047 QUIRK_USE_KBD_BACKLIGHT },
1048 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
Hans de Goede762f9482017-11-26 16:40:10 +01001049 USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1050 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1051 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1052 USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
Hans de Goede76dd1fb2017-05-15 09:31:41 +02001053 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
Aleix Roca Nonell0e1f37d2018-12-16 16:53:19 +01001054 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1055 USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD), QUIRK_T101HA_DOCK },
Daniel Drake5be91802017-06-05 14:58:56 -06001056 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
Daniel Drake38b2d782017-06-05 14:58:57 -06001057 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
Daniel Drake5be91802017-06-05 14:58:56 -06001058 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
Hans de Goede5703e522017-07-02 16:34:15 +02001059 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1060 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1061
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +09001062 { }
1063};
1064MODULE_DEVICE_TABLE(hid, asus_devices);
1065
1066static struct hid_driver asus_driver = {
Brendan McGrath9ce12d82016-11-29 18:59:25 +11001067 .name = "asus",
1068 .id_table = asus_devices,
1069 .report_fixup = asus_report_fixup,
1070 .probe = asus_probe,
Carlo Caioneaf22a612017-04-06 12:18:17 +02001071 .remove = asus_remove,
Brendan McGrath9ce12d82016-11-29 18:59:25 +11001072 .input_mapping = asus_input_mapping,
1073 .input_configured = asus_input_configured,
1074#ifdef CONFIG_PM
1075 .reset_resume = asus_reset_resume,
1076#endif
Hans de Goedee98e3802018-11-26 11:52:16 +01001077 .event = asus_event,
Brendan McGrath9ce12d82016-11-29 18:59:25 +11001078 .raw_event = asus_raw_event
Yusuke Fujimakieeb01a52016-03-21 16:18:42 +09001079};
1080module_hid_driver(asus_driver);
1081
1082MODULE_LICENSE("GPL");