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 = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
118 rc = usb_reset_device(hid_to_usb_dev(hid));
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 (test_bit(HID_DISCONNECTED, &usbhid->iofl))
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 dev_warn(&urb->dev->dev, "input irq status %d "
217 "received\n", urb->status);
220 status = usb_submit_urb(urb, GFP_ATOMIC);
222 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
223 if (status != -EPERM) {
224 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
225 hid_to_usb_dev(hid)->bus->bus_name,
226 hid_to_usb_dev(hid)->devpath,
227 usbhid->ifnum, status);
233 static int hid_submit_out(struct hid_device *hid)
235 struct hid_report *report;
237 struct usbhid_device *usbhid = hid->driver_data;
239 report = usbhid->out[usbhid->outtail].report;
240 raw_report = usbhid->out[usbhid->outtail].raw_report;
242 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
243 usbhid->urbout->dev = hid_to_usb_dev(hid);
244 memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
247 dbg_hid("submitting out urb\n");
249 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
250 err_hid("usb_submit_urb(out) failed");
257 static int hid_submit_ctrl(struct hid_device *hid)
259 struct hid_report *report;
263 struct usbhid_device *usbhid = hid->driver_data;
265 report = usbhid->ctrl[usbhid->ctrltail].report;
266 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
267 dir = usbhid->ctrl[usbhid->ctrltail].dir;
269 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
270 if (dir == USB_DIR_OUT) {
271 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
272 usbhid->urbctrl->transfer_buffer_length = len;
273 memcpy(usbhid->ctrlbuf, raw_report, len);
276 int maxpacket, padlen;
278 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
279 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
281 padlen = DIV_ROUND_UP(len, maxpacket);
283 if (padlen > usbhid->bufsize)
284 padlen = usbhid->bufsize;
287 usbhid->urbctrl->transfer_buffer_length = padlen;
289 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
291 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
292 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
293 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
294 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
295 usbhid->cr->wLength = cpu_to_le16(len);
297 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
298 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
299 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
301 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
302 err_hid("usb_submit_urb(ctrl) failed");
310 * Output interrupt completion handler.
313 static void hid_irq_out(struct urb *urb)
315 struct hid_device *hid = urb->context;
316 struct usbhid_device *usbhid = hid->driver_data;
320 switch (urb->status) {
321 case 0: /* success */
323 case -ESHUTDOWN: /* unplug */
325 case -EILSEQ: /* protocol error or unplug */
326 case -EPROTO: /* protocol error or unplug */
327 case -ECONNRESET: /* unlink */
331 dev_warn(&urb->dev->dev, "output irq status %d "
332 "received\n", urb->status);
335 spin_lock_irqsave(&usbhid->outlock, flags);
338 usbhid->outtail = usbhid->outhead;
340 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
342 if (usbhid->outhead != usbhid->outtail) {
343 if (hid_submit_out(hid)) {
344 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
345 wake_up(&usbhid->wait);
347 spin_unlock_irqrestore(&usbhid->outlock, flags);
351 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
352 spin_unlock_irqrestore(&usbhid->outlock, flags);
353 wake_up(&usbhid->wait);
357 * Control pipe completion handler.
360 static void hid_ctrl(struct urb *urb)
362 struct hid_device *hid = urb->context;
363 struct usbhid_device *usbhid = hid->driver_data;
367 spin_lock_irqsave(&usbhid->ctrllock, flags);
369 switch (urb->status) {
370 case 0: /* success */
371 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
372 hid_input_report(urb->context,
373 usbhid->ctrl[usbhid->ctrltail].report->type,
374 urb->transfer_buffer, urb->actual_length, 0);
376 case -ESHUTDOWN: /* unplug */
378 case -EILSEQ: /* protocol error or unplug */
379 case -EPROTO: /* protocol error or unplug */
380 case -ECONNRESET: /* unlink */
382 case -EPIPE: /* report not available */
385 dev_warn(&urb->dev->dev, "ctrl urb status %d "
386 "received\n", urb->status);
390 usbhid->ctrltail = usbhid->ctrlhead;
392 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
394 if (usbhid->ctrlhead != usbhid->ctrltail) {
395 if (hid_submit_ctrl(hid)) {
396 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
397 wake_up(&usbhid->wait);
399 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
403 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
404 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
405 wake_up(&usbhid->wait);
408 void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
412 struct usbhid_device *usbhid = hid->driver_data;
413 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
415 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
418 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
420 spin_lock_irqsave(&usbhid->outlock, flags);
422 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
423 spin_unlock_irqrestore(&usbhid->outlock, flags);
424 dev_warn(&hid->dev, "output queue full\n");
428 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
429 if (!usbhid->out[usbhid->outhead].raw_report) {
430 spin_unlock_irqrestore(&usbhid->outlock, flags);
431 dev_warn(&hid->dev, "output queueing failed\n");
434 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
435 usbhid->out[usbhid->outhead].report = report;
436 usbhid->outhead = head;
438 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
439 if (hid_submit_out(hid))
440 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
442 spin_unlock_irqrestore(&usbhid->outlock, flags);
446 spin_lock_irqsave(&usbhid->ctrllock, flags);
448 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
449 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
450 dev_warn(&hid->dev, "control queue full\n");
454 if (dir == USB_DIR_OUT) {
455 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
456 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
457 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
458 dev_warn(&hid->dev, "control queueing failed\n");
461 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
463 usbhid->ctrl[usbhid->ctrlhead].report = report;
464 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
465 usbhid->ctrlhead = head;
467 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
468 if (hid_submit_ctrl(hid))
469 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
471 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
473 EXPORT_SYMBOL_GPL(usbhid_submit_report);
475 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
477 struct hid_device *hid = input_get_drvdata(dev);
478 struct hid_field *field;
482 return input_ff_event(dev, type, code, value);
487 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
488 dev_warn(&dev->dev, "event field not found\n");
492 hid_set_field(field, offset, value);
493 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
498 int usbhid_wait_io(struct hid_device *hid)
500 struct usbhid_device *usbhid = hid->driver_data;
502 if (!wait_event_timeout(usbhid->wait,
503 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
504 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
506 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
513 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
515 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
516 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
517 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
520 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
521 unsigned char type, void *buf, int size)
523 int result, retries = 4;
525 memset(buf, 0, size);
528 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
529 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
530 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
532 } while (result < size && retries);
536 int usbhid_open(struct hid_device *hid)
538 struct usbhid_device *usbhid = hid->driver_data;
542 res = usb_autopm_get_interface(usbhid->intf);
548 if (hid_start_in(hid))
553 void usbhid_close(struct hid_device *hid)
555 struct usbhid_device *usbhid = hid->driver_data;
558 usb_kill_urb(usbhid->urbin);
559 usb_autopm_put_interface(usbhid->intf);
564 * Initialize all reports
567 void usbhid_init_reports(struct hid_device *hid)
569 struct hid_report *report;
570 struct usbhid_device *usbhid = hid->driver_data;
573 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
574 usbhid_submit_report(hid, report, USB_DIR_IN);
576 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
577 usbhid_submit_report(hid, report, USB_DIR_IN);
580 ret = usbhid_wait_io(hid);
583 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
584 usb_kill_urb(usbhid->urbctrl);
585 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
586 usb_kill_urb(usbhid->urbout);
587 ret = usbhid_wait_io(hid);
591 dev_warn(&hid->dev, "timeout initializing reports\n");
595 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
597 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
598 unsigned int hid_code, struct hid_field **pfield)
600 struct hid_report *report;
601 struct hid_field *field;
602 struct hid_usage *usage;
605 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
606 for (i = 0; i < report->maxfield; i++) {
607 field = report->field[i];
608 for (j = 0; j < field->maxusage; j++) {
609 usage = &field->usage[j];
610 if ((usage->hid & HID_USAGE_PAGE) == page &&
611 (usage->hid & 0xFFFF) == hid_code) {
621 void usbhid_set_leds(struct hid_device *hid)
623 struct hid_field *field;
626 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
627 hid_set_field(field, offset, 0);
628 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
631 EXPORT_SYMBOL_GPL(usbhid_set_leds);
634 * Traverse the supplied list of reports and find the longest
636 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
639 struct hid_report *report;
642 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
643 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
649 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
651 struct usbhid_device *usbhid = hid->driver_data;
653 usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
655 usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
656 &usbhid->outbuf_dma);
657 usbhid->cr = usb_buffer_alloc(dev, sizeof(*usbhid->cr), GFP_KERNEL,
659 usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
660 &usbhid->ctrlbuf_dma);
661 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
668 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
670 struct usbhid_device *usbhid = hid->driver_data;
671 struct usb_device *dev = hid_to_usb_dev(hid);
672 struct usb_interface *intf = usbhid->intf;
673 struct usb_host_interface *interface = intf->cur_altsetting;
676 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
678 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
679 ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
680 interface->desc.bInterfaceNumber, buf + 1, count - 1,
681 USB_CTRL_SET_TIMEOUT);
683 /* count also the report id */
690 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
692 struct usbhid_device *usbhid = hid->driver_data;
694 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
695 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
696 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
697 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
700 static int usbhid_parse(struct hid_device *hid)
702 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
703 struct usb_host_interface *interface = intf->cur_altsetting;
704 struct usb_device *dev = interface_to_usbdev (intf);
705 struct hid_descriptor *hdesc;
707 unsigned int rsize = 0;
711 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
712 le16_to_cpu(dev->descriptor.idProduct));
714 if (quirks & HID_QUIRK_IGNORE)
717 /* Many keyboards and mice don't like to be polled for reports,
718 * so we will always set the HID_QUIRK_NOGET flag for them. */
719 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
720 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
721 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
722 quirks |= HID_QUIRK_NOGET;
725 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
726 (!interface->desc.bNumEndpoints ||
727 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
728 dbg_hid("class descriptor not present\n");
732 hid->version = le16_to_cpu(hdesc->bcdHID);
733 hid->country = hdesc->bCountryCode;
735 for (n = 0; n < hdesc->bNumDescriptors; n++)
736 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
737 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
739 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
740 dbg_hid("weird size of report descriptor (%u)\n", rsize);
744 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
745 dbg_hid("couldn't allocate rdesc memory\n");
749 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
751 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
752 HID_DT_REPORT, rdesc, rsize);
754 dbg_hid("reading report descriptor failed\n");
759 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
760 for (n = 0; n < rsize; n++)
761 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
764 ret = hid_parse_report(hid, rdesc, rsize);
767 dbg_hid("parsing report descriptor failed\n");
771 hid->quirks = quirks;
778 static int usbhid_start(struct hid_device *hid)
780 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
781 struct usb_host_interface *interface = intf->cur_altsetting;
782 struct usb_device *dev = interface_to_usbdev(intf);
783 struct usbhid_device *usbhid = hid->driver_data;
784 unsigned int n, insize = 0;
787 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
789 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
790 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
791 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
792 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
794 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
795 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
797 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
799 if (insize > HID_MAX_BUFFER_SIZE)
800 insize = HID_MAX_BUFFER_SIZE;
802 if (hid_alloc_buffers(dev, hid)) {
807 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
808 struct usb_endpoint_descriptor *endpoint;
812 endpoint = &interface->endpoint[n].desc;
813 if (!usb_endpoint_xfer_int(endpoint))
816 interval = endpoint->bInterval;
818 /* Some vendors give fullspeed interval on highspeed devides */
819 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
820 dev->speed == USB_SPEED_HIGH) {
821 interval = fls(endpoint->bInterval*8);
822 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
823 hid->name, endpoint->bInterval, interval);
826 /* Change the polling interval of mice. */
827 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
828 interval = hid_mousepoll_interval;
831 if (usb_endpoint_dir_in(endpoint)) {
834 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
836 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
837 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
838 hid_irq_in, hid, interval);
839 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
840 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
844 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
846 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
847 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
848 hid_irq_out, hid, interval);
849 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
850 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
854 init_waitqueue_head(&usbhid->wait);
855 INIT_WORK(&usbhid->reset_work, hid_reset);
856 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
858 spin_lock_init(&usbhid->inlock);
859 spin_lock_init(&usbhid->outlock);
860 spin_lock_init(&usbhid->ctrllock);
863 usbhid->ifnum = interface->desc.bInterfaceNumber;
865 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
866 if (!usbhid->urbctrl) {
871 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
872 usbhid->ctrlbuf, 1, hid_ctrl, hid);
873 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
874 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
875 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
877 usbhid_init_reports(hid);
878 hid_dump_device(hid);
880 set_bit(HID_STARTED, &usbhid->iofl);
882 /* Some keyboards don't work until their LEDs have been set.
883 * Since BIOSes do set the LEDs, it must be safe for any device
884 * that supports the keyboard boot protocol.
886 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
887 interface->desc.bInterfaceProtocol ==
888 USB_INTERFACE_PROTOCOL_KEYBOARD)
889 usbhid_set_leds(hid);
894 usb_free_urb(usbhid->urbin);
895 usb_free_urb(usbhid->urbout);
896 usb_free_urb(usbhid->urbctrl);
897 usbhid->urbin = NULL;
898 usbhid->urbout = NULL;
899 usbhid->urbctrl = NULL;
900 hid_free_buffers(dev, hid);
904 static void usbhid_stop(struct hid_device *hid)
906 struct usbhid_device *usbhid = hid->driver_data;
908 if (WARN_ON(!usbhid))
911 clear_bit(HID_STARTED, &usbhid->iofl);
912 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
913 set_bit(HID_DISCONNECTED, &usbhid->iofl);
914 spin_unlock_irq(&usbhid->inlock);
915 usb_kill_urb(usbhid->urbin);
916 usb_kill_urb(usbhid->urbout);
917 usb_kill_urb(usbhid->urbctrl);
919 del_timer_sync(&usbhid->io_retry);
920 cancel_work_sync(&usbhid->reset_work);
922 if (hid->claimed & HID_CLAIMED_INPUT)
923 hidinput_disconnect(hid);
924 if (hid->claimed & HID_CLAIMED_HIDDEV)
925 hiddev_disconnect(hid);
926 if (hid->claimed & HID_CLAIMED_HIDRAW)
927 hidraw_disconnect(hid);
931 usb_free_urb(usbhid->urbin);
932 usb_free_urb(usbhid->urbctrl);
933 usb_free_urb(usbhid->urbout);
934 usbhid->urbin = NULL; /* don't mess up next start */
935 usbhid->urbctrl = NULL;
936 usbhid->urbout = NULL;
938 hid_free_buffers(hid_to_usb_dev(hid), hid);
941 static struct hid_ll_driver usb_hid_driver = {
942 .parse = usbhid_parse,
943 .start = usbhid_start,
946 .close = usbhid_close,
947 .hidinput_input_event = usb_hidinput_input_event,
950 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
952 struct usb_host_interface *interface = intf->cur_altsetting;
953 struct usb_device *dev = interface_to_usbdev(intf);
954 struct usbhid_device *usbhid;
955 struct hid_device *hid;
956 unsigned int n, has_in = 0;
960 dbg_hid("HID probe called for ifnum %d\n",
961 intf->altsetting->desc.bInterfaceNumber);
963 for (n = 0; n < interface->desc.bNumEndpoints; n++)
964 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
967 dev_err(&intf->dev, "couldn't find an input interrupt "
972 hid = hid_allocate_device();
976 usb_set_intfdata(intf, hid);
977 hid->ll_driver = &usb_hid_driver;
978 hid->hid_output_raw_report = usbhid_output_raw_report;
979 hid->ff_init = hid_pidff_init;
980 #ifdef CONFIG_USB_HIDDEV
981 hid->hiddev_connect = hiddev_connect;
982 hid->hiddev_hid_event = hiddev_hid_event;
983 hid->hiddev_report_event = hiddev_report_event;
985 hid->dev.parent = &intf->dev;
987 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
988 hid->product = le16_to_cpu(dev->descriptor.idProduct);
990 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
991 USB_INTERFACE_PROTOCOL_MOUSE)
992 hid->type = HID_TYPE_USBMOUSE;
994 if (dev->manufacturer)
995 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
998 if (dev->manufacturer)
999 strlcat(hid->name, " ", sizeof(hid->name));
1000 strlcat(hid->name, dev->product, sizeof(hid->name));
1003 if (!strlen(hid->name))
1004 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1005 le16_to_cpu(dev->descriptor.idVendor),
1006 le16_to_cpu(dev->descriptor.idProduct));
1008 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1009 strlcat(hid->phys, "/input", sizeof(hid->phys));
1010 len = strlen(hid->phys);
1011 if (len < sizeof(hid->phys) - 1)
1012 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1013 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1015 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1018 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1019 if (usbhid == NULL) {
1024 hid->driver_data = usbhid;
1027 ret = hid_add_device(hid);
1030 dev_err(&intf->dev, "can't add hid device: %d\n", ret);
1038 hid_destroy_device(hid);
1042 static void hid_disconnect(struct usb_interface *intf)
1044 struct hid_device *hid = usb_get_intfdata(intf);
1045 struct usbhid_device *usbhid;
1050 usbhid = hid->driver_data;
1051 hid_destroy_device(hid);
1055 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1057 struct hid_device *hid = usb_get_intfdata (intf);
1058 struct usbhid_device *usbhid = hid->driver_data;
1060 if (!test_bit(HID_STARTED, &usbhid->iofl))
1063 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1064 set_bit(HID_SUSPENDED, &usbhid->iofl);
1065 spin_unlock_irq(&usbhid->inlock);
1066 del_timer_sync(&usbhid->io_retry);
1067 usb_kill_urb(usbhid->urbin);
1068 dev_dbg(&intf->dev, "suspend\n");
1072 static int hid_resume(struct usb_interface *intf)
1074 struct hid_device *hid = usb_get_intfdata (intf);
1075 struct usbhid_device *usbhid = hid->driver_data;
1078 if (!test_bit(HID_STARTED, &usbhid->iofl))
1081 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1082 usbhid->retry_delay = 0;
1083 status = hid_start_in(hid);
1084 dev_dbg(&intf->dev, "resume status %d\n", status);
1088 /* Treat USB reset pretty much the same as suspend/resume */
1089 static int hid_pre_reset(struct usb_interface *intf)
1091 /* FIXME: What if the interface is already suspended? */
1092 hid_suspend(intf, PMSG_ON);
1096 /* Same routine used for post_reset and reset_resume */
1097 static int hid_post_reset(struct usb_interface *intf)
1099 struct usb_device *dev = interface_to_usbdev (intf);
1101 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1102 /* FIXME: Any more reinitialization needed? */
1104 return hid_resume(intf);
1107 static struct usb_device_id hid_usb_ids [] = {
1108 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1109 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1110 { } /* Terminating entry */
1113 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1115 static struct usb_driver hid_driver = {
1118 .disconnect = hid_disconnect,
1119 .suspend = hid_suspend,
1120 .resume = hid_resume,
1121 .reset_resume = hid_post_reset,
1122 .pre_reset = hid_pre_reset,
1123 .post_reset = hid_post_reset,
1124 .id_table = hid_usb_ids,
1125 .supports_autosuspend = 1,
1128 static const struct hid_device_id hid_usb_table[] = {
1129 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1133 static struct hid_driver hid_usb_driver = {
1134 .name = "generic-usb",
1135 .id_table = hid_usb_table,
1138 static int __init hid_init(void)
1141 retval = hid_register_driver(&hid_usb_driver);
1143 goto hid_register_fail;
1144 retval = usbhid_quirks_init(quirks_param);
1146 goto usbhid_quirks_init_fail;
1147 retval = hiddev_init();
1149 goto hiddev_init_fail;
1150 retval = usb_register(&hid_driver);
1152 goto usb_register_fail;
1153 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1160 usbhid_quirks_exit();
1161 usbhid_quirks_init_fail:
1162 hid_unregister_driver(&hid_usb_driver);
1167 static void __exit hid_exit(void)
1169 usb_deregister(&hid_driver);
1171 usbhid_quirks_exit();
1172 hid_unregister_driver(&hid_usb_driver);
1175 module_init(hid_init);
1176 module_exit(hid_exit);
1178 MODULE_AUTHOR(DRIVER_AUTHOR);
1179 MODULE_DESCRIPTION(DRIVER_DESC);
1180 MODULE_LICENSE(DRIVER_LICENSE);