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