Linux Audio

Check our new training course

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