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