blob: 259b9f44e12227ec847d1dd0707cd7a9a5634b97 [file] [log] [blame]
Jiri Slaby78a849a2008-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
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
22#include <linux/module.h>
23
24#include "hid-ids.h"
25
Ari Savolainen23c10be2011-07-11 21:42:52 +030026#define MS_HIDINPUT 0x01
27#define MS_ERGONOMY 0x02
28#define MS_PRESENTER 0x04
29#define MS_RDESC 0x08
30#define MS_NOGET 0x10
31#define MS_DUPLICATE_USAGES 0x20
Jiri Slabyb4be3bb2012-10-19 13:28:46 +020032#define MS_RDESC_3K 0x40
Jiri Slaby78a849a2008-06-20 21:26:11 +020033
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040034static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
35 unsigned int *rsize)
Jiri Slaby78a849a2008-06-20 21:26:11 +020036{
37 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
38
Jiri Slabyb4be3bb2012-10-19 13:28:46 +020039 /*
40 * Microsoft Wireless Desktop Receiver (Model 1028) has
41 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
42 */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040043 if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
Jiri Slaby78a849a2008-06-20 21:26:11 +020044 rdesc[559] == 0x29) {
Joe Perches4291ee32010-12-09 19:29:03 -080045 hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
Jiri Kosina0fb21de2009-01-14 03:03:21 +010046 rdesc[557] = 0x35;
47 rdesc[559] = 0x45;
Jiri Slaby78a849a2008-06-20 21:26:11 +020048 }
Jiri Slabyb4be3bb2012-10-19 13:28:46 +020049 /* the same as above (s/usage/physical/) */
Jiri Slabyfe7c5e12012-11-12 10:16:09 +010050 if ((quirks & MS_RDESC_3K) && *rsize == 106 && rdesc[94] == 0x19 &&
51 rdesc[95] == 0x00 && rdesc[96] == 0x29 &&
52 rdesc[97] == 0xff) {
Jiri Slabyb4be3bb2012-10-19 13:28:46 +020053 rdesc[94] = 0x35;
54 rdesc[96] = 0x45;
55 }
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040056 return rdesc;
Jiri Slaby78a849a2008-06-20 21:26:11 +020057}
58
59#define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
60 EV_KEY, (c))
61static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
62 unsigned long **bit, int *max)
63{
64 struct input_dev *input = hi->input;
65
66 switch (usage->hid & HID_USAGE) {
67 case 0xfd06: ms_map_key_clear(KEY_CHAT); break;
68 case 0xfd07: ms_map_key_clear(KEY_PHONE); break;
69 case 0xff05:
70 set_bit(EV_REP, input->evbit);
71 ms_map_key_clear(KEY_F13);
72 set_bit(KEY_F14, input->keybit);
73 set_bit(KEY_F15, input->keybit);
74 set_bit(KEY_F16, input->keybit);
75 set_bit(KEY_F17, input->keybit);
76 set_bit(KEY_F18, input->keybit);
77 default:
78 return 0;
79 }
80 return 1;
81}
82
83static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
84 unsigned long **bit, int *max)
85{
86 set_bit(EV_REP, hi->input->evbit);
87 switch (usage->hid & HID_USAGE) {
88 case 0xfd08: ms_map_key_clear(KEY_FORWARD); break;
89 case 0xfd09: ms_map_key_clear(KEY_BACK); break;
90 case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE); break;
91 case 0xfd0e: ms_map_key_clear(KEY_CLOSE); break;
92 case 0xfd0f: ms_map_key_clear(KEY_PLAY); break;
93 default:
94 return 0;
95 }
96 return 1;
97}
98
99static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
100 struct hid_field *field, struct hid_usage *usage,
101 unsigned long **bit, int *max)
102{
103 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
104
105 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
106 return 0;
107
108 if (quirks & MS_ERGONOMY) {
109 int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
110 if (ret)
111 return ret;
112 }
113
114 if ((quirks & MS_PRESENTER) &&
115 ms_presenter_8k_quirk(hi, usage, bit, max))
116 return 1;
117
118 return 0;
119}
120
Ari Savolainen23c10be2011-07-11 21:42:52 +0300121static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi,
122 struct hid_field *field, struct hid_usage *usage,
123 unsigned long **bit, int *max)
124{
125 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
126
127 if (quirks & MS_DUPLICATE_USAGES)
128 clear_bit(usage->code, *bit);
129
130 return 0;
131}
132
Jiri Slaby78a849a2008-06-20 21:26:11 +0200133static int ms_event(struct hid_device *hdev, struct hid_field *field,
134 struct hid_usage *usage, __s32 value)
135{
136 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
137
138 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
139 !usage->type)
140 return 0;
141
142 /* Handling MS keyboards special buttons */
143 if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
144 struct input_dev *input = field->hidinput->input;
145 static unsigned int last_key = 0;
146 unsigned int key = 0;
147 switch (value) {
148 case 0x01: key = KEY_F14; break;
149 case 0x02: key = KEY_F15; break;
150 case 0x04: key = KEY_F16; break;
151 case 0x08: key = KEY_F17; break;
152 case 0x10: key = KEY_F18; break;
153 }
154 if (key) {
155 input_event(input, usage->type, key, 1);
156 last_key = key;
157 } else
158 input_event(input, usage->type, last_key, 0);
159
160 return 1;
161 }
162
163 return 0;
164}
165
166static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
167{
168 unsigned long quirks = id->driver_data;
169 int ret;
170
171 hid_set_drvdata(hdev, (void *)quirks);
172
Jiri Slaby78a849a2008-06-20 21:26:11 +0200173 if (quirks & MS_NOGET)
174 hdev->quirks |= HID_QUIRK_NOGET;
175
176 ret = hid_parse(hdev);
177 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800178 hid_err(hdev, "parse failed\n");
Jiri Slaby78a849a2008-06-20 21:26:11 +0200179 goto err_free;
180 }
181
Jiri Slaby93c10132008-06-27 00:04:24 +0200182 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
183 HID_CONNECT_HIDINPUT_FORCE : 0));
Jiri Slaby78a849a2008-06-20 21:26:11 +0200184 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800185 hid_err(hdev, "hw start failed\n");
Jiri Slaby78a849a2008-06-20 21:26:11 +0200186 goto err_free;
187 }
188
189 return 0;
190err_free:
191 return ret;
192}
193
194static const struct hid_device_id ms_devices[] = {
195 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
196 .driver_data = MS_HIDINPUT },
197 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
198 .driver_data = MS_ERGONOMY },
199 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
200 .driver_data = MS_ERGONOMY | MS_RDESC },
201 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
202 .driver_data = MS_PRESENTER },
Jiri Kosinaf3b83d72011-06-13 23:08:18 +0300203 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
Jiri Slabyb4be3bb2012-10-19 13:28:46 +0200204 .driver_data = MS_ERGONOMY | MS_RDESC_3K },
Jiri Slaby78a849a2008-06-20 21:26:11 +0200205 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
206 .driver_data = MS_NOGET },
Ari Savolainen23c10be2011-07-11 21:42:52 +0300207 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
208 .driver_data = MS_DUPLICATE_USAGES },
Jiri Slaby78a849a2008-06-20 21:26:11 +0200209
210 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
211 .driver_data = MS_PRESENTER },
212 { }
213};
214MODULE_DEVICE_TABLE(hid, ms_devices);
215
216static struct hid_driver ms_driver = {
217 .name = "microsoft",
218 .id_table = ms_devices,
219 .report_fixup = ms_report_fixup,
220 .input_mapping = ms_input_mapping,
Ari Savolainen23c10be2011-07-11 21:42:52 +0300221 .input_mapped = ms_input_mapped,
Jiri Slaby78a849a2008-06-20 21:26:11 +0200222 .event = ms_event,
223 .probe = ms_probe,
224};
225
Peter Huewea24f4232009-07-02 19:08:38 +0200226static int __init ms_init(void)
Jiri Slaby78a849a2008-06-20 21:26:11 +0200227{
228 return hid_register_driver(&ms_driver);
229}
230
Peter Huewea24f4232009-07-02 19:08:38 +0200231static void __exit ms_exit(void)
Jiri Slaby78a849a2008-06-20 21:26:11 +0200232{
233 hid_unregister_driver(&ms_driver);
234}
235
236module_init(ms_init);
237module_exit(ms_exit);
238MODULE_LICENSE("GPL");