blob: 96e7d3231d2fbacf7f92f11915a9a4ef000a0920 [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
Ari Savolainen23c10be2011-07-11 21:42:52 +030025#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
Jiri Slaby78a849a682008-06-20 21:26:11 +020031
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040032static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
33 unsigned int *rsize)
Jiri Slaby78a849a682008-06-20 21:26:11 +020034{
35 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
36
Jiri Slaby3ccc60f2012-10-19 13:28:46 +020037 /*
38 * Microsoft Wireless Desktop Receiver (Model 1028) has
39 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
40 */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040041 if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
Jiri Slaby78a849a682008-06-20 21:26:11 +020042 rdesc[559] == 0x29) {
Joe Perches4291ee32010-12-09 19:29:03 -080043 hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
Jiri Kosina0fb21de2009-01-14 03:03:21 +010044 rdesc[557] = 0x35;
45 rdesc[559] = 0x45;
Jiri Slaby78a849a682008-06-20 21:26:11 +020046 }
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040047 return rdesc;
Jiri Slaby78a849a682008-06-20 21:26:11 +020048}
49
50#define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
51 EV_KEY, (c))
52static 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
Hans de Goede34e75dc2014-01-29 17:57:44 +010057 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
Hans de Goedeaac59f62014-01-29 17:57:42 +010073 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
74 return 0;
75
Jiri Slaby78a849a682008-06-20 21:26:11 +020076 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;
Hans de Goede3faed1a2014-01-29 17:57:43 +010079 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;
Jiri Slaby78a849a682008-06-20 21:26:11 +020099 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);
Jiri Kosinab2b98ea2013-12-15 18:25:26 +0100107 break;
Jiri Slaby78a849a682008-06-20 21:26:11 +0200108 default:
109 return 0;
110 }
111 return 1;
112}
113
114static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
115 unsigned long **bit, int *max)
116{
Hans de Goedeaac59f62014-01-29 17:57:42 +0100117 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
118 return 0;
119
Jiri Slaby78a849a682008-06-20 21:26:11 +0200120 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
133static 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200139 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
Ari Savolainen23c10be2011-07-11 21:42:52 +0300152static 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200164static 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);
Hans de Goede3faed1a2014-01-29 17:57:43 +0100168 struct input_dev *input;
Jiri Slaby78a849a682008-06-20 21:26:11 +0200169
170 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
171 !usage->type)
172 return 0;
173
Hans de Goede3faed1a2014-01-29 17:57:43 +0100174 input = field->hidinput->input;
175
Jiri Slaby78a849a682008-06-20 21:26:11 +0200176 /* Handling MS keyboards special buttons */
Hans de Goede3faed1a2014-01-29 17:57:43 +0100177 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200200 if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
Jiri Slaby78a849a682008-06-20 21:26:11 +0200201 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
222static 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
Jiri Slaby78a849a682008-06-20 21:26:11 +0200229 if (quirks & MS_NOGET)
230 hdev->quirks |= HID_QUIRK_NOGET;
231
232 ret = hid_parse(hdev);
233 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800234 hid_err(hdev, "parse failed\n");
Jiri Slaby78a849a682008-06-20 21:26:11 +0200235 goto err_free;
236 }
237
Jiri Slaby93c10132008-06-27 00:04:24 +0200238 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
239 HID_CONNECT_HIDINPUT_FORCE : 0));
Jiri Slaby78a849a682008-06-20 21:26:11 +0200240 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800241 hid_err(hdev, "hw start failed\n");
Jiri Slaby78a849a682008-06-20 21:26:11 +0200242 goto err_free;
243 }
244
245 return 0;
246err_free:
247 return ret;
248}
249
250static 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 },
Hans de Goede3faed1a2014-01-29 17:57:43 +0100253 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_OFFICE_KB),
254 .driver_data = MS_ERGONOMY },
Jiri Slaby78a849a682008-06-20 21:26:11 +0200255 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
256 .driver_data = MS_ERGONOMY },
Adam Jiang89759e22013-04-29 13:27:31 +0900257 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP),
258 .driver_data = MS_ERGONOMY },
Jakub Sitnickief567cf2015-02-21 20:51:08 +0100259 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE7K),
260 .driver_data = MS_ERGONOMY },
Jiri Slaby78a849a682008-06-20 21:26:11 +0200261 { 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 },
Jiri Kosinaf3b83d72011-06-13 23:08:18 +0300265 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
Michel Hermier52dc0852016-09-20 19:50:51 +0200266 .driver_data = MS_ERGONOMY },
Alistair Leslie-Hughesc847a892016-04-04 20:51:40 +1000267 { 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 },
Jiri Slaby78a849a682008-06-20 21:26:11 +0200273 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
274 .driver_data = MS_NOGET },
Ari Savolainen23c10be2011-07-11 21:42:52 +0300275 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
276 .driver_data = MS_DUPLICATE_USAGES },
Raimund Roth18eec2c2015-06-08 11:11:38 +0200277 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_POWER_COVER),
278 .driver_data = MS_HIDINPUT },
Slava Bacherikovf9a82c22016-03-11 18:57:24 +0200279 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_KEYBOARD),
280 .driver_data = MS_ERGONOMY},
Jiri Slaby78a849a682008-06-20 21:26:11 +0200281
282 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
283 .driver_data = MS_PRESENTER },
284 { }
285};
286MODULE_DEVICE_TABLE(hid, ms_devices);
287
288static 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,
Ari Savolainen23c10be2011-07-11 21:42:52 +0300293 .input_mapped = ms_input_mapped,
Jiri Slaby78a849a682008-06-20 21:26:11 +0200294 .event = ms_event,
295 .probe = ms_probe,
296};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700297module_hid_driver(ms_driver);
Jiri Slaby78a849a682008-06-20 21:26:11 +0200298
Jiri Slaby78a849a682008-06-20 21:26:11 +0200299MODULE_LICENSE("GPL");