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_SUSPENDED
, &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
);
210 if (!hid
|| test_bit(HID_RESET_PENDING
, &usbhid
->iofl
) ||
211 test_bit(HID_SUSPENDED
, &usbhid
->iofl
))
214 if ((kicked
= (usbhid
->outhead
!= usbhid
->outtail
))) {
215 hid_dbg(hid
, "Kicking head %d tail %d", usbhid
->outhead
, usbhid
->outtail
);
217 /* Try to wake up from autosuspend... */
218 r
= usb_autopm_get_interface_async(usbhid
->intf
);
223 * If still suspended, don't submit. Submission will
224 * occur if/when resume drains the queue.
226 if (test_bit(HID_SUSPENDED
, &usbhid
->iofl
)) {
227 usb_autopm_put_interface_no_suspend(usbhid
->intf
);
231 /* Asynchronously flush queue. */
232 set_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
233 if (hid_submit_out(hid
)) {
234 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
235 usb_autopm_put_interface_async(usbhid
->intf
);
237 wake_up(&usbhid
->wait
);
242 static int usbhid_restart_ctrl_queue(struct usbhid_device
*usbhid
)
244 struct hid_device
*hid
= usb_get_intfdata(usbhid
->intf
);
248 WARN_ON(hid
== NULL
);
249 if (!hid
|| test_bit(HID_RESET_PENDING
, &usbhid
->iofl
) ||
250 test_bit(HID_SUSPENDED
, &usbhid
->iofl
))
253 if ((kicked
= (usbhid
->ctrlhead
!= usbhid
->ctrltail
))) {
254 hid_dbg(hid
, "Kicking head %d tail %d", usbhid
->ctrlhead
, usbhid
->ctrltail
);
256 /* Try to wake up from autosuspend... */
257 r
= usb_autopm_get_interface_async(usbhid
->intf
);
262 * If still suspended, don't submit. Submission will
263 * occur if/when resume drains the queue.
265 if (test_bit(HID_SUSPENDED
, &usbhid
->iofl
)) {
266 usb_autopm_put_interface_no_suspend(usbhid
->intf
);
270 /* Asynchronously flush queue. */
271 set_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
272 if (hid_submit_ctrl(hid
)) {
273 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
274 usb_autopm_put_interface_async(usbhid
->intf
);
276 wake_up(&usbhid
->wait
);
282 * Input interrupt completion handler.
285 static void hid_irq_in(struct urb
*urb
)
287 struct hid_device
*hid
= urb
->context
;
288 struct usbhid_device
*usbhid
= hid
->driver_data
;
291 switch (urb
->status
) {
292 case 0: /* success */
293 usbhid_mark_busy(usbhid
);
294 usbhid
->retry_delay
= 0;
295 hid_input_report(urb
->context
, HID_INPUT_REPORT
,
296 urb
->transfer_buffer
,
297 urb
->actual_length
, 1);
299 * autosuspend refused while keys are pressed
300 * because most keyboards don't wake up when
303 if (hid_check_keys_pressed(hid
))
304 set_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
);
306 clear_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
);
308 case -EPIPE
: /* stall */
309 usbhid_mark_busy(usbhid
);
310 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
311 set_bit(HID_CLEAR_HALT
, &usbhid
->iofl
);
312 schedule_work(&usbhid
->reset_work
);
314 case -ECONNRESET
: /* unlink */
316 case -ESHUTDOWN
: /* unplug */
317 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
319 case -EILSEQ
: /* protocol error or unplug */
320 case -EPROTO
: /* protocol error or unplug */
321 case -ETIME
: /* protocol error or unplug */
322 case -ETIMEDOUT
: /* Should never happen, but... */
323 usbhid_mark_busy(usbhid
);
324 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
328 hid_warn(urb
->dev
, "input irq status %d received\n",
332 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
334 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
335 if (status
!= -EPERM
) {
336 hid_err(hid
, "can't resubmit intr, %s-%s/input%d, status %d\n",
337 hid_to_usb_dev(hid
)->bus
->bus_name
,
338 hid_to_usb_dev(hid
)->devpath
,
339 usbhid
->ifnum
, status
);
345 static int hid_submit_out(struct hid_device
*hid
)
347 struct hid_report
*report
;
349 struct usbhid_device
*usbhid
= hid
->driver_data
;
352 report
= usbhid
->out
[usbhid
->outtail
].report
;
353 raw_report
= usbhid
->out
[usbhid
->outtail
].raw_report
;
355 usbhid
->urbout
->transfer_buffer_length
= ((report
->size
- 1) >> 3) +
356 1 + (report
->id
> 0);
357 usbhid
->urbout
->dev
= hid_to_usb_dev(hid
);
359 memcpy(usbhid
->outbuf
, raw_report
,
360 usbhid
->urbout
->transfer_buffer_length
);
362 usbhid
->out
[usbhid
->outtail
].raw_report
= NULL
;
365 dbg_hid("submitting out urb\n");
367 r
= usb_submit_urb(usbhid
->urbout
, GFP_ATOMIC
);
369 hid_err(hid
, "usb_submit_urb(out) failed: %d\n", r
);
372 usbhid
->last_out
= jiffies
;
376 static int hid_submit_ctrl(struct hid_device
*hid
)
378 struct hid_report
*report
;
382 struct usbhid_device
*usbhid
= hid
->driver_data
;
384 report
= usbhid
->ctrl
[usbhid
->ctrltail
].report
;
385 raw_report
= usbhid
->ctrl
[usbhid
->ctrltail
].raw_report
;
386 dir
= usbhid
->ctrl
[usbhid
->ctrltail
].dir
;
388 len
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
389 if (dir
== USB_DIR_OUT
) {
390 usbhid
->urbctrl
->pipe
= usb_sndctrlpipe(hid_to_usb_dev(hid
), 0);
391 usbhid
->urbctrl
->transfer_buffer_length
= len
;
393 memcpy(usbhid
->ctrlbuf
, raw_report
, len
);
395 usbhid
->ctrl
[usbhid
->ctrltail
].raw_report
= NULL
;
398 int maxpacket
, padlen
;
400 usbhid
->urbctrl
->pipe
= usb_rcvctrlpipe(hid_to_usb_dev(hid
), 0);
401 maxpacket
= usb_maxpacket(hid_to_usb_dev(hid
),
402 usbhid
->urbctrl
->pipe
, 0);
404 padlen
= DIV_ROUND_UP(len
, maxpacket
);
406 if (padlen
> usbhid
->bufsize
)
407 padlen
= usbhid
->bufsize
;
410 usbhid
->urbctrl
->transfer_buffer_length
= padlen
;
412 usbhid
->urbctrl
->dev
= hid_to_usb_dev(hid
);
414 usbhid
->cr
->bRequestType
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| dir
;
415 usbhid
->cr
->bRequest
= (dir
== USB_DIR_OUT
) ? HID_REQ_SET_REPORT
:
417 usbhid
->cr
->wValue
= cpu_to_le16(((report
->type
+ 1) << 8) |
419 usbhid
->cr
->wIndex
= cpu_to_le16(usbhid
->ifnum
);
420 usbhid
->cr
->wLength
= cpu_to_le16(len
);
422 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
423 usbhid
->cr
->bRequest
== HID_REQ_SET_REPORT
? "Set_Report" :
425 usbhid
->cr
->wValue
, usbhid
->cr
->wIndex
, usbhid
->cr
->wLength
);
427 r
= usb_submit_urb(usbhid
->urbctrl
, GFP_ATOMIC
);
429 hid_err(hid
, "usb_submit_urb(ctrl) failed: %d\n", r
);
432 usbhid
->last_ctrl
= jiffies
;
437 * Output interrupt completion handler.
440 static void hid_irq_out(struct urb
*urb
)
442 struct hid_device
*hid
= urb
->context
;
443 struct usbhid_device
*usbhid
= hid
->driver_data
;
447 switch (urb
->status
) {
448 case 0: /* success */
450 case -ESHUTDOWN
: /* unplug */
452 case -EILSEQ
: /* protocol error or unplug */
453 case -EPROTO
: /* protocol error or unplug */
454 case -ECONNRESET
: /* unlink */
458 hid_warn(urb
->dev
, "output irq status %d received\n",
462 spin_lock_irqsave(&usbhid
->lock
, flags
);
465 usbhid
->outtail
= usbhid
->outhead
;
467 usbhid
->outtail
= (usbhid
->outtail
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1);
469 if (usbhid
->outhead
!= usbhid
->outtail
&&
470 hid_submit_out(hid
) == 0) {
471 /* Successfully submitted next urb in queue */
472 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
477 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
478 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
479 usb_autopm_put_interface_async(usbhid
->intf
);
480 wake_up(&usbhid
->wait
);
484 * Control pipe completion handler.
487 static void hid_ctrl(struct urb
*urb
)
489 struct hid_device
*hid
= urb
->context
;
490 struct usbhid_device
*usbhid
= hid
->driver_data
;
491 int unplug
= 0, status
= urb
->status
;
493 spin_lock(&usbhid
->lock
);
496 case 0: /* success */
497 if (usbhid
->ctrl
[usbhid
->ctrltail
].dir
== USB_DIR_IN
)
498 hid_input_report(urb
->context
,
499 usbhid
->ctrl
[usbhid
->ctrltail
].report
->type
,
500 urb
->transfer_buffer
, urb
->actual_length
, 0);
502 case -ESHUTDOWN
: /* unplug */
504 case -EILSEQ
: /* protocol error or unplug */
505 case -EPROTO
: /* protocol error or unplug */
506 case -ECONNRESET
: /* unlink */
508 case -EPIPE
: /* report not available */
511 hid_warn(urb
->dev
, "ctrl urb status %d received\n", status
);
515 usbhid
->ctrltail
= usbhid
->ctrlhead
;
517 usbhid
->ctrltail
= (usbhid
->ctrltail
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1);
519 if (usbhid
->ctrlhead
!= usbhid
->ctrltail
&&
520 hid_submit_ctrl(hid
) == 0) {
521 /* Successfully submitted next urb in queue */
522 spin_unlock(&usbhid
->lock
);
527 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
528 spin_unlock(&usbhid
->lock
);
529 usb_autopm_put_interface_async(usbhid
->intf
);
530 wake_up(&usbhid
->wait
);
533 static void __usbhid_submit_report(struct hid_device
*hid
, struct hid_report
*report
,
537 struct usbhid_device
*usbhid
= hid
->driver_data
;
538 int len
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
540 if ((hid
->quirks
& HID_QUIRK_NOGET
) && dir
== USB_DIR_IN
)
543 if (usbhid
->urbout
&& dir
== USB_DIR_OUT
&& report
->type
== HID_OUTPUT_REPORT
) {
544 if ((head
= (usbhid
->outhead
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1)) == usbhid
->outtail
) {
545 hid_warn(hid
, "output queue full\n");
549 usbhid
->out
[usbhid
->outhead
].raw_report
= kmalloc(len
, GFP_ATOMIC
);
550 if (!usbhid
->out
[usbhid
->outhead
].raw_report
) {
551 hid_warn(hid
, "output queueing failed\n");
554 hid_output_report(report
, usbhid
->out
[usbhid
->outhead
].raw_report
);
555 usbhid
->out
[usbhid
->outhead
].report
= report
;
556 usbhid
->outhead
= head
;
558 /* If the queue isn't running, restart it */
559 if (!test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)) {
560 usbhid_restart_out_queue(usbhid
);
562 /* Otherwise see if an earlier request has timed out */
563 } else if (time_after(jiffies
, usbhid
->last_out
+ HZ
* 5)) {
565 /* Prevent autosuspend following the unlink */
566 usb_autopm_get_interface_no_resume(usbhid
->intf
);
569 * Prevent resubmission in case the URB completes
570 * before we can unlink it. We don't want to cancel
571 * the wrong transfer!
573 usb_block_urb(usbhid
->urbout
);
575 /* Drop lock to avoid deadlock if the callback runs */
576 spin_unlock(&usbhid
->lock
);
578 usb_unlink_urb(usbhid
->urbout
);
579 spin_lock(&usbhid
->lock
);
580 usb_unblock_urb(usbhid
->urbout
);
582 /* Unlink might have stopped the queue */
583 if (!test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
584 usbhid_restart_out_queue(usbhid
);
586 /* Now we can allow autosuspend again */
587 usb_autopm_put_interface_async(usbhid
->intf
);
592 if ((head
= (usbhid
->ctrlhead
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1)) == usbhid
->ctrltail
) {
593 hid_warn(hid
, "control queue full\n");
597 if (dir
== USB_DIR_OUT
) {
598 usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
= kmalloc(len
, GFP_ATOMIC
);
599 if (!usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
) {
600 hid_warn(hid
, "control queueing failed\n");
603 hid_output_report(report
, usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
);
605 usbhid
->ctrl
[usbhid
->ctrlhead
].report
= report
;
606 usbhid
->ctrl
[usbhid
->ctrlhead
].dir
= dir
;
607 usbhid
->ctrlhead
= head
;
609 /* If the queue isn't running, restart it */
610 if (!test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
)) {
611 usbhid_restart_ctrl_queue(usbhid
);
613 /* Otherwise see if an earlier request has timed out */
614 } else if (time_after(jiffies
, usbhid
->last_ctrl
+ HZ
* 5)) {
616 /* Prevent autosuspend following the unlink */
617 usb_autopm_get_interface_no_resume(usbhid
->intf
);
620 * Prevent resubmission in case the URB completes
621 * before we can unlink it. We don't want to cancel
622 * the wrong transfer!
624 usb_block_urb(usbhid
->urbctrl
);
626 /* Drop lock to avoid deadlock if the callback runs */
627 spin_unlock(&usbhid
->lock
);
629 usb_unlink_urb(usbhid
->urbctrl
);
630 spin_lock(&usbhid
->lock
);
631 usb_unblock_urb(usbhid
->urbctrl
);
633 /* Unlink might have stopped the queue */
634 if (!test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
635 usbhid_restart_ctrl_queue(usbhid
);
637 /* Now we can allow autosuspend again */
638 usb_autopm_put_interface_async(usbhid
->intf
);
642 static void usbhid_submit_report(struct hid_device
*hid
, struct hid_report
*report
, unsigned char dir
)
644 struct usbhid_device
*usbhid
= hid
->driver_data
;
647 spin_lock_irqsave(&usbhid
->lock
, flags
);
648 __usbhid_submit_report(hid
, report
, dir
);
649 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
652 /* Workqueue routine to send requests to change LEDs */
653 static void hid_led(struct work_struct
*work
)
655 struct usbhid_device
*usbhid
=
656 container_of(work
, struct usbhid_device
, led_work
);
657 struct hid_device
*hid
= usbhid
->hid
;
658 struct hid_field
*field
;
661 field
= hidinput_get_led_field(hid
);
663 hid_warn(hid
, "LED event field not found\n");
667 spin_lock_irqsave(&usbhid
->lock
, flags
);
668 if (!test_bit(HID_DISCONNECTED
, &usbhid
->iofl
)) {
669 usbhid
->ledcount
= hidinput_count_leds(hid
);
670 hid_dbg(usbhid
->hid
, "New ledcount = %u\n", usbhid
->ledcount
);
671 __usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
673 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
676 static int usb_hidinput_input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
678 struct hid_device
*hid
= input_get_drvdata(dev
);
679 struct usbhid_device
*usbhid
= hid
->driver_data
;
680 struct hid_field
*field
;
685 return input_ff_event(dev
, type
, code
, value
);
690 if ((offset
= hidinput_find_field(hid
, type
, code
, &field
)) == -1) {
691 hid_warn(dev
, "event field not found\n");
695 spin_lock_irqsave(&usbhid
->lock
, flags
);
696 hid_set_field(field
, offset
, value
);
697 spin_unlock_irqrestore(&usbhid
->lock
, flags
);
700 * Defer performing requested LED action.
701 * This is more likely gather all LED changes into a single URB.
703 schedule_work(&usbhid
->led_work
);
708 static int usbhid_wait_io(struct hid_device
*hid
)
710 struct usbhid_device
*usbhid
= hid
->driver_data
;
712 if (!wait_event_timeout(usbhid
->wait
,
713 (!test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
) &&
714 !test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)),
716 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
723 static int hid_set_idle(struct usb_device
*dev
, int ifnum
, int report
, int idle
)
725 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
726 HID_REQ_SET_IDLE
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
, (idle
<< 8) | report
,
727 ifnum
, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
730 static int hid_get_class_descriptor(struct usb_device
*dev
, int ifnum
,
731 unsigned char type
, void *buf
, int size
)
733 int result
, retries
= 4;
735 memset(buf
, 0, size
);
738 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
739 USB_REQ_GET_DESCRIPTOR
, USB_RECIP_INTERFACE
| USB_DIR_IN
,
740 (type
<< 8), ifnum
, buf
, size
, USB_CTRL_GET_TIMEOUT
);
742 } while (result
< size
&& retries
);
746 int usbhid_open(struct hid_device
*hid
)
748 struct usbhid_device
*usbhid
= hid
->driver_data
;
751 mutex_lock(&hid_open_mut
);
753 res
= usb_autopm_get_interface(usbhid
->intf
);
754 /* the device must be awake to reliably request remote wakeup */
760 usbhid
->intf
->needs_remote_wakeup
= 1;
761 res
= hid_start_in(hid
);
763 if (res
!= -ENOSPC
) {
767 /* no use opening if resources are insufficient */
770 usbhid
->intf
->needs_remote_wakeup
= 0;
773 usb_autopm_put_interface(usbhid
->intf
);
776 mutex_unlock(&hid_open_mut
);
780 void usbhid_close(struct hid_device
*hid
)
782 struct usbhid_device
*usbhid
= hid
->driver_data
;
784 mutex_lock(&hid_open_mut
);
786 /* protecting hid->open to make sure we don't restart
787 * data acquistion due to a resumption we no longer
790 spin_lock_irq(&usbhid
->lock
);
792 spin_unlock_irq(&usbhid
->lock
);
793 hid_cancel_delayed_stuff(usbhid
);
794 usb_kill_urb(usbhid
->urbin
);
795 usbhid
->intf
->needs_remote_wakeup
= 0;
797 spin_unlock_irq(&usbhid
->lock
);
799 mutex_unlock(&hid_open_mut
);
803 * Initialize all reports
806 void usbhid_init_reports(struct hid_device
*hid
)
808 struct hid_report
*report
;
809 struct usbhid_device
*usbhid
= hid
->driver_data
;
812 list_for_each_entry(report
, &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
813 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
815 list_for_each_entry(report
, &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
816 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
819 ret
= usbhid_wait_io(hid
);
822 if (test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
823 usb_kill_urb(usbhid
->urbctrl
);
824 if (test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
825 usb_kill_urb(usbhid
->urbout
);
826 ret
= usbhid_wait_io(hid
);
830 hid_warn(hid
, "timeout initializing reports\n");
834 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
836 static int hid_find_field_early(struct hid_device
*hid
, unsigned int page
,
837 unsigned int hid_code
, struct hid_field
**pfield
)
839 struct hid_report
*report
;
840 struct hid_field
*field
;
841 struct hid_usage
*usage
;
844 list_for_each_entry(report
, &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
, list
) {
845 for (i
= 0; i
< report
->maxfield
; i
++) {
846 field
= report
->field
[i
];
847 for (j
= 0; j
< field
->maxusage
; j
++) {
848 usage
= &field
->usage
[j
];
849 if ((usage
->hid
& HID_USAGE_PAGE
) == page
&&
850 (usage
->hid
& 0xFFFF) == hid_code
) {
860 void usbhid_set_leds(struct hid_device
*hid
)
862 struct hid_field
*field
;
865 if ((offset
= hid_find_field_early(hid
, HID_UP_LED
, 0x01, &field
)) != -1) {
866 hid_set_field(field
, offset
, 0);
867 usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
870 EXPORT_SYMBOL_GPL(usbhid_set_leds
);
873 * Traverse the supplied list of reports and find the longest
875 static void hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
878 struct hid_report
*report
;
881 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
882 size
= ((report
->size
- 1) >> 3) + 1 + hid
->report_enum
[type
].numbered
;
888 static int hid_alloc_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
890 struct usbhid_device
*usbhid
= hid
->driver_data
;
892 usbhid
->inbuf
= usb_alloc_coherent(dev
, usbhid
->bufsize
, GFP_KERNEL
,
894 usbhid
->outbuf
= usb_alloc_coherent(dev
, usbhid
->bufsize
, GFP_KERNEL
,
895 &usbhid
->outbuf_dma
);
896 usbhid
->cr
= kmalloc(sizeof(*usbhid
->cr
), GFP_KERNEL
);
897 usbhid
->ctrlbuf
= usb_alloc_coherent(dev
, usbhid
->bufsize
, GFP_KERNEL
,
898 &usbhid
->ctrlbuf_dma
);
899 if (!usbhid
->inbuf
|| !usbhid
->outbuf
|| !usbhid
->cr
||
906 static int usbhid_get_raw_report(struct hid_device
*hid
,
907 unsigned char report_number
, __u8
*buf
, size_t count
,
908 unsigned char report_type
)
910 struct usbhid_device
*usbhid
= hid
->driver_data
;
911 struct usb_device
*dev
= hid_to_usb_dev(hid
);
912 struct usb_interface
*intf
= usbhid
->intf
;
913 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
914 int skipped_report_id
= 0;
917 /* Byte 0 is the report number. Report data starts at byte 1.*/
918 buf
[0] = report_number
;
919 if (report_number
== 0x0) {
920 /* Offset the return buffer by 1, so that the report ID
921 will remain in byte 0. */
924 skipped_report_id
= 1;
926 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
928 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
929 ((report_type
+ 1) << 8) | report_number
,
930 interface
->desc
.bInterfaceNumber
, buf
, count
,
931 USB_CTRL_SET_TIMEOUT
);
933 /* count also the report id */
934 if (ret
> 0 && skipped_report_id
)
940 static int usbhid_output_raw_report(struct hid_device
*hid
, __u8
*buf
, size_t count
,
941 unsigned char report_type
)
943 struct usbhid_device
*usbhid
= hid
->driver_data
;
944 struct usb_device
*dev
= hid_to_usb_dev(hid
);
945 struct usb_interface
*intf
= usbhid
->intf
;
946 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
949 if (usbhid
->urbout
&& report_type
!= HID_FEATURE_REPORT
) {
951 int skipped_report_id
= 0;
954 /* Don't send the Report ID */
957 skipped_report_id
= 1;
959 ret
= usb_interrupt_msg(dev
, usbhid
->urbout
->pipe
,
960 buf
, count
, &actual_length
,
961 USB_CTRL_SET_TIMEOUT
);
962 /* return the number of bytes transferred */
965 /* count also the report id */
966 if (skipped_report_id
)
970 int skipped_report_id
= 0;
971 int report_id
= buf
[0];
973 /* Don't send the Report ID */
976 skipped_report_id
= 1;
978 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
980 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
981 ((report_type
+ 1) << 8) | report_id
,
982 interface
->desc
.bInterfaceNumber
, buf
, count
,
983 USB_CTRL_SET_TIMEOUT
);
984 /* count also the report id, if this was a numbered report. */
985 if (ret
> 0 && skipped_report_id
)
992 static void usbhid_restart_queues(struct usbhid_device
*usbhid
)
994 if (usbhid
->urbout
&& !test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
995 usbhid_restart_out_queue(usbhid
);
996 if (!test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
997 usbhid_restart_ctrl_queue(usbhid
);
1000 static void hid_free_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
1002 struct usbhid_device
*usbhid
= hid
->driver_data
;
1004 usb_free_coherent(dev
, usbhid
->bufsize
, usbhid
->inbuf
, usbhid
->inbuf_dma
);
1005 usb_free_coherent(dev
, usbhid
->bufsize
, usbhid
->outbuf
, usbhid
->outbuf_dma
);
1007 usb_free_coherent(dev
, usbhid
->bufsize
, usbhid
->ctrlbuf
, usbhid
->ctrlbuf_dma
);
1010 static int usbhid_parse(struct hid_device
*hid
)
1012 struct usb_interface
*intf
= to_usb_interface(hid
->dev
.parent
);
1013 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1014 struct usb_device
*dev
= interface_to_usbdev (intf
);
1015 struct hid_descriptor
*hdesc
;
1017 unsigned int rsize
= 0;
1021 quirks
= usbhid_lookup_quirk(le16_to_cpu(dev
->descriptor
.idVendor
),
1022 le16_to_cpu(dev
->descriptor
.idProduct
));
1024 if (quirks
& HID_QUIRK_IGNORE
)
1027 /* Many keyboards and mice don't like to be polled for reports,
1028 * so we will always set the HID_QUIRK_NOGET flag for them. */
1029 if (interface
->desc
.bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
) {
1030 if (interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_KEYBOARD
||
1031 interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_MOUSE
)
1032 quirks
|= HID_QUIRK_NOGET
;
1035 if (usb_get_extra_descriptor(interface
, HID_DT_HID
, &hdesc
) &&
1036 (!interface
->desc
.bNumEndpoints
||
1037 usb_get_extra_descriptor(&interface
->endpoint
[0], HID_DT_HID
, &hdesc
))) {
1038 dbg_hid("class descriptor not present\n");
1042 hid
->version
= le16_to_cpu(hdesc
->bcdHID
);
1043 hid
->country
= hdesc
->bCountryCode
;
1045 for (n
= 0; n
< hdesc
->bNumDescriptors
; n
++)
1046 if (hdesc
->desc
[n
].bDescriptorType
== HID_DT_REPORT
)
1047 rsize
= le16_to_cpu(hdesc
->desc
[n
].wDescriptorLength
);
1049 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
1050 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
1054 if (!(rdesc
= kmalloc(rsize
, GFP_KERNEL
))) {
1055 dbg_hid("couldn't allocate rdesc memory\n");
1059 hid_set_idle(dev
, interface
->desc
.bInterfaceNumber
, 0, 0);
1061 ret
= hid_get_class_descriptor(dev
, interface
->desc
.bInterfaceNumber
,
1062 HID_DT_REPORT
, rdesc
, rsize
);
1064 dbg_hid("reading report descriptor failed\n");
1069 ret
= hid_parse_report(hid
, rdesc
, rsize
);
1072 dbg_hid("parsing report descriptor failed\n");
1076 hid
->quirks
|= quirks
;
1083 static int usbhid_start(struct hid_device
*hid
)
1085 struct usb_interface
*intf
= to_usb_interface(hid
->dev
.parent
);
1086 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1087 struct usb_device
*dev
= interface_to_usbdev(intf
);
1088 struct usbhid_device
*usbhid
= hid
->driver_data
;
1089 unsigned int n
, insize
= 0;
1092 clear_bit(HID_DISCONNECTED
, &usbhid
->iofl
);
1094 usbhid
->bufsize
= HID_MIN_BUFFER_SIZE
;
1095 hid_find_max_report(hid
, HID_INPUT_REPORT
, &usbhid
->bufsize
);
1096 hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &usbhid
->bufsize
);
1097 hid_find_max_report(hid
, HID_FEATURE_REPORT
, &usbhid
->bufsize
);
1099 if (usbhid
->bufsize
> HID_MAX_BUFFER_SIZE
)
1100 usbhid
->bufsize
= HID_MAX_BUFFER_SIZE
;
1102 hid_find_max_report(hid
, HID_INPUT_REPORT
, &insize
);
1104 if (insize
> HID_MAX_BUFFER_SIZE
)
1105 insize
= HID_MAX_BUFFER_SIZE
;
1107 if (hid_alloc_buffers(dev
, hid
)) {
1112 for (n
= 0; n
< interface
->desc
.bNumEndpoints
; n
++) {
1113 struct usb_endpoint_descriptor
*endpoint
;
1117 endpoint
= &interface
->endpoint
[n
].desc
;
1118 if (!usb_endpoint_xfer_int(endpoint
))
1121 interval
= endpoint
->bInterval
;
1123 /* Some vendors give fullspeed interval on highspeed devides */
1124 if (hid
->quirks
& HID_QUIRK_FULLSPEED_INTERVAL
&&
1125 dev
->speed
== USB_SPEED_HIGH
) {
1126 interval
= fls(endpoint
->bInterval
*8);
1127 printk(KERN_INFO
"%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1128 hid
->name
, endpoint
->bInterval
, interval
);
1131 /* Change the polling interval of mice. */
1132 if (hid
->collection
->usage
== HID_GD_MOUSE
&& hid_mousepoll_interval
> 0)
1133 interval
= hid_mousepoll_interval
;
1136 if (usb_endpoint_dir_in(endpoint
)) {
1139 if (!(usbhid
->urbin
= usb_alloc_urb(0, GFP_KERNEL
)))
1141 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
1142 usb_fill_int_urb(usbhid
->urbin
, dev
, pipe
, usbhid
->inbuf
, insize
,
1143 hid_irq_in
, hid
, interval
);
1144 usbhid
->urbin
->transfer_dma
= usbhid
->inbuf_dma
;
1145 usbhid
->urbin
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1149 if (!(usbhid
->urbout
= usb_alloc_urb(0, GFP_KERNEL
)))
1151 pipe
= usb_sndintpipe(dev
, endpoint
->bEndpointAddress
);
1152 usb_fill_int_urb(usbhid
->urbout
, dev
, pipe
, usbhid
->outbuf
, 0,
1153 hid_irq_out
, hid
, interval
);
1154 usbhid
->urbout
->transfer_dma
= usbhid
->outbuf_dma
;
1155 usbhid
->urbout
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1159 usbhid
->urbctrl
= usb_alloc_urb(0, GFP_KERNEL
);
1160 if (!usbhid
->urbctrl
) {
1165 usb_fill_control_urb(usbhid
->urbctrl
, dev
, 0, (void *) usbhid
->cr
,
1166 usbhid
->ctrlbuf
, 1, hid_ctrl
, hid
);
1167 usbhid
->urbctrl
->transfer_dma
= usbhid
->ctrlbuf_dma
;
1168 usbhid
->urbctrl
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1170 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
1171 usbhid_init_reports(hid
);
1173 set_bit(HID_STARTED
, &usbhid
->iofl
);
1175 /* Some keyboards don't work until their LEDs have been set.
1176 * Since BIOSes do set the LEDs, it must be safe for any device
1177 * that supports the keyboard boot protocol.
1178 * In addition, enable remote wakeup by default for all keyboard
1179 * devices supporting the boot protocol.
1181 if (interface
->desc
.bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
&&
1182 interface
->desc
.bInterfaceProtocol
==
1183 USB_INTERFACE_PROTOCOL_KEYBOARD
) {
1184 usbhid_set_leds(hid
);
1185 device_set_wakeup_enable(&dev
->dev
, 1);
1190 usb_free_urb(usbhid
->urbin
);
1191 usb_free_urb(usbhid
->urbout
);
1192 usb_free_urb(usbhid
->urbctrl
);
1193 usbhid
->urbin
= NULL
;
1194 usbhid
->urbout
= NULL
;
1195 usbhid
->urbctrl
= NULL
;
1196 hid_free_buffers(dev
, hid
);
1200 static void usbhid_stop(struct hid_device
*hid
)
1202 struct usbhid_device
*usbhid
= hid
->driver_data
;
1204 if (WARN_ON(!usbhid
))
1207 clear_bit(HID_STARTED
, &usbhid
->iofl
);
1208 spin_lock_irq(&usbhid
->lock
); /* Sync with error and led handlers */
1209 set_bit(HID_DISCONNECTED
, &usbhid
->iofl
);
1210 spin_unlock_irq(&usbhid
->lock
);
1211 usb_kill_urb(usbhid
->urbin
);
1212 usb_kill_urb(usbhid
->urbout
);
1213 usb_kill_urb(usbhid
->urbctrl
);
1215 hid_cancel_delayed_stuff(usbhid
);
1219 usb_free_urb(usbhid
->urbin
);
1220 usb_free_urb(usbhid
->urbctrl
);
1221 usb_free_urb(usbhid
->urbout
);
1222 usbhid
->urbin
= NULL
; /* don't mess up next start */
1223 usbhid
->urbctrl
= NULL
;
1224 usbhid
->urbout
= NULL
;
1226 hid_free_buffers(hid_to_usb_dev(hid
), hid
);
1229 static int usbhid_power(struct hid_device
*hid
, int lvl
)
1234 case PM_HINT_FULLON
:
1235 r
= usbhid_get_power(hid
);
1237 case PM_HINT_NORMAL
:
1238 usbhid_put_power(hid
);
1244 static void usbhid_request(struct hid_device
*hid
, struct hid_report
*rep
, int reqtype
)
1247 case HID_REQ_GET_REPORT
:
1248 usbhid_submit_report(hid
, rep
, USB_DIR_IN
);
1250 case HID_REQ_SET_REPORT
:
1251 usbhid_submit_report(hid
, rep
, USB_DIR_OUT
);
1256 static int usbhid_idle(struct hid_device
*hid
, int report
, int idle
,
1259 struct usb_device
*dev
= hid_to_usb_dev(hid
);
1260 struct usb_interface
*intf
= to_usb_interface(hid
->dev
.parent
);
1261 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1262 int ifnum
= interface
->desc
.bInterfaceNumber
;
1264 if (reqtype
!= HID_REQ_SET_IDLE
)
1267 return hid_set_idle(dev
, ifnum
, report
, idle
);
1270 static struct hid_ll_driver usb_hid_driver
= {
1271 .parse
= usbhid_parse
,
1272 .start
= usbhid_start
,
1273 .stop
= usbhid_stop
,
1274 .open
= usbhid_open
,
1275 .close
= usbhid_close
,
1276 .power
= usbhid_power
,
1277 .hidinput_input_event
= usb_hidinput_input_event
,
1278 .request
= usbhid_request
,
1279 .wait
= usbhid_wait_io
,
1280 .idle
= usbhid_idle
,
1283 static int usbhid_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1285 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1286 struct usb_device
*dev
= interface_to_usbdev(intf
);
1287 struct usbhid_device
*usbhid
;
1288 struct hid_device
*hid
;
1289 unsigned int n
, has_in
= 0;
1293 dbg_hid("HID probe called for ifnum %d\n",
1294 intf
->altsetting
->desc
.bInterfaceNumber
);
1296 for (n
= 0; n
< interface
->desc
.bNumEndpoints
; n
++)
1297 if (usb_endpoint_is_int_in(&interface
->endpoint
[n
].desc
))
1300 hid_err(intf
, "couldn't find an input interrupt endpoint\n");
1304 hid
= hid_allocate_device();
1306 return PTR_ERR(hid
);
1308 usb_set_intfdata(intf
, hid
);
1309 hid
->ll_driver
= &usb_hid_driver
;
1310 hid
->hid_get_raw_report
= usbhid_get_raw_report
;
1311 hid
->hid_output_raw_report
= usbhid_output_raw_report
;
1312 hid
->ff_init
= hid_pidff_init
;
1313 #ifdef CONFIG_USB_HIDDEV
1314 hid
->hiddev_connect
= hiddev_connect
;
1315 hid
->hiddev_disconnect
= hiddev_disconnect
;
1316 hid
->hiddev_hid_event
= hiddev_hid_event
;
1317 hid
->hiddev_report_event
= hiddev_report_event
;
1319 hid
->dev
.parent
= &intf
->dev
;
1321 hid
->vendor
= le16_to_cpu(dev
->descriptor
.idVendor
);
1322 hid
->product
= le16_to_cpu(dev
->descriptor
.idProduct
);
1324 hid
->quirks
= usbhid_lookup_quirk(hid
->vendor
, hid
->product
);
1325 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
==
1326 USB_INTERFACE_PROTOCOL_MOUSE
)
1327 hid
->type
= HID_TYPE_USBMOUSE
;
1328 else if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
== 0)
1329 hid
->type
= HID_TYPE_USBNONE
;
1331 if (dev
->manufacturer
)
1332 strlcpy(hid
->name
, dev
->manufacturer
, sizeof(hid
->name
));
1335 if (dev
->manufacturer
)
1336 strlcat(hid
->name
, " ", sizeof(hid
->name
));
1337 strlcat(hid
->name
, dev
->product
, sizeof(hid
->name
));
1340 if (!strlen(hid
->name
))
1341 snprintf(hid
->name
, sizeof(hid
->name
), "HID %04x:%04x",
1342 le16_to_cpu(dev
->descriptor
.idVendor
),
1343 le16_to_cpu(dev
->descriptor
.idProduct
));
1345 usb_make_path(dev
, hid
->phys
, sizeof(hid
->phys
));
1346 strlcat(hid
->phys
, "/input", sizeof(hid
->phys
));
1347 len
= strlen(hid
->phys
);
1348 if (len
< sizeof(hid
->phys
) - 1)
1349 snprintf(hid
->phys
+ len
, sizeof(hid
->phys
) - len
,
1350 "%d", intf
->altsetting
[0].desc
.bInterfaceNumber
);
1352 if (usb_string(dev
, dev
->descriptor
.iSerialNumber
, hid
->uniq
, 64) <= 0)
1355 usbhid
= kzalloc(sizeof(*usbhid
), GFP_KERNEL
);
1356 if (usbhid
== NULL
) {
1361 hid
->driver_data
= usbhid
;
1363 usbhid
->intf
= intf
;
1364 usbhid
->ifnum
= interface
->desc
.bInterfaceNumber
;
1366 init_waitqueue_head(&usbhid
->wait
);
1367 INIT_WORK(&usbhid
->reset_work
, hid_reset
);
1368 setup_timer(&usbhid
->io_retry
, hid_retry_timeout
, (unsigned long) hid
);
1369 spin_lock_init(&usbhid
->lock
);
1371 INIT_WORK(&usbhid
->led_work
, hid_led
);
1373 ret
= hid_add_device(hid
);
1376 hid_err(intf
, "can't add hid device: %d\n", ret
);
1384 hid_destroy_device(hid
);
1388 static void usbhid_disconnect(struct usb_interface
*intf
)
1390 struct hid_device
*hid
= usb_get_intfdata(intf
);
1391 struct usbhid_device
*usbhid
;
1396 usbhid
= hid
->driver_data
;
1397 hid_destroy_device(hid
);
1401 static void hid_cancel_delayed_stuff(struct usbhid_device
*usbhid
)
1403 del_timer_sync(&usbhid
->io_retry
);
1404 cancel_work_sync(&usbhid
->reset_work
);
1405 cancel_work_sync(&usbhid
->led_work
);
1408 static void hid_cease_io(struct usbhid_device
*usbhid
)
1410 del_timer_sync(&usbhid
->io_retry
);
1411 usb_kill_urb(usbhid
->urbin
);
1412 usb_kill_urb(usbhid
->urbctrl
);
1413 usb_kill_urb(usbhid
->urbout
);
1416 /* Treat USB reset pretty much the same as suspend/resume */
1417 static int hid_pre_reset(struct usb_interface
*intf
)
1419 struct hid_device
*hid
= usb_get_intfdata(intf
);
1420 struct usbhid_device
*usbhid
= hid
->driver_data
;
1422 spin_lock_irq(&usbhid
->lock
);
1423 set_bit(HID_RESET_PENDING
, &usbhid
->iofl
);
1424 spin_unlock_irq(&usbhid
->lock
);
1425 hid_cease_io(usbhid
);
1430 /* Same routine used for post_reset and reset_resume */
1431 static int hid_post_reset(struct usb_interface
*intf
)
1433 struct usb_device
*dev
= interface_to_usbdev (intf
);
1434 struct hid_device
*hid
= usb_get_intfdata(intf
);
1435 struct usbhid_device
*usbhid
= hid
->driver_data
;
1436 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
1440 /* Fetch and examine the HID report descriptor. If this
1441 * has changed, then rebind. Since usbcore's check of the
1442 * configuration descriptors passed, we already know that
1443 * the size of the HID report descriptor has not changed.
1445 rdesc
= kmalloc(hid
->dev_rsize
, GFP_KERNEL
);
1447 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1450 status
= hid_get_class_descriptor(dev
,
1451 interface
->desc
.bInterfaceNumber
,
1452 HID_DT_REPORT
, rdesc
, hid
->dev_rsize
);
1454 dbg_hid("reading report descriptor failed (post_reset)\n");
1458 status
= memcmp(rdesc
, hid
->dev_rdesc
, hid
->dev_rsize
);
1461 dbg_hid("report descriptor changed\n");
1465 spin_lock_irq(&usbhid
->lock
);
1466 clear_bit(HID_RESET_PENDING
, &usbhid
->iofl
);
1467 spin_unlock_irq(&usbhid
->lock
);
1468 hid_set_idle(dev
, intf
->cur_altsetting
->desc
.bInterfaceNumber
, 0, 0);
1469 status
= hid_start_in(hid
);
1472 usbhid_restart_queues(usbhid
);
1477 int usbhid_get_power(struct hid_device
*hid
)
1479 struct usbhid_device
*usbhid
= hid
->driver_data
;
1481 return usb_autopm_get_interface(usbhid
->intf
);
1484 void usbhid_put_power(struct hid_device
*hid
)
1486 struct usbhid_device
*usbhid
= hid
->driver_data
;
1488 usb_autopm_put_interface(usbhid
->intf
);
1493 static int hid_resume_common(struct hid_device
*hid
, bool driver_suspended
)
1495 struct usbhid_device
*usbhid
= hid
->driver_data
;
1498 spin_lock_irq(&usbhid
->lock
);
1499 clear_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1500 usbhid_mark_busy(usbhid
);
1502 if (test_bit(HID_CLEAR_HALT
, &usbhid
->iofl
) ||
1503 test_bit(HID_RESET_PENDING
, &usbhid
->iofl
))
1504 schedule_work(&usbhid
->reset_work
);
1505 usbhid
->retry_delay
= 0;
1507 usbhid_restart_queues(usbhid
);
1508 spin_unlock_irq(&usbhid
->lock
);
1510 status
= hid_start_in(hid
);
1514 if (driver_suspended
&& hid
->driver
&& hid
->driver
->resume
)
1515 status
= hid
->driver
->resume(hid
);
1519 static int hid_suspend(struct usb_interface
*intf
, pm_message_t message
)
1521 struct hid_device
*hid
= usb_get_intfdata(intf
);
1522 struct usbhid_device
*usbhid
= hid
->driver_data
;
1524 bool driver_suspended
= false;
1526 if (PMSG_IS_AUTO(message
)) {
1527 spin_lock_irq(&usbhid
->lock
); /* Sync with error handler */
1528 if (!test_bit(HID_RESET_PENDING
, &usbhid
->iofl
)
1529 && !test_bit(HID_CLEAR_HALT
, &usbhid
->iofl
)
1530 && !test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)
1531 && !test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
)
1532 && !test_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
)
1533 && (!usbhid
->ledcount
|| ignoreled
))
1535 set_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1536 spin_unlock_irq(&usbhid
->lock
);
1537 if (hid
->driver
&& hid
->driver
->suspend
) {
1538 status
= hid
->driver
->suspend(hid
, message
);
1542 driver_suspended
= true;
1544 usbhid_mark_busy(usbhid
);
1545 spin_unlock_irq(&usbhid
->lock
);
1550 /* TODO: resume() might need to handle suspend failure */
1551 if (hid
->driver
&& hid
->driver
->suspend
)
1552 status
= hid
->driver
->suspend(hid
, message
);
1553 driver_suspended
= true;
1554 spin_lock_irq(&usbhid
->lock
);
1555 set_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1556 spin_unlock_irq(&usbhid
->lock
);
1557 if (usbhid_wait_io(hid
) < 0)
1561 hid_cancel_delayed_stuff(usbhid
);
1562 hid_cease_io(usbhid
);
1564 if (PMSG_IS_AUTO(message
) && test_bit(HID_KEYS_PRESSED
, &usbhid
->iofl
)) {
1565 /* lost race against keypresses */
1569 dev_dbg(&intf
->dev
, "suspend\n");
1573 hid_resume_common(hid
, driver_suspended
);
1577 static int hid_resume(struct usb_interface
*intf
)
1579 struct hid_device
*hid
= usb_get_intfdata (intf
);
1580 struct usbhid_device
*usbhid
= hid
->driver_data
;
1583 if (!test_bit(HID_STARTED
, &usbhid
->iofl
))
1586 status
= hid_resume_common(hid
, true);
1587 dev_dbg(&intf
->dev
, "resume status %d\n", status
);
1591 static int hid_reset_resume(struct usb_interface
*intf
)
1593 struct hid_device
*hid
= usb_get_intfdata(intf
);
1594 struct usbhid_device
*usbhid
= hid
->driver_data
;
1597 clear_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1598 status
= hid_post_reset(intf
);
1599 if (status
>= 0 && hid
->driver
&& hid
->driver
->reset_resume
) {
1600 int ret
= hid
->driver
->reset_resume(hid
);
1607 #endif /* CONFIG_PM */
1609 static const struct usb_device_id hid_usb_ids
[] = {
1610 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
1611 .bInterfaceClass
= USB_INTERFACE_CLASS_HID
},
1612 { } /* Terminating entry */
1615 MODULE_DEVICE_TABLE (usb
, hid_usb_ids
);
1617 static struct usb_driver hid_driver
= {
1619 .probe
= usbhid_probe
,
1620 .disconnect
= usbhid_disconnect
,
1622 .suspend
= hid_suspend
,
1623 .resume
= hid_resume
,
1624 .reset_resume
= hid_reset_resume
,
1626 .pre_reset
= hid_pre_reset
,
1627 .post_reset
= hid_post_reset
,
1628 .id_table
= hid_usb_ids
,
1629 .supports_autosuspend
= 1,
1632 struct usb_interface
*usbhid_find_interface(int minor
)
1634 return usb_find_interface(&hid_driver
, minor
);
1637 static int __init
hid_init(void)
1639 int retval
= -ENOMEM
;
1641 retval
= usbhid_quirks_init(quirks_param
);
1643 goto usbhid_quirks_init_fail
;
1644 retval
= usb_register(&hid_driver
);
1646 goto usb_register_fail
;
1647 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_DESC
"\n");
1651 usbhid_quirks_exit();
1652 usbhid_quirks_init_fail
:
1656 static void __exit
hid_exit(void)
1658 usb_deregister(&hid_driver
);
1659 usbhid_quirks_exit();
1662 module_init(hid_init
);
1663 module_exit(hid_exit
);
1665 MODULE_AUTHOR("Andreas Gal");
1666 MODULE_AUTHOR("Vojtech Pavlik");
1667 MODULE_AUTHOR("Jiri Kosina");
1668 MODULE_DESCRIPTION(DRIVER_DESC
);
1669 MODULE_LICENSE(DRIVER_LICENSE
);