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