xref: /openbmc/linux/drivers/hid/hid-microsoft.c (revision d2574c33)
1 /*
2  *  HID driver for some microsoft "special" devices
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7  *  Copyright (c) 2006-2007 Jiri Kosina
8  *  Copyright (c) 2008 Jiri Slaby
9  */
10 
11 /*
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 2 of the License, or (at your option)
15  * any later version.
16  */
17 
18 #include <linux/device.h>
19 #include <linux/input.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 
23 #include "hid-ids.h"
24 
25 #define MS_HIDINPUT		BIT(0)
26 #define MS_ERGONOMY		BIT(1)
27 #define MS_PRESENTER		BIT(2)
28 #define MS_RDESC		BIT(3)
29 #define MS_NOGET		BIT(4)
30 #define MS_DUPLICATE_USAGES	BIT(5)
31 #define MS_SURFACE_DIAL		BIT(6)
32 #define MS_QUIRK_FF		BIT(7)
33 
34 struct ms_data {
35 	unsigned long quirks;
36 	struct hid_device *hdev;
37 	struct work_struct ff_worker;
38 	__u8 strong;
39 	__u8 weak;
40 	void *output_report_dmabuf;
41 };
42 
43 #define XB1S_FF_REPORT		3
44 #define ENABLE_WEAK		BIT(0)
45 #define ENABLE_STRONG		BIT(1)
46 
47 enum {
48 	MAGNITUDE_STRONG = 2,
49 	MAGNITUDE_WEAK,
50 	MAGNITUDE_NUM
51 };
52 
53 struct xb1s_ff_report {
54 	__u8	report_id;
55 	__u8	enable;
56 	__u8	magnitude[MAGNITUDE_NUM];
57 	__u8	duration_10ms;
58 	__u8	start_delay_10ms;
59 	__u8	loop_count;
60 } __packed;
61 
62 static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
63 		unsigned int *rsize)
64 {
65 	struct ms_data *ms = hid_get_drvdata(hdev);
66 	unsigned long quirks = ms->quirks;
67 
68 	/*
69 	 * Microsoft Wireless Desktop Receiver (Model 1028) has
70 	 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
71 	 */
72 	if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
73 			rdesc[559] == 0x29) {
74 		hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
75 		rdesc[557] = 0x35;
76 		rdesc[559] = 0x45;
77 	}
78 	return rdesc;
79 }
80 
81 #define ms_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, max, \
82 					EV_KEY, (c))
83 static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
84 		unsigned long **bit, int *max)
85 {
86 	struct input_dev *input = hi->input;
87 
88 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
89 		switch (usage->hid & HID_USAGE) {
90 		/*
91 		 * Microsoft uses these 2 reserved usage ids for 2 keys on
92 		 * the MS office kb labelled "Office Home" and "Task Pane".
93 		 */
94 		case 0x29d:
95 			ms_map_key_clear(KEY_PROG1);
96 			return 1;
97 		case 0x29e:
98 			ms_map_key_clear(KEY_PROG2);
99 			return 1;
100 		}
101 		return 0;
102 	}
103 
104 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
105 		return 0;
106 
107 	switch (usage->hid & HID_USAGE) {
108 	case 0xfd06: ms_map_key_clear(KEY_CHAT);	break;
109 	case 0xfd07: ms_map_key_clear(KEY_PHONE);	break;
110 	case 0xff00:
111 		/* Special keypad keys */
112 		ms_map_key_clear(KEY_KPEQUAL);
113 		set_bit(KEY_KPLEFTPAREN, input->keybit);
114 		set_bit(KEY_KPRIGHTPAREN, input->keybit);
115 		break;
116 	case 0xff01:
117 		/* Scroll wheel */
118 		hid_map_usage_clear(hi, usage, bit, max, EV_REL, REL_WHEEL);
119 		break;
120 	case 0xff02:
121 		/*
122 		 * This byte contains a copy of the modifier keys byte of a
123 		 * standard hid keyboard report, as send by interface 0
124 		 * (this usage is found on interface 1).
125 		 *
126 		 * This byte only gets send when another key in the same report
127 		 * changes state, and as such is useless, ignore it.
128 		 */
129 		return -1;
130 	case 0xff05:
131 		set_bit(EV_REP, input->evbit);
132 		ms_map_key_clear(KEY_F13);
133 		set_bit(KEY_F14, input->keybit);
134 		set_bit(KEY_F15, input->keybit);
135 		set_bit(KEY_F16, input->keybit);
136 		set_bit(KEY_F17, input->keybit);
137 		set_bit(KEY_F18, input->keybit);
138 		break;
139 	default:
140 		return 0;
141 	}
142 	return 1;
143 }
144 
145 static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
146 		unsigned long **bit, int *max)
147 {
148 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
149 		return 0;
150 
151 	set_bit(EV_REP, hi->input->evbit);
152 	switch (usage->hid & HID_USAGE) {
153 	case 0xfd08: ms_map_key_clear(KEY_FORWARD);	break;
154 	case 0xfd09: ms_map_key_clear(KEY_BACK);	break;
155 	case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE);	break;
156 	case 0xfd0e: ms_map_key_clear(KEY_CLOSE);	break;
157 	case 0xfd0f: ms_map_key_clear(KEY_PLAY);	break;
158 	default:
159 		return 0;
160 	}
161 	return 1;
162 }
163 
164 static int ms_surface_dial_quirk(struct hid_input *hi, struct hid_field *field,
165 		struct hid_usage *usage, unsigned long **bit, int *max)
166 {
167 	switch (usage->hid & HID_USAGE_PAGE) {
168 	case 0xff070000:
169 		/* fall-through */
170 	case HID_UP_DIGITIZER:
171 		/* ignore those axis */
172 		return -1;
173 	case HID_UP_GENDESK:
174 		switch (usage->hid) {
175 		case HID_GD_X:
176 			/* fall-through */
177 		case HID_GD_Y:
178 			/* fall-through */
179 		case HID_GD_RFKILL_BTN:
180 			/* ignore those axis */
181 			return -1;
182 		}
183 	}
184 
185 	return 0;
186 }
187 
188 static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
189 		struct hid_field *field, struct hid_usage *usage,
190 		unsigned long **bit, int *max)
191 {
192 	struct ms_data *ms = hid_get_drvdata(hdev);
193 	unsigned long quirks = ms->quirks;
194 
195 	if (quirks & MS_ERGONOMY) {
196 		int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
197 		if (ret)
198 			return ret;
199 	}
200 
201 	if ((quirks & MS_PRESENTER) &&
202 			ms_presenter_8k_quirk(hi, usage, bit, max))
203 		return 1;
204 
205 	if (quirks & MS_SURFACE_DIAL) {
206 		int ret = ms_surface_dial_quirk(hi, field, usage, bit, max);
207 
208 		if (ret)
209 			return ret;
210 	}
211 
212 	return 0;
213 }
214 
215 static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi,
216 		struct hid_field *field, struct hid_usage *usage,
217 		unsigned long **bit, int *max)
218 {
219 	struct ms_data *ms = hid_get_drvdata(hdev);
220 	unsigned long quirks = ms->quirks;
221 
222 	if (quirks & MS_DUPLICATE_USAGES)
223 		clear_bit(usage->code, *bit);
224 
225 	return 0;
226 }
227 
228 static int ms_event(struct hid_device *hdev, struct hid_field *field,
229 		struct hid_usage *usage, __s32 value)
230 {
231 	struct ms_data *ms = hid_get_drvdata(hdev);
232 	unsigned long quirks = ms->quirks;
233 	struct input_dev *input;
234 
235 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
236 			!usage->type)
237 		return 0;
238 
239 	input = field->hidinput->input;
240 
241 	/* Handling MS keyboards special buttons */
242 	if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff00)) {
243 		/* Special keypad keys */
244 		input_report_key(input, KEY_KPEQUAL, value & 0x01);
245 		input_report_key(input, KEY_KPLEFTPAREN, value & 0x02);
246 		input_report_key(input, KEY_KPRIGHTPAREN, value & 0x04);
247 		return 1;
248 	}
249 
250 	if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff01)) {
251 		/* Scroll wheel */
252 		int step = ((value & 0x60) >> 5) + 1;
253 
254 		switch (value & 0x1f) {
255 		case 0x01:
256 			input_report_rel(input, REL_WHEEL, step);
257 			break;
258 		case 0x1f:
259 			input_report_rel(input, REL_WHEEL, -step);
260 			break;
261 		}
262 		return 1;
263 	}
264 
265 	if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
266 		static unsigned int last_key = 0;
267 		unsigned int key = 0;
268 		switch (value) {
269 		case 0x01: key = KEY_F14; break;
270 		case 0x02: key = KEY_F15; break;
271 		case 0x04: key = KEY_F16; break;
272 		case 0x08: key = KEY_F17; break;
273 		case 0x10: key = KEY_F18; break;
274 		}
275 		if (key) {
276 			input_event(input, usage->type, key, 1);
277 			last_key = key;
278 		} else
279 			input_event(input, usage->type, last_key, 0);
280 
281 		return 1;
282 	}
283 
284 	return 0;
285 }
286 
287 static void ms_ff_worker(struct work_struct *work)
288 {
289 	struct ms_data *ms = container_of(work, struct ms_data, ff_worker);
290 	struct hid_device *hdev = ms->hdev;
291 	struct xb1s_ff_report *r = ms->output_report_dmabuf;
292 	int ret;
293 
294 	memset(r, 0, sizeof(*r));
295 
296 	r->report_id = XB1S_FF_REPORT;
297 	r->enable = ENABLE_WEAK | ENABLE_STRONG;
298 	/*
299 	 * Specifying maximum duration and maximum loop count should
300 	 * cover maximum duration of a single effect, which is 65536
301 	 * ms
302 	 */
303 	r->duration_10ms = U8_MAX;
304 	r->loop_count = U8_MAX;
305 	r->magnitude[MAGNITUDE_STRONG] = ms->strong; /* left actuator */
306 	r->magnitude[MAGNITUDE_WEAK] = ms->weak;     /* right actuator */
307 
308 	ret = hid_hw_output_report(hdev, (__u8 *)r, sizeof(*r));
309 	if (ret)
310 		hid_warn(hdev, "failed to send FF report\n");
311 }
312 
313 static int ms_play_effect(struct input_dev *dev, void *data,
314 			  struct ff_effect *effect)
315 {
316 	struct hid_device *hid = input_get_drvdata(dev);
317 	struct ms_data *ms = hid_get_drvdata(hid);
318 
319 	if (effect->type != FF_RUMBLE)
320 		return 0;
321 
322 	/*
323 	 * Magnitude is 0..100 so scale the 16-bit input here
324 	 */
325 	ms->strong = ((u32) effect->u.rumble.strong_magnitude * 100) / U16_MAX;
326 	ms->weak = ((u32) effect->u.rumble.weak_magnitude * 100) / U16_MAX;
327 
328 	schedule_work(&ms->ff_worker);
329 	return 0;
330 }
331 
332 static int ms_init_ff(struct hid_device *hdev)
333 {
334 	struct hid_input *hidinput = list_entry(hdev->inputs.next,
335 						struct hid_input, list);
336 	struct input_dev *input_dev = hidinput->input;
337 	struct ms_data *ms = hid_get_drvdata(hdev);
338 
339 	if (!(ms->quirks & MS_QUIRK_FF))
340 		return 0;
341 
342 	ms->hdev = hdev;
343 	INIT_WORK(&ms->ff_worker, ms_ff_worker);
344 
345 	ms->output_report_dmabuf = devm_kzalloc(&hdev->dev,
346 						sizeof(struct xb1s_ff_report),
347 						GFP_KERNEL);
348 	if (ms->output_report_dmabuf == NULL)
349 		return -ENOMEM;
350 
351 	input_set_capability(input_dev, EV_FF, FF_RUMBLE);
352 	return input_ff_create_memless(input_dev, NULL, ms_play_effect);
353 }
354 
355 static void ms_remove_ff(struct hid_device *hdev)
356 {
357 	struct ms_data *ms = hid_get_drvdata(hdev);
358 
359 	if (!(ms->quirks & MS_QUIRK_FF))
360 		return;
361 
362 	cancel_work_sync(&ms->ff_worker);
363 }
364 
365 static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
366 {
367 	unsigned long quirks = id->driver_data;
368 	struct ms_data *ms;
369 	int ret;
370 
371 	ms = devm_kzalloc(&hdev->dev, sizeof(*ms), GFP_KERNEL);
372 	if (ms == NULL)
373 		return -ENOMEM;
374 
375 	ms->quirks = quirks;
376 
377 	hid_set_drvdata(hdev, ms);
378 
379 	if (quirks & MS_NOGET)
380 		hdev->quirks |= HID_QUIRK_NOGET;
381 
382 	if (quirks & MS_SURFACE_DIAL)
383 		hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
384 
385 	ret = hid_parse(hdev);
386 	if (ret) {
387 		hid_err(hdev, "parse failed\n");
388 		goto err_free;
389 	}
390 
391 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
392 				HID_CONNECT_HIDINPUT_FORCE : 0));
393 	if (ret) {
394 		hid_err(hdev, "hw start failed\n");
395 		goto err_free;
396 	}
397 
398 	ret = ms_init_ff(hdev);
399 	if (ret)
400 		hid_err(hdev, "could not initialize ff, continuing anyway");
401 
402 	return 0;
403 err_free:
404 	return ret;
405 }
406 
407 static void ms_remove(struct hid_device *hdev)
408 {
409 	hid_hw_stop(hdev);
410 	ms_remove_ff(hdev);
411 }
412 
413 static const struct hid_device_id ms_devices[] = {
414 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
415 		.driver_data = MS_HIDINPUT },
416 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_OFFICE_KB),
417 		.driver_data = MS_ERGONOMY },
418 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
419 		.driver_data = MS_ERGONOMY },
420 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP),
421 		.driver_data = MS_ERGONOMY },
422 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE7K),
423 		.driver_data = MS_ERGONOMY },
424 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
425 		.driver_data = MS_ERGONOMY | MS_RDESC },
426 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
427 		.driver_data = MS_PRESENTER },
428 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
429 		.driver_data = MS_ERGONOMY },
430 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_7K),
431 		.driver_data = MS_ERGONOMY },
432 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_600),
433 		.driver_data = MS_ERGONOMY },
434 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3KV1),
435 		.driver_data = MS_ERGONOMY },
436 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
437 		.driver_data = MS_NOGET },
438 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
439 		.driver_data = MS_DUPLICATE_USAGES },
440 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_POWER_COVER),
441 		.driver_data = MS_HIDINPUT },
442 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_KEYBOARD),
443 		.driver_data = MS_ERGONOMY},
444 
445 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
446 		.driver_data = MS_PRESENTER },
447 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, 0x091B),
448 		.driver_data = MS_SURFACE_DIAL },
449 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_XBOX_ONE_S_CONTROLLER),
450 		.driver_data = MS_QUIRK_FF },
451 	{ }
452 };
453 MODULE_DEVICE_TABLE(hid, ms_devices);
454 
455 static struct hid_driver ms_driver = {
456 	.name = "microsoft",
457 	.id_table = ms_devices,
458 	.report_fixup = ms_report_fixup,
459 	.input_mapping = ms_input_mapping,
460 	.input_mapped = ms_input_mapped,
461 	.event = ms_event,
462 	.probe = ms_probe,
463 	.remove = ms_remove,
464 };
465 module_hid_driver(ms_driver);
466 
467 MODULE_LICENSE("GPL");
468