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) 2006-2008 Jiri Kosina
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)
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
23 #include <linux/mutex.h>
24 #include <linux/smp_lock.h>
25 #include <linux/spinlock.h>
26 #include <asm/unaligned.h>
27 #include <asm/byteorder.h>
28 #include <linux/input.h>
29 #include <linux/wait.h>
31 #include <linux/usb.h>
33 #include <linux/hid.h>
34 #include <linux/hiddev.h>
35 #include <linux/hid-debug.h>
36 #include <linux/hidraw.h>
43 #define DRIVER_VERSION "v2.6"
44 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
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 /* Quirks specified at module load time */
57 static char *quirks_param
[MAX_USBHID_BOOT_QUIRKS
] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS
- 1) ] = NULL
};
58 module_param_array_named(quirks
, quirks_param
, charp
, NULL
, 0444);
59 MODULE_PARM_DESC(quirks
, "Add/modify USB HID quirks by specifying "
60 " quirks=vendorID:productID:quirks"
61 " where vendorID, productID, and quirks are all in"
64 * Input submission and I/O error handler.
67 static void hid_io_error(struct hid_device
*hid
);
69 /* Start up the input URB */
70 static int hid_start_in(struct hid_device
*hid
)
74 struct usbhid_device
*usbhid
= hid
->driver_data
;
76 spin_lock_irqsave(&usbhid
->inlock
, flags
);
77 if (hid
->open
> 0 && !test_bit(HID_SUSPENDED
, &usbhid
->iofl
) &&
78 !test_bit(HID_DISCONNECTED
, &usbhid
->iofl
) &&
79 !test_and_set_bit(HID_IN_RUNNING
, &usbhid
->iofl
)) {
80 rc
= usb_submit_urb(usbhid
->urbin
, GFP_ATOMIC
);
82 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
84 spin_unlock_irqrestore(&usbhid
->inlock
, flags
);
88 /* I/O retry timer routine */
89 static void hid_retry_timeout(unsigned long _hid
)
91 struct hid_device
*hid
= (struct hid_device
*) _hid
;
92 struct usbhid_device
*usbhid
= hid
->driver_data
;
94 dev_dbg(&usbhid
->intf
->dev
, "retrying intr urb\n");
95 if (hid_start_in(hid
))
99 /* Workqueue routine to reset the device or clear a halt */
100 static void hid_reset(struct work_struct
*work
)
102 struct usbhid_device
*usbhid
=
103 container_of(work
, struct usbhid_device
, reset_work
);
104 struct hid_device
*hid
= usbhid
->hid
;
107 if (test_bit(HID_CLEAR_HALT
, &usbhid
->iofl
)) {
108 dev_dbg(&usbhid
->intf
->dev
, "clear halt\n");
109 rc
= usb_clear_halt(hid_to_usb_dev(hid
), usbhid
->urbin
->pipe
);
110 clear_bit(HID_CLEAR_HALT
, &usbhid
->iofl
);
114 else if (test_bit(HID_RESET_PENDING
, &usbhid
->iofl
)) {
115 dev_dbg(&usbhid
->intf
->dev
, "resetting device\n");
116 rc
= rc_lock
= usb_lock_device_for_reset(hid_to_usb_dev(hid
), usbhid
->intf
);
118 rc
= usb_reset_device(hid_to_usb_dev(hid
));
120 usb_unlock_device(hid_to_usb_dev(hid
));
122 clear_bit(HID_RESET_PENDING
, &usbhid
->iofl
);
127 if (!test_bit(HID_IN_RUNNING
, &usbhid
->iofl
))
131 err_hid("can't reset device, %s-%s/input%d, status %d",
132 hid_to_usb_dev(hid
)->bus
->bus_name
,
133 hid_to_usb_dev(hid
)->devpath
,
143 /* Main I/O error handler */
144 static void hid_io_error(struct hid_device
*hid
)
147 struct usbhid_device
*usbhid
= hid
->driver_data
;
149 spin_lock_irqsave(&usbhid
->inlock
, flags
);
151 /* Stop when disconnected */
152 if (test_bit(HID_DISCONNECTED
, &usbhid
->iofl
))
155 /* If it has been a while since the last error, we'll assume
156 * this a brand new error and reset the retry timeout. */
157 if (time_after(jiffies
, usbhid
->stop_retry
+ HZ
/2))
158 usbhid
->retry_delay
= 0;
160 /* When an error occurs, retry at increasing intervals */
161 if (usbhid
->retry_delay
== 0) {
162 usbhid
->retry_delay
= 13; /* Then 26, 52, 104, 104, ... */
163 usbhid
->stop_retry
= jiffies
+ msecs_to_jiffies(1000);
164 } else if (usbhid
->retry_delay
< 100)
165 usbhid
->retry_delay
*= 2;
167 if (time_after(jiffies
, usbhid
->stop_retry
)) {
169 /* Retries failed, so do a port reset */
170 if (!test_and_set_bit(HID_RESET_PENDING
, &usbhid
->iofl
)) {
171 schedule_work(&usbhid
->reset_work
);
176 mod_timer(&usbhid
->io_retry
,
177 jiffies
+ msecs_to_jiffies(usbhid
->retry_delay
));
179 spin_unlock_irqrestore(&usbhid
->inlock
, flags
);
183 * Input interrupt completion handler.
186 static void hid_irq_in(struct urb
*urb
)
188 struct hid_device
*hid
= urb
->context
;
189 struct usbhid_device
*usbhid
= hid
->driver_data
;
192 switch (urb
->status
) {
193 case 0: /* success */
194 usbhid
->retry_delay
= 0;
195 hid_input_report(urb
->context
, HID_INPUT_REPORT
,
196 urb
->transfer_buffer
,
197 urb
->actual_length
, 1);
199 case -EPIPE
: /* stall */
200 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
201 set_bit(HID_CLEAR_HALT
, &usbhid
->iofl
);
202 schedule_work(&usbhid
->reset_work
);
204 case -ECONNRESET
: /* unlink */
206 case -ESHUTDOWN
: /* unplug */
207 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
209 case -EILSEQ
: /* protocol error or unplug */
210 case -EPROTO
: /* protocol error or unplug */
211 case -ETIME
: /* protocol error or unplug */
212 case -ETIMEDOUT
: /* Should never happen, but... */
213 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
217 dev_warn(&urb
->dev
->dev
, "input irq status %d "
218 "received\n", urb
->status
);
221 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
223 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
224 if (status
!= -EPERM
) {
225 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
226 hid_to_usb_dev(hid
)->bus
->bus_name
,
227 hid_to_usb_dev(hid
)->devpath
,
228 usbhid
->ifnum
, status
);
234 static int hid_submit_out(struct hid_device
*hid
)
236 struct hid_report
*report
;
238 struct usbhid_device
*usbhid
= hid
->driver_data
;
240 report
= usbhid
->out
[usbhid
->outtail
].report
;
241 raw_report
= usbhid
->out
[usbhid
->outtail
].raw_report
;
243 usbhid
->urbout
->transfer_buffer_length
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
244 usbhid
->urbout
->dev
= hid_to_usb_dev(hid
);
245 memcpy(usbhid
->outbuf
, raw_report
, usbhid
->urbout
->transfer_buffer_length
);
248 dbg_hid("submitting out urb\n");
250 if (usb_submit_urb(usbhid
->urbout
, GFP_ATOMIC
)) {
251 err_hid("usb_submit_urb(out) failed");
258 static int hid_submit_ctrl(struct hid_device
*hid
)
260 struct hid_report
*report
;
264 struct usbhid_device
*usbhid
= hid
->driver_data
;
266 report
= usbhid
->ctrl
[usbhid
->ctrltail
].report
;
267 raw_report
= usbhid
->ctrl
[usbhid
->ctrltail
].raw_report
;
268 dir
= usbhid
->ctrl
[usbhid
->ctrltail
].dir
;
270 len
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
271 if (dir
== USB_DIR_OUT
) {
272 usbhid
->urbctrl
->pipe
= usb_sndctrlpipe(hid_to_usb_dev(hid
), 0);
273 usbhid
->urbctrl
->transfer_buffer_length
= len
;
274 memcpy(usbhid
->ctrlbuf
, raw_report
, len
);
277 int maxpacket
, padlen
;
279 usbhid
->urbctrl
->pipe
= usb_rcvctrlpipe(hid_to_usb_dev(hid
), 0);
280 maxpacket
= usb_maxpacket(hid_to_usb_dev(hid
), usbhid
->urbctrl
->pipe
, 0);
282 padlen
= DIV_ROUND_UP(len
, maxpacket
);
284 if (padlen
> usbhid
->bufsize
)
285 padlen
= usbhid
->bufsize
;
288 usbhid
->urbctrl
->transfer_buffer_length
= padlen
;
290 usbhid
->urbctrl
->dev
= hid_to_usb_dev(hid
);
292 usbhid
->cr
->bRequestType
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| dir
;
293 usbhid
->cr
->bRequest
= (dir
== USB_DIR_OUT
) ? HID_REQ_SET_REPORT
: HID_REQ_GET_REPORT
;
294 usbhid
->cr
->wValue
= cpu_to_le16(((report
->type
+ 1) << 8) | report
->id
);
295 usbhid
->cr
->wIndex
= cpu_to_le16(usbhid
->ifnum
);
296 usbhid
->cr
->wLength
= cpu_to_le16(len
);
298 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
299 usbhid
->cr
->bRequest
== HID_REQ_SET_REPORT
? "Set_Report" : "Get_Report",
300 usbhid
->cr
->wValue
, usbhid
->cr
->wIndex
, usbhid
->cr
->wLength
);
302 if (usb_submit_urb(usbhid
->urbctrl
, GFP_ATOMIC
)) {
303 err_hid("usb_submit_urb(ctrl) failed");
311 * Output interrupt completion handler.
314 static void hid_irq_out(struct urb
*urb
)
316 struct hid_device
*hid
= urb
->context
;
317 struct usbhid_device
*usbhid
= hid
->driver_data
;
321 switch (urb
->status
) {
322 case 0: /* success */
324 case -ESHUTDOWN
: /* unplug */
326 case -EILSEQ
: /* protocol error or unplug */
327 case -EPROTO
: /* protocol error or unplug */
328 case -ECONNRESET
: /* unlink */
332 dev_warn(&urb
->dev
->dev
, "output irq status %d "
333 "received\n", urb
->status
);
336 spin_lock_irqsave(&usbhid
->outlock
, flags
);
339 usbhid
->outtail
= usbhid
->outhead
;
341 usbhid
->outtail
= (usbhid
->outtail
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1);
343 if (usbhid
->outhead
!= usbhid
->outtail
) {
344 if (hid_submit_out(hid
)) {
345 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
346 wake_up(&usbhid
->wait
);
348 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
352 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
353 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
354 wake_up(&usbhid
->wait
);
358 * Control pipe completion handler.
361 static void hid_ctrl(struct urb
*urb
)
363 struct hid_device
*hid
= urb
->context
;
364 struct usbhid_device
*usbhid
= hid
->driver_data
;
368 spin_lock_irqsave(&usbhid
->ctrllock
, flags
);
370 switch (urb
->status
) {
371 case 0: /* success */
372 if (usbhid
->ctrl
[usbhid
->ctrltail
].dir
== USB_DIR_IN
)
373 hid_input_report(urb
->context
,
374 usbhid
->ctrl
[usbhid
->ctrltail
].report
->type
,
375 urb
->transfer_buffer
, urb
->actual_length
, 0);
377 case -ESHUTDOWN
: /* unplug */
379 case -EILSEQ
: /* protocol error or unplug */
380 case -EPROTO
: /* protocol error or unplug */
381 case -ECONNRESET
: /* unlink */
383 case -EPIPE
: /* report not available */
386 dev_warn(&urb
->dev
->dev
, "ctrl urb status %d "
387 "received\n", urb
->status
);
391 usbhid
->ctrltail
= usbhid
->ctrlhead
;
393 usbhid
->ctrltail
= (usbhid
->ctrltail
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1);
395 if (usbhid
->ctrlhead
!= usbhid
->ctrltail
) {
396 if (hid_submit_ctrl(hid
)) {
397 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
398 wake_up(&usbhid
->wait
);
400 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
404 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
405 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
406 wake_up(&usbhid
->wait
);
409 void usbhid_submit_report(struct hid_device
*hid
, struct hid_report
*report
, unsigned char dir
)
413 struct usbhid_device
*usbhid
= hid
->driver_data
;
414 int len
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
416 if ((hid
->quirks
& HID_QUIRK_NOGET
) && dir
== USB_DIR_IN
)
419 if (usbhid
->urbout
&& dir
== USB_DIR_OUT
&& report
->type
== HID_OUTPUT_REPORT
) {
421 spin_lock_irqsave(&usbhid
->outlock
, flags
);
423 if ((head
= (usbhid
->outhead
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1)) == usbhid
->outtail
) {
424 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
425 dev_warn(&hid
->dev
, "output queue full\n");
429 usbhid
->out
[usbhid
->outhead
].raw_report
= kmalloc(len
, GFP_ATOMIC
);
430 if (!usbhid
->out
[usbhid
->outhead
].raw_report
) {
431 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
432 dev_warn(&hid
->dev
, "output queueing failed\n");
435 hid_output_report(report
, usbhid
->out
[usbhid
->outhead
].raw_report
);
436 usbhid
->out
[usbhid
->outhead
].report
= report
;
437 usbhid
->outhead
= head
;
439 if (!test_and_set_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
440 if (hid_submit_out(hid
))
441 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
443 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
447 spin_lock_irqsave(&usbhid
->ctrllock
, flags
);
449 if ((head
= (usbhid
->ctrlhead
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1)) == usbhid
->ctrltail
) {
450 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
451 dev_warn(&hid
->dev
, "control queue full\n");
455 if (dir
== USB_DIR_OUT
) {
456 usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
= kmalloc(len
, GFP_ATOMIC
);
457 if (!usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
) {
458 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
459 dev_warn(&hid
->dev
, "control queueing failed\n");
462 hid_output_report(report
, usbhid
->ctrl
[usbhid
->ctrlhead
].raw_report
);
464 usbhid
->ctrl
[usbhid
->ctrlhead
].report
= report
;
465 usbhid
->ctrl
[usbhid
->ctrlhead
].dir
= dir
;
466 usbhid
->ctrlhead
= head
;
468 if (!test_and_set_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
469 if (hid_submit_ctrl(hid
))
470 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
472 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
474 EXPORT_SYMBOL_GPL(usbhid_submit_report
);
476 static int usb_hidinput_input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
478 struct hid_device
*hid
= input_get_drvdata(dev
);
479 struct hid_field
*field
;
483 return input_ff_event(dev
, type
, code
, value
);
488 if ((offset
= hidinput_find_field(hid
, type
, code
, &field
)) == -1) {
489 dev_warn(&dev
->dev
, "event field not found\n");
493 hid_set_field(field
, offset
, value
);
494 usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
499 int usbhid_wait_io(struct hid_device
*hid
)
501 struct usbhid_device
*usbhid
= hid
->driver_data
;
503 if (!wait_event_timeout(usbhid
->wait
,
504 (!test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
) &&
505 !test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)),
507 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
514 static int hid_set_idle(struct usb_device
*dev
, int ifnum
, int report
, int idle
)
516 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
517 HID_REQ_SET_IDLE
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
, (idle
<< 8) | report
,
518 ifnum
, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
521 static int hid_get_class_descriptor(struct usb_device
*dev
, int ifnum
,
522 unsigned char type
, void *buf
, int size
)
524 int result
, retries
= 4;
526 memset(buf
, 0, size
);
529 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
530 USB_REQ_GET_DESCRIPTOR
, USB_RECIP_INTERFACE
| USB_DIR_IN
,
531 (type
<< 8), ifnum
, buf
, size
, USB_CTRL_GET_TIMEOUT
);
533 } while (result
< size
&& retries
);
537 int usbhid_open(struct hid_device
*hid
)
539 struct usbhid_device
*usbhid
= hid
->driver_data
;
543 res
= usb_autopm_get_interface(usbhid
->intf
);
549 if (hid_start_in(hid
))
554 void usbhid_close(struct hid_device
*hid
)
556 struct usbhid_device
*usbhid
= hid
->driver_data
;
559 usb_kill_urb(usbhid
->urbin
);
560 usb_autopm_put_interface(usbhid
->intf
);
565 * Initialize all reports
568 void usbhid_init_reports(struct hid_device
*hid
)
570 struct hid_report
*report
;
571 struct usbhid_device
*usbhid
= hid
->driver_data
;
574 list_for_each_entry(report
, &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
575 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
577 list_for_each_entry(report
, &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
578 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
581 ret
= usbhid_wait_io(hid
);
584 if (test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
585 usb_kill_urb(usbhid
->urbctrl
);
586 if (test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
587 usb_kill_urb(usbhid
->urbout
);
588 ret
= usbhid_wait_io(hid
);
592 dev_warn(&hid
->dev
, "timeout initializing reports\n");
596 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
598 static int hid_find_field_early(struct hid_device
*hid
, unsigned int page
,
599 unsigned int hid_code
, struct hid_field
**pfield
)
601 struct hid_report
*report
;
602 struct hid_field
*field
;
603 struct hid_usage
*usage
;
606 list_for_each_entry(report
, &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
, list
) {
607 for (i
= 0; i
< report
->maxfield
; i
++) {
608 field
= report
->field
[i
];
609 for (j
= 0; j
< field
->maxusage
; j
++) {
610 usage
= &field
->usage
[j
];
611 if ((usage
->hid
& HID_USAGE_PAGE
) == page
&&
612 (usage
->hid
& 0xFFFF) == hid_code
) {
622 void usbhid_set_leds(struct hid_device
*hid
)
624 struct hid_field
*field
;
627 if ((offset
= hid_find_field_early(hid
, HID_UP_LED
, 0x01, &field
)) != -1) {
628 hid_set_field(field
, offset
, 0);
629 usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
632 EXPORT_SYMBOL_GPL(usbhid_set_leds
);
635 * Traverse the supplied list of reports and find the longest
637 static void hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
640 struct hid_report
*report
;
643 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
644 size
= ((report
->size
- 1) >> 3) + 1 + hid
->report_enum
[type
].numbered
;
650 static int hid_alloc_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
652 struct usbhid_device
*usbhid
= hid
->driver_data
;
654 if (!(usbhid
->inbuf
= usb_buffer_alloc(dev
, usbhid
->bufsize
, GFP_ATOMIC
, &usbhid
->inbuf_dma
)))
656 if (!(usbhid
->outbuf
= usb_buffer_alloc(dev
, usbhid
->bufsize
, GFP_ATOMIC
, &usbhid
->outbuf_dma
)))
658 if (!(usbhid
->cr
= usb_buffer_alloc(dev
, sizeof(*(usbhid
->cr
)), GFP_ATOMIC
, &usbhid
->cr_dma
)))
660 if (!(usbhid
->ctrlbuf
= usb_buffer_alloc(dev
, usbhid
->bufsize
, GFP_ATOMIC
, &usbhid
->ctrlbuf_dma
)))
666 static int usbhid_output_raw_report(struct hid_device
*hid
, __u8
*buf
, size_t count
)
668 struct usbhid_device
*usbhid
= hid
->driver_data
;
669 struct usb_device
*dev
= hid_to_usb_dev(hid
);
670 struct usb_interface
*intf
= usbhid
->intf
;
671 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
674 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
676 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
677 ((HID_OUTPUT_REPORT
+ 1) << 8) | *buf
,
678 interface
->desc
.bInterfaceNumber
, buf
+ 1, count
- 1,
679 USB_CTRL_SET_TIMEOUT
);
681 /* count also the report id */
688 static void hid_free_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
690 struct usbhid_device
*usbhid
= hid
->driver_data
;
692 usb_buffer_free(dev
, usbhid
->bufsize
, usbhid
->inbuf
, usbhid
->inbuf_dma
);
693 usb_buffer_free(dev
, usbhid
->bufsize
, usbhid
->outbuf
, usbhid
->outbuf_dma
);
694 usb_buffer_free(dev
, sizeof(*(usbhid
->cr
)), usbhid
->cr
, usbhid
->cr_dma
);
695 usb_buffer_free(dev
, usbhid
->bufsize
, usbhid
->ctrlbuf
, usbhid
->ctrlbuf_dma
);
698 static int usbhid_parse(struct hid_device
*hid
)
700 struct usb_interface
*intf
= to_usb_interface(hid
->dev
.parent
);
701 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
702 struct usb_device
*dev
= interface_to_usbdev (intf
);
703 struct hid_descriptor
*hdesc
;
705 unsigned int rsize
= 0;
709 quirks
= usbhid_lookup_quirk(le16_to_cpu(dev
->descriptor
.idVendor
),
710 le16_to_cpu(dev
->descriptor
.idProduct
));
712 /* Many keyboards and mice don't like to be polled for reports,
713 * so we will always set the HID_QUIRK_NOGET flag for them. */
714 if (interface
->desc
.bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
) {
715 if (interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_KEYBOARD
||
716 interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_MOUSE
)
717 quirks
|= HID_QUIRK_NOGET
;
720 if (usb_get_extra_descriptor(interface
, HID_DT_HID
, &hdesc
) &&
721 (!interface
->desc
.bNumEndpoints
||
722 usb_get_extra_descriptor(&interface
->endpoint
[0], HID_DT_HID
, &hdesc
))) {
723 dbg_hid("class descriptor not present\n");
727 hid
->version
= le16_to_cpu(hdesc
->bcdHID
);
728 hid
->country
= hdesc
->bCountryCode
;
730 for (n
= 0; n
< hdesc
->bNumDescriptors
; n
++)
731 if (hdesc
->desc
[n
].bDescriptorType
== HID_DT_REPORT
)
732 rsize
= le16_to_cpu(hdesc
->desc
[n
].wDescriptorLength
);
734 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
735 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
739 if (!(rdesc
= kmalloc(rsize
, GFP_KERNEL
))) {
740 dbg_hid("couldn't allocate rdesc memory\n");
744 hid_set_idle(dev
, interface
->desc
.bInterfaceNumber
, 0, 0);
746 ret
= hid_get_class_descriptor(dev
, interface
->desc
.bInterfaceNumber
,
747 HID_DT_REPORT
, rdesc
, rsize
);
749 dbg_hid("reading report descriptor failed\n");
754 dbg_hid("report descriptor (size %u, read %d) = ", rsize
, n
);
755 for (n
= 0; n
< rsize
; n
++)
756 dbg_hid_line(" %02x", (unsigned char) rdesc
[n
]);
759 ret
= hid_parse_report(hid
, rdesc
, rsize
);
762 dbg_hid("parsing report descriptor failed\n");
766 hid
->quirks
= quirks
;
773 static int usbhid_start(struct hid_device
*hid
)
775 struct usb_interface
*intf
= to_usb_interface(hid
->dev
.parent
);
776 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
777 struct usb_device
*dev
= interface_to_usbdev(intf
);
778 struct usbhid_device
*usbhid
= hid
->driver_data
;
779 unsigned int n
, insize
= 0;
782 clear_bit(HID_DISCONNECTED
, &usbhid
->iofl
);
784 usbhid
->bufsize
= HID_MIN_BUFFER_SIZE
;
785 hid_find_max_report(hid
, HID_INPUT_REPORT
, &usbhid
->bufsize
);
786 hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &usbhid
->bufsize
);
787 hid_find_max_report(hid
, HID_FEATURE_REPORT
, &usbhid
->bufsize
);
789 if (usbhid
->bufsize
> HID_MAX_BUFFER_SIZE
)
790 usbhid
->bufsize
= HID_MAX_BUFFER_SIZE
;
792 hid_find_max_report(hid
, HID_INPUT_REPORT
, &insize
);
794 if (insize
> HID_MAX_BUFFER_SIZE
)
795 insize
= HID_MAX_BUFFER_SIZE
;
797 if (hid_alloc_buffers(dev
, hid
)) {
802 for (n
= 0; n
< interface
->desc
.bNumEndpoints
; n
++) {
803 struct usb_endpoint_descriptor
*endpoint
;
807 endpoint
= &interface
->endpoint
[n
].desc
;
808 if ((endpoint
->bmAttributes
& 3) != 3) /* Not an interrupt endpoint */
811 interval
= endpoint
->bInterval
;
813 /* Some vendors give fullspeed interval on highspeed devides */
814 if (hid
->quirks
& HID_QUIRK_FULLSPEED_INTERVAL
&&
815 dev
->speed
== USB_SPEED_HIGH
) {
816 interval
= fls(endpoint
->bInterval
*8);
817 printk(KERN_INFO
"%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
818 hid
->name
, endpoint
->bInterval
, interval
);
821 /* Change the polling interval of mice. */
822 if (hid
->collection
->usage
== HID_GD_MOUSE
&& hid_mousepoll_interval
> 0)
823 interval
= hid_mousepoll_interval
;
826 if (usb_endpoint_dir_in(endpoint
)) {
829 if (!(usbhid
->urbin
= usb_alloc_urb(0, GFP_KERNEL
)))
831 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
832 usb_fill_int_urb(usbhid
->urbin
, dev
, pipe
, usbhid
->inbuf
, insize
,
833 hid_irq_in
, hid
, interval
);
834 usbhid
->urbin
->transfer_dma
= usbhid
->inbuf_dma
;
835 usbhid
->urbin
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
839 if (!(usbhid
->urbout
= usb_alloc_urb(0, GFP_KERNEL
)))
841 pipe
= usb_sndintpipe(dev
, endpoint
->bEndpointAddress
);
842 usb_fill_int_urb(usbhid
->urbout
, dev
, pipe
, usbhid
->outbuf
, 0,
843 hid_irq_out
, hid
, interval
);
844 usbhid
->urbout
->transfer_dma
= usbhid
->outbuf_dma
;
845 usbhid
->urbout
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
849 init_waitqueue_head(&usbhid
->wait
);
850 INIT_WORK(&usbhid
->reset_work
, hid_reset
);
851 setup_timer(&usbhid
->io_retry
, hid_retry_timeout
, (unsigned long) hid
);
853 spin_lock_init(&usbhid
->inlock
);
854 spin_lock_init(&usbhid
->outlock
);
855 spin_lock_init(&usbhid
->ctrllock
);
858 usbhid
->ifnum
= interface
->desc
.bInterfaceNumber
;
860 usbhid
->urbctrl
= usb_alloc_urb(0, GFP_KERNEL
);
861 if (!usbhid
->urbctrl
) {
866 usb_fill_control_urb(usbhid
->urbctrl
, dev
, 0, (void *) usbhid
->cr
,
867 usbhid
->ctrlbuf
, 1, hid_ctrl
, hid
);
868 usbhid
->urbctrl
->setup_dma
= usbhid
->cr_dma
;
869 usbhid
->urbctrl
->transfer_dma
= usbhid
->ctrlbuf_dma
;
870 usbhid
->urbctrl
->transfer_flags
|= (URB_NO_TRANSFER_DMA_MAP
| URB_NO_SETUP_DMA_MAP
);
872 usbhid_init_reports(hid
);
873 hid_dump_device(hid
);
875 set_bit(HID_STARTED
, &usbhid
->iofl
);
877 /* Some keyboards don't work until their LEDs have been set.
878 * Since BIOSes do set the LEDs, it must be safe for any device
879 * that supports the keyboard boot protocol.
881 if (interface
->desc
.bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
&&
882 interface
->desc
.bInterfaceProtocol
==
883 USB_INTERFACE_PROTOCOL_KEYBOARD
)
884 usbhid_set_leds(hid
);
889 usb_free_urb(usbhid
->urbin
);
890 usb_free_urb(usbhid
->urbout
);
891 usb_free_urb(usbhid
->urbctrl
);
892 usbhid
->urbin
= NULL
;
893 usbhid
->urbout
= NULL
;
894 usbhid
->urbctrl
= NULL
;
895 hid_free_buffers(dev
, hid
);
899 static void usbhid_stop(struct hid_device
*hid
)
901 struct usbhid_device
*usbhid
= hid
->driver_data
;
903 if (WARN_ON(!usbhid
))
906 clear_bit(HID_STARTED
, &usbhid
->iofl
);
907 spin_lock_irq(&usbhid
->inlock
); /* Sync with error handler */
908 set_bit(HID_DISCONNECTED
, &usbhid
->iofl
);
909 spin_unlock_irq(&usbhid
->inlock
);
910 usb_kill_urb(usbhid
->urbin
);
911 usb_kill_urb(usbhid
->urbout
);
912 usb_kill_urb(usbhid
->urbctrl
);
914 del_timer_sync(&usbhid
->io_retry
);
915 cancel_work_sync(&usbhid
->reset_work
);
917 if (hid
->claimed
& HID_CLAIMED_INPUT
)
918 hidinput_disconnect(hid
);
919 if (hid
->claimed
& HID_CLAIMED_HIDDEV
)
920 hiddev_disconnect(hid
);
921 if (hid
->claimed
& HID_CLAIMED_HIDRAW
)
922 hidraw_disconnect(hid
);
926 usb_free_urb(usbhid
->urbin
);
927 usb_free_urb(usbhid
->urbctrl
);
928 usb_free_urb(usbhid
->urbout
);
929 usbhid
->urbin
= NULL
; /* don't mess up next start */
930 usbhid
->urbctrl
= NULL
;
931 usbhid
->urbout
= NULL
;
933 hid_free_buffers(hid_to_usb_dev(hid
), hid
);
936 static struct hid_ll_driver usb_hid_driver
= {
937 .parse
= usbhid_parse
,
938 .start
= usbhid_start
,
941 .close
= usbhid_close
,
942 .hidinput_input_event
= usb_hidinput_input_event
,
945 static int hid_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
947 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
948 struct usb_device
*dev
= interface_to_usbdev(intf
);
949 struct usbhid_device
*usbhid
;
950 struct hid_device
*hid
;
951 unsigned int n
, has_in
= 0;
955 dbg_hid("HID probe called for ifnum %d\n",
956 intf
->altsetting
->desc
.bInterfaceNumber
);
958 for (n
= 0; n
< interface
->desc
.bNumEndpoints
; n
++)
959 if (usb_endpoint_is_int_in(&interface
->endpoint
[n
].desc
))
962 dev_err(&intf
->dev
, "couldn't find an input interrupt "
967 hid
= hid_allocate_device();
971 usb_set_intfdata(intf
, hid
);
972 hid
->ll_driver
= &usb_hid_driver
;
973 hid
->hid_output_raw_report
= usbhid_output_raw_report
;
974 hid
->ff_init
= hid_pidff_init
;
975 #ifdef CONFIG_USB_HIDDEV
976 hid
->hiddev_connect
= hiddev_connect
;
977 hid
->hiddev_hid_event
= hiddev_hid_event
;
978 hid
->hiddev_report_event
= hiddev_report_event
;
980 hid
->dev
.parent
= &intf
->dev
;
982 hid
->vendor
= le16_to_cpu(dev
->descriptor
.idVendor
);
983 hid
->product
= le16_to_cpu(dev
->descriptor
.idProduct
);
985 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
==
986 USB_INTERFACE_PROTOCOL_MOUSE
)
987 hid
->type
= HID_TYPE_USBMOUSE
;
989 if (dev
->manufacturer
)
990 strlcpy(hid
->name
, dev
->manufacturer
, sizeof(hid
->name
));
993 if (dev
->manufacturer
)
994 strlcat(hid
->name
, " ", sizeof(hid
->name
));
995 strlcat(hid
->name
, dev
->product
, sizeof(hid
->name
));
998 if (!strlen(hid
->name
))
999 snprintf(hid
->name
, sizeof(hid
->name
), "HID %04x:%04x",
1000 le16_to_cpu(dev
->descriptor
.idVendor
),
1001 le16_to_cpu(dev
->descriptor
.idProduct
));
1003 usb_make_path(dev
, hid
->phys
, sizeof(hid
->phys
));
1004 strlcat(hid
->phys
, "/input", sizeof(hid
->phys
));
1005 len
= strlen(hid
->phys
);
1006 if (len
< sizeof(hid
->phys
) - 1)
1007 snprintf(hid
->phys
+ len
, sizeof(hid
->phys
) - len
,
1008 "%d", intf
->altsetting
[0].desc
.bInterfaceNumber
);
1010 if (usb_string(dev
, dev
->descriptor
.iSerialNumber
, hid
->uniq
, 64) <= 0)
1013 usbhid
= kzalloc(sizeof(*usbhid
), GFP_KERNEL
);
1014 if (usbhid
== NULL
) {
1019 hid
->driver_data
= usbhid
;
1022 ret
= hid_add_device(hid
);
1025 dev_err(&intf
->dev
, "can't add hid device: %d\n", ret
);
1033 hid_destroy_device(hid
);
1037 static void hid_disconnect(struct usb_interface
*intf
)
1039 struct hid_device
*hid
= usb_get_intfdata(intf
);
1040 struct usbhid_device
*usbhid
;
1045 usbhid
= hid
->driver_data
;
1046 hid_destroy_device(hid
);
1050 static int hid_suspend(struct usb_interface
*intf
, pm_message_t message
)
1052 struct hid_device
*hid
= usb_get_intfdata (intf
);
1053 struct usbhid_device
*usbhid
= hid
->driver_data
;
1055 if (!test_bit(HID_STARTED
, &usbhid
->iofl
))
1058 spin_lock_irq(&usbhid
->inlock
); /* Sync with error handler */
1059 set_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1060 spin_unlock_irq(&usbhid
->inlock
);
1061 del_timer_sync(&usbhid
->io_retry
);
1062 usb_kill_urb(usbhid
->urbin
);
1063 dev_dbg(&intf
->dev
, "suspend\n");
1067 static int hid_resume(struct usb_interface
*intf
)
1069 struct hid_device
*hid
= usb_get_intfdata (intf
);
1070 struct usbhid_device
*usbhid
= hid
->driver_data
;
1073 if (!test_bit(HID_STARTED
, &usbhid
->iofl
))
1076 clear_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1077 usbhid
->retry_delay
= 0;
1078 status
= hid_start_in(hid
);
1079 dev_dbg(&intf
->dev
, "resume status %d\n", status
);
1083 /* Treat USB reset pretty much the same as suspend/resume */
1084 static int hid_pre_reset(struct usb_interface
*intf
)
1086 /* FIXME: What if the interface is already suspended? */
1087 hid_suspend(intf
, PMSG_ON
);
1091 /* Same routine used for post_reset and reset_resume */
1092 static int hid_post_reset(struct usb_interface
*intf
)
1094 struct usb_device
*dev
= interface_to_usbdev (intf
);
1096 hid_set_idle(dev
, intf
->cur_altsetting
->desc
.bInterfaceNumber
, 0, 0);
1097 /* FIXME: Any more reinitialization needed? */
1099 return hid_resume(intf
);
1102 static struct usb_device_id hid_usb_ids
[] = {
1103 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
1104 .bInterfaceClass
= USB_INTERFACE_CLASS_HID
},
1105 { } /* Terminating entry */
1108 MODULE_DEVICE_TABLE (usb
, hid_usb_ids
);
1110 static struct usb_driver hid_driver
= {
1113 .disconnect
= hid_disconnect
,
1114 .suspend
= hid_suspend
,
1115 .resume
= hid_resume
,
1116 .reset_resume
= hid_post_reset
,
1117 .pre_reset
= hid_pre_reset
,
1118 .post_reset
= hid_post_reset
,
1119 .id_table
= hid_usb_ids
,
1120 .supports_autosuspend
= 1,
1123 static const struct hid_device_id hid_usb_table
[] = {
1124 { HID_USB_DEVICE(HID_ANY_ID
, HID_ANY_ID
) },
1128 static struct hid_driver hid_usb_driver
= {
1129 .name
= "generic-usb",
1130 .id_table
= hid_usb_table
,
1133 static int __init
hid_init(void)
1136 retval
= hid_register_driver(&hid_usb_driver
);
1138 goto hid_register_fail
;
1139 retval
= usbhid_quirks_init(quirks_param
);
1141 goto usbhid_quirks_init_fail
;
1142 retval
= hiddev_init();
1144 goto hiddev_init_fail
;
1145 retval
= usb_register(&hid_driver
);
1147 goto usb_register_fail
;
1148 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
1155 usbhid_quirks_exit();
1156 usbhid_quirks_init_fail
:
1157 hid_unregister_driver(&hid_usb_driver
);
1162 static void __exit
hid_exit(void)
1164 usb_deregister(&hid_driver
);
1166 usbhid_quirks_exit();
1167 hid_unregister_driver(&hid_usb_driver
);
1170 module_init(hid_init
);
1171 module_exit(hid_exit
);
1173 MODULE_AUTHOR(DRIVER_AUTHOR
);
1174 MODULE_DESCRIPTION(DRIVER_DESC
);
1175 MODULE_LICENSE(DRIVER_LICENSE
);