xref: /linux/drivers/hid/hid-microsoft.c (revision 367b8112fe2ea5c39a7bb4d263dcdd9b612fae18)
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) 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 
26 #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 
32 /*
33  * Microsoft Wireless Desktop Receiver (Model 1028) has several
34  * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
35  */
36 static void ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
37 		unsigned int rsize)
38 {
39 	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
40 
41 	if ((quirks & MS_RDESC) && rsize == 571 && rdesc[284] == 0x19 &&
42 			rdesc[286] == 0x2a && rdesc[304] == 0x19 &&
43 			rdesc[306] == 0x29 && rdesc[352] == 0x1a &&
44 			rdesc[355] == 0x2a && rdesc[557] == 0x19 &&
45 			rdesc[559] == 0x29) {
46 		dev_info(&hdev->dev, "fixing up Microsoft Wireless Receiver "
47 				"Model 1028 report descriptor\n");
48 		rdesc[284] = rdesc[304] = rdesc[557] = 0x35;
49 		rdesc[352] = 0x36;
50 		rdesc[286] = rdesc[355] = 0x46;
51 		rdesc[306] = rdesc[559] = 0x45;
52 	}
53 }
54 
55 #define ms_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, max, \
56 					EV_KEY, (c))
57 static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
58 		unsigned long **bit, int *max)
59 {
60 	struct input_dev *input = hi->input;
61 
62 	switch (usage->hid & HID_USAGE) {
63 	case 0xfd06: ms_map_key_clear(KEY_CHAT);	break;
64 	case 0xfd07: ms_map_key_clear(KEY_PHONE);	break;
65 	case 0xff05:
66 		set_bit(EV_REP, input->evbit);
67 		ms_map_key_clear(KEY_F13);
68 		set_bit(KEY_F14, input->keybit);
69 		set_bit(KEY_F15, input->keybit);
70 		set_bit(KEY_F16, input->keybit);
71 		set_bit(KEY_F17, input->keybit);
72 		set_bit(KEY_F18, input->keybit);
73 	default:
74 		return 0;
75 	}
76 	return 1;
77 }
78 
79 static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
80 		unsigned long **bit, int *max)
81 {
82 	set_bit(EV_REP, hi->input->evbit);
83 	switch (usage->hid & HID_USAGE) {
84 	case 0xfd08: ms_map_key_clear(KEY_FORWARD);	break;
85 	case 0xfd09: ms_map_key_clear(KEY_BACK);	break;
86 	case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE);	break;
87 	case 0xfd0e: ms_map_key_clear(KEY_CLOSE);	break;
88 	case 0xfd0f: ms_map_key_clear(KEY_PLAY);	break;
89 	default:
90 		return 0;
91 	}
92 	return 1;
93 }
94 
95 static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
96 		struct hid_field *field, struct hid_usage *usage,
97 		unsigned long **bit, int *max)
98 {
99 	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
100 
101 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
102 		return 0;
103 
104 	if (quirks & MS_ERGONOMY) {
105 		int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
106 		if (ret)
107 			return ret;
108 	}
109 
110 	if ((quirks & MS_PRESENTER) &&
111 			ms_presenter_8k_quirk(hi, usage, bit, max))
112 		return 1;
113 
114 	return 0;
115 }
116 
117 static int ms_event(struct hid_device *hdev, struct hid_field *field,
118 		struct hid_usage *usage, __s32 value)
119 {
120 	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
121 
122 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
123 			!usage->type)
124 		return 0;
125 
126 	/* Handling MS keyboards special buttons */
127 	if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
128 		struct input_dev *input = field->hidinput->input;
129 		static unsigned int last_key = 0;
130 		unsigned int key = 0;
131 		switch (value) {
132 		case 0x01: key = KEY_F14; break;
133 		case 0x02: key = KEY_F15; break;
134 		case 0x04: key = KEY_F16; break;
135 		case 0x08: key = KEY_F17; break;
136 		case 0x10: key = KEY_F18; break;
137 		}
138 		if (key) {
139 			input_event(input, usage->type, key, 1);
140 			last_key = key;
141 		} else
142 			input_event(input, usage->type, last_key, 0);
143 
144 		return 1;
145 	}
146 
147 	return 0;
148 }
149 
150 static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
151 {
152 	unsigned long quirks = id->driver_data;
153 	int ret;
154 
155 	hid_set_drvdata(hdev, (void *)quirks);
156 
157 	if (quirks & MS_NOGET)
158 		hdev->quirks |= HID_QUIRK_NOGET;
159 
160 	ret = hid_parse(hdev);
161 	if (ret) {
162 		dev_err(&hdev->dev, "parse failed\n");
163 		goto err_free;
164 	}
165 
166 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
167 				HID_CONNECT_HIDINPUT_FORCE : 0));
168 	if (ret) {
169 		dev_err(&hdev->dev, "hw start failed\n");
170 		goto err_free;
171 	}
172 
173 	return 0;
174 err_free:
175 	return ret;
176 }
177 
178 static const struct hid_device_id ms_devices[] = {
179 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
180 		.driver_data = MS_HIDINPUT },
181 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
182 		.driver_data = MS_ERGONOMY },
183 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
184 		.driver_data = MS_ERGONOMY | MS_RDESC },
185 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
186 		.driver_data = MS_PRESENTER },
187 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
188 		.driver_data = MS_NOGET },
189 
190 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
191 		.driver_data = MS_PRESENTER },
192 	{ }
193 };
194 MODULE_DEVICE_TABLE(hid, ms_devices);
195 
196 static struct hid_driver ms_driver = {
197 	.name = "microsoft",
198 	.id_table = ms_devices,
199 	.report_fixup = ms_report_fixup,
200 	.input_mapping = ms_input_mapping,
201 	.event = ms_event,
202 	.probe = ms_probe,
203 };
204 
205 static int ms_init(void)
206 {
207 	return hid_register_driver(&ms_driver);
208 }
209 
210 static void ms_exit(void)
211 {
212 	hid_unregister_driver(&ms_driver);
213 }
214 
215 module_init(ms_init);
216 module_exit(ms_exit);
217 MODULE_LICENSE("GPL");
218 
219 HID_COMPAT_LOAD_DRIVER(microsoft);
220