USB HID: extend quirk for Logitech S510 keyboard
[pandora-kernel.git] / drivers / hid / usbhid / hid-core.c
1 /*
2  *  USB HID support for Linux
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  */
9
10 /*
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  */
16
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/mm.h>
23 #include <linux/smp_lock.h>
24 #include <linux/spinlock.h>
25 #include <asm/unaligned.h>
26 #include <asm/byteorder.h>
27 #include <linux/input.h>
28 #include <linux/wait.h>
29
30 #include <linux/usb.h>
31
32 #include <linux/hid.h>
33 #include <linux/hiddev.h>
34 #include <linux/hid-debug.h>
35 #include "usbhid.h"
36
37 /*
38  * Version Information
39  */
40
41 #define DRIVER_VERSION "v2.6"
42 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
43 #define DRIVER_DESC "USB HID core driver"
44 #define DRIVER_LICENSE "GPL"
45
46 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
47                                 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
48 /*
49  * Module parameters.
50  */
51
52 static unsigned int hid_mousepoll_interval;
53 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55
56 #define USB_VENDOR_ID_A4TECH            0x09da
57 #define USB_DEVICE_ID_A4TECH_WCP32PU    0x0006
58
59 #define USB_VENDOR_ID_AASHIMA           0x06d6
60 #define USB_DEVICE_ID_AASHIMA_GAMEPAD   0x0025
61 #define USB_DEVICE_ID_AASHIMA_PREDATOR  0x0026
62
63 #define USB_VENDOR_ID_ACECAD            0x0460
64 #define USB_DEVICE_ID_ACECAD_FLAIR      0x0004
65 #define USB_DEVICE_ID_ACECAD_302        0x0008
66
67 #define USB_VENDOR_ID_AIPTEK            0x08ca
68 #define USB_DEVICE_ID_AIPTEK_01         0x0001
69 #define USB_DEVICE_ID_AIPTEK_10         0x0010
70 #define USB_DEVICE_ID_AIPTEK_20         0x0020
71 #define USB_DEVICE_ID_AIPTEK_21         0x0021
72 #define USB_DEVICE_ID_AIPTEK_22         0x0022
73 #define USB_DEVICE_ID_AIPTEK_23         0x0023
74 #define USB_DEVICE_ID_AIPTEK_24         0x0024
75
76 #define USB_VENDOR_ID_AIRCABLE          0x16CA
77 #define USB_DEVICE_ID_AIRCABLE1         0x1502
78
79 #define USB_VENDOR_ID_ALCOR             0x058f
80 #define USB_DEVICE_ID_ALCOR_USBRS232    0x9720
81
82 #define USB_VENDOR_ID_ALPS              0x0433
83 #define USB_DEVICE_ID_IBM_GAMEPAD       0x1101
84
85 #define USB_VENDOR_ID_APPLE             0x05ac
86 #define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
87 #define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI       0x020e
88 #define USB_DEVICE_ID_APPLE_FOUNTAIN_ISO        0x020f
89 #define USB_DEVICE_ID_APPLE_GEYSER_ANSI 0x0214
90 #define USB_DEVICE_ID_APPLE_GEYSER_ISO  0x0215
91 #define USB_DEVICE_ID_APPLE_GEYSER_JIS  0x0216
92 #define USB_DEVICE_ID_APPLE_GEYSER3_ANSI        0x0217
93 #define USB_DEVICE_ID_APPLE_GEYSER3_ISO 0x0218
94 #define USB_DEVICE_ID_APPLE_GEYSER3_JIS 0x0219
95 #define USB_DEVICE_ID_APPLE_GEYSER4_ANSI        0x021a
96 #define USB_DEVICE_ID_APPLE_GEYSER4_ISO 0x021b
97 #define USB_DEVICE_ID_APPLE_GEYSER4_JIS 0x021c
98 #define USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY    0x030a
99 #define USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY     0x030b
100 #define USB_DEVICE_ID_APPLE_IR          0x8240
101
102 #define USB_VENDOR_ID_ATEN              0x0557
103 #define USB_DEVICE_ID_ATEN_UC100KM      0x2004
104 #define USB_DEVICE_ID_ATEN_CS124U       0x2202
105 #define USB_DEVICE_ID_ATEN_2PORTKVM     0x2204
106 #define USB_DEVICE_ID_ATEN_4PORTKVM     0x2205
107 #define USB_DEVICE_ID_ATEN_4PORTKVMC    0x2208
108
109 #define USB_VENDOR_ID_BELKIN           0x050d
110 #define USB_DEVICE_ID_FLIP_KVM         0x3201
111
112 #define USB_VENDOR_ID_BERKSHIRE         0x0c98
113 #define USB_DEVICE_ID_BERKSHIRE_PCWD    0x1140
114
115 #define USB_VENDOR_ID_CHERRY            0x046a
116 #define USB_DEVICE_ID_CHERRY_CYMOTION   0x0023
117
118 #define USB_VENDOR_ID_CHIC              0x05fe
119 #define USB_DEVICE_ID_CHIC_GAMEPAD      0x0014
120
121 #define USB_VENDOR_ID_CIDC              0x1677
122
123 #define USB_VENDOR_ID_CODEMERCS         0x07c0
124 #define USB_DEVICE_ID_CODEMERCS_IOW_FIRST       0x1500
125 #define USB_DEVICE_ID_CODEMERCS_IOW_LAST        0x15ff
126
127 #define USB_VENDOR_ID_CYPRESS           0x04b4
128 #define USB_DEVICE_ID_CYPRESS_MOUSE     0x0001
129 #define USB_DEVICE_ID_CYPRESS_HIDCOM    0x5500
130 #define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE        0x7417
131
132 #define USB_VENDOR_ID_DELORME           0x1163
133 #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
134 #define USB_DEVICE_ID_DELORME_EM_LT20   0x0200
135
136 #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
137 #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
138
139 #define USB_VENDOR_ID_GLAB              0x06c2
140 #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
141 #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
142 #define USB_DEVICE_ID_0_0_4_IF_KIT      0x0040
143 #define USB_DEVICE_ID_0_16_16_IF_KIT    0x0044
144 #define USB_DEVICE_ID_8_8_8_IF_KIT      0x0045
145 #define USB_DEVICE_ID_0_8_7_IF_KIT      0x0051
146 #define USB_DEVICE_ID_0_8_8_IF_KIT      0x0053
147 #define USB_DEVICE_ID_PHIDGET_MOTORCONTROL      0x0058
148
149 #define USB_VENDOR_ID_GRIFFIN           0x077d
150 #define USB_DEVICE_ID_POWERMATE         0x0410
151 #define USB_DEVICE_ID_SOUNDKNOB         0x04AA
152
153 #define USB_VENDOR_ID_GTCO              0x078c
154 #define USB_DEVICE_ID_GTCO_90           0x0090
155 #define USB_DEVICE_ID_GTCO_100          0x0100
156 #define USB_DEVICE_ID_GTCO_101          0x0101
157 #define USB_DEVICE_ID_GTCO_103          0x0103
158 #define USB_DEVICE_ID_GTCO_104          0x0104
159 #define USB_DEVICE_ID_GTCO_105          0x0105
160 #define USB_DEVICE_ID_GTCO_106          0x0106
161 #define USB_DEVICE_ID_GTCO_107          0x0107
162 #define USB_DEVICE_ID_GTCO_108          0x0108
163 #define USB_DEVICE_ID_GTCO_200          0x0200
164 #define USB_DEVICE_ID_GTCO_201          0x0201
165 #define USB_DEVICE_ID_GTCO_202          0x0202
166 #define USB_DEVICE_ID_GTCO_203          0x0203
167 #define USB_DEVICE_ID_GTCO_204          0x0204
168 #define USB_DEVICE_ID_GTCO_205          0x0205
169 #define USB_DEVICE_ID_GTCO_206          0x0206
170 #define USB_DEVICE_ID_GTCO_207          0x0207
171 #define USB_DEVICE_ID_GTCO_300          0x0300
172 #define USB_DEVICE_ID_GTCO_301          0x0301
173 #define USB_DEVICE_ID_GTCO_302          0x0302
174 #define USB_DEVICE_ID_GTCO_303          0x0303
175 #define USB_DEVICE_ID_GTCO_304          0x0304
176 #define USB_DEVICE_ID_GTCO_305          0x0305
177 #define USB_DEVICE_ID_GTCO_306          0x0306
178 #define USB_DEVICE_ID_GTCO_307          0x0307
179 #define USB_DEVICE_ID_GTCO_308          0x0308
180 #define USB_DEVICE_ID_GTCO_309          0x0309
181 #define USB_DEVICE_ID_GTCO_400          0x0400
182 #define USB_DEVICE_ID_GTCO_401          0x0401
183 #define USB_DEVICE_ID_GTCO_402          0x0402
184 #define USB_DEVICE_ID_GTCO_403          0x0403
185 #define USB_DEVICE_ID_GTCO_404          0x0404
186 #define USB_DEVICE_ID_GTCO_405          0x0405
187 #define USB_DEVICE_ID_GTCO_500          0x0500
188 #define USB_DEVICE_ID_GTCO_501          0x0501
189 #define USB_DEVICE_ID_GTCO_502          0x0502
190 #define USB_DEVICE_ID_GTCO_503          0x0503
191 #define USB_DEVICE_ID_GTCO_504          0x0504
192 #define USB_DEVICE_ID_GTCO_1000         0x1000
193 #define USB_DEVICE_ID_GTCO_1001         0x1001
194 #define USB_DEVICE_ID_GTCO_1002         0x1002
195 #define USB_DEVICE_ID_GTCO_1003         0x1003
196 #define USB_DEVICE_ID_GTCO_1004         0x1004
197 #define USB_DEVICE_ID_GTCO_1005         0x1005
198 #define USB_DEVICE_ID_GTCO_1006         0x1006
199
200 #define USB_VENDOR_ID_HAPP              0x078b
201 #define USB_DEVICE_ID_UGCI_DRIVING      0x0010
202 #define USB_DEVICE_ID_UGCI_FLYING       0x0020
203 #define USB_DEVICE_ID_UGCI_FIGHTING     0x0030
204
205 #define USB_VENDOR_ID_IMATION           0x0718
206 #define USB_DEVICE_ID_DISC_STAKKA       0xd000
207
208 #define USB_VENDOR_ID_KBGEAR            0x084e
209 #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO  0x1001
210
211 #define USB_VENDOR_ID_LD                0x0f11
212 #define USB_DEVICE_ID_LD_CASSY          0x1000
213 #define USB_DEVICE_ID_LD_POCKETCASSY    0x1010
214 #define USB_DEVICE_ID_LD_MOBILECASSY    0x1020
215 #define USB_DEVICE_ID_LD_JWM            0x1080
216 #define USB_DEVICE_ID_LD_DMMP           0x1081
217 #define USB_DEVICE_ID_LD_UMIP           0x1090
218 #define USB_DEVICE_ID_LD_XRAY1          0x1100
219 #define USB_DEVICE_ID_LD_XRAY2          0x1101
220 #define USB_DEVICE_ID_LD_VIDEOCOM       0x1200
221 #define USB_DEVICE_ID_LD_COM3LAB        0x2000
222 #define USB_DEVICE_ID_LD_TELEPORT       0x2010
223 #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
224 #define USB_DEVICE_ID_LD_POWERCONTROL   0x2030
225 #define USB_DEVICE_ID_LD_MACHINETEST    0x2040
226
227 #define USB_VENDOR_ID_LOGITECH          0x046d
228 #define USB_DEVICE_ID_LOGITECH_USB_RECEIVER     0xc101
229 #define USB_DEVICE_ID_S510_USB_RECEIVER 0xc50c
230 #define USB_DEVICE_ID_S510_USB_RECEIVER_2       0xc517
231 #define USB_DEVICE_ID_DINOVO_EDGE       0xc714
232
233 #define USB_VENDOR_ID_MCC               0x09db
234 #define USB_DEVICE_ID_MCC_PMD1024LS     0x0076
235 #define USB_DEVICE_ID_MCC_PMD1208LS     0x007a
236
237 #define USB_VENDOR_ID_MGE               0x0463
238 #define USB_DEVICE_ID_MGE_UPS           0xffff
239 #define USB_DEVICE_ID_MGE_UPS1          0x0001
240
241 #define USB_VENDOR_ID_NEC               0x073e
242 #define USB_DEVICE_ID_NEC_USB_GAME_PAD  0x0301
243
244 #define USB_VENDOR_ID_ONTRAK            0x0a07
245 #define USB_DEVICE_ID_ONTRAK_ADU100     0x0064
246
247 #define USB_VENDOR_ID_PANJIT            0x134c
248
249 #define USB_VENDOR_ID_PANTHERLORD       0x0810
250 #define USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK     0x0001
251
252 #define USB_VENDOR_ID_SAITEK            0x06a3
253 #define USB_DEVICE_ID_SAITEK_RUMBLEPAD  0xff17
254
255 #define USB_VENDOR_ID_SONY                      0x054c
256 #define USB_DEVICE_ID_SONY_PS3_CONTROLLER       0x0268
257
258 #define USB_VENDOR_ID_SUN               0x0430
259 #define USB_DEVICE_ID_RARITAN_KVM_DONGLE        0xcdab
260
261 #define USB_VENDOR_ID_TOPMAX            0x0663
262 #define USB_DEVICE_ID_TOPMAX_COBRAPAD   0x0103
263
264 #define USB_VENDOR_ID_TURBOX            0x062a
265 #define USB_DEVICE_ID_TURBOX_KEYBOARD   0x0201
266
267 #define USB_VENDOR_ID_VERNIER           0x08f7
268 #define USB_DEVICE_ID_VERNIER_LABPRO    0x0001
269 #define USB_DEVICE_ID_VERNIER_GOTEMP    0x0002
270 #define USB_DEVICE_ID_VERNIER_SKIP      0x0003
271 #define USB_DEVICE_ID_VERNIER_CYCLOPS   0x0004
272
273 #define USB_VENDOR_ID_WACOM             0x056a
274
275 #define USB_VENDOR_ID_WISEGROUP         0x0925
276 #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
277 #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
278 #define USB_DEVICE_ID_8_8_4_IF_KIT      0x8201
279 #define USB_DEVICE_ID_DUAL_USB_JOYPAD   0x8866
280
281 #define USB_VENDOR_ID_WISEGROUP_LTD     0x6677
282 #define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
283
284 #define USB_VENDOR_ID_YEALINK           0x6993
285 #define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K       0xb001
286
287 /*
288  * Alphabetically sorted blacklist by quirk type.
289  */
290
291 static const struct hid_blacklist {
292         __u16 idVendor;
293         __u16 idProduct;
294         __u32 quirks;
295 } hid_blacklist[] = {
296
297         { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
298         { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
299
300         { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_USB_RECEIVER, HID_QUIRK_BAD_RELATIVE_KEYS },
301
302         { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
303         { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
304         { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
305         { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
306         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
307         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
308         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
309         { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
310         { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
311         { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
312         
313         { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_CYMOTION },
314
315         { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE, HID_QUIRK_DUPLICATE_USAGES },
316
317         { USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM, HID_QUIRK_HIDDEV },
318
319         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
320         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
321         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
322         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
323         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
324         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
325         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
326         { USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1, HID_QUIRK_IGNORE },
327         { USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232, HID_QUIRK_IGNORE },
328         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IR, HID_QUIRK_IGNORE },
329         { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
330         { USB_VENDOR_ID_CIDC, 0x0103, HID_QUIRK_IGNORE },
331         { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
332         { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
333         { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
334         { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
335         { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
336         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
337         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
338         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
339         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT, HID_QUIRK_IGNORE },
340         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
341         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT, HID_QUIRK_IGNORE },
342         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
343         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL, HID_QUIRK_IGNORE },
344         { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
345         { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
346         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90, HID_QUIRK_IGNORE },
347         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100, HID_QUIRK_IGNORE },
348         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101, HID_QUIRK_IGNORE },
349         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103, HID_QUIRK_IGNORE },
350         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104, HID_QUIRK_IGNORE },
351         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105, HID_QUIRK_IGNORE },
352         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106, HID_QUIRK_IGNORE },
353         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107, HID_QUIRK_IGNORE },
354         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108, HID_QUIRK_IGNORE },
355         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200, HID_QUIRK_IGNORE },
356         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201, HID_QUIRK_IGNORE },
357         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202, HID_QUIRK_IGNORE },
358         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203, HID_QUIRK_IGNORE },
359         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204, HID_QUIRK_IGNORE },
360         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205, HID_QUIRK_IGNORE },
361         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206, HID_QUIRK_IGNORE },
362         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207, HID_QUIRK_IGNORE },
363         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300, HID_QUIRK_IGNORE },
364         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301, HID_QUIRK_IGNORE },
365         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302, HID_QUIRK_IGNORE },
366         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303, HID_QUIRK_IGNORE },
367         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304, HID_QUIRK_IGNORE },
368         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305, HID_QUIRK_IGNORE },
369         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306, HID_QUIRK_IGNORE },
370         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307, HID_QUIRK_IGNORE },
371         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308, HID_QUIRK_IGNORE },
372         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309, HID_QUIRK_IGNORE },
373         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400, HID_QUIRK_IGNORE },
374         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401, HID_QUIRK_IGNORE },
375         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402, HID_QUIRK_IGNORE },
376         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403, HID_QUIRK_IGNORE },
377         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404, HID_QUIRK_IGNORE },
378         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405, HID_QUIRK_IGNORE },
379         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500, HID_QUIRK_IGNORE },
380         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501, HID_QUIRK_IGNORE },
381         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502, HID_QUIRK_IGNORE },
382         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503, HID_QUIRK_IGNORE },
383         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504, HID_QUIRK_IGNORE },
384         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000, HID_QUIRK_IGNORE },
385         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001, HID_QUIRK_IGNORE },
386         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002, HID_QUIRK_IGNORE },
387         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003, HID_QUIRK_IGNORE },
388         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004, HID_QUIRK_IGNORE },
389         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005, HID_QUIRK_IGNORE },
390         { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006, HID_QUIRK_IGNORE },
391         { USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA, HID_QUIRK_IGNORE },
392         { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
393         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
394         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
395         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
396         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
397         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
398         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
399         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
400         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
401         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
402         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
403         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
404         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
405         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
406         { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
407         { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
408         { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
409         { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
410         { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
411         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
412         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20, HID_QUIRK_IGNORE },
413         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30, HID_QUIRK_IGNORE },
414         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
415         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108, HID_QUIRK_IGNORE },
416         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118, HID_QUIRK_IGNORE },
417         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
418         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
419         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
420         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
421         { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
422         { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
423         { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
424         { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
425         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
426         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
427         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
428         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
429         { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
430         { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
431         { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT, HID_QUIRK_IGNORE },
432         { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
433
434         { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
435         { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
436
437         { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_USB_RECEIVER, HID_QUIRK_LOGITECH_S510_DESCRIPTOR },
438         { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_USB_RECEIVER_2, HID_QUIRK_LOGITECH_S510_DESCRIPTOR },
439
440         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
441
442         { USB_VENDOR_ID_PANTHERLORD, USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK, HID_QUIRK_MULTI_INPUT | HID_QUIRK_SKIP_OUTPUT_REPORTS },
443
444         { USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER, HID_QUIRK_SONY_PS3_CONTROLLER },
445
446         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
447         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
448         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
449         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
450         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
451         { USB_VENDOR_ID_SUN, USB_DEVICE_ID_RARITAN_KVM_DONGLE, HID_QUIRK_NOGET },
452         { USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_TURBOX_KEYBOARD, HID_QUIRK_NOGET },
453         { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
454         { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
455
456         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
457         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
458         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
459         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
460         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
461         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
462         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
463         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
464         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
465         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
466         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
467         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
468         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
469
470         { 0, 0 }
471 };
472
473 /*
474  * Input submission and I/O error handler.
475  */
476
477 static void hid_io_error(struct hid_device *hid);
478
479 /* Start up the input URB */
480 static int hid_start_in(struct hid_device *hid)
481 {
482         unsigned long flags;
483         int rc = 0;
484         struct usbhid_device *usbhid = hid->driver_data;
485
486         spin_lock_irqsave(&usbhid->inlock, flags);
487         if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
488                         !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
489                 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
490                 if (rc != 0)
491                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
492         }
493         spin_unlock_irqrestore(&usbhid->inlock, flags);
494         return rc;
495 }
496
497 /* I/O retry timer routine */
498 static void hid_retry_timeout(unsigned long _hid)
499 {
500         struct hid_device *hid = (struct hid_device *) _hid;
501         struct usbhid_device *usbhid = hid->driver_data;
502
503         dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
504         if (hid_start_in(hid))
505                 hid_io_error(hid);
506 }
507
508 /* Workqueue routine to reset the device or clear a halt */
509 static void hid_reset(struct work_struct *work)
510 {
511         struct usbhid_device *usbhid =
512                 container_of(work, struct usbhid_device, reset_work);
513         struct hid_device *hid = usbhid->hid;
514         int rc_lock, rc = 0;
515
516         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
517                 dev_dbg(&usbhid->intf->dev, "clear halt\n");
518                 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
519                 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
520                 hid_start_in(hid);
521         }
522
523         else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
524                 dev_dbg(&usbhid->intf->dev, "resetting device\n");
525                 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
526                 if (rc_lock >= 0) {
527                         rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
528                         if (rc_lock)
529                                 usb_unlock_device(hid_to_usb_dev(hid));
530                 }
531                 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
532         }
533
534         switch (rc) {
535         case 0:
536                 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
537                         hid_io_error(hid);
538                 break;
539         default:
540                 err("can't reset device, %s-%s/input%d, status %d",
541                                 hid_to_usb_dev(hid)->bus->bus_name,
542                                 hid_to_usb_dev(hid)->devpath,
543                                 usbhid->ifnum, rc);
544                 /* FALLTHROUGH */
545         case -EHOSTUNREACH:
546         case -ENODEV:
547         case -EINTR:
548                 break;
549         }
550 }
551
552 /* Main I/O error handler */
553 static void hid_io_error(struct hid_device *hid)
554 {
555         unsigned long flags;
556         struct usbhid_device *usbhid = hid->driver_data;
557
558         spin_lock_irqsave(&usbhid->inlock, flags);
559
560         /* Stop when disconnected */
561         if (usb_get_intfdata(usbhid->intf) == NULL)
562                 goto done;
563
564         /* If it has been a while since the last error, we'll assume
565          * this a brand new error and reset the retry timeout. */
566         if (time_after(jiffies, usbhid->stop_retry + HZ/2))
567                 usbhid->retry_delay = 0;
568
569         /* When an error occurs, retry at increasing intervals */
570         if (usbhid->retry_delay == 0) {
571                 usbhid->retry_delay = 13;       /* Then 26, 52, 104, 104, ... */
572                 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
573         } else if (usbhid->retry_delay < 100)
574                 usbhid->retry_delay *= 2;
575
576         if (time_after(jiffies, usbhid->stop_retry)) {
577
578                 /* Retries failed, so do a port reset */
579                 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
580                         schedule_work(&usbhid->reset_work);
581                         goto done;
582                 }
583         }
584
585         mod_timer(&usbhid->io_retry,
586                         jiffies + msecs_to_jiffies(usbhid->retry_delay));
587 done:
588         spin_unlock_irqrestore(&usbhid->inlock, flags);
589 }
590
591 /*
592  * Input interrupt completion handler.
593  */
594
595 static void hid_irq_in(struct urb *urb)
596 {
597         struct hid_device       *hid = urb->context;
598         struct usbhid_device    *usbhid = hid->driver_data;
599         int                     status;
600
601         switch (urb->status) {
602                 case 0:                 /* success */
603                         usbhid->retry_delay = 0;
604                         hid_input_report(urb->context, HID_INPUT_REPORT,
605                                          urb->transfer_buffer,
606                                          urb->actual_length, 1);
607                         break;
608                 case -EPIPE:            /* stall */
609                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
610                         set_bit(HID_CLEAR_HALT, &usbhid->iofl);
611                         schedule_work(&usbhid->reset_work);
612                         return;
613                 case -ECONNRESET:       /* unlink */
614                 case -ENOENT:
615                 case -ESHUTDOWN:        /* unplug */
616                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
617                         return;
618                 case -EILSEQ:           /* protocol error or unplug */
619                 case -EPROTO:           /* protocol error or unplug */
620                 case -ETIME:            /* protocol error or unplug */
621                 case -ETIMEDOUT:        /* Should never happen, but... */
622                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
623                         hid_io_error(hid);
624                         return;
625                 default:                /* error */
626                         warn("input irq status %d received", urb->status);
627         }
628
629         status = usb_submit_urb(urb, GFP_ATOMIC);
630         if (status) {
631                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
632                 if (status != -EPERM) {
633                         err("can't resubmit intr, %s-%s/input%d, status %d",
634                                         hid_to_usb_dev(hid)->bus->bus_name,
635                                         hid_to_usb_dev(hid)->devpath,
636                                         usbhid->ifnum, status);
637                         hid_io_error(hid);
638                 }
639         }
640 }
641
642 static int hid_submit_out(struct hid_device *hid)
643 {
644         struct hid_report *report;
645         struct usbhid_device *usbhid = hid->driver_data;
646
647         report = usbhid->out[usbhid->outtail];
648
649         hid_output_report(report, usbhid->outbuf);
650         usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
651         usbhid->urbout->dev = hid_to_usb_dev(hid);
652
653         dbg("submitting out urb");
654
655         if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
656                 err("usb_submit_urb(out) failed");
657                 return -1;
658         }
659
660         return 0;
661 }
662
663 static int hid_submit_ctrl(struct hid_device *hid)
664 {
665         struct hid_report *report;
666         unsigned char dir;
667         int len;
668         struct usbhid_device *usbhid = hid->driver_data;
669
670         report = usbhid->ctrl[usbhid->ctrltail].report;
671         dir = usbhid->ctrl[usbhid->ctrltail].dir;
672
673         len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
674         if (dir == USB_DIR_OUT) {
675                 hid_output_report(report, usbhid->ctrlbuf);
676                 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
677                 usbhid->urbctrl->transfer_buffer_length = len;
678         } else {
679                 int maxpacket, padlen;
680
681                 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
682                 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
683                 if (maxpacket > 0) {
684                         padlen = (len + maxpacket - 1) / maxpacket;
685                         padlen *= maxpacket;
686                         if (padlen > usbhid->bufsize)
687                                 padlen = usbhid->bufsize;
688                 } else
689                         padlen = 0;
690                 usbhid->urbctrl->transfer_buffer_length = padlen;
691         }
692         usbhid->urbctrl->dev = hid_to_usb_dev(hid);
693
694         usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
695         usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
696         usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
697         usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
698         usbhid->cr->wLength = cpu_to_le16(len);
699
700         dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
701                 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
702                 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
703
704         if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
705                 err("usb_submit_urb(ctrl) failed");
706                 return -1;
707         }
708
709         return 0;
710 }
711
712 /*
713  * Output interrupt completion handler.
714  */
715
716 static void hid_irq_out(struct urb *urb)
717 {
718         struct hid_device *hid = urb->context;
719         struct usbhid_device *usbhid = hid->driver_data;
720         unsigned long flags;
721         int unplug = 0;
722
723         switch (urb->status) {
724                 case 0:                 /* success */
725                         break;
726                 case -ESHUTDOWN:        /* unplug */
727                         unplug = 1;
728                 case -EILSEQ:           /* protocol error or unplug */
729                 case -EPROTO:           /* protocol error or unplug */
730                 case -ECONNRESET:       /* unlink */
731                 case -ENOENT:
732                         break;
733                 default:                /* error */
734                         warn("output irq status %d received", urb->status);
735         }
736
737         spin_lock_irqsave(&usbhid->outlock, flags);
738
739         if (unplug)
740                 usbhid->outtail = usbhid->outhead;
741         else
742                 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
743
744         if (usbhid->outhead != usbhid->outtail) {
745                 if (hid_submit_out(hid)) {
746                         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
747                         wake_up(&hid->wait);
748                 }
749                 spin_unlock_irqrestore(&usbhid->outlock, flags);
750                 return;
751         }
752
753         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
754         spin_unlock_irqrestore(&usbhid->outlock, flags);
755         wake_up(&hid->wait);
756 }
757
758 /*
759  * Control pipe completion handler.
760  */
761
762 static void hid_ctrl(struct urb *urb)
763 {
764         struct hid_device *hid = urb->context;
765         struct usbhid_device *usbhid = hid->driver_data;
766         unsigned long flags;
767         int unplug = 0;
768
769         spin_lock_irqsave(&usbhid->ctrllock, flags);
770
771         switch (urb->status) {
772                 case 0:                 /* success */
773                         if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
774                                 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
775                                                 urb->transfer_buffer, urb->actual_length, 0);
776                         break;
777                 case -ESHUTDOWN:        /* unplug */
778                         unplug = 1;
779                 case -EILSEQ:           /* protocol error or unplug */
780                 case -EPROTO:           /* protocol error or unplug */
781                 case -ECONNRESET:       /* unlink */
782                 case -ENOENT:
783                 case -EPIPE:            /* report not available */
784                         break;
785                 default:                /* error */
786                         warn("ctrl urb status %d received", urb->status);
787         }
788
789         if (unplug)
790                 usbhid->ctrltail = usbhid->ctrlhead;
791         else
792                 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
793
794         if (usbhid->ctrlhead != usbhid->ctrltail) {
795                 if (hid_submit_ctrl(hid)) {
796                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
797                         wake_up(&hid->wait);
798                 }
799                 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
800                 return;
801         }
802
803         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
804         spin_unlock_irqrestore(&usbhid->ctrllock, flags);
805         wake_up(&hid->wait);
806 }
807
808 void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
809 {
810         int head;
811         unsigned long flags;
812         struct usbhid_device *usbhid = hid->driver_data;
813
814         if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
815                 return;
816
817         if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
818
819                 spin_lock_irqsave(&usbhid->outlock, flags);
820
821                 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
822                         spin_unlock_irqrestore(&usbhid->outlock, flags);
823                         warn("output queue full");
824                         return;
825                 }
826
827                 usbhid->out[usbhid->outhead] = report;
828                 usbhid->outhead = head;
829
830                 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
831                         if (hid_submit_out(hid))
832                                 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
833
834                 spin_unlock_irqrestore(&usbhid->outlock, flags);
835                 return;
836         }
837
838         spin_lock_irqsave(&usbhid->ctrllock, flags);
839
840         if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
841                 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
842                 warn("control queue full");
843                 return;
844         }
845
846         usbhid->ctrl[usbhid->ctrlhead].report = report;
847         usbhid->ctrl[usbhid->ctrlhead].dir = dir;
848         usbhid->ctrlhead = head;
849
850         if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
851                 if (hid_submit_ctrl(hid))
852                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
853
854         spin_unlock_irqrestore(&usbhid->ctrllock, flags);
855 }
856
857 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
858 {
859         struct hid_device *hid = dev->private;
860         struct hid_field *field;
861         int offset;
862
863         if (type == EV_FF)
864                 return input_ff_event(dev, type, code, value);
865
866         if (type != EV_LED)
867                 return -1;
868
869         if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
870                 warn("event field not found");
871                 return -1;
872         }
873
874         hid_set_field(field, offset, value);
875         usbhid_submit_report(hid, field->report, USB_DIR_OUT);
876
877         return 0;
878 }
879
880 int usbhid_wait_io(struct hid_device *hid)
881 {
882         struct usbhid_device *usbhid = hid->driver_data;
883
884         if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
885                                         !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
886                                         10*HZ)) {
887                 dbg("timeout waiting for ctrl or out queue to clear");
888                 return -1;
889         }
890
891         return 0;
892 }
893
894 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
895 {
896         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
897                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
898                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
899 }
900
901 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
902                 unsigned char type, void *buf, int size)
903 {
904         int result, retries = 4;
905
906         memset(buf, 0, size);
907
908         do {
909                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
910                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
911                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
912                 retries--;
913         } while (result < size && retries);
914         return result;
915 }
916
917 int usbhid_open(struct hid_device *hid)
918 {
919         ++hid->open;
920         if (hid_start_in(hid))
921                 hid_io_error(hid);
922         return 0;
923 }
924
925 void usbhid_close(struct hid_device *hid)
926 {
927         struct usbhid_device *usbhid = hid->driver_data;
928
929         if (!--hid->open)
930                 usb_kill_urb(usbhid->urbin);
931 }
932
933 /*
934  * Initialize all reports
935  */
936
937 void usbhid_init_reports(struct hid_device *hid)
938 {
939         struct hid_report *report;
940         struct usbhid_device *usbhid = hid->driver_data;
941         int err, ret;
942
943         list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
944                 usbhid_submit_report(hid, report, USB_DIR_IN);
945
946         list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
947                 usbhid_submit_report(hid, report, USB_DIR_IN);
948
949         err = 0;
950         ret = usbhid_wait_io(hid);
951         while (ret) {
952                 err |= ret;
953                 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
954                         usb_kill_urb(usbhid->urbctrl);
955                 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
956                         usb_kill_urb(usbhid->urbout);
957                 ret = usbhid_wait_io(hid);
958         }
959
960         if (err)
961                 warn("timeout initializing reports");
962 }
963
964 /*
965  * Traverse the supplied list of reports and find the longest
966  */
967 static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
968 {
969         struct hid_report *report;
970         int size;
971
972         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
973                 size = ((report->size - 1) >> 3) + 1;
974                 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
975                         size++;
976                 if (*max < size)
977                         *max = size;
978         }
979 }
980
981 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
982 {
983         struct usbhid_device *usbhid = hid->driver_data;
984
985         if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
986                 return -1;
987         if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
988                 return -1;
989         if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
990                 return -1;
991         if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
992                 return -1;
993
994         return 0;
995 }
996
997 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
998 {
999         struct usbhid_device *usbhid = hid->driver_data;
1000
1001         if (usbhid->inbuf)
1002                 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1003         if (usbhid->outbuf)
1004                 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
1005         if (usbhid->cr)
1006                 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
1007         if (usbhid->ctrlbuf)
1008                 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
1009 }
1010
1011 /*
1012  * Cherry Cymotion keyboard have an invalid HID report descriptor,
1013  * that needs fixing before we can parse it.
1014  */
1015
1016 static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
1017 {
1018         if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
1019                 info("Fixing up Cherry Cymotion report descriptor");
1020                 rdesc[11] = rdesc[16] = 0xff;
1021                 rdesc[12] = rdesc[17] = 0x03;
1022         }
1023 }
1024
1025 /*
1026  * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1027  * to "operational".  Without this, the ps3 controller will not report any
1028  * events.
1029  */
1030 static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
1031 {
1032         int result;
1033         char *buf = kmalloc(18, GFP_KERNEL);
1034
1035         if (!buf)
1036                 return;
1037
1038         result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1039                                  HID_REQ_GET_REPORT,
1040                                  USB_DIR_IN | USB_TYPE_CLASS |
1041                                  USB_RECIP_INTERFACE,
1042                                  (3 << 8) | 0xf2, ifnum, buf, 17,
1043                                  USB_CTRL_GET_TIMEOUT);
1044
1045         if (result < 0)
1046                 err("%s failed: %d\n", __func__, result);
1047
1048         kfree(buf);
1049 }
1050
1051 /*
1052  * Logitech S510 keyboard sends in report #3 keys which are far
1053  * above the logical maximum described in descriptor. This extends
1054  * the original value of 0x28c of logical maximum to 0x104d
1055  */
1056 static void hid_fixup_s510_descriptor(unsigned char *rdesc, int rsize)
1057 {
1058         if (rsize >= 90 && rdesc[83] == 0x26
1059                         && rdesc[84] == 0x8c
1060                         && rdesc[85] == 0x02) {
1061                 info("Fixing up Logitech S510 report descriptor");
1062                 rdesc[84] = rdesc[89] = 0x4d;
1063                 rdesc[85] = rdesc[90] = 0x10;
1064         }
1065 }
1066
1067 static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1068 {
1069         struct usb_host_interface *interface = intf->cur_altsetting;
1070         struct usb_device *dev = interface_to_usbdev (intf);
1071         struct hid_descriptor *hdesc;
1072         struct hid_device *hid;
1073         __u32 quirks = 0;
1074         unsigned rsize = 0;
1075         char *rdesc;
1076         int n, len, insize = 0;
1077         struct usbhid_device *usbhid;
1078
1079         /* Ignore all Wacom devices */
1080         if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_WACOM)
1081                 return NULL;
1082         /* ignore all Code Mercenaries IOWarrior devices */
1083         if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_CODEMERCS)
1084                 if (le16_to_cpu(dev->descriptor.idProduct) >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1085                     le16_to_cpu(dev->descriptor.idProduct) <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1086                         return NULL;
1087
1088         for (n = 0; hid_blacklist[n].idVendor; n++)
1089                 if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
1090                         (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
1091                                 quirks = hid_blacklist[n].quirks;
1092
1093         /* Many keyboards and mice don't like to be polled for reports,
1094          * so we will always set the HID_QUIRK_NOGET flag for them. */
1095         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1096                 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1097                         interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1098                                 quirks |= HID_QUIRK_NOGET;
1099         }
1100
1101         if (quirks & HID_QUIRK_IGNORE)
1102                 return NULL;
1103
1104         if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
1105                 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
1106                         return NULL;
1107
1108
1109         if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1110             (!interface->desc.bNumEndpoints ||
1111              usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1112                 dbg("class descriptor not present\n");
1113                 return NULL;
1114         }
1115
1116         for (n = 0; n < hdesc->bNumDescriptors; n++)
1117                 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1118                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1119
1120         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1121                 dbg("weird size of report descriptor (%u)", rsize);
1122                 return NULL;
1123         }
1124
1125         if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1126                 dbg("couldn't allocate rdesc memory");
1127                 return NULL;
1128         }
1129
1130         hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1131
1132         if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1133                 dbg("reading report descriptor failed");
1134                 kfree(rdesc);
1135                 return NULL;
1136         }
1137
1138         if ((quirks & HID_QUIRK_CYMOTION))
1139                 hid_fixup_cymotion_descriptor(rdesc, rsize);
1140
1141         if (quirks & HID_QUIRK_LOGITECH_S510_DESCRIPTOR)
1142                 hid_fixup_s510_descriptor(rdesc, rsize);
1143
1144 #ifdef CONFIG_HID_DEBUG
1145         printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1146         for (n = 0; n < rsize; n++)
1147                 printk(" %02x", (unsigned char) rdesc[n]);
1148         printk("\n");
1149 #endif
1150
1151         if (!(hid = hid_parse_report(rdesc, n))) {
1152                 dbg("parsing report descriptor failed");
1153                 kfree(rdesc);
1154                 return NULL;
1155         }
1156
1157         kfree(rdesc);
1158         hid->quirks = quirks;
1159
1160         if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
1161                 goto fail;
1162
1163         hid->driver_data = usbhid;
1164         usbhid->hid = hid;
1165
1166         usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1167         hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1168         hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1169         hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1170
1171         if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1172                 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
1173
1174         hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1175
1176         if (insize > HID_MAX_BUFFER_SIZE)
1177                 insize = HID_MAX_BUFFER_SIZE;
1178
1179         if (hid_alloc_buffers(dev, hid)) {
1180                 hid_free_buffers(dev, hid);
1181                 goto fail;
1182         }
1183
1184         for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1185
1186                 struct usb_endpoint_descriptor *endpoint;
1187                 int pipe;
1188                 int interval;
1189
1190                 endpoint = &interface->endpoint[n].desc;
1191                 if ((endpoint->bmAttributes & 3) != 3)          /* Not an interrupt endpoint */
1192                         continue;
1193
1194                 interval = endpoint->bInterval;
1195
1196                 /* Change the polling interval of mice. */
1197                 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1198                         interval = hid_mousepoll_interval;
1199
1200                 if (usb_endpoint_dir_in(endpoint)) {
1201                         if (usbhid->urbin)
1202                                 continue;
1203                         if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1204                                 goto fail;
1205                         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1206                         usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1207                                          hid_irq_in, hid, interval);
1208                         usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1209                         usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1210                 } else {
1211                         if (usbhid->urbout)
1212                                 continue;
1213                         if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1214                                 goto fail;
1215                         pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1216                         usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1217                                          hid_irq_out, hid, interval);
1218                         usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1219                         usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1220                 }
1221         }
1222
1223         if (!usbhid->urbin) {
1224                 err("couldn't find an input interrupt endpoint");
1225                 goto fail;
1226         }
1227
1228         init_waitqueue_head(&hid->wait);
1229
1230         INIT_WORK(&usbhid->reset_work, hid_reset);
1231         setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1232
1233         spin_lock_init(&usbhid->inlock);
1234         spin_lock_init(&usbhid->outlock);
1235         spin_lock_init(&usbhid->ctrllock);
1236
1237         hid->version = le16_to_cpu(hdesc->bcdHID);
1238         hid->country = hdesc->bCountryCode;
1239         hid->dev = &intf->dev;
1240         usbhid->intf = intf;
1241         usbhid->ifnum = interface->desc.bInterfaceNumber;
1242
1243         hid->name[0] = 0;
1244
1245         if (dev->manufacturer)
1246                 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1247
1248         if (dev->product) {
1249                 if (dev->manufacturer)
1250                         strlcat(hid->name, " ", sizeof(hid->name));
1251                 strlcat(hid->name, dev->product, sizeof(hid->name));
1252         }
1253
1254         if (!strlen(hid->name))
1255                 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1256                          le16_to_cpu(dev->descriptor.idVendor),
1257                          le16_to_cpu(dev->descriptor.idProduct));
1258
1259         hid->bus = BUS_USB;
1260         hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1261         hid->product = le16_to_cpu(dev->descriptor.idProduct);
1262
1263         usb_make_path(dev, hid->phys, sizeof(hid->phys));
1264         strlcat(hid->phys, "/input", sizeof(hid->phys));
1265         len = strlen(hid->phys);
1266         if (len < sizeof(hid->phys) - 1)
1267                 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1268                          "%d", intf->altsetting[0].desc.bInterfaceNumber);
1269
1270         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1271                 hid->uniq[0] = 0;
1272
1273         usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1274         if (!usbhid->urbctrl)
1275                 goto fail;
1276
1277         usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1278                              usbhid->ctrlbuf, 1, hid_ctrl, hid);
1279         usbhid->urbctrl->setup_dma = usbhid->cr_dma;
1280         usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1281         usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
1282         hid->hidinput_input_event = usb_hidinput_input_event;
1283         hid->hid_open = usbhid_open;
1284         hid->hid_close = usbhid_close;
1285 #ifdef CONFIG_USB_HIDDEV
1286         hid->hiddev_hid_event = hiddev_hid_event;
1287         hid->hiddev_report_event = hiddev_report_event;
1288 #endif
1289         return hid;
1290
1291 fail:
1292         usb_free_urb(usbhid->urbin);
1293         usb_free_urb(usbhid->urbout);
1294         usb_free_urb(usbhid->urbctrl);
1295         hid_free_buffers(dev, hid);
1296         hid_free_device(hid);
1297
1298         return NULL;
1299 }
1300
1301 static void hid_disconnect(struct usb_interface *intf)
1302 {
1303         struct hid_device *hid = usb_get_intfdata (intf);
1304         struct usbhid_device *usbhid;
1305
1306         if (!hid)
1307                 return;
1308
1309         usbhid = hid->driver_data;
1310
1311         spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1312         usb_set_intfdata(intf, NULL);
1313         spin_unlock_irq(&usbhid->inlock);
1314         usb_kill_urb(usbhid->urbin);
1315         usb_kill_urb(usbhid->urbout);
1316         usb_kill_urb(usbhid->urbctrl);
1317
1318         del_timer_sync(&usbhid->io_retry);
1319         flush_scheduled_work();
1320
1321         if (hid->claimed & HID_CLAIMED_INPUT)
1322                 hidinput_disconnect(hid);
1323         if (hid->claimed & HID_CLAIMED_HIDDEV)
1324                 hiddev_disconnect(hid);
1325
1326         usb_free_urb(usbhid->urbin);
1327         usb_free_urb(usbhid->urbctrl);
1328         usb_free_urb(usbhid->urbout);
1329
1330         hid_free_buffers(hid_to_usb_dev(hid), hid);
1331         hid_free_device(hid);
1332 }
1333
1334 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1335 {
1336         struct hid_device *hid;
1337         char path[64];
1338         int i;
1339         char *c;
1340
1341         dbg("HID probe called for ifnum %d",
1342                         intf->altsetting->desc.bInterfaceNumber);
1343
1344         if (!(hid = usb_hid_configure(intf)))
1345                 return -ENODEV;
1346
1347         usbhid_init_reports(hid);
1348         hid_dump_device(hid);
1349
1350         if (!hidinput_connect(hid))
1351                 hid->claimed |= HID_CLAIMED_INPUT;
1352         if (!hiddev_connect(hid))
1353                 hid->claimed |= HID_CLAIMED_HIDDEV;
1354
1355         usb_set_intfdata(intf, hid);
1356
1357         if (!hid->claimed) {
1358                 printk ("HID device not claimed by input or hiddev\n");
1359                 hid_disconnect(intf);
1360                 return -ENODEV;
1361         }
1362
1363         if ((hid->claimed & HID_CLAIMED_INPUT))
1364                 hid_ff_init(hid);
1365
1366         if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
1367                 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
1368                         intf->cur_altsetting->desc.bInterfaceNumber);
1369
1370         printk(KERN_INFO);
1371
1372         if (hid->claimed & HID_CLAIMED_INPUT)
1373                 printk("input");
1374         if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1375                 printk(",");
1376         if (hid->claimed & HID_CLAIMED_HIDDEV)
1377                 printk("hiddev%d", hid->minor);
1378
1379         c = "Device";
1380         for (i = 0; i < hid->maxcollection; i++) {
1381                 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1382                     (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1383                     (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1384                         c = hid_types[hid->collection[i].usage & 0xffff];
1385                         break;
1386                 }
1387         }
1388
1389         usb_make_path(interface_to_usbdev(intf), path, 63);
1390
1391         printk(": USB HID v%x.%02x %s [%s] on %s\n",
1392                 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1393
1394         return 0;
1395 }
1396
1397 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1398 {
1399         struct hid_device *hid = usb_get_intfdata (intf);
1400         struct usbhid_device *usbhid = hid->driver_data;
1401
1402         spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1403         set_bit(HID_SUSPENDED, &usbhid->iofl);
1404         spin_unlock_irq(&usbhid->inlock);
1405         del_timer(&usbhid->io_retry);
1406         usb_kill_urb(usbhid->urbin);
1407         dev_dbg(&intf->dev, "suspend\n");
1408         return 0;
1409 }
1410
1411 static int hid_resume(struct usb_interface *intf)
1412 {
1413         struct hid_device *hid = usb_get_intfdata (intf);
1414         struct usbhid_device *usbhid = hid->driver_data;
1415         int status;
1416
1417         clear_bit(HID_SUSPENDED, &usbhid->iofl);
1418         usbhid->retry_delay = 0;
1419         status = hid_start_in(hid);
1420         dev_dbg(&intf->dev, "resume status %d\n", status);
1421         return status;
1422 }
1423
1424 /* Treat USB reset pretty much the same as suspend/resume */
1425 static void hid_pre_reset(struct usb_interface *intf)
1426 {
1427         /* FIXME: What if the interface is already suspended? */
1428         hid_suspend(intf, PMSG_ON);
1429 }
1430
1431 static void hid_post_reset(struct usb_interface *intf)
1432 {
1433         struct usb_device *dev = interface_to_usbdev (intf);
1434
1435         hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1436         /* FIXME: Any more reinitialization needed? */
1437
1438         hid_resume(intf);
1439 }
1440
1441 static struct usb_device_id hid_usb_ids [] = {
1442         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1443                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1444         { }                                             /* Terminating entry */
1445 };
1446
1447 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1448
1449 static struct usb_driver hid_driver = {
1450         .name =         "usbhid",
1451         .probe =        hid_probe,
1452         .disconnect =   hid_disconnect,
1453         .suspend =      hid_suspend,
1454         .resume =       hid_resume,
1455         .pre_reset =    hid_pre_reset,
1456         .post_reset =   hid_post_reset,
1457         .id_table =     hid_usb_ids,
1458 };
1459
1460 static int __init hid_init(void)
1461 {
1462         int retval;
1463         retval = hiddev_init();
1464         if (retval)
1465                 goto hiddev_init_fail;
1466         retval = usb_register(&hid_driver);
1467         if (retval)
1468                 goto usb_register_fail;
1469         info(DRIVER_VERSION ":" DRIVER_DESC);
1470
1471         return 0;
1472 usb_register_fail:
1473         hiddev_exit();
1474 hiddev_init_fail:
1475         return retval;
1476 }
1477
1478 static void __exit hid_exit(void)
1479 {
1480         usb_deregister(&hid_driver);
1481         hiddev_exit();
1482 }
1483
1484 module_init(hid_init);
1485 module_exit(hid_exit);
1486
1487 MODULE_AUTHOR(DRIVER_AUTHOR);
1488 MODULE_DESCRIPTION(DRIVER_DESC);
1489 MODULE_LICENSE(DRIVER_LICENSE);