Linux Audio

Check our new training course

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