]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/hid/usbhid/hid-core.c
HID: add USB ID for another dual gameron adapter
[net-next-2.6.git] / drivers / hid / usbhid / hid-core.c
CommitLineData
1da177e4
LT
1/*
2 * USB HID support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
bf0964dc
MH
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
b55fd23c 7 * Copyright (c) 2006-2007 Jiri Kosina
1da177e4
LT
8 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
1da177e4
LT
21#include <linux/list.h>
22#include <linux/mm.h>
3d5afd32 23#include <linux/mutex.h>
1da177e4
LT
24#include <linux/smp_lock.h>
25#include <linux/spinlock.h>
26#include <asm/unaligned.h>
27#include <asm/byteorder.h>
28#include <linux/input.h>
29#include <linux/wait.h>
30
1da177e4
LT
31#include <linux/usb.h>
32
dde5845a 33#include <linux/hid.h>
1da177e4 34#include <linux/hiddev.h>
c080d89a 35#include <linux/hid-debug.h>
86166b7b 36#include <linux/hidraw.h>
4916b3a5 37#include "usbhid.h"
1da177e4
LT
38
39/*
40 * Version Information
41 */
42
bf0964dc 43#define DRIVER_VERSION "v2.6"
f142b3a4 44#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
1da177e4
LT
45#define DRIVER_DESC "USB HID core driver"
46#define DRIVER_LICENSE "GPL"
47
1da177e4
LT
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
876b9276
PW
56/* Quirks specified at module load time */
57static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
58module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
59MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
60 " quirks=vendorID:productID:quirks"
61 " where vendorID, productID, and quirks are all in"
62 " 0x-prefixed hex");
1da177e4 63/*
48b4554a 64 * Input submission and I/O error handler.
1da177e4
LT
65 */
66
48b4554a
JK
67static void hid_io_error(struct hid_device *hid);
68
69/* Start up the input URB */
70static int hid_start_in(struct hid_device *hid)
1da177e4 71{
1da177e4 72 unsigned long flags;
48b4554a
JK
73 int rc = 0;
74 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 75
48b4554a
JK
76 spin_lock_irqsave(&usbhid->inlock, flags);
77 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
69626f23 78 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
48b4554a
JK
79 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
80 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
81 if (rc != 0)
82 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
1da177e4 83 }
48b4554a
JK
84 spin_unlock_irqrestore(&usbhid->inlock, flags);
85 return rc;
1da177e4
LT
86}
87
48b4554a
JK
88/* I/O retry timer routine */
89static void hid_retry_timeout(unsigned long _hid)
1da177e4 90{
48b4554a 91 struct hid_device *hid = (struct hid_device *) _hid;
4916b3a5 92 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 93
48b4554a
JK
94 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
95 if (hid_start_in(hid))
96 hid_io_error(hid);
1da177e4
LT
97}
98
48b4554a
JK
99/* Workqueue routine to reset the device or clear a halt */
100static void hid_reset(struct work_struct *work)
1da177e4 101{
48b4554a
JK
102 struct usbhid_device *usbhid =
103 container_of(work, struct usbhid_device, reset_work);
104 struct hid_device *hid = usbhid->hid;
105 int rc_lock, rc = 0;
1da177e4 106
48b4554a
JK
107 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
108 dev_dbg(&usbhid->intf->dev, "clear halt\n");
109 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
110 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
111 hid_start_in(hid);
112 }
1da177e4 113
48b4554a
JK
114 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
115 dev_dbg(&usbhid->intf->dev, "resetting device\n");
116 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
117 if (rc_lock >= 0) {
742120c6 118 rc = usb_reset_device(hid_to_usb_dev(hid));
48b4554a
JK
119 if (rc_lock)
120 usb_unlock_device(hid_to_usb_dev(hid));
1da177e4 121 }
48b4554a 122 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1da177e4
LT
123 }
124
48b4554a
JK
125 switch (rc) {
126 case 0:
127 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
128 hid_io_error(hid);
129 break;
130 default:
58037eb9 131 err_hid("can't reset device, %s-%s/input%d, status %d",
48b4554a
JK
132 hid_to_usb_dev(hid)->bus->bus_name,
133 hid_to_usb_dev(hid)->devpath,
134 usbhid->ifnum, rc);
135 /* FALLTHROUGH */
136 case -EHOSTUNREACH:
137 case -ENODEV:
138 case -EINTR:
139 break;
1da177e4 140 }
1da177e4
LT
141}
142
48b4554a
JK
143/* Main I/O error handler */
144static void hid_io_error(struct hid_device *hid)
dde5845a 145{
48b4554a
JK
146 unsigned long flags;
147 struct usbhid_device *usbhid = hid->driver_data;
dde5845a 148
48b4554a 149 spin_lock_irqsave(&usbhid->inlock, flags);
dde5845a 150
48b4554a 151 /* Stop when disconnected */
69626f23 152 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
48b4554a 153 goto done;
dde5845a 154
5e2a55f2
AS
155 /* If it has been a while since the last error, we'll assume
156 * this a brand new error and reset the retry timeout. */
157 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
158 usbhid->retry_delay = 0;
159
48b4554a
JK
160 /* When an error occurs, retry at increasing intervals */
161 if (usbhid->retry_delay == 0) {
162 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
163 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
164 } else if (usbhid->retry_delay < 100)
165 usbhid->retry_delay *= 2;
dde5845a 166
48b4554a 167 if (time_after(jiffies, usbhid->stop_retry)) {
4916b3a5 168
48b4554a
JK
169 /* Retries failed, so do a port reset */
170 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
171 schedule_work(&usbhid->reset_work);
172 goto done;
173 }
1da177e4
LT
174 }
175
48b4554a
JK
176 mod_timer(&usbhid->io_retry,
177 jiffies + msecs_to_jiffies(usbhid->retry_delay));
178done:
179 spin_unlock_irqrestore(&usbhid->inlock, flags);
1da177e4
LT
180}
181
48b4554a
JK
182/*
183 * Input interrupt completion handler.
184 */
854561b0 185
48b4554a 186static void hid_irq_in(struct urb *urb)
1da177e4 187{
48b4554a
JK
188 struct hid_device *hid = urb->context;
189 struct usbhid_device *usbhid = hid->driver_data;
190 int status;
1da177e4 191
48b4554a 192 switch (urb->status) {
880d29f1
JS
193 case 0: /* success */
194 usbhid->retry_delay = 0;
195 hid_input_report(urb->context, HID_INPUT_REPORT,
196 urb->transfer_buffer,
197 urb->actual_length, 1);
198 break;
199 case -EPIPE: /* stall */
200 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
201 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
202 schedule_work(&usbhid->reset_work);
203 return;
204 case -ECONNRESET: /* unlink */
205 case -ENOENT:
206 case -ESHUTDOWN: /* unplug */
207 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
208 return;
209 case -EILSEQ: /* protocol error or unplug */
210 case -EPROTO: /* protocol error or unplug */
211 case -ETIME: /* protocol error or unplug */
212 case -ETIMEDOUT: /* Should never happen, but... */
213 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
214 hid_io_error(hid);
215 return;
216 default: /* error */
7d89fe12
GKH
217 dev_warn(&urb->dev->dev, "input irq status %d "
218 "received\n", urb->status);
48b4554a 219 }
1da177e4 220
48b4554a
JK
221 status = usb_submit_urb(urb, GFP_ATOMIC);
222 if (status) {
223 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
224 if (status != -EPERM) {
58037eb9 225 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
48b4554a
JK
226 hid_to_usb_dev(hid)->bus->bus_name,
227 hid_to_usb_dev(hid)->devpath,
228 usbhid->ifnum, status);
229 hid_io_error(hid);
230 }
231 }
1da177e4
LT
232}
233
48b4554a 234static int hid_submit_out(struct hid_device *hid)
1da177e4 235{
48b4554a 236 struct hid_report *report;
f129ea6d 237 char *raw_report;
4916b3a5
JK
238 struct usbhid_device *usbhid = hid->driver_data;
239
f129ea6d
AH
240 report = usbhid->out[usbhid->outtail].report;
241 raw_report = usbhid->out[usbhid->outtail].raw_report;
1da177e4 242
48b4554a
JK
243 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
244 usbhid->urbout->dev = hid_to_usb_dev(hid);
f129ea6d
AH
245 memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
246 kfree(raw_report);
1d3e2023 247
58037eb9 248 dbg_hid("submitting out urb\n");
d57cdcff 249
48b4554a 250 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
58037eb9 251 err_hid("usb_submit_urb(out) failed");
48b4554a
JK
252 return -1;
253 }
1da177e4 254
48b4554a
JK
255 return 0;
256}
257
258static int hid_submit_ctrl(struct hid_device *hid)
1da177e4
LT
259{
260 struct hid_report *report;
48b4554a 261 unsigned char dir;
f129ea6d 262 char *raw_report;
48b4554a 263 int len;
4916b3a5 264 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 265
48b4554a 266 report = usbhid->ctrl[usbhid->ctrltail].report;
f129ea6d 267 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
48b4554a 268 dir = usbhid->ctrl[usbhid->ctrltail].dir;
1da177e4 269
48b4554a
JK
270 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
271 if (dir == USB_DIR_OUT) {
48b4554a
JK
272 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
273 usbhid->urbctrl->transfer_buffer_length = len;
f129ea6d
AH
274 memcpy(usbhid->ctrlbuf, raw_report, len);
275 kfree(raw_report);
48b4554a
JK
276 } else {
277 int maxpacket, padlen;
1da177e4 278
48b4554a
JK
279 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
280 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
281 if (maxpacket > 0) {
92c4a1b9 282 padlen = DIV_ROUND_UP(len, maxpacket);
48b4554a
JK
283 padlen *= maxpacket;
284 if (padlen > usbhid->bufsize)
285 padlen = usbhid->bufsize;
286 } else
287 padlen = 0;
288 usbhid->urbctrl->transfer_buffer_length = padlen;
1da177e4 289 }
48b4554a 290 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
1da177e4 291
48b4554a
JK
292 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
293 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
294 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
295 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
296 usbhid->cr->wLength = cpu_to_le16(len);
1da177e4 297
58037eb9 298 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
48b4554a
JK
299 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
300 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
1da177e4 301
48b4554a 302 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
58037eb9 303 err_hid("usb_submit_urb(ctrl) failed");
48b4554a
JK
304 return -1;
305 }
1da177e4 306
48b4554a
JK
307 return 0;
308}
1da177e4 309
48b4554a
JK
310/*
311 * Output interrupt completion handler.
312 */
1da177e4 313
48b4554a
JK
314static void hid_irq_out(struct urb *urb)
315{
316 struct hid_device *hid = urb->context;
317 struct usbhid_device *usbhid = hid->driver_data;
318 unsigned long flags;
319 int unplug = 0;
1da177e4 320
48b4554a 321 switch (urb->status) {
880d29f1
JS
322 case 0: /* success */
323 break;
324 case -ESHUTDOWN: /* unplug */
325 unplug = 1;
326 case -EILSEQ: /* protocol error or unplug */
327 case -EPROTO: /* protocol error or unplug */
328 case -ECONNRESET: /* unlink */
329 case -ENOENT:
330 break;
331 default: /* error */
7d89fe12
GKH
332 dev_warn(&urb->dev->dev, "output irq status %d "
333 "received\n", urb->status);
48b4554a 334 }
1da177e4 335
48b4554a 336 spin_lock_irqsave(&usbhid->outlock, flags);
1da177e4 337
48b4554a
JK
338 if (unplug)
339 usbhid->outtail = usbhid->outhead;
340 else
341 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1da177e4 342
48b4554a
JK
343 if (usbhid->outhead != usbhid->outtail) {
344 if (hid_submit_out(hid)) {
345 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
1d1bdd20 346 wake_up(&usbhid->wait);
48b4554a
JK
347 }
348 spin_unlock_irqrestore(&usbhid->outlock, flags);
349 return;
350 }
1da177e4 351
48b4554a
JK
352 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
353 spin_unlock_irqrestore(&usbhid->outlock, flags);
1d1bdd20 354 wake_up(&usbhid->wait);
48b4554a 355}
6345fdfd 356
48b4554a
JK
357/*
358 * Control pipe completion handler.
359 */
1da177e4 360
48b4554a
JK
361static void hid_ctrl(struct urb *urb)
362{
363 struct hid_device *hid = urb->context;
364 struct usbhid_device *usbhid = hid->driver_data;
365 unsigned long flags;
366 int unplug = 0;
1da177e4 367
48b4554a 368 spin_lock_irqsave(&usbhid->ctrllock, flags);
1da177e4 369
48b4554a 370 switch (urb->status) {
880d29f1
JS
371 case 0: /* success */
372 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
373 hid_input_report(urb->context,
374 usbhid->ctrl[usbhid->ctrltail].report->type,
375 urb->transfer_buffer, urb->actual_length, 0);
376 break;
377 case -ESHUTDOWN: /* unplug */
378 unplug = 1;
379 case -EILSEQ: /* protocol error or unplug */
380 case -EPROTO: /* protocol error or unplug */
381 case -ECONNRESET: /* unlink */
382 case -ENOENT:
383 case -EPIPE: /* report not available */
384 break;
385 default: /* error */
7d89fe12
GKH
386 dev_warn(&urb->dev->dev, "ctrl urb status %d "
387 "received\n", urb->status);
48b4554a 388 }
1da177e4 389
48b4554a
JK
390 if (unplug)
391 usbhid->ctrltail = usbhid->ctrlhead;
392 else
393 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1da177e4 394
48b4554a
JK
395 if (usbhid->ctrlhead != usbhid->ctrltail) {
396 if (hid_submit_ctrl(hid)) {
397 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
1d1bdd20 398 wake_up(&usbhid->wait);
48b4554a
JK
399 }
400 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
401 return;
402 }
1da177e4 403
48b4554a
JK
404 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
405 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
1d1bdd20 406 wake_up(&usbhid->wait);
48b4554a 407}
1da177e4 408
48b4554a
JK
409void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
410{
411 int head;
412 unsigned long flags;
413 struct usbhid_device *usbhid = hid->driver_data;
f129ea6d 414 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1da177e4 415
48b4554a
JK
416 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
417 return;
b857c651 418
48b4554a 419 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
1da177e4 420
48b4554a 421 spin_lock_irqsave(&usbhid->outlock, flags);
1da177e4 422
48b4554a
JK
423 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
424 spin_unlock_irqrestore(&usbhid->outlock, flags);
7d89fe12 425 dev_warn(&hid->dev, "output queue full\n");
48b4554a
JK
426 return;
427 }
1da177e4 428
f129ea6d
AH
429 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
430 if (!usbhid->out[usbhid->outhead].raw_report) {
431 spin_unlock_irqrestore(&usbhid->outlock, flags);
46fcaec5 432 dev_warn(&hid->dev, "output queueing failed\n");
f129ea6d
AH
433 return;
434 }
435 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
436 usbhid->out[usbhid->outhead].report = report;
48b4554a 437 usbhid->outhead = head;
4871d3be 438
48b4554a
JK
439 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
440 if (hid_submit_out(hid))
441 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
8fd6db47 442
48b4554a
JK
443 spin_unlock_irqrestore(&usbhid->outlock, flags);
444 return;
445 }
1da177e4 446
48b4554a 447 spin_lock_irqsave(&usbhid->ctrllock, flags);
940824b0 448
48b4554a
JK
449 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
450 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
7d89fe12 451 dev_warn(&hid->dev, "control queue full\n");
48b4554a
JK
452 return;
453 }
8fd80133 454
f129ea6d
AH
455 if (dir == USB_DIR_OUT) {
456 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
457 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
458 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
46fcaec5 459 dev_warn(&hid->dev, "control queueing failed\n");
f129ea6d
AH
460 return;
461 }
462 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
463 }
48b4554a
JK
464 usbhid->ctrl[usbhid->ctrlhead].report = report;
465 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
466 usbhid->ctrlhead = head;
8fd80133 467
48b4554a
JK
468 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
469 if (hid_submit_ctrl(hid))
470 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
931e24b9 471
48b4554a
JK
472 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
473}
606bd0a8 474EXPORT_SYMBOL_GPL(usbhid_submit_report);
23b0d968 475
48b4554a
JK
476static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
477{
e0712985 478 struct hid_device *hid = input_get_drvdata(dev);
48b4554a
JK
479 struct hid_field *field;
480 int offset;
41ad5fba 481
48b4554a
JK
482 if (type == EV_FF)
483 return input_ff_event(dev, type, code, value);
8d2bad87 484
48b4554a
JK
485 if (type != EV_LED)
486 return -1;
5556feae 487
48b4554a 488 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
7d89fe12 489 dev_warn(&dev->dev, "event field not found\n");
48b4554a
JK
490 return -1;
491 }
4a1a4d8b 492
48b4554a
JK
493 hid_set_field(field, offset, value);
494 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
1da177e4 495
48b4554a
JK
496 return 0;
497}
1da177e4 498
48b4554a
JK
499int usbhid_wait_io(struct hid_device *hid)
500{
501 struct usbhid_device *usbhid = hid->driver_data;
25914662 502
1d1bdd20
JS
503 if (!wait_event_timeout(usbhid->wait,
504 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
505 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
48b4554a 506 10*HZ)) {
58037eb9 507 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
48b4554a
JK
508 return -1;
509 }
1da177e4 510
48b4554a
JK
511 return 0;
512}
53880546 513
48b4554a
JK
514static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
515{
516 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
517 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
518 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
519}
1da177e4 520
48b4554a
JK
521static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
522 unsigned char type, void *buf, int size)
523{
524 int result, retries = 4;
1da177e4 525
48b4554a 526 memset(buf, 0, size);
1da177e4 527
48b4554a
JK
528 do {
529 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
530 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
531 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
532 retries--;
533 } while (result < size && retries);
534 return result;
535}
940824b0 536
48b4554a
JK
537int usbhid_open(struct hid_device *hid)
538{
933e3187
ON
539 struct usbhid_device *usbhid = hid->driver_data;
540 int res;
541
542 if (!hid->open++) {
543 res = usb_autopm_get_interface(usbhid->intf);
544 if (res < 0) {
545 hid->open--;
546 return -EIO;
547 }
548 }
48b4554a
JK
549 if (hid_start_in(hid))
550 hid_io_error(hid);
551 return 0;
552}
a417a21e 553
48b4554a
JK
554void usbhid_close(struct hid_device *hid)
555{
556 struct usbhid_device *usbhid = hid->driver_data;
eab9edd2 557
933e3187 558 if (!--hid->open) {
48b4554a 559 usb_kill_urb(usbhid->urbin);
933e3187
ON
560 usb_autopm_put_interface(usbhid->intf);
561 }
48b4554a 562}
1d3e2023 563
48b4554a
JK
564/*
565 * Initialize all reports
566 */
41ad5fba 567
48b4554a
JK
568void usbhid_init_reports(struct hid_device *hid)
569{
570 struct hid_report *report;
571 struct usbhid_device *usbhid = hid->driver_data;
572 int err, ret;
41ad5fba 573
48b4554a
JK
574 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
575 usbhid_submit_report(hid, report, USB_DIR_IN);
5556feae 576
48b4554a
JK
577 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
578 usbhid_submit_report(hid, report, USB_DIR_IN);
4a1a4d8b 579
48b4554a
JK
580 err = 0;
581 ret = usbhid_wait_io(hid);
582 while (ret) {
583 err |= ret;
584 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
585 usb_kill_urb(usbhid->urbctrl);
586 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
587 usb_kill_urb(usbhid->urbout);
588 ret = usbhid_wait_io(hid);
589 }
3f9b4076 590
48b4554a 591 if (err)
7d89fe12 592 dev_warn(&hid->dev, "timeout initializing reports\n");
48b4554a 593}
1da177e4 594
713c8aad
PZ
595/*
596 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
597 */
598static int hid_find_field_early(struct hid_device *hid, unsigned int page,
599 unsigned int hid_code, struct hid_field **pfield)
600{
601 struct hid_report *report;
602 struct hid_field *field;
603 struct hid_usage *usage;
604 int i, j;
605
606 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
607 for (i = 0; i < report->maxfield; i++) {
608 field = report->field[i];
609 for (j = 0; j < field->maxusage; j++) {
610 usage = &field->usage[j];
611 if ((usage->hid & HID_USAGE_PAGE) == page &&
612 (usage->hid & 0xFFFF) == hid_code) {
613 *pfield = field;
614 return j;
615 }
616 }
617 }
618 }
619 return -1;
620}
621
6edfa8dc 622void usbhid_set_leds(struct hid_device *hid)
713c8aad
PZ
623{
624 struct hid_field *field;
625 int offset;
626
627 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
628 hid_set_field(field, offset, 0);
629 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
630 }
631}
6edfa8dc 632EXPORT_SYMBOL_GPL(usbhid_set_leds);
713c8aad 633
bf0964dc
MH
634/*
635 * Traverse the supplied list of reports and find the longest
636 */
282bfd4c
JS
637static void hid_find_max_report(struct hid_device *hid, unsigned int type,
638 unsigned int *max)
bf0964dc
MH
639{
640 struct hid_report *report;
282bfd4c 641 unsigned int size;
bf0964dc
MH
642
643 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
644 size = ((report->size - 1) >> 3) + 1;
645 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
646 size++;
647 if (*max < size)
648 *max = size;
649 }
650}
651
1da177e4
LT
652static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
653{
4916b3a5
JK
654 struct usbhid_device *usbhid = hid->driver_data;
655
656 if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
1da177e4 657 return -1;
4916b3a5 658 if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
1da177e4 659 return -1;
4916b3a5 660 if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
1da177e4 661 return -1;
4916b3a5 662 if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
1da177e4
LT
663 return -1;
664
665 return 0;
666}
667
efc493f9
JK
668static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
669{
670 struct usbhid_device *usbhid = hid->driver_data;
671 struct usb_device *dev = hid_to_usb_dev(hid);
672 struct usb_interface *intf = usbhid->intf;
673 struct usb_host_interface *interface = intf->cur_altsetting;
674 int ret;
675
676 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
677 HID_REQ_SET_REPORT,
678 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
01d7b369 679 ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
efc493f9
JK
680 interface->desc.bInterfaceNumber, buf + 1, count - 1,
681 USB_CTRL_SET_TIMEOUT);
682
683 /* count also the report id */
684 if (ret > 0)
685 ret++;
686
687 return ret;
688}
689
1da177e4
LT
690static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
691{
4916b3a5
JK
692 struct usbhid_device *usbhid = hid->driver_data;
693
6675c5bd
DT
694 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
695 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
696 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
697 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
1da177e4
LT
698}
699
c500c971
JS
700static int usbhid_parse(struct hid_device *hid)
701{
702 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1da177e4
LT
703 struct usb_host_interface *interface = intf->cur_altsetting;
704 struct usb_device *dev = interface_to_usbdev (intf);
705 struct hid_descriptor *hdesc;
2eb5dc30 706 u32 quirks = 0;
c500c971 707 unsigned int rsize = 0;
c5b7c7c3 708 char *rdesc;
c500c971 709 int ret, n;
1da177e4 710
2eb5dc30
PW
711 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
712 le16_to_cpu(dev->descriptor.idProduct));
1da177e4 713
0f28b55d
AS
714 /* Many keyboards and mice don't like to be polled for reports,
715 * so we will always set the HID_QUIRK_NOGET flag for them. */
716 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
717 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
718 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
719 quirks |= HID_QUIRK_NOGET;
720 }
721
c5b7c7c3
DT
722 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
723 (!interface->desc.bNumEndpoints ||
724 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
58037eb9 725 dbg_hid("class descriptor not present\n");
c500c971 726 return -ENODEV;
1da177e4
LT
727 }
728
c500c971
JS
729 hid->version = le16_to_cpu(hdesc->bcdHID);
730 hid->country = hdesc->bCountryCode;
731
1da177e4
LT
732 for (n = 0; n < hdesc->bNumDescriptors; n++)
733 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
734 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
735
736 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
58037eb9 737 dbg_hid("weird size of report descriptor (%u)\n", rsize);
c500c971 738 return -EINVAL;
1da177e4
LT
739 }
740
741 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
58037eb9 742 dbg_hid("couldn't allocate rdesc memory\n");
c500c971 743 return -ENOMEM;
1da177e4
LT
744 }
745
854561b0
VP
746 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
747
c500c971
JS
748 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
749 HID_DT_REPORT, rdesc, rsize);
750 if (ret < 0) {
58037eb9 751 dbg_hid("reading report descriptor failed\n");
1da177e4 752 kfree(rdesc);
c500c971 753 goto err;
1da177e4
LT
754 }
755
58037eb9 756 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
1da177e4 757 for (n = 0; n < rsize; n++)
58037eb9
JK
758 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
759 dbg_hid_line("\n");
1da177e4 760
c500c971
JS
761 ret = hid_parse_report(hid, rdesc, rsize);
762 kfree(rdesc);
763 if (ret) {
58037eb9 764 dbg_hid("parsing report descriptor failed\n");
c500c971 765 goto err;
1da177e4
LT
766 }
767
1da177e4
LT
768 hid->quirks = quirks;
769
c500c971
JS
770 return 0;
771err:
772 return ret;
773}
774
775static int usbhid_start(struct hid_device *hid)
776{
777 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
778 struct usb_host_interface *interface = intf->cur_altsetting;
779 struct usb_device *dev = interface_to_usbdev(intf);
3d5afd32 780 struct usbhid_device *usbhid = hid->driver_data;
c500c971
JS
781 unsigned int n, insize = 0;
782 int ret;
783
e3e14de5
JS
784 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
785
4916b3a5
JK
786 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
787 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
788 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
789 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
bf0964dc 790
4916b3a5
JK
791 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
792 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
bf0964dc
MH
793
794 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
795
796 if (insize > HID_MAX_BUFFER_SIZE)
797 insize = HID_MAX_BUFFER_SIZE;
798
3d5afd32 799 mutex_lock(&usbhid->setup);
c500c971
JS
800 if (hid_alloc_buffers(dev, hid)) {
801 ret = -ENOMEM;
1da177e4 802 goto fail;
f345c37c
PS
803 }
804
1da177e4 805 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1da177e4
LT
806 struct usb_endpoint_descriptor *endpoint;
807 int pipe;
808 int interval;
809
810 endpoint = &interface->endpoint[n].desc;
811 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
812 continue;
813
1da177e4 814 interval = endpoint->bInterval;
1da177e4 815
f345c37c 816 /* Some vendors give fullspeed interval on highspeed devides */
c500c971 817 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
f345c37c
PS
818 dev->speed == USB_SPEED_HIGH) {
819 interval = fls(endpoint->bInterval*8);
820 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
821 hid->name, endpoint->bInterval, interval);
822 }
823
1da177e4
LT
824 /* Change the polling interval of mice. */
825 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
826 interval = hid_mousepoll_interval;
05f091ab 827
c500c971 828 ret = -ENOMEM;
0f12aa03 829 if (usb_endpoint_dir_in(endpoint)) {
4916b3a5 830 if (usbhid->urbin)
1da177e4 831 continue;
4916b3a5 832 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4
LT
833 goto fail;
834 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
4916b3a5 835 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1da177e4 836 hid_irq_in, hid, interval);
4916b3a5
JK
837 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
838 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1da177e4 839 } else {
4916b3a5 840 if (usbhid->urbout)
1da177e4 841 continue;
4916b3a5 842 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4
LT
843 goto fail;
844 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
4916b3a5 845 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1da177e4 846 hid_irq_out, hid, interval);
4916b3a5
JK
847 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
848 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1da177e4
LT
849 }
850 }
851
1d1bdd20 852 init_waitqueue_head(&usbhid->wait);
4916b3a5
JK
853 INIT_WORK(&usbhid->reset_work, hid_reset);
854 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
aef4e266 855
4916b3a5
JK
856 spin_lock_init(&usbhid->inlock);
857 spin_lock_init(&usbhid->outlock);
858 spin_lock_init(&usbhid->ctrllock);
1da177e4 859
4916b3a5
JK
860 usbhid->intf = intf;
861 usbhid->ifnum = interface->desc.bInterfaceNumber;
1da177e4 862
4916b3a5 863 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
c500c971
JS
864 if (!usbhid->urbctrl) {
865 ret = -ENOMEM;
1da177e4 866 goto fail;
c500c971 867 }
c5b7c7c3 868
4916b3a5
JK
869 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
870 usbhid->ctrlbuf, 1, hid_ctrl, hid);
871 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
872 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
873 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
c500c971 874
93c10132
JS
875 usbhid_init_reports(hid);
876 hid_dump_device(hid);
877
3d5afd32
JS
878 set_bit(HID_STARTED, &usbhid->iofl);
879 mutex_unlock(&usbhid->setup);
880
c500c971 881 return 0;
1da177e4
LT
882
883fail:
4916b3a5
JK
884 usb_free_urb(usbhid->urbin);
885 usb_free_urb(usbhid->urbout);
886 usb_free_urb(usbhid->urbctrl);
e3e14de5
JS
887 usbhid->urbin = NULL;
888 usbhid->urbout = NULL;
889 usbhid->urbctrl = NULL;
22f675f3 890 hid_free_buffers(dev, hid);
3d5afd32 891 mutex_unlock(&usbhid->setup);
c500c971 892 return ret;
1da177e4
LT
893}
894
c500c971 895static void usbhid_stop(struct hid_device *hid)
1da177e4 896{
c500c971 897 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 898
c500c971 899 if (WARN_ON(!usbhid))
1da177e4
LT
900 return;
901
3d5afd32
JS
902 mutex_lock(&usbhid->setup);
903 clear_bit(HID_STARTED, &usbhid->iofl);
4916b3a5 904 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
69626f23 905 set_bit(HID_DISCONNECTED, &usbhid->iofl);
4916b3a5
JK
906 spin_unlock_irq(&usbhid->inlock);
907 usb_kill_urb(usbhid->urbin);
908 usb_kill_urb(usbhid->urbout);
909 usb_kill_urb(usbhid->urbctrl);
1da177e4 910
4916b3a5 911 del_timer_sync(&usbhid->io_retry);
2fa45a4c 912 cancel_work_sync(&usbhid->reset_work);
aef4e266 913
1da177e4
LT
914 if (hid->claimed & HID_CLAIMED_INPUT)
915 hidinput_disconnect(hid);
916 if (hid->claimed & HID_CLAIMED_HIDDEV)
917 hiddev_disconnect(hid);
86166b7b
JK
918 if (hid->claimed & HID_CLAIMED_HIDRAW)
919 hidraw_disconnect(hid);
1da177e4 920
c500c971
JS
921 hid->claimed = 0;
922
4916b3a5
JK
923 usb_free_urb(usbhid->urbin);
924 usb_free_urb(usbhid->urbctrl);
925 usb_free_urb(usbhid->urbout);
e3e14de5
JS
926 usbhid->urbin = NULL; /* don't mess up next start */
927 usbhid->urbctrl = NULL;
928 usbhid->urbout = NULL;
1da177e4 929
be820975 930 hid_free_buffers(hid_to_usb_dev(hid), hid);
3d5afd32 931 mutex_unlock(&usbhid->setup);
1da177e4
LT
932}
933
c500c971
JS
934static struct hid_ll_driver usb_hid_driver = {
935 .parse = usbhid_parse,
936 .start = usbhid_start,
937 .stop = usbhid_stop,
938 .open = usbhid_open,
939 .close = usbhid_close,
940 .hidinput_input_event = usb_hidinput_input_event,
941};
942
1da177e4
LT
943static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
944{
131d3a7a 945 struct usb_host_interface *interface = intf->cur_altsetting;
c500c971 946 struct usb_device *dev = interface_to_usbdev(intf);
3d5afd32 947 struct usbhid_device *usbhid;
1da177e4 948 struct hid_device *hid;
131d3a7a 949 unsigned int n, has_in = 0;
c500c971
JS
950 size_t len;
951 int ret;
1da177e4 952
58037eb9 953 dbg_hid("HID probe called for ifnum %d\n",
1da177e4
LT
954 intf->altsetting->desc.bInterfaceNumber);
955
131d3a7a
JS
956 for (n = 0; n < interface->desc.bNumEndpoints; n++)
957 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
958 has_in++;
959 if (!has_in) {
960 dev_err(&intf->dev, "couldn't find an input interrupt "
961 "endpoint\n");
962 return -ENODEV;
963 }
964
c500c971
JS
965 hid = hid_allocate_device();
966 if (IS_ERR(hid))
967 return PTR_ERR(hid);
1da177e4
LT
968
969 usb_set_intfdata(intf, hid);
c500c971
JS
970 hid->ll_driver = &usb_hid_driver;
971 hid->hid_output_raw_report = usbhid_output_raw_report;
76483cf4 972 hid->ff_init = hid_pidff_init;
c500c971 973#ifdef CONFIG_USB_HIDDEV
93c10132 974 hid->hiddev_connect = hiddev_connect;
c500c971
JS
975 hid->hiddev_hid_event = hiddev_hid_event;
976 hid->hiddev_report_event = hiddev_report_event;
977#endif
978 hid->dev.parent = &intf->dev;
979 hid->bus = BUS_USB;
980 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
981 hid->product = le16_to_cpu(dev->descriptor.idProduct);
982 hid->name[0] = 0;
a73a6370
JS
983 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
984 USB_INTERFACE_PROTOCOL_MOUSE)
985 hid->type = HID_TYPE_USBMOUSE;
1da177e4 986
c500c971
JS
987 if (dev->manufacturer)
988 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1da177e4 989
c500c971
JS
990 if (dev->product) {
991 if (dev->manufacturer)
992 strlcat(hid->name, " ", sizeof(hid->name));
993 strlcat(hid->name, dev->product, sizeof(hid->name));
1da177e4
LT
994 }
995
c500c971
JS
996 if (!strlen(hid->name))
997 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
998 le16_to_cpu(dev->descriptor.idVendor),
999 le16_to_cpu(dev->descriptor.idProduct));
1da177e4 1000
c500c971
JS
1001 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1002 strlcat(hid->phys, "/input", sizeof(hid->phys));
1003 len = strlen(hid->phys);
1004 if (len < sizeof(hid->phys) - 1)
1005 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1006 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1007
1008 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1009 hid->uniq[0] = 0;
1da177e4 1010
3d5afd32
JS
1011 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1012 if (usbhid == NULL) {
1013 ret = -ENOMEM;
1014 goto err;
1015 }
1016
1017 hid->driver_data = usbhid;
1018 usbhid->hid = hid;
1019 mutex_init(&usbhid->setup); /* needed on suspend/resume */
1020
85cdaf52
JS
1021 ret = hid_add_device(hid);
1022 if (ret) {
d458a9df
JS
1023 if (ret != -ENODEV)
1024 dev_err(&intf->dev, "can't add hid device: %d\n", ret);
3d5afd32 1025 goto err_free;
85cdaf52 1026 }
c500c971
JS
1027
1028 return 0;
3d5afd32
JS
1029err_free:
1030 kfree(usbhid);
c500c971
JS
1031err:
1032 hid_destroy_device(hid);
85cdaf52 1033 return ret;
1da177e4
LT
1034}
1035
c500c971
JS
1036static void hid_disconnect(struct usb_interface *intf)
1037{
1038 struct hid_device *hid = usb_get_intfdata(intf);
3d5afd32 1039 struct usbhid_device *usbhid;
c500c971
JS
1040
1041 if (WARN_ON(!hid))
1042 return;
1043
3d5afd32 1044 usbhid = hid->driver_data;
c500c971 1045 hid_destroy_device(hid);
3d5afd32 1046 kfree(usbhid);
c500c971
JS
1047}
1048
27d72e85 1049static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1050{
1051 struct hid_device *hid = usb_get_intfdata (intf);
4916b3a5 1052 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 1053
3d5afd32
JS
1054 mutex_lock(&usbhid->setup);
1055 if (!test_bit(HID_STARTED, &usbhid->iofl)) {
1056 mutex_unlock(&usbhid->setup);
1057 return 0;
1058 }
1059
4916b3a5
JK
1060 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1061 set_bit(HID_SUSPENDED, &usbhid->iofl);
1062 spin_unlock_irq(&usbhid->inlock);
b170060c 1063 del_timer_sync(&usbhid->io_retry);
4916b3a5 1064 usb_kill_urb(usbhid->urbin);
3d5afd32 1065 mutex_unlock(&usbhid->setup);
1da177e4
LT
1066 dev_dbg(&intf->dev, "suspend\n");
1067 return 0;
1068}
1069
1070static int hid_resume(struct usb_interface *intf)
1071{
1072 struct hid_device *hid = usb_get_intfdata (intf);
4916b3a5 1073 struct usbhid_device *usbhid = hid->driver_data;
1da177e4
LT
1074 int status;
1075
3d5afd32
JS
1076 mutex_lock(&usbhid->setup);
1077 if (!test_bit(HID_STARTED, &usbhid->iofl)) {
1078 mutex_unlock(&usbhid->setup);
1079 return 0;
1080 }
1081
4916b3a5
JK
1082 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1083 usbhid->retry_delay = 0;
aef4e266 1084 status = hid_start_in(hid);
3d5afd32 1085 mutex_unlock(&usbhid->setup);
1da177e4
LT
1086 dev_dbg(&intf->dev, "resume status %d\n", status);
1087 return status;
1088}
1089
df9a1f48 1090/* Treat USB reset pretty much the same as suspend/resume */
f07600cf 1091static int hid_pre_reset(struct usb_interface *intf)
df9a1f48
AS
1092{
1093 /* FIXME: What if the interface is already suspended? */
1094 hid_suspend(intf, PMSG_ON);
f07600cf 1095 return 0;
df9a1f48
AS
1096}
1097
f07600cf
AS
1098/* Same routine used for post_reset and reset_resume */
1099static int hid_post_reset(struct usb_interface *intf)
df9a1f48
AS
1100{
1101 struct usb_device *dev = interface_to_usbdev (intf);
1102
1103 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1104 /* FIXME: Any more reinitialization needed? */
1105
f07600cf 1106 return hid_resume(intf);
df9a1f48
AS
1107}
1108
1da177e4
LT
1109static struct usb_device_id hid_usb_ids [] = {
1110 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1111 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1112 { } /* Terminating entry */
1113};
1114
1115MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1116
1117static struct usb_driver hid_driver = {
1da177e4
LT
1118 .name = "usbhid",
1119 .probe = hid_probe,
1120 .disconnect = hid_disconnect,
1121 .suspend = hid_suspend,
1122 .resume = hid_resume,
f07600cf 1123 .reset_resume = hid_post_reset,
df9a1f48
AS
1124 .pre_reset = hid_pre_reset,
1125 .post_reset = hid_post_reset,
1da177e4 1126 .id_table = hid_usb_ids,
933e3187 1127 .supports_autosuspend = 1,
1da177e4
LT
1128};
1129
85cdaf52
JS
1130static const struct hid_device_id hid_usb_table[] = {
1131 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1132 { }
1133};
1134
1135static struct hid_driver hid_usb_driver = {
1136 .name = "generic-usb",
1137 .id_table = hid_usb_table,
1138};
1139
1da177e4
LT
1140static int __init hid_init(void)
1141{
1142 int retval;
85cdaf52
JS
1143 retval = hid_register_driver(&hid_usb_driver);
1144 if (retval)
1145 goto hid_register_fail;
876b9276
PW
1146 retval = usbhid_quirks_init(quirks_param);
1147 if (retval)
1148 goto usbhid_quirks_init_fail;
1da177e4
LT
1149 retval = hiddev_init();
1150 if (retval)
1151 goto hiddev_init_fail;
1152 retval = usb_register(&hid_driver);
1153 if (retval)
1154 goto usb_register_fail;
ddbe3249
GKH
1155 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1156 DRIVER_DESC "\n");
1da177e4
LT
1157
1158 return 0;
1159usb_register_fail:
1160 hiddev_exit();
1161hiddev_init_fail:
876b9276
PW
1162 usbhid_quirks_exit();
1163usbhid_quirks_init_fail:
85cdaf52
JS
1164 hid_unregister_driver(&hid_usb_driver);
1165hid_register_fail:
1da177e4
LT
1166 return retval;
1167}
1168
1169static void __exit hid_exit(void)
1170{
1171 usb_deregister(&hid_driver);
1172 hiddev_exit();
876b9276 1173 usbhid_quirks_exit();
85cdaf52 1174 hid_unregister_driver(&hid_usb_driver);
1da177e4
LT
1175}
1176
1177module_init(hid_init);
1178module_exit(hid_exit);
1179
1180MODULE_AUTHOR(DRIVER_AUTHOR);
1181MODULE_DESCRIPTION(DRIVER_DESC);
1182MODULE_LICENSE(DRIVER_LICENSE);