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