2 * USB HID support for Linux
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2007-2008 Oliver Neukum
8 * Copyright (c) 2006-2010 Jiri Kosina
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
24 #include <linux/mutex.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 #include <linux/workqueue.h>
31 #include <linux/string.h>
33 #include <linux/usb.h>
35 #include <linux/hid.h>
36 #include <linux/hiddev.h>
37 #include <linux/hid-debug.h>
38 #include <linux/hidraw.h>
45 #define DRIVER_DESC "USB HID core driver"
46 #define DRIVER_LICENSE "GPL"
52 static unsigned int hid_mousepoll_interval
;
53 module_param_named(mousepoll
, hid_mousepoll_interval
, uint
, 0644);
54 MODULE_PARM_DESC(mousepoll
, "Polling interval of mice");
56 static unsigned int ignoreled
;
57 module_param_named(ignoreled
, ignoreled
, uint
, 0644);
58 MODULE_PARM_DESC(ignoreled
, "Autosuspend with active leds");
60 /* Quirks specified at module load time */
61 static char *quirks_param
[MAX_USBHID_BOOT_QUIRKS
] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS
- 1) ] = NULL
};
62 module_param_array_named(quirks
, quirks_param
, charp
, NULL
, 0444);
63 MODULE_PARM_DESC(quirks
, "Add/modify USB HID quirks by specifying "
64 " quirks=vendorID:productID:quirks"
65 " where vendorID, productID, and quirks are all in"
68 * Input submission and I/O error handler.
70 static DEFINE_MUTEX(hid_open_mut
);
72 static void hid_io_error(struct hid_device
*hid
);
73 static int hid_submit_out(struct hid_device
*hid
);
74 static int hid_submit_ctrl(struct hid_device
*hid
);
75 static void hid_cancel_delayed_stuff(struct usbhid_device
*usbhid
);
77 /* Start up the input URB */
78 static int hid_start_in(struct hid_device
*hid
)
82 struct usbhid_device
*usbhid
= hid
->driver_data
;
84 spin_lock_irqsave(&usbhid
->lock
, flags
);
86 !test_bit(HID_DISCONNECTED
, &usbhid
->iofl
) &&
87 !test_bit(HID_REPORTED_IDLE
, &usbhid
->iofl
) &&
88 !test_and_set_bit(HID_IN_RUNNING
, &usbhid
->iofl
)) {
89 rc
= usb_submit_urb(usbhid
->urbin
, GFP_ATOMIC
);
91 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
93 set_bit(HID_NO_BANDWIDTH
, &usbhid
->iofl
);
95 clear_bit(HID_NO_BANDWIDTH
, &usbhid
->iofl
);
98 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
102 /* I/O retry timer routine */
103 static void hid_retry_timeout(unsigned long _hid
)
105 struct hid_device
*hid
= (struct hid_device
*) _hid
;
106 struct usbhid_device
*usbhid
= hid
->driver_data
;
108 dev_dbg(&usbhid
->intf
->dev
, "retrying intr urb\n");
109 if (hid_start_in(hid
))
113 /* Workqueue routine to reset the device or clear a halt */
114 static void hid_reset(struct work_struct
*work
)
116 struct usbhid_device
*usbhid
=
117 container_of(work
, struct usbhid_device
, reset_work
);
118 struct hid_device
*hid
= usbhid
->hid
;
121 if (test_bit(HID_CLEAR_HALT
, &usbhid
->iofl
)) {
122 dev_dbg(&usbhid
->intf
->dev
, "clear halt\n");
123 rc
= usb_clear_halt(hid_to_usb_dev(hid
), usbhid
->urbin
->pipe
);
124 clear_bit(HID_CLEAR_HALT
, &usbhid
->iofl
);
128 else if (test_bit(HID_RESET_PENDING
, &usbhid
->iofl
)) {
129 dev_dbg(&usbhid
->intf
->dev
, "resetting device\n");
130 rc
= usb_lock_device_for_reset(hid_to_usb_dev(hid
), usbhid
->intf
);
132 rc
= usb_reset_device(hid_to_usb_dev(hid
));
133 usb_unlock_device(hid_to_usb_dev(hid
));
135 clear_bit(HID_RESET_PENDING
, &usbhid
->iofl
);
140 if (!test_bit(HID_IN_RUNNING
, &usbhid
->iofl
))
144 hid_err(hid
, "can't reset device, %s-%s/input%d, status %d\n",
145 hid_to_usb_dev(hid
)->bus
->bus_name
,
146 hid_to_usb_dev(hid
)->devpath
,
156 /* Main I/O error handler */
157 static void hid_io_error(struct hid_device
*hid
)
160 struct usbhid_device
*usbhid
= hid
->driver_data
;
162 spin_lock_irqsave(&usbhid
->lock
, flags
);
164 /* Stop when disconnected */
165 if (test_bit(HID_DISCONNECTED
, &usbhid
->iofl
))
168 /* If it has been a while since the last error, we'll assume
169 * this a brand new error and reset the retry timeout. */
170 if (time_after(jiffies
, usbhid
->stop_retry
+ HZ
/2))
171 usbhid
->retry_delay
= 0;
173 /* When an error occurs, retry at increasing intervals */
174 if (usbhid
->retry_delay
== 0) {
175 usbhid
->retry_delay
= 13; /* Then 26, 52, 104, 104, ... */
176 usbhid
->stop_retry
= jiffies
+ msecs_to_jiffies(1000);
177 } else if (usbhid
->retry_delay
< 100)
178 usbhid
->retry_delay
*= 2;
180 if (time_after(jiffies
, usbhid
->stop_retry
)) {
182 /* Retries failed, so do a port reset unless we lack bandwidth*/
183 if (test_bit(HID_NO_BANDWIDTH
, &usbhid
->iofl
)
184 && !test_and_set_bit(HID_RESET_PENDING
, &usbhid
->iofl
)) {
186 schedule_work(&usbhid
->reset_work
);
191 mod_timer(&usbhid
->io_retry
,
192 jiffies
+ msecs_to_jiffies(usbhid
->retry_delay
));
194 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
197 static void usbhid_mark_busy(struct usbhid_device
*usbhid
)
199 struct usb_interface
*intf
= usbhid
->intf
;
201 usb_mark_last_busy(interface_to_usbdev(intf
));
204 static int usbhid_restart_out_queue(struct usbhid_device
*usbhid
)
206 struct hid_device
*hid
= usb_get_intfdata(usbhid
->intf
);
213 if ((kicked
= (usbhid
->outhead
!= usbhid
->outtail
))) {
214 dbg("Kicking head %d tail %d", usbhid
->outhead
, usbhid
->outtail
);
216 r
= usb_autopm_get_interface_async(usbhid
->intf
);
219 /* Asynchronously flush queue. */
220 set_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
221 if (hid_submit_out(hid
)) {
222 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
223 usb_autopm_put_interface_async(usbhid
->intf
);
225 wake_up(&usbhid
->wait
);
230 static int usbhid_restart_ctrl_queue(struct usbhid_device
*usbhid
)
232 struct hid_device
*hid
= usb_get_intfdata(usbhid
->intf
);
236 WARN_ON(hid
== NULL
);
240 if ((kicked
= (usbhid
->ctrlhead
!= usbhid
->ctrltail
))) {
241 dbg("Kicking head %d tail %d", usbhid
->ctrlhead
, usbhid
->ctrltail
);
243 r
= usb_autopm_get_interface_async(usbhid
->intf
);
246 /* Asynchronously flush queue. */
247 set_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
248 if (hid_submit_ctrl(hid
)) {
249 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
250 usb_autopm_put_interface_async(usbhid
->intf
);
252 wake_up(&usbhid
->wait
);
258 * Input interrupt completion handler.
261 static void hid_irq_in(struct urb
*urb
)
263 struct hid_device
*hid
= urb
->context
;
264 struct usbhid_device
*usbhid
= hid
->driver_data
;
267 switch (urb
->status
) {
268 case 0: /* success */
269 usbhid_mark_busy(usbhid
);
270 usbhid
->retry_delay
= 0;
271 hid_input_report(urb
->context
, HID_INPUT_REPORT
,
272 urb
->transfer_buffer
,
273 urb
->actual_length
, 1);
275 * autosuspend refused while keys are pressed
276 * because most keyboards don't wake up when
279 if (hid_check_keys_pressed(hid
))
280 set_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
);
282 clear_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
);
284 case -EPIPE
: /* stall */
285 usbhid_mark_busy(usbhid
);
286 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
287 set_bit(HID_CLEAR_HALT
, &usbhid
->iofl
);
288 schedule_work(&usbhid
->reset_work
);
290 case -ECONNRESET
: /* unlink */
292 case -ESHUTDOWN
: /* unplug */
293 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
295 case -EILSEQ
: /* protocol error or unplug */
296 case -EPROTO
: /* protocol error or unplug */
297 case -ETIME
: /* protocol error or unplug */
298 case -ETIMEDOUT
: /* Should never happen, but... */
299 usbhid_mark_busy(usbhid
);
300 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
304 hid_warn(urb
->dev
, "input irq status %d received\n",
308 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
310 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
311 if (status
!= -EPERM
) {
312 hid_err(hid
, "can't resubmit intr, %s-%s/input%d, status %d\n",
313 hid_to_usb_dev(hid
)->bus
->bus_name
,
314 hid_to_usb_dev(hid
)->devpath
,
315 usbhid
->ifnum
, status
);
321 static int hid_submit_out(struct hid_device
*hid
)
323 struct hid_report
*report
;
325 struct usbhid_device
*usbhid
= hid
->driver_data
;
328 report
= usbhid
->out
[usbhid
->outtail
].report
;
329 raw_report
= usbhid
->out
[usbhid
->outtail
].raw_report
;
331 usbhid
->urbout
->transfer_buffer_length
= ((report
->size
- 1) >> 3) +
332 1 + (report
->id
> 0);
333 usbhid
->urbout
->dev
= hid_to_usb_dev(hid
);
334 memcpy(usbhid
->outbuf
, raw_report
,
335 usbhid
->urbout
->transfer_buffer_length
);
338 dbg_hid("submitting out urb\n");
340 r
= usb_submit_urb(usbhid
->urbout
, GFP_ATOMIC
);
342 hid_err(hid
, "usb_submit_urb(out) failed: %d\n", r
);
345 usbhid
->last_out
= jiffies
;
349 static int hid_submit_ctrl(struct hid_device
*hid
)
351 struct hid_report
*report
;
355 struct usbhid_device
*usbhid
= hid
->driver_data
;
357 report
= usbhid
->ctrl
[usbhid
->ctrltail
].report
;
358 raw_report
= usbhid
->ctrl
[usbhid
->ctrltail
].raw_report
;
359 dir
= usbhid
->ctrl
[usbhid
->ctrltail
].dir
;
361 len
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
362 if (dir
== USB_DIR_OUT
) {
363 usbhid
->urbctrl
->pipe
= usb_sndctrlpipe(hid_to_usb_dev(hid
), 0);
364 usbhid
->urbctrl
->transfer_buffer_length
= len
;
365 memcpy(usbhid
->ctrlbuf
, raw_report
, len
);
368 int maxpacket
, padlen
;
370 usbhid
->urbctrl
->pipe
= usb_rcvctrlpipe(hid_to_usb_dev(hid
), 0);
371 maxpacket
= usb_maxpacket(hid_to_usb_dev(hid
),
372 usbhid
->urbctrl
->pipe
, 0);
374 padlen
= DIV_ROUND_UP(len
, maxpacket
);
376 if (padlen
> usbhid
->bufsize
)
377 padlen
= usbhid
->bufsize
;
380 usbhid
->urbctrl
->transfer_buffer_length
= padlen
;
382 usbhid
->urbctrl
->dev
= hid_to_usb_dev(hid
);
384 usbhid
->cr
->bRequestType
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| dir
;
385 usbhid
->cr
->bRequest
= (dir
== USB_DIR_OUT
) ? HID_REQ_SET_REPORT
:
387 usbhid
->cr
->wValue
= cpu_to_le16(((report
->type
+ 1) << 8) |
389 usbhid
->cr
->wIndex
= cpu_to_le16(usbhid
->ifnum
);
390 usbhid
->cr
->wLength
= cpu_to_le16(len
);
392 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
393 usbhid
->cr
->bRequest
== HID_REQ_SET_REPORT
? "Set_Report" :
395 usbhid
->cr
->wValue
, usbhid
->cr
->wIndex
, usbhid
->cr
->wLength
);
397 r
= usb_submit_urb(usbhid
->urbctrl
, GFP_ATOMIC
);
399 hid_err(hid
, "usb_submit_urb(ctrl) failed: %d\n", r
);
402 usbhid
->last_ctrl
= jiffies
;
407 * Output interrupt completion handler.
410 static void hid_irq_out(struct urb
*urb
)
412 struct hid_device
*hid
= urb
->context
;
413 struct usbhid_device
*usbhid
= hid
->driver_data
;
417 switch (urb
->status
) {
418 case 0: /* success */
420 case -ESHUTDOWN
: /* unplug */
422 case -EILSEQ
: /* protocol error or unplug */
423 case -EPROTO
: /* protocol error or unplug */
424 case -ECONNRESET
: /* unlink */
428 hid_warn(urb
->dev
, "output irq status %d received\n",
432 spin_lock_irqsave(&usbhid
->lock
, flags
);
435 usbhid
->outtail
= usbhid
->outhead
;
437 usbhid
->outtail
= (usbhid
->outtail
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1);
439 if (usbhid
->outhead
!= usbhid
->outtail
&& !hid_submit_out(hid
)) {
440 /* Successfully submitted next urb in queue */
441 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
445 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
446 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
447 usb_autopm_put_interface_async(usbhid
->intf
);
448 wake_up(&usbhid
->wait
);
452 * Control pipe completion handler.
455 static void hid_ctrl(struct urb
*urb
)
457 struct hid_device
*hid
= urb
->context
;
458 struct usbhid_device
*usbhid
= hid
->driver_data
;
459 int unplug
= 0, status
= urb
->status
;
461 spin_lock(&usbhid
->lock
);
464 case 0: /* success */
465 if (usbhid
->ctrl
[usbhid
->ctrltail
].dir
== USB_DIR_IN
)
466 hid_input_report(urb
->context
,
467 usbhid
->ctrl
[usbhid
->ctrltail
].report
->type
,
468 urb
->transfer_buffer
, urb
->actual_length
, 0);
470 case -ESHUTDOWN
: /* unplug */
472 case -EILSEQ
: /* protocol error or unplug */
473 case -EPROTO
: /* protocol error or unplug */
474 case -ECONNRESET
: /* unlink */
476 case -EPIPE
: /* report not available */
479 hid_warn(urb
->dev
, "ctrl urb status %d received\n", status
);
483 usbhid
->ctrltail
= usbhid
->ctrlhead
;
485 usbhid
->ctrltail
= (usbhid
->ctrltail
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1);
487 if (usbhid
->ctrlhead
!= usbhid
->ctrltail
&& !hid_submit_ctrl(hid
)) {
488 /* Successfully submitted next urb in queue */
489 spin_unlock(&usbhid
->lock
);
493 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
494 spin_unlock(&usbhid
->lock
);
495 usb_autopm_put_interface_async(usbhid
->intf
);
496 wake_up(&usbhid
->wait
);
499 static void __usbhid_submit_report(struct hid_device
*hid
, struct hid_report
*report
,
503 struct usbhid_device
*usbhid
= hid
->driver_data
;
504 int len
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
506 if ((hid
->quirks
& HID_QUIRK_NOGET
) && dir
== USB_DIR_IN
)
509 if (usbhid
->urbout
&& dir
== USB_DIR_OUT
&& report
->type
== HID_OUTPUT_REPORT
) {
510 if ((head
= (usbhid
->outhead
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1)) == usbhid
->outtail
) {
511 hid_warn(hid
, "output queue full\n");
515 usbhid
->out
[usbhid
->outhead
].raw_report
= kmalloc(len
, GFP_ATOMIC
);
516 if (!usbhid
->out
[usbhid
->outhead
].raw_report
) {
517 hid_warn(hid
, "output queueing failed\n");
520 hid_output_report(report
, usbhid
->out
[usbhid
->outhead
].raw_report
);
521 usbhid
->out
[usbhid
->outhead
].report
= report
;
522 usbhid
->outhead
= head
;
524 /* Try to awake from autosuspend... */
525 if (usb_autopm_get_interface_async(usbhid
->intf
) < 0)
529 * But if still suspended, leave urb enqueued, don't submit.
530 * Submission will occur if/when resume() drains the queue.
532 if (test_bit(HID_REPORTED_IDLE
, &usbhid
->iofl
))
535 if (!test_and_set_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)) {
536 if (hid_submit_out(hid
)) {
537 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
538 usb_autopm_put_interface_async(usbhid
->intf
);
540 wake_up(&usbhid
->wait
);
543 * the queue is known to run
544 * but an earlier request may be stuck
545 * we may need to time out
546 * no race because this is called under
549 if (time_after(jiffies
, usbhid
->last_out
+ HZ
* 5))
550 usb_unlink_urb(usbhid
->urbout
);
555 if ((head
= (usbhid
->ctrlhead
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1)) == usbhid
->ctrltail
) {
556 hid_warn(hid
, "control queue full\n");
560 if (dir
== USB_DIR_OUT
) {
561 usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
= kmalloc(len
, GFP_ATOMIC
);
562 if (!usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
) {
563 hid_warn(hid
, "control queueing failed\n");
566 hid_output_report(report
, usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
);
568 usbhid
->ctrl
[usbhid
->ctrlhead
].report
= report
;
569 usbhid
->ctrl
[usbhid
->ctrlhead
].dir
= dir
;
570 usbhid
->ctrlhead
= head
;
572 /* Try to awake from autosuspend... */
573 if (usb_autopm_get_interface_async(usbhid
->intf
) < 0)
577 * If already suspended, leave urb enqueued, but don't submit.
578 * Submission will occur if/when resume() drains the queue.
580 if (test_bit(HID_REPORTED_IDLE
, &usbhid
->iofl
))
583 if (!test_and_set_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
)) {
584 if (hid_submit_ctrl(hid
)) {
585 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
586 usb_autopm_put_interface_async(usbhid
->intf
);
588 wake_up(&usbhid
->wait
);
591 * the queue is known to run
592 * but an earlier request may be stuck
593 * we may need to time out
594 * no race because this is called under
597 if (time_after(jiffies
, usbhid
->last_ctrl
+ HZ
* 5))
598 usb_unlink_urb(usbhid
->urbctrl
);
602 void usbhid_submit_report(struct hid_device
*hid
, struct hid_report
*report
, unsigned char dir
)
604 struct usbhid_device
*usbhid
= hid
->driver_data
;
607 spin_lock_irqsave(&usbhid
->lock
, flags
);
608 __usbhid_submit_report(hid
, report
, dir
);
609 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
611 EXPORT_SYMBOL_GPL(usbhid_submit_report
);
613 /* Workqueue routine to send requests to change LEDs */
614 static void hid_led(struct work_struct
*work
)
616 struct usbhid_device
*usbhid
=
617 container_of(work
, struct usbhid_device
, led_work
);
618 struct hid_device
*hid
= usbhid
->hid
;
619 struct hid_field
*field
;
622 field
= hidinput_get_led_field(hid
);
624 hid_warn(hid
, "LED event field not found\n");
628 spin_lock_irqsave(&usbhid
->lock
, flags
);
629 if (!test_bit(HID_DISCONNECTED
, &usbhid
->iofl
)) {
630 usbhid
->ledcount
= hidinput_count_leds(hid
);
631 hid_dbg(usbhid
->hid
, "New ledcount = %u\n", usbhid
->ledcount
);
632 __usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
634 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
637 static int usb_hidinput_input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
639 struct hid_device
*hid
= input_get_drvdata(dev
);
640 struct usbhid_device
*usbhid
= hid
->driver_data
;
641 struct hid_field
*field
;
646 return input_ff_event(dev
, type
, code
, value
);
651 if ((offset
= hidinput_find_field(hid
, type
, code
, &field
)) == -1) {
652 hid_warn(dev
, "event field not found\n");
656 spin_lock_irqsave(&usbhid
->lock
, flags
);
657 hid_set_field(field
, offset
, value
);
658 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
661 * Defer performing requested LED action.
662 * This is more likely gather all LED changes into a single URB.
664 schedule_work(&usbhid
->led_work
);
669 int usbhid_wait_io(struct hid_device
*hid
)
671 struct usbhid_device
*usbhid
= hid
->driver_data
;
673 if (!wait_event_timeout(usbhid
->wait
,
674 (!test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
) &&
675 !test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)),
677 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
683 EXPORT_SYMBOL_GPL(usbhid_wait_io
);
685 static int hid_set_idle(struct usb_device
*dev
, int ifnum
, int report
, int idle
)
687 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
688 HID_REQ_SET_IDLE
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
, (idle
<< 8) | report
,
689 ifnum
, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
692 static int hid_get_class_descriptor(struct usb_device
*dev
, int ifnum
,
693 unsigned char type
, void *buf
, int size
)
695 int result
, retries
= 4;
697 memset(buf
, 0, size
);
700 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
701 USB_REQ_GET_DESCRIPTOR
, USB_RECIP_INTERFACE
| USB_DIR_IN
,
702 (type
<< 8), ifnum
, buf
, size
, USB_CTRL_GET_TIMEOUT
);
704 } while (result
< size
&& retries
);
708 int usbhid_open(struct hid_device
*hid
)
710 struct usbhid_device
*usbhid
= hid
->driver_data
;
713 mutex_lock(&hid_open_mut
);
715 res
= usb_autopm_get_interface(usbhid
->intf
);
716 /* the device must be awake to reliably request remote wakeup */
722 usbhid
->intf
->needs_remote_wakeup
= 1;
723 res
= hid_start_in(hid
);
725 if (res
!= -ENOSPC
) {
729 /* no use opening if resources are insufficient */
732 usbhid
->intf
->needs_remote_wakeup
= 0;
735 usb_autopm_put_interface(usbhid
->intf
);
738 mutex_unlock(&hid_open_mut
);
742 void usbhid_close(struct hid_device
*hid
)
744 struct usbhid_device
*usbhid
= hid
->driver_data
;
746 mutex_lock(&hid_open_mut
);
748 /* protecting hid->open to make sure we don't restart
749 * data acquistion due to a resumption we no longer
752 spin_lock_irq(&usbhid
->lock
);
754 spin_unlock_irq(&usbhid
->lock
);
755 hid_cancel_delayed_stuff(usbhid
);
756 usb_kill_urb(usbhid
->urbin
);
757 usbhid
->intf
->needs_remote_wakeup
= 0;
759 spin_unlock_irq(&usbhid
->lock
);
761 mutex_unlock(&hid_open_mut
);
765 * Initialize all reports
768 void usbhid_init_reports(struct hid_device
*hid
)
770 struct hid_report
*report
;
771 struct usbhid_device
*usbhid
= hid
->driver_data
;
774 list_for_each_entry(report
, &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
775 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
777 list_for_each_entry(report
, &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
778 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
781 ret
= usbhid_wait_io(hid
);
784 if (test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
785 usb_kill_urb(usbhid
->urbctrl
);
786 if (test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
787 usb_kill_urb(usbhid
->urbout
);
788 ret
= usbhid_wait_io(hid
);
792 hid_warn(hid
, "timeout initializing reports\n");
796 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
798 static int hid_find_field_early(struct hid_device
*hid
, unsigned int page
,
799 unsigned int hid_code
, struct hid_field
**pfield
)
801 struct hid_report
*report
;
802 struct hid_field
*field
;
803 struct hid_usage
*usage
;
806 list_for_each_entry(report
, &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
, list
) {
807 for (i
= 0; i
< report
->maxfield
; i
++) {
808 field
= report
->field
[i
];
809 for (j
= 0; j
< field
->maxusage
; j
++) {
810 usage
= &field
->usage
[j
];
811 if ((usage
->hid
& HID_USAGE_PAGE
) == page
&&
812 (usage
->hid
& 0xFFFF) == hid_code
) {
822 void usbhid_set_leds(struct hid_device
*hid
)
824 struct hid_field
*field
;
827 if ((offset
= hid_find_field_early(hid
, HID_UP_LED
, 0x01, &field
)) != -1) {
828 hid_set_field(field
, offset
, 0);
829 usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
832 EXPORT_SYMBOL_GPL(usbhid_set_leds
);
835 * Traverse the supplied list of reports and find the longest
837 static void hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
840 struct hid_report
*report
;
843 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
844 size
= ((report
->size
- 1) >> 3) + 1 + hid
->report_enum
[type
].numbered
;
850 static int hid_alloc_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
852 struct usbhid_device
*usbhid
= hid
->driver_data
;
854 usbhid
->inbuf
= usb_alloc_coherent(dev
, usbhid
->bufsize
, GFP_KERNEL
,
856 usbhid
->outbuf
= usb_alloc_coherent(dev
, usbhid
->bufsize
, GFP_KERNEL
,
857 &usbhid
->outbuf_dma
);
858 usbhid
->cr
= kmalloc(sizeof(*usbhid
->cr
), GFP_KERNEL
);
859 usbhid
->ctrlbuf
= usb_alloc_coherent(dev
, usbhid
->bufsize
, GFP_KERNEL
,
860 &usbhid
->ctrlbuf_dma
);
861 if (!usbhid
->inbuf
|| !usbhid
->outbuf
|| !usbhid
->cr
||
868 static int usbhid_get_raw_report(struct hid_device
*hid
,
869 unsigned char report_number
, __u8
*buf
, size_t count
,
870 unsigned char report_type
)
872 struct usbhid_device
*usbhid
= hid
->driver_data
;
873 struct usb_device
*dev
= hid_to_usb_dev(hid
);
874 struct usb_interface
*intf
= usbhid
->intf
;
875 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
876 int skipped_report_id
= 0;
879 /* Byte 0 is the report number. Report data starts at byte 1.*/
880 buf
[0] = report_number
;
881 if (report_number
== 0x0) {
882 /* Offset the return buffer by 1, so that the report ID
883 will remain in byte 0. */
886 skipped_report_id
= 1;
888 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
890 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
891 ((report_type
+ 1) << 8) | report_number
,
892 interface
->desc
.bInterfaceNumber
, buf
, count
,
893 USB_CTRL_SET_TIMEOUT
);
895 /* count also the report id */
896 if (ret
> 0 && skipped_report_id
)
902 static int usbhid_output_raw_report(struct hid_device
*hid
, __u8
*buf
, size_t count
,
903 unsigned char report_type
)
905 struct usbhid_device
*usbhid
= hid
->driver_data
;
906 struct usb_device
*dev
= hid_to_usb_dev(hid
);
907 struct usb_interface
*intf
= usbhid
->intf
;
908 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
911 if (usbhid
->urbout
&& report_type
!= HID_FEATURE_REPORT
) {
913 int skipped_report_id
= 0;
916 /* Don't send the Report ID */
919 skipped_report_id
= 1;
921 ret
= usb_interrupt_msg(dev
, usbhid
->urbout
->pipe
,
922 buf
, count
, &actual_length
,
923 USB_CTRL_SET_TIMEOUT
);
924 /* return the number of bytes transferred */
927 /* count also the report id */
928 if (skipped_report_id
)
932 int skipped_report_id
= 0;
933 int report_id
= buf
[0];
935 /* Don't send the Report ID */
938 skipped_report_id
= 1;
940 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
942 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
943 ((report_type
+ 1) << 8) | report_id
,
944 interface
->desc
.bInterfaceNumber
, buf
, count
,
945 USB_CTRL_SET_TIMEOUT
);
946 /* count also the report id, if this was a numbered report. */
947 if (ret
> 0 && skipped_report_id
)
954 static void usbhid_restart_queues(struct usbhid_device
*usbhid
)
957 usbhid_restart_out_queue(usbhid
);
958 usbhid_restart_ctrl_queue(usbhid
);
961 static void hid_free_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
963 struct usbhid_device
*usbhid
= hid
->driver_data
;
965 usb_free_coherent(dev
, usbhid
->bufsize
, usbhid
->inbuf
, usbhid
->inbuf_dma
);
966 usb_free_coherent(dev
, usbhid
->bufsize
, usbhid
->outbuf
, usbhid
->outbuf_dma
);
968 usb_free_coherent(dev
, usbhid
->bufsize
, usbhid
->ctrlbuf
, usbhid
->ctrlbuf_dma
);
971 static int usbhid_parse(struct hid_device
*hid
)
973 struct usb_interface
*intf
= to_usb_interface(hid
->dev
.parent
);
974 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
975 struct usb_device
*dev
= interface_to_usbdev (intf
);
976 struct hid_descriptor
*hdesc
;
978 unsigned int rsize
= 0;
982 quirks
= usbhid_lookup_quirk(le16_to_cpu(dev
->descriptor
.idVendor
),
983 le16_to_cpu(dev
->descriptor
.idProduct
));
985 if (quirks
& HID_QUIRK_IGNORE
)
988 /* Many keyboards and mice don't like to be polled for reports,
989 * so we will always set the HID_QUIRK_NOGET flag for them. */
990 if (interface
->desc
.bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
) {
991 if (interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_KEYBOARD
||
992 interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_MOUSE
)
993 quirks
|= HID_QUIRK_NOGET
;
996 if (usb_get_extra_descriptor(interface
, HID_DT_HID
, &hdesc
) &&
997 (!interface
->desc
.bNumEndpoints
||
998 usb_get_extra_descriptor(&interface
->endpoint
[0], HID_DT_HID
, &hdesc
))) {
999 dbg_hid("class descriptor not present\n");
1003 hid
->version
= le16_to_cpu(hdesc
->bcdHID
);
1004 hid
->country
= hdesc
->bCountryCode
;
1006 for (n
= 0; n
< hdesc
->bNumDescriptors
; n
++)
1007 if (hdesc
->desc
[n
].bDescriptorType
== HID_DT_REPORT
)
1008 rsize
= le16_to_cpu(hdesc
->desc
[n
].wDescriptorLength
);
1010 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
1011 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
1015 if (!(rdesc
= kmalloc(rsize
, GFP_KERNEL
))) {
1016 dbg_hid("couldn't allocate rdesc memory\n");
1020 hid_set_idle(dev
, interface
->desc
.bInterfaceNumber
, 0, 0);
1022 ret
= hid_get_class_descriptor(dev
, interface
->desc
.bInterfaceNumber
,
1023 HID_DT_REPORT
, rdesc
, rsize
);
1025 dbg_hid("reading report descriptor failed\n");
1030 ret
= hid_parse_report(hid
, rdesc
, rsize
);
1033 dbg_hid("parsing report descriptor failed\n");
1037 hid
->quirks
|= quirks
;
1044 static int usbhid_start(struct hid_device
*hid
)
1046 struct usb_interface
*intf
= to_usb_interface(hid
->dev
.parent
);
1047 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1048 struct usb_device
*dev
= interface_to_usbdev(intf
);
1049 struct usbhid_device
*usbhid
= hid
->driver_data
;
1050 unsigned int n
, insize
= 0;
1053 clear_bit(HID_DISCONNECTED
, &usbhid
->iofl
);
1055 usbhid
->bufsize
= HID_MIN_BUFFER_SIZE
;
1056 hid_find_max_report(hid
, HID_INPUT_REPORT
, &usbhid
->bufsize
);
1057 hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &usbhid
->bufsize
);
1058 hid_find_max_report(hid
, HID_FEATURE_REPORT
, &usbhid
->bufsize
);
1060 if (usbhid
->bufsize
> HID_MAX_BUFFER_SIZE
)
1061 usbhid
->bufsize
= HID_MAX_BUFFER_SIZE
;
1063 hid_find_max_report(hid
, HID_INPUT_REPORT
, &insize
);
1065 if (insize
> HID_MAX_BUFFER_SIZE
)
1066 insize
= HID_MAX_BUFFER_SIZE
;
1068 if (hid_alloc_buffers(dev
, hid
)) {
1073 for (n
= 0; n
< interface
->desc
.bNumEndpoints
; n
++) {
1074 struct usb_endpoint_descriptor
*endpoint
;
1078 endpoint
= &interface
->endpoint
[n
].desc
;
1079 if (!usb_endpoint_xfer_int(endpoint
))
1082 interval
= endpoint
->bInterval
;
1084 /* Some vendors give fullspeed interval on highspeed devides */
1085 if (hid
->quirks
& HID_QUIRK_FULLSPEED_INTERVAL
&&
1086 dev
->speed
== USB_SPEED_HIGH
) {
1087 interval
= fls(endpoint
->bInterval
*8);
1088 printk(KERN_INFO
"%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1089 hid
->name
, endpoint
->bInterval
, interval
);
1092 /* Change the polling interval of mice. */
1093 if (hid
->collection
->usage
== HID_GD_MOUSE
&& hid_mousepoll_interval
> 0)
1094 interval
= hid_mousepoll_interval
;
1097 if (usb_endpoint_dir_in(endpoint
)) {
1100 if (!(usbhid
->urbin
= usb_alloc_urb(0, GFP_KERNEL
)))
1102 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
1103 usb_fill_int_urb(usbhid
->urbin
, dev
, pipe
, usbhid
->inbuf
, insize
,
1104 hid_irq_in
, hid
, interval
);
1105 usbhid
->urbin
->transfer_dma
= usbhid
->inbuf_dma
;
1106 usbhid
->urbin
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1110 if (!(usbhid
->urbout
= usb_alloc_urb(0, GFP_KERNEL
)))
1112 pipe
= usb_sndintpipe(dev
, endpoint
->bEndpointAddress
);
1113 usb_fill_int_urb(usbhid
->urbout
, dev
, pipe
, usbhid
->outbuf
, 0,
1114 hid_irq_out
, hid
, interval
);
1115 usbhid
->urbout
->transfer_dma
= usbhid
->outbuf_dma
;
1116 usbhid
->urbout
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1120 usbhid
->urbctrl
= usb_alloc_urb(0, GFP_KERNEL
);
1121 if (!usbhid
->urbctrl
) {
1126 usb_fill_control_urb(usbhid
->urbctrl
, dev
, 0, (void *) usbhid
->cr
,
1127 usbhid
->ctrlbuf
, 1, hid_ctrl
, hid
);
1128 usbhid
->urbctrl
->transfer_dma
= usbhid
->ctrlbuf_dma
;
1129 usbhid
->urbctrl
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1131 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
1132 usbhid_init_reports(hid
);
1134 set_bit(HID_STARTED
, &usbhid
->iofl
);
1136 /* Some keyboards don't work until their LEDs have been set.
1137 * Since BIOSes do set the LEDs, it must be safe for any device
1138 * that supports the keyboard boot protocol.
1139 * In addition, enable remote wakeup by default for all keyboard
1140 * devices supporting the boot protocol.
1142 if (interface
->desc
.bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
&&
1143 interface
->desc
.bInterfaceProtocol
==
1144 USB_INTERFACE_PROTOCOL_KEYBOARD
) {
1145 usbhid_set_leds(hid
);
1146 device_set_wakeup_enable(&dev
->dev
, 1);
1151 usb_free_urb(usbhid
->urbin
);
1152 usb_free_urb(usbhid
->urbout
);
1153 usb_free_urb(usbhid
->urbctrl
);
1154 usbhid
->urbin
= NULL
;
1155 usbhid
->urbout
= NULL
;
1156 usbhid
->urbctrl
= NULL
;
1157 hid_free_buffers(dev
, hid
);
1161 static void usbhid_stop(struct hid_device
*hid
)
1163 struct usbhid_device
*usbhid
= hid
->driver_data
;
1165 if (WARN_ON(!usbhid
))
1168 clear_bit(HID_STARTED
, &usbhid
->iofl
);
1169 spin_lock_irq(&usbhid
->lock
); /* Sync with error and led handlers */
1170 set_bit(HID_DISCONNECTED
, &usbhid
->iofl
);
1171 spin_unlock_irq(&usbhid
->lock
);
1172 usb_kill_urb(usbhid
->urbin
);
1173 usb_kill_urb(usbhid
->urbout
);
1174 usb_kill_urb(usbhid
->urbctrl
);
1176 hid_cancel_delayed_stuff(usbhid
);
1180 usb_free_urb(usbhid
->urbin
);
1181 usb_free_urb(usbhid
->urbctrl
);
1182 usb_free_urb(usbhid
->urbout
);
1183 usbhid
->urbin
= NULL
; /* don't mess up next start */
1184 usbhid
->urbctrl
= NULL
;
1185 usbhid
->urbout
= NULL
;
1187 hid_free_buffers(hid_to_usb_dev(hid
), hid
);
1190 static int usbhid_power(struct hid_device
*hid
, int lvl
)
1195 case PM_HINT_FULLON
:
1196 r
= usbhid_get_power(hid
);
1198 case PM_HINT_NORMAL
:
1199 usbhid_put_power(hid
);
1205 static struct hid_ll_driver usb_hid_driver
= {
1206 .parse
= usbhid_parse
,
1207 .start
= usbhid_start
,
1208 .stop
= usbhid_stop
,
1209 .open
= usbhid_open
,
1210 .close
= usbhid_close
,
1211 .power
= usbhid_power
,
1212 .hidinput_input_event
= usb_hidinput_input_event
,
1215 static int usbhid_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1217 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1218 struct usb_device
*dev
= interface_to_usbdev(intf
);
1219 struct usbhid_device
*usbhid
;
1220 struct hid_device
*hid
;
1221 unsigned int n
, has_in
= 0;
1225 dbg_hid("HID probe called for ifnum %d\n",
1226 intf
->altsetting
->desc
.bInterfaceNumber
);
1228 for (n
= 0; n
< interface
->desc
.bNumEndpoints
; n
++)
1229 if (usb_endpoint_is_int_in(&interface
->endpoint
[n
].desc
))
1232 hid_err(intf
, "couldn't find an input interrupt endpoint\n");
1236 hid
= hid_allocate_device();
1238 return PTR_ERR(hid
);
1240 usb_set_intfdata(intf
, hid
);
1241 hid
->ll_driver
= &usb_hid_driver
;
1242 hid
->hid_get_raw_report
= usbhid_get_raw_report
;
1243 hid
->hid_output_raw_report
= usbhid_output_raw_report
;
1244 hid
->ff_init
= hid_pidff_init
;
1245 #ifdef CONFIG_USB_HIDDEV
1246 hid
->hiddev_connect
= hiddev_connect
;
1247 hid
->hiddev_disconnect
= hiddev_disconnect
;
1248 hid
->hiddev_hid_event
= hiddev_hid_event
;
1249 hid
->hiddev_report_event
= hiddev_report_event
;
1251 hid
->dev
.parent
= &intf
->dev
;
1253 hid
->vendor
= le16_to_cpu(dev
->descriptor
.idVendor
);
1254 hid
->product
= le16_to_cpu(dev
->descriptor
.idProduct
);
1256 hid
->quirks
= usbhid_lookup_quirk(hid
->vendor
, hid
->product
);
1257 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
==
1258 USB_INTERFACE_PROTOCOL_MOUSE
)
1259 hid
->type
= HID_TYPE_USBMOUSE
;
1260 else if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
== 0)
1261 hid
->type
= HID_TYPE_USBNONE
;
1263 if (dev
->manufacturer
)
1264 strlcpy(hid
->name
, dev
->manufacturer
, sizeof(hid
->name
));
1267 if (dev
->manufacturer
)
1268 strlcat(hid
->name
, " ", sizeof(hid
->name
));
1269 strlcat(hid
->name
, dev
->product
, sizeof(hid
->name
));
1272 if (!strlen(hid
->name
))
1273 snprintf(hid
->name
, sizeof(hid
->name
), "HID %04x:%04x",
1274 le16_to_cpu(dev
->descriptor
.idVendor
),
1275 le16_to_cpu(dev
->descriptor
.idProduct
));
1277 usb_make_path(dev
, hid
->phys
, sizeof(hid
->phys
));
1278 strlcat(hid
->phys
, "/input", sizeof(hid
->phys
));
1279 len
= strlen(hid
->phys
);
1280 if (len
< sizeof(hid
->phys
) - 1)
1281 snprintf(hid
->phys
+ len
, sizeof(hid
->phys
) - len
,
1282 "%d", intf
->altsetting
[0].desc
.bInterfaceNumber
);
1284 if (usb_string(dev
, dev
->descriptor
.iSerialNumber
, hid
->uniq
, 64) <= 0)
1287 usbhid
= kzalloc(sizeof(*usbhid
), GFP_KERNEL
);
1288 if (usbhid
== NULL
) {
1293 hid
->driver_data
= usbhid
;
1295 usbhid
->intf
= intf
;
1296 usbhid
->ifnum
= interface
->desc
.bInterfaceNumber
;
1298 init_waitqueue_head(&usbhid
->wait
);
1299 INIT_WORK(&usbhid
->reset_work
, hid_reset
);
1300 setup_timer(&usbhid
->io_retry
, hid_retry_timeout
, (unsigned long) hid
);
1301 spin_lock_init(&usbhid
->lock
);
1303 INIT_WORK(&usbhid
->led_work
, hid_led
);
1305 ret
= hid_add_device(hid
);
1308 hid_err(intf
, "can't add hid device: %d\n", ret
);
1316 hid_destroy_device(hid
);
1320 static void usbhid_disconnect(struct usb_interface
*intf
)
1322 struct hid_device
*hid
= usb_get_intfdata(intf
);
1323 struct usbhid_device
*usbhid
;
1328 usbhid
= hid
->driver_data
;
1329 hid_destroy_device(hid
);
1333 static void hid_cancel_delayed_stuff(struct usbhid_device
*usbhid
)
1335 del_timer_sync(&usbhid
->io_retry
);
1336 cancel_work_sync(&usbhid
->reset_work
);
1337 cancel_work_sync(&usbhid
->led_work
);
1340 static void hid_cease_io(struct usbhid_device
*usbhid
)
1342 del_timer_sync(&usbhid
->io_retry
);
1343 usb_kill_urb(usbhid
->urbin
);
1344 usb_kill_urb(usbhid
->urbctrl
);
1345 usb_kill_urb(usbhid
->urbout
);
1348 /* Treat USB reset pretty much the same as suspend/resume */
1349 static int hid_pre_reset(struct usb_interface
*intf
)
1351 struct hid_device
*hid
= usb_get_intfdata(intf
);
1352 struct usbhid_device
*usbhid
= hid
->driver_data
;
1354 spin_lock_irq(&usbhid
->lock
);
1355 set_bit(HID_RESET_PENDING
, &usbhid
->iofl
);
1356 spin_unlock_irq(&usbhid
->lock
);
1357 hid_cease_io(usbhid
);
1362 /* Same routine used for post_reset and reset_resume */
1363 static int hid_post_reset(struct usb_interface
*intf
)
1365 struct usb_device
*dev
= interface_to_usbdev (intf
);
1366 struct hid_device
*hid
= usb_get_intfdata(intf
);
1367 struct usbhid_device
*usbhid
= hid
->driver_data
;
1368 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1372 /* Fetch and examine the HID report descriptor. If this
1373 * has changed, then rebind. Since usbcore's check of the
1374 * configuration descriptors passed, we already know that
1375 * the size of the HID report descriptor has not changed.
1377 rdesc
= kmalloc(hid
->rsize
, GFP_KERNEL
);
1379 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1382 status
= hid_get_class_descriptor(dev
,
1383 interface
->desc
.bInterfaceNumber
,
1384 HID_DT_REPORT
, rdesc
, hid
->rsize
);
1386 dbg_hid("reading report descriptor failed (post_reset)\n");
1390 status
= memcmp(rdesc
, hid
->rdesc
, hid
->rsize
);
1393 dbg_hid("report descriptor changed\n");
1397 spin_lock_irq(&usbhid
->lock
);
1398 clear_bit(HID_RESET_PENDING
, &usbhid
->iofl
);
1399 spin_unlock_irq(&usbhid
->lock
);
1400 hid_set_idle(dev
, intf
->cur_altsetting
->desc
.bInterfaceNumber
, 0, 0);
1401 status
= hid_start_in(hid
);
1404 usbhid_restart_queues(usbhid
);
1409 int usbhid_get_power(struct hid_device
*hid
)
1411 struct usbhid_device
*usbhid
= hid
->driver_data
;
1413 return usb_autopm_get_interface(usbhid
->intf
);
1416 void usbhid_put_power(struct hid_device
*hid
)
1418 struct usbhid_device
*usbhid
= hid
->driver_data
;
1420 usb_autopm_put_interface(usbhid
->intf
);
1425 static int hid_suspend(struct usb_interface
*intf
, pm_message_t message
)
1427 struct hid_device
*hid
= usb_get_intfdata(intf
);
1428 struct usbhid_device
*usbhid
= hid
->driver_data
;
1431 if (PMSG_IS_AUTO(message
)) {
1432 spin_lock_irq(&usbhid
->lock
); /* Sync with error handler */
1433 if (!test_bit(HID_RESET_PENDING
, &usbhid
->iofl
)
1434 && !test_bit(HID_CLEAR_HALT
, &usbhid
->iofl
)
1435 && !test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)
1436 && !test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
)
1437 && !test_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
)
1438 && (!usbhid
->ledcount
|| ignoreled
))
1440 set_bit(HID_REPORTED_IDLE
, &usbhid
->iofl
);
1441 spin_unlock_irq(&usbhid
->lock
);
1442 if (hid
->driver
&& hid
->driver
->suspend
) {
1443 status
= hid
->driver
->suspend(hid
, message
);
1448 usbhid_mark_busy(usbhid
);
1449 spin_unlock_irq(&usbhid
->lock
);
1454 if (hid
->driver
&& hid
->driver
->suspend
) {
1455 status
= hid
->driver
->suspend(hid
, message
);
1459 spin_lock_irq(&usbhid
->lock
);
1460 set_bit(HID_REPORTED_IDLE
, &usbhid
->iofl
);
1461 spin_unlock_irq(&usbhid
->lock
);
1462 if (usbhid_wait_io(hid
) < 0)
1466 hid_cancel_delayed_stuff(usbhid
);
1467 hid_cease_io(usbhid
);
1469 if (PMSG_IS_AUTO(message
) && test_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
)) {
1470 /* lost race against keypresses */
1471 status
= hid_start_in(hid
);
1474 usbhid_mark_busy(usbhid
);
1477 dev_dbg(&intf
->dev
, "suspend\n");
1481 static int hid_resume(struct usb_interface
*intf
)
1483 struct hid_device
*hid
= usb_get_intfdata (intf
);
1484 struct usbhid_device
*usbhid
= hid
->driver_data
;
1487 if (!test_bit(HID_STARTED
, &usbhid
->iofl
))
1490 clear_bit(HID_REPORTED_IDLE
, &usbhid
->iofl
);
1491 usbhid_mark_busy(usbhid
);
1493 if (test_bit(HID_CLEAR_HALT
, &usbhid
->iofl
) ||
1494 test_bit(HID_RESET_PENDING
, &usbhid
->iofl
))
1495 schedule_work(&usbhid
->reset_work
);
1496 usbhid
->retry_delay
= 0;
1497 status
= hid_start_in(hid
);
1500 usbhid_restart_queues(usbhid
);
1502 if (status
>= 0 && hid
->driver
&& hid
->driver
->resume
) {
1503 int ret
= hid
->driver
->resume(hid
);
1507 dev_dbg(&intf
->dev
, "resume status %d\n", status
);
1511 static int hid_reset_resume(struct usb_interface
*intf
)
1513 struct hid_device
*hid
= usb_get_intfdata(intf
);
1514 struct usbhid_device
*usbhid
= hid
->driver_data
;
1517 clear_bit(HID_REPORTED_IDLE
, &usbhid
->iofl
);
1518 status
= hid_post_reset(intf
);
1519 if (status
>= 0 && hid
->driver
&& hid
->driver
->reset_resume
) {
1520 int ret
= hid
->driver
->reset_resume(hid
);
1527 #endif /* CONFIG_PM */
1529 static const struct usb_device_id hid_usb_ids
[] = {
1530 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
1531 .bInterfaceClass
= USB_INTERFACE_CLASS_HID
},
1532 { } /* Terminating entry */
1535 MODULE_DEVICE_TABLE (usb
, hid_usb_ids
);
1537 static struct usb_driver hid_driver
= {
1539 .probe
= usbhid_probe
,
1540 .disconnect
= usbhid_disconnect
,
1542 .suspend
= hid_suspend
,
1543 .resume
= hid_resume
,
1544 .reset_resume
= hid_reset_resume
,
1546 .pre_reset
= hid_pre_reset
,
1547 .post_reset
= hid_post_reset
,
1548 .id_table
= hid_usb_ids
,
1549 .supports_autosuspend
= 1,
1552 struct usb_interface
*usbhid_find_interface(int minor
)
1554 return usb_find_interface(&hid_driver
, minor
);
1557 static int __init
hid_init(void)
1559 int retval
= -ENOMEM
;
1561 retval
= usbhid_quirks_init(quirks_param
);
1563 goto usbhid_quirks_init_fail
;
1564 retval
= usb_register(&hid_driver
);
1566 goto usb_register_fail
;
1567 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_DESC
"\n");
1571 usbhid_quirks_exit();
1572 usbhid_quirks_init_fail
:
1576 static void __exit
hid_exit(void)
1578 usb_deregister(&hid_driver
);
1579 usbhid_quirks_exit();
1582 module_init(hid_init
);
1583 module_exit(hid_exit
);
1585 MODULE_AUTHOR("Andreas Gal");
1586 MODULE_AUTHOR("Vojtech Pavlik");
1587 MODULE_AUTHOR("Jiri Kosina");
1588 MODULE_DESCRIPTION(DRIVER_DESC
);
1589 MODULE_LICENSE(DRIVER_LICENSE
);