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