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_X13_TAB: 509 case USB_DEVICE_ID_LENOVO_X1_TAB: 510 case USB_DEVICE_ID_LENOVO_X1_TAB2: 511 case USB_DEVICE_ID_LENOVO_X1_TAB3: 512 return lenovo_input_mapping_x1_tab_kbd(hdev, hi, field, usage, bit, max); 513 default: 514 return 0; 515 } 516 } 517 518 #undef map_key_clear 519 520 /* Send a config command to the keyboard */ 521 static int lenovo_send_cmd_cptkbd(struct hid_device *hdev, 522 unsigned char byte2, unsigned char byte3) 523 { 524 int ret; 525 unsigned char *buf; 526 527 buf = kzalloc(3, GFP_KERNEL); 528 if (!buf) 529 return -ENOMEM; 530 531 /* 532 * Feature report 0x13 is used for USB, 533 * output report 0x18 is used for Bluetooth. 534 * buf[0] is ignored by hid_hw_raw_request. 535 */ 536 buf[0] = 0x18; 537 buf[1] = byte2; 538 buf[2] = byte3; 539 540 switch (hdev->product) { 541 case USB_DEVICE_ID_LENOVO_CUSBKBD: 542 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 543 ret = hid_hw_raw_request(hdev, 0x13, buf, 3, 544 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 545 break; 546 case USB_DEVICE_ID_LENOVO_CBTKBD: 547 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 548 ret = hid_hw_output_report(hdev, buf, 3); 549 break; 550 default: 551 ret = -EINVAL; 552 break; 553 } 554 555 kfree(buf); 556 557 return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */ 558 } 559 560 static void lenovo_features_set_cptkbd(struct hid_device *hdev) 561 { 562 int ret; 563 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 564 565 /* 566 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into 567 * regular keys (Compact only) 568 */ 569 if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD || 570 hdev->product == USB_DEVICE_ID_LENOVO_CBTKBD) { 571 ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03); 572 if (ret) 573 hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret); 574 } 575 576 /* Switch middle button to native mode */ 577 ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01); 578 if (ret) 579 hid_warn(hdev, "Failed to switch middle button: %d\n", ret); 580 581 ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock); 582 if (ret) 583 hid_err(hdev, "Fn-lock setting failed: %d\n", ret); 584 585 ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity); 586 if (ret) 587 hid_err(hdev, "Sensitivity setting failed: %d\n", ret); 588 } 589 590 static ssize_t attr_fn_lock_show(struct device *dev, 591 struct device_attribute *attr, 592 char *buf) 593 { 594 struct hid_device *hdev = to_hid_device(dev); 595 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 596 597 return sysfs_emit(buf, "%u\n", data->fn_lock); 598 } 599 600 static ssize_t attr_fn_lock_store(struct device *dev, 601 struct device_attribute *attr, 602 const char *buf, 603 size_t count) 604 { 605 struct hid_device *hdev = to_hid_device(dev); 606 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 607 int value, ret; 608 609 if (kstrtoint(buf, 10, &value)) 610 return -EINVAL; 611 if (value < 0 || value > 1) 612 return -EINVAL; 613 614 data->fn_lock = !!value; 615 616 switch (hdev->product) { 617 case USB_DEVICE_ID_LENOVO_CUSBKBD: 618 case USB_DEVICE_ID_LENOVO_CBTKBD: 619 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 620 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 621 lenovo_features_set_cptkbd(hdev); 622 break; 623 case USB_DEVICE_ID_LENOVO_X12_TAB: 624 case USB_DEVICE_ID_LENOVO_X12_TAB2: 625 case USB_DEVICE_ID_LENOVO_X13_TAB: 626 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 627 case USB_DEVICE_ID_LENOVO_X1_TAB: 628 case USB_DEVICE_ID_LENOVO_X1_TAB2: 629 case USB_DEVICE_ID_LENOVO_X1_TAB3: 630 ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value); 631 if (ret) 632 return ret; 633 break; 634 } 635 636 return count; 637 } 638 639 static ssize_t attr_sensitivity_show_cptkbd(struct device *dev, 640 struct device_attribute *attr, 641 char *buf) 642 { 643 struct hid_device *hdev = to_hid_device(dev); 644 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 645 646 return sysfs_emit(buf, "%u\n", cptkbd_data->sensitivity); 647 } 648 649 static ssize_t attr_sensitivity_store_cptkbd(struct device *dev, 650 struct device_attribute *attr, 651 const char *buf, 652 size_t count) 653 { 654 struct hid_device *hdev = to_hid_device(dev); 655 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 656 int value; 657 658 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) 659 return -EINVAL; 660 661 cptkbd_data->sensitivity = value; 662 lenovo_features_set_cptkbd(hdev); 663 664 return count; 665 } 666 667 static ssize_t attr_middleclick_workaround_show_cptkbd(struct device *dev, 668 struct device_attribute *attr, 669 char *buf) 670 { 671 struct hid_device *hdev = to_hid_device(dev); 672 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 673 674 return sysfs_emit(buf, "%u\n", 675 cptkbd_data->middleclick_workaround_cptkbd); 676 } 677 678 static ssize_t attr_middleclick_workaround_store_cptkbd(struct device *dev, 679 struct device_attribute *attr, 680 const char *buf, 681 size_t count) 682 { 683 struct hid_device *hdev = to_hid_device(dev); 684 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 685 int value; 686 687 if (kstrtoint(buf, 10, &value)) 688 return -EINVAL; 689 if (value < 0 || value > 1) 690 return -EINVAL; 691 692 cptkbd_data->middleclick_workaround_cptkbd = !!value; 693 694 return count; 695 } 696 697 698 static struct device_attribute dev_attr_fn_lock = 699 __ATTR(fn_lock, S_IWUSR | S_IRUGO, 700 attr_fn_lock_show, 701 attr_fn_lock_store); 702 703 static struct device_attribute dev_attr_sensitivity_cptkbd = 704 __ATTR(sensitivity, S_IWUSR | S_IRUGO, 705 attr_sensitivity_show_cptkbd, 706 attr_sensitivity_store_cptkbd); 707 708 static struct device_attribute dev_attr_middleclick_workaround_cptkbd = 709 __ATTR(middleclick_workaround, S_IWUSR | S_IRUGO, 710 attr_middleclick_workaround_show_cptkbd, 711 attr_middleclick_workaround_store_cptkbd); 712 713 714 static struct attribute *lenovo_attributes_cptkbd[] = { 715 &dev_attr_fn_lock.attr, 716 &dev_attr_sensitivity_cptkbd.attr, 717 &dev_attr_middleclick_workaround_cptkbd.attr, 718 NULL 719 }; 720 721 static const struct attribute_group lenovo_attr_group_cptkbd = { 722 .attrs = lenovo_attributes_cptkbd, 723 }; 724 725 /* Function to handle Lenovo Thinkpad TAB X12's HID raw inputs for fn keys*/ 726 static int lenovo_raw_event_TP_X12_tab(struct hid_device *hdev, u32 raw_data) 727 { 728 struct hid_input *hidinput; 729 struct input_dev *input = NULL; 730 731 /* Iterate through all associated input devices */ 732 list_for_each_entry(hidinput, &hdev->inputs, list) { 733 input = hidinput->input; 734 if (!input) 735 continue; 736 737 switch (raw_data) { 738 /* fn-F20 being used here for MIC mute*/ 739 case TP_X12_RAW_HOTKEY_FN_F4: 740 report_key_event(input, LENOVO_KEY_MICMUTE); 741 return 1; 742 /* Power-mode or Airplane mode will be called based on the device*/ 743 case TP_X12_RAW_HOTKEY_FN_F8: 744 /* 745 * TP X12 TAB uses Fn-F8 calls Airplanemode 746 * Whereas TP X12 TAB2 uses Fn-F8 for toggling 747 * Power modes 748 */ 749 if (hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) { 750 report_key_event(input, KEY_RFKILL); 751 return 1; 752 } 753 report_key_event(input, KEY_PERFORMANCE); 754 return 1; 755 case TP_X12_RAW_HOTKEY_FN_F10: 756 /* TAB1 has PICKUP Phone and TAB2 use Snipping tool*/ 757 (hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) ? 758 report_key_event(input, KEY_PICKUP_PHONE) : 759 report_key_event(input, KEY_SELECTIVE_SCREENSHOT); 760 return 1; 761 case TP_X12_RAW_HOTKEY_FN_F12: 762 /* BookMarks/STAR key*/ 763 report_key_event(input, KEY_BOOKMARKS); 764 return 1; 765 case TP_X12_RAW_HOTKEY_FN_SPACE: 766 /* Keyboard LED backlight toggle*/ 767 report_key_event(input, KEY_KBDILLUMTOGGLE); 768 return 1; 769 default: 770 break; 771 } 772 } 773 return 0; 774 } 775 776 static int lenovo_raw_event(struct hid_device *hdev, 777 struct hid_report *report, u8 *data, int size) 778 { 779 /* 780 * Compact USB keyboard's Fn-F12 report holds down many other keys, and 781 * its own key is outside the usage page range. Remove extra 782 * keypresses and remap to inside usage page. 783 */ 784 if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD 785 && size == 3 786 && data[0] == 0x15 787 && data[1] == 0x94 788 && data[2] == 0x01)) { 789 data[1] = 0x00; 790 data[2] = 0x01; 791 } 792 793 /* 794 * Lenovo TP X12 Tab KBD's Fn+XX is HID raw data defined. Report ID is 0x03 795 * e.g.: Raw data received for MIC mute is 0x00020003. 796 */ 797 if (unlikely((hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB || 798 hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2 || 799 hdev->product == USB_DEVICE_ID_LENOVO_X13_TAB) && 800 size >= 4 && report->id == 0x03)) 801 return lenovo_raw_event_TP_X12_tab(hdev, get_unaligned_le32(data)); 802 803 return 0; 804 } 805 806 static int lenovo_event_tp10ubkbd(struct hid_device *hdev, 807 struct hid_field *field, struct hid_usage *usage, __s32 value) 808 { 809 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 810 811 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) { 812 /* 813 * The user has toggled the Fn-lock state. Toggle our own 814 * cached value of it and sync our value to the keyboard to 815 * ensure things are in sync (the sycning should be a no-op). 816 */ 817 data->fn_lock = !data->fn_lock; 818 schedule_work(&data->fn_lock_sync_work); 819 } 820 821 return 0; 822 } 823 824 static int lenovo_event_cptkbd(struct hid_device *hdev, 825 struct hid_field *field, struct hid_usage *usage, __s32 value) 826 { 827 struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); 828 829 if (cptkbd_data->middleclick_workaround_cptkbd) { 830 /* "wheel" scroll events */ 831 if (usage->type == EV_REL && (usage->code == REL_WHEEL || 832 usage->code == REL_HWHEEL)) { 833 /* Scroll events disable middle-click event */ 834 cptkbd_data->middlebutton_state = 2; 835 return 0; 836 } 837 838 /* Middle click events */ 839 if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) { 840 if (value == 1) { 841 cptkbd_data->middlebutton_state = 1; 842 } else if (value == 0) { 843 if (cptkbd_data->middlebutton_state == 1) { 844 /* No scrolling inbetween, send middle-click */ 845 input_event(field->hidinput->input, 846 EV_KEY, BTN_MIDDLE, 1); 847 input_sync(field->hidinput->input); 848 input_event(field->hidinput->input, 849 EV_KEY, BTN_MIDDLE, 0); 850 input_sync(field->hidinput->input); 851 } 852 cptkbd_data->middlebutton_state = 0; 853 } 854 return 1; 855 } 856 } 857 858 if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) { 859 /* 860 * The user has toggled the Fn-lock state. Toggle our own 861 * cached value of it and sync our value to the keyboard to 862 * ensure things are in sync (the syncing should be a no-op). 863 */ 864 cptkbd_data->fn_lock = !cptkbd_data->fn_lock; 865 } 866 867 return 0; 868 } 869 870 static int lenovo_event(struct hid_device *hdev, struct hid_field *field, 871 struct hid_usage *usage, __s32 value) 872 { 873 if (!hid_get_drvdata(hdev)) 874 return 0; 875 876 switch (hdev->product) { 877 case USB_DEVICE_ID_LENOVO_CUSBKBD: 878 case USB_DEVICE_ID_LENOVO_CBTKBD: 879 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 880 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 881 return lenovo_event_cptkbd(hdev, field, usage, value); 882 case USB_DEVICE_ID_LENOVO_X12_TAB: 883 case USB_DEVICE_ID_LENOVO_X12_TAB2: 884 case USB_DEVICE_ID_LENOVO_X13_TAB: 885 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 886 case USB_DEVICE_ID_LENOVO_X1_TAB: 887 case USB_DEVICE_ID_LENOVO_X1_TAB2: 888 case USB_DEVICE_ID_LENOVO_X1_TAB3: 889 return lenovo_event_tp10ubkbd(hdev, field, usage, value); 890 default: 891 return 0; 892 } 893 } 894 895 static int lenovo_features_set_tpkbd(struct hid_device *hdev) 896 { 897 struct hid_report *report; 898 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 899 900 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4]; 901 902 report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02; 903 report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08; 904 report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20; 905 report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40; 906 report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver 907 report->field[2]->value[0] = data_pointer->sensitivity; 908 report->field[3]->value[0] = data_pointer->press_speed; 909 910 hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 911 return 0; 912 } 913 914 static ssize_t attr_press_to_select_show_tpkbd(struct device *dev, 915 struct device_attribute *attr, 916 char *buf) 917 { 918 struct hid_device *hdev = to_hid_device(dev); 919 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 920 921 return sysfs_emit(buf, "%u\n", data_pointer->press_to_select); 922 } 923 924 static ssize_t attr_press_to_select_store_tpkbd(struct device *dev, 925 struct device_attribute *attr, 926 const char *buf, 927 size_t count) 928 { 929 struct hid_device *hdev = to_hid_device(dev); 930 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 931 int value; 932 933 if (kstrtoint(buf, 10, &value)) 934 return -EINVAL; 935 if (value < 0 || value > 1) 936 return -EINVAL; 937 938 data_pointer->press_to_select = value; 939 lenovo_features_set_tpkbd(hdev); 940 941 return count; 942 } 943 944 static ssize_t attr_dragging_show_tpkbd(struct device *dev, 945 struct device_attribute *attr, 946 char *buf) 947 { 948 struct hid_device *hdev = to_hid_device(dev); 949 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 950 951 return sysfs_emit(buf, "%u\n", data_pointer->dragging); 952 } 953 954 static ssize_t attr_dragging_store_tpkbd(struct device *dev, 955 struct device_attribute *attr, 956 const char *buf, 957 size_t count) 958 { 959 struct hid_device *hdev = to_hid_device(dev); 960 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 961 int value; 962 963 if (kstrtoint(buf, 10, &value)) 964 return -EINVAL; 965 if (value < 0 || value > 1) 966 return -EINVAL; 967 968 data_pointer->dragging = value; 969 lenovo_features_set_tpkbd(hdev); 970 971 return count; 972 } 973 974 static ssize_t attr_release_to_select_show_tpkbd(struct device *dev, 975 struct device_attribute *attr, 976 char *buf) 977 { 978 struct hid_device *hdev = to_hid_device(dev); 979 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 980 981 return sysfs_emit(buf, "%u\n", data_pointer->release_to_select); 982 } 983 984 static ssize_t attr_release_to_select_store_tpkbd(struct device *dev, 985 struct device_attribute *attr, 986 const char *buf, 987 size_t count) 988 { 989 struct hid_device *hdev = to_hid_device(dev); 990 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 991 int value; 992 993 if (kstrtoint(buf, 10, &value)) 994 return -EINVAL; 995 if (value < 0 || value > 1) 996 return -EINVAL; 997 998 data_pointer->release_to_select = value; 999 lenovo_features_set_tpkbd(hdev); 1000 1001 return count; 1002 } 1003 1004 static ssize_t attr_select_right_show_tpkbd(struct device *dev, 1005 struct device_attribute *attr, 1006 char *buf) 1007 { 1008 struct hid_device *hdev = to_hid_device(dev); 1009 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1010 1011 return sysfs_emit(buf, "%u\n", data_pointer->select_right); 1012 } 1013 1014 static ssize_t attr_select_right_store_tpkbd(struct device *dev, 1015 struct device_attribute *attr, 1016 const char *buf, 1017 size_t count) 1018 { 1019 struct hid_device *hdev = to_hid_device(dev); 1020 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1021 int value; 1022 1023 if (kstrtoint(buf, 10, &value)) 1024 return -EINVAL; 1025 if (value < 0 || value > 1) 1026 return -EINVAL; 1027 1028 data_pointer->select_right = value; 1029 lenovo_features_set_tpkbd(hdev); 1030 1031 return count; 1032 } 1033 1034 static ssize_t attr_sensitivity_show_tpkbd(struct device *dev, 1035 struct device_attribute *attr, 1036 char *buf) 1037 { 1038 struct hid_device *hdev = to_hid_device(dev); 1039 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1040 1041 return sysfs_emit(buf, "%u\n", data_pointer->sensitivity); 1042 } 1043 1044 static ssize_t attr_sensitivity_store_tpkbd(struct device *dev, 1045 struct device_attribute *attr, 1046 const char *buf, 1047 size_t count) 1048 { 1049 struct hid_device *hdev = to_hid_device(dev); 1050 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1051 int value; 1052 1053 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) 1054 return -EINVAL; 1055 1056 data_pointer->sensitivity = value; 1057 lenovo_features_set_tpkbd(hdev); 1058 1059 return count; 1060 } 1061 1062 static ssize_t attr_press_speed_show_tpkbd(struct device *dev, 1063 struct device_attribute *attr, 1064 char *buf) 1065 { 1066 struct hid_device *hdev = to_hid_device(dev); 1067 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1068 1069 return sysfs_emit(buf, "%u\n", data_pointer->press_speed); 1070 } 1071 1072 static ssize_t attr_press_speed_store_tpkbd(struct device *dev, 1073 struct device_attribute *attr, 1074 const char *buf, 1075 size_t count) 1076 { 1077 struct hid_device *hdev = to_hid_device(dev); 1078 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1079 int value; 1080 1081 if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) 1082 return -EINVAL; 1083 1084 data_pointer->press_speed = value; 1085 lenovo_features_set_tpkbd(hdev); 1086 1087 return count; 1088 } 1089 1090 static struct device_attribute dev_attr_press_to_select_tpkbd = 1091 __ATTR(press_to_select, S_IWUSR | S_IRUGO, 1092 attr_press_to_select_show_tpkbd, 1093 attr_press_to_select_store_tpkbd); 1094 1095 static struct device_attribute dev_attr_dragging_tpkbd = 1096 __ATTR(dragging, S_IWUSR | S_IRUGO, 1097 attr_dragging_show_tpkbd, 1098 attr_dragging_store_tpkbd); 1099 1100 static struct device_attribute dev_attr_release_to_select_tpkbd = 1101 __ATTR(release_to_select, S_IWUSR | S_IRUGO, 1102 attr_release_to_select_show_tpkbd, 1103 attr_release_to_select_store_tpkbd); 1104 1105 static struct device_attribute dev_attr_select_right_tpkbd = 1106 __ATTR(select_right, S_IWUSR | S_IRUGO, 1107 attr_select_right_show_tpkbd, 1108 attr_select_right_store_tpkbd); 1109 1110 static struct device_attribute dev_attr_sensitivity_tpkbd = 1111 __ATTR(sensitivity, S_IWUSR | S_IRUGO, 1112 attr_sensitivity_show_tpkbd, 1113 attr_sensitivity_store_tpkbd); 1114 1115 static struct device_attribute dev_attr_press_speed_tpkbd = 1116 __ATTR(press_speed, S_IWUSR | S_IRUGO, 1117 attr_press_speed_show_tpkbd, 1118 attr_press_speed_store_tpkbd); 1119 1120 static struct attribute *lenovo_attributes_tpkbd[] = { 1121 &dev_attr_press_to_select_tpkbd.attr, 1122 &dev_attr_dragging_tpkbd.attr, 1123 &dev_attr_release_to_select_tpkbd.attr, 1124 &dev_attr_select_right_tpkbd.attr, 1125 &dev_attr_sensitivity_tpkbd.attr, 1126 &dev_attr_press_speed_tpkbd.attr, 1127 NULL 1128 }; 1129 1130 static const struct attribute_group lenovo_attr_group_tpkbd = { 1131 .attrs = lenovo_attributes_tpkbd, 1132 }; 1133 1134 static void lenovo_led_set_tpkbd(struct hid_device *hdev) 1135 { 1136 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1137 struct hid_report *report; 1138 1139 report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3]; 1140 report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1; 1141 report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1; 1142 hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 1143 } 1144 1145 static int lenovo_led_brightness_set(struct led_classdev *led_cdev, 1146 enum led_brightness value) 1147 { 1148 struct device *dev = led_cdev->dev->parent; 1149 struct hid_device *hdev = to_hid_device(dev); 1150 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1151 static const u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED }; 1152 int led_nr = 0; 1153 int ret = 0; 1154 1155 if (led_cdev == &data_pointer->led_micmute) 1156 led_nr = 1; 1157 1158 if (value == LED_OFF) 1159 data_pointer->led_state &= ~(1 << led_nr); 1160 else 1161 data_pointer->led_state |= 1 << led_nr; 1162 1163 switch (hdev->product) { 1164 case USB_DEVICE_ID_LENOVO_TPKBD: 1165 lenovo_led_set_tpkbd(hdev); 1166 break; 1167 case USB_DEVICE_ID_LENOVO_X12_TAB: 1168 case USB_DEVICE_ID_LENOVO_X12_TAB2: 1169 case USB_DEVICE_ID_LENOVO_X13_TAB: 1170 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 1171 case USB_DEVICE_ID_LENOVO_X1_TAB: 1172 case USB_DEVICE_ID_LENOVO_X1_TAB2: 1173 case USB_DEVICE_ID_LENOVO_X1_TAB3: 1174 ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value); 1175 break; 1176 } 1177 1178 return ret; 1179 } 1180 1181 static int lenovo_register_leds(struct hid_device *hdev) 1182 { 1183 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 1184 size_t name_sz = strlen(dev_name(&hdev->dev)) + 16; 1185 char *name_mute, *name_micm; 1186 int ret; 1187 1188 name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL); 1189 name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL); 1190 if (name_mute == NULL || name_micm == NULL) { 1191 hid_err(hdev, "Could not allocate memory for led data\n"); 1192 return -ENOMEM; 1193 } 1194 snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev)); 1195 snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev)); 1196 1197 data->led_mute.name = name_mute; 1198 data->led_mute.default_trigger = "audio-mute"; 1199 data->led_mute.brightness_set_blocking = lenovo_led_brightness_set; 1200 data->led_mute.max_brightness = 1; 1201 data->led_mute.flags = LED_HW_PLUGGABLE; 1202 data->led_mute.dev = &hdev->dev; 1203 ret = led_classdev_register(&hdev->dev, &data->led_mute); 1204 if (ret < 0) 1205 return ret; 1206 1207 data->led_micmute.name = name_micm; 1208 data->led_micmute.default_trigger = "audio-micmute"; 1209 data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set; 1210 data->led_micmute.max_brightness = 1; 1211 data->led_micmute.flags = LED_HW_PLUGGABLE; 1212 data->led_micmute.dev = &hdev->dev; 1213 ret = led_classdev_register(&hdev->dev, &data->led_micmute); 1214 if (ret < 0) { 1215 led_classdev_unregister(&data->led_mute); 1216 return ret; 1217 } 1218 1219 return 0; 1220 } 1221 1222 static int lenovo_probe_tpkbd(struct hid_device *hdev) 1223 { 1224 struct lenovo_drvdata *data_pointer; 1225 int i, ret; 1226 1227 /* 1228 * Only register extra settings against subdevice where input_mapping 1229 * set drvdata to 1, i.e. the trackpoint. 1230 */ 1231 if (!hid_get_drvdata(hdev)) 1232 return 0; 1233 1234 hid_set_drvdata(hdev, NULL); 1235 1236 /* Validate required reports. */ 1237 for (i = 0; i < 4; i++) { 1238 if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1)) 1239 return -ENODEV; 1240 } 1241 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2)) 1242 return -ENODEV; 1243 1244 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd); 1245 if (ret) 1246 hid_warn(hdev, "Could not create sysfs group: %d\n", ret); 1247 1248 data_pointer = devm_kzalloc(&hdev->dev, 1249 sizeof(struct lenovo_drvdata), 1250 GFP_KERNEL); 1251 if (data_pointer == NULL) { 1252 hid_err(hdev, "Could not allocate memory for driver data\n"); 1253 ret = -ENOMEM; 1254 goto err; 1255 } 1256 1257 // set same default values as windows driver 1258 data_pointer->sensitivity = 0xa0; 1259 data_pointer->press_speed = 0x38; 1260 1261 hid_set_drvdata(hdev, data_pointer); 1262 1263 ret = lenovo_register_leds(hdev); 1264 if (ret) 1265 goto err; 1266 1267 lenovo_features_set_tpkbd(hdev); 1268 1269 return 0; 1270 err: 1271 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd); 1272 return ret; 1273 } 1274 1275 static int lenovo_probe_cptkbd(struct hid_device *hdev) 1276 { 1277 int ret; 1278 struct lenovo_drvdata *cptkbd_data; 1279 1280 /* All the custom action happens on the USBMOUSE device for USB */ 1281 if (((hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD) || 1282 (hdev->product == USB_DEVICE_ID_LENOVO_TPIIUSBKBD)) && 1283 hdev->type != HID_TYPE_USBMOUSE) { 1284 hid_dbg(hdev, "Ignoring keyboard half of device\n"); 1285 return 0; 1286 } 1287 1288 cptkbd_data = devm_kzalloc(&hdev->dev, 1289 sizeof(*cptkbd_data), 1290 GFP_KERNEL); 1291 if (cptkbd_data == NULL) { 1292 hid_err(hdev, "can't alloc keyboard descriptor\n"); 1293 return -ENOMEM; 1294 } 1295 hid_set_drvdata(hdev, cptkbd_data); 1296 1297 /* Set keyboard settings to known state */ 1298 cptkbd_data->middlebutton_state = 0; 1299 cptkbd_data->fn_lock = true; 1300 cptkbd_data->sensitivity = 0x05; 1301 cptkbd_data->middleclick_workaround_cptkbd = true; 1302 lenovo_features_set_cptkbd(hdev); 1303 1304 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd); 1305 if (ret) 1306 hid_warn(hdev, "Could not create sysfs group: %d\n", ret); 1307 1308 return 0; 1309 } 1310 1311 static struct attribute *lenovo_attributes_tp10ubkbd[] = { 1312 &dev_attr_fn_lock.attr, 1313 NULL 1314 }; 1315 1316 static const struct attribute_group lenovo_attr_group_tp10ubkbd = { 1317 .attrs = lenovo_attributes_tp10ubkbd, 1318 }; 1319 1320 static int lenovo_probe_tp10ubkbd(struct hid_device *hdev) 1321 { 1322 struct hid_report_enum *rep_enum; 1323 struct lenovo_drvdata *data; 1324 struct hid_report *rep; 1325 bool found; 1326 int ret; 1327 1328 /* 1329 * The LEDs and the Fn-lock functionality use output report 9, 1330 * with an application of 0xffa0001, add the LEDs on the interface 1331 * with this output report. 1332 */ 1333 found = false; 1334 rep_enum = &hdev->report_enum[HID_OUTPUT_REPORT]; 1335 list_for_each_entry(rep, &rep_enum->report_list, list) { 1336 if (rep->application == 0xffa00001) 1337 found = true; 1338 } 1339 if (!found) 1340 return 0; 1341 1342 data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL); 1343 if (!data) 1344 return -ENOMEM; 1345 1346 mutex_init(&data->led_report_mutex); 1347 INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock); 1348 data->hdev = hdev; 1349 1350 hid_set_drvdata(hdev, data); 1351 1352 /* 1353 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on. 1354 * We cannot read the state, only set it, so we force it to on here 1355 * (which should be a no-op) to make sure that our state matches the 1356 * keyboard's FN-lock state. This is the same as what Windows does. 1357 * 1358 * For X12 TAB and TAB2, the default windows behaviour Fn-lock Off. 1359 * Adding additional check to ensure the behaviour in case of 1360 * Thinkpad X12 Tabs. 1361 */ 1362 1363 data->fn_lock = !(hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB || 1364 hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2 || 1365 hdev->product == USB_DEVICE_ID_LENOVO_X13_TAB); 1366 1367 lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock); 1368 1369 ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd); 1370 if (ret) 1371 return ret; 1372 1373 ret = lenovo_register_leds(hdev); 1374 if (ret) 1375 goto err; 1376 1377 return 0; 1378 err: 1379 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd); 1380 return ret; 1381 } 1382 1383 static int lenovo_probe(struct hid_device *hdev, 1384 const struct hid_device_id *id) 1385 { 1386 int ret; 1387 1388 ret = hid_parse(hdev); 1389 if (ret) { 1390 hid_err(hdev, "hid_parse failed\n"); 1391 goto err; 1392 } 1393 1394 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 1395 if (ret) { 1396 hid_err(hdev, "hid_hw_start failed\n"); 1397 goto err; 1398 } 1399 1400 switch (hdev->product) { 1401 case USB_DEVICE_ID_LENOVO_TPKBD: 1402 ret = lenovo_probe_tpkbd(hdev); 1403 break; 1404 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1405 case USB_DEVICE_ID_LENOVO_CBTKBD: 1406 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1407 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 1408 ret = lenovo_probe_cptkbd(hdev); 1409 break; 1410 case USB_DEVICE_ID_LENOVO_X12_TAB: 1411 case USB_DEVICE_ID_LENOVO_X12_TAB2: 1412 case USB_DEVICE_ID_LENOVO_X13_TAB: 1413 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 1414 case USB_DEVICE_ID_LENOVO_X1_TAB: 1415 case USB_DEVICE_ID_LENOVO_X1_TAB2: 1416 case USB_DEVICE_ID_LENOVO_X1_TAB3: 1417 ret = lenovo_probe_tp10ubkbd(hdev); 1418 break; 1419 default: 1420 ret = 0; 1421 break; 1422 } 1423 if (ret) 1424 goto err_hid; 1425 1426 return 0; 1427 err_hid: 1428 hid_hw_stop(hdev); 1429 err: 1430 return ret; 1431 } 1432 1433 static int lenovo_reset_resume(struct hid_device *hdev) 1434 { 1435 switch (hdev->product) { 1436 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1437 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1438 if (hdev->type == HID_TYPE_USBMOUSE) 1439 lenovo_features_set_cptkbd(hdev); 1440 1441 break; 1442 default: 1443 break; 1444 } 1445 1446 return 0; 1447 } 1448 1449 static void lenovo_remove_tpkbd(struct hid_device *hdev) 1450 { 1451 struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev); 1452 1453 /* 1454 * Only the trackpoint half of the keyboard has drvdata and stuff that 1455 * needs unregistering. 1456 */ 1457 if (data_pointer == NULL) 1458 return; 1459 1460 sysfs_remove_group(&hdev->dev.kobj, 1461 &lenovo_attr_group_tpkbd); 1462 1463 led_classdev_unregister(&data_pointer->led_micmute); 1464 led_classdev_unregister(&data_pointer->led_mute); 1465 } 1466 1467 static void lenovo_remove_cptkbd(struct hid_device *hdev) 1468 { 1469 sysfs_remove_group(&hdev->dev.kobj, 1470 &lenovo_attr_group_cptkbd); 1471 } 1472 1473 static void lenovo_remove_tp10ubkbd(struct hid_device *hdev) 1474 { 1475 struct lenovo_drvdata *data = hid_get_drvdata(hdev); 1476 1477 if (data == NULL) 1478 return; 1479 1480 led_classdev_unregister(&data->led_micmute); 1481 led_classdev_unregister(&data->led_mute); 1482 1483 sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd); 1484 cancel_work_sync(&data->fn_lock_sync_work); 1485 } 1486 1487 static void lenovo_remove(struct hid_device *hdev) 1488 { 1489 switch (hdev->product) { 1490 case USB_DEVICE_ID_LENOVO_TPKBD: 1491 lenovo_remove_tpkbd(hdev); 1492 break; 1493 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1494 case USB_DEVICE_ID_LENOVO_CBTKBD: 1495 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1496 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 1497 lenovo_remove_cptkbd(hdev); 1498 break; 1499 case USB_DEVICE_ID_LENOVO_X12_TAB: 1500 case USB_DEVICE_ID_LENOVO_X12_TAB2: 1501 case USB_DEVICE_ID_LENOVO_X13_TAB: 1502 case USB_DEVICE_ID_LENOVO_TP10UBKBD: 1503 case USB_DEVICE_ID_LENOVO_X1_TAB: 1504 case USB_DEVICE_ID_LENOVO_X1_TAB2: 1505 case USB_DEVICE_ID_LENOVO_X1_TAB3: 1506 lenovo_remove_tp10ubkbd(hdev); 1507 break; 1508 } 1509 1510 hid_hw_stop(hdev); 1511 } 1512 1513 static int lenovo_input_configured(struct hid_device *hdev, 1514 struct hid_input *hi) 1515 { 1516 switch (hdev->product) { 1517 case USB_DEVICE_ID_LENOVO_TPKBD: 1518 case USB_DEVICE_ID_LENOVO_CUSBKBD: 1519 case USB_DEVICE_ID_LENOVO_CBTKBD: 1520 case USB_DEVICE_ID_LENOVO_TPIIUSBKBD: 1521 case USB_DEVICE_ID_LENOVO_TPIIBTKBD: 1522 if (test_bit(EV_REL, hi->input->evbit)) { 1523 /* set only for trackpoint device */ 1524 __set_bit(INPUT_PROP_POINTER, hi->input->propbit); 1525 __set_bit(INPUT_PROP_POINTING_STICK, 1526 hi->input->propbit); 1527 } 1528 break; 1529 case USB_DEVICE_ID_LENOVO_X12_TAB2: 1530 case USB_DEVICE_ID_LENOVO_X13_TAB: 1531 input_set_capability(hi->input, EV_KEY, KEY_PERFORMANCE); 1532 break; 1533 } 1534 1535 return 0; 1536 } 1537 1538 1539 static const struct hid_device_id lenovo_devices[] = { 1540 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) }, 1541 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) }, 1542 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIUSBKBD) }, 1543 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) }, 1544 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPIIBTKBD) }, 1545 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) }, 1546 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) }, 1547 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) }, 1548 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) }, 1549 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) }, 1550 { HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) }, 1551 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) }, 1552 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) }, 1553 /* 1554 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard 1555 * part, while letting hid-multitouch.c handle the touchpad and trackpoint. 1556 */ 1557 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1558 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB) }, 1559 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1560 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB2) }, 1561 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1562 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB3) }, 1563 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1564 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB) }, 1565 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1566 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB2) }, 1567 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1568 USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X13_TAB) }, 1569 { HID_DEVICE(BUS_I2C, HID_GROUP_GENERIC, 1570 USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_YOGA_SLIM_7X_KEYBOARD) }, 1571 { } 1572 }; 1573 1574 MODULE_DEVICE_TABLE(hid, lenovo_devices); 1575 1576 static struct hid_driver lenovo_driver = { 1577 .name = "lenovo", 1578 .id_table = lenovo_devices, 1579 .input_configured = lenovo_input_configured, 1580 .input_mapping = lenovo_input_mapping, 1581 .probe = lenovo_probe, 1582 .remove = lenovo_remove, 1583 .raw_event = lenovo_raw_event, 1584 .event = lenovo_event, 1585 .report_fixup = lenovo_report_fixup, 1586 .reset_resume = pm_ptr(lenovo_reset_resume), 1587 }; 1588 module_hid_driver(lenovo_driver); 1589 1590 MODULE_DESCRIPTION("HID driver for IBM/Lenovo"); 1591 MODULE_LICENSE("GPL"); 1592