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