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