blob: 5e430813d4d4602578757504b5a9d0585aea9191 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB HID support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
Michael Haboustakbf0964d2005-09-05 00:12:01 -05005 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Jiri Kosinab55fd232007-02-21 19:27:49 +01007 * Copyright (c) 2006-2007 Jiri Kosina
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/usb.h>
31
Jiri Kosinadde58452006-12-08 18:40:44 +010032#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010034#include <linux/hid-debug.h>
Jiri Kosina4916b3a2006-12-08 18:41:03 +010035#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Version Information
39 */
40
Michael Haboustakbf0964d2005-09-05 00:12:01 -050041#define DRIVER_VERSION "v2.6"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
43#define DRIVER_DESC "USB HID core driver"
44#define DRIVER_LICENSE "GPL"
45
46static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
47 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
48/*
49 * Module parameters.
50 */
51
52static unsigned int hid_mousepoll_interval;
53module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55
Jiri Kosina48b45542007-04-03 23:39:37 +020056#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_BERKSHIRE 0x0c98
110#define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
111
112#define USB_VENDOR_ID_CHERRY 0x046a
113#define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
114
115#define USB_VENDOR_ID_CHIC 0x05fe
116#define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
117
118#define USB_VENDOR_ID_CIDC 0x1677
119
120#define USB_VENDOR_ID_CODEMERCS 0x07c0
121#define USB_DEVICE_ID_CODEMERCS_IOW_FIRST 0x1500
122#define USB_DEVICE_ID_CODEMERCS_IOW_LAST 0x15ff
123
124#define USB_VENDOR_ID_CYPRESS 0x04b4
125#define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
126#define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
127#define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
128
129#define USB_VENDOR_ID_DELORME 0x1163
130#define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
131#define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
132
133#define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
134#define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
135
136#define USB_VENDOR_ID_GLAB 0x06c2
137#define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
138#define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
139#define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
140#define USB_DEVICE_ID_0_16_16_IF_KIT 0x0044
141#define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
142#define USB_DEVICE_ID_0_8_7_IF_KIT 0x0051
143#define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
144#define USB_DEVICE_ID_PHIDGET_MOTORCONTROL 0x0058
145
146#define USB_VENDOR_ID_GRIFFIN 0x077d
147#define USB_DEVICE_ID_POWERMATE 0x0410
148#define USB_DEVICE_ID_SOUNDKNOB 0x04AA
149
150#define USB_VENDOR_ID_GTCO 0x078c
151#define USB_DEVICE_ID_GTCO_90 0x0090
152#define USB_DEVICE_ID_GTCO_100 0x0100
153#define USB_DEVICE_ID_GTCO_101 0x0101
154#define USB_DEVICE_ID_GTCO_103 0x0103
155#define USB_DEVICE_ID_GTCO_104 0x0104
156#define USB_DEVICE_ID_GTCO_105 0x0105
157#define USB_DEVICE_ID_GTCO_106 0x0106
158#define USB_DEVICE_ID_GTCO_107 0x0107
159#define USB_DEVICE_ID_GTCO_108 0x0108
160#define USB_DEVICE_ID_GTCO_200 0x0200
161#define USB_DEVICE_ID_GTCO_201 0x0201
162#define USB_DEVICE_ID_GTCO_202 0x0202
163#define USB_DEVICE_ID_GTCO_203 0x0203
164#define USB_DEVICE_ID_GTCO_204 0x0204
165#define USB_DEVICE_ID_GTCO_205 0x0205
166#define USB_DEVICE_ID_GTCO_206 0x0206
167#define USB_DEVICE_ID_GTCO_207 0x0207
168#define USB_DEVICE_ID_GTCO_300 0x0300
169#define USB_DEVICE_ID_GTCO_301 0x0301
170#define USB_DEVICE_ID_GTCO_302 0x0302
171#define USB_DEVICE_ID_GTCO_303 0x0303
172#define USB_DEVICE_ID_GTCO_304 0x0304
173#define USB_DEVICE_ID_GTCO_305 0x0305
174#define USB_DEVICE_ID_GTCO_306 0x0306
175#define USB_DEVICE_ID_GTCO_307 0x0307
176#define USB_DEVICE_ID_GTCO_308 0x0308
177#define USB_DEVICE_ID_GTCO_309 0x0309
178#define USB_DEVICE_ID_GTCO_400 0x0400
179#define USB_DEVICE_ID_GTCO_401 0x0401
180#define USB_DEVICE_ID_GTCO_402 0x0402
181#define USB_DEVICE_ID_GTCO_403 0x0403
182#define USB_DEVICE_ID_GTCO_404 0x0404
183#define USB_DEVICE_ID_GTCO_405 0x0405
184#define USB_DEVICE_ID_GTCO_500 0x0500
185#define USB_DEVICE_ID_GTCO_501 0x0501
186#define USB_DEVICE_ID_GTCO_502 0x0502
187#define USB_DEVICE_ID_GTCO_503 0x0503
188#define USB_DEVICE_ID_GTCO_504 0x0504
189#define USB_DEVICE_ID_GTCO_1000 0x1000
190#define USB_DEVICE_ID_GTCO_1001 0x1001
191#define USB_DEVICE_ID_GTCO_1002 0x1002
192#define USB_DEVICE_ID_GTCO_1003 0x1003
193#define USB_DEVICE_ID_GTCO_1004 0x1004
194#define USB_DEVICE_ID_GTCO_1005 0x1005
195#define USB_DEVICE_ID_GTCO_1006 0x1006
196
197#define USB_VENDOR_ID_HAPP 0x078b
198#define USB_DEVICE_ID_UGCI_DRIVING 0x0010
199#define USB_DEVICE_ID_UGCI_FLYING 0x0020
200#define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
201
202#define USB_VENDOR_ID_IMATION 0x0718
203#define USB_DEVICE_ID_DISC_STAKKA 0xd000
204
205#define USB_VENDOR_ID_KBGEAR 0x084e
206#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
207
208#define USB_VENDOR_ID_LD 0x0f11
209#define USB_DEVICE_ID_LD_CASSY 0x1000
210#define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
211#define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
212#define USB_DEVICE_ID_LD_JWM 0x1080
213#define USB_DEVICE_ID_LD_DMMP 0x1081
214#define USB_DEVICE_ID_LD_UMIP 0x1090
215#define USB_DEVICE_ID_LD_XRAY1 0x1100
216#define USB_DEVICE_ID_LD_XRAY2 0x1101
217#define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
218#define USB_DEVICE_ID_LD_COM3LAB 0x2000
219#define USB_DEVICE_ID_LD_TELEPORT 0x2010
220#define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
221#define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
222#define USB_DEVICE_ID_LD_MACHINETEST 0x2040
223
224#define USB_VENDOR_ID_LOGITECH 0x046d
225#define USB_DEVICE_ID_LOGITECH_USB_RECEIVER 0xc101
226#define USB_DEVICE_ID_LOGITECH_USB_RECEIVER_2 0xc517
227#define USB_DEVICE_ID_DINOVO_EDGE 0xc714
228
229#define USB_VENDOR_ID_MCC 0x09db
230#define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
231#define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
232
233#define USB_VENDOR_ID_MGE 0x0463
234#define USB_DEVICE_ID_MGE_UPS 0xffff
235#define USB_DEVICE_ID_MGE_UPS1 0x0001
236
237#define USB_VENDOR_ID_NEC 0x073e
238#define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
239
240#define USB_VENDOR_ID_ONTRAK 0x0a07
241#define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
242
243#define USB_VENDOR_ID_PANJIT 0x134c
244
245#define USB_VENDOR_ID_PANTHERLORD 0x0810
246#define USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK 0x0001
247
248#define USB_VENDOR_ID_SAITEK 0x06a3
249#define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
250
251#define USB_VENDOR_ID_SONY 0x054c
252#define USB_DEVICE_ID_SONY_PS3_CONTROLLER 0x0268
253
254#define USB_VENDOR_ID_SUN 0x0430
255#define USB_DEVICE_ID_RARITAN_KVM_DONGLE 0xcdab
256
257#define USB_VENDOR_ID_TOPMAX 0x0663
258#define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
259
260#define USB_VENDOR_ID_TURBOX 0x062a
261#define USB_DEVICE_ID_TURBOX_KEYBOARD 0x0201
262
263#define USB_VENDOR_ID_VERNIER 0x08f7
264#define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
265#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
266#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
267#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
268
269#define USB_VENDOR_ID_WACOM 0x056a
270
271#define USB_VENDOR_ID_WISEGROUP 0x0925
272#define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
273#define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
274#define USB_DEVICE_ID_8_8_4_IF_KIT 0x8201
275#define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
276
277#define USB_VENDOR_ID_WISEGROUP_LTD 0x6677
278#define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
279
280#define USB_VENDOR_ID_YEALINK 0x6993
281#define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
282
283/*
284 * Alphabetically sorted blacklist by quirk type.
285 */
286
287static const struct hid_blacklist {
288 __u16 idVendor;
289 __u16 idProduct;
Paul Walmsley4cbe7d22007-03-20 19:23:16 +0100290 __u32 quirks;
Jiri Kosina48b45542007-04-03 23:39:37 +0200291} hid_blacklist[] = {
292
293 { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
294 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
295
296 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_USB_RECEIVER, HID_QUIRK_BAD_RELATIVE_KEYS },
297
298 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
299 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
300 { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
301 { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
302 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
303 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
304 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
305 { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
306 { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
307 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
308
309 { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_CYMOTION },
310
311 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE, HID_QUIRK_DUPLICATE_USAGES },
312
313 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
314 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
315 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
316 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
317 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
318 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
319 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
320 { USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1, HID_QUIRK_IGNORE },
321 { USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232, HID_QUIRK_IGNORE },
322 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IR, HID_QUIRK_IGNORE },
323 { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
324 { USB_VENDOR_ID_CIDC, 0x0103, HID_QUIRK_IGNORE },
325 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
326 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
327 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
328 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
329 { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
330 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
331 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
332 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
333 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT, HID_QUIRK_IGNORE },
334 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
335 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT, HID_QUIRK_IGNORE },
336 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
337 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL, HID_QUIRK_IGNORE },
338 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
339 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
340 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90, HID_QUIRK_IGNORE },
341 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100, HID_QUIRK_IGNORE },
342 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101, HID_QUIRK_IGNORE },
343 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103, HID_QUIRK_IGNORE },
344 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104, HID_QUIRK_IGNORE },
345 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105, HID_QUIRK_IGNORE },
346 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106, HID_QUIRK_IGNORE },
347 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107, HID_QUIRK_IGNORE },
348 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108, HID_QUIRK_IGNORE },
349 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200, HID_QUIRK_IGNORE },
350 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201, HID_QUIRK_IGNORE },
351 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202, HID_QUIRK_IGNORE },
352 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203, HID_QUIRK_IGNORE },
353 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204, HID_QUIRK_IGNORE },
354 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205, HID_QUIRK_IGNORE },
355 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206, HID_QUIRK_IGNORE },
356 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207, HID_QUIRK_IGNORE },
357 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300, HID_QUIRK_IGNORE },
358 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301, HID_QUIRK_IGNORE },
359 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302, HID_QUIRK_IGNORE },
360 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303, HID_QUIRK_IGNORE },
361 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304, HID_QUIRK_IGNORE },
362 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305, HID_QUIRK_IGNORE },
363 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306, HID_QUIRK_IGNORE },
364 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307, HID_QUIRK_IGNORE },
365 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308, HID_QUIRK_IGNORE },
366 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309, HID_QUIRK_IGNORE },
367 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400, HID_QUIRK_IGNORE },
368 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401, HID_QUIRK_IGNORE },
369 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402, HID_QUIRK_IGNORE },
370 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403, HID_QUIRK_IGNORE },
371 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404, HID_QUIRK_IGNORE },
372 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405, HID_QUIRK_IGNORE },
373 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500, HID_QUIRK_IGNORE },
374 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501, HID_QUIRK_IGNORE },
375 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502, HID_QUIRK_IGNORE },
376 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503, HID_QUIRK_IGNORE },
377 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504, HID_QUIRK_IGNORE },
378 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000, HID_QUIRK_IGNORE },
379 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001, HID_QUIRK_IGNORE },
380 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002, HID_QUIRK_IGNORE },
381 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003, HID_QUIRK_IGNORE },
382 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004, HID_QUIRK_IGNORE },
383 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005, HID_QUIRK_IGNORE },
384 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006, HID_QUIRK_IGNORE },
385 { USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA, HID_QUIRK_IGNORE },
386 { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
387 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
388 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
389 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
390 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
391 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
392 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
393 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
394 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
395 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
396 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
397 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
398 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
399 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
400 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
401 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
402 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
403 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
404 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
405 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
406 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20, HID_QUIRK_IGNORE },
407 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30, HID_QUIRK_IGNORE },
408 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
409 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108, HID_QUIRK_IGNORE },
410 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118, HID_QUIRK_IGNORE },
411 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
412 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
413 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
414 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
415 { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
416 { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
417 { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
418 { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
419 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
420 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
421 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
422 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
423 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
424 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
425 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT, HID_QUIRK_IGNORE },
426 { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
427
428 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
429 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
430
431 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_USB_RECEIVER_2, HID_QUIRK_LOGITECH_S510_DESCRIPTOR },
432
433 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
434
435 { USB_VENDOR_ID_PANTHERLORD, USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK, HID_QUIRK_MULTI_INPUT | HID_QUIRK_SKIP_OUTPUT_REPORTS },
436
437 { USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER, HID_QUIRK_SONY_PS3_CONTROLLER },
438
439 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
440 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
441 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
442 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
443 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
444 { USB_VENDOR_ID_SUN, USB_DEVICE_ID_RARITAN_KVM_DONGLE, HID_QUIRK_NOGET },
445 { USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_TURBOX_KEYBOARD, HID_QUIRK_NOGET },
446 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
447 { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
448
449 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
450 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
451 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
452 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
453 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
454 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
455 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
456 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
457 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
458 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
459 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
460 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
461 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
462
463 { 0, 0 }
464};
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/*
Alan Sternaef4e262006-01-31 12:58:38 -0500467 * Input submission and I/O error handler.
468 */
469
470static void hid_io_error(struct hid_device *hid);
471
472/* Start up the input URB */
473static int hid_start_in(struct hid_device *hid)
474{
475 unsigned long flags;
476 int rc = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100477 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500478
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100479 spin_lock_irqsave(&usbhid->inlock, flags);
480 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
481 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
482 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500483 if (rc != 0)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100484 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500485 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100486 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500487 return rc;
488}
489
490/* I/O retry timer routine */
491static void hid_retry_timeout(unsigned long _hid)
492{
493 struct hid_device *hid = (struct hid_device *) _hid;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100494 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500495
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100496 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
Alan Sternaef4e262006-01-31 12:58:38 -0500497 if (hid_start_in(hid))
498 hid_io_error(hid);
499}
500
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400501/* Workqueue routine to reset the device or clear a halt */
David Howellsc4028952006-11-22 14:57:56 +0000502static void hid_reset(struct work_struct *work)
Alan Sternaef4e262006-01-31 12:58:38 -0500503{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100504 struct usbhid_device *usbhid =
505 container_of(work, struct usbhid_device, reset_work);
506 struct hid_device *hid = usbhid->hid;
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400507 int rc_lock, rc = 0;
Alan Sternaef4e262006-01-31 12:58:38 -0500508
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100509 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
510 dev_dbg(&usbhid->intf->dev, "clear halt\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200511 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100512 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400513 hid_start_in(hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500514 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400515
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100516 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
517 dev_dbg(&usbhid->intf->dev, "resetting device\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200518 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400519 if (rc_lock >= 0) {
Anssi Hannulabe820972007-01-19 19:28:17 +0200520 rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400521 if (rc_lock)
Anssi Hannulabe820972007-01-19 19:28:17 +0200522 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400523 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100524 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400525 }
Alan Sternaef4e262006-01-31 12:58:38 -0500526
Alan Sterndf9a1f42006-06-01 13:55:28 -0400527 switch (rc) {
528 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100529 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500530 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400531 break;
532 default:
Alan Sternaef4e262006-01-31 12:58:38 -0500533 err("can't reset device, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200534 hid_to_usb_dev(hid)->bus->bus_name,
535 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100536 usbhid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400537 /* FALLTHROUGH */
538 case -EHOSTUNREACH:
539 case -ENODEV:
540 case -EINTR:
541 break;
542 }
Alan Sternaef4e262006-01-31 12:58:38 -0500543}
544
545/* Main I/O error handler */
546static void hid_io_error(struct hid_device *hid)
547{
548 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100549 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500550
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100551 spin_lock_irqsave(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500552
553 /* Stop when disconnected */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100554 if (usb_get_intfdata(usbhid->intf) == NULL)
Alan Sternaef4e262006-01-31 12:58:38 -0500555 goto done;
556
Alan Stern5e2a55f2007-03-20 19:03:31 +0100557 /* If it has been a while since the last error, we'll assume
558 * this a brand new error and reset the retry timeout. */
559 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
560 usbhid->retry_delay = 0;
561
Alan Sternaef4e262006-01-31 12:58:38 -0500562 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100563 if (usbhid->retry_delay == 0) {
564 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
565 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
566 } else if (usbhid->retry_delay < 100)
567 usbhid->retry_delay *= 2;
Alan Sternaef4e262006-01-31 12:58:38 -0500568
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100569 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500570
571 /* Retries failed, so do a port reset */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100572 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
573 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400574 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500575 }
576 }
577
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100578 mod_timer(&usbhid->io_retry,
579 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500580done:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100581 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500582}
583
584/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * Input interrupt completion handler.
586 */
587
David Howells7d12e782006-10-05 14:55:46 +0100588static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100591 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int status;
593
594 switch (urb->status) {
595 case 0: /* success */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100596 usbhid->retry_delay = 0;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100597 hid_input_report(urb->context, HID_INPUT_REPORT,
598 urb->transfer_buffer,
599 urb->actual_length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400601 case -EPIPE: /* stall */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100602 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
603 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
604 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400605 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 case -ECONNRESET: /* unlink */
607 case -ENOENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 case -ESHUTDOWN: /* unplug */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100609 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return;
Alan Sternaef4e262006-01-31 12:58:38 -0500611 case -EILSEQ: /* protocol error or unplug */
612 case -EPROTO: /* protocol error or unplug */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700613 case -ETIME: /* protocol error or unplug */
614 case -ETIMEDOUT: /* Should never happen, but... */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100615 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500616 hid_io_error(hid);
617 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 default: /* error */
619 warn("input irq status %d received", urb->status);
620 }
621
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800622 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500623 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100624 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500625 if (status != -EPERM) {
626 err("can't resubmit intr, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200627 hid_to_usb_dev(hid)->bus->bus_name,
628 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100629 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500630 hid_io_error(hid);
631 }
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static int hid_submit_out(struct hid_device *hid)
636{
637 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100638 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100640 report = usbhid->out[usbhid->outtail];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100642 hid_output_report(report, usbhid->outbuf);
643 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Anssi Hannulabe820972007-01-19 19:28:17 +0200644 usbhid->urbout->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 dbg("submitting out urb");
647
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100648 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 err("usb_submit_urb(out) failed");
650 return -1;
651 }
652
653 return 0;
654}
655
656static int hid_submit_ctrl(struct hid_device *hid)
657{
658 struct hid_report *report;
659 unsigned char dir;
660 int len;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100661 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100663 report = usbhid->ctrl[usbhid->ctrltail].report;
664 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
667 if (dir == USB_DIR_OUT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100668 hid_output_report(report, usbhid->ctrlbuf);
Anssi Hannulabe820972007-01-19 19:28:17 +0200669 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100670 usbhid->urbctrl->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 } else {
672 int maxpacket, padlen;
673
Anssi Hannulabe820972007-01-19 19:28:17 +0200674 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
675 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (maxpacket > 0) {
677 padlen = (len + maxpacket - 1) / maxpacket;
678 padlen *= maxpacket;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100679 if (padlen > usbhid->bufsize)
680 padlen = usbhid->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 } else
682 padlen = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100683 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Anssi Hannulabe820972007-01-19 19:28:17 +0200685 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100687 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
688 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
689 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
690 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
691 usbhid->cr->wLength = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100694 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
695 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100697 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 err("usb_submit_urb(ctrl) failed");
699 return -1;
700 }
701
702 return 0;
703}
704
705/*
706 * Output interrupt completion handler.
707 */
708
David Howells7d12e782006-10-05 14:55:46 +0100709static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100712 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 unsigned long flags;
714 int unplug = 0;
715
716 switch (urb->status) {
717 case 0: /* success */
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -0500718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -0500721 case -EILSEQ: /* protocol error or unplug */
722 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 case -ECONNRESET: /* unlink */
724 case -ENOENT:
725 break;
726 default: /* error */
727 warn("output irq status %d received", urb->status);
728 }
729
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100730 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100733 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100735 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100737 if (usbhid->outhead != usbhid->outtail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100739 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 wake_up(&hid->wait);
741 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100742 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return;
744 }
745
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100746 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
747 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 wake_up(&hid->wait);
749}
750
751/*
752 * Control pipe completion handler.
753 */
754
David Howells7d12e782006-10-05 14:55:46 +0100755static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100758 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 unsigned long flags;
760 int unplug = 0;
761
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100762 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 switch (urb->status) {
765 case 0: /* success */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100766 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100767 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
768 urb->transfer_buffer, urb->actual_length, 0);
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -0500769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -0500772 case -EILSEQ: /* protocol error or unplug */
773 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case -ECONNRESET: /* unlink */
775 case -ENOENT:
776 case -EPIPE: /* report not available */
777 break;
778 default: /* error */
779 warn("ctrl urb status %d received", urb->status);
780 }
781
782 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100783 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100785 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100787 if (usbhid->ctrlhead != usbhid->ctrltail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100789 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 wake_up(&hid->wait);
791 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100792 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return;
794 }
795
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100796 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
797 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 wake_up(&hid->wait);
799}
800
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100801void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 int head;
804 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100805 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
808 return;
809
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100810 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100812 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100814 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
815 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 warn("output queue full");
817 return;
818 }
819
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100820 usbhid->out[usbhid->outhead] = report;
821 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100823 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (hid_submit_out(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100825 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100827 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return;
829 }
830
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100831 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100833 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
834 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 warn("control queue full");
836 return;
837 }
838
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100839 usbhid->ctrl[usbhid->ctrlhead].report = report;
840 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
841 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100843 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (hid_submit_ctrl(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100845 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100847 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Jiri Kosina229695e2006-12-08 18:40:53 +0100850static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100851{
852 struct hid_device *hid = dev->private;
853 struct hid_field *field;
854 int offset;
855
856 if (type == EV_FF)
857 return input_ff_event(dev, type, code, value);
858
859 if (type != EV_LED)
860 return -1;
861
862 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
863 warn("event field not found");
864 return -1;
865 }
866
867 hid_set_field(field, offset, value);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100868 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
Jiri Kosinadde58452006-12-08 18:40:44 +0100869
870 return 0;
871}
872
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100873int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100875 struct usbhid_device *usbhid = hid->driver_data;
876
877 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
878 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 10*HZ)) {
880 dbg("timeout waiting for ctrl or out queue to clear");
881 return -1;
882 }
883
884 return 0;
885}
886
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500887static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
888{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500889 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500890 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
891 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
895 unsigned char type, void *buf, int size)
896{
897 int result, retries = 4;
898
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100899 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 do {
902 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
903 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
904 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
905 retries--;
906 } while (result < size && retries);
907 return result;
908}
909
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100910int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Alan Sternaef4e262006-01-31 12:58:38 -0500912 ++hid->open;
913 if (hid_start_in(hid))
914 hid_io_error(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100918void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100920 struct usbhid_device *usbhid = hid->driver_data;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!--hid->open)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100923 usb_kill_urb(usbhid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/*
927 * Initialize all reports
928 */
929
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100930void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100933 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int err, ret;
935
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500936 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100937 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100940 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100943 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 while (ret) {
945 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100946 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
947 usb_kill_urb(usbhid->urbctrl);
948 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
949 usb_kill_urb(usbhid->urbout);
950 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
953 if (err)
Olaf Heringde289fd2006-01-06 12:45:28 +0100954 warn("timeout initializing reports");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500957/*
958 * Traverse the supplied list of reports and find the longest
959 */
960static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
961{
962 struct hid_report *report;
963 int size;
964
965 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
966 size = ((report->size - 1) >> 3) + 1;
967 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
968 size++;
969 if (*max < size)
970 *max = size;
971 }
972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
975{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100976 struct usbhid_device *usbhid = hid->driver_data;
977
978 if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100980 if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100982 if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100984 if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -1;
986
987 return 0;
988}
989
990static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
991{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100992 struct usbhid_device *usbhid = hid->driver_data;
993
994 if (usbhid->inbuf)
995 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
996 if (usbhid->outbuf)
997 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
998 if (usbhid->cr)
999 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
1000 if (usbhid->ctrlbuf)
1001 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001004/*
1005 * Cherry Cymotion keyboard have an invalid HID report descriptor,
1006 * that needs fixing before we can parse it.
1007 */
1008
1009static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
1010{
1011 if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
1012 info("Fixing up Cherry Cymotion report descriptor");
1013 rdesc[11] = rdesc[16] = 0xff;
1014 rdesc[12] = rdesc[17] = 0x03;
1015 }
1016}
1017
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001018/*
1019 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1020 * to "operational". Without this, the ps3 controller will not report any
1021 * events.
1022 */
1023static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
1024{
1025 int result;
1026 char *buf = kmalloc(18, GFP_KERNEL);
1027
1028 if (!buf)
1029 return;
1030
1031 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1032 HID_REQ_GET_REPORT,
1033 USB_DIR_IN | USB_TYPE_CLASS |
1034 USB_RECIP_INTERFACE,
1035 (3 << 8) | 0xf2, ifnum, buf, 17,
1036 USB_CTRL_GET_TIMEOUT);
1037
1038 if (result < 0)
1039 err("%s failed: %d\n", __func__, result);
1040
1041 kfree(buf);
1042}
1043
Jiri Kosinab55fd232007-02-21 19:27:49 +01001044/*
1045 * Logitech S510 keyboard sends in report #3 keys which are far
1046 * above the logical maximum described in descriptor. This extends
1047 * the original value of 0x28c of logical maximum to 0x104d
1048 */
1049static void hid_fixup_s510_descriptor(unsigned char *rdesc, int rsize)
1050{
1051 if (rsize >= 90 && rdesc[83] == 0x26
1052 && rdesc[84] == 0x8c
1053 && rdesc[85] == 0x02) {
1054 info("Fixing up Logitech S510 report descriptor");
1055 rdesc[84] = rdesc[89] = 0x4d;
1056 rdesc[85] = rdesc[90] = 0x10;
1057 }
1058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1061{
1062 struct usb_host_interface *interface = intf->cur_altsetting;
1063 struct usb_device *dev = interface_to_usbdev (intf);
1064 struct hid_descriptor *hdesc;
1065 struct hid_device *hid;
Paul Walmsley4cbe7d22007-03-20 19:23:16 +01001066 __u32 quirks = 0;
1067 unsigned rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001068 char *rdesc;
1069 int n, len, insize = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001070 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Robert Marquardt5ddc3222007-03-02 16:01:52 +01001072 /* Ignore all Wacom devices */
1073 if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_WACOM)
1074 return NULL;
1075 /* ignore all Code Mercenaries IOWarrior devices */
1076 if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_CODEMERCS)
1077 if (le16_to_cpu(dev->descriptor.idProduct) >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1078 le16_to_cpu(dev->descriptor.idProduct) <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1079 return NULL;
Ping Chengea186652006-07-14 16:50:04 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 for (n = 0; hid_blacklist[n].idVendor; n++)
1082 if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
1083 (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
1084 quirks = hid_blacklist[n].quirks;
1085
Alan Stern0f28b552006-05-15 14:49:04 -04001086 /* Many keyboards and mice don't like to be polled for reports,
1087 * so we will always set the HID_QUIRK_NOGET flag for them. */
1088 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1089 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1090 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1091 quirks |= HID_QUIRK_NOGET;
1092 }
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (quirks & HID_QUIRK_IGNORE)
1095 return NULL;
1096
Soeren Sonnenburga417a212007-02-05 10:06:01 +01001097 if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
1098 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
1099 return NULL;
1100
1101
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001102 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1103 (!interface->desc.bNumEndpoints ||
1104 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1105 dbg("class descriptor not present\n");
1106 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
1109 for (n = 0; n < hdesc->bNumDescriptors; n++)
1110 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1111 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1112
1113 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1114 dbg("weird size of report descriptor (%u)", rsize);
1115 return NULL;
1116 }
1117
1118 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1119 dbg("couldn't allocate rdesc memory");
1120 return NULL;
1121 }
1122
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001123 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1126 dbg("reading report descriptor failed");
1127 kfree(rdesc);
1128 return NULL;
1129 }
1130
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001131 if ((quirks & HID_QUIRK_CYMOTION))
1132 hid_fixup_cymotion_descriptor(rdesc, rsize);
1133
Jiri Kosinab55fd232007-02-21 19:27:49 +01001134 if (quirks & HID_QUIRK_LOGITECH_S510_DESCRIPTOR)
1135 hid_fixup_s510_descriptor(rdesc, rsize);
1136
Jiri Kosinafdc9c562007-02-19 14:15:59 +01001137#ifdef CONFIG_HID_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1139 for (n = 0; n < rsize; n++)
1140 printk(" %02x", (unsigned char) rdesc[n]);
1141 printk("\n");
1142#endif
1143
1144 if (!(hid = hid_parse_report(rdesc, n))) {
1145 dbg("parsing report descriptor failed");
1146 kfree(rdesc);
1147 return NULL;
1148 }
1149
1150 kfree(rdesc);
1151 hid->quirks = quirks;
1152
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001153 if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
1154 goto fail;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001155
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001156 hid->driver_data = usbhid;
1157 usbhid->hid = hid;
1158
1159 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1160 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1161 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1162 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1163
1164 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1165 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001166
1167 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1168
1169 if (insize > HID_MAX_BUFFER_SIZE)
1170 insize = HID_MAX_BUFFER_SIZE;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (hid_alloc_buffers(dev, hid)) {
1173 hid_free_buffers(dev, hid);
1174 goto fail;
1175 }
1176
1177 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1178
1179 struct usb_endpoint_descriptor *endpoint;
1180 int pipe;
1181 int interval;
1182
1183 endpoint = &interface->endpoint[n].desc;
1184 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
1185 continue;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /* Change the polling interval of mice. */
1190 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1191 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -05001192
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001193 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001194 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001196 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 goto fail;
1198 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001199 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001201 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1202 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001204 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001206 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto fail;
1208 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001209 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001211 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1212 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 }
1215
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001216 if (!usbhid->urbin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 err("couldn't find an input interrupt endpoint");
1218 goto fail;
1219 }
1220
1221 init_waitqueue_head(&hid->wait);
1222
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001223 INIT_WORK(&usbhid->reset_work, hid_reset);
1224 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
Alan Sternaef4e262006-01-31 12:58:38 -05001225
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001226 spin_lock_init(&usbhid->inlock);
1227 spin_lock_init(&usbhid->outlock);
1228 spin_lock_init(&usbhid->ctrllock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 hid->version = le16_to_cpu(hdesc->bcdHID);
1231 hid->country = hdesc->bCountryCode;
Anssi Hannulabe820972007-01-19 19:28:17 +02001232 hid->dev = &intf->dev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001233 usbhid->intf = intf;
1234 usbhid->ifnum = interface->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 hid->name[0] = 0;
1237
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001238 if (dev->manufacturer)
1239 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001241 if (dev->product) {
1242 if (dev->manufacturer)
1243 strlcat(hid->name, " ", sizeof(hid->name));
1244 strlcat(hid->name, dev->product, sizeof(hid->name));
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001247 if (!strlen(hid->name))
1248 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1249 le16_to_cpu(dev->descriptor.idVendor),
1250 le16_to_cpu(dev->descriptor.idProduct));
1251
Jiri Kosina229695e2006-12-08 18:40:53 +01001252 hid->bus = BUS_USB;
Julien BLACHE9fa2ad52007-02-11 18:20:25 +01001253 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1254 hid->product = le16_to_cpu(dev->descriptor.idProduct);
Jiri Kosina229695e2006-12-08 18:40:53 +01001255
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001256 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1257 strlcat(hid->phys, "/input", sizeof(hid->phys));
1258 len = strlen(hid->phys);
1259 if (len < sizeof(hid->phys) - 1)
1260 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1261 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1264 hid->uniq[0] = 0;
1265
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001266 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1267 if (!usbhid->urbctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 goto fail;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001269
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001270 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1271 usbhid->ctrlbuf, 1, hid_ctrl, hid);
1272 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
1273 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1274 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
Jiri Kosina229695e2006-12-08 18:40:53 +01001275 hid->hidinput_input_event = usb_hidinput_input_event;
Jiri Kosina7c379142007-01-24 11:54:19 +01001276 hid->hid_open = usbhid_open;
1277 hid->hid_close = usbhid_close;
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001278#ifdef CONFIG_USB_HIDDEV
1279 hid->hiddev_hid_event = hiddev_hid_event;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001280 hid->hiddev_report_event = hiddev_report_event;
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return hid;
1283
1284fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001285 usb_free_urb(usbhid->urbin);
1286 usb_free_urb(usbhid->urbout);
1287 usb_free_urb(usbhid->urbctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 hid_free_buffers(dev, hid);
1289 hid_free_device(hid);
1290
1291 return NULL;
1292}
1293
1294static void hid_disconnect(struct usb_interface *intf)
1295{
1296 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001297 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (!hid)
1300 return;
1301
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001302 usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001304 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1305 usb_set_intfdata(intf, NULL);
1306 spin_unlock_irq(&usbhid->inlock);
1307 usb_kill_urb(usbhid->urbin);
1308 usb_kill_urb(usbhid->urbout);
1309 usb_kill_urb(usbhid->urbctrl);
1310
1311 del_timer_sync(&usbhid->io_retry);
Alan Sternaef4e262006-01-31 12:58:38 -05001312 flush_scheduled_work();
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (hid->claimed & HID_CLAIMED_INPUT)
1315 hidinput_disconnect(hid);
1316 if (hid->claimed & HID_CLAIMED_HIDDEV)
1317 hiddev_disconnect(hid);
1318
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001319 usb_free_urb(usbhid->urbin);
1320 usb_free_urb(usbhid->urbctrl);
1321 usb_free_urb(usbhid->urbout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Anssi Hannulabe820972007-01-19 19:28:17 +02001323 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 hid_free_device(hid);
1325}
1326
1327static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1328{
1329 struct hid_device *hid;
1330 char path[64];
1331 int i;
1332 char *c;
1333
1334 dbg("HID probe called for ifnum %d",
1335 intf->altsetting->desc.bInterfaceNumber);
1336
1337 if (!(hid = usb_hid_configure(intf)))
Stelian Pop479f6ea2005-06-22 17:53:28 +02001338 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001340 usbhid_init_reports(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 hid_dump_device(hid);
1342
1343 if (!hidinput_connect(hid))
1344 hid->claimed |= HID_CLAIMED_INPUT;
1345 if (!hiddev_connect(hid))
1346 hid->claimed |= HID_CLAIMED_HIDDEV;
1347
1348 usb_set_intfdata(intf, hid);
1349
1350 if (!hid->claimed) {
1351 printk ("HID device not claimed by input or hiddev\n");
1352 hid_disconnect(intf);
Stelian Pop479f6ea2005-06-22 17:53:28 +02001353 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Anssi Hannulac4146062007-01-11 16:51:16 +02001356 if ((hid->claimed & HID_CLAIMED_INPUT))
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001357 hid_ff_init(hid);
1358
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001359 if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
1360 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
1361 intf->cur_altsetting->desc.bInterfaceNumber);
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 printk(KERN_INFO);
1364
1365 if (hid->claimed & HID_CLAIMED_INPUT)
1366 printk("input");
1367 if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1368 printk(",");
1369 if (hid->claimed & HID_CLAIMED_HIDDEV)
1370 printk("hiddev%d", hid->minor);
1371
1372 c = "Device";
1373 for (i = 0; i < hid->maxcollection; i++) {
1374 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1375 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1376 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1377 c = hid_types[hid->collection[i].usage & 0xffff];
1378 break;
1379 }
1380 }
1381
1382 usb_make_path(interface_to_usbdev(intf), path, 63);
1383
1384 printk(": USB HID v%x.%02x %s [%s] on %s\n",
1385 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1386
1387 return 0;
1388}
1389
David Brownell27d72e82005-04-18 17:39:22 -07001390static int hid_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001393 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001395 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1396 set_bit(HID_SUSPENDED, &usbhid->iofl);
1397 spin_unlock_irq(&usbhid->inlock);
1398 del_timer(&usbhid->io_retry);
1399 usb_kill_urb(usbhid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 dev_dbg(&intf->dev, "suspend\n");
1401 return 0;
1402}
1403
1404static int hid_resume(struct usb_interface *intf)
1405{
1406 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001407 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int status;
1409
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001410 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1411 usbhid->retry_delay = 0;
Alan Sternaef4e262006-01-31 12:58:38 -05001412 status = hid_start_in(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 dev_dbg(&intf->dev, "resume status %d\n", status);
1414 return status;
1415}
1416
Alan Sterndf9a1f42006-06-01 13:55:28 -04001417/* Treat USB reset pretty much the same as suspend/resume */
1418static void hid_pre_reset(struct usb_interface *intf)
1419{
1420 /* FIXME: What if the interface is already suspended? */
1421 hid_suspend(intf, PMSG_ON);
1422}
1423
1424static void hid_post_reset(struct usb_interface *intf)
1425{
1426 struct usb_device *dev = interface_to_usbdev (intf);
1427
1428 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1429 /* FIXME: Any more reinitialization needed? */
1430
1431 hid_resume(intf);
1432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434static struct usb_device_id hid_usb_ids [] = {
1435 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1436 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1437 { } /* Terminating entry */
1438};
1439
1440MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1441
1442static struct usb_driver hid_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 .name = "usbhid",
1444 .probe = hid_probe,
1445 .disconnect = hid_disconnect,
1446 .suspend = hid_suspend,
1447 .resume = hid_resume,
Alan Sterndf9a1f42006-06-01 13:55:28 -04001448 .pre_reset = hid_pre_reset,
1449 .post_reset = hid_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 .id_table = hid_usb_ids,
1451};
1452
1453static int __init hid_init(void)
1454{
1455 int retval;
1456 retval = hiddev_init();
1457 if (retval)
1458 goto hiddev_init_fail;
1459 retval = usb_register(&hid_driver);
1460 if (retval)
1461 goto usb_register_fail;
1462 info(DRIVER_VERSION ":" DRIVER_DESC);
1463
1464 return 0;
1465usb_register_fail:
1466 hiddev_exit();
1467hiddev_init_fail:
1468 return retval;
1469}
1470
1471static void __exit hid_exit(void)
1472{
1473 usb_deregister(&hid_driver);
1474 hiddev_exit();
1475}
1476
1477module_init(hid_init);
1478module_exit(hid_exit);
1479
1480MODULE_AUTHOR(DRIVER_AUTHOR);
1481MODULE_DESCRIPTION(DRIVER_DESC);
1482MODULE_LICENSE(DRIVER_LICENSE);