1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * HID driver for Lenovo: 4 * - ThinkPad USB Keyboard with TrackPoint (tpkbd) 5 * - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd) 6 * - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd) 7 * - ThinkPad TrackPoint Keyboard II USB/Bluetooth (cptkbd/tpIIkbd) 8 * 9 * Copyright (c) 2012 Bernhard Seibold 10 * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk> 11 * 12 * Linux IBM/Lenovo Scrollpoint mouse driver: 13 * - IBM Scrollpoint III 14 * - IBM Scrollpoint Pro 15 * - IBM Scrollpoint Optical 16 * - IBM Scrollpoint Optical 800dpi 17 * - IBM Scrollpoint Optical 800dpi Pro 18 * - Lenovo Scrollpoint Optical 19 * 20 * Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com> 21 * Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com> 22 */ 23 24 /* 25 */ 26 27 #include <linux/module.h> 28 #include <linux/sysfs.h> 29 #include <linux/device.h> 30 #include <linux/hid.h> 31 #include <linux/input.h> 32 #include <linux/leds.h> 33 #include <linux/workqueue.h> 34 35 #include <linux/platform_profile.h> 36 37 #include "hid-ids.h" 38 39 /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */ 40 #define LENOVO_KEY_MICMUTE KEY_F20 41 42 /* HID raw events for ThinkPad X12 Tabs*/ 43 #define TP_X12_RAW_HOTKEY_FN_F4 0x00020003 44 #define TP_X12_RAW_HOTKEY_FN_F8 0x38001003 45 #define TP_X12_RAW_HOTKEY_FN_F10 0x00000803 46 #define TP_X12_RAW_HOTKEY_FN_F12 0x00000403 47 #define TP_X12_RAW_HOTKEY_FN_SPACE 0x18001003 48 49 struct lenovo_drvdata { 50 u8 led_report[3]; /* Must be first for proper alignment */ 51 int led_state; 52 struct mutex led_report_mutex; 53 struct led_classdev led_mute; 54 struct led_classdev led_micmute; 55 struct work_struct fn_lock_sync_work; 56 struct hid_device *hdev; 57 int press_to_select; 58 int dragging; 59 int release_to_select; 60 int select_right; 61 int sensitivity; 62 int press_speed; 63 /* 0: Up 64 * 1: Down (undecided) 65 * 2: Scrolling 66 */ 67 u8 middlebutton_state; 68 bool fn_lock; 69 bool middleclick_workaround_cptkbd; 70 }; 71 72 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c)) 73 74 #define TP10UBKBD_LED_OUTPUT_REPORT 9 75 76 #define TP10UBKBD_FN_LOCK_LED 0x54 77 #define TP10UBKBD_MUTE_LED 0x64 78 #define TP10UBKBD_MICMUTE_LED 0x74 79 80 #define TP10UBKBD_LED_OFF 1 81 #define TP10UBKBD_LED_ON 2 82 83 /* Function to report raw_events as key events*/ 84 static inline void report_key_event(struct input_dev *input, int keycode) 85 { 86 input_report_key(input, keycode, 1); 87 input_report_key(input, keycode, 0); 88 input_sync(input); 89 } 90 91 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code, 92 enum led_brightness value) 93 { 94 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 95 int ret; 96 97 mutex_lock(&data->led_report_mutex); 98 99 data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT; 100 data->led_report[1] = led_code; 101 data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF; 102 ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3, 103 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); 104 if (ret != 3) { 105 if (ret != -ENODEV) 106 hid_err(hdev, "Set LED output report error: %d\n", ret); 107 108 ret = ret < 0 ? ret : -EIO; 109 } else { 110 ret = 0; 111 } 112 113 mutex_unlock(&data->led_report_mutex); 114 115 return ret; 116 } 117 118 static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work) 119 { 120 struct lenovo_drvdata *data = 121 container_of(work, struct lenovo_drvdata, fn_lock_sync_work); 122 123 lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED, 124 data->fn_lock); 125 } 126 127 static const __u8 lenovo_pro_dock_need_fixup_collection[] = { 128 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */ 129 0x09, 0x01, /* Usage (Vendor Usage 0x01) */ 130 0xa1, 0x01, /* Collection (Application) */ 131 0x85, 0x04, /* Report ID (4) */ 132 0x19, 0x00, /* Usage Minimum (0) */ 133 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */ 134 }; 135 136 /* Broken ThinkPad TrackPoint II collection (Bluetooth mode) */ 137 static const __u8 lenovo_tpIIbtkbd_need_fixup_collection[] = { 138 0x06, 0x00, 0xFF, /* Usage Page (Vendor Defined 0xFF00) */ 139 0x09, 0x01, /* Usage (0x01) */ 140 0xA1, 0x01, /* Collection (Application) */ 141 0x85, 0x05, /* Report ID (5) */ 142 0x1A, 0xF1, 0x00, /* Usage Minimum (0xF1) */ 143 0x2A, 0xFC, 0x00, /* Usage Maximum (0xFC) */ 144 0x15, 0x00, /* Logical Minimum (0) */ 145 0x25, 0x01, /* Logical Maximum (1) */ 146 0x75, 0x01, /* Report Size (1) */ 147 0x95, 0x0D, /* Report Count (13) */ 148 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */ 149 0x95, 0x03, /* Report Count (3) */ 150 0x81, 0x01, /* Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */ 151 }; 152 153 static const __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc, 154 unsigned int *rsize) 155 { 156 switch (hdev->product) { 157 case USB_DEVICE_ID_LENOVO_TPPRODOCK: 158 /* the fixups that need to be done: 159 * - get a reasonable usage max for the vendor collection 160 * 0x8801 from the report ID 4 161 */ 162 if (*rsize >= 153 && 163 memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection, 164 sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) { 165 rdesc[151] = 0x01; 166 rdesc[152] = 0x00; 167 } 168 break; 169 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 170 if (*rsize >= 263 && 171 memcmp(&rdesc[234], lenovo_tpIIbtkbd_need_fixup_collection, 172 sizeof(lenovo_tpIIbtkbd_need_fixup_collection)) == 0) { 173 rdesc[244] = 0x00; /* usage minimum = 0x00 */ 174 rdesc[247] = 0xff; /* usage maximum = 0xff */ 175 rdesc[252] = 0xff; /* logical maximum = 0xff */ 176 rdesc[254] = 0x08; /* report size = 0x08 */ 177 rdesc[256] = 0x01; /* report count = 0x01 */ 178 rdesc[258] = 0x00; /* input = 0x00 */ 179 rdesc[260] = 0x01; /* report count (2) = 0x01 */ 180 } 181 break; 182 } 183 return rdesc; 184 } 185 186 static int lenovo_input_mapping_tpkbd(struct hid_device *hdev, 187 struct hid_input *hi, struct hid_field *field, 188 struct hid_usage *usage, unsigned long **bit, int *max) 189 { 190 if (usage->hid == (HID_UP_BUTTON | 0x0010)) { 191 /* This sub-device contains trackpoint, mark it */ 192 hid_set_drvdata(hdev, (void *)1); 193 map_key_clear(LENOVO_KEY_MICMUTE); 194 return 1; 195 } 196 return 0; 197 } 198 199 static int lenovo_input_mapping_cptkbd(struct hid_device *hdev, 200 struct hid_input *hi, struct hid_field *field, 201 struct hid_usage *usage, unsigned long **bit, int *max) 202 { 203 /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */ 204 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR || 205 (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) { 206 switch (usage->hid & HID_USAGE) { 207 case 0x00f1: /* Fn-F4: Mic mute */ 208 map_key_clear(LENOVO_KEY_MICMUTE); 209 return 1; 210 case 0x00f2: /* Fn-F5: Brightness down */ 211 map_key_clear(KEY_BRIGHTNESSDOWN); 212 return 1; 213 case 0x00f3: /* Fn-F6: Brightness up */ 214 map_key_clear(KEY_BRIGHTNESSUP); 215 return 1; 216 case 0x00f4: /* Fn-F7: External display (projector) */ 217 map_key_clear(KEY_SWITCHVIDEOMODE); 218 return 1; 219 case 0x00f5: /* Fn-F8: Wireless */ 220 map_key_clear(KEY_WLAN); 221 return 1; 222 case 0x00f6: /* Fn-F9: Control panel */ 223 map_key_clear(KEY_CONFIG); 224 return 1; 225 case 0x00f8: /* Fn-F11: View open applications (3 boxes) */ 226 map_key_clear(KEY_SCALE); 227 return 1; 228 case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */ 229 /* NB: This mapping is invented in raw_event below */ 230 map_key_clear(KEY_FILE); 231 return 1; 232 case 0x00fa: /* Fn-Esc: Fn-lock toggle */ 233 map_key_clear(KEY_FN_ESC); 234 return 1; 235 case 0x00fb: /* Middle mouse button (in native mode) */ 236 map_key_clear(BTN_MIDDLE); 237 return 1; 238 } 239 } 240 241 /* Compatibility middle/wheel mappings should be ignored */ 242 if (usage->hid == HID_GD_WHEEL) 243 return -1; 244 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON && 245 (usage->hid & HID_USAGE) == 0x003) 246 return -1; 247 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER && 248 (usage->hid & HID_USAGE) == 0x238) 249 return -1; 250 251 /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */ 252 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 || 253 (usage->hid & HID_USAGE_PAGE) == 0xffa10000) { 254 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE; 255 field->logical_minimum = -127; 256 field->logical_maximum = 127; 257 258 switch (usage->hid & HID_USAGE) { 259 case 0x0000: 260 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL); 261 return 1; 262 case 0x0001: 263 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL); 264 return 1; 265 default: 266 return -1; 267 } 268 } 269 270 return 0; 271 } 272 273 static int lenovo_input_mapping_tpIIkbd(struct hid_device *hdev, 274 struct hid_input *hi, struct hid_field *field, 275 struct hid_usage *usage, unsigned long **bit, int *max) 276 { 277 /* 278 * 0xff0a0000 = USB, HID_UP_MSVENDOR = BT. 279 * 280 * In BT mode, there are two HID_UP_MSVENDOR pages. 281 * Use only the page that contains report ID == 5. 282 */ 283 if (((usage->hid & HID_USAGE_PAGE) == 0xff0a0000 || 284 (usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) && 285 field->report->id == 5) { 286 switch (usage->hid & HID_USAGE) { 287 case 0x00bb: /* Fn-F4: Mic mute */ 288 map_key_clear(LENOVO_KEY_MICMUTE); 289 return 1; 290 case 0x00c3: /* Fn-F5: Brightness down */ 291 map_key_clear(KEY_BRIGHTNESSDOWN); 292 return 1; 293 case 0x00c4: /* Fn-F6: Brightness up */ 294 map_key_clear(KEY_BRIGHTNESSUP); 295 return 1; 296 case 0x00c1: /* Fn-F8: Notification center */ 297 map_key_clear(KEY_NOTIFICATION_CENTER); 298 return 1; 299 case 0x00bc: /* Fn-F9: Control panel */ 300 map_key_clear(KEY_CONFIG); 301 return 1; 302 case 0x00b6: /* Fn-F10: Bluetooth */ 303 map_key_clear(KEY_BLUETOOTH); 304 return 1; 305 case 0x00b7: /* Fn-F11: Keyboard config */ 306 map_key_clear(KEY_KEYBOARD); 307 return 1; 308 case 0x00b8: /* Fn-F12: User function */ 309 map_key_clear(KEY_PROG1); 310 return 1; 311 case 0x00b9: /* Fn-PrtSc: Snipping tool */ 312 map_key_clear(KEY_SELECTIVE_SCREENSHOT); 313 return 1; 314 case 0x00b5: /* Fn-Esc: Fn-lock toggle */ 315 map_key_clear(KEY_FN_ESC); 316 return 1; 317 } 318 } 319 320 if ((usage->hid & HID_USAGE_PAGE) == 0xffa00000) { 321 switch (usage->hid & HID_USAGE) { 322 case 0x00fb: /* Middle mouse (in native USB mode) */ 323 map_key_clear(BTN_MIDDLE); 324 return 1; 325 } 326 } 327 328 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && 329 field->report->id == 21) { 330 switch (usage->hid & HID_USAGE) { 331 case 0x0004: /* Middle mouse (in native Bluetooth mode) */ 332 map_key_clear(BTN_MIDDLE); 333 return 1; 334 } 335 } 336 337 /* Compatibility middle/wheel mappings should be ignored */ 338 if (usage->hid == HID_GD_WHEEL) 339 return -1; 340 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON && 341 (usage->hid & HID_USAGE) == 0x003) 342 return -1; 343 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER && 344 (usage->hid & HID_USAGE) == 0x238) 345 return -1; 346 347 /* Map wheel emulation reports: 0xff10 */ 348 if ((usage->hid & HID_USAGE_PAGE) == 0xff100000) { 349 field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE; 350 field->logical_minimum = -127; 351 field->logical_maximum = 127; 352 353 switch (usage->hid & HID_USAGE) { 354 case 0x0000: 355 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL); 356 return 1; 357 case 0x0001: 358 hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL); 359 return 1; 360 default: 361 return -1; 362 } 363 } 364 365 return 0; 366 } 367 368 static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev, 369 struct hid_input *hi, struct hid_field *field, 370 struct hid_usage *usage, unsigned long **bit, int *max) 371 { 372 if (usage->hid == HID_GD_Z) { 373 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL); 374 return 1; 375 } 376 return 0; 377 } 378 379 static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev, 380 struct hid_input *hi, struct hid_field *field, 381 struct hid_usage *usage, unsigned long **bit, int *max) 382 { 383 /* 384 * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for 385 * a bunch of keys which have no standard consumer page code. 386 */ 387 if (usage->hid == 0x000c0001) { 388 switch (usage->usage_index) { 389 case 8: /* Fn-Esc: Fn-lock toggle */ 390 map_key_clear(KEY_FN_ESC); 391 return 1; 392 case 9: /* Fn-F4: Mic mute */ 393 map_key_clear(LENOVO_KEY_MICMUTE); 394 return 1; 395 case 10: /* Fn-F7: Control panel */ 396 map_key_clear(KEY_CONFIG); 397 return 1; 398 case 11: /* Fn-F8: Search (magnifier glass) */ 399 map_key_clear(KEY_SEARCH); 400 return 1; 401 case 12: /* Fn-F10: Open My computer (6 boxes) */ 402 map_key_clear(KEY_FILE); 403 return 1; 404 } 405 } 406 407 /* 408 * The Ultrabook Keyboard sends a spurious F23 key-press when resuming 409 * from suspend and it does not actually have a F23 key, ignore it. 410 */ 411 if (usage->hid == 0x00070072) 412 return -1; 413 414 return 0; 415 } 416 417 static int lenovo_input_mapping_x1_tab_kbd(struct hid_device *hdev, 418 struct hid_input *hi, struct hid_field *field, 419 struct hid_usage *usage, unsigned long **bit, int *max) 420 { 421 /* 422 * The ThinkPad X1 Tablet Thin Keyboard uses 0x000c0001 usage for 423 * a bunch of keys which have no standard consumer page code. 424 */ 425 if (usage->hid == 0x000c0001) { 426 switch (usage->usage_index) { 427 case 0: /* Fn-F10: Enable/disable bluetooth */ 428 map_key_clear(KEY_BLUETOOTH); 429 return 1; 430 case 1: /* Fn-F11: Keyboard settings */ 431 map_key_clear(KEY_KEYBOARD); 432 return 1; 433 case 2: /* Fn-F12: User function / Cortana */ 434 map_key_clear(KEY_MACRO1); 435 return 1; 436 case 3: /* Fn-PrtSc: Snipping tool */ 437 map_key_clear(KEY_SELECTIVE_SCREENSHOT); 438 return 1; 439 case 8: /* Fn-Esc: Fn-lock toggle */ 440 map_key_clear(KEY_FN_ESC); 441 return 1; 442 case 9: /* Fn-F4: Mute/unmute microphone */ 443 map_key_clear(KEY_MICMUTE); 444 return 1; 445 case 10: /* Fn-F9: Settings */ 446 map_key_clear(KEY_CONFIG); 447 return 1; 448 case 13: /* Fn-F7: Manage external displays */ 449 map_key_clear(KEY_SWITCHVIDEOMODE); 450 return 1; 451 case 14: /* Fn-F8: Enable/disable wifi */ 452 map_key_clear(KEY_WLAN); 453 return 1; 454 } 455 } 456 457 if (usage->hid == (HID_UP_KEYBOARD | 0x009a)) { 458 map_key_clear(KEY_SYSRQ); 459 return 1; 460 } 461 462 return 0; 463 } 464 465 static int lenovo_input_mapping(struct hid_device *hdev, 466 struct hid_input *hi, struct hid_field *field, 467 struct hid_usage *usage, unsigned long **bit, int *max) 468 { 469 switch (hdev->product) { 470 case USB_DEVICE_ID_LENOVO_TPKBD: 471 return lenovo_input_mapping_tpkbd(hdev, hi, field, 472 usage, bit, max); 473 case USB_DEVICE_ID_LENOVO_CUSBKBD: 474 case USB_DEVICE_ID_LENOVO_CBTKBD: 475 return lenovo_input_mapping_cptkbd(hdev, hi, field, 476 usage, bit, max); 477 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 478 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 479 return lenovo_input_mapping_tpIIkbd(hdev, hi, field, 480 usage, bit, max); 481 case USB_DEVICE_ID_IBM_SCROLLPOINT_III: 482 case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO: 483 case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL: 484 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL: 485 case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO: 486 case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL: 487 return lenovo_input_mapping_scrollpoint(hdev, hi, field, 488 usage, bit, max); 489 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 490 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field, 491 usage, bit, max); 492 case USB_DEVICE_ID_LENOVO_X12_TAB: 493 case USB_DEVICE_ID_LENOVO_X12_TAB2: 494 case USB_DEVICE_ID_LENOVO_X1_TAB: 495 case USB_DEVICE_ID_LENOVO_X1_TAB3: 496 return lenovo_input_mapping_x1_tab_kbd(hdev, hi, field, usage, bit, max); 497 default: 498 return 0; 499 } 500 } 501 502 #undef map_key_clear 503 504 /* Send a config command to the keyboard */ 505 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev, 506 unsigned char byte2, unsigned char byte3) 507 { 508 int ret; 509 unsigned char *buf; 510 511 buf = kzalloc(3, GFP_KERNEL); 512 if (!buf) 513 return -ENOMEM; 514 515 /* 516 * Feature report 0x13 is used for USB, 517 * output report 0x18 is used for Bluetooth. 518 * buf[0] is ignored by hid_hw_raw_request. 519 */ 520 buf[0] = 0x18; 521 buf[1] = byte2; 522 buf[2] = byte3; 523 524 switch (hdev->product) { 525 case USB_DEVICE_ID_LENOVO_CUSBKBD: 526 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 527 ret = hid_hw_raw_request(hdev, 0x13, buf, 3, 528 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 529 break; 530 case USB_DEVICE_ID_LENOVO_CBTKBD: 531 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 532 ret = hid_hw_output_report(hdev, buf, 3); 533 break; 534 default: 535 ret = -EINVAL; 536 break; 537 } 538 539 kfree(buf); 540 541 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */ 542 } 543 544 static void lenovo_features_set_cptkbd(struct hid_device *hdev) 545 { 546 int ret; 547 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 548 549 /* 550 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into 551 * regular keys 552 */ 553 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03); 554 if (ret) 555 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret); 556 557 /* Switch middle button to native mode */ 558 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01); 559 if (ret) 560 hid_warn(hdev, "Failed to switch middle button: %d\n", ret); 561 562 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock); 563 if (ret) 564 hid_err(hdev, "Fn-lock setting failed: %d\n", ret); 565 566 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity); 567 if (ret) 568 hid_err(hdev, "Sensitivity setting failed: %d\n", ret); 569 } 570 571 static ssize_t attr_fn_lock_show(struct device *dev, 572 struct device_attribute *attr, 573 char *buf) 574 { 575 struct hid_device *hdev = to_hid_device(dev); 576 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 577 578 return sysfs_emit(buf, "%u\n", data->fn_lock); 579 } 580 581 static ssize_t attr_fn_lock_store(struct device *dev, 582 struct device_attribute *attr, 583 const char *buf, 584 size_t count) 585 { 586 struct hid_device *hdev = to_hid_device(dev); 587 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 588 int value, ret; 589 590 if (kstrtoint(buf, 10, &value)) 591 return -EINVAL; 592 if (value < 0 || value > 1) 593 return -EINVAL; 594 595 data->fn_lock = !!value; 596 597 switch (hdev->product) { 598 case USB_DEVICE_ID_LENOVO_CUSBKBD: 599 case USB_DEVICE_ID_LENOVO_CBTKBD: 600 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 601 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 602 lenovo_features_set_cptkbd(hdev); 603 break; 604 case USB_DEVICE_ID_LENOVO_X12_TAB: 605 case USB_DEVICE_ID_LENOVO_X12_TAB2: 606 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 607 case USB_DEVICE_ID_LENOVO_X1_TAB: 608 case USB_DEVICE_ID_LENOVO_X1_TAB3: 609 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value); 610 if (ret) 611 return ret; 612 break; 613 } 614 615 return count; 616 } 617 618 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev, 619 struct device_attribute *attr, 620 char *buf) 621 { 622 struct hid_device *hdev = to_hid_device(dev); 623 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 624 625 return sysfs_emit(buf, "%u\n", cptkbd_data->sensitivity); 626 } 627 628 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev, 629 struct device_attribute *attr, 630 const char *buf, 631 size_t count) 632 { 633 struct hid_device *hdev = to_hid_device(dev); 634 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 635 int value; 636 637 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) 638 return -EINVAL; 639 640 cptkbd_data->sensitivity = value; 641 lenovo_features_set_cptkbd(hdev); 642 643 return count; 644 } 645 646 static ssize_t attr_middleclick_workaround_show_cptkbd(struct device *dev, 647 struct device_attribute *attr, 648 char *buf) 649 { 650 struct hid_device *hdev = to_hid_device(dev); 651 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 652 653 return sysfs_emit(buf, "%u\n", 654 cptkbd_data->middleclick_workaround_cptkbd); 655 } 656 657 static ssize_t attr_middleclick_workaround_store_cptkbd(struct device *dev, 658 struct device_attribute *attr, 659 const char *buf, 660 size_t count) 661 { 662 struct hid_device *hdev = to_hid_device(dev); 663 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 664 int value; 665 666 if (kstrtoint(buf, 10, &value)) 667 return -EINVAL; 668 if (value < 0 || value > 1) 669 return -EINVAL; 670 671 cptkbd_data->middleclick_workaround_cptkbd = !!value; 672 673 return count; 674 } 675 676 677 static struct device_attribute dev_attr_fn_lock = 678 __ATTR(fn_lock, S_IWUSR | S_IRUGO, 679 attr_fn_lock_show, 680 attr_fn_lock_store); 681 682 static struct device_attribute dev_attr_sensitivity_cptkbd = 683 __ATTR(sensitivity, S_IWUSR | S_IRUGO, 684 attr_sensitivity_show_cptkbd, 685 attr_sensitivity_store_cptkbd); 686 687 static struct device_attribute dev_attr_middleclick_workaround_cptkbd = 688 __ATTR(middleclick_workaround, S_IWUSR | S_IRUGO, 689 attr_middleclick_workaround_show_cptkbd, 690 attr_middleclick_workaround_store_cptkbd); 691 692 693 static struct attribute *lenovo_attributes_cptkbd[] = { 694 &dev_attr_fn_lock.attr, 695 &dev_attr_sensitivity_cptkbd.attr, 696 &dev_attr_middleclick_workaround_cptkbd.attr, 697 NULL 698 }; 699 700 static const struct attribute_group lenovo_attr_group_cptkbd = { 701 .attrs = lenovo_attributes_cptkbd, 702 }; 703 704 /* Function to handle Lenovo Thinkpad TAB X12's HID raw inputs for fn keys*/ 705 static int lenovo_raw_event_TP_X12_tab(struct hid_device *hdev, u32 raw_data) 706 { 707 struct hid_input *hidinput; 708 struct input_dev *input = NULL; 709 710 /* Iterate through all associated input devices */ 711 list_for_each_entry(hidinput, &hdev->inputs, list) { 712 input = hidinput->input; 713 if (!input) 714 continue; 715 716 switch (raw_data) { 717 /* fn-F20 being used here for MIC mute*/ 718 case TP_X12_RAW_HOTKEY_FN_F4: 719 report_key_event(input, LENOVO_KEY_MICMUTE); 720 return 1; 721 /* Power-mode or Airplane mode will be called based on the device*/ 722 case TP_X12_RAW_HOTKEY_FN_F8: 723 /* 724 * TP X12 TAB uses Fn-F8 calls Airplanemode 725 * Whereas TP X12 TAB2 uses Fn-F8 for toggling 726 * Power modes 727 */ 728 if (hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) { 729 report_key_event(input, KEY_RFKILL); 730 return 1; 731 } 732 platform_profile_cycle(); 733 return 1; 734 case TP_X12_RAW_HOTKEY_FN_F10: 735 /* TAB1 has PICKUP Phone and TAB2 use Snipping tool*/ 736 (hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) ? 737 report_key_event(input, KEY_PICKUP_PHONE) : 738 report_key_event(input, KEY_SELECTIVE_SCREENSHOT); 739 return 1; 740 case TP_X12_RAW_HOTKEY_FN_F12: 741 /* BookMarks/STAR key*/ 742 report_key_event(input, KEY_BOOKMARKS); 743 return 1; 744 case TP_X12_RAW_HOTKEY_FN_SPACE: 745 /* Keyboard LED backlight toggle*/ 746 report_key_event(input, KEY_KBDILLUMTOGGLE); 747 return 1; 748 default: 749 break; 750 } 751 } 752 return 0; 753 } 754 755 static int lenovo_raw_event(struct hid_device *hdev, 756 struct hid_report *report, u8 *data, int size) 757 { 758 /* 759 * Compact USB keyboard's Fn-F12 report holds down many other keys, and 760 * its own key is outside the usage page range. Remove extra 761 * keypresses and remap to inside usage page. 762 */ 763 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD 764 && size == 3 765 && data[0] == 0x15 766 && data[1] == 0x94 767 && data[2] == 0x01)) { 768 data[1] = 0x00; 769 data[2] = 0x01; 770 } 771 772 /* 773 * Lenovo TP X12 Tab KBD's Fn+XX is HID raw data defined. Report ID is 0x03 774 * e.g.: Raw data received for MIC mute is 0x00020003. 775 */ 776 if (unlikely((hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB 777 || hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2) 778 && size >= 3 && report->id == 0x03)) 779 return lenovo_raw_event_TP_X12_tab(hdev, le32_to_cpu(*(__le32 *)data)); 780 781 return 0; 782 } 783 784 static int lenovo_event_tp10ubkbd(struct hid_device *hdev, 785 struct hid_field *field, struct hid_usage *usage, __s32 value) 786 { 787 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 788 789 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) { 790 /* 791 * The user has toggled the Fn-lock state. Toggle our own 792 * cached value of it and sync our value to the keyboard to 793 * ensure things are in sync (the sycning should be a no-op). 794 */ 795 data->fn_lock = !data->fn_lock; 796 schedule_work(&data->fn_lock_sync_work); 797 } 798 799 return 0; 800 } 801 802 static int lenovo_event_cptkbd(struct hid_device *hdev, 803 struct hid_field *field, struct hid_usage *usage, __s32 value) 804 { 805 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 806 807 if (cptkbd_data->middleclick_workaround_cptkbd) { 808 /* "wheel" scroll events */ 809 if (usage->type == EV_REL && (usage->code == REL_WHEEL || 810 usage->code == REL_HWHEEL)) { 811 /* Scroll events disable middle-click event */ 812 cptkbd_data->middlebutton_state = 2; 813 return 0; 814 } 815 816 /* Middle click events */ 817 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) { 818 if (value == 1) { 819 cptkbd_data->middlebutton_state = 1; 820 } else if (value == 0) { 821 if (cptkbd_data->middlebutton_state == 1) { 822 /* No scrolling inbetween, send middle-click */ 823 input_event(field->hidinput->input, 824 EV_KEY, BTN_MIDDLE, 1); 825 input_sync(field->hidinput->input); 826 input_event(field->hidinput->input, 827 EV_KEY, BTN_MIDDLE, 0); 828 input_sync(field->hidinput->input); 829 } 830 cptkbd_data->middlebutton_state = 0; 831 } 832 return 1; 833 } 834 } 835 836 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) { 837 /* 838 * The user has toggled the Fn-lock state. Toggle our own 839 * cached value of it and sync our value to the keyboard to 840 * ensure things are in sync (the syncing should be a no-op). 841 */ 842 cptkbd_data->fn_lock = !cptkbd_data->fn_lock; 843 } 844 845 return 0; 846 } 847 848 static int lenovo_event(struct hid_device *hdev, struct hid_field *field, 849 struct hid_usage *usage, __s32 value) 850 { 851 if (!hid_get_drvdata(hdev)) 852 return 0; 853 854 switch (hdev->product) { 855 case USB_DEVICE_ID_LENOVO_CUSBKBD: 856 case USB_DEVICE_ID_LENOVO_CBTKBD: 857 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 858 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 859 return lenovo_event_cptkbd(hdev, field, usage, value); 860 case USB_DEVICE_ID_LENOVO_X12_TAB: 861 case USB_DEVICE_ID_LENOVO_X12_TAB2: 862 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 863 case USB_DEVICE_ID_LENOVO_X1_TAB: 864 case USB_DEVICE_ID_LENOVO_X1_TAB3: 865 return lenovo_event_tp10ubkbd(hdev, field, usage, value); 866 default: 867 return 0; 868 } 869 } 870 871 static int lenovo_features_set_tpkbd(struct hid_device *hdev) 872 { 873 struct hid_report *report; 874 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 875 876 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4]; 877 878 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02; 879 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08; 880 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20; 881 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40; 882 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver 883 report->field[2]->value[0] = data_pointer->sensitivity; 884 report->field[3]->value[0] = data_pointer->press_speed; 885 886 hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 887 return 0; 888 } 889 890 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev, 891 struct device_attribute *attr, 892 char *buf) 893 { 894 struct hid_device *hdev = to_hid_device(dev); 895 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 896 897 return sysfs_emit(buf, "%u\n", data_pointer->press_to_select); 898 } 899 900 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev, 901 struct device_attribute *attr, 902 const char *buf, 903 size_t count) 904 { 905 struct hid_device *hdev = to_hid_device(dev); 906 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 907 int value; 908 909 if (kstrtoint(buf, 10, &value)) 910 return -EINVAL; 911 if (value < 0 || value > 1) 912 return -EINVAL; 913 914 data_pointer->press_to_select = value; 915 lenovo_features_set_tpkbd(hdev); 916 917 return count; 918 } 919 920 static ssize_t attr_dragging_show_tpkbd(struct device *dev, 921 struct device_attribute *attr, 922 char *buf) 923 { 924 struct hid_device *hdev = to_hid_device(dev); 925 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 926 927 return sysfs_emit(buf, "%u\n", data_pointer->dragging); 928 } 929 930 static ssize_t attr_dragging_store_tpkbd(struct device *dev, 931 struct device_attribute *attr, 932 const char *buf, 933 size_t count) 934 { 935 struct hid_device *hdev = to_hid_device(dev); 936 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 937 int value; 938 939 if (kstrtoint(buf, 10, &value)) 940 return -EINVAL; 941 if (value < 0 || value > 1) 942 return -EINVAL; 943 944 data_pointer->dragging = value; 945 lenovo_features_set_tpkbd(hdev); 946 947 return count; 948 } 949 950 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev, 951 struct device_attribute *attr, 952 char *buf) 953 { 954 struct hid_device *hdev = to_hid_device(dev); 955 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 956 957 return sysfs_emit(buf, "%u\n", data_pointer->release_to_select); 958 } 959 960 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev, 961 struct device_attribute *attr, 962 const char *buf, 963 size_t count) 964 { 965 struct hid_device *hdev = to_hid_device(dev); 966 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 967 int value; 968 969 if (kstrtoint(buf, 10, &value)) 970 return -EINVAL; 971 if (value < 0 || value > 1) 972 return -EINVAL; 973 974 data_pointer->release_to_select = value; 975 lenovo_features_set_tpkbd(hdev); 976 977 return count; 978 } 979 980 static ssize_t attr_select_right_show_tpkbd(struct device *dev, 981 struct device_attribute *attr, 982 char *buf) 983 { 984 struct hid_device *hdev = to_hid_device(dev); 985 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 986 987 return sysfs_emit(buf, "%u\n", data_pointer->select_right); 988 } 989 990 static ssize_t attr_select_right_store_tpkbd(struct device *dev, 991 struct device_attribute *attr, 992 const char *buf, 993 size_t count) 994 { 995 struct hid_device *hdev = to_hid_device(dev); 996 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 997 int value; 998 999 if (kstrtoint(buf, 10, &value)) 1000 return -EINVAL; 1001 if (value < 0 || value > 1) 1002 return -EINVAL; 1003 1004 data_pointer->select_right = value; 1005 lenovo_features_set_tpkbd(hdev); 1006 1007 return count; 1008 } 1009 1010 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev, 1011 struct device_attribute *attr, 1012 char *buf) 1013 { 1014 struct hid_device *hdev = to_hid_device(dev); 1015 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1016 1017 return sysfs_emit(buf, "%u\n", data_pointer->sensitivity); 1018 } 1019 1020 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev, 1021 struct device_attribute *attr, 1022 const char *buf, 1023 size_t count) 1024 { 1025 struct hid_device *hdev = to_hid_device(dev); 1026 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1027 int value; 1028 1029 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) 1030 return -EINVAL; 1031 1032 data_pointer->sensitivity = value; 1033 lenovo_features_set_tpkbd(hdev); 1034 1035 return count; 1036 } 1037 1038 static ssize_t attr_press_speed_show_tpkbd(struct device *dev, 1039 struct device_attribute *attr, 1040 char *buf) 1041 { 1042 struct hid_device *hdev = to_hid_device(dev); 1043 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1044 1045 return sysfs_emit(buf, "%u\n", data_pointer->press_speed); 1046 } 1047 1048 static ssize_t attr_press_speed_store_tpkbd(struct device *dev, 1049 struct device_attribute *attr, 1050 const char *buf, 1051 size_t count) 1052 { 1053 struct hid_device *hdev = to_hid_device(dev); 1054 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1055 int value; 1056 1057 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) 1058 return -EINVAL; 1059 1060 data_pointer->press_speed = value; 1061 lenovo_features_set_tpkbd(hdev); 1062 1063 return count; 1064 } 1065 1066 static struct device_attribute dev_attr_press_to_select_tpkbd = 1067 __ATTR(press_to_select, S_IWUSR | S_IRUGO, 1068 attr_press_to_select_show_tpkbd, 1069 attr_press_to_select_store_tpkbd); 1070 1071 static struct device_attribute dev_attr_dragging_tpkbd = 1072 __ATTR(dragging, S_IWUSR | S_IRUGO, 1073 attr_dragging_show_tpkbd, 1074 attr_dragging_store_tpkbd); 1075 1076 static struct device_attribute dev_attr_release_to_select_tpkbd = 1077 __ATTR(release_to_select, S_IWUSR | S_IRUGO, 1078 attr_release_to_select_show_tpkbd, 1079 attr_release_to_select_store_tpkbd); 1080 1081 static struct device_attribute dev_attr_select_right_tpkbd = 1082 __ATTR(select_right, S_IWUSR | S_IRUGO, 1083 attr_select_right_show_tpkbd, 1084 attr_select_right_store_tpkbd); 1085 1086 static struct device_attribute dev_attr_sensitivity_tpkbd = 1087 __ATTR(sensitivity, S_IWUSR | S_IRUGO, 1088 attr_sensitivity_show_tpkbd, 1089 attr_sensitivity_store_tpkbd); 1090 1091 static struct device_attribute dev_attr_press_speed_tpkbd = 1092 __ATTR(press_speed, S_IWUSR | S_IRUGO, 1093 attr_press_speed_show_tpkbd, 1094 attr_press_speed_store_tpkbd); 1095 1096 static struct attribute *lenovo_attributes_tpkbd[] = { 1097 &dev_attr_press_to_select_tpkbd.attr, 1098 &dev_attr_dragging_tpkbd.attr, 1099 &dev_attr_release_to_select_tpkbd.attr, 1100 &dev_attr_select_right_tpkbd.attr, 1101 &dev_attr_sensitivity_tpkbd.attr, 1102 &dev_attr_press_speed_tpkbd.attr, 1103 NULL 1104 }; 1105 1106 static const struct attribute_group lenovo_attr_group_tpkbd = { 1107 .attrs = lenovo_attributes_tpkbd, 1108 }; 1109 1110 static void lenovo_led_set_tpkbd(struct hid_device *hdev) 1111 { 1112 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1113 struct hid_report *report; 1114 1115 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3]; 1116 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1; 1117 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1; 1118 hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 1119 } 1120 1121 static int lenovo_led_brightness_set(struct led_classdev *led_cdev, 1122 enum led_brightness value) 1123 { 1124 struct device *dev = led_cdev->dev->parent; 1125 struct hid_device *hdev = to_hid_device(dev); 1126 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1127 static const u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED }; 1128 int led_nr = 0; 1129 int ret = 0; 1130 1131 if (led_cdev == &data_pointer->led_micmute) 1132 led_nr = 1; 1133 1134 if (value == LED_OFF) 1135 data_pointer->led_state &= ~(1 << led_nr); 1136 else 1137 data_pointer->led_state |= 1 << led_nr; 1138 1139 switch (hdev->product) { 1140 case USB_DEVICE_ID_LENOVO_TPKBD: 1141 lenovo_led_set_tpkbd(hdev); 1142 break; 1143 case USB_DEVICE_ID_LENOVO_X12_TAB: 1144 case USB_DEVICE_ID_LENOVO_X12_TAB2: 1145 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 1146 case USB_DEVICE_ID_LENOVO_X1_TAB: 1147 case USB_DEVICE_ID_LENOVO_X1_TAB3: 1148 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value); 1149 break; 1150 } 1151 1152 return ret; 1153 } 1154 1155 static int lenovo_register_leds(struct hid_device *hdev) 1156 { 1157 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 1158 size_t name_sz = strlen(dev_name(&hdev->dev)) + 16; 1159 char *name_mute, *name_micm; 1160 int ret; 1161 1162 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL); 1163 name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL); 1164 if (name_mute == NULL || name_micm == NULL) { 1165 hid_err(hdev, "Could not allocate memory for led data\n"); 1166 return -ENOMEM; 1167 } 1168 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev)); 1169 snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev)); 1170 1171 data->led_mute.name = name_mute; 1172 data->led_mute.default_trigger = "audio-mute"; 1173 data->led_mute.brightness_set_blocking = lenovo_led_brightness_set; 1174 data->led_mute.max_brightness = 1; 1175 data->led_mute.flags = LED_HW_PLUGGABLE; 1176 data->led_mute.dev = &hdev->dev; 1177 ret = led_classdev_register(&hdev->dev, &data->led_mute); 1178 if (ret < 0) 1179 return ret; 1180 1181 data->led_micmute.name = name_micm; 1182 data->led_micmute.default_trigger = "audio-micmute"; 1183 data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set; 1184 data->led_micmute.max_brightness = 1; 1185 data->led_micmute.flags = LED_HW_PLUGGABLE; 1186 data->led_micmute.dev = &hdev->dev; 1187 ret = led_classdev_register(&hdev->dev, &data->led_micmute); 1188 if (ret < 0) { 1189 led_classdev_unregister(&data->led_mute); 1190 return ret; 1191 } 1192 1193 return 0; 1194 } 1195 1196 static int lenovo_probe_tpkbd(struct hid_device *hdev) 1197 { 1198 struct lenovo_drvdata *data_pointer; 1199 int i, ret; 1200 1201 /* 1202 * Only register extra settings against subdevice where input_mapping 1203 * set drvdata to 1, i.e. the trackpoint. 1204 */ 1205 if (!hid_get_drvdata(hdev)) 1206 return 0; 1207 1208 hid_set_drvdata(hdev, NULL); 1209 1210 /* Validate required reports. */ 1211 for (i = 0; i < 4; i++) { 1212 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1)) 1213 return -ENODEV; 1214 } 1215 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2)) 1216 return -ENODEV; 1217 1218 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd); 1219 if (ret) 1220 hid_warn(hdev, "Could not create sysfs group: %d\n", ret); 1221 1222 data_pointer = devm_kzalloc(&hdev->dev, 1223 sizeof(struct lenovo_drvdata), 1224 GFP_KERNEL); 1225 if (data_pointer == NULL) { 1226 hid_err(hdev, "Could not allocate memory for driver data\n"); 1227 ret = -ENOMEM; 1228 goto err; 1229 } 1230 1231 // set same default values as windows driver 1232 data_pointer->sensitivity = 0xa0; 1233 data_pointer->press_speed = 0x38; 1234 1235 hid_set_drvdata(hdev, data_pointer); 1236 1237 ret = lenovo_register_leds(hdev); 1238 if (ret) 1239 goto err; 1240 1241 lenovo_features_set_tpkbd(hdev); 1242 1243 return 0; 1244 err: 1245 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd); 1246 return ret; 1247 } 1248 1249 static int lenovo_probe_cptkbd(struct hid_device *hdev) 1250 { 1251 int ret; 1252 struct lenovo_drvdata *cptkbd_data; 1253 1254 /* All the custom action happens on the USBMOUSE device for USB */ 1255 if (((hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD) || 1256 (hdev->product == USB_DEVICE_ID_LENOVO_TPIIUSBKBD)) && 1257 hdev->type != HID_TYPE_USBMOUSE) { 1258 hid_dbg(hdev, "Ignoring keyboard half of device\n"); 1259 return 0; 1260 } 1261 1262 cptkbd_data = devm_kzalloc(&hdev->dev, 1263 sizeof(*cptkbd_data), 1264 GFP_KERNEL); 1265 if (cptkbd_data == NULL) { 1266 hid_err(hdev, "can't alloc keyboard descriptor\n"); 1267 return -ENOMEM; 1268 } 1269 hid_set_drvdata(hdev, cptkbd_data); 1270 1271 /* Set keyboard settings to known state */ 1272 cptkbd_data->middlebutton_state = 0; 1273 cptkbd_data->fn_lock = true; 1274 cptkbd_data->sensitivity = 0x05; 1275 cptkbd_data->middleclick_workaround_cptkbd = true; 1276 lenovo_features_set_cptkbd(hdev); 1277 1278 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd); 1279 if (ret) 1280 hid_warn(hdev, "Could not create sysfs group: %d\n", ret); 1281 1282 return 0; 1283 } 1284 1285 static struct attribute *lenovo_attributes_tp10ubkbd[] = { 1286 &dev_attr_fn_lock.attr, 1287 NULL 1288 }; 1289 1290 static const struct attribute_group lenovo_attr_group_tp10ubkbd = { 1291 .attrs = lenovo_attributes_tp10ubkbd, 1292 }; 1293 1294 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev) 1295 { 1296 struct hid_report_enum *rep_enum; 1297 struct lenovo_drvdata *data; 1298 struct hid_report *rep; 1299 bool found; 1300 int ret; 1301 1302 /* 1303 * The LEDs and the Fn-lock functionality use output report 9, 1304 * with an application of 0xffa0001, add the LEDs on the interface 1305 * with this output report. 1306 */ 1307 found = false; 1308 rep_enum = &hdev->report_enum[HID_OUTPUT_REPORT]; 1309 list_for_each_entry(rep, &rep_enum->report_list, list) { 1310 if (rep->application == 0xffa00001) 1311 found = true; 1312 } 1313 if (!found) 1314 return 0; 1315 1316 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL); 1317 if (!data) 1318 return -ENOMEM; 1319 1320 mutex_init(&data->led_report_mutex); 1321 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock); 1322 data->hdev = hdev; 1323 1324 hid_set_drvdata(hdev, data); 1325 1326 /* 1327 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on. 1328 * We cannot read the state, only set it, so we force it to on here 1329 * (which should be a no-op) to make sure that our state matches the 1330 * keyboard's FN-lock state. This is the same as what Windows does. 1331 * 1332 * For X12 TAB and TAB2, the default windows behaviour Fn-lock Off. 1333 * Adding additional check to ensure the behaviour in case of 1334 * Thinkpad X12 Tabs. 1335 */ 1336 1337 data->fn_lock = !(hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB || 1338 hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2); 1339 1340 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock); 1341 1342 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd); 1343 if (ret) 1344 return ret; 1345 1346 ret = lenovo_register_leds(hdev); 1347 if (ret) 1348 goto err; 1349 1350 return 0; 1351 err: 1352 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd); 1353 return ret; 1354 } 1355 1356 static int lenovo_probe(struct hid_device *hdev, 1357 const struct hid_device_id *id) 1358 { 1359 int ret; 1360 1361 ret = hid_parse(hdev); 1362 if (ret) { 1363 hid_err(hdev, "hid_parse failed\n"); 1364 goto err; 1365 } 1366 1367 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 1368 if (ret) { 1369 hid_err(hdev, "hid_hw_start failed\n"); 1370 goto err; 1371 } 1372 1373 switch (hdev->product) { 1374 case USB_DEVICE_ID_LENOVO_TPKBD: 1375 ret = lenovo_probe_tpkbd(hdev); 1376 break; 1377 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1378 case USB_DEVICE_ID_LENOVO_CBTKBD: 1379 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1380 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 1381 ret = lenovo_probe_cptkbd(hdev); 1382 break; 1383 case USB_DEVICE_ID_LENOVO_X12_TAB: 1384 case USB_DEVICE_ID_LENOVO_X12_TAB2: 1385 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 1386 case USB_DEVICE_ID_LENOVO_X1_TAB: 1387 case USB_DEVICE_ID_LENOVO_X1_TAB3: 1388 ret = lenovo_probe_tp10ubkbd(hdev); 1389 break; 1390 default: 1391 ret = 0; 1392 break; 1393 } 1394 if (ret) 1395 goto err_hid; 1396 1397 return 0; 1398 err_hid: 1399 hid_hw_stop(hdev); 1400 err: 1401 return ret; 1402 } 1403 1404 #ifdef CONFIG_PM 1405 static int lenovo_reset_resume(struct hid_device *hdev) 1406 { 1407 switch (hdev->product) { 1408 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1409 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1410 if (hdev->type == HID_TYPE_USBMOUSE) 1411 lenovo_features_set_cptkbd(hdev); 1412 1413 break; 1414 default: 1415 break; 1416 } 1417 1418 return 0; 1419 } 1420 #endif 1421 1422 static void lenovo_remove_tpkbd(struct hid_device *hdev) 1423 { 1424 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1425 1426 /* 1427 * Only the trackpoint half of the keyboard has drvdata and stuff that 1428 * needs unregistering. 1429 */ 1430 if (data_pointer == NULL) 1431 return; 1432 1433 sysfs_remove_group(&hdev->dev.kobj, 1434 &lenovo_attr_group_tpkbd); 1435 1436 led_classdev_unregister(&data_pointer->led_micmute); 1437 led_classdev_unregister(&data_pointer->led_mute); 1438 } 1439 1440 static void lenovo_remove_cptkbd(struct hid_device *hdev) 1441 { 1442 sysfs_remove_group(&hdev->dev.kobj, 1443 &lenovo_attr_group_cptkbd); 1444 } 1445 1446 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev) 1447 { 1448 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 1449 1450 if (data == NULL) 1451 return; 1452 1453 led_classdev_unregister(&data->led_micmute); 1454 led_classdev_unregister(&data->led_mute); 1455 1456 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd); 1457 cancel_work_sync(&data->fn_lock_sync_work); 1458 } 1459 1460 static void lenovo_remove(struct hid_device *hdev) 1461 { 1462 switch (hdev->product) { 1463 case USB_DEVICE_ID_LENOVO_TPKBD: 1464 lenovo_remove_tpkbd(hdev); 1465 break; 1466 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1467 case USB_DEVICE_ID_LENOVO_CBTKBD: 1468 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1469 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 1470 lenovo_remove_cptkbd(hdev); 1471 break; 1472 case USB_DEVICE_ID_LENOVO_X12_TAB: 1473 case USB_DEVICE_ID_LENOVO_X12_TAB2: 1474 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 1475 case USB_DEVICE_ID_LENOVO_X1_TAB: 1476 case USB_DEVICE_ID_LENOVO_X1_TAB3: 1477 lenovo_remove_tp10ubkbd(hdev); 1478 break; 1479 } 1480 1481 hid_hw_stop(hdev); 1482 } 1483 1484 static int lenovo_input_configured(struct hid_device *hdev, 1485 struct hid_input *hi) 1486 { 1487 switch (hdev->product) { 1488 case USB_DEVICE_ID_LENOVO_TPKBD: 1489 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1490 case USB_DEVICE_ID_LENOVO_CBTKBD: 1491 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1492 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 1493 if (test_bit(EV_REL, hi->input->evbit)) { 1494 /* set only for trackpoint device */ 1495 __set_bit(INPUT_PROP_POINTER, hi->input->propbit); 1496 __set_bit(INPUT_PROP_POINTING_STICK, 1497 hi->input->propbit); 1498 } 1499 break; 1500 } 1501 1502 return 0; 1503 } 1504 1505 1506 static const struct hid_device_id lenovo_devices[] = { 1507 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) }, 1508 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) }, 1509 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIUSBKBD) }, 1510 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) }, 1511 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIBTKBD) }, 1512 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) }, 1513 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) }, 1514 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) }, 1515 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) }, 1516 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) }, 1517 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) }, 1518 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) }, 1519 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) }, 1520 /* 1521 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard 1522 * part, while letting hid-multitouch.c handle the touchpad and trackpoint. 1523 */ 1524 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1525 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB) }, 1526 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1527 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB3) }, 1528 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1529 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB) }, 1530 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1531 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB2) }, 1532 { } 1533 }; 1534 1535 MODULE_DEVICE_TABLE(hid, lenovo_devices); 1536 1537 static struct hid_driver lenovo_driver = { 1538 .name = "lenovo", 1539 .id_table = lenovo_devices, 1540 .input_configured = lenovo_input_configured, 1541 .input_mapping = lenovo_input_mapping, 1542 .probe = lenovo_probe, 1543 .remove = lenovo_remove, 1544 .raw_event = lenovo_raw_event, 1545 .event = lenovo_event, 1546 .report_fixup = lenovo_report_fixup, 1547 #ifdef CONFIG_PM 1548 .reset_resume = lenovo_reset_resume, 1549 #endif 1550 }; 1551 module_hid_driver(lenovo_driver); 1552 1553 MODULE_DESCRIPTION("HID driver for IBM/Lenovo"); 1554 MODULE_LICENSE("GPL"); 1555