xref: /linux/drivers/hid/hid-microsoft.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
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 
33 static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
34 		unsigned int *rsize)
35 {
36 	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
37 
38 	/*
39 	 * Microsoft Wireless Desktop Receiver (Model 1028) has
40 	 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
41 	 */
42 	if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
43 			rdesc[559] == 0x29) {
44 		hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
45 		rdesc[557] = 0x35;
46 		rdesc[559] = 0x45;
47 	}
48 	return rdesc;
49 }
50 
51 #define ms_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, max, \
52 					EV_KEY, (c))
53 static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
54 		unsigned long **bit, int *max)
55 {
56 	struct input_dev *input = hi->input;
57 
58 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
59 		switch (usage->hid & HID_USAGE) {
60 		/*
61 		 * Microsoft uses these 2 reserved usage ids for 2 keys on
62 		 * the MS office kb labelled "Office Home" and "Task Pane".
63 		 */
64 		case 0x29d:
65 			ms_map_key_clear(KEY_PROG1);
66 			return 1;
67 		case 0x29e:
68 			ms_map_key_clear(KEY_PROG2);
69 			return 1;
70 		}
71 		return 0;
72 	}
73 
74 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
75 		return 0;
76 
77 	switch (usage->hid & HID_USAGE) {
78 	case 0xfd06: ms_map_key_clear(KEY_CHAT);	break;
79 	case 0xfd07: ms_map_key_clear(KEY_PHONE);	break;
80 	case 0xff00:
81 		/* Special keypad keys */
82 		ms_map_key_clear(KEY_KPEQUAL);
83 		set_bit(KEY_KPLEFTPAREN, input->keybit);
84 		set_bit(KEY_KPRIGHTPAREN, input->keybit);
85 		break;
86 	case 0xff01:
87 		/* Scroll wheel */
88 		hid_map_usage_clear(hi, usage, bit, max, EV_REL, REL_WHEEL);
89 		break;
90 	case 0xff02:
91 		/*
92 		 * This byte contains a copy of the modifier keys byte of a
93 		 * standard hid keyboard report, as send by interface 0
94 		 * (this usage is found on interface 1).
95 		 *
96 		 * This byte only gets send when another key in the same report
97 		 * changes state, and as such is useless, ignore it.
98 		 */
99 		return -1;
100 	case 0xff05:
101 		set_bit(EV_REP, input->evbit);
102 		ms_map_key_clear(KEY_F13);
103 		set_bit(KEY_F14, input->keybit);
104 		set_bit(KEY_F15, input->keybit);
105 		set_bit(KEY_F16, input->keybit);
106 		set_bit(KEY_F17, input->keybit);
107 		set_bit(KEY_F18, input->keybit);
108 		break;
109 	default:
110 		return 0;
111 	}
112 	return 1;
113 }
114 
115 static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
116 		unsigned long **bit, int *max)
117 {
118 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
119 		return 0;
120 
121 	set_bit(EV_REP, hi->input->evbit);
122 	switch (usage->hid & HID_USAGE) {
123 	case 0xfd08: ms_map_key_clear(KEY_FORWARD);	break;
124 	case 0xfd09: ms_map_key_clear(KEY_BACK);	break;
125 	case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE);	break;
126 	case 0xfd0e: ms_map_key_clear(KEY_CLOSE);	break;
127 	case 0xfd0f: ms_map_key_clear(KEY_PLAY);	break;
128 	default:
129 		return 0;
130 	}
131 	return 1;
132 }
133 
134 static int ms_surface_dial_quirk(struct hid_input *hi, struct hid_field *field,
135 		struct hid_usage *usage, unsigned long **bit, int *max)
136 {
137 	switch (usage->hid & HID_USAGE_PAGE) {
138 	case 0xff070000:
139 		/* fall-through */
140 	case HID_UP_DIGITIZER:
141 		/* ignore those axis */
142 		return -1;
143 	case HID_UP_GENDESK:
144 		switch (usage->hid) {
145 		case HID_GD_X:
146 			/* fall-through */
147 		case HID_GD_Y:
148 			/* fall-through */
149 		case HID_GD_RFKILL_BTN:
150 			/* ignore those axis */
151 			return -1;
152 		}
153 	}
154 
155 	return 0;
156 }
157 
158 static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
159 		struct hid_field *field, struct hid_usage *usage,
160 		unsigned long **bit, int *max)
161 {
162 	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
163 
164 	if (quirks & MS_ERGONOMY) {
165 		int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
166 		if (ret)
167 			return ret;
168 	}
169 
170 	if ((quirks & MS_PRESENTER) &&
171 			ms_presenter_8k_quirk(hi, usage, bit, max))
172 		return 1;
173 
174 	if (quirks & MS_SURFACE_DIAL) {
175 		int ret = ms_surface_dial_quirk(hi, field, usage, bit, max);
176 
177 		if (ret)
178 			return ret;
179 	}
180 
181 	return 0;
182 }
183 
184 static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi,
185 		struct hid_field *field, struct hid_usage *usage,
186 		unsigned long **bit, int *max)
187 {
188 	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
189 
190 	if (quirks & MS_DUPLICATE_USAGES)
191 		clear_bit(usage->code, *bit);
192 
193 	return 0;
194 }
195 
196 static int ms_event(struct hid_device *hdev, struct hid_field *field,
197 		struct hid_usage *usage, __s32 value)
198 {
199 	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
200 	struct input_dev *input;
201 
202 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
203 			!usage->type)
204 		return 0;
205 
206 	input = field->hidinput->input;
207 
208 	/* Handling MS keyboards special buttons */
209 	if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff00)) {
210 		/* Special keypad keys */
211 		input_report_key(input, KEY_KPEQUAL, value & 0x01);
212 		input_report_key(input, KEY_KPLEFTPAREN, value & 0x02);
213 		input_report_key(input, KEY_KPRIGHTPAREN, value & 0x04);
214 		return 1;
215 	}
216 
217 	if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff01)) {
218 		/* Scroll wheel */
219 		int step = ((value & 0x60) >> 5) + 1;
220 
221 		switch (value & 0x1f) {
222 		case 0x01:
223 			input_report_rel(input, REL_WHEEL, step);
224 			break;
225 		case 0x1f:
226 			input_report_rel(input, REL_WHEEL, -step);
227 			break;
228 		}
229 		return 1;
230 	}
231 
232 	if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
233 		static unsigned int last_key = 0;
234 		unsigned int key = 0;
235 		switch (value) {
236 		case 0x01: key = KEY_F14; break;
237 		case 0x02: key = KEY_F15; break;
238 		case 0x04: key = KEY_F16; break;
239 		case 0x08: key = KEY_F17; break;
240 		case 0x10: key = KEY_F18; break;
241 		}
242 		if (key) {
243 			input_event(input, usage->type, key, 1);
244 			last_key = key;
245 		} else
246 			input_event(input, usage->type, last_key, 0);
247 
248 		return 1;
249 	}
250 
251 	return 0;
252 }
253 
254 static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
255 {
256 	unsigned long quirks = id->driver_data;
257 	int ret;
258 
259 	hid_set_drvdata(hdev, (void *)quirks);
260 
261 	if (quirks & MS_NOGET)
262 		hdev->quirks |= HID_QUIRK_NOGET;
263 
264 	if (quirks & MS_SURFACE_DIAL)
265 		hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
266 
267 	ret = hid_parse(hdev);
268 	if (ret) {
269 		hid_err(hdev, "parse failed\n");
270 		goto err_free;
271 	}
272 
273 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
274 				HID_CONNECT_HIDINPUT_FORCE : 0));
275 	if (ret) {
276 		hid_err(hdev, "hw start failed\n");
277 		goto err_free;
278 	}
279 
280 	return 0;
281 err_free:
282 	return ret;
283 }
284 
285 static const struct hid_device_id ms_devices[] = {
286 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
287 		.driver_data = MS_HIDINPUT },
288 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_OFFICE_KB),
289 		.driver_data = MS_ERGONOMY },
290 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
291 		.driver_data = MS_ERGONOMY },
292 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP),
293 		.driver_data = MS_ERGONOMY },
294 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE7K),
295 		.driver_data = MS_ERGONOMY },
296 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
297 		.driver_data = MS_ERGONOMY | MS_RDESC },
298 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
299 		.driver_data = MS_PRESENTER },
300 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
301 		.driver_data = MS_ERGONOMY },
302 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_7K),
303 		.driver_data = MS_ERGONOMY },
304 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_600),
305 		.driver_data = MS_ERGONOMY },
306 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3KV1),
307 		.driver_data = MS_ERGONOMY },
308 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
309 		.driver_data = MS_NOGET },
310 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
311 		.driver_data = MS_DUPLICATE_USAGES },
312 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_POWER_COVER),
313 		.driver_data = MS_HIDINPUT },
314 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_KEYBOARD),
315 		.driver_data = MS_ERGONOMY},
316 
317 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
318 		.driver_data = MS_PRESENTER },
319 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, 0x091B),
320 		.driver_data = MS_SURFACE_DIAL },
321 	{ }
322 };
323 MODULE_DEVICE_TABLE(hid, ms_devices);
324 
325 static struct hid_driver ms_driver = {
326 	.name = "microsoft",
327 	.id_table = ms_devices,
328 	.report_fixup = ms_report_fixup,
329 	.input_mapping = ms_input_mapping,
330 	.input_mapped = ms_input_mapped,
331 	.event = ms_event,
332 	.probe = ms_probe,
333 };
334 module_hid_driver(ms_driver);
335 
336 MODULE_LICENSE("GPL");
337