blob: ac2493fd40cd171ab967cbc1aab068c782a16d6c [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"
Jiri Kosinaf142b3a2007-04-16 11:29:28 +020042#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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
Daniel P. Engel66df5142007-04-03 23:43:46 +0200109#define USB_VENDOR_ID_BELKIN 0x050d
110#define USB_DEVICE_ID_FLIP_KVM 0x3201
111
Jiri Kosina48b45542007-04-03 23:39:37 +0200112#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
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200132#define USB_VENDOR_ID_DELL 0x413c
133#define USB_DEVICE_ID_DELL_W7658 0x2005
134
Jiri Kosina48b45542007-04-03 23:39:37 +0200135#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
Jiri Kosina85cbea32007-04-05 12:23:09 +0200231#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
Jiri Kosina48b45542007-04-03 23:39:37 +0200235#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
Paul Zaremba4d5a1be2007-04-18 15:29:47 +0200256#define USB_VENDOR_ID_PLAYDOTCOM 0x0b43
257#define USB_DEVICE_ID_PLAYDOTCOM_EMS_USBII 0x0003
258
Jiri Kosina48b45542007-04-03 23:39:37 +0200259#define USB_VENDOR_ID_SAITEK 0x06a3
260#define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
261
262#define USB_VENDOR_ID_SONY 0x054c
263#define USB_DEVICE_ID_SONY_PS3_CONTROLLER 0x0268
264
265#define USB_VENDOR_ID_SUN 0x0430
266#define USB_DEVICE_ID_RARITAN_KVM_DONGLE 0xcdab
267
268#define USB_VENDOR_ID_TOPMAX 0x0663
269#define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
270
271#define USB_VENDOR_ID_TURBOX 0x062a
272#define USB_DEVICE_ID_TURBOX_KEYBOARD 0x0201
273
274#define USB_VENDOR_ID_VERNIER 0x08f7
275#define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
276#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
277#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
278#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
279
280#define USB_VENDOR_ID_WACOM 0x056a
281
282#define USB_VENDOR_ID_WISEGROUP 0x0925
283#define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
284#define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
285#define USB_DEVICE_ID_8_8_4_IF_KIT 0x8201
Sam Liddicott42cfb632007-04-10 15:50:45 +0200286#define USB_DEVICE_ID_QUAD_USB_JOYPAD 0x8800
287#define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
Jiri Kosina48b45542007-04-03 23:39:37 +0200288
289#define USB_VENDOR_ID_WISEGROUP_LTD 0x6677
290#define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
291
292#define USB_VENDOR_ID_YEALINK 0x6993
293#define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
294
295/*
296 * Alphabetically sorted blacklist by quirk type.
297 */
298
299static const struct hid_blacklist {
300 __u16 idVendor;
301 __u16 idProduct;
Paul Walmsley4cbe7d22007-03-20 19:23:16 +0100302 __u32 quirks;
Jiri Kosina48b45542007-04-03 23:39:37 +0200303} hid_blacklist[] = {
304
305 { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
306 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
307
Jiri Kosina85cbea32007-04-05 12:23:09 +0200308 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER, HID_QUIRK_BAD_RELATIVE_KEYS },
Jiri Kosina48b45542007-04-03 23:39:37 +0200309
310 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
311 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
312 { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
313 { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
314 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
315 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
316 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
317 { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
318 { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
319 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
320
321 { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_CYMOTION },
322
323 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE, HID_QUIRK_DUPLICATE_USAGES },
324
Daniel P. Engel66df5142007-04-03 23:43:46 +0200325 { USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM, HID_QUIRK_HIDDEV },
326
Jiri Kosina48b45542007-04-03 23:39:37 +0200327 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
328 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
329 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
330 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
331 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
332 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
333 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
334 { USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1, HID_QUIRK_IGNORE },
335 { USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232, HID_QUIRK_IGNORE },
336 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IR, HID_QUIRK_IGNORE },
337 { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
338 { USB_VENDOR_ID_CIDC, 0x0103, HID_QUIRK_IGNORE },
339 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
340 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
341 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
342 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
343 { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
344 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
345 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
346 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
347 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT, HID_QUIRK_IGNORE },
348 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
349 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT, HID_QUIRK_IGNORE },
350 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
351 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL, HID_QUIRK_IGNORE },
352 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
353 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
354 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90, HID_QUIRK_IGNORE },
355 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100, HID_QUIRK_IGNORE },
356 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101, HID_QUIRK_IGNORE },
357 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103, HID_QUIRK_IGNORE },
358 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104, HID_QUIRK_IGNORE },
359 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105, HID_QUIRK_IGNORE },
360 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106, HID_QUIRK_IGNORE },
361 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107, HID_QUIRK_IGNORE },
362 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108, HID_QUIRK_IGNORE },
363 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200, HID_QUIRK_IGNORE },
364 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201, HID_QUIRK_IGNORE },
365 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202, HID_QUIRK_IGNORE },
366 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203, HID_QUIRK_IGNORE },
367 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204, HID_QUIRK_IGNORE },
368 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205, HID_QUIRK_IGNORE },
369 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206, HID_QUIRK_IGNORE },
370 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207, HID_QUIRK_IGNORE },
371 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300, HID_QUIRK_IGNORE },
372 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301, HID_QUIRK_IGNORE },
373 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302, HID_QUIRK_IGNORE },
374 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303, HID_QUIRK_IGNORE },
375 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304, HID_QUIRK_IGNORE },
376 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305, HID_QUIRK_IGNORE },
377 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306, HID_QUIRK_IGNORE },
378 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307, HID_QUIRK_IGNORE },
379 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308, HID_QUIRK_IGNORE },
380 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309, HID_QUIRK_IGNORE },
381 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400, HID_QUIRK_IGNORE },
382 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401, HID_QUIRK_IGNORE },
383 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402, HID_QUIRK_IGNORE },
384 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403, HID_QUIRK_IGNORE },
385 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404, HID_QUIRK_IGNORE },
386 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405, HID_QUIRK_IGNORE },
387 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500, HID_QUIRK_IGNORE },
388 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501, HID_QUIRK_IGNORE },
389 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502, HID_QUIRK_IGNORE },
390 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503, HID_QUIRK_IGNORE },
391 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504, HID_QUIRK_IGNORE },
392 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000, HID_QUIRK_IGNORE },
393 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001, HID_QUIRK_IGNORE },
394 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002, HID_QUIRK_IGNORE },
395 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003, HID_QUIRK_IGNORE },
396 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004, HID_QUIRK_IGNORE },
397 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005, HID_QUIRK_IGNORE },
398 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006, HID_QUIRK_IGNORE },
399 { USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA, HID_QUIRK_IGNORE },
400 { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
401 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
402 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
403 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
404 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
405 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
406 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
407 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
408 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
409 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
410 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
411 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
412 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
413 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
414 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
415 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
416 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
417 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
418 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
419 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
420 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20, HID_QUIRK_IGNORE },
421 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30, HID_QUIRK_IGNORE },
422 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
423 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108, HID_QUIRK_IGNORE },
424 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118, HID_QUIRK_IGNORE },
425 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
426 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
427 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
428 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
429 { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
430 { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
431 { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
432 { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
433 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
434 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
435 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
436 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
437 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
438 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
439 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT, HID_QUIRK_IGNORE },
440 { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
441
442 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
443 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
444
Jiri Kosina85cbea32007-04-05 12:23:09 +0200445 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER, HID_QUIRK_LOGITECH_DESCRIPTOR },
446 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER, HID_QUIRK_LOGITECH_DESCRIPTOR },
447 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2, HID_QUIRK_LOGITECH_DESCRIPTOR },
Jiri Kosina48b45542007-04-03 23:39:37 +0200448
449 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
450
451 { USB_VENDOR_ID_PANTHERLORD, USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK, HID_QUIRK_MULTI_INPUT | HID_QUIRK_SKIP_OUTPUT_REPORTS },
Paul Zaremba4d5a1be2007-04-18 15:29:47 +0200452 { USB_VENDOR_ID_PLAYDOTCOM, USB_DEVICE_ID_PLAYDOTCOM_EMS_USBII, HID_QUIRK_MULTI_INPUT },
Jiri Kosina48b45542007-04-03 23:39:37 +0200453
454 { USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER, HID_QUIRK_SONY_PS3_CONTROLLER },
455
456 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
457 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
458 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
459 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
460 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
461 { USB_VENDOR_ID_SUN, USB_DEVICE_ID_RARITAN_KVM_DONGLE, HID_QUIRK_NOGET },
462 { USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_TURBOX_KEYBOARD, HID_QUIRK_NOGET },
463 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
Sam Liddicott42cfb632007-04-10 15:50:45 +0200464 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_QUAD_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
465
Jiri Kosina48b45542007-04-03 23:39:37 +0200466 { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
467
468 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
469 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
470 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
471 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
472 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
473 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
474 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
475 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
476 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
477 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_POWERBOOK_ISO_KEYBOARD},
478 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
479 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
480 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY, HID_QUIRK_POWERBOOK_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
481
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200482 { USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_W7658, HID_QUIRK_RESET_LEDS },
483
Jiri Kosina48b45542007-04-03 23:39:37 +0200484 { 0, 0 }
485};
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
Alan Sternaef4e262006-01-31 12:58:38 -0500488 * Input submission and I/O error handler.
489 */
490
491static void hid_io_error(struct hid_device *hid);
492
493/* Start up the input URB */
494static int hid_start_in(struct hid_device *hid)
495{
496 unsigned long flags;
497 int rc = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100498 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500499
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100500 spin_lock_irqsave(&usbhid->inlock, flags);
501 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
502 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
503 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500504 if (rc != 0)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100505 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500506 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100507 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500508 return rc;
509}
510
511/* I/O retry timer routine */
512static void hid_retry_timeout(unsigned long _hid)
513{
514 struct hid_device *hid = (struct hid_device *) _hid;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100515 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500516
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100517 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
Alan Sternaef4e262006-01-31 12:58:38 -0500518 if (hid_start_in(hid))
519 hid_io_error(hid);
520}
521
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400522/* Workqueue routine to reset the device or clear a halt */
David Howellsc4028952006-11-22 14:57:56 +0000523static void hid_reset(struct work_struct *work)
Alan Sternaef4e262006-01-31 12:58:38 -0500524{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100525 struct usbhid_device *usbhid =
526 container_of(work, struct usbhid_device, reset_work);
527 struct hid_device *hid = usbhid->hid;
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400528 int rc_lock, rc = 0;
Alan Sternaef4e262006-01-31 12:58:38 -0500529
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100530 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
531 dev_dbg(&usbhid->intf->dev, "clear halt\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200532 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100533 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400534 hid_start_in(hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500535 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400536
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100537 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
538 dev_dbg(&usbhid->intf->dev, "resetting device\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200539 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400540 if (rc_lock >= 0) {
Anssi Hannulabe820972007-01-19 19:28:17 +0200541 rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400542 if (rc_lock)
Anssi Hannulabe820972007-01-19 19:28:17 +0200543 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400544 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100545 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400546 }
Alan Sternaef4e262006-01-31 12:58:38 -0500547
Alan Sterndf9a1f42006-06-01 13:55:28 -0400548 switch (rc) {
549 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100550 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500551 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400552 break;
553 default:
Alan Sternaef4e262006-01-31 12:58:38 -0500554 err("can't reset device, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200555 hid_to_usb_dev(hid)->bus->bus_name,
556 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100557 usbhid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400558 /* FALLTHROUGH */
559 case -EHOSTUNREACH:
560 case -ENODEV:
561 case -EINTR:
562 break;
563 }
Alan Sternaef4e262006-01-31 12:58:38 -0500564}
565
566/* Main I/O error handler */
567static void hid_io_error(struct hid_device *hid)
568{
569 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100570 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500571
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100572 spin_lock_irqsave(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500573
574 /* Stop when disconnected */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100575 if (usb_get_intfdata(usbhid->intf) == NULL)
Alan Sternaef4e262006-01-31 12:58:38 -0500576 goto done;
577
Alan Stern5e2a55f2007-03-20 19:03:31 +0100578 /* If it has been a while since the last error, we'll assume
579 * this a brand new error and reset the retry timeout. */
580 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
581 usbhid->retry_delay = 0;
582
Alan Sternaef4e262006-01-31 12:58:38 -0500583 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100584 if (usbhid->retry_delay == 0) {
585 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
586 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
587 } else if (usbhid->retry_delay < 100)
588 usbhid->retry_delay *= 2;
Alan Sternaef4e262006-01-31 12:58:38 -0500589
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100590 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500591
592 /* Retries failed, so do a port reset */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100593 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
594 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400595 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500596 }
597 }
598
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100599 mod_timer(&usbhid->io_retry,
600 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500601done:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100602 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500603}
604
605/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * Input interrupt completion handler.
607 */
608
David Howells7d12e782006-10-05 14:55:46 +0100609static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100612 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int status;
614
615 switch (urb->status) {
616 case 0: /* success */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100617 usbhid->retry_delay = 0;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100618 hid_input_report(urb->context, HID_INPUT_REPORT,
619 urb->transfer_buffer,
620 urb->actual_length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400622 case -EPIPE: /* stall */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100623 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
624 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
625 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400626 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 case -ECONNRESET: /* unlink */
628 case -ENOENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 case -ESHUTDOWN: /* unplug */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100630 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return;
Alan Sternaef4e262006-01-31 12:58:38 -0500632 case -EILSEQ: /* protocol error or unplug */
633 case -EPROTO: /* protocol error or unplug */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700634 case -ETIME: /* protocol error or unplug */
635 case -ETIMEDOUT: /* Should never happen, but... */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100636 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500637 hid_io_error(hid);
638 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 default: /* error */
640 warn("input irq status %d received", urb->status);
641 }
642
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800643 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500644 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100645 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500646 if (status != -EPERM) {
647 err("can't resubmit intr, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200648 hid_to_usb_dev(hid)->bus->bus_name,
649 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100650 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500651 hid_io_error(hid);
652 }
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static int hid_submit_out(struct hid_device *hid)
657{
658 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100659 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100661 report = usbhid->out[usbhid->outtail];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100663 hid_output_report(report, usbhid->outbuf);
664 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Anssi Hannulabe820972007-01-19 19:28:17 +0200665 usbhid->urbout->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 dbg("submitting out urb");
668
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100669 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 err("usb_submit_urb(out) failed");
671 return -1;
672 }
673
674 return 0;
675}
676
677static int hid_submit_ctrl(struct hid_device *hid)
678{
679 struct hid_report *report;
680 unsigned char dir;
681 int len;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100682 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100684 report = usbhid->ctrl[usbhid->ctrltail].report;
685 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
688 if (dir == USB_DIR_OUT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100689 hid_output_report(report, usbhid->ctrlbuf);
Anssi Hannulabe820972007-01-19 19:28:17 +0200690 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100691 usbhid->urbctrl->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 } else {
693 int maxpacket, padlen;
694
Anssi Hannulabe820972007-01-19 19:28:17 +0200695 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
696 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (maxpacket > 0) {
698 padlen = (len + maxpacket - 1) / maxpacket;
699 padlen *= maxpacket;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100700 if (padlen > usbhid->bufsize)
701 padlen = usbhid->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 } else
703 padlen = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100704 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Anssi Hannulabe820972007-01-19 19:28:17 +0200706 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100708 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
709 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
710 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
711 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
712 usbhid->cr->wLength = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100715 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
716 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100718 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 err("usb_submit_urb(ctrl) failed");
720 return -1;
721 }
722
723 return 0;
724}
725
726/*
727 * Output interrupt completion handler.
728 */
729
David Howells7d12e782006-10-05 14:55:46 +0100730static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100733 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned long flags;
735 int unplug = 0;
736
737 switch (urb->status) {
738 case 0: /* success */
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -0500739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -0500742 case -EILSEQ: /* protocol error or unplug */
743 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case -ECONNRESET: /* unlink */
745 case -ENOENT:
746 break;
747 default: /* error */
748 warn("output irq status %d received", urb->status);
749 }
750
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100751 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100754 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100756 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100758 if (usbhid->outhead != usbhid->outtail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100760 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 wake_up(&hid->wait);
762 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100763 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return;
765 }
766
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100767 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
768 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 wake_up(&hid->wait);
770}
771
772/*
773 * Control pipe completion handler.
774 */
775
David Howells7d12e782006-10-05 14:55:46 +0100776static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100779 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 unsigned long flags;
781 int unplug = 0;
782
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100783 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 switch (urb->status) {
786 case 0: /* success */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100787 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100788 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
789 urb->transfer_buffer, urb->actual_length, 0);
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -0500790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -0500793 case -EILSEQ: /* protocol error or unplug */
794 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 case -ECONNRESET: /* unlink */
796 case -ENOENT:
797 case -EPIPE: /* report not available */
798 break;
799 default: /* error */
800 warn("ctrl urb status %d received", urb->status);
801 }
802
803 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100804 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100806 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100808 if (usbhid->ctrlhead != usbhid->ctrltail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100810 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 wake_up(&hid->wait);
812 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100813 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815 }
816
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100817 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
818 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 wake_up(&hid->wait);
820}
821
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100822void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 int head;
825 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100826 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
829 return;
830
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100831 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100833 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100835 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
836 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 warn("output queue full");
838 return;
839 }
840
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100841 usbhid->out[usbhid->outhead] = report;
842 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100844 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (hid_submit_out(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100846 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100848 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return;
850 }
851
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100852 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100854 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
855 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 warn("control queue full");
857 return;
858 }
859
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100860 usbhid->ctrl[usbhid->ctrlhead].report = report;
861 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
862 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100864 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (hid_submit_ctrl(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100866 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100868 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Jiri Kosina229695e2006-12-08 18:40:53 +0100871static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100872{
873 struct hid_device *hid = dev->private;
874 struct hid_field *field;
875 int offset;
876
877 if (type == EV_FF)
878 return input_ff_event(dev, type, code, value);
879
880 if (type != EV_LED)
881 return -1;
882
883 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
884 warn("event field not found");
885 return -1;
886 }
887
888 hid_set_field(field, offset, value);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100889 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
Jiri Kosinadde58452006-12-08 18:40:44 +0100890
891 return 0;
892}
893
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100894int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100896 struct usbhid_device *usbhid = hid->driver_data;
897
898 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
899 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 10*HZ)) {
901 dbg("timeout waiting for ctrl or out queue to clear");
902 return -1;
903 }
904
905 return 0;
906}
907
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500908static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
909{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500910 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500911 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
912 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
916 unsigned char type, void *buf, int size)
917{
918 int result, retries = 4;
919
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100920 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 do {
923 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
924 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
925 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
926 retries--;
927 } while (result < size && retries);
928 return result;
929}
930
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100931int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Alan Sternaef4e262006-01-31 12:58:38 -0500933 ++hid->open;
934 if (hid_start_in(hid))
935 hid_io_error(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100939void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100941 struct usbhid_device *usbhid = hid->driver_data;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!--hid->open)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100944 usb_kill_urb(usbhid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/*
948 * Initialize all reports
949 */
950
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100951void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100954 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int err, ret;
956
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500957 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100958 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100961 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100964 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 while (ret) {
966 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100967 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
968 usb_kill_urb(usbhid->urbctrl);
969 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
970 usb_kill_urb(usbhid->urbout);
971 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
974 if (err)
Olaf Heringde289fd2006-01-06 12:45:28 +0100975 warn("timeout initializing reports");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500978/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200979 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
980 */
981static int hid_find_field_early(struct hid_device *hid, unsigned int page,
982 unsigned int hid_code, struct hid_field **pfield)
983{
984 struct hid_report *report;
985 struct hid_field *field;
986 struct hid_usage *usage;
987 int i, j;
988
989 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
990 for (i = 0; i < report->maxfield; i++) {
991 field = report->field[i];
992 for (j = 0; j < field->maxusage; j++) {
993 usage = &field->usage[j];
994 if ((usage->hid & HID_USAGE_PAGE) == page &&
995 (usage->hid & 0xFFFF) == hid_code) {
996 *pfield = field;
997 return j;
998 }
999 }
1000 }
1001 }
1002 return -1;
1003}
1004
1005static void usbhid_set_leds(struct hid_device *hid)
1006{
1007 struct hid_field *field;
1008 int offset;
1009
1010 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
1011 hid_set_field(field, offset, 0);
1012 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
1013 }
1014}
1015
1016/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001017 * Traverse the supplied list of reports and find the longest
1018 */
1019static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
1020{
1021 struct hid_report *report;
1022 int size;
1023
1024 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
1025 size = ((report->size - 1) >> 3) + 1;
1026 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
1027 size++;
1028 if (*max < size)
1029 *max = size;
1030 }
1031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
1034{
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001035 struct usbhid_device *usbhid = hid->driver_data;
1036
1037 if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001039 if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001041 if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001043 if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return -1;
1045
1046 return 0;
1047}
1048
1049static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1050{
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001051 struct usbhid_device *usbhid = hid->driver_data;
1052
1053 if (usbhid->inbuf)
1054 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1055 if (usbhid->outbuf)
1056 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
1057 if (usbhid->cr)
1058 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
1059 if (usbhid->ctrlbuf)
1060 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001063/*
1064 * Cherry Cymotion keyboard have an invalid HID report descriptor,
1065 * that needs fixing before we can parse it.
1066 */
1067
1068static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
1069{
1070 if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
1071 info("Fixing up Cherry Cymotion report descriptor");
1072 rdesc[11] = rdesc[16] = 0xff;
1073 rdesc[12] = rdesc[17] = 0x03;
1074 }
1075}
1076
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001077/*
1078 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1079 * to "operational". Without this, the ps3 controller will not report any
1080 * events.
1081 */
1082static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
1083{
1084 int result;
1085 char *buf = kmalloc(18, GFP_KERNEL);
1086
1087 if (!buf)
1088 return;
1089
1090 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1091 HID_REQ_GET_REPORT,
1092 USB_DIR_IN | USB_TYPE_CLASS |
1093 USB_RECIP_INTERFACE,
1094 (3 << 8) | 0xf2, ifnum, buf, 17,
1095 USB_CTRL_GET_TIMEOUT);
1096
1097 if (result < 0)
1098 err("%s failed: %d\n", __func__, result);
1099
1100 kfree(buf);
1101}
1102
Jiri Kosinab55fd232007-02-21 19:27:49 +01001103/*
Jiri Kosina85cbea32007-04-05 12:23:09 +02001104 * Certain Logitech keyboards send in report #3 keys which are far
Jiri Kosinab55fd232007-02-21 19:27:49 +01001105 * above the logical maximum described in descriptor. This extends
1106 * the original value of 0x28c of logical maximum to 0x104d
1107 */
Jiri Kosina85cbea32007-04-05 12:23:09 +02001108static void hid_fixup_logitech_descriptor(unsigned char *rdesc, int rsize)
Jiri Kosinab55fd232007-02-21 19:27:49 +01001109{
1110 if (rsize >= 90 && rdesc[83] == 0x26
1111 && rdesc[84] == 0x8c
1112 && rdesc[85] == 0x02) {
Jiri Kosina85cbea32007-04-05 12:23:09 +02001113 info("Fixing up Logitech keyboard report descriptor");
Jiri Kosinab55fd232007-02-21 19:27:49 +01001114 rdesc[84] = rdesc[89] = 0x4d;
1115 rdesc[85] = rdesc[90] = 0x10;
1116 }
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1120{
1121 struct usb_host_interface *interface = intf->cur_altsetting;
1122 struct usb_device *dev = interface_to_usbdev (intf);
1123 struct hid_descriptor *hdesc;
1124 struct hid_device *hid;
Paul Walmsley4cbe7d22007-03-20 19:23:16 +01001125 __u32 quirks = 0;
1126 unsigned rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001127 char *rdesc;
1128 int n, len, insize = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001129 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Robert Marquardt5ddc3222007-03-02 16:01:52 +01001131 /* Ignore all Wacom devices */
1132 if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_WACOM)
1133 return NULL;
1134 /* ignore all Code Mercenaries IOWarrior devices */
1135 if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_CODEMERCS)
1136 if (le16_to_cpu(dev->descriptor.idProduct) >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1137 le16_to_cpu(dev->descriptor.idProduct) <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1138 return NULL;
Ping Chengea186652006-07-14 16:50:04 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 for (n = 0; hid_blacklist[n].idVendor; n++)
1141 if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
1142 (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
1143 quirks = hid_blacklist[n].quirks;
1144
Alan Stern0f28b552006-05-15 14:49:04 -04001145 /* Many keyboards and mice don't like to be polled for reports,
1146 * so we will always set the HID_QUIRK_NOGET flag for them. */
1147 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1148 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1149 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1150 quirks |= HID_QUIRK_NOGET;
1151 }
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (quirks & HID_QUIRK_IGNORE)
1154 return NULL;
1155
Soeren Sonnenburga417a212007-02-05 10:06:01 +01001156 if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
1157 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
1158 return NULL;
1159
1160
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001161 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1162 (!interface->desc.bNumEndpoints ||
1163 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1164 dbg("class descriptor not present\n");
1165 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
1168 for (n = 0; n < hdesc->bNumDescriptors; n++)
1169 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1170 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1171
1172 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1173 dbg("weird size of report descriptor (%u)", rsize);
1174 return NULL;
1175 }
1176
1177 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1178 dbg("couldn't allocate rdesc memory");
1179 return NULL;
1180 }
1181
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001182 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1185 dbg("reading report descriptor failed");
1186 kfree(rdesc);
1187 return NULL;
1188 }
1189
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001190 if ((quirks & HID_QUIRK_CYMOTION))
1191 hid_fixup_cymotion_descriptor(rdesc, rsize);
1192
Jiri Kosina85cbea32007-04-05 12:23:09 +02001193 if (quirks & HID_QUIRK_LOGITECH_DESCRIPTOR)
1194 hid_fixup_logitech_descriptor(rdesc, rsize);
Jiri Kosinab55fd232007-02-21 19:27:49 +01001195
Jiri Kosinafdc9c562007-02-19 14:15:59 +01001196#ifdef CONFIG_HID_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1198 for (n = 0; n < rsize; n++)
1199 printk(" %02x", (unsigned char) rdesc[n]);
1200 printk("\n");
1201#endif
1202
1203 if (!(hid = hid_parse_report(rdesc, n))) {
1204 dbg("parsing report descriptor failed");
1205 kfree(rdesc);
1206 return NULL;
1207 }
1208
1209 kfree(rdesc);
1210 hid->quirks = quirks;
1211
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001212 if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
1213 goto fail;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001214
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001215 hid->driver_data = usbhid;
1216 usbhid->hid = hid;
1217
1218 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1219 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1220 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1221 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1222
1223 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1224 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001225
1226 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1227
1228 if (insize > HID_MAX_BUFFER_SIZE)
1229 insize = HID_MAX_BUFFER_SIZE;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (hid_alloc_buffers(dev, hid)) {
1232 hid_free_buffers(dev, hid);
1233 goto fail;
1234 }
1235
1236 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1237
1238 struct usb_endpoint_descriptor *endpoint;
1239 int pipe;
1240 int interval;
1241
1242 endpoint = &interface->endpoint[n].desc;
1243 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
1244 continue;
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* Change the polling interval of mice. */
1249 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1250 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -05001251
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001252 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001253 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001255 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 goto fail;
1257 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001258 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001260 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1261 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001263 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001265 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 goto fail;
1267 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001268 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001270 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1271 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 }
1274
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001275 if (!usbhid->urbin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 err("couldn't find an input interrupt endpoint");
1277 goto fail;
1278 }
1279
1280 init_waitqueue_head(&hid->wait);
1281
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001282 INIT_WORK(&usbhid->reset_work, hid_reset);
1283 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
Alan Sternaef4e262006-01-31 12:58:38 -05001284
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001285 spin_lock_init(&usbhid->inlock);
1286 spin_lock_init(&usbhid->outlock);
1287 spin_lock_init(&usbhid->ctrllock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 hid->version = le16_to_cpu(hdesc->bcdHID);
1290 hid->country = hdesc->bCountryCode;
Anssi Hannulabe820972007-01-19 19:28:17 +02001291 hid->dev = &intf->dev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001292 usbhid->intf = intf;
1293 usbhid->ifnum = interface->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 hid->name[0] = 0;
1296
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001297 if (dev->manufacturer)
1298 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001300 if (dev->product) {
1301 if (dev->manufacturer)
1302 strlcat(hid->name, " ", sizeof(hid->name));
1303 strlcat(hid->name, dev->product, sizeof(hid->name));
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001306 if (!strlen(hid->name))
1307 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1308 le16_to_cpu(dev->descriptor.idVendor),
1309 le16_to_cpu(dev->descriptor.idProduct));
1310
Jiri Kosina229695e2006-12-08 18:40:53 +01001311 hid->bus = BUS_USB;
Julien BLACHE9fa2ad52007-02-11 18:20:25 +01001312 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1313 hid->product = le16_to_cpu(dev->descriptor.idProduct);
Jiri Kosina229695e2006-12-08 18:40:53 +01001314
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001315 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1316 strlcat(hid->phys, "/input", sizeof(hid->phys));
1317 len = strlen(hid->phys);
1318 if (len < sizeof(hid->phys) - 1)
1319 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1320 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1323 hid->uniq[0] = 0;
1324
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001325 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1326 if (!usbhid->urbctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto fail;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001328
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001329 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1330 usbhid->ctrlbuf, 1, hid_ctrl, hid);
1331 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
1332 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1333 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
Jiri Kosina229695e2006-12-08 18:40:53 +01001334 hid->hidinput_input_event = usb_hidinput_input_event;
Jiri Kosina7c379142007-01-24 11:54:19 +01001335 hid->hid_open = usbhid_open;
1336 hid->hid_close = usbhid_close;
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001337#ifdef CONFIG_USB_HIDDEV
1338 hid->hiddev_hid_event = hiddev_hid_event;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001339 hid->hiddev_report_event = hiddev_report_event;
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return hid;
1342
1343fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001344 usb_free_urb(usbhid->urbin);
1345 usb_free_urb(usbhid->urbout);
1346 usb_free_urb(usbhid->urbctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 hid_free_buffers(dev, hid);
1348 hid_free_device(hid);
1349
1350 return NULL;
1351}
1352
1353static void hid_disconnect(struct usb_interface *intf)
1354{
1355 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001356 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 if (!hid)
1359 return;
1360
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001361 usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001363 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1364 usb_set_intfdata(intf, NULL);
1365 spin_unlock_irq(&usbhid->inlock);
1366 usb_kill_urb(usbhid->urbin);
1367 usb_kill_urb(usbhid->urbout);
1368 usb_kill_urb(usbhid->urbctrl);
1369
1370 del_timer_sync(&usbhid->io_retry);
Alan Sternaef4e262006-01-31 12:58:38 -05001371 flush_scheduled_work();
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (hid->claimed & HID_CLAIMED_INPUT)
1374 hidinput_disconnect(hid);
1375 if (hid->claimed & HID_CLAIMED_HIDDEV)
1376 hiddev_disconnect(hid);
1377
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001378 usb_free_urb(usbhid->urbin);
1379 usb_free_urb(usbhid->urbctrl);
1380 usb_free_urb(usbhid->urbout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Anssi Hannulabe820972007-01-19 19:28:17 +02001382 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 hid_free_device(hid);
1384}
1385
1386static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1387{
1388 struct hid_device *hid;
1389 char path[64];
1390 int i;
1391 char *c;
1392
1393 dbg("HID probe called for ifnum %d",
1394 intf->altsetting->desc.bInterfaceNumber);
1395
1396 if (!(hid = usb_hid_configure(intf)))
Stelian Pop479f6ea2005-06-22 17:53:28 +02001397 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001399 usbhid_init_reports(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 hid_dump_device(hid);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +02001401 if (hid->quirks & HID_QUIRK_RESET_LEDS)
1402 usbhid_set_leds(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (!hidinput_connect(hid))
1405 hid->claimed |= HID_CLAIMED_INPUT;
1406 if (!hiddev_connect(hid))
1407 hid->claimed |= HID_CLAIMED_HIDDEV;
1408
1409 usb_set_intfdata(intf, hid);
1410
1411 if (!hid->claimed) {
1412 printk ("HID device not claimed by input or hiddev\n");
1413 hid_disconnect(intf);
Stelian Pop479f6ea2005-06-22 17:53:28 +02001414 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Anssi Hannulac4146062007-01-11 16:51:16 +02001417 if ((hid->claimed & HID_CLAIMED_INPUT))
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001418 hid_ff_init(hid);
1419
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001420 if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
1421 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
1422 intf->cur_altsetting->desc.bInterfaceNumber);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 printk(KERN_INFO);
1425
1426 if (hid->claimed & HID_CLAIMED_INPUT)
1427 printk("input");
1428 if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1429 printk(",");
1430 if (hid->claimed & HID_CLAIMED_HIDDEV)
1431 printk("hiddev%d", hid->minor);
1432
1433 c = "Device";
1434 for (i = 0; i < hid->maxcollection; i++) {
1435 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1436 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1437 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1438 c = hid_types[hid->collection[i].usage & 0xffff];
1439 break;
1440 }
1441 }
1442
1443 usb_make_path(interface_to_usbdev(intf), path, 63);
1444
1445 printk(": USB HID v%x.%02x %s [%s] on %s\n",
1446 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1447
1448 return 0;
1449}
1450
David Brownell27d72e82005-04-18 17:39:22 -07001451static int hid_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001454 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001456 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1457 set_bit(HID_SUSPENDED, &usbhid->iofl);
1458 spin_unlock_irq(&usbhid->inlock);
1459 del_timer(&usbhid->io_retry);
1460 usb_kill_urb(usbhid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 dev_dbg(&intf->dev, "suspend\n");
1462 return 0;
1463}
1464
1465static int hid_resume(struct usb_interface *intf)
1466{
1467 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001468 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int status;
1470
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001471 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1472 usbhid->retry_delay = 0;
Alan Sternaef4e262006-01-31 12:58:38 -05001473 status = hid_start_in(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 dev_dbg(&intf->dev, "resume status %d\n", status);
1475 return status;
1476}
1477
Alan Sterndf9a1f42006-06-01 13:55:28 -04001478/* Treat USB reset pretty much the same as suspend/resume */
1479static void hid_pre_reset(struct usb_interface *intf)
1480{
1481 /* FIXME: What if the interface is already suspended? */
1482 hid_suspend(intf, PMSG_ON);
1483}
1484
1485static void hid_post_reset(struct usb_interface *intf)
1486{
1487 struct usb_device *dev = interface_to_usbdev (intf);
1488
1489 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1490 /* FIXME: Any more reinitialization needed? */
1491
1492 hid_resume(intf);
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495static struct usb_device_id hid_usb_ids [] = {
1496 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1497 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1498 { } /* Terminating entry */
1499};
1500
1501MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1502
1503static struct usb_driver hid_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .name = "usbhid",
1505 .probe = hid_probe,
1506 .disconnect = hid_disconnect,
1507 .suspend = hid_suspend,
1508 .resume = hid_resume,
Alan Sterndf9a1f42006-06-01 13:55:28 -04001509 .pre_reset = hid_pre_reset,
1510 .post_reset = hid_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .id_table = hid_usb_ids,
1512};
1513
1514static int __init hid_init(void)
1515{
1516 int retval;
1517 retval = hiddev_init();
1518 if (retval)
1519 goto hiddev_init_fail;
1520 retval = usb_register(&hid_driver);
1521 if (retval)
1522 goto usb_register_fail;
1523 info(DRIVER_VERSION ":" DRIVER_DESC);
1524
1525 return 0;
1526usb_register_fail:
1527 hiddev_exit();
1528hiddev_init_fail:
1529 return retval;
1530}
1531
1532static void __exit hid_exit(void)
1533{
1534 usb_deregister(&hid_driver);
1535 hiddev_exit();
1536}
1537
1538module_init(hid_init);
1539module_exit(hid_exit);
1540
1541MODULE_AUTHOR(DRIVER_AUTHOR);
1542MODULE_DESCRIPTION(DRIVER_DESC);
1543MODULE_LICENSE(DRIVER_LICENSE);