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