Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/hid/hid-asus.c
29266 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* HID driver for Asus notebook built-in keyboard.
4
* Fixes small logical maximum to match usage maximum.
5
*
6
* Currently supported devices are:
7
* EeeBook X205TA
8
* VivoBook E200HA
9
*
10
* Copyright (c) 2016 Yusuke Fujimaki <[email protected]>
11
*
12
* This module based on hid-ortek by
13
* Copyright (c) 2010 Johnathon Harris <[email protected]>
14
* Copyright (c) 2011 Jiri Kosina
15
*
16
* This module has been updated to add support for Asus i2c touchpad.
17
*
18
* Copyright (c) 2016 Brendan McGrath <[email protected]>
19
* Copyright (c) 2016 Victor Vlasenko <[email protected]>
20
* Copyright (c) 2016 Frederik Wenigwieser <[email protected]>
21
*/
22
23
/*
24
*/
25
26
#include <linux/dmi.h>
27
#include <linux/hid.h>
28
#include <linux/module.h>
29
#include <linux/platform_data/x86/asus-wmi.h>
30
#include <linux/input/mt.h>
31
#include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
32
#include <linux/power_supply.h>
33
#include <linux/leds.h>
34
35
#include "hid-ids.h"
36
37
MODULE_AUTHOR("Yusuke Fujimaki <[email protected]>");
38
MODULE_AUTHOR("Brendan McGrath <[email protected]>");
39
MODULE_AUTHOR("Victor Vlasenko <[email protected]>");
40
MODULE_AUTHOR("Frederik Wenigwieser <[email protected]>");
41
MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
42
43
#define T100_TPAD_INTF 2
44
#define MEDION_E1239T_TPAD_INTF 1
45
46
#define E1239T_TP_TOGGLE_REPORT_ID 0x05
47
#define T100CHI_MOUSE_REPORT_ID 0x06
48
#define FEATURE_REPORT_ID 0x0d
49
#define INPUT_REPORT_ID 0x5d
50
#define FEATURE_KBD_REPORT_ID 0x5a
51
#define FEATURE_KBD_REPORT_SIZE 16
52
#define FEATURE_KBD_LED_REPORT_ID1 0x5d
53
#define FEATURE_KBD_LED_REPORT_ID2 0x5e
54
55
#define ROG_ALLY_REPORT_SIZE 64
56
#define ROG_ALLY_X_MIN_MCU 313
57
#define ROG_ALLY_MIN_MCU 319
58
59
#define SUPPORT_KBD_BACKLIGHT BIT(0)
60
61
#define MAX_TOUCH_MAJOR 8
62
#define MAX_PRESSURE 128
63
64
#define BTN_LEFT_MASK 0x01
65
#define CONTACT_TOOL_TYPE_MASK 0x80
66
#define CONTACT_X_MSB_MASK 0xf0
67
#define CONTACT_Y_MSB_MASK 0x0f
68
#define CONTACT_TOUCH_MAJOR_MASK 0x07
69
#define CONTACT_PRESSURE_MASK 0x7f
70
71
#define BATTERY_REPORT_ID (0x03)
72
#define BATTERY_REPORT_SIZE (1 + 8)
73
#define BATTERY_LEVEL_MAX ((u8)255)
74
#define BATTERY_STAT_DISCONNECT (0)
75
#define BATTERY_STAT_CHARGING (1)
76
#define BATTERY_STAT_FULL (2)
77
78
#define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
79
#define QUIRK_NO_INIT_REPORTS BIT(1)
80
#define QUIRK_SKIP_INPUT_MAPPING BIT(2)
81
#define QUIRK_IS_MULTITOUCH BIT(3)
82
#define QUIRK_NO_CONSUMER_USAGES BIT(4)
83
#define QUIRK_USE_KBD_BACKLIGHT BIT(5)
84
#define QUIRK_T100_KEYBOARD BIT(6)
85
#define QUIRK_T100CHI BIT(7)
86
#define QUIRK_G752_KEYBOARD BIT(8)
87
#define QUIRK_T90CHI BIT(9)
88
#define QUIRK_MEDION_E1239T BIT(10)
89
#define QUIRK_ROG_NKEY_KEYBOARD BIT(11)
90
#define QUIRK_ROG_CLAYMORE_II_KEYBOARD BIT(12)
91
#define QUIRK_ROG_ALLY_XPAD BIT(13)
92
93
#define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
94
QUIRK_NO_INIT_REPORTS | \
95
QUIRK_NO_CONSUMER_USAGES)
96
#define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
97
QUIRK_SKIP_INPUT_MAPPING | \
98
QUIRK_IS_MULTITOUCH)
99
100
#define TRKID_SGN ((TRKID_MAX + 1) >> 1)
101
102
struct asus_kbd_leds {
103
struct led_classdev cdev;
104
struct hid_device *hdev;
105
struct work_struct work;
106
unsigned int brightness;
107
spinlock_t lock;
108
bool removed;
109
};
110
111
struct asus_touchpad_info {
112
int max_x;
113
int max_y;
114
int res_x;
115
int res_y;
116
int contact_size;
117
int max_contacts;
118
int report_size;
119
};
120
121
struct asus_drvdata {
122
unsigned long quirks;
123
struct hid_device *hdev;
124
struct input_dev *input;
125
struct input_dev *tp_kbd_input;
126
struct asus_kbd_leds *kbd_backlight;
127
const struct asus_touchpad_info *tp;
128
bool enable_backlight;
129
struct power_supply *battery;
130
struct power_supply_desc battery_desc;
131
int battery_capacity;
132
int battery_stat;
133
bool battery_in_query;
134
unsigned long battery_next_query;
135
};
136
137
static int asus_report_battery(struct asus_drvdata *, u8 *, int);
138
139
static const struct asus_touchpad_info asus_i2c_tp = {
140
.max_x = 2794,
141
.max_y = 1758,
142
.contact_size = 5,
143
.max_contacts = 5,
144
.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
145
};
146
147
static const struct asus_touchpad_info asus_t100ta_tp = {
148
.max_x = 2240,
149
.max_y = 1120,
150
.res_x = 30, /* units/mm */
151
.res_y = 27, /* units/mm */
152
.contact_size = 5,
153
.max_contacts = 5,
154
.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
155
};
156
157
static const struct asus_touchpad_info asus_t100ha_tp = {
158
.max_x = 2640,
159
.max_y = 1320,
160
.res_x = 30, /* units/mm */
161
.res_y = 29, /* units/mm */
162
.contact_size = 5,
163
.max_contacts = 5,
164
.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
165
};
166
167
static const struct asus_touchpad_info asus_t200ta_tp = {
168
.max_x = 3120,
169
.max_y = 1716,
170
.res_x = 30, /* units/mm */
171
.res_y = 28, /* units/mm */
172
.contact_size = 5,
173
.max_contacts = 5,
174
.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
175
};
176
177
static const struct asus_touchpad_info asus_t100chi_tp = {
178
.max_x = 2640,
179
.max_y = 1320,
180
.res_x = 31, /* units/mm */
181
.res_y = 29, /* units/mm */
182
.contact_size = 3,
183
.max_contacts = 4,
184
.report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
185
};
186
187
static const struct asus_touchpad_info medion_e1239t_tp = {
188
.max_x = 2640,
189
.max_y = 1380,
190
.res_x = 29, /* units/mm */
191
.res_y = 28, /* units/mm */
192
.contact_size = 5,
193
.max_contacts = 5,
194
.report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
195
};
196
197
static void asus_report_contact_down(struct asus_drvdata *drvdat,
198
int toolType, u8 *data)
199
{
200
struct input_dev *input = drvdat->input;
201
int touch_major, pressure, x, y;
202
203
x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
204
y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
205
206
input_report_abs(input, ABS_MT_POSITION_X, x);
207
input_report_abs(input, ABS_MT_POSITION_Y, y);
208
209
if (drvdat->tp->contact_size < 5)
210
return;
211
212
if (toolType == MT_TOOL_PALM) {
213
touch_major = MAX_TOUCH_MAJOR;
214
pressure = MAX_PRESSURE;
215
} else {
216
touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
217
pressure = data[4] & CONTACT_PRESSURE_MASK;
218
}
219
220
input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
221
input_report_abs(input, ABS_MT_PRESSURE, pressure);
222
}
223
224
/* Required for Synaptics Palm Detection */
225
static void asus_report_tool_width(struct asus_drvdata *drvdat)
226
{
227
struct input_mt *mt = drvdat->input->mt;
228
struct input_mt_slot *oldest;
229
int oldid, i;
230
231
if (drvdat->tp->contact_size < 5)
232
return;
233
234
oldest = NULL;
235
oldid = mt->trkid;
236
237
for (i = 0; i < mt->num_slots; ++i) {
238
struct input_mt_slot *ps = &mt->slots[i];
239
int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
240
241
if (id < 0)
242
continue;
243
if ((id - oldid) & TRKID_SGN) {
244
oldest = ps;
245
oldid = id;
246
}
247
}
248
249
if (oldest) {
250
input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
251
input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
252
}
253
}
254
255
static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
256
{
257
int i, toolType = MT_TOOL_FINGER;
258
u8 *contactData = data + 2;
259
260
if (size != drvdat->tp->report_size)
261
return 0;
262
263
for (i = 0; i < drvdat->tp->max_contacts; i++) {
264
bool down = !!(data[1] & BIT(i+3));
265
266
if (drvdat->tp->contact_size >= 5)
267
toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
268
MT_TOOL_PALM : MT_TOOL_FINGER;
269
270
input_mt_slot(drvdat->input, i);
271
input_mt_report_slot_state(drvdat->input, toolType, down);
272
273
if (down) {
274
asus_report_contact_down(drvdat, toolType, contactData);
275
contactData += drvdat->tp->contact_size;
276
}
277
}
278
279
input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
280
asus_report_tool_width(drvdat);
281
282
input_mt_sync_frame(drvdat->input);
283
input_sync(drvdat->input);
284
285
return 1;
286
}
287
288
static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size)
289
{
290
if (size != 3)
291
return 0;
292
293
/* Handle broken mute key which only sends press events */
294
if (!drvdat->tp &&
295
data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) {
296
input_report_key(drvdat->input, KEY_MUTE, 1);
297
input_sync(drvdat->input);
298
input_report_key(drvdat->input, KEY_MUTE, 0);
299
input_sync(drvdat->input);
300
return 1;
301
}
302
303
/* Handle custom touchpad toggle key which only sends press events */
304
if (drvdat->tp_kbd_input &&
305
data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) {
306
input_report_key(drvdat->tp_kbd_input, KEY_F21, 1);
307
input_sync(drvdat->tp_kbd_input);
308
input_report_key(drvdat->tp_kbd_input, KEY_F21, 0);
309
input_sync(drvdat->tp_kbd_input);
310
return 1;
311
}
312
313
return 0;
314
}
315
316
static int asus_event(struct hid_device *hdev, struct hid_field *field,
317
struct hid_usage *usage, __s32 value)
318
{
319
if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
320
(usage->hid & HID_USAGE) != 0x00 &&
321
(usage->hid & HID_USAGE) != 0xff && !usage->type) {
322
hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
323
usage->hid & HID_USAGE);
324
}
325
326
return 0;
327
}
328
329
static int asus_raw_event(struct hid_device *hdev,
330
struct hid_report *report, u8 *data, int size)
331
{
332
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
333
334
if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
335
return asus_report_battery(drvdata, data, size);
336
337
if (drvdata->tp && data[0] == INPUT_REPORT_ID)
338
return asus_report_input(drvdata, data, size);
339
340
if (drvdata->quirks & QUIRK_MEDION_E1239T)
341
return asus_e1239t_event(drvdata, data, size);
342
343
/*
344
* Skip these report ID, the device emits a continuous stream associated
345
* with the AURA mode it is in which looks like an 'echo'.
346
*/
347
if (report->id == FEATURE_KBD_LED_REPORT_ID1 || report->id == FEATURE_KBD_LED_REPORT_ID2)
348
return -1;
349
if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
350
/*
351
* G713 and G733 send these codes on some keypresses, depending on
352
* the key pressed it can trigger a shutdown event if not caught.
353
*/
354
if (data[0] == 0x02 && data[1] == 0x30) {
355
return -1;
356
}
357
}
358
359
if (drvdata->quirks & QUIRK_ROG_CLAYMORE_II_KEYBOARD) {
360
/*
361
* CLAYMORE II keyboard sends this packet when it goes to sleep
362
* this causes the whole system to go into suspend.
363
*/
364
365
if(size == 2 && data[0] == 0x02 && data[1] == 0x00) {
366
return -1;
367
}
368
}
369
370
return 0;
371
}
372
373
static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t buf_size)
374
{
375
unsigned char *dmabuf;
376
int ret;
377
378
dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
379
if (!dmabuf)
380
return -ENOMEM;
381
382
/*
383
* The report ID should be set from the incoming buffer due to LED and key
384
* interfaces having different pages
385
*/
386
ret = hid_hw_raw_request(hdev, buf[0], dmabuf,
387
buf_size, HID_FEATURE_REPORT,
388
HID_REQ_SET_REPORT);
389
kfree(dmabuf);
390
391
return ret;
392
}
393
394
static int asus_kbd_init(struct hid_device *hdev, u8 report_id)
395
{
396
const u8 buf[] = { report_id, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
397
0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
398
int ret;
399
400
ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
401
if (ret < 0)
402
hid_err(hdev, "Asus failed to send init command: %d\n", ret);
403
404
return ret;
405
}
406
407
static int asus_kbd_get_functions(struct hid_device *hdev,
408
unsigned char *kbd_func,
409
u8 report_id)
410
{
411
const u8 buf[] = { report_id, 0x05, 0x20, 0x31, 0x00, 0x08 };
412
u8 *readbuf;
413
int ret;
414
415
ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
416
if (ret < 0) {
417
hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
418
return ret;
419
}
420
421
readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
422
if (!readbuf)
423
return -ENOMEM;
424
425
ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
426
FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
427
HID_REQ_GET_REPORT);
428
if (ret < 0) {
429
hid_err(hdev, "Asus failed to request functions: %d\n", ret);
430
kfree(readbuf);
431
return ret;
432
}
433
434
*kbd_func = readbuf[6];
435
436
kfree(readbuf);
437
return ret;
438
}
439
440
static int asus_kbd_disable_oobe(struct hid_device *hdev)
441
{
442
const u8 init[][6] = {
443
{ FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 },
444
{ FEATURE_KBD_REPORT_ID, 0xBA, 0xC5, 0xC4 },
445
{ FEATURE_KBD_REPORT_ID, 0xD0, 0x8F, 0x01 },
446
{ FEATURE_KBD_REPORT_ID, 0xD0, 0x85, 0xFF }
447
};
448
int ret;
449
450
for (size_t i = 0; i < ARRAY_SIZE(init); i++) {
451
ret = asus_kbd_set_report(hdev, init[i], sizeof(init[i]));
452
if (ret < 0)
453
return ret;
454
}
455
456
hid_info(hdev, "Disabled OOBE for keyboard\n");
457
return 0;
458
}
459
460
static void asus_schedule_work(struct asus_kbd_leds *led)
461
{
462
unsigned long flags;
463
464
spin_lock_irqsave(&led->lock, flags);
465
if (!led->removed)
466
schedule_work(&led->work);
467
spin_unlock_irqrestore(&led->lock, flags);
468
}
469
470
static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
471
enum led_brightness brightness)
472
{
473
struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
474
cdev);
475
unsigned long flags;
476
477
spin_lock_irqsave(&led->lock, flags);
478
led->brightness = brightness;
479
spin_unlock_irqrestore(&led->lock, flags);
480
481
asus_schedule_work(led);
482
}
483
484
static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
485
{
486
struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
487
cdev);
488
enum led_brightness brightness;
489
unsigned long flags;
490
491
spin_lock_irqsave(&led->lock, flags);
492
brightness = led->brightness;
493
spin_unlock_irqrestore(&led->lock, flags);
494
495
return brightness;
496
}
497
498
static void asus_kbd_backlight_work(struct work_struct *work)
499
{
500
struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
501
u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
502
int ret;
503
unsigned long flags;
504
505
spin_lock_irqsave(&led->lock, flags);
506
buf[4] = led->brightness;
507
spin_unlock_irqrestore(&led->lock, flags);
508
509
ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
510
if (ret < 0)
511
hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
512
}
513
514
/* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
515
* precedence. We only activate HID-based backlight control when the
516
* WMI control is not available.
517
*/
518
static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
519
{
520
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
521
u32 value;
522
int ret;
523
524
if (!IS_ENABLED(CONFIG_ASUS_WMI))
525
return false;
526
527
if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD &&
528
dmi_check_system(asus_use_hid_led_dmi_ids)) {
529
hid_info(hdev, "using HID for asus::kbd_backlight\n");
530
return false;
531
}
532
533
ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
534
ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
535
hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
536
if (ret)
537
return false;
538
539
return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
540
}
541
542
/*
543
* We don't care about any other part of the string except the version section.
544
* Example strings: FGA80100.RC72LA.312_T01, FGA80100.RC71LS.318_T01
545
* The bytes "5a 05 03 31 00 1a 13" and possibly more come before the version
546
* string, and there may be additional bytes after the version string such as
547
* "75 00 74 00 65 00" or a postfix such as "_T01"
548
*/
549
static int mcu_parse_version_string(const u8 *response, size_t response_size)
550
{
551
const u8 *end = response + response_size;
552
const u8 *p = response;
553
int dots, err, version;
554
char buf[4];
555
556
dots = 0;
557
while (p < end && dots < 2) {
558
if (*p++ == '.')
559
dots++;
560
}
561
562
if (dots != 2 || p >= end || (p + 3) >= end)
563
return -EINVAL;
564
565
memcpy(buf, p, 3);
566
buf[3] = '\0';
567
568
err = kstrtoint(buf, 10, &version);
569
if (err || version < 0)
570
return -EINVAL;
571
572
return version;
573
}
574
575
static int mcu_request_version(struct hid_device *hdev)
576
{
577
u8 *response __free(kfree) = kzalloc(ROG_ALLY_REPORT_SIZE, GFP_KERNEL);
578
const u8 request[] = { 0x5a, 0x05, 0x03, 0x31, 0x00, 0x20 };
579
int ret;
580
581
if (!response)
582
return -ENOMEM;
583
584
ret = asus_kbd_set_report(hdev, request, sizeof(request));
585
if (ret < 0)
586
return ret;
587
588
ret = hid_hw_raw_request(hdev, FEATURE_REPORT_ID, response,
589
ROG_ALLY_REPORT_SIZE, HID_FEATURE_REPORT,
590
HID_REQ_GET_REPORT);
591
if (ret < 0)
592
return ret;
593
594
ret = mcu_parse_version_string(response, ROG_ALLY_REPORT_SIZE);
595
if (ret < 0) {
596
pr_err("Failed to parse MCU version: %d\n", ret);
597
print_hex_dump(KERN_ERR, "MCU: ", DUMP_PREFIX_NONE,
598
16, 1, response, ROG_ALLY_REPORT_SIZE, false);
599
}
600
601
return ret;
602
}
603
604
static void validate_mcu_fw_version(struct hid_device *hdev, int idProduct)
605
{
606
int min_version, version;
607
608
version = mcu_request_version(hdev);
609
if (version < 0)
610
return;
611
612
switch (idProduct) {
613
case USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY:
614
min_version = ROG_ALLY_MIN_MCU;
615
break;
616
case USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X:
617
min_version = ROG_ALLY_X_MIN_MCU;
618
break;
619
default:
620
min_version = 0;
621
}
622
623
if (version < min_version) {
624
hid_warn(hdev,
625
"The MCU firmware version must be %d or greater to avoid issues with suspend.\n",
626
min_version);
627
} else {
628
set_ally_mcu_hack(ASUS_WMI_ALLY_MCU_HACK_DISABLED);
629
set_ally_mcu_powersave(true);
630
}
631
}
632
633
static int asus_kbd_register_leds(struct hid_device *hdev)
634
{
635
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
636
struct usb_interface *intf;
637
struct usb_device *udev;
638
unsigned char kbd_func;
639
int ret;
640
641
if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
642
/* Initialize keyboard */
643
ret = asus_kbd_init(hdev, FEATURE_KBD_REPORT_ID);
644
if (ret < 0)
645
return ret;
646
647
/* The LED endpoint is initialised in two HID */
648
ret = asus_kbd_init(hdev, FEATURE_KBD_LED_REPORT_ID1);
649
if (ret < 0)
650
return ret;
651
652
ret = asus_kbd_init(hdev, FEATURE_KBD_LED_REPORT_ID2);
653
if (ret < 0)
654
return ret;
655
656
if (dmi_match(DMI_PRODUCT_FAMILY, "ProArt P16")) {
657
ret = asus_kbd_disable_oobe(hdev);
658
if (ret < 0)
659
return ret;
660
}
661
662
if (drvdata->quirks & QUIRK_ROG_ALLY_XPAD) {
663
intf = to_usb_interface(hdev->dev.parent);
664
udev = interface_to_usbdev(intf);
665
validate_mcu_fw_version(hdev,
666
le16_to_cpu(udev->descriptor.idProduct));
667
}
668
669
} else {
670
/* Initialize keyboard */
671
ret = asus_kbd_init(hdev, FEATURE_KBD_REPORT_ID);
672
if (ret < 0)
673
return ret;
674
675
/* Get keyboard functions */
676
ret = asus_kbd_get_functions(hdev, &kbd_func, FEATURE_KBD_REPORT_ID);
677
if (ret < 0)
678
return ret;
679
680
/* Check for backlight support */
681
if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
682
return -ENODEV;
683
}
684
685
drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
686
sizeof(struct asus_kbd_leds),
687
GFP_KERNEL);
688
if (!drvdata->kbd_backlight)
689
return -ENOMEM;
690
691
drvdata->kbd_backlight->removed = false;
692
drvdata->kbd_backlight->brightness = 0;
693
drvdata->kbd_backlight->hdev = hdev;
694
drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
695
drvdata->kbd_backlight->cdev.max_brightness = 3;
696
drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
697
drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
698
INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
699
spin_lock_init(&drvdata->kbd_backlight->lock);
700
701
ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
702
if (ret < 0) {
703
/* No need to have this still around */
704
devm_kfree(&hdev->dev, drvdata->kbd_backlight);
705
}
706
707
return ret;
708
}
709
710
/*
711
* [0] REPORT_ID (same value defined in report descriptor)
712
* [1] rest battery level. range [0..255]
713
* [2]..[7] Bluetooth hardware address (MAC address)
714
* [8] charging status
715
* = 0 : AC offline / discharging
716
* = 1 : AC online / charging
717
* = 2 : AC online / fully charged
718
*/
719
static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
720
{
721
u8 sts;
722
u8 lvl;
723
int val;
724
725
lvl = data[1];
726
sts = data[8];
727
728
drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
729
730
switch (sts) {
731
case BATTERY_STAT_CHARGING:
732
val = POWER_SUPPLY_STATUS_CHARGING;
733
break;
734
case BATTERY_STAT_FULL:
735
val = POWER_SUPPLY_STATUS_FULL;
736
break;
737
case BATTERY_STAT_DISCONNECT:
738
default:
739
val = POWER_SUPPLY_STATUS_DISCHARGING;
740
break;
741
}
742
drvdata->battery_stat = val;
743
744
return 0;
745
}
746
747
static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
748
{
749
/* notify only the autonomous event by device */
750
if ((drvdata->battery_in_query == false) &&
751
(size == BATTERY_REPORT_SIZE))
752
power_supply_changed(drvdata->battery);
753
754
return 0;
755
}
756
757
static int asus_battery_query(struct asus_drvdata *drvdata)
758
{
759
u8 *buf;
760
int ret = 0;
761
762
buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
763
if (!buf)
764
return -ENOMEM;
765
766
drvdata->battery_in_query = true;
767
ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
768
buf, BATTERY_REPORT_SIZE,
769
HID_INPUT_REPORT, HID_REQ_GET_REPORT);
770
drvdata->battery_in_query = false;
771
if (ret == BATTERY_REPORT_SIZE)
772
ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
773
else
774
ret = -ENODATA;
775
776
kfree(buf);
777
778
return ret;
779
}
780
781
static enum power_supply_property asus_battery_props[] = {
782
POWER_SUPPLY_PROP_STATUS,
783
POWER_SUPPLY_PROP_PRESENT,
784
POWER_SUPPLY_PROP_CAPACITY,
785
POWER_SUPPLY_PROP_SCOPE,
786
POWER_SUPPLY_PROP_MODEL_NAME,
787
};
788
789
#define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */
790
791
static int asus_battery_get_property(struct power_supply *psy,
792
enum power_supply_property psp,
793
union power_supply_propval *val)
794
{
795
struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
796
int ret = 0;
797
798
switch (psp) {
799
case POWER_SUPPLY_PROP_STATUS:
800
case POWER_SUPPLY_PROP_CAPACITY:
801
if (time_before(drvdata->battery_next_query, jiffies)) {
802
drvdata->battery_next_query =
803
jiffies + QUERY_MIN_INTERVAL;
804
ret = asus_battery_query(drvdata);
805
if (ret)
806
return ret;
807
}
808
if (psp == POWER_SUPPLY_PROP_STATUS)
809
val->intval = drvdata->battery_stat;
810
else
811
val->intval = drvdata->battery_capacity;
812
break;
813
case POWER_SUPPLY_PROP_PRESENT:
814
val->intval = 1;
815
break;
816
case POWER_SUPPLY_PROP_SCOPE:
817
val->intval = POWER_SUPPLY_SCOPE_DEVICE;
818
break;
819
case POWER_SUPPLY_PROP_MODEL_NAME:
820
val->strval = drvdata->hdev->name;
821
break;
822
default:
823
ret = -EINVAL;
824
break;
825
}
826
827
return ret;
828
}
829
830
static int asus_battery_probe(struct hid_device *hdev)
831
{
832
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
833
struct power_supply_config pscfg = { .drv_data = drvdata };
834
int ret = 0;
835
836
drvdata->battery_capacity = 0;
837
drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
838
drvdata->battery_in_query = false;
839
840
drvdata->battery_desc.properties = asus_battery_props;
841
drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
842
drvdata->battery_desc.get_property = asus_battery_get_property;
843
drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
844
drvdata->battery_desc.use_for_apm = 0;
845
drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
846
"asus-keyboard-%s-battery",
847
strlen(hdev->uniq) ?
848
hdev->uniq : dev_name(&hdev->dev));
849
if (!drvdata->battery_desc.name)
850
return -ENOMEM;
851
852
drvdata->battery_next_query = jiffies;
853
854
drvdata->battery = devm_power_supply_register(&hdev->dev,
855
&(drvdata->battery_desc), &pscfg);
856
if (IS_ERR(drvdata->battery)) {
857
ret = PTR_ERR(drvdata->battery);
858
drvdata->battery = NULL;
859
hid_err(hdev, "Unable to register battery device\n");
860
return ret;
861
}
862
863
power_supply_powers(drvdata->battery, &hdev->dev);
864
865
return ret;
866
}
867
868
static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
869
{
870
struct input_dev *input = hi->input;
871
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
872
873
/* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
874
if (drvdata->quirks & QUIRK_T100CHI &&
875
hi->report->id != T100CHI_MOUSE_REPORT_ID)
876
return 0;
877
878
/* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
879
if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
880
switch (hi->report->id) {
881
case E1239T_TP_TOGGLE_REPORT_ID:
882
input_set_capability(input, EV_KEY, KEY_F21);
883
input->name = "Asus Touchpad Keys";
884
drvdata->tp_kbd_input = input;
885
return 0;
886
case INPUT_REPORT_ID:
887
break; /* Touchpad report, handled below */
888
default:
889
return 0; /* Ignore other reports */
890
}
891
}
892
893
if (drvdata->tp) {
894
int ret;
895
896
input_set_abs_params(input, ABS_MT_POSITION_X, 0,
897
drvdata->tp->max_x, 0, 0);
898
input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
899
drvdata->tp->max_y, 0, 0);
900
input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
901
input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
902
903
if (drvdata->tp->contact_size >= 5) {
904
input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
905
MAX_TOUCH_MAJOR, 0, 0);
906
input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
907
MAX_TOUCH_MAJOR, 0, 0);
908
input_set_abs_params(input, ABS_MT_PRESSURE, 0,
909
MAX_PRESSURE, 0, 0);
910
}
911
912
__set_bit(BTN_LEFT, input->keybit);
913
__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
914
915
ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
916
INPUT_MT_POINTER);
917
918
if (ret) {
919
hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
920
return ret;
921
}
922
}
923
924
drvdata->input = input;
925
926
if (drvdata->enable_backlight &&
927
!asus_kbd_wmi_led_control_present(hdev) &&
928
asus_kbd_register_leds(hdev))
929
hid_warn(hdev, "Failed to initialize backlight.\n");
930
931
return 0;
932
}
933
934
#define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
935
max, EV_KEY, (c))
936
static int asus_input_mapping(struct hid_device *hdev,
937
struct hid_input *hi, struct hid_field *field,
938
struct hid_usage *usage, unsigned long **bit,
939
int *max)
940
{
941
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
942
943
if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
944
/* Don't map anything from the HID report.
945
* We do it all manually in asus_input_configured
946
*/
947
return -1;
948
}
949
950
/*
951
* Ignore a bunch of bogus collections in the T100CHI descriptor.
952
* This avoids a bunch of non-functional hid_input devices getting
953
* created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
954
*/
955
if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
956
(field->application == (HID_UP_GENDESK | 0x0080) ||
957
field->application == HID_GD_MOUSE ||
958
usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
959
usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
960
usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
961
return -1;
962
963
/* ASUS-specific keyboard hotkeys and led backlight */
964
if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ASUSVENDOR) {
965
switch (usage->hid & HID_USAGE) {
966
case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
967
case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
968
case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break;
969
case 0x6c: asus_map_key_clear(KEY_SLEEP); break;
970
case 0x7c: asus_map_key_clear(KEY_MICMUTE); break;
971
case 0x82: asus_map_key_clear(KEY_CAMERA); break;
972
case 0x88: asus_map_key_clear(KEY_RFKILL); break;
973
case 0xb5: asus_map_key_clear(KEY_CALC); break;
974
case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break;
975
case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break;
976
case 0xc7: asus_map_key_clear(KEY_KBDILLUMTOGGLE); break;
977
case 0x4e: asus_map_key_clear(KEY_FN_ESC); break;
978
case 0x7e: asus_map_key_clear(KEY_EMOJI_PICKER); break;
979
980
case 0x8b: asus_map_key_clear(KEY_PROG1); break; /* ProArt Creator Hub key */
981
case 0x6b: asus_map_key_clear(KEY_F21); break; /* ASUS touchpad toggle */
982
case 0x38: asus_map_key_clear(KEY_PROG1); break; /* ROG key */
983
case 0xba: asus_map_key_clear(KEY_PROG2); break; /* Fn+C ASUS Splendid */
984
case 0x5c: asus_map_key_clear(KEY_PROG3); break; /* Fn+Space Power4Gear */
985
case 0x99: asus_map_key_clear(KEY_PROG4); break; /* Fn+F5 "fan" symbol */
986
case 0xae: asus_map_key_clear(KEY_PROG4); break; /* Fn+F5 "fan" symbol */
987
case 0x92: asus_map_key_clear(KEY_CALC); break; /* Fn+Ret "Calc" symbol */
988
case 0xb2: asus_map_key_clear(KEY_PROG2); break; /* Fn+Left previous aura */
989
case 0xb3: asus_map_key_clear(KEY_PROG3); break; /* Fn+Left next aura */
990
case 0x6a: asus_map_key_clear(KEY_F13); break; /* Screenpad toggle */
991
case 0x4b: asus_map_key_clear(KEY_F14); break; /* Arrows/Pg-Up/Dn toggle */
992
case 0xa5: asus_map_key_clear(KEY_F15); break; /* ROG Ally left back */
993
case 0xa6: asus_map_key_clear(KEY_F16); break; /* ROG Ally QAM button */
994
case 0xa7: asus_map_key_clear(KEY_F17); break; /* ROG Ally ROG long-press */
995
case 0xa8: asus_map_key_clear(KEY_F18); break; /* ROG Ally ROG long-press-release */
996
997
default:
998
/* ASUS lazily declares 256 usages, ignore the rest,
999
* as some make the keyboard appear as a pointer device. */
1000
return -1;
1001
}
1002
1003
/*
1004
* Check and enable backlight only on devices with UsagePage ==
1005
* 0xff31 to avoid initializing the keyboard firmware multiple
1006
* times on devices with multiple HID descriptors but same
1007
* PID/VID.
1008
*/
1009
if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
1010
drvdata->enable_backlight = true;
1011
1012
set_bit(EV_REP, hi->input->evbit);
1013
return 1;
1014
}
1015
1016
if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
1017
switch (usage->hid & HID_USAGE) {
1018
case 0xff01: asus_map_key_clear(BTN_1); break;
1019
case 0xff02: asus_map_key_clear(BTN_2); break;
1020
case 0xff03: asus_map_key_clear(BTN_3); break;
1021
case 0xff04: asus_map_key_clear(BTN_4); break;
1022
case 0xff05: asus_map_key_clear(BTN_5); break;
1023
case 0xff06: asus_map_key_clear(BTN_6); break;
1024
case 0xff07: asus_map_key_clear(BTN_7); break;
1025
case 0xff08: asus_map_key_clear(BTN_8); break;
1026
case 0xff09: asus_map_key_clear(BTN_9); break;
1027
case 0xff0a: asus_map_key_clear(BTN_A); break;
1028
case 0xff0b: asus_map_key_clear(BTN_B); break;
1029
case 0x00f1: asus_map_key_clear(KEY_WLAN); break;
1030
case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break;
1031
case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break;
1032
case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break;
1033
case 0x00f7: asus_map_key_clear(KEY_CAMERA); break;
1034
case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
1035
default:
1036
return 0;
1037
}
1038
1039
set_bit(EV_REP, hi->input->evbit);
1040
return 1;
1041
}
1042
1043
if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
1044
(usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
1045
switch (usage->hid & HID_USAGE) {
1046
case 0xe2: /* Mute */
1047
case 0xe9: /* Volume up */
1048
case 0xea: /* Volume down */
1049
return 0;
1050
default:
1051
/* Ignore dummy Consumer usages which make the
1052
* keyboard incorrectly appear as a pointer device.
1053
*/
1054
return -1;
1055
}
1056
}
1057
1058
/*
1059
* The mute button is broken and only sends press events, we
1060
* deal with this in our raw_event handler, so do not map it.
1061
*/
1062
if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
1063
usage->hid == (HID_UP_CONSUMER | 0xe2)) {
1064
input_set_capability(hi->input, EV_KEY, KEY_MUTE);
1065
return -1;
1066
}
1067
1068
return 0;
1069
}
1070
1071
static int asus_start_multitouch(struct hid_device *hdev)
1072
{
1073
int ret;
1074
static const unsigned char buf[] = {
1075
FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
1076
};
1077
unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
1078
1079
if (!dmabuf) {
1080
ret = -ENOMEM;
1081
hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
1082
return ret;
1083
}
1084
1085
ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
1086
HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
1087
1088
kfree(dmabuf);
1089
1090
if (ret != sizeof(buf)) {
1091
hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
1092
return ret;
1093
}
1094
1095
return 0;
1096
}
1097
1098
static int __maybe_unused asus_resume(struct hid_device *hdev) {
1099
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1100
int ret = 0;
1101
1102
if (drvdata->kbd_backlight) {
1103
const u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4,
1104
drvdata->kbd_backlight->cdev.brightness };
1105
ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
1106
if (ret < 0) {
1107
hid_err(hdev, "Asus failed to set keyboard backlight: %d\n", ret);
1108
goto asus_resume_err;
1109
}
1110
}
1111
1112
asus_resume_err:
1113
return ret;
1114
}
1115
1116
static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
1117
{
1118
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1119
1120
if (drvdata->tp)
1121
return asus_start_multitouch(hdev);
1122
1123
return 0;
1124
}
1125
1126
static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
1127
{
1128
int ret;
1129
struct asus_drvdata *drvdata;
1130
1131
drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1132
if (drvdata == NULL) {
1133
hid_err(hdev, "Can't alloc Asus descriptor\n");
1134
return -ENOMEM;
1135
}
1136
1137
hid_set_drvdata(hdev, drvdata);
1138
1139
drvdata->quirks = id->driver_data;
1140
1141
/*
1142
* T90CHI's keyboard dock returns same ID values as T100CHI's dock.
1143
* Thus, identify T90CHI dock with product name string.
1144
*/
1145
if (strstr(hdev->name, "T90CHI")) {
1146
drvdata->quirks &= ~QUIRK_T100CHI;
1147
drvdata->quirks |= QUIRK_T90CHI;
1148
}
1149
1150
if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
1151
drvdata->tp = &asus_i2c_tp;
1152
1153
if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && hid_is_usb(hdev)) {
1154
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1155
1156
if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
1157
drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
1158
/*
1159
* The T100HA uses the same USB-ids as the T100TAF and
1160
* the T200TA uses the same USB-ids as the T100TA, while
1161
* both have different max x/y values as the T100TA[F].
1162
*/
1163
if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
1164
drvdata->tp = &asus_t100ha_tp;
1165
else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
1166
drvdata->tp = &asus_t200ta_tp;
1167
else
1168
drvdata->tp = &asus_t100ta_tp;
1169
}
1170
}
1171
1172
if (drvdata->quirks & QUIRK_T100CHI) {
1173
/*
1174
* All functionality is on a single HID interface and for
1175
* userspace the touchpad must be a separate input_dev.
1176
*/
1177
hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1178
drvdata->tp = &asus_t100chi_tp;
1179
}
1180
1181
if ((drvdata->quirks & QUIRK_MEDION_E1239T) && hid_is_usb(hdev)) {
1182
struct usb_host_interface *alt =
1183
to_usb_interface(hdev->dev.parent)->altsetting;
1184
1185
if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
1186
/* For separate input-devs for tp and tp toggle key */
1187
hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1188
drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
1189
drvdata->tp = &medion_e1239t_tp;
1190
}
1191
}
1192
1193
if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
1194
hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1195
1196
drvdata->hdev = hdev;
1197
1198
if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1199
ret = asus_battery_probe(hdev);
1200
if (ret) {
1201
hid_err(hdev,
1202
"Asus hid battery_probe failed: %d\n", ret);
1203
return ret;
1204
}
1205
}
1206
1207
ret = hid_parse(hdev);
1208
if (ret) {
1209
hid_err(hdev, "Asus hid parse failed: %d\n", ret);
1210
return ret;
1211
}
1212
1213
ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1214
if (ret) {
1215
hid_err(hdev, "Asus hw start failed: %d\n", ret);
1216
return ret;
1217
}
1218
1219
/*
1220
* Check that input registration succeeded. Checking that
1221
* HID_CLAIMED_INPUT is set prevents a UAF when all input devices
1222
* were freed during registration due to no usages being mapped,
1223
* leaving drvdata->input pointing to freed memory.
1224
*/
1225
if (!drvdata->input || !(hdev->claimed & HID_CLAIMED_INPUT)) {
1226
hid_err(hdev, "Asus input not registered\n");
1227
ret = -ENOMEM;
1228
goto err_stop_hw;
1229
}
1230
1231
if (drvdata->tp) {
1232
drvdata->input->name = "Asus TouchPad";
1233
} else {
1234
drvdata->input->name = "Asus Keyboard";
1235
}
1236
1237
if (drvdata->tp) {
1238
ret = asus_start_multitouch(hdev);
1239
if (ret)
1240
goto err_stop_hw;
1241
}
1242
1243
return 0;
1244
err_stop_hw:
1245
hid_hw_stop(hdev);
1246
return ret;
1247
}
1248
1249
static void asus_remove(struct hid_device *hdev)
1250
{
1251
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1252
unsigned long flags;
1253
1254
if (drvdata->kbd_backlight) {
1255
spin_lock_irqsave(&drvdata->kbd_backlight->lock, flags);
1256
drvdata->kbd_backlight->removed = true;
1257
spin_unlock_irqrestore(&drvdata->kbd_backlight->lock, flags);
1258
1259
cancel_work_sync(&drvdata->kbd_backlight->work);
1260
}
1261
1262
hid_hw_stop(hdev);
1263
}
1264
1265
static const __u8 asus_g752_fixed_rdesc[] = {
1266
0x19, 0x00, /* Usage Minimum (0x00) */
1267
0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */
1268
};
1269
1270
static const __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1271
unsigned int *rsize)
1272
{
1273
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1274
1275
if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
1276
*rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
1277
hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
1278
rdesc[55] = 0xdd;
1279
}
1280
/* For the T100TA/T200TA keyboard dock */
1281
if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
1282
(*rsize == 76 || *rsize == 101) &&
1283
rdesc[73] == 0x81 && rdesc[74] == 0x01) {
1284
hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
1285
rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
1286
}
1287
/* For the T100CHI/T90CHI keyboard dock */
1288
if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1289
int rsize_orig;
1290
int offs;
1291
1292
if (drvdata->quirks & QUIRK_T100CHI) {
1293
rsize_orig = 403;
1294
offs = 388;
1295
} else {
1296
rsize_orig = 306;
1297
offs = 291;
1298
}
1299
1300
/*
1301
* Change Usage (76h) to Usage Minimum (00h), Usage Maximum
1302
* (FFh) and clear the flags in the Input() byte.
1303
* Note the descriptor has a bogus 0 byte at the end so we
1304
* only need 1 extra byte.
1305
*/
1306
if (*rsize == rsize_orig &&
1307
rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
1308
*rsize = rsize_orig + 1;
1309
rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
1310
if (!rdesc)
1311
return NULL;
1312
1313
hid_info(hdev, "Fixing up %s keyb report descriptor\n",
1314
drvdata->quirks & QUIRK_T100CHI ?
1315
"T100CHI" : "T90CHI");
1316
memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1317
rdesc[offs] = 0x19;
1318
rdesc[offs + 1] = 0x00;
1319
rdesc[offs + 2] = 0x29;
1320
rdesc[offs + 3] = 0xff;
1321
rdesc[offs + 14] = 0x00;
1322
}
1323
}
1324
1325
if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1326
*rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1327
/* report is missing usage minimum and maximum */
1328
__u8 *new_rdesc;
1329
size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1330
1331
new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1332
if (new_rdesc == NULL)
1333
return rdesc;
1334
1335
hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1336
/* copy the valid part */
1337
memcpy(new_rdesc, rdesc, 61);
1338
/* insert missing part */
1339
memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1340
/* copy remaining data */
1341
memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1342
1343
*rsize = new_size;
1344
rdesc = new_rdesc;
1345
}
1346
1347
if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD &&
1348
*rsize == 331 && rdesc[190] == 0x85 && rdesc[191] == 0x5a &&
1349
rdesc[204] == 0x95 && rdesc[205] == 0x05) {
1350
hid_info(hdev, "Fixing up Asus N-KEY keyb report descriptor\n");
1351
rdesc[205] = 0x01;
1352
}
1353
1354
/* match many more n-key devices */
1355
if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD && *rsize > 15) {
1356
for (int i = 0; i < *rsize - 15; i++) {
1357
/* offset to the count from 0x5a report part always 14 */
1358
if (rdesc[i] == 0x85 && rdesc[i + 1] == 0x5a &&
1359
rdesc[i + 14] == 0x95 && rdesc[i + 15] == 0x05) {
1360
hid_info(hdev, "Fixing up Asus N-Key report descriptor\n");
1361
rdesc[i + 15] = 0x01;
1362
break;
1363
}
1364
}
1365
}
1366
1367
return rdesc;
1368
}
1369
1370
static const struct hid_device_id asus_devices[] = {
1371
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1372
USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
1373
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1374
USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1375
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1376
USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1377
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1378
USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
1379
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1380
USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1381
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1382
USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1383
QUIRK_USE_KBD_BACKLIGHT },
1384
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1385
USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD),
1386
QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1387
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1388
USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2),
1389
QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1390
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1391
USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD3),
1392
QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1393
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1394
USB_DEVICE_ID_ASUSTEK_ROG_Z13_LIGHTBAR),
1395
QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1396
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1397
USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY),
1398
QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD | QUIRK_ROG_ALLY_XPAD},
1399
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1400
USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X),
1401
QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD | QUIRK_ROG_ALLY_XPAD },
1402
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1403
USB_DEVICE_ID_ASUSTEK_ROG_CLAYMORE_II_KEYBOARD),
1404
QUIRK_ROG_CLAYMORE_II_KEYBOARD },
1405
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1406
USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1407
QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1408
{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1409
USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
1410
QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1411
{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
1412
{ HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
1413
{ HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
1414
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1415
USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1416
{ HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
1417
QUIRK_MEDION_E1239T },
1418
/*
1419
* Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1420
* part, while letting hid-multitouch.c handle the touchpad.
1421
*/
1422
{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1423
USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
1424
{ }
1425
};
1426
MODULE_DEVICE_TABLE(hid, asus_devices);
1427
1428
static struct hid_driver asus_driver = {
1429
.name = "asus",
1430
.id_table = asus_devices,
1431
.report_fixup = asus_report_fixup,
1432
.probe = asus_probe,
1433
.remove = asus_remove,
1434
.input_mapping = asus_input_mapping,
1435
.input_configured = asus_input_configured,
1436
#ifdef CONFIG_PM
1437
.reset_resume = asus_reset_resume,
1438
.resume = asus_resume,
1439
#endif
1440
.event = asus_event,
1441
.raw_event = asus_raw_event
1442
};
1443
module_hid_driver(asus_driver);
1444
1445
MODULE_IMPORT_NS("ASUS_WMI");
1446
MODULE_LICENSE("GPL");
1447
1448