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"
51 static unsigned int hid_mousepoll_interval;
52 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
53 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55 static unsigned int hid_jspoll_interval;
56 module_param_named(jspoll, hid_jspoll_interval, uint, 0644);
57 MODULE_PARM_DESC(jspoll, "Polling interval of joysticks");
59 static unsigned int ignoreled;
60 module_param_named(ignoreled, ignoreled, uint, 0644);
61 MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
63 /* Quirks specified at module load time */
64 static char *quirks_param[MAX_USBHID_BOOT_QUIRKS];
65 module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
66 MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
67 " quirks=vendorID:productID:quirks"
68 " where vendorID, productID, and quirks are all in"
71 * Input submission and I/O error handler.
73 static DEFINE_MUTEX(hid_open_mut);
75 static void hid_io_error(struct hid_device *hid);
76 static int hid_submit_out(struct hid_device *hid);
77 static int hid_submit_ctrl(struct hid_device *hid);
78 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
80 /* Start up the input URB */
81 static int hid_start_in(struct hid_device *hid)
85 struct usbhid_device *usbhid = hid->driver_data;
87 spin_lock_irqsave(&usbhid->lock, flags);
88 if ((hid->open > 0 || hid->quirks & HID_QUIRK_ALWAYS_POLL) &&
89 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
90 !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
91 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
92 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
94 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
96 set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
98 clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
101 spin_unlock_irqrestore(&usbhid->lock, flags);
105 /* I/O retry timer routine */
106 static void hid_retry_timeout(unsigned long _hid)
108 struct hid_device *hid = (struct hid_device *) _hid;
109 struct usbhid_device *usbhid = hid->driver_data;
111 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
112 if (hid_start_in(hid))
116 /* Workqueue routine to reset the device or clear a halt */
117 static void hid_reset(struct work_struct *work)
119 struct usbhid_device *usbhid =
120 container_of(work, struct usbhid_device, reset_work);
121 struct hid_device *hid = usbhid->hid;
124 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
125 dev_dbg(&usbhid->intf->dev, "clear halt\n");
126 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
127 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
131 dev_dbg(&usbhid->intf->dev,
132 "clear-halt failed: %d\n", rc);
133 set_bit(HID_RESET_PENDING, &usbhid->iofl);
137 if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
138 dev_dbg(&usbhid->intf->dev, "resetting device\n");
139 usb_queue_reset_device(usbhid->intf);
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->lock, 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 unless we lack bandwidth*/
170 if (!test_bit(HID_NO_BANDWIDTH, &usbhid->iofl)
171 && !test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
173 schedule_work(&usbhid->reset_work);
178 mod_timer(&usbhid->io_retry,
179 jiffies + msecs_to_jiffies(usbhid->retry_delay));
181 spin_unlock_irqrestore(&usbhid->lock, flags);
184 static void usbhid_mark_busy(struct usbhid_device *usbhid)
186 struct usb_interface *intf = usbhid->intf;
188 usb_mark_last_busy(interface_to_usbdev(intf));
191 static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
193 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
197 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
198 test_bit(HID_SUSPENDED, &usbhid->iofl))
201 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
202 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
204 /* Try to wake up from autosuspend... */
205 r = usb_autopm_get_interface_async(usbhid->intf);
210 * If still suspended, don't submit. Submission will
211 * occur if/when resume drains the queue.
213 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
214 usb_autopm_put_interface_no_suspend(usbhid->intf);
218 /* Asynchronously flush queue. */
219 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
220 if (hid_submit_out(hid)) {
221 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
222 usb_autopm_put_interface_async(usbhid->intf);
224 wake_up(&usbhid->wait);
229 static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
231 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
235 WARN_ON(hid == NULL);
236 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
237 test_bit(HID_SUSPENDED, &usbhid->iofl))
240 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
241 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
243 /* Try to wake up from autosuspend... */
244 r = usb_autopm_get_interface_async(usbhid->intf);
249 * If still suspended, don't submit. Submission will
250 * occur if/when resume drains the queue.
252 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
253 usb_autopm_put_interface_no_suspend(usbhid->intf);
257 /* Asynchronously flush queue. */
258 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
259 if (hid_submit_ctrl(hid)) {
260 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
261 usb_autopm_put_interface_async(usbhid->intf);
263 wake_up(&usbhid->wait);
269 * Input interrupt completion handler.
272 static void hid_irq_in(struct urb *urb)
274 struct hid_device *hid = urb->context;
275 struct usbhid_device *usbhid = hid->driver_data;
278 switch (urb->status) {
279 case 0: /* success */
280 usbhid->retry_delay = 0;
281 if ((hid->quirks & HID_QUIRK_ALWAYS_POLL) && !hid->open)
283 usbhid_mark_busy(usbhid);
284 if (!test_bit(HID_RESUME_RUNNING, &usbhid->iofl)) {
285 hid_input_report(urb->context, HID_INPUT_REPORT,
286 urb->transfer_buffer,
287 urb->actual_length, 1);
289 * autosuspend refused while keys are pressed
290 * because most keyboards don't wake up when
293 if (hid_check_keys_pressed(hid))
294 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
296 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
299 case -EPIPE: /* stall */
300 usbhid_mark_busy(usbhid);
301 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
302 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
303 schedule_work(&usbhid->reset_work);
305 case -ECONNRESET: /* unlink */
307 case -ESHUTDOWN: /* unplug */
308 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
310 case -EILSEQ: /* protocol error or unplug */
311 case -EPROTO: /* protocol error or unplug */
312 case -ETIME: /* protocol error or unplug */
313 case -ETIMEDOUT: /* Should never happen, but... */
314 usbhid_mark_busy(usbhid);
315 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
319 hid_warn(urb->dev, "input irq status %d received\n",
323 status = usb_submit_urb(urb, GFP_ATOMIC);
325 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
326 if (status != -EPERM) {
327 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
328 hid_to_usb_dev(hid)->bus->bus_name,
329 hid_to_usb_dev(hid)->devpath,
330 usbhid->ifnum, status);
336 static int hid_submit_out(struct hid_device *hid)
338 struct hid_report *report;
340 struct usbhid_device *usbhid = hid->driver_data;
343 report = usbhid->out[usbhid->outtail].report;
344 raw_report = usbhid->out[usbhid->outtail].raw_report;
346 usbhid->urbout->transfer_buffer_length = hid_report_len(report);
347 usbhid->urbout->dev = hid_to_usb_dev(hid);
349 memcpy(usbhid->outbuf, raw_report,
350 usbhid->urbout->transfer_buffer_length);
352 usbhid->out[usbhid->outtail].raw_report = NULL;
355 dbg_hid("submitting out urb\n");
357 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
359 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
362 usbhid->last_out = jiffies;
366 static int hid_submit_ctrl(struct hid_device *hid)
368 struct hid_report *report;
372 struct usbhid_device *usbhid = hid->driver_data;
374 report = usbhid->ctrl[usbhid->ctrltail].report;
375 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
376 dir = usbhid->ctrl[usbhid->ctrltail].dir;
378 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
379 if (dir == USB_DIR_OUT) {
380 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
381 usbhid->urbctrl->transfer_buffer_length = len;
383 memcpy(usbhid->ctrlbuf, raw_report, len);
385 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
388 int maxpacket, padlen;
390 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
391 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
392 usbhid->urbctrl->pipe, 0);
394 padlen = DIV_ROUND_UP(len, maxpacket);
396 if (padlen > usbhid->bufsize)
397 padlen = usbhid->bufsize;
400 usbhid->urbctrl->transfer_buffer_length = padlen;
402 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
404 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
405 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
407 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
409 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
410 usbhid->cr->wLength = cpu_to_le16(len);
412 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
413 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
415 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
417 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
419 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
422 usbhid->last_ctrl = jiffies;
427 * Output interrupt completion handler.
430 static void hid_irq_out(struct urb *urb)
432 struct hid_device *hid = urb->context;
433 struct usbhid_device *usbhid = hid->driver_data;
437 switch (urb->status) {
438 case 0: /* success */
440 case -ESHUTDOWN: /* unplug */
442 case -EILSEQ: /* protocol error or unplug */
443 case -EPROTO: /* protocol error or unplug */
444 case -ECONNRESET: /* unlink */
448 hid_warn(urb->dev, "output irq status %d received\n",
452 spin_lock_irqsave(&usbhid->lock, flags);
455 usbhid->outtail = usbhid->outhead;
457 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
459 if (usbhid->outhead != usbhid->outtail &&
460 hid_submit_out(hid) == 0) {
461 /* Successfully submitted next urb in queue */
462 spin_unlock_irqrestore(&usbhid->lock, flags);
467 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
468 spin_unlock_irqrestore(&usbhid->lock, flags);
469 usb_autopm_put_interface_async(usbhid->intf);
470 wake_up(&usbhid->wait);
474 * Control pipe completion handler.
477 static void hid_ctrl(struct urb *urb)
479 struct hid_device *hid = urb->context;
480 struct usbhid_device *usbhid = hid->driver_data;
481 int unplug = 0, status = urb->status;
484 case 0: /* success */
485 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
486 hid_input_report(urb->context,
487 usbhid->ctrl[usbhid->ctrltail].report->type,
488 urb->transfer_buffer, urb->actual_length, 0);
490 case -ESHUTDOWN: /* unplug */
492 case -EILSEQ: /* protocol error or unplug */
493 case -EPROTO: /* protocol error or unplug */
494 case -ECONNRESET: /* unlink */
496 case -EPIPE: /* report not available */
499 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
502 spin_lock(&usbhid->lock);
505 usbhid->ctrltail = usbhid->ctrlhead;
507 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
509 if (usbhid->ctrlhead != usbhid->ctrltail &&
510 hid_submit_ctrl(hid) == 0) {
511 /* Successfully submitted next urb in queue */
512 spin_unlock(&usbhid->lock);
517 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
518 spin_unlock(&usbhid->lock);
519 usb_autopm_put_interface_async(usbhid->intf);
520 wake_up(&usbhid->wait);
523 static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
527 struct usbhid_device *usbhid = hid->driver_data;
529 if (((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) ||
530 test_bit(HID_DISCONNECTED, &usbhid->iofl))
533 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
534 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
535 hid_warn(hid, "output queue full\n");
539 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
540 if (!usbhid->out[usbhid->outhead].raw_report) {
541 hid_warn(hid, "output queueing failed\n");
544 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
545 usbhid->out[usbhid->outhead].report = report;
546 usbhid->outhead = head;
548 /* If the queue isn't running, restart it */
549 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
550 usbhid_restart_out_queue(usbhid);
552 /* Otherwise see if an earlier request has timed out */
553 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
555 /* Prevent autosuspend following the unlink */
556 usb_autopm_get_interface_no_resume(usbhid->intf);
559 * Prevent resubmission in case the URB completes
560 * before we can unlink it. We don't want to cancel
561 * the wrong transfer!
563 usb_block_urb(usbhid->urbout);
565 /* Drop lock to avoid deadlock if the callback runs */
566 spin_unlock(&usbhid->lock);
568 usb_unlink_urb(usbhid->urbout);
569 spin_lock(&usbhid->lock);
570 usb_unblock_urb(usbhid->urbout);
572 /* Unlink might have stopped the queue */
573 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
574 usbhid_restart_out_queue(usbhid);
576 /* Now we can allow autosuspend again */
577 usb_autopm_put_interface_async(usbhid->intf);
582 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
583 hid_warn(hid, "control queue full\n");
587 if (dir == USB_DIR_OUT) {
588 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
589 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
590 hid_warn(hid, "control queueing failed\n");
593 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
595 usbhid->ctrl[usbhid->ctrlhead].report = report;
596 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
597 usbhid->ctrlhead = head;
599 /* If the queue isn't running, restart it */
600 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
601 usbhid_restart_ctrl_queue(usbhid);
603 /* Otherwise see if an earlier request has timed out */
604 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
606 /* Prevent autosuspend following the unlink */
607 usb_autopm_get_interface_no_resume(usbhid->intf);
610 * Prevent resubmission in case the URB completes
611 * before we can unlink it. We don't want to cancel
612 * the wrong transfer!
614 usb_block_urb(usbhid->urbctrl);
616 /* Drop lock to avoid deadlock if the callback runs */
617 spin_unlock(&usbhid->lock);
619 usb_unlink_urb(usbhid->urbctrl);
620 spin_lock(&usbhid->lock);
621 usb_unblock_urb(usbhid->urbctrl);
623 /* Unlink might have stopped the queue */
624 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
625 usbhid_restart_ctrl_queue(usbhid);
627 /* Now we can allow autosuspend again */
628 usb_autopm_put_interface_async(usbhid->intf);
632 static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
634 struct usbhid_device *usbhid = hid->driver_data;
637 spin_lock_irqsave(&usbhid->lock, flags);
638 __usbhid_submit_report(hid, report, dir);
639 spin_unlock_irqrestore(&usbhid->lock, flags);
642 static int usbhid_wait_io(struct hid_device *hid)
644 struct usbhid_device *usbhid = hid->driver_data;
646 if (!wait_event_timeout(usbhid->wait,
647 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
648 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
650 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
657 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
659 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
660 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
661 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
664 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
665 unsigned char type, void *buf, int size)
667 int result, retries = 4;
669 memset(buf, 0, size);
672 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
673 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
674 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
676 } while (result < size && retries);
680 int usbhid_open(struct hid_device *hid)
682 struct usbhid_device *usbhid = hid->driver_data;
685 mutex_lock(&hid_open_mut);
687 res = usb_autopm_get_interface(usbhid->intf);
688 /* the device must be awake to reliably request remote wakeup */
694 usbhid->intf->needs_remote_wakeup = 1;
695 set_bit(HID_RESUME_RUNNING, &usbhid->iofl);
696 res = hid_start_in(hid);
698 if (res != -ENOSPC) {
702 /* no use opening if resources are insufficient */
705 usbhid->intf->needs_remote_wakeup = 0;
708 usb_autopm_put_interface(usbhid->intf);
711 * In case events are generated while nobody was listening,
712 * some are released when the device is re-opened.
713 * Wait 50 msec for the queue to empty before allowing events
716 if (res == 0 && !(hid->quirks & HID_QUIRK_ALWAYS_POLL))
718 clear_bit(HID_RESUME_RUNNING, &usbhid->iofl);
721 mutex_unlock(&hid_open_mut);
725 void usbhid_close(struct hid_device *hid)
727 struct usbhid_device *usbhid = hid->driver_data;
729 mutex_lock(&hid_open_mut);
731 /* protecting hid->open to make sure we don't restart
732 * data acquistion due to a resumption we no longer
735 spin_lock_irq(&usbhid->lock);
737 spin_unlock_irq(&usbhid->lock);
738 hid_cancel_delayed_stuff(usbhid);
739 if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL)) {
740 usb_kill_urb(usbhid->urbin);
741 usbhid->intf->needs_remote_wakeup = 0;
744 spin_unlock_irq(&usbhid->lock);
746 mutex_unlock(&hid_open_mut);
750 * Initialize all reports
753 void usbhid_init_reports(struct hid_device *hid)
755 struct hid_report *report;
756 struct usbhid_device *usbhid = hid->driver_data;
757 struct hid_report_enum *report_enum;
760 report_enum = &hid->report_enum[HID_INPUT_REPORT];
761 list_for_each_entry(report, &report_enum->report_list, list)
762 usbhid_submit_report(hid, report, USB_DIR_IN);
764 report_enum = &hid->report_enum[HID_FEATURE_REPORT];
765 list_for_each_entry(report, &report_enum->report_list, list)
766 usbhid_submit_report(hid, report, USB_DIR_IN);
769 ret = usbhid_wait_io(hid);
772 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
773 usb_kill_urb(usbhid->urbctrl);
774 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
775 usb_kill_urb(usbhid->urbout);
776 ret = usbhid_wait_io(hid);
780 hid_warn(hid, "timeout initializing reports\n");
784 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
786 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
787 unsigned int hid_code, struct hid_field **pfield)
789 struct hid_report *report;
790 struct hid_field *field;
791 struct hid_usage *usage;
794 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
795 for (i = 0; i < report->maxfield; i++) {
796 field = report->field[i];
797 for (j = 0; j < field->maxusage; j++) {
798 usage = &field->usage[j];
799 if ((usage->hid & HID_USAGE_PAGE) == page &&
800 (usage->hid & 0xFFFF) == hid_code) {
810 static void usbhid_set_leds(struct hid_device *hid)
812 struct hid_field *field;
815 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
816 hid_set_field(field, offset, 0);
817 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
822 * Traverse the supplied list of reports and find the longest
824 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
827 struct hid_report *report;
830 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
831 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
837 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
839 struct usbhid_device *usbhid = hid->driver_data;
841 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
843 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
844 &usbhid->outbuf_dma);
845 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
846 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
847 &usbhid->ctrlbuf_dma);
848 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
855 static int usbhid_get_raw_report(struct hid_device *hid,
856 unsigned char report_number, __u8 *buf, size_t count,
857 unsigned char report_type)
859 struct usbhid_device *usbhid = hid->driver_data;
860 struct usb_device *dev = hid_to_usb_dev(hid);
861 struct usb_interface *intf = usbhid->intf;
862 struct usb_host_interface *interface = intf->cur_altsetting;
863 int skipped_report_id = 0;
866 /* Byte 0 is the report number. Report data starts at byte 1.*/
867 buf[0] = report_number;
868 if (report_number == 0x0) {
869 /* Offset the return buffer by 1, so that the report ID
870 will remain in byte 0. */
873 skipped_report_id = 1;
875 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
877 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
878 ((report_type + 1) << 8) | report_number,
879 interface->desc.bInterfaceNumber, buf, count,
880 USB_CTRL_SET_TIMEOUT);
882 /* count also the report id */
883 if (ret > 0 && skipped_report_id)
889 static int usbhid_set_raw_report(struct hid_device *hid, unsigned int reportnum,
890 __u8 *buf, size_t count, unsigned char rtype)
892 struct usbhid_device *usbhid = hid->driver_data;
893 struct usb_device *dev = hid_to_usb_dev(hid);
894 struct usb_interface *intf = usbhid->intf;
895 struct usb_host_interface *interface = intf->cur_altsetting;
896 int ret, skipped_report_id = 0;
898 /* Byte 0 is the report number. Report data starts at byte 1.*/
899 if ((rtype == HID_OUTPUT_REPORT) &&
900 (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORT_ID))
906 /* Don't send the Report ID */
909 skipped_report_id = 1;
912 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
914 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
915 ((rtype + 1) << 8) | reportnum,
916 interface->desc.bInterfaceNumber, buf, count,
917 USB_CTRL_SET_TIMEOUT);
918 /* count also the report id, if this was a numbered report. */
919 if (ret > 0 && skipped_report_id)
925 static int usbhid_output_report(struct hid_device *hid, __u8 *buf, size_t count)
927 struct usbhid_device *usbhid = hid->driver_data;
928 struct usb_device *dev = hid_to_usb_dev(hid);
929 int actual_length, skipped_report_id = 0, ret;
935 /* Don't send the Report ID */
938 skipped_report_id = 1;
941 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
942 buf, count, &actual_length,
943 USB_CTRL_SET_TIMEOUT);
944 /* return the number of bytes transferred */
947 /* count also the report id */
948 if (skipped_report_id)
955 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
957 struct usbhid_device *usbhid = hid->driver_data;
959 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
960 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
962 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
965 static int usbhid_parse(struct hid_device *hid)
967 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
968 struct usb_host_interface *interface = intf->cur_altsetting;
969 struct usb_device *dev = interface_to_usbdev (intf);
970 struct hid_descriptor *hdesc;
972 unsigned int rsize = 0;
976 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
977 le16_to_cpu(dev->descriptor.idProduct));
979 if (quirks & HID_QUIRK_IGNORE)
982 /* Many keyboards and mice don't like to be polled for reports,
983 * so we will always set the HID_QUIRK_NOGET flag for them. */
984 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
985 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
986 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
987 quirks |= HID_QUIRK_NOGET;
990 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
991 (!interface->desc.bNumEndpoints ||
992 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
993 dbg_hid("class descriptor not present\n");
997 hid->version = le16_to_cpu(hdesc->bcdHID);
998 hid->country = hdesc->bCountryCode;
1000 for (n = 0; n < hdesc->bNumDescriptors; n++)
1001 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1002 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1004 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1005 dbg_hid("weird size of report descriptor (%u)\n", rsize);
1009 rdesc = kmalloc(rsize, GFP_KERNEL);
1013 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1015 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1016 HID_DT_REPORT, rdesc, rsize);
1018 dbg_hid("reading report descriptor failed\n");
1023 ret = hid_parse_report(hid, rdesc, rsize);
1026 dbg_hid("parsing report descriptor failed\n");
1030 hid->quirks |= quirks;
1037 static int usbhid_start(struct hid_device *hid)
1039 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1040 struct usb_host_interface *interface = intf->cur_altsetting;
1041 struct usb_device *dev = interface_to_usbdev(intf);
1042 struct usbhid_device *usbhid = hid->driver_data;
1043 unsigned int n, insize = 0;
1046 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1048 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1049 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1050 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1051 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1053 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1054 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
1056 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1058 if (insize > HID_MAX_BUFFER_SIZE)
1059 insize = HID_MAX_BUFFER_SIZE;
1061 if (hid_alloc_buffers(dev, hid)) {
1066 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1067 struct usb_endpoint_descriptor *endpoint;
1071 endpoint = &interface->endpoint[n].desc;
1072 if (!usb_endpoint_xfer_int(endpoint))
1075 interval = endpoint->bInterval;
1077 /* Some vendors give fullspeed interval on highspeed devides */
1078 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
1079 dev->speed == USB_SPEED_HIGH) {
1080 interval = fls(endpoint->bInterval*8);
1081 pr_info("%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1082 hid->name, endpoint->bInterval, interval);
1085 /* Change the polling interval of mice and joysticks. */
1086 switch (hid->collection->usage) {
1088 if (hid_mousepoll_interval > 0)
1089 interval = hid_mousepoll_interval;
1091 case HID_GD_JOYSTICK:
1092 if (hid_jspoll_interval > 0)
1093 interval = hid_jspoll_interval;
1098 if (usb_endpoint_dir_in(endpoint)) {
1101 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1103 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1104 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1105 hid_irq_in, hid, interval);
1106 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1107 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1111 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1113 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1114 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1115 hid_irq_out, hid, interval);
1116 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1117 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1121 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1122 if (!usbhid->urbctrl) {
1127 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1128 usbhid->ctrlbuf, 1, hid_ctrl, hid);
1129 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1130 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1132 set_bit(HID_STARTED, &usbhid->iofl);
1134 if (hid->quirks & HID_QUIRK_ALWAYS_POLL) {
1135 ret = usb_autopm_get_interface(usbhid->intf);
1138 usbhid->intf->needs_remote_wakeup = 1;
1139 ret = hid_start_in(hid);
1142 "failed to start in urb: %d\n", ret);
1144 usb_autopm_put_interface(usbhid->intf);
1147 /* Some keyboards don't work until their LEDs have been set.
1148 * Since BIOSes do set the LEDs, it must be safe for any device
1149 * that supports the keyboard boot protocol.
1150 * In addition, enable remote wakeup by default for all keyboard
1151 * devices supporting the boot protocol.
1153 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1154 interface->desc.bInterfaceProtocol ==
1155 USB_INTERFACE_PROTOCOL_KEYBOARD) {
1156 usbhid_set_leds(hid);
1157 device_set_wakeup_enable(&dev->dev, 1);
1162 usb_free_urb(usbhid->urbin);
1163 usb_free_urb(usbhid->urbout);
1164 usb_free_urb(usbhid->urbctrl);
1165 usbhid->urbin = NULL;
1166 usbhid->urbout = NULL;
1167 usbhid->urbctrl = NULL;
1168 hid_free_buffers(dev, hid);
1172 static void usbhid_stop(struct hid_device *hid)
1174 struct usbhid_device *usbhid = hid->driver_data;
1176 if (WARN_ON(!usbhid))
1179 if (hid->quirks & HID_QUIRK_ALWAYS_POLL)
1180 usbhid->intf->needs_remote_wakeup = 0;
1182 clear_bit(HID_STARTED, &usbhid->iofl);
1183 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1184 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1185 spin_unlock_irq(&usbhid->lock);
1186 usb_kill_urb(usbhid->urbin);
1187 usb_kill_urb(usbhid->urbout);
1188 usb_kill_urb(usbhid->urbctrl);
1190 hid_cancel_delayed_stuff(usbhid);
1194 usb_free_urb(usbhid->urbin);
1195 usb_free_urb(usbhid->urbctrl);
1196 usb_free_urb(usbhid->urbout);
1197 usbhid->urbin = NULL; /* don't mess up next start */
1198 usbhid->urbctrl = NULL;
1199 usbhid->urbout = NULL;
1201 hid_free_buffers(hid_to_usb_dev(hid), hid);
1204 static int usbhid_power(struct hid_device *hid, int lvl)
1209 case PM_HINT_FULLON:
1210 r = usbhid_get_power(hid);
1212 case PM_HINT_NORMAL:
1213 usbhid_put_power(hid);
1219 static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1222 case HID_REQ_GET_REPORT:
1223 usbhid_submit_report(hid, rep, USB_DIR_IN);
1225 case HID_REQ_SET_REPORT:
1226 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1231 static int usbhid_raw_request(struct hid_device *hid, unsigned char reportnum,
1232 __u8 *buf, size_t len, unsigned char rtype,
1236 case HID_REQ_GET_REPORT:
1237 return usbhid_get_raw_report(hid, reportnum, buf, len, rtype);
1238 case HID_REQ_SET_REPORT:
1239 return usbhid_set_raw_report(hid, reportnum, buf, len, rtype);
1245 static int usbhid_idle(struct hid_device *hid, int report, int idle,
1248 struct usb_device *dev = hid_to_usb_dev(hid);
1249 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1250 struct usb_host_interface *interface = intf->cur_altsetting;
1251 int ifnum = interface->desc.bInterfaceNumber;
1253 if (reqtype != HID_REQ_SET_IDLE)
1256 return hid_set_idle(dev, ifnum, report, idle);
1259 static struct hid_ll_driver usb_hid_driver = {
1260 .parse = usbhid_parse,
1261 .start = usbhid_start,
1262 .stop = usbhid_stop,
1263 .open = usbhid_open,
1264 .close = usbhid_close,
1265 .power = usbhid_power,
1266 .request = usbhid_request,
1267 .wait = usbhid_wait_io,
1268 .raw_request = usbhid_raw_request,
1269 .output_report = usbhid_output_report,
1270 .idle = usbhid_idle,
1273 static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1275 struct usb_host_interface *interface = intf->cur_altsetting;
1276 struct usb_device *dev = interface_to_usbdev(intf);
1277 struct usbhid_device *usbhid;
1278 struct hid_device *hid;
1279 unsigned int n, has_in = 0;
1283 dbg_hid("HID probe called for ifnum %d\n",
1284 intf->altsetting->desc.bInterfaceNumber);
1286 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1287 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1290 hid_err(intf, "couldn't find an input interrupt endpoint\n");
1294 hid = hid_allocate_device();
1296 return PTR_ERR(hid);
1298 usb_set_intfdata(intf, hid);
1299 hid->ll_driver = &usb_hid_driver;
1300 hid->ff_init = hid_pidff_init;
1301 #ifdef CONFIG_USB_HIDDEV
1302 hid->hiddev_connect = hiddev_connect;
1303 hid->hiddev_disconnect = hiddev_disconnect;
1304 hid->hiddev_hid_event = hiddev_hid_event;
1305 hid->hiddev_report_event = hiddev_report_event;
1307 hid->dev.parent = &intf->dev;
1309 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1310 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1312 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
1313 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1314 USB_INTERFACE_PROTOCOL_MOUSE)
1315 hid->type = HID_TYPE_USBMOUSE;
1316 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1317 hid->type = HID_TYPE_USBNONE;
1319 if (dev->manufacturer)
1320 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1323 if (dev->manufacturer)
1324 strlcat(hid->name, " ", sizeof(hid->name));
1325 strlcat(hid->name, dev->product, sizeof(hid->name));
1328 if (!strlen(hid->name))
1329 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1330 le16_to_cpu(dev->descriptor.idVendor),
1331 le16_to_cpu(dev->descriptor.idProduct));
1333 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1334 strlcat(hid->phys, "/input", sizeof(hid->phys));
1335 len = strlen(hid->phys);
1336 if (len < sizeof(hid->phys) - 1)
1337 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1338 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1340 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1343 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1344 if (usbhid == NULL) {
1349 hid->driver_data = usbhid;
1351 usbhid->intf = intf;
1352 usbhid->ifnum = interface->desc.bInterfaceNumber;
1354 init_waitqueue_head(&usbhid->wait);
1355 INIT_WORK(&usbhid->reset_work, hid_reset);
1356 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1357 spin_lock_init(&usbhid->lock);
1359 ret = hid_add_device(hid);
1362 hid_err(intf, "can't add hid device: %d\n", ret);
1370 hid_destroy_device(hid);
1374 static void usbhid_disconnect(struct usb_interface *intf)
1376 struct hid_device *hid = usb_get_intfdata(intf);
1377 struct usbhid_device *usbhid;
1382 usbhid = hid->driver_data;
1383 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1384 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1385 spin_unlock_irq(&usbhid->lock);
1386 hid_destroy_device(hid);
1390 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1392 del_timer_sync(&usbhid->io_retry);
1393 cancel_work_sync(&usbhid->reset_work);
1396 static void hid_cease_io(struct usbhid_device *usbhid)
1398 del_timer_sync(&usbhid->io_retry);
1399 usb_kill_urb(usbhid->urbin);
1400 usb_kill_urb(usbhid->urbctrl);
1401 usb_kill_urb(usbhid->urbout);
1404 static void hid_restart_io(struct hid_device *hid)
1406 struct usbhid_device *usbhid = hid->driver_data;
1407 int clear_halt = test_bit(HID_CLEAR_HALT, &usbhid->iofl);
1408 int reset_pending = test_bit(HID_RESET_PENDING, &usbhid->iofl);
1410 spin_lock_irq(&usbhid->lock);
1411 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1412 usbhid_mark_busy(usbhid);
1414 if (clear_halt || reset_pending)
1415 schedule_work(&usbhid->reset_work);
1416 usbhid->retry_delay = 0;
1417 spin_unlock_irq(&usbhid->lock);
1419 if (reset_pending || !test_bit(HID_STARTED, &usbhid->iofl))
1423 if (hid_start_in(hid) < 0)
1427 spin_lock_irq(&usbhid->lock);
1428 if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
1429 usbhid_restart_out_queue(usbhid);
1430 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
1431 usbhid_restart_ctrl_queue(usbhid);
1432 spin_unlock_irq(&usbhid->lock);
1435 /* Treat USB reset pretty much the same as suspend/resume */
1436 static int hid_pre_reset(struct usb_interface *intf)
1438 struct hid_device *hid = usb_get_intfdata(intf);
1439 struct usbhid_device *usbhid = hid->driver_data;
1441 spin_lock_irq(&usbhid->lock);
1442 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1443 spin_unlock_irq(&usbhid->lock);
1444 hid_cease_io(usbhid);
1449 /* Same routine used for post_reset and reset_resume */
1450 static int hid_post_reset(struct usb_interface *intf)
1452 struct usb_device *dev = interface_to_usbdev (intf);
1453 struct hid_device *hid = usb_get_intfdata(intf);
1454 struct usbhid_device *usbhid = hid->driver_data;
1455 struct usb_host_interface *interface = intf->cur_altsetting;
1459 /* Fetch and examine the HID report descriptor. If this
1460 * has changed, then rebind. Since usbcore's check of the
1461 * configuration descriptors passed, we already know that
1462 * the size of the HID report descriptor has not changed.
1464 rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
1468 status = hid_get_class_descriptor(dev,
1469 interface->desc.bInterfaceNumber,
1470 HID_DT_REPORT, rdesc, hid->dev_rsize);
1472 dbg_hid("reading report descriptor failed (post_reset)\n");
1476 status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
1479 dbg_hid("report descriptor changed\n");
1483 /* No need to do another reset or clear a halted endpoint */
1484 spin_lock_irq(&usbhid->lock);
1485 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1486 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
1487 spin_unlock_irq(&usbhid->lock);
1488 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1490 hid_restart_io(hid);
1495 int usbhid_get_power(struct hid_device *hid)
1497 struct usbhid_device *usbhid = hid->driver_data;
1499 return usb_autopm_get_interface(usbhid->intf);
1502 void usbhid_put_power(struct hid_device *hid)
1504 struct usbhid_device *usbhid = hid->driver_data;
1506 usb_autopm_put_interface(usbhid->intf);
1511 static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1515 hid_restart_io(hid);
1516 if (driver_suspended && hid->driver && hid->driver->resume)
1517 status = hid->driver->resume(hid);
1521 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1523 struct hid_device *hid = usb_get_intfdata(intf);
1524 struct usbhid_device *usbhid = hid->driver_data;
1526 bool driver_suspended = false;
1527 unsigned int ledcount;
1529 if (PMSG_IS_AUTO(message)) {
1530 ledcount = hidinput_count_leds(hid);
1531 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1532 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1533 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1534 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1535 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1536 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1537 && (!ledcount || ignoreled))
1539 set_bit(HID_SUSPENDED, &usbhid->iofl);
1540 spin_unlock_irq(&usbhid->lock);
1541 if (hid->driver && hid->driver->suspend) {
1542 status = hid->driver->suspend(hid, message);
1546 driver_suspended = true;
1548 usbhid_mark_busy(usbhid);
1549 spin_unlock_irq(&usbhid->lock);
1554 /* TODO: resume() might need to handle suspend failure */
1555 if (hid->driver && hid->driver->suspend)
1556 status = hid->driver->suspend(hid, message);
1557 driver_suspended = true;
1558 spin_lock_irq(&usbhid->lock);
1559 set_bit(HID_SUSPENDED, &usbhid->iofl);
1560 spin_unlock_irq(&usbhid->lock);
1561 if (usbhid_wait_io(hid) < 0)
1565 hid_cancel_delayed_stuff(usbhid);
1566 hid_cease_io(usbhid);
1568 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1569 /* lost race against keypresses */
1573 dev_dbg(&intf->dev, "suspend\n");
1577 hid_resume_common(hid, driver_suspended);
1581 static int hid_resume(struct usb_interface *intf)
1583 struct hid_device *hid = usb_get_intfdata (intf);
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);
1596 status = hid_post_reset(intf);
1597 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1598 int ret = hid->driver->reset_resume(hid);
1605 #endif /* CONFIG_PM */
1607 static const struct usb_device_id hid_usb_ids[] = {
1608 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1609 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1610 { } /* Terminating entry */
1613 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1615 static struct usb_driver hid_driver = {
1617 .probe = usbhid_probe,
1618 .disconnect = usbhid_disconnect,
1620 .suspend = hid_suspend,
1621 .resume = hid_resume,
1622 .reset_resume = hid_reset_resume,
1624 .pre_reset = hid_pre_reset,
1625 .post_reset = hid_post_reset,
1626 .id_table = hid_usb_ids,
1627 .supports_autosuspend = 1,
1630 struct usb_interface *usbhid_find_interface(int minor)
1632 return usb_find_interface(&hid_driver, minor);
1635 static int __init hid_init(void)
1637 int retval = -ENOMEM;
1639 retval = usbhid_quirks_init(quirks_param);
1641 goto usbhid_quirks_init_fail;
1642 retval = usb_register(&hid_driver);
1644 goto usb_register_fail;
1645 pr_info(KBUILD_MODNAME ": " DRIVER_DESC "\n");
1649 usbhid_quirks_exit();
1650 usbhid_quirks_init_fail:
1654 static void __exit hid_exit(void)
1656 usb_deregister(&hid_driver);
1657 usbhid_quirks_exit();
1660 module_init(hid_init);
1661 module_exit(hid_exit);
1663 MODULE_AUTHOR("Andreas Gal");
1664 MODULE_AUTHOR("Vojtech Pavlik");
1665 MODULE_AUTHOR("Jiri Kosina");
1666 MODULE_DESCRIPTION(DRIVER_DESC);
1667 MODULE_LICENSE("GPL");