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/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 34 #include "hid-ids.h" 35 36 MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>"); 37 MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>"); 38 MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>"); 39 MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>"); 40 MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad"); 41 42 #define T100_TPAD_INTF 2 43 #define MEDION_E1239T_TPAD_INTF 1 44 45 #define E1239T_TP_TOGGLE_REPORT_ID 0x05 46 #define T100CHI_MOUSE_REPORT_ID 0x06 47 #define FEATURE_REPORT_ID 0x0d 48 #define INPUT_REPORT_ID 0x5d 49 #define FEATURE_KBD_REPORT_ID 0x5a 50 #define FEATURE_KBD_REPORT_SIZE 16 51 52 #define SUPPORT_KBD_BACKLIGHT BIT(0) 53 54 #define MAX_TOUCH_MAJOR 8 55 #define MAX_PRESSURE 128 56 57 #define BTN_LEFT_MASK 0x01 58 #define CONTACT_TOOL_TYPE_MASK 0x80 59 #define CONTACT_X_MSB_MASK 0xf0 60 #define CONTACT_Y_MSB_MASK 0x0f 61 #define CONTACT_TOUCH_MAJOR_MASK 0x07 62 #define CONTACT_PRESSURE_MASK 0x7f 63 64 #define BATTERY_REPORT_ID (0x03) 65 #define BATTERY_REPORT_SIZE (1 + 8) 66 #define BATTERY_LEVEL_MAX ((u8)255) 67 #define BATTERY_STAT_DISCONNECT (0) 68 #define BATTERY_STAT_CHARGING (1) 69 #define BATTERY_STAT_FULL (2) 70 71 #define QUIRK_FIX_NOTEBOOK_REPORT BIT(0) 72 #define QUIRK_NO_INIT_REPORTS BIT(1) 73 #define QUIRK_SKIP_INPUT_MAPPING BIT(2) 74 #define QUIRK_IS_MULTITOUCH BIT(3) 75 #define QUIRK_NO_CONSUMER_USAGES BIT(4) 76 #define QUIRK_USE_KBD_BACKLIGHT BIT(5) 77 #define QUIRK_T100_KEYBOARD BIT(6) 78 #define QUIRK_T100CHI BIT(7) 79 #define QUIRK_G752_KEYBOARD BIT(8) 80 #define QUIRK_T101HA_DOCK BIT(9) 81 #define QUIRK_T90CHI BIT(10) 82 #define QUIRK_MEDION_E1239T BIT(11) 83 84 #define I2C_KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \ 85 QUIRK_NO_INIT_REPORTS | \ 86 QUIRK_NO_CONSUMER_USAGES) 87 #define I2C_TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \ 88 QUIRK_SKIP_INPUT_MAPPING | \ 89 QUIRK_IS_MULTITOUCH) 90 91 #define TRKID_SGN ((TRKID_MAX + 1) >> 1) 92 93 struct asus_kbd_leds { 94 struct led_classdev cdev; 95 struct hid_device *hdev; 96 struct work_struct work; 97 unsigned int brightness; 98 bool removed; 99 }; 100 101 struct asus_touchpad_info { 102 int max_x; 103 int max_y; 104 int res_x; 105 int res_y; 106 int contact_size; 107 int max_contacts; 108 int report_size; 109 }; 110 111 struct asus_drvdata { 112 unsigned long quirks; 113 struct hid_device *hdev; 114 struct input_dev *input; 115 struct input_dev *tp_kbd_input; 116 struct asus_kbd_leds *kbd_backlight; 117 const struct asus_touchpad_info *tp; 118 bool enable_backlight; 119 struct power_supply *battery; 120 struct power_supply_desc battery_desc; 121 int battery_capacity; 122 int battery_stat; 123 bool battery_in_query; 124 unsigned long battery_next_query; 125 }; 126 127 static int asus_report_battery(struct asus_drvdata *, u8 *, int); 128 129 static const struct asus_touchpad_info asus_i2c_tp = { 130 .max_x = 2794, 131 .max_y = 1758, 132 .contact_size = 5, 133 .max_contacts = 5, 134 .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */, 135 }; 136 137 static const struct asus_touchpad_info asus_t100ta_tp = { 138 .max_x = 2240, 139 .max_y = 1120, 140 .res_x = 30, /* units/mm */ 141 .res_y = 27, /* units/mm */ 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_t100ha_tp = { 148 .max_x = 2640, 149 .max_y = 1320, 150 .res_x = 30, /* units/mm */ 151 .res_y = 29, /* 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_t200ta_tp = { 158 .max_x = 3120, 159 .max_y = 1716, 160 .res_x = 30, /* units/mm */ 161 .res_y = 28, /* 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_t100chi_tp = { 168 .max_x = 2640, 169 .max_y = 1320, 170 .res_x = 31, /* units/mm */ 171 .res_y = 29, /* units/mm */ 172 .contact_size = 3, 173 .max_contacts = 4, 174 .report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */, 175 }; 176 177 static const struct asus_touchpad_info medion_e1239t_tp = { 178 .max_x = 2640, 179 .max_y = 1380, 180 .res_x = 29, /* units/mm */ 181 .res_y = 28, /* units/mm */ 182 .contact_size = 5, 183 .max_contacts = 5, 184 .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */, 185 }; 186 187 static void asus_report_contact_down(struct asus_drvdata *drvdat, 188 int toolType, u8 *data) 189 { 190 struct input_dev *input = drvdat->input; 191 int touch_major, pressure, x, y; 192 193 x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1]; 194 y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]); 195 196 input_report_abs(input, ABS_MT_POSITION_X, x); 197 input_report_abs(input, ABS_MT_POSITION_Y, y); 198 199 if (drvdat->tp->contact_size < 5) 200 return; 201 202 if (toolType == MT_TOOL_PALM) { 203 touch_major = MAX_TOUCH_MAJOR; 204 pressure = MAX_PRESSURE; 205 } else { 206 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK; 207 pressure = data[4] & CONTACT_PRESSURE_MASK; 208 } 209 210 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major); 211 input_report_abs(input, ABS_MT_PRESSURE, pressure); 212 } 213 214 /* Required for Synaptics Palm Detection */ 215 static void asus_report_tool_width(struct asus_drvdata *drvdat) 216 { 217 struct input_mt *mt = drvdat->input->mt; 218 struct input_mt_slot *oldest; 219 int oldid, count, i; 220 221 if (drvdat->tp->contact_size < 5) 222 return; 223 224 oldest = NULL; 225 oldid = mt->trkid; 226 count = 0; 227 228 for (i = 0; i < mt->num_slots; ++i) { 229 struct input_mt_slot *ps = &mt->slots[i]; 230 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID); 231 232 if (id < 0) 233 continue; 234 if ((id - oldid) & TRKID_SGN) { 235 oldest = ps; 236 oldid = id; 237 } 238 count++; 239 } 240 241 if (oldest) { 242 input_report_abs(drvdat->input, ABS_TOOL_WIDTH, 243 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR)); 244 } 245 } 246 247 static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size) 248 { 249 int i, toolType = MT_TOOL_FINGER; 250 u8 *contactData = data + 2; 251 252 if (size != drvdat->tp->report_size) 253 return 0; 254 255 for (i = 0; i < drvdat->tp->max_contacts; i++) { 256 bool down = !!(data[1] & BIT(i+3)); 257 258 if (drvdat->tp->contact_size >= 5) 259 toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ? 260 MT_TOOL_PALM : MT_TOOL_FINGER; 261 262 input_mt_slot(drvdat->input, i); 263 input_mt_report_slot_state(drvdat->input, toolType, down); 264 265 if (down) { 266 asus_report_contact_down(drvdat, toolType, contactData); 267 contactData += drvdat->tp->contact_size; 268 } 269 } 270 271 input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK); 272 asus_report_tool_width(drvdat); 273 274 input_mt_sync_frame(drvdat->input); 275 input_sync(drvdat->input); 276 277 return 1; 278 } 279 280 static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size) 281 { 282 if (size != 3) 283 return 0; 284 285 /* Handle broken mute key which only sends press events */ 286 if (!drvdat->tp && 287 data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) { 288 input_report_key(drvdat->input, KEY_MUTE, 1); 289 input_sync(drvdat->input); 290 input_report_key(drvdat->input, KEY_MUTE, 0); 291 input_sync(drvdat->input); 292 return 1; 293 } 294 295 /* Handle custom touchpad toggle key which only sends press events */ 296 if (drvdat->tp_kbd_input && 297 data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) { 298 input_report_key(drvdat->tp_kbd_input, KEY_F21, 1); 299 input_sync(drvdat->tp_kbd_input); 300 input_report_key(drvdat->tp_kbd_input, KEY_F21, 0); 301 input_sync(drvdat->tp_kbd_input); 302 return 1; 303 } 304 305 return 0; 306 } 307 308 static int asus_event(struct hid_device *hdev, struct hid_field *field, 309 struct hid_usage *usage, __s32 value) 310 { 311 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 && 312 (usage->hid & HID_USAGE) != 0x00 && 313 (usage->hid & HID_USAGE) != 0xff && !usage->type) { 314 hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n", 315 usage->hid & HID_USAGE); 316 } 317 318 return 0; 319 } 320 321 static int asus_raw_event(struct hid_device *hdev, 322 struct hid_report *report, u8 *data, int size) 323 { 324 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 325 326 if (drvdata->battery && data[0] == BATTERY_REPORT_ID) 327 return asus_report_battery(drvdata, data, size); 328 329 if (drvdata->tp && data[0] == INPUT_REPORT_ID) 330 return asus_report_input(drvdata, data, size); 331 332 if (drvdata->quirks & QUIRK_MEDION_E1239T) 333 return asus_e1239t_event(drvdata, data, size); 334 335 return 0; 336 } 337 338 static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size) 339 { 340 unsigned char *dmabuf; 341 int ret; 342 343 dmabuf = kmemdup(buf, buf_size, GFP_KERNEL); 344 if (!dmabuf) 345 return -ENOMEM; 346 347 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, dmabuf, 348 buf_size, HID_FEATURE_REPORT, 349 HID_REQ_SET_REPORT); 350 kfree(dmabuf); 351 352 return ret; 353 } 354 355 static int asus_kbd_init(struct hid_device *hdev) 356 { 357 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54, 358 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 }; 359 int ret; 360 361 ret = asus_kbd_set_report(hdev, buf, sizeof(buf)); 362 if (ret < 0) 363 hid_err(hdev, "Asus failed to send init command: %d\n", ret); 364 365 return ret; 366 } 367 368 static int asus_kbd_get_functions(struct hid_device *hdev, 369 unsigned char *kbd_func) 370 { 371 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 }; 372 u8 *readbuf; 373 int ret; 374 375 ret = asus_kbd_set_report(hdev, buf, sizeof(buf)); 376 if (ret < 0) { 377 hid_err(hdev, "Asus failed to send configuration command: %d\n", ret); 378 return ret; 379 } 380 381 readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL); 382 if (!readbuf) 383 return -ENOMEM; 384 385 ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf, 386 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT, 387 HID_REQ_GET_REPORT); 388 if (ret < 0) { 389 hid_err(hdev, "Asus failed to request functions: %d\n", ret); 390 kfree(readbuf); 391 return ret; 392 } 393 394 *kbd_func = readbuf[6]; 395 396 kfree(readbuf); 397 return ret; 398 } 399 400 static void asus_kbd_backlight_set(struct led_classdev *led_cdev, 401 enum led_brightness brightness) 402 { 403 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds, 404 cdev); 405 if (led->brightness == brightness) 406 return; 407 408 led->brightness = brightness; 409 schedule_work(&led->work); 410 } 411 412 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev) 413 { 414 struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds, 415 cdev); 416 417 return led->brightness; 418 } 419 420 static void asus_kbd_backlight_work(struct work_struct *work) 421 { 422 struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work); 423 u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 }; 424 int ret; 425 426 if (led->removed) 427 return; 428 429 buf[4] = led->brightness; 430 431 ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf)); 432 if (ret < 0) 433 hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret); 434 } 435 436 /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes 437 * precedence. We only activate HID-based backlight control when the 438 * WMI control is not available. 439 */ 440 static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev) 441 { 442 u32 value; 443 int ret; 444 445 if (!IS_ENABLED(CONFIG_ASUS_WMI)) 446 return false; 447 448 ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 449 ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value); 450 hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value); 451 if (ret) 452 return false; 453 454 return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT); 455 } 456 457 static int asus_kbd_register_leds(struct hid_device *hdev) 458 { 459 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 460 unsigned char kbd_func; 461 int ret; 462 463 /* Initialize keyboard */ 464 ret = asus_kbd_init(hdev); 465 if (ret < 0) 466 return ret; 467 468 /* Get keyboard functions */ 469 ret = asus_kbd_get_functions(hdev, &kbd_func); 470 if (ret < 0) 471 return ret; 472 473 /* Check for backlight support */ 474 if (!(kbd_func & SUPPORT_KBD_BACKLIGHT)) 475 return -ENODEV; 476 477 drvdata->kbd_backlight = devm_kzalloc(&hdev->dev, 478 sizeof(struct asus_kbd_leds), 479 GFP_KERNEL); 480 if (!drvdata->kbd_backlight) 481 return -ENOMEM; 482 483 drvdata->kbd_backlight->removed = false; 484 drvdata->kbd_backlight->brightness = 0; 485 drvdata->kbd_backlight->hdev = hdev; 486 drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight"; 487 drvdata->kbd_backlight->cdev.max_brightness = 3; 488 drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set; 489 drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get; 490 INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work); 491 492 ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev); 493 if (ret < 0) { 494 /* No need to have this still around */ 495 devm_kfree(&hdev->dev, drvdata->kbd_backlight); 496 } 497 498 return ret; 499 } 500 501 /* 502 * [0] REPORT_ID (same value defined in report descriptor) 503 * [1] rest battery level. range [0..255] 504 * [2]..[7] Bluetooth hardware address (MAC address) 505 * [8] charging status 506 * = 0 : AC offline / discharging 507 * = 1 : AC online / charging 508 * = 2 : AC online / fully charged 509 */ 510 static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size) 511 { 512 u8 sts; 513 u8 lvl; 514 int val; 515 516 lvl = data[1]; 517 sts = data[8]; 518 519 drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX; 520 521 switch (sts) { 522 case BATTERY_STAT_CHARGING: 523 val = POWER_SUPPLY_STATUS_CHARGING; 524 break; 525 case BATTERY_STAT_FULL: 526 val = POWER_SUPPLY_STATUS_FULL; 527 break; 528 case BATTERY_STAT_DISCONNECT: 529 default: 530 val = POWER_SUPPLY_STATUS_DISCHARGING; 531 break; 532 } 533 drvdata->battery_stat = val; 534 535 return 0; 536 } 537 538 static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size) 539 { 540 /* notify only the autonomous event by device */ 541 if ((drvdata->battery_in_query == false) && 542 (size == BATTERY_REPORT_SIZE)) 543 power_supply_changed(drvdata->battery); 544 545 return 0; 546 } 547 548 static int asus_battery_query(struct asus_drvdata *drvdata) 549 { 550 u8 *buf; 551 int ret = 0; 552 553 buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL); 554 if (!buf) 555 return -ENOMEM; 556 557 drvdata->battery_in_query = true; 558 ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID, 559 buf, BATTERY_REPORT_SIZE, 560 HID_INPUT_REPORT, HID_REQ_GET_REPORT); 561 drvdata->battery_in_query = false; 562 if (ret == BATTERY_REPORT_SIZE) 563 ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE); 564 else 565 ret = -ENODATA; 566 567 kfree(buf); 568 569 return ret; 570 } 571 572 static enum power_supply_property asus_battery_props[] = { 573 POWER_SUPPLY_PROP_STATUS, 574 POWER_SUPPLY_PROP_PRESENT, 575 POWER_SUPPLY_PROP_CAPACITY, 576 POWER_SUPPLY_PROP_SCOPE, 577 POWER_SUPPLY_PROP_MODEL_NAME, 578 }; 579 580 #define QUERY_MIN_INTERVAL (60 * HZ) /* 60[sec] */ 581 582 static int asus_battery_get_property(struct power_supply *psy, 583 enum power_supply_property psp, 584 union power_supply_propval *val) 585 { 586 struct asus_drvdata *drvdata = power_supply_get_drvdata(psy); 587 int ret = 0; 588 589 switch (psp) { 590 case POWER_SUPPLY_PROP_STATUS: 591 case POWER_SUPPLY_PROP_CAPACITY: 592 if (time_before(drvdata->battery_next_query, jiffies)) { 593 drvdata->battery_next_query = 594 jiffies + QUERY_MIN_INTERVAL; 595 ret = asus_battery_query(drvdata); 596 if (ret) 597 return ret; 598 } 599 if (psp == POWER_SUPPLY_PROP_STATUS) 600 val->intval = drvdata->battery_stat; 601 else 602 val->intval = drvdata->battery_capacity; 603 break; 604 case POWER_SUPPLY_PROP_PRESENT: 605 val->intval = 1; 606 break; 607 case POWER_SUPPLY_PROP_SCOPE: 608 val->intval = POWER_SUPPLY_SCOPE_DEVICE; 609 break; 610 case POWER_SUPPLY_PROP_MODEL_NAME: 611 val->strval = drvdata->hdev->name; 612 break; 613 default: 614 ret = -EINVAL; 615 break; 616 } 617 618 return ret; 619 } 620 621 static int asus_battery_probe(struct hid_device *hdev) 622 { 623 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 624 struct power_supply_config pscfg = { .drv_data = drvdata }; 625 int ret = 0; 626 627 drvdata->battery_capacity = 0; 628 drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN; 629 drvdata->battery_in_query = false; 630 631 drvdata->battery_desc.properties = asus_battery_props; 632 drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props); 633 drvdata->battery_desc.get_property = asus_battery_get_property; 634 drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; 635 drvdata->battery_desc.use_for_apm = 0; 636 drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 637 "asus-keyboard-%s-battery", 638 strlen(hdev->uniq) ? 639 hdev->uniq : dev_name(&hdev->dev)); 640 if (!drvdata->battery_desc.name) 641 return -ENOMEM; 642 643 drvdata->battery_next_query = jiffies; 644 645 drvdata->battery = devm_power_supply_register(&hdev->dev, 646 &(drvdata->battery_desc), &pscfg); 647 if (IS_ERR(drvdata->battery)) { 648 ret = PTR_ERR(drvdata->battery); 649 drvdata->battery = NULL; 650 hid_err(hdev, "Unable to register battery device\n"); 651 return ret; 652 } 653 654 power_supply_powers(drvdata->battery, &hdev->dev); 655 656 return ret; 657 } 658 659 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi) 660 { 661 struct input_dev *input = hi->input; 662 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 663 664 /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */ 665 if (drvdata->quirks & QUIRK_T100CHI && 666 hi->report->id != T100CHI_MOUSE_REPORT_ID) 667 return 0; 668 669 /* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */ 670 if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) { 671 switch (hi->report->id) { 672 case E1239T_TP_TOGGLE_REPORT_ID: 673 input_set_capability(input, EV_KEY, KEY_F21); 674 input->name = "Asus Touchpad Keys"; 675 drvdata->tp_kbd_input = input; 676 return 0; 677 case INPUT_REPORT_ID: 678 break; /* Touchpad report, handled below */ 679 default: 680 return 0; /* Ignore other reports */ 681 } 682 } 683 684 if (drvdata->tp) { 685 int ret; 686 687 input_set_abs_params(input, ABS_MT_POSITION_X, 0, 688 drvdata->tp->max_x, 0, 0); 689 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 690 drvdata->tp->max_y, 0, 0); 691 input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x); 692 input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y); 693 694 if (drvdata->tp->contact_size >= 5) { 695 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, 696 MAX_TOUCH_MAJOR, 0, 0); 697 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 698 MAX_TOUCH_MAJOR, 0, 0); 699 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 700 MAX_PRESSURE, 0, 0); 701 } 702 703 __set_bit(BTN_LEFT, input->keybit); 704 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); 705 706 ret = input_mt_init_slots(input, drvdata->tp->max_contacts, 707 INPUT_MT_POINTER); 708 709 if (ret) { 710 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret); 711 return ret; 712 } 713 } 714 715 drvdata->input = input; 716 717 if (drvdata->enable_backlight && 718 !asus_kbd_wmi_led_control_present(hdev) && 719 asus_kbd_register_leds(hdev)) 720 hid_warn(hdev, "Failed to initialize backlight.\n"); 721 722 return 0; 723 } 724 725 #define asus_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \ 726 max, EV_KEY, (c)) 727 static int asus_input_mapping(struct hid_device *hdev, 728 struct hid_input *hi, struct hid_field *field, 729 struct hid_usage *usage, unsigned long **bit, 730 int *max) 731 { 732 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 733 734 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) { 735 /* Don't map anything from the HID report. 736 * We do it all manually in asus_input_configured 737 */ 738 return -1; 739 } 740 741 /* 742 * Ignore a bunch of bogus collections in the T100CHI descriptor. 743 * This avoids a bunch of non-functional hid_input devices getting 744 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT. 745 */ 746 if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) && 747 (field->application == (HID_UP_GENDESK | 0x0080) || 748 field->application == HID_GD_MOUSE || 749 usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) || 750 usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) || 751 usage->hid == (HID_UP_GENDEVCTRLS | 0x0026))) 752 return -1; 753 754 /* ASUS-specific keyboard hotkeys */ 755 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) { 756 switch (usage->hid & HID_USAGE) { 757 case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break; 758 case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP); break; 759 case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF); break; 760 case 0x6c: asus_map_key_clear(KEY_SLEEP); break; 761 case 0x7c: asus_map_key_clear(KEY_MICMUTE); break; 762 case 0x82: asus_map_key_clear(KEY_CAMERA); break; 763 case 0x88: asus_map_key_clear(KEY_RFKILL); break; 764 case 0xb5: asus_map_key_clear(KEY_CALC); break; 765 case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP); break; 766 case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN); break; 767 768 /* ASUS touchpad toggle */ 769 case 0x6b: asus_map_key_clear(KEY_F21); break; 770 771 /* ROG key */ 772 case 0x38: asus_map_key_clear(KEY_PROG1); break; 773 774 /* Fn+C ASUS Splendid */ 775 case 0xba: asus_map_key_clear(KEY_PROG2); break; 776 777 /* Fn+Space Power4Gear Hybrid */ 778 case 0x5c: asus_map_key_clear(KEY_PROG3); break; 779 780 /* Fn+F5 "fan" symbol on FX503VD */ 781 case 0x99: asus_map_key_clear(KEY_PROG4); break; 782 783 default: 784 /* ASUS lazily declares 256 usages, ignore the rest, 785 * as some make the keyboard appear as a pointer device. */ 786 return -1; 787 } 788 789 /* 790 * Check and enable backlight only on devices with UsagePage == 791 * 0xff31 to avoid initializing the keyboard firmware multiple 792 * times on devices with multiple HID descriptors but same 793 * PID/VID. 794 */ 795 if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) 796 drvdata->enable_backlight = true; 797 798 set_bit(EV_REP, hi->input->evbit); 799 return 1; 800 } 801 802 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) { 803 switch (usage->hid & HID_USAGE) { 804 case 0xff01: asus_map_key_clear(BTN_1); break; 805 case 0xff02: asus_map_key_clear(BTN_2); break; 806 case 0xff03: asus_map_key_clear(BTN_3); break; 807 case 0xff04: asus_map_key_clear(BTN_4); break; 808 case 0xff05: asus_map_key_clear(BTN_5); break; 809 case 0xff06: asus_map_key_clear(BTN_6); break; 810 case 0xff07: asus_map_key_clear(BTN_7); break; 811 case 0xff08: asus_map_key_clear(BTN_8); break; 812 case 0xff09: asus_map_key_clear(BTN_9); break; 813 case 0xff0a: asus_map_key_clear(BTN_A); break; 814 case 0xff0b: asus_map_key_clear(BTN_B); break; 815 case 0x00f1: asus_map_key_clear(KEY_WLAN); break; 816 case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN); break; 817 case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP); break; 818 case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF); break; 819 case 0x00f7: asus_map_key_clear(KEY_CAMERA); break; 820 case 0x00f8: asus_map_key_clear(KEY_PROG1); break; 821 default: 822 return 0; 823 } 824 825 set_bit(EV_REP, hi->input->evbit); 826 return 1; 827 } 828 829 if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES && 830 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) { 831 switch (usage->hid & HID_USAGE) { 832 case 0xe2: /* Mute */ 833 case 0xe9: /* Volume up */ 834 case 0xea: /* Volume down */ 835 return 0; 836 default: 837 /* Ignore dummy Consumer usages which make the 838 * keyboard incorrectly appear as a pointer device. 839 */ 840 return -1; 841 } 842 } 843 844 /* 845 * The mute button is broken and only sends press events, we 846 * deal with this in our raw_event handler, so do not map it. 847 */ 848 if ((drvdata->quirks & QUIRK_MEDION_E1239T) && 849 usage->hid == (HID_UP_CONSUMER | 0xe2)) { 850 input_set_capability(hi->input, EV_KEY, KEY_MUTE); 851 return -1; 852 } 853 854 return 0; 855 } 856 857 static int asus_start_multitouch(struct hid_device *hdev) 858 { 859 int ret; 860 static const unsigned char buf[] = { 861 FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00 862 }; 863 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL); 864 865 if (!dmabuf) { 866 ret = -ENOMEM; 867 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret); 868 return ret; 869 } 870 871 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf), 872 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 873 874 kfree(dmabuf); 875 876 if (ret != sizeof(buf)) { 877 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret); 878 return ret; 879 } 880 881 return 0; 882 } 883 884 static int __maybe_unused asus_reset_resume(struct hid_device *hdev) 885 { 886 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 887 888 if (drvdata->tp) 889 return asus_start_multitouch(hdev); 890 891 return 0; 892 } 893 894 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) 895 { 896 int ret; 897 struct asus_drvdata *drvdata; 898 899 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); 900 if (drvdata == NULL) { 901 hid_err(hdev, "Can't alloc Asus descriptor\n"); 902 return -ENOMEM; 903 } 904 905 hid_set_drvdata(hdev, drvdata); 906 907 drvdata->quirks = id->driver_data; 908 909 /* 910 * T90CHI's keyboard dock returns same ID values as T100CHI's dock. 911 * Thus, identify T90CHI dock with product name string. 912 */ 913 if (strstr(hdev->name, "T90CHI")) { 914 drvdata->quirks &= ~QUIRK_T100CHI; 915 drvdata->quirks |= QUIRK_T90CHI; 916 } 917 918 if (drvdata->quirks & QUIRK_IS_MULTITOUCH) 919 drvdata->tp = &asus_i2c_tp; 920 921 if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && 922 hid_is_using_ll_driver(hdev, &usb_hid_driver)) { 923 struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 924 925 if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) { 926 drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING; 927 /* 928 * The T100HA uses the same USB-ids as the T100TAF and 929 * the T200TA uses the same USB-ids as the T100TA, while 930 * both have different max x/y values as the T100TA[F]. 931 */ 932 if (dmi_match(DMI_PRODUCT_NAME, "T100HAN")) 933 drvdata->tp = &asus_t100ha_tp; 934 else if (dmi_match(DMI_PRODUCT_NAME, "T200TA")) 935 drvdata->tp = &asus_t200ta_tp; 936 else 937 drvdata->tp = &asus_t100ta_tp; 938 } 939 } 940 941 if (drvdata->quirks & QUIRK_T100CHI) { 942 /* 943 * All functionality is on a single HID interface and for 944 * userspace the touchpad must be a separate input_dev. 945 */ 946 hdev->quirks |= HID_QUIRK_MULTI_INPUT; 947 drvdata->tp = &asus_t100chi_tp; 948 } 949 950 if ((drvdata->quirks & QUIRK_MEDION_E1239T) && 951 hid_is_using_ll_driver(hdev, &usb_hid_driver)) { 952 struct usb_host_interface *alt = 953 to_usb_interface(hdev->dev.parent)->altsetting; 954 955 if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) { 956 /* For separate input-devs for tp and tp toggle key */ 957 hdev->quirks |= HID_QUIRK_MULTI_INPUT; 958 drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING; 959 drvdata->tp = &medion_e1239t_tp; 960 } 961 } 962 963 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS) 964 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; 965 966 drvdata->hdev = hdev; 967 968 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) { 969 ret = asus_battery_probe(hdev); 970 if (ret) { 971 hid_err(hdev, 972 "Asus hid battery_probe failed: %d\n", ret); 973 return ret; 974 } 975 } 976 977 ret = hid_parse(hdev); 978 if (ret) { 979 hid_err(hdev, "Asus hid parse failed: %d\n", ret); 980 return ret; 981 } 982 983 /* use hid-multitouch for T101HA touchpad */ 984 if (id->driver_data & QUIRK_T101HA_DOCK && 985 hdev->collection->usage == HID_GD_MOUSE) 986 return -ENODEV; 987 988 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 989 if (ret) { 990 hid_err(hdev, "Asus hw start failed: %d\n", ret); 991 return ret; 992 } 993 994 if (!drvdata->input) { 995 hid_err(hdev, "Asus input not registered\n"); 996 ret = -ENOMEM; 997 goto err_stop_hw; 998 } 999 1000 if (drvdata->tp) { 1001 drvdata->input->name = "Asus TouchPad"; 1002 } else { 1003 drvdata->input->name = "Asus Keyboard"; 1004 } 1005 1006 if (drvdata->tp) { 1007 ret = asus_start_multitouch(hdev); 1008 if (ret) 1009 goto err_stop_hw; 1010 } 1011 1012 return 0; 1013 err_stop_hw: 1014 hid_hw_stop(hdev); 1015 return ret; 1016 } 1017 1018 static void asus_remove(struct hid_device *hdev) 1019 { 1020 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 1021 1022 if (drvdata->kbd_backlight) { 1023 drvdata->kbd_backlight->removed = true; 1024 cancel_work_sync(&drvdata->kbd_backlight->work); 1025 } 1026 1027 hid_hw_stop(hdev); 1028 } 1029 1030 static const __u8 asus_g752_fixed_rdesc[] = { 1031 0x19, 0x00, /* Usage Minimum (0x00) */ 1032 0x2A, 0xFF, 0x00, /* Usage Maximum (0xFF) */ 1033 }; 1034 1035 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc, 1036 unsigned int *rsize) 1037 { 1038 struct asus_drvdata *drvdata = hid_get_drvdata(hdev); 1039 1040 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT && 1041 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) { 1042 hid_info(hdev, "Fixing up Asus notebook report descriptor\n"); 1043 rdesc[55] = 0xdd; 1044 } 1045 /* For the T100TA/T200TA keyboard dock */ 1046 if (drvdata->quirks & QUIRK_T100_KEYBOARD && 1047 (*rsize == 76 || *rsize == 101) && 1048 rdesc[73] == 0x81 && rdesc[74] == 0x01) { 1049 hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n"); 1050 rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT; 1051 } 1052 /* For the T100CHI/T90CHI keyboard dock */ 1053 if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) { 1054 int rsize_orig; 1055 int offs; 1056 1057 if (drvdata->quirks & QUIRK_T100CHI) { 1058 rsize_orig = 403; 1059 offs = 388; 1060 } else { 1061 rsize_orig = 306; 1062 offs = 291; 1063 } 1064 1065 /* 1066 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum 1067 * (FFh) and clear the flags in the Input() byte. 1068 * Note the descriptor has a bogus 0 byte at the end so we 1069 * only need 1 extra byte. 1070 */ 1071 if (*rsize == rsize_orig && 1072 rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) { 1073 *rsize = rsize_orig + 1; 1074 rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL); 1075 if (!rdesc) 1076 return NULL; 1077 1078 hid_info(hdev, "Fixing up %s keyb report descriptor\n", 1079 drvdata->quirks & QUIRK_T100CHI ? 1080 "T100CHI" : "T90CHI"); 1081 memmove(rdesc + offs + 4, rdesc + offs + 2, 12); 1082 rdesc[offs] = 0x19; 1083 rdesc[offs + 1] = 0x00; 1084 rdesc[offs + 2] = 0x29; 1085 rdesc[offs + 3] = 0xff; 1086 rdesc[offs + 14] = 0x00; 1087 } 1088 } 1089 1090 if (drvdata->quirks & QUIRK_G752_KEYBOARD && 1091 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) { 1092 /* report is missing usage mninum and maximum */ 1093 __u8 *new_rdesc; 1094 size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc); 1095 1096 new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL); 1097 if (new_rdesc == NULL) 1098 return rdesc; 1099 1100 hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n"); 1101 /* copy the valid part */ 1102 memcpy(new_rdesc, rdesc, 61); 1103 /* insert missing part */ 1104 memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc)); 1105 /* copy remaining data */ 1106 memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61); 1107 1108 *rsize = new_size; 1109 rdesc = new_rdesc; 1110 } 1111 1112 return rdesc; 1113 } 1114 1115 static const struct hid_device_id asus_devices[] = { 1116 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, 1117 USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS}, 1118 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, 1119 USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS }, 1120 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1121 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT }, 1122 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1123 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT }, 1124 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1125 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD }, 1126 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1127 USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD), 1128 QUIRK_USE_KBD_BACKLIGHT }, 1129 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1130 USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD), 1131 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES }, 1132 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1133 USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD), 1134 QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES }, 1135 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1136 USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD), QUIRK_T101HA_DOCK }, 1137 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) }, 1138 { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) }, 1139 { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) }, 1140 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK, 1141 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI }, 1142 { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T), 1143 QUIRK_MEDION_E1239T }, 1144 { } 1145 }; 1146 MODULE_DEVICE_TABLE(hid, asus_devices); 1147 1148 static struct hid_driver asus_driver = { 1149 .name = "asus", 1150 .id_table = asus_devices, 1151 .report_fixup = asus_report_fixup, 1152 .probe = asus_probe, 1153 .remove = asus_remove, 1154 .input_mapping = asus_input_mapping, 1155 .input_configured = asus_input_configured, 1156 #ifdef CONFIG_PM 1157 .reset_resume = asus_reset_resume, 1158 #endif 1159 .event = asus_event, 1160 .raw_event = asus_raw_event 1161 }; 1162 module_hid_driver(asus_driver); 1163 1164 MODULE_LICENSE("GPL"); 1165