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-2007 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/smp_lock.h>
24 #include <linux/spinlock.h>
25 #include <asm/unaligned.h>
26 #include <asm/byteorder.h>
27 #include <linux/input.h>
28 #include <linux/wait.h>
30 #include <linux/usb.h>
32 #include <linux/hid.h>
33 #include <linux/hiddev.h>
34 #include <linux/hid-debug.h>
41 #define DRIVER_VERSION "v2.6"
42 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
43 #define DRIVER_DESC "USB HID core driver"
44 #define DRIVER_LICENSE "GPL"
46 static char *hid_types
[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
47 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
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_and_set_bit(HID_IN_RUNNING
, &usbhid
->iofl
)) {
79 rc
= usb_submit_urb(usbhid
->urbin
, GFP_ATOMIC
);
81 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
83 spin_unlock_irqrestore(&usbhid
->inlock
, flags
);
87 /* I/O retry timer routine */
88 static void hid_retry_timeout(unsigned long _hid
)
90 struct hid_device
*hid
= (struct hid_device
*) _hid
;
91 struct usbhid_device
*usbhid
= hid
->driver_data
;
93 dev_dbg(&usbhid
->intf
->dev
, "retrying intr urb\n");
94 if (hid_start_in(hid
))
98 /* Workqueue routine to reset the device or clear a halt */
99 static void hid_reset(struct work_struct
*work
)
101 struct usbhid_device
*usbhid
=
102 container_of(work
, struct usbhid_device
, reset_work
);
103 struct hid_device
*hid
= usbhid
->hid
;
106 if (test_bit(HID_CLEAR_HALT
, &usbhid
->iofl
)) {
107 dev_dbg(&usbhid
->intf
->dev
, "clear halt\n");
108 rc
= usb_clear_halt(hid_to_usb_dev(hid
), usbhid
->urbin
->pipe
);
109 clear_bit(HID_CLEAR_HALT
, &usbhid
->iofl
);
113 else if (test_bit(HID_RESET_PENDING
, &usbhid
->iofl
)) {
114 dev_dbg(&usbhid
->intf
->dev
, "resetting device\n");
115 rc
= rc_lock
= usb_lock_device_for_reset(hid_to_usb_dev(hid
), usbhid
->intf
);
117 rc
= usb_reset_composite_device(hid_to_usb_dev(hid
), usbhid
->intf
);
119 usb_unlock_device(hid_to_usb_dev(hid
));
121 clear_bit(HID_RESET_PENDING
, &usbhid
->iofl
);
126 if (!test_bit(HID_IN_RUNNING
, &usbhid
->iofl
))
130 err_hid("can't reset device, %s-%s/input%d, status %d",
131 hid_to_usb_dev(hid
)->bus
->bus_name
,
132 hid_to_usb_dev(hid
)->devpath
,
142 /* Main I/O error handler */
143 static void hid_io_error(struct hid_device
*hid
)
146 struct usbhid_device
*usbhid
= hid
->driver_data
;
148 spin_lock_irqsave(&usbhid
->inlock
, flags
);
150 /* Stop when disconnected */
151 if (usb_get_intfdata(usbhid
->intf
) == NULL
)
154 /* If it has been a while since the last error, we'll assume
155 * this a brand new error and reset the retry timeout. */
156 if (time_after(jiffies
, usbhid
->stop_retry
+ HZ
/2))
157 usbhid
->retry_delay
= 0;
159 /* When an error occurs, retry at increasing intervals */
160 if (usbhid
->retry_delay
== 0) {
161 usbhid
->retry_delay
= 13; /* Then 26, 52, 104, 104, ... */
162 usbhid
->stop_retry
= jiffies
+ msecs_to_jiffies(1000);
163 } else if (usbhid
->retry_delay
< 100)
164 usbhid
->retry_delay
*= 2;
166 if (time_after(jiffies
, usbhid
->stop_retry
)) {
168 /* Retries failed, so do a port reset */
169 if (!test_and_set_bit(HID_RESET_PENDING
, &usbhid
->iofl
)) {
170 schedule_work(&usbhid
->reset_work
);
175 mod_timer(&usbhid
->io_retry
,
176 jiffies
+ msecs_to_jiffies(usbhid
->retry_delay
));
178 spin_unlock_irqrestore(&usbhid
->inlock
, flags
);
182 * Input interrupt completion handler.
185 static void hid_irq_in(struct urb
*urb
)
187 struct hid_device
*hid
= urb
->context
;
188 struct usbhid_device
*usbhid
= hid
->driver_data
;
191 switch (urb
->status
) {
192 case 0: /* success */
193 usbhid
->retry_delay
= 0;
194 hid_input_report(urb
->context
, HID_INPUT_REPORT
,
195 urb
->transfer_buffer
,
196 urb
->actual_length
, 1);
198 case -EPIPE
: /* stall */
199 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
200 set_bit(HID_CLEAR_HALT
, &usbhid
->iofl
);
201 schedule_work(&usbhid
->reset_work
);
203 case -ECONNRESET
: /* unlink */
205 case -ESHUTDOWN
: /* unplug */
206 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
208 case -EILSEQ
: /* protocol error or unplug */
209 case -EPROTO
: /* protocol error or unplug */
210 case -ETIME
: /* protocol error or unplug */
211 case -ETIMEDOUT
: /* Should never happen, but... */
212 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
216 warn("input irq status %d received", urb
->status
);
219 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
221 clear_bit(HID_IN_RUNNING
, &usbhid
->iofl
);
222 if (status
!= -EPERM
) {
223 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
224 hid_to_usb_dev(hid
)->bus
->bus_name
,
225 hid_to_usb_dev(hid
)->devpath
,
226 usbhid
->ifnum
, status
);
232 static int hid_submit_out(struct hid_device
*hid
)
234 struct hid_report
*report
;
235 struct usbhid_device
*usbhid
= hid
->driver_data
;
237 report
= usbhid
->out
[usbhid
->outtail
];
239 hid_output_report(report
, usbhid
->outbuf
);
240 usbhid
->urbout
->transfer_buffer_length
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
241 usbhid
->urbout
->dev
= hid_to_usb_dev(hid
);
243 dbg_hid("submitting out urb\n");
245 if (usb_submit_urb(usbhid
->urbout
, GFP_ATOMIC
)) {
246 err_hid("usb_submit_urb(out) failed");
253 static int hid_submit_ctrl(struct hid_device
*hid
)
255 struct hid_report
*report
;
258 struct usbhid_device
*usbhid
= hid
->driver_data
;
260 report
= usbhid
->ctrl
[usbhid
->ctrltail
].report
;
261 dir
= usbhid
->ctrl
[usbhid
->ctrltail
].dir
;
263 len
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
264 if (dir
== USB_DIR_OUT
) {
265 hid_output_report(report
, usbhid
->ctrlbuf
);
266 usbhid
->urbctrl
->pipe
= usb_sndctrlpipe(hid_to_usb_dev(hid
), 0);
267 usbhid
->urbctrl
->transfer_buffer_length
= len
;
269 int maxpacket
, padlen
;
271 usbhid
->urbctrl
->pipe
= usb_rcvctrlpipe(hid_to_usb_dev(hid
), 0);
272 maxpacket
= usb_maxpacket(hid_to_usb_dev(hid
), usbhid
->urbctrl
->pipe
, 0);
274 padlen
= (len
+ maxpacket
- 1) / maxpacket
;
276 if (padlen
> usbhid
->bufsize
)
277 padlen
= usbhid
->bufsize
;
280 usbhid
->urbctrl
->transfer_buffer_length
= padlen
;
282 usbhid
->urbctrl
->dev
= hid_to_usb_dev(hid
);
284 usbhid
->cr
->bRequestType
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| dir
;
285 usbhid
->cr
->bRequest
= (dir
== USB_DIR_OUT
) ? HID_REQ_SET_REPORT
: HID_REQ_GET_REPORT
;
286 usbhid
->cr
->wValue
= cpu_to_le16(((report
->type
+ 1) << 8) | report
->id
);
287 usbhid
->cr
->wIndex
= cpu_to_le16(usbhid
->ifnum
);
288 usbhid
->cr
->wLength
= cpu_to_le16(len
);
290 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
291 usbhid
->cr
->bRequest
== HID_REQ_SET_REPORT
? "Set_Report" : "Get_Report",
292 usbhid
->cr
->wValue
, usbhid
->cr
->wIndex
, usbhid
->cr
->wLength
);
294 if (usb_submit_urb(usbhid
->urbctrl
, GFP_ATOMIC
)) {
295 err_hid("usb_submit_urb(ctrl) failed");
303 * Output interrupt completion handler.
306 static void hid_irq_out(struct urb
*urb
)
308 struct hid_device
*hid
= urb
->context
;
309 struct usbhid_device
*usbhid
= hid
->driver_data
;
313 switch (urb
->status
) {
314 case 0: /* success */
316 case -ESHUTDOWN
: /* unplug */
318 case -EILSEQ
: /* protocol error or unplug */
319 case -EPROTO
: /* protocol error or unplug */
320 case -ECONNRESET
: /* unlink */
324 warn("output irq status %d received", urb
->status
);
327 spin_lock_irqsave(&usbhid
->outlock
, flags
);
330 usbhid
->outtail
= usbhid
->outhead
;
332 usbhid
->outtail
= (usbhid
->outtail
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1);
334 if (usbhid
->outhead
!= usbhid
->outtail
) {
335 if (hid_submit_out(hid
)) {
336 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
339 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
343 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
344 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
349 * Control pipe completion handler.
352 static void hid_ctrl(struct urb
*urb
)
354 struct hid_device
*hid
= urb
->context
;
355 struct usbhid_device
*usbhid
= hid
->driver_data
;
359 spin_lock_irqsave(&usbhid
->ctrllock
, flags
);
361 switch (urb
->status
) {
362 case 0: /* success */
363 if (usbhid
->ctrl
[usbhid
->ctrltail
].dir
== USB_DIR_IN
)
364 hid_input_report(urb
->context
, usbhid
->ctrl
[usbhid
->ctrltail
].report
->type
,
365 urb
->transfer_buffer
, urb
->actual_length
, 0);
367 case -ESHUTDOWN
: /* unplug */
369 case -EILSEQ
: /* protocol error or unplug */
370 case -EPROTO
: /* protocol error or unplug */
371 case -ECONNRESET
: /* unlink */
373 case -EPIPE
: /* report not available */
376 warn("ctrl urb status %d received", urb
->status
);
380 usbhid
->ctrltail
= usbhid
->ctrlhead
;
382 usbhid
->ctrltail
= (usbhid
->ctrltail
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1);
384 if (usbhid
->ctrlhead
!= usbhid
->ctrltail
) {
385 if (hid_submit_ctrl(hid
)) {
386 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
389 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
393 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
394 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
398 void usbhid_submit_report(struct hid_device
*hid
, struct hid_report
*report
, unsigned char dir
)
402 struct usbhid_device
*usbhid
= hid
->driver_data
;
404 if ((hid
->quirks
& HID_QUIRK_NOGET
) && dir
== USB_DIR_IN
)
407 if (usbhid
->urbout
&& dir
== USB_DIR_OUT
&& report
->type
== HID_OUTPUT_REPORT
) {
409 spin_lock_irqsave(&usbhid
->outlock
, flags
);
411 if ((head
= (usbhid
->outhead
+ 1) & (HID_OUTPUT_FIFO_SIZE
- 1)) == usbhid
->outtail
) {
412 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
413 warn("output queue full");
417 usbhid
->out
[usbhid
->outhead
] = report
;
418 usbhid
->outhead
= head
;
420 if (!test_and_set_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
421 if (hid_submit_out(hid
))
422 clear_bit(HID_OUT_RUNNING
, &usbhid
->iofl
);
424 spin_unlock_irqrestore(&usbhid
->outlock
, flags
);
428 spin_lock_irqsave(&usbhid
->ctrllock
, flags
);
430 if ((head
= (usbhid
->ctrlhead
+ 1) & (HID_CONTROL_FIFO_SIZE
- 1)) == usbhid
->ctrltail
) {
431 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
432 warn("control queue full");
436 usbhid
->ctrl
[usbhid
->ctrlhead
].report
= report
;
437 usbhid
->ctrl
[usbhid
->ctrlhead
].dir
= dir
;
438 usbhid
->ctrlhead
= head
;
440 if (!test_and_set_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
441 if (hid_submit_ctrl(hid
))
442 clear_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
);
444 spin_unlock_irqrestore(&usbhid
->ctrllock
, flags
);
447 static int usb_hidinput_input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
449 struct hid_device
*hid
= input_get_drvdata(dev
);
450 struct hid_field
*field
;
454 return input_ff_event(dev
, type
, code
, value
);
459 if ((offset
= hidinput_find_field(hid
, type
, code
, &field
)) == -1) {
460 warn("event field not found");
464 hid_set_field(field
, offset
, value
);
465 usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
470 int usbhid_wait_io(struct hid_device
*hid
)
472 struct usbhid_device
*usbhid
= hid
->driver_data
;
474 if (!wait_event_timeout(hid
->wait
, (!test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
) &&
475 !test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
)),
477 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
484 static int hid_set_idle(struct usb_device
*dev
, int ifnum
, int report
, int idle
)
486 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
487 HID_REQ_SET_IDLE
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
, (idle
<< 8) | report
,
488 ifnum
, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
491 static int hid_get_class_descriptor(struct usb_device
*dev
, int ifnum
,
492 unsigned char type
, void *buf
, int size
)
494 int result
, retries
= 4;
496 memset(buf
, 0, size
);
499 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
500 USB_REQ_GET_DESCRIPTOR
, USB_RECIP_INTERFACE
| USB_DIR_IN
,
501 (type
<< 8), ifnum
, buf
, size
, USB_CTRL_GET_TIMEOUT
);
503 } while (result
< size
&& retries
);
507 int usbhid_open(struct hid_device
*hid
)
510 if (hid_start_in(hid
))
515 void usbhid_close(struct hid_device
*hid
)
517 struct usbhid_device
*usbhid
= hid
->driver_data
;
520 usb_kill_urb(usbhid
->urbin
);
524 * Initialize all reports
527 void usbhid_init_reports(struct hid_device
*hid
)
529 struct hid_report
*report
;
530 struct usbhid_device
*usbhid
= hid
->driver_data
;
533 list_for_each_entry(report
, &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
534 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
536 list_for_each_entry(report
, &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
537 usbhid_submit_report(hid
, report
, USB_DIR_IN
);
540 ret
= usbhid_wait_io(hid
);
543 if (test_bit(HID_CTRL_RUNNING
, &usbhid
->iofl
))
544 usb_kill_urb(usbhid
->urbctrl
);
545 if (test_bit(HID_OUT_RUNNING
, &usbhid
->iofl
))
546 usb_kill_urb(usbhid
->urbout
);
547 ret
= usbhid_wait_io(hid
);
551 warn("timeout initializing reports");
555 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
557 static int hid_find_field_early(struct hid_device
*hid
, unsigned int page
,
558 unsigned int hid_code
, struct hid_field
**pfield
)
560 struct hid_report
*report
;
561 struct hid_field
*field
;
562 struct hid_usage
*usage
;
565 list_for_each_entry(report
, &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
, list
) {
566 for (i
= 0; i
< report
->maxfield
; i
++) {
567 field
= report
->field
[i
];
568 for (j
= 0; j
< field
->maxusage
; j
++) {
569 usage
= &field
->usage
[j
];
570 if ((usage
->hid
& HID_USAGE_PAGE
) == page
&&
571 (usage
->hid
& 0xFFFF) == hid_code
) {
581 static void usbhid_set_leds(struct hid_device
*hid
)
583 struct hid_field
*field
;
586 if ((offset
= hid_find_field_early(hid
, HID_UP_LED
, 0x01, &field
)) != -1) {
587 hid_set_field(field
, offset
, 0);
588 usbhid_submit_report(hid
, field
->report
, USB_DIR_OUT
);
593 * Traverse the supplied list of reports and find the longest
595 static void hid_find_max_report(struct hid_device
*hid
, unsigned int type
, int *max
)
597 struct hid_report
*report
;
600 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
601 size
= ((report
->size
- 1) >> 3) + 1;
602 if (type
== HID_INPUT_REPORT
&& hid
->report_enum
[type
].numbered
)
609 static int hid_alloc_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
611 struct usbhid_device
*usbhid
= hid
->driver_data
;
613 if (!(usbhid
->inbuf
= usb_buffer_alloc(dev
, usbhid
->bufsize
, GFP_ATOMIC
, &usbhid
->inbuf_dma
)))
615 if (!(usbhid
->outbuf
= usb_buffer_alloc(dev
, usbhid
->bufsize
, GFP_ATOMIC
, &usbhid
->outbuf_dma
)))
617 if (!(usbhid
->cr
= usb_buffer_alloc(dev
, sizeof(*(usbhid
->cr
)), GFP_ATOMIC
, &usbhid
->cr_dma
)))
619 if (!(usbhid
->ctrlbuf
= usb_buffer_alloc(dev
, usbhid
->bufsize
, GFP_ATOMIC
, &usbhid
->ctrlbuf_dma
)))
625 static void hid_free_buffers(struct usb_device
*dev
, struct hid_device
*hid
)
627 struct usbhid_device
*usbhid
= hid
->driver_data
;
629 usb_buffer_free(dev
, usbhid
->bufsize
, usbhid
->inbuf
, usbhid
->inbuf_dma
);
630 usb_buffer_free(dev
, usbhid
->bufsize
, usbhid
->outbuf
, usbhid
->outbuf_dma
);
631 usb_buffer_free(dev
, sizeof(*(usbhid
->cr
)), usbhid
->cr
, usbhid
->cr_dma
);
632 usb_buffer_free(dev
, usbhid
->bufsize
, usbhid
->ctrlbuf
, usbhid
->ctrlbuf_dma
);
636 * Cherry Cymotion keyboard have an invalid HID report descriptor,
637 * that needs fixing before we can parse it.
640 static void hid_fixup_cymotion_descriptor(char *rdesc
, int rsize
)
642 if (rsize
>= 17 && rdesc
[11] == 0x3c && rdesc
[12] == 0x02) {
643 info("Fixing up Cherry Cymotion report descriptor");
644 rdesc
[11] = rdesc
[16] = 0xff;
645 rdesc
[12] = rdesc
[17] = 0x03;
650 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
651 * to "operational". Without this, the ps3 controller will not report any
654 static void hid_fixup_sony_ps3_controller(struct usb_device
*dev
, int ifnum
)
657 char *buf
= kmalloc(18, GFP_KERNEL
);
662 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
664 USB_DIR_IN
| USB_TYPE_CLASS
|
666 (3 << 8) | 0xf2, ifnum
, buf
, 17,
667 USB_CTRL_GET_TIMEOUT
);
670 err_hid("%s failed: %d\n", __func__
, result
);
676 * Certain Logitech keyboards send in report #3 keys which are far
677 * above the logical maximum described in descriptor. This extends
678 * the original value of 0x28c of logical maximum to 0x104d
680 static void hid_fixup_logitech_descriptor(unsigned char *rdesc
, int rsize
)
682 if (rsize
>= 90 && rdesc
[83] == 0x26
684 && rdesc
[85] == 0x02) {
685 info("Fixing up Logitech keyboard report descriptor");
686 rdesc
[84] = rdesc
[89] = 0x4d;
687 rdesc
[85] = rdesc
[90] = 0x10;
692 * Some USB barcode readers from cypress have usage min and usage max in
695 static void hid_fixup_cypress_descriptor(unsigned char *rdesc
, int rsize
)
700 for (i
= 0; i
< rsize
- 4; i
++) {
701 if (rdesc
[i
] == 0x29 && rdesc
[i
+2] == 0x19) {
704 rdesc
[i
] = 0x19; rdesc
[i
+2] = 0x29;
706 rdesc
[i
+3] = rdesc
[i
+1];
712 info("Fixing up Cypress report descriptor");
715 static struct hid_device
*usb_hid_configure(struct usb_interface
*intf
)
717 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
718 struct usb_device
*dev
= interface_to_usbdev (intf
);
719 struct hid_descriptor
*hdesc
;
720 struct hid_device
*hid
;
724 int n
, len
, insize
= 0;
725 struct usbhid_device
*usbhid
;
727 quirks
= usbhid_lookup_quirk(le16_to_cpu(dev
->descriptor
.idVendor
),
728 le16_to_cpu(dev
->descriptor
.idProduct
));
730 /* Many keyboards and mice don't like to be polled for reports,
731 * so we will always set the HID_QUIRK_NOGET flag for them. */
732 if (interface
->desc
.bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
) {
733 if (interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_KEYBOARD
||
734 interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_MOUSE
)
735 quirks
|= HID_QUIRK_NOGET
;
738 if (quirks
& HID_QUIRK_IGNORE
)
741 if ((quirks
& HID_QUIRK_IGNORE_MOUSE
) &&
742 (interface
->desc
.bInterfaceProtocol
== USB_INTERFACE_PROTOCOL_MOUSE
))
746 if (usb_get_extra_descriptor(interface
, HID_DT_HID
, &hdesc
) &&
747 (!interface
->desc
.bNumEndpoints
||
748 usb_get_extra_descriptor(&interface
->endpoint
[0], HID_DT_HID
, &hdesc
))) {
749 dbg_hid("class descriptor not present\n");
753 for (n
= 0; n
< hdesc
->bNumDescriptors
; n
++)
754 if (hdesc
->desc
[n
].bDescriptorType
== HID_DT_REPORT
)
755 rsize
= le16_to_cpu(hdesc
->desc
[n
].wDescriptorLength
);
757 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
758 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
762 if (!(rdesc
= kmalloc(rsize
, GFP_KERNEL
))) {
763 dbg_hid("couldn't allocate rdesc memory\n");
767 hid_set_idle(dev
, interface
->desc
.bInterfaceNumber
, 0, 0);
769 if ((n
= hid_get_class_descriptor(dev
, interface
->desc
.bInterfaceNumber
, HID_DT_REPORT
, rdesc
, rsize
)) < 0) {
770 dbg_hid("reading report descriptor failed\n");
775 if ((quirks
& HID_QUIRK_CYMOTION
))
776 hid_fixup_cymotion_descriptor(rdesc
, rsize
);
778 if (quirks
& HID_QUIRK_LOGITECH_DESCRIPTOR
)
779 hid_fixup_logitech_descriptor(rdesc
, rsize
);
781 if (quirks
& HID_QUIRK_SWAPPED_MIN_MAX
)
782 hid_fixup_cypress_descriptor(rdesc
, rsize
);
784 dbg_hid("report descriptor (size %u, read %d) = ", rsize
, n
);
785 for (n
= 0; n
< rsize
; n
++)
786 dbg_hid_line(" %02x", (unsigned char) rdesc
[n
]);
789 if (!(hid
= hid_parse_report(rdesc
, n
))) {
790 dbg_hid("parsing report descriptor failed\n");
796 hid
->quirks
= quirks
;
798 if (!(usbhid
= kzalloc(sizeof(struct usbhid_device
), GFP_KERNEL
)))
801 hid
->driver_data
= usbhid
;
804 usbhid
->bufsize
= HID_MIN_BUFFER_SIZE
;
805 hid_find_max_report(hid
, HID_INPUT_REPORT
, &usbhid
->bufsize
);
806 hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &usbhid
->bufsize
);
807 hid_find_max_report(hid
, HID_FEATURE_REPORT
, &usbhid
->bufsize
);
809 if (usbhid
->bufsize
> HID_MAX_BUFFER_SIZE
)
810 usbhid
->bufsize
= HID_MAX_BUFFER_SIZE
;
812 hid_find_max_report(hid
, HID_INPUT_REPORT
, &insize
);
814 if (insize
> HID_MAX_BUFFER_SIZE
)
815 insize
= HID_MAX_BUFFER_SIZE
;
817 if (hid_alloc_buffers(dev
, hid
)) {
818 hid_free_buffers(dev
, hid
);
822 for (n
= 0; n
< interface
->desc
.bNumEndpoints
; n
++) {
824 struct usb_endpoint_descriptor
*endpoint
;
828 endpoint
= &interface
->endpoint
[n
].desc
;
829 if ((endpoint
->bmAttributes
& 3) != 3) /* Not an interrupt endpoint */
832 interval
= endpoint
->bInterval
;
834 /* Change the polling interval of mice. */
835 if (hid
->collection
->usage
== HID_GD_MOUSE
&& hid_mousepoll_interval
> 0)
836 interval
= hid_mousepoll_interval
;
838 if (usb_endpoint_dir_in(endpoint
)) {
841 if (!(usbhid
->urbin
= usb_alloc_urb(0, GFP_KERNEL
)))
843 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
844 usb_fill_int_urb(usbhid
->urbin
, dev
, pipe
, usbhid
->inbuf
, insize
,
845 hid_irq_in
, hid
, interval
);
846 usbhid
->urbin
->transfer_dma
= usbhid
->inbuf_dma
;
847 usbhid
->urbin
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
851 if (!(usbhid
->urbout
= usb_alloc_urb(0, GFP_KERNEL
)))
853 pipe
= usb_sndintpipe(dev
, endpoint
->bEndpointAddress
);
854 usb_fill_int_urb(usbhid
->urbout
, dev
, pipe
, usbhid
->outbuf
, 0,
855 hid_irq_out
, hid
, interval
);
856 usbhid
->urbout
->transfer_dma
= usbhid
->outbuf_dma
;
857 usbhid
->urbout
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
861 if (!usbhid
->urbin
) {
862 err_hid("couldn't find an input interrupt endpoint");
866 init_waitqueue_head(&hid
->wait
);
868 INIT_WORK(&usbhid
->reset_work
, hid_reset
);
869 setup_timer(&usbhid
->io_retry
, hid_retry_timeout
, (unsigned long) hid
);
871 spin_lock_init(&usbhid
->inlock
);
872 spin_lock_init(&usbhid
->outlock
);
873 spin_lock_init(&usbhid
->ctrllock
);
875 hid
->version
= le16_to_cpu(hdesc
->bcdHID
);
876 hid
->country
= hdesc
->bCountryCode
;
877 hid
->dev
= &intf
->dev
;
879 usbhid
->ifnum
= interface
->desc
.bInterfaceNumber
;
883 if (dev
->manufacturer
)
884 strlcpy(hid
->name
, dev
->manufacturer
, sizeof(hid
->name
));
887 if (dev
->manufacturer
)
888 strlcat(hid
->name
, " ", sizeof(hid
->name
));
889 strlcat(hid
->name
, dev
->product
, sizeof(hid
->name
));
892 if (!strlen(hid
->name
))
893 snprintf(hid
->name
, sizeof(hid
->name
), "HID %04x:%04x",
894 le16_to_cpu(dev
->descriptor
.idVendor
),
895 le16_to_cpu(dev
->descriptor
.idProduct
));
898 hid
->vendor
= le16_to_cpu(dev
->descriptor
.idVendor
);
899 hid
->product
= le16_to_cpu(dev
->descriptor
.idProduct
);
901 usb_make_path(dev
, hid
->phys
, sizeof(hid
->phys
));
902 strlcat(hid
->phys
, "/input", sizeof(hid
->phys
));
903 len
= strlen(hid
->phys
);
904 if (len
< sizeof(hid
->phys
) - 1)
905 snprintf(hid
->phys
+ len
, sizeof(hid
->phys
) - len
,
906 "%d", intf
->altsetting
[0].desc
.bInterfaceNumber
);
908 if (usb_string(dev
, dev
->descriptor
.iSerialNumber
, hid
->uniq
, 64) <= 0)
911 usbhid
->urbctrl
= usb_alloc_urb(0, GFP_KERNEL
);
912 if (!usbhid
->urbctrl
)
915 usb_fill_control_urb(usbhid
->urbctrl
, dev
, 0, (void *) usbhid
->cr
,
916 usbhid
->ctrlbuf
, 1, hid_ctrl
, hid
);
917 usbhid
->urbctrl
->setup_dma
= usbhid
->cr_dma
;
918 usbhid
->urbctrl
->transfer_dma
= usbhid
->ctrlbuf_dma
;
919 usbhid
->urbctrl
->transfer_flags
|= (URB_NO_TRANSFER_DMA_MAP
| URB_NO_SETUP_DMA_MAP
);
920 hid
->hidinput_input_event
= usb_hidinput_input_event
;
921 hid
->hid_open
= usbhid_open
;
922 hid
->hid_close
= usbhid_close
;
923 #ifdef CONFIG_USB_HIDDEV
924 hid
->hiddev_hid_event
= hiddev_hid_event
;
925 hid
->hiddev_report_event
= hiddev_report_event
;
930 usb_free_urb(usbhid
->urbin
);
931 usb_free_urb(usbhid
->urbout
);
932 usb_free_urb(usbhid
->urbctrl
);
933 hid_free_buffers(dev
, hid
);
934 hid_free_device(hid
);
939 static void hid_disconnect(struct usb_interface
*intf
)
941 struct hid_device
*hid
= usb_get_intfdata (intf
);
942 struct usbhid_device
*usbhid
;
947 usbhid
= hid
->driver_data
;
949 spin_lock_irq(&usbhid
->inlock
); /* Sync with error handler */
950 usb_set_intfdata(intf
, NULL
);
951 spin_unlock_irq(&usbhid
->inlock
);
952 usb_kill_urb(usbhid
->urbin
);
953 usb_kill_urb(usbhid
->urbout
);
954 usb_kill_urb(usbhid
->urbctrl
);
956 del_timer_sync(&usbhid
->io_retry
);
957 flush_scheduled_work();
959 if (hid
->claimed
& HID_CLAIMED_INPUT
)
960 hidinput_disconnect(hid
);
961 if (hid
->claimed
& HID_CLAIMED_HIDDEV
)
962 hiddev_disconnect(hid
);
964 usb_free_urb(usbhid
->urbin
);
965 usb_free_urb(usbhid
->urbctrl
);
966 usb_free_urb(usbhid
->urbout
);
968 hid_free_buffers(hid_to_usb_dev(hid
), hid
);
969 hid_free_device(hid
);
972 static int hid_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
974 struct hid_device
*hid
;
979 dbg_hid("HID probe called for ifnum %d\n",
980 intf
->altsetting
->desc
.bInterfaceNumber
);
982 if (!(hid
= usb_hid_configure(intf
)))
985 usbhid_init_reports(hid
);
986 hid_dump_device(hid
);
987 if (hid
->quirks
& HID_QUIRK_RESET_LEDS
)
988 usbhid_set_leds(hid
);
990 if (!hidinput_connect(hid
))
991 hid
->claimed
|= HID_CLAIMED_INPUT
;
992 if (!hiddev_connect(hid
))
993 hid
->claimed
|= HID_CLAIMED_HIDDEV
;
995 usb_set_intfdata(intf
, hid
);
998 printk ("HID device not claimed by input or hiddev\n");
999 hid_disconnect(intf
);
1003 if ((hid
->claimed
& HID_CLAIMED_INPUT
))
1006 if (hid
->quirks
& HID_QUIRK_SONY_PS3_CONTROLLER
)
1007 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf
),
1008 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1012 if (hid
->claimed
& HID_CLAIMED_INPUT
)
1014 if (hid
->claimed
== (HID_CLAIMED_INPUT
| HID_CLAIMED_HIDDEV
))
1016 if (hid
->claimed
& HID_CLAIMED_HIDDEV
)
1017 printk("hiddev%d", hid
->minor
);
1020 for (i
= 0; i
< hid
->maxcollection
; i
++) {
1021 if (hid
->collection
[i
].type
== HID_COLLECTION_APPLICATION
&&
1022 (hid
->collection
[i
].usage
& HID_USAGE_PAGE
) == HID_UP_GENDESK
&&
1023 (hid
->collection
[i
].usage
& 0xffff) < ARRAY_SIZE(hid_types
)) {
1024 c
= hid_types
[hid
->collection
[i
].usage
& 0xffff];
1029 usb_make_path(interface_to_usbdev(intf
), path
, 63);
1031 printk(": USB HID v%x.%02x %s [%s] on %s\n",
1032 hid
->version
>> 8, hid
->version
& 0xff, c
, hid
->name
, path
);
1037 static int hid_suspend(struct usb_interface
*intf
, pm_message_t message
)
1039 struct hid_device
*hid
= usb_get_intfdata (intf
);
1040 struct usbhid_device
*usbhid
= hid
->driver_data
;
1042 spin_lock_irq(&usbhid
->inlock
); /* Sync with error handler */
1043 set_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1044 spin_unlock_irq(&usbhid
->inlock
);
1045 del_timer(&usbhid
->io_retry
);
1046 usb_kill_urb(usbhid
->urbin
);
1047 dev_dbg(&intf
->dev
, "suspend\n");
1051 static int hid_resume(struct usb_interface
*intf
)
1053 struct hid_device
*hid
= usb_get_intfdata (intf
);
1054 struct usbhid_device
*usbhid
= hid
->driver_data
;
1057 clear_bit(HID_SUSPENDED
, &usbhid
->iofl
);
1058 usbhid
->retry_delay
= 0;
1059 status
= hid_start_in(hid
);
1060 dev_dbg(&intf
->dev
, "resume status %d\n", status
);
1064 /* Treat USB reset pretty much the same as suspend/resume */
1065 static void hid_pre_reset(struct usb_interface
*intf
)
1067 /* FIXME: What if the interface is already suspended? */
1068 hid_suspend(intf
, PMSG_ON
);
1071 static void hid_post_reset(struct usb_interface
*intf
)
1073 struct usb_device
*dev
= interface_to_usbdev (intf
);
1075 hid_set_idle(dev
, intf
->cur_altsetting
->desc
.bInterfaceNumber
, 0, 0);
1076 /* FIXME: Any more reinitialization needed? */
1081 static struct usb_device_id hid_usb_ids
[] = {
1082 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
1083 .bInterfaceClass
= USB_INTERFACE_CLASS_HID
},
1084 { } /* Terminating entry */
1087 MODULE_DEVICE_TABLE (usb
, hid_usb_ids
);
1089 static struct usb_driver hid_driver
= {
1092 .disconnect
= hid_disconnect
,
1093 .suspend
= hid_suspend
,
1094 .resume
= hid_resume
,
1095 .pre_reset
= hid_pre_reset
,
1096 .post_reset
= hid_post_reset
,
1097 .id_table
= hid_usb_ids
,
1100 static int __init
hid_init(void)
1103 retval
= usbhid_quirks_init(quirks_param
);
1105 goto usbhid_quirks_init_fail
;
1106 retval
= hiddev_init();
1108 goto hiddev_init_fail
;
1109 retval
= usb_register(&hid_driver
);
1111 goto usb_register_fail
;
1112 info(DRIVER_VERSION
":" DRIVER_DESC
);
1118 usbhid_quirks_exit();
1119 usbhid_quirks_init_fail
:
1123 static void __exit
hid_exit(void)
1125 usb_deregister(&hid_driver
);
1127 usbhid_quirks_exit();
1130 module_init(hid_init
);
1131 module_exit(hid_exit
);
1133 MODULE_AUTHOR(DRIVER_AUTHOR
);
1134 MODULE_DESCRIPTION(DRIVER_DESC
);
1135 MODULE_LICENSE(DRIVER_LICENSE
);