blob: 72d983626afd0fb4a2b2a68084da61ae47828f70 [file] [log] [blame]
Jiri Slaby78a849a682008-06-20 21:26:11 +02001/*
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
Jiri Slaby78a849a682008-06-20 21:26:11 +02008 * 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
Benjamin Tissoires30576c52018-07-13 16:13:51 +020025#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)
Jiri Slaby78a849a682008-06-20 21:26:11 +020032
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040033static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
34 unsigned int *rsize)
Jiri Slaby78a849a682008-06-20 21:26:11 +020035{
36 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
37
Jiri Slaby3ccc60f2012-10-19 13:28:46 +020038 /*
39 * Microsoft Wireless Desktop Receiver (Model 1028) has
40 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
41 */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040042 if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
Jiri Slaby78a849a682008-06-20 21:26:11 +020043 rdesc[559] == 0x29) {
Joe Perches4291ee32010-12-09 19:29:03 -080044 hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
Jiri Kosina0fb21de2009-01-14 03:03:21 +010045 rdesc[557] = 0x35;
46 rdesc[559] = 0x45;
Jiri Slaby78a849a682008-06-20 21:26:11 +020047 }
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040048 return rdesc;
Jiri Slaby78a849a682008-06-20 21:26:11 +020049}
50
51#define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
52 EV_KEY, (c))
53static 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
Hans de Goede34e75dc2014-01-29 17:57:44 +010058 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
Hans de Goedeaac59f62014-01-29 17:57:42 +010074 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
75 return 0;
76
Jiri Slaby78a849a682008-06-20 21:26:11 +020077 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;
Hans de Goede3faed1a2014-01-29 17:57:43 +010080 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;
Jiri Slaby78a849a682008-06-20 21:26:11 +0200100 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);
Jiri Kosinab2b98ea2013-12-15 18:25:26 +0100108 break;
Jiri Slaby78a849a682008-06-20 21:26:11 +0200109 default:
110 return 0;
111 }
112 return 1;
113}
114
115static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
116 unsigned long **bit, int *max)
117{
Hans de Goedeaac59f62014-01-29 17:57:42 +0100118 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
119 return 0;
120
Jiri Slaby78a849a682008-06-20 21:26:11 +0200121 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
Benjamin Tissoires30576c52018-07-13 16:13:51 +0200134static 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200158static 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200164 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
Benjamin Tissoires30576c52018-07-13 16:13:51 +0200174 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200181 return 0;
182}
183
Ari Savolainen23c10be2011-07-11 21:42:52 +0300184static 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200196static 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);
Hans de Goede3faed1a2014-01-29 17:57:43 +0100200 struct input_dev *input;
Jiri Slaby78a849a682008-06-20 21:26:11 +0200201
202 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
203 !usage->type)
204 return 0;
205
Hans de Goede3faed1a2014-01-29 17:57:43 +0100206 input = field->hidinput->input;
207
Jiri Slaby78a849a682008-06-20 21:26:11 +0200208 /* Handling MS keyboards special buttons */
Hans de Goede3faed1a2014-01-29 17:57:43 +0100209 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200232 if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
Jiri Slaby78a849a682008-06-20 21:26:11 +0200233 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
254static 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200261 if (quirks & MS_NOGET)
262 hdev->quirks |= HID_QUIRK_NOGET;
263
Benjamin Tissoires30576c52018-07-13 16:13:51 +0200264 if (quirks & MS_SURFACE_DIAL)
265 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
266
Jiri Slaby78a849a682008-06-20 21:26:11 +0200267 ret = hid_parse(hdev);
268 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800269 hid_err(hdev, "parse failed\n");
Jiri Slaby78a849a682008-06-20 21:26:11 +0200270 goto err_free;
271 }
272
Jiri Slaby93c10132008-06-27 00:04:24 +0200273 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
274 HID_CONNECT_HIDINPUT_FORCE : 0));
Jiri Slaby78a849a682008-06-20 21:26:11 +0200275 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800276 hid_err(hdev, "hw start failed\n");
Jiri Slaby78a849a682008-06-20 21:26:11 +0200277 goto err_free;
278 }
279
280 return 0;
281err_free:
282 return ret;
283}
284
285static 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 },
Hans de Goede3faed1a2014-01-29 17:57:43 +0100288 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_OFFICE_KB),
289 .driver_data = MS_ERGONOMY },
Jiri Slaby78a849a682008-06-20 21:26:11 +0200290 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
291 .driver_data = MS_ERGONOMY },
Adam Jiang89759e22013-04-29 13:27:31 +0900292 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP),
293 .driver_data = MS_ERGONOMY },
Jakub Sitnickief567cf2015-02-21 20:51:08 +0100294 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE7K),
295 .driver_data = MS_ERGONOMY },
Jiri Slaby78a849a682008-06-20 21:26:11 +0200296 { 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 },
Jiri Kosinaf3b83d72011-06-13 23:08:18 +0300300 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
Michel Hermier52dc0852016-09-20 19:50:51 +0200301 .driver_data = MS_ERGONOMY },
Alistair Leslie-Hughesc847a892016-04-04 20:51:40 +1000302 { 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 },
Jiri Slaby78a849a682008-06-20 21:26:11 +0200308 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
309 .driver_data = MS_NOGET },
Ari Savolainen23c10be2011-07-11 21:42:52 +0300310 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
311 .driver_data = MS_DUPLICATE_USAGES },
Raimund Roth18eec2c2015-06-08 11:11:38 +0200312 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_POWER_COVER),
313 .driver_data = MS_HIDINPUT },
Slava Bacherikovf9a82c22016-03-11 18:57:24 +0200314 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_KEYBOARD),
315 .driver_data = MS_ERGONOMY},
Jiri Slaby78a849a682008-06-20 21:26:11 +0200316
317 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
318 .driver_data = MS_PRESENTER },
Benjamin Tissoires30576c52018-07-13 16:13:51 +0200319 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, 0x091B),
320 .driver_data = MS_SURFACE_DIAL },
Jiri Slaby78a849a682008-06-20 21:26:11 +0200321 { }
322};
323MODULE_DEVICE_TABLE(hid, ms_devices);
324
325static 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,
Ari Savolainen23c10be2011-07-11 21:42:52 +0300330 .input_mapped = ms_input_mapped,
Jiri Slaby78a849a682008-06-20 21:26:11 +0200331 .event = ms_event,
332 .probe = ms_probe,
333};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700334module_hid_driver(ms_driver);
Jiri Slaby78a849a682008-06-20 21:26:11 +0200335
Jiri Slaby78a849a682008-06-20 21:26:11 +0200336MODULE_LICENSE("GPL");