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
9 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock.h>
27 #include <asm/unaligned.h>
28 #include <asm/byteorder.h>
29 #include <linux/input.h>
30 #include <linux/wait.h>
31 #include <linux/workqueue.h>
32 #include <linux/string.h>
34 #include <linux/usb.h>
36 #include <linux/hid.h>
37 #include <linux/hiddev.h>
38 #include <linux/hid-debug.h>
39 #include <linux/hidraw.h>
46 #define DRIVER_DESC "USB HID core driver"
47 #define DRIVER_LICENSE "GPL"
53 static unsigned int hid_mousepoll_interval;
54 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
55 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
57 static unsigned int ignoreled;
58 module_param_named(ignoreled, ignoreled, uint, 0644);
59 MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
61 /* Quirks specified at module load time */
62 static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
63 module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
64 MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
65 " quirks=vendorID:productID:quirks"
66 " where vendorID, productID, and quirks are all in"
69 * Input submission and I/O error handler.
71 static DEFINE_MUTEX(hid_open_mut);
73 static void hid_io_error(struct hid_device *hid);
74 static int hid_submit_out(struct hid_device *hid);
75 static int hid_submit_ctrl(struct hid_device *hid);
76 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
78 /* Start up the input URB */
79 static int hid_start_in(struct hid_device *hid)
83 struct usbhid_device *usbhid = hid->driver_data;
85 spin_lock_irqsave(&usbhid->lock, flags);
87 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
88 !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
89 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
90 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
92 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
94 set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
96 clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
99 spin_unlock_irqrestore(&usbhid->lock, flags);
103 /* I/O retry timer routine */
104 static void hid_retry_timeout(unsigned long _hid)
106 struct hid_device *hid = (struct hid_device *) _hid;
107 struct usbhid_device *usbhid = hid->driver_data;
109 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
110 if (hid_start_in(hid))
114 /* Workqueue routine to reset the device or clear a halt */
115 static void hid_reset(struct work_struct *work)
117 struct usbhid_device *usbhid =
118 container_of(work, struct usbhid_device, reset_work);
119 struct hid_device *hid = usbhid->hid;
122 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
123 dev_dbg(&usbhid->intf->dev, "clear halt\n");
124 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
125 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
129 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
130 dev_dbg(&usbhid->intf->dev, "resetting device\n");
131 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
133 rc = usb_reset_device(hid_to_usb_dev(hid));
134 usb_unlock_device(hid_to_usb_dev(hid));
136 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
141 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
145 hid_err(hid, "can't reset device, %s-%s/input%d, status %d\n",
146 hid_to_usb_dev(hid)->bus->bus_name,
147 hid_to_usb_dev(hid)->devpath,
157 /* Main I/O error handler */
158 static void hid_io_error(struct hid_device *hid)
161 struct usbhid_device *usbhid = hid->driver_data;
163 spin_lock_irqsave(&usbhid->lock, flags);
165 /* Stop when disconnected */
166 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
169 /* If it has been a while since the last error, we'll assume
170 * this a brand new error and reset the retry timeout. */
171 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
172 usbhid->retry_delay = 0;
174 /* When an error occurs, retry at increasing intervals */
175 if (usbhid->retry_delay == 0) {
176 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
177 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
178 } else if (usbhid->retry_delay < 100)
179 usbhid->retry_delay *= 2;
181 if (time_after(jiffies, usbhid->stop_retry)) {
183 /* Retries failed, so do a port reset unless we lack bandwidth*/
184 if (test_bit(HID_NO_BANDWIDTH, &usbhid->iofl)
185 && !test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
187 schedule_work(&usbhid->reset_work);
192 mod_timer(&usbhid->io_retry,
193 jiffies + msecs_to_jiffies(usbhid->retry_delay));
195 spin_unlock_irqrestore(&usbhid->lock, flags);
198 static void usbhid_mark_busy(struct usbhid_device *usbhid)
200 struct usb_interface *intf = usbhid->intf;
202 usb_mark_last_busy(interface_to_usbdev(intf));
205 static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
207 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
211 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
212 test_bit(HID_SUSPENDED, &usbhid->iofl))
215 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
216 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
218 /* Try to wake up from autosuspend... */
219 r = usb_autopm_get_interface_async(usbhid->intf);
224 * If still suspended, don't submit. Submission will
225 * occur if/when resume drains the queue.
227 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
228 usb_autopm_put_interface_no_suspend(usbhid->intf);
232 /* Asynchronously flush queue. */
233 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
234 if (hid_submit_out(hid)) {
235 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
236 usb_autopm_put_interface_async(usbhid->intf);
238 wake_up(&usbhid->wait);
243 static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
245 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
249 WARN_ON(hid == NULL);
250 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
251 test_bit(HID_SUSPENDED, &usbhid->iofl))
254 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
255 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
257 /* Try to wake up from autosuspend... */
258 r = usb_autopm_get_interface_async(usbhid->intf);
263 * If still suspended, don't submit. Submission will
264 * occur if/when resume drains the queue.
266 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
267 usb_autopm_put_interface_no_suspend(usbhid->intf);
271 /* Asynchronously flush queue. */
272 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
273 if (hid_submit_ctrl(hid)) {
274 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
275 usb_autopm_put_interface_async(usbhid->intf);
277 wake_up(&usbhid->wait);
283 * Input interrupt completion handler.
286 static void hid_irq_in(struct urb *urb)
288 struct hid_device *hid = urb->context;
289 struct usbhid_device *usbhid = hid->driver_data;
292 switch (urb->status) {
293 case 0: /* success */
294 usbhid_mark_busy(usbhid);
295 usbhid->retry_delay = 0;
296 hid_input_report(urb->context, HID_INPUT_REPORT,
297 urb->transfer_buffer,
298 urb->actual_length, 1);
300 * autosuspend refused while keys are pressed
301 * because most keyboards don't wake up when
304 if (hid_check_keys_pressed(hid))
305 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
307 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
309 case -EPIPE: /* stall */
310 usbhid_mark_busy(usbhid);
311 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
312 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
313 schedule_work(&usbhid->reset_work);
315 case -ECONNRESET: /* unlink */
317 case -ESHUTDOWN: /* unplug */
318 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
320 case -EILSEQ: /* protocol error or unplug */
321 case -EPROTO: /* protocol error or unplug */
322 case -ETIME: /* protocol error or unplug */
323 case -ETIMEDOUT: /* Should never happen, but... */
324 usbhid_mark_busy(usbhid);
325 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
329 hid_warn(urb->dev, "input irq status %d received\n",
333 status = usb_submit_urb(urb, GFP_ATOMIC);
335 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
336 if (status != -EPERM) {
337 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
338 hid_to_usb_dev(hid)->bus->bus_name,
339 hid_to_usb_dev(hid)->devpath,
340 usbhid->ifnum, status);
346 static int hid_submit_out(struct hid_device *hid)
348 struct hid_report *report;
350 struct usbhid_device *usbhid = hid->driver_data;
353 report = usbhid->out[usbhid->outtail].report;
354 raw_report = usbhid->out[usbhid->outtail].raw_report;
356 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
357 1 + (report->id > 0);
358 usbhid->urbout->dev = hid_to_usb_dev(hid);
360 memcpy(usbhid->outbuf, raw_report,
361 usbhid->urbout->transfer_buffer_length);
363 usbhid->out[usbhid->outtail].raw_report = NULL;
366 dbg_hid("submitting out urb\n");
368 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
370 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
373 usbhid->last_out = jiffies;
377 static int hid_submit_ctrl(struct hid_device *hid)
379 struct hid_report *report;
383 struct usbhid_device *usbhid = hid->driver_data;
385 report = usbhid->ctrl[usbhid->ctrltail].report;
386 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
387 dir = usbhid->ctrl[usbhid->ctrltail].dir;
389 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
390 if (dir == USB_DIR_OUT) {
391 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
392 usbhid->urbctrl->transfer_buffer_length = len;
394 memcpy(usbhid->ctrlbuf, raw_report, len);
396 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
399 int maxpacket, padlen;
401 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
402 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
403 usbhid->urbctrl->pipe, 0);
405 padlen = DIV_ROUND_UP(len, maxpacket);
407 if (padlen > usbhid->bufsize)
408 padlen = usbhid->bufsize;
411 usbhid->urbctrl->transfer_buffer_length = padlen;
413 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
415 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
416 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
418 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
420 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
421 usbhid->cr->wLength = cpu_to_le16(len);
423 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
424 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
426 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
428 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
430 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
433 usbhid->last_ctrl = jiffies;
438 * Output interrupt completion handler.
441 static void hid_irq_out(struct urb *urb)
443 struct hid_device *hid = urb->context;
444 struct usbhid_device *usbhid = hid->driver_data;
448 switch (urb->status) {
449 case 0: /* success */
451 case -ESHUTDOWN: /* unplug */
453 case -EILSEQ: /* protocol error or unplug */
454 case -EPROTO: /* protocol error or unplug */
455 case -ECONNRESET: /* unlink */
459 hid_warn(urb->dev, "output irq status %d received\n",
463 spin_lock_irqsave(&usbhid->lock, flags);
466 usbhid->outtail = usbhid->outhead;
468 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
470 if (usbhid->outhead != usbhid->outtail &&
471 hid_submit_out(hid) == 0) {
472 /* Successfully submitted next urb in queue */
473 spin_unlock_irqrestore(&usbhid->lock, flags);
478 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
479 spin_unlock_irqrestore(&usbhid->lock, flags);
480 usb_autopm_put_interface_async(usbhid->intf);
481 wake_up(&usbhid->wait);
485 * Control pipe completion handler.
488 static void hid_ctrl(struct urb *urb)
490 struct hid_device *hid = urb->context;
491 struct usbhid_device *usbhid = hid->driver_data;
492 int unplug = 0, status = urb->status;
494 spin_lock(&usbhid->lock);
497 case 0: /* success */
498 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
499 hid_input_report(urb->context,
500 usbhid->ctrl[usbhid->ctrltail].report->type,
501 urb->transfer_buffer, urb->actual_length, 0);
503 case -ESHUTDOWN: /* unplug */
505 case -EILSEQ: /* protocol error or unplug */
506 case -EPROTO: /* protocol error or unplug */
507 case -ECONNRESET: /* unlink */
509 case -EPIPE: /* report not available */
512 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
516 usbhid->ctrltail = usbhid->ctrlhead;
518 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
520 if (usbhid->ctrlhead != usbhid->ctrltail &&
521 hid_submit_ctrl(hid) == 0) {
522 /* Successfully submitted next urb in queue */
523 spin_unlock(&usbhid->lock);
528 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
529 spin_unlock(&usbhid->lock);
530 usb_autopm_put_interface_async(usbhid->intf);
531 wake_up(&usbhid->wait);
534 static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
538 struct usbhid_device *usbhid = hid->driver_data;
540 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
543 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
544 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
545 hid_warn(hid, "output queue full\n");
549 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
550 if (!usbhid->out[usbhid->outhead].raw_report) {
551 hid_warn(hid, "output queueing failed\n");
554 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
555 usbhid->out[usbhid->outhead].report = report;
556 usbhid->outhead = head;
558 /* If the queue isn't running, restart it */
559 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
560 usbhid_restart_out_queue(usbhid);
562 /* Otherwise see if an earlier request has timed out */
563 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
565 /* Prevent autosuspend following the unlink */
566 usb_autopm_get_interface_no_resume(usbhid->intf);
569 * Prevent resubmission in case the URB completes
570 * before we can unlink it. We don't want to cancel
571 * the wrong transfer!
573 usb_block_urb(usbhid->urbout);
575 /* Drop lock to avoid deadlock if the callback runs */
576 spin_unlock(&usbhid->lock);
578 usb_unlink_urb(usbhid->urbout);
579 spin_lock(&usbhid->lock);
580 usb_unblock_urb(usbhid->urbout);
582 /* Unlink might have stopped the queue */
583 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
584 usbhid_restart_out_queue(usbhid);
586 /* Now we can allow autosuspend again */
587 usb_autopm_put_interface_async(usbhid->intf);
592 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
593 hid_warn(hid, "control queue full\n");
597 if (dir == USB_DIR_OUT) {
598 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
599 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
600 hid_warn(hid, "control queueing failed\n");
603 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
605 usbhid->ctrl[usbhid->ctrlhead].report = report;
606 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
607 usbhid->ctrlhead = head;
609 /* If the queue isn't running, restart it */
610 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
611 usbhid_restart_ctrl_queue(usbhid);
613 /* Otherwise see if an earlier request has timed out */
614 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
616 /* Prevent autosuspend following the unlink */
617 usb_autopm_get_interface_no_resume(usbhid->intf);
620 * Prevent resubmission in case the URB completes
621 * before we can unlink it. We don't want to cancel
622 * the wrong transfer!
624 usb_block_urb(usbhid->urbctrl);
626 /* Drop lock to avoid deadlock if the callback runs */
627 spin_unlock(&usbhid->lock);
629 usb_unlink_urb(usbhid->urbctrl);
630 spin_lock(&usbhid->lock);
631 usb_unblock_urb(usbhid->urbctrl);
633 /* Unlink might have stopped the queue */
634 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
635 usbhid_restart_ctrl_queue(usbhid);
637 /* Now we can allow autosuspend again */
638 usb_autopm_put_interface_async(usbhid->intf);
642 static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
644 struct usbhid_device *usbhid = hid->driver_data;
647 spin_lock_irqsave(&usbhid->lock, flags);
648 __usbhid_submit_report(hid, report, dir);
649 spin_unlock_irqrestore(&usbhid->lock, flags);
652 /* Workqueue routine to send requests to change LEDs */
653 static void hid_led(struct work_struct *work)
655 struct usbhid_device *usbhid =
656 container_of(work, struct usbhid_device, led_work);
657 struct hid_device *hid = usbhid->hid;
658 struct hid_field *field;
661 field = hidinput_get_led_field(hid);
663 hid_warn(hid, "LED event field not found\n");
667 spin_lock_irqsave(&usbhid->lock, flags);
668 if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
669 usbhid->ledcount = hidinput_count_leds(hid);
670 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
671 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
673 spin_unlock_irqrestore(&usbhid->lock, flags);
676 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
678 struct hid_device *hid = input_get_drvdata(dev);
679 struct usbhid_device *usbhid = hid->driver_data;
680 struct hid_field *field;
685 return input_ff_event(dev, type, code, value);
690 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
691 hid_warn(dev, "event field not found\n");
695 spin_lock_irqsave(&usbhid->lock, flags);
696 hid_set_field(field, offset, value);
697 spin_unlock_irqrestore(&usbhid->lock, flags);
700 * Defer performing requested LED action.
701 * This is more likely gather all LED changes into a single URB.
703 schedule_work(&usbhid->led_work);
708 static int usbhid_wait_io(struct hid_device *hid)
710 struct usbhid_device *usbhid = hid->driver_data;
712 if (!wait_event_timeout(usbhid->wait,
713 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
714 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
716 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
723 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
725 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
726 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
727 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
730 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
731 unsigned char type, void *buf, int size)
733 int result, retries = 4;
735 memset(buf, 0, size);
738 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
739 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
740 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
742 } while (result < size && retries);
746 int usbhid_open(struct hid_device *hid)
748 struct usbhid_device *usbhid = hid->driver_data;
751 mutex_lock(&hid_open_mut);
753 res = usb_autopm_get_interface(usbhid->intf);
754 /* the device must be awake to reliably request remote wakeup */
760 usbhid->intf->needs_remote_wakeup = 1;
761 res = hid_start_in(hid);
763 if (res != -ENOSPC) {
767 /* no use opening if resources are insufficient */
770 usbhid->intf->needs_remote_wakeup = 0;
773 usb_autopm_put_interface(usbhid->intf);
776 mutex_unlock(&hid_open_mut);
780 void usbhid_close(struct hid_device *hid)
782 struct usbhid_device *usbhid = hid->driver_data;
784 mutex_lock(&hid_open_mut);
786 /* protecting hid->open to make sure we don't restart
787 * data acquistion due to a resumption we no longer
790 spin_lock_irq(&usbhid->lock);
792 spin_unlock_irq(&usbhid->lock);
793 hid_cancel_delayed_stuff(usbhid);
794 usb_kill_urb(usbhid->urbin);
795 usbhid->intf->needs_remote_wakeup = 0;
797 spin_unlock_irq(&usbhid->lock);
799 mutex_unlock(&hid_open_mut);
803 * Initialize all reports
806 void usbhid_init_reports(struct hid_device *hid)
808 struct hid_report *report;
809 struct usbhid_device *usbhid = hid->driver_data;
812 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
813 usbhid_submit_report(hid, report, USB_DIR_IN);
815 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
816 usbhid_submit_report(hid, report, USB_DIR_IN);
819 ret = usbhid_wait_io(hid);
822 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
823 usb_kill_urb(usbhid->urbctrl);
824 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
825 usb_kill_urb(usbhid->urbout);
826 ret = usbhid_wait_io(hid);
830 hid_warn(hid, "timeout initializing reports\n");
834 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
836 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
837 unsigned int hid_code, struct hid_field **pfield)
839 struct hid_report *report;
840 struct hid_field *field;
841 struct hid_usage *usage;
844 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
845 for (i = 0; i < report->maxfield; i++) {
846 field = report->field[i];
847 for (j = 0; j < field->maxusage; j++) {
848 usage = &field->usage[j];
849 if ((usage->hid & HID_USAGE_PAGE) == page &&
850 (usage->hid & 0xFFFF) == hid_code) {
860 void usbhid_set_leds(struct hid_device *hid)
862 struct hid_field *field;
865 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
866 hid_set_field(field, offset, 0);
867 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
870 EXPORT_SYMBOL_GPL(usbhid_set_leds);
873 * Traverse the supplied list of reports and find the longest
875 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
878 struct hid_report *report;
881 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
882 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
888 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
890 struct usbhid_device *usbhid = hid->driver_data;
892 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
894 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
895 &usbhid->outbuf_dma);
896 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
897 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
898 &usbhid->ctrlbuf_dma);
899 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
906 static int usbhid_get_raw_report(struct hid_device *hid,
907 unsigned char report_number, __u8 *buf, size_t count,
908 unsigned char report_type)
910 struct usbhid_device *usbhid = hid->driver_data;
911 struct usb_device *dev;
912 struct usb_interface *intf;
913 struct usb_host_interface *interface;
914 int skipped_report_id = 0;
919 pr_err("%s: no USB intf\n", __func__);
922 spin_lock_irq(&usbhid->lock);
923 if (test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
924 pr_err("hid device disconnected\n");
925 spin_unlock_irq(&usbhid->lock);
928 spin_unlock_irq(&usbhid->lock);
930 dev = hid_to_usb_dev(hid);
931 interface = intf->cur_altsetting;
933 /* Byte 0 is the report number. Report data starts at byte 1.*/
934 buf[0] = report_number;
935 if (report_number == 0x0) {
936 /* Offset the return buffer by 1, so that the report ID
937 will remain in byte 0. */
940 skipped_report_id = 1;
942 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
944 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
945 ((report_type + 1) << 8) | report_number,
946 interface->desc.bInterfaceNumber, buf, count,
947 USB_CTRL_SET_TIMEOUT);
949 /* count also the report id */
950 if (ret > 0 && skipped_report_id)
956 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
957 unsigned char report_type)
959 struct usbhid_device *usbhid = hid->driver_data;
960 struct usb_device *dev = hid_to_usb_dev(hid);
961 struct usb_interface *intf = usbhid->intf;
962 struct usb_host_interface *interface = intf->cur_altsetting;
965 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
967 int skipped_report_id = 0;
970 /* Don't send the Report ID */
973 skipped_report_id = 1;
975 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
976 buf, count, &actual_length,
977 USB_CTRL_SET_TIMEOUT);
978 /* return the number of bytes transferred */
981 /* count also the report id */
982 if (skipped_report_id)
986 int skipped_report_id = 0;
987 int report_id = buf[0];
989 /* Don't send the Report ID */
992 skipped_report_id = 1;
994 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
996 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
997 ((report_type + 1) << 8) | report_id,
998 interface->desc.bInterfaceNumber, buf, count,
999 USB_CTRL_SET_TIMEOUT);
1000 /* count also the report id, if this was a numbered report. */
1001 if (ret > 0 && skipped_report_id)
1008 static void usbhid_restart_queues(struct usbhid_device *usbhid)
1010 if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
1011 usbhid_restart_out_queue(usbhid);
1012 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
1013 usbhid_restart_ctrl_queue(usbhid);
1016 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1018 struct usbhid_device *usbhid = hid->driver_data;
1020 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1021 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
1023 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
1026 static int usbhid_parse(struct hid_device *hid)
1028 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1029 struct usb_host_interface *interface = intf->cur_altsetting;
1030 struct usb_device *dev = interface_to_usbdev (intf);
1031 struct hid_descriptor *hdesc;
1033 unsigned int rsize = 0;
1037 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
1038 le16_to_cpu(dev->descriptor.idProduct));
1040 if (quirks & HID_QUIRK_IGNORE)
1043 /* Many keyboards and mice don't like to be polled for reports,
1044 * so we will always set the HID_QUIRK_NOGET flag for them. */
1045 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1046 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1047 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1048 quirks |= HID_QUIRK_NOGET;
1051 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1052 (!interface->desc.bNumEndpoints ||
1053 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1054 dbg_hid("class descriptor not present\n");
1058 hid->version = le16_to_cpu(hdesc->bcdHID);
1059 hid->country = hdesc->bCountryCode;
1061 for (n = 0; n < hdesc->bNumDescriptors; n++)
1062 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1063 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1065 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1066 dbg_hid("weird size of report descriptor (%u)\n", rsize);
1070 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1071 dbg_hid("couldn't allocate rdesc memory\n");
1075 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1077 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1078 HID_DT_REPORT, rdesc, rsize);
1080 dbg_hid("reading report descriptor failed\n");
1085 ret = hid_parse_report(hid, rdesc, rsize);
1088 dbg_hid("parsing report descriptor failed\n");
1092 hid->quirks |= quirks;
1099 static int usbhid_start(struct hid_device *hid)
1101 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1102 struct usb_host_interface *interface = intf->cur_altsetting;
1103 struct usb_device *dev = interface_to_usbdev(intf);
1104 struct usbhid_device *usbhid = hid->driver_data;
1105 unsigned int n, insize = 0;
1108 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1110 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1111 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1112 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1113 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1115 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1116 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
1118 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1120 if (insize > HID_MAX_BUFFER_SIZE)
1121 insize = HID_MAX_BUFFER_SIZE;
1123 if (hid_alloc_buffers(dev, hid)) {
1128 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1129 struct usb_endpoint_descriptor *endpoint;
1133 endpoint = &interface->endpoint[n].desc;
1134 if (!usb_endpoint_xfer_int(endpoint))
1137 interval = endpoint->bInterval;
1139 /* Some vendors give fullspeed interval on highspeed devides */
1140 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
1141 dev->speed == USB_SPEED_HIGH) {
1142 interval = fls(endpoint->bInterval*8);
1143 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1144 hid->name, endpoint->bInterval, interval);
1147 /* Change the polling interval of mice. */
1148 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1149 interval = hid_mousepoll_interval;
1152 if (usb_endpoint_dir_in(endpoint)) {
1155 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1157 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1158 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1159 hid_irq_in, hid, interval);
1160 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1161 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1165 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1167 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1168 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1169 hid_irq_out, hid, interval);
1170 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1171 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1175 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1176 if (!usbhid->urbctrl) {
1181 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1182 usbhid->ctrlbuf, 1, hid_ctrl, hid);
1183 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1184 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1186 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1187 usbhid_init_reports(hid);
1189 set_bit(HID_STARTED, &usbhid->iofl);
1191 /* Some keyboards don't work until their LEDs have been set.
1192 * Since BIOSes do set the LEDs, it must be safe for any device
1193 * that supports the keyboard boot protocol.
1194 * In addition, enable remote wakeup by default for all keyboard
1195 * devices supporting the boot protocol.
1197 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1198 interface->desc.bInterfaceProtocol ==
1199 USB_INTERFACE_PROTOCOL_KEYBOARD) {
1200 usbhid_set_leds(hid);
1201 device_set_wakeup_enable(&dev->dev, 1);
1202 usb_disable_autosuspend(dev);
1204 #ifdef CONFIG_USB_EHCI_TEGRA
1205 else if (interface->desc.bInterfaceProtocol ==
1206 USB_INTERFACE_PROTOCOL_MOUSE)
1207 usb_disable_autosuspend(dev);
1212 usb_free_urb(usbhid->urbin);
1213 usb_free_urb(usbhid->urbout);
1214 usb_free_urb(usbhid->urbctrl);
1215 usbhid->urbin = NULL;
1216 usbhid->urbout = NULL;
1217 usbhid->urbctrl = NULL;
1218 hid_free_buffers(dev, hid);
1222 static void usbhid_stop(struct hid_device *hid)
1224 struct usbhid_device *usbhid = hid->driver_data;
1226 if (WARN_ON(!usbhid))
1229 clear_bit(HID_STARTED, &usbhid->iofl);
1230 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1231 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1232 spin_unlock_irq(&usbhid->lock);
1233 usb_kill_urb(usbhid->urbin);
1234 usb_kill_urb(usbhid->urbout);
1235 usb_kill_urb(usbhid->urbctrl);
1237 hid_cancel_delayed_stuff(usbhid);
1241 usb_free_urb(usbhid->urbin);
1242 usb_free_urb(usbhid->urbctrl);
1243 usb_free_urb(usbhid->urbout);
1244 usbhid->urbin = NULL; /* don't mess up next start */
1245 usbhid->urbctrl = NULL;
1246 usbhid->urbout = NULL;
1248 hid_free_buffers(hid_to_usb_dev(hid), hid);
1251 static int usbhid_power(struct hid_device *hid, int lvl)
1256 case PM_HINT_FULLON:
1257 r = usbhid_get_power(hid);
1259 case PM_HINT_NORMAL:
1260 usbhid_put_power(hid);
1266 static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1269 case HID_REQ_GET_REPORT:
1270 usbhid_submit_report(hid, rep, USB_DIR_IN);
1272 case HID_REQ_SET_REPORT:
1273 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1278 static int usbhid_idle(struct hid_device *hid, int report, int idle,
1281 struct usb_device *dev = hid_to_usb_dev(hid);
1282 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1283 struct usb_host_interface *interface = intf->cur_altsetting;
1284 int ifnum = interface->desc.bInterfaceNumber;
1286 if (reqtype != HID_REQ_SET_IDLE)
1289 return hid_set_idle(dev, ifnum, report, idle);
1292 static struct hid_ll_driver usb_hid_driver = {
1293 .parse = usbhid_parse,
1294 .start = usbhid_start,
1295 .stop = usbhid_stop,
1296 .open = usbhid_open,
1297 .close = usbhid_close,
1298 .power = usbhid_power,
1299 .hidinput_input_event = usb_hidinput_input_event,
1300 .request = usbhid_request,
1301 .wait = usbhid_wait_io,
1302 .idle = usbhid_idle,
1305 static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1307 struct usb_host_interface *interface = intf->cur_altsetting;
1308 struct usb_device *dev = interface_to_usbdev(intf);
1309 struct usbhid_device *usbhid;
1310 struct hid_device *hid;
1311 unsigned int n, has_in = 0;
1315 dbg_hid("HID probe called for ifnum %d\n",
1316 intf->altsetting->desc.bInterfaceNumber);
1318 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1319 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1322 hid_err(intf, "couldn't find an input interrupt endpoint\n");
1326 hid = hid_allocate_device();
1328 return PTR_ERR(hid);
1330 usb_set_intfdata(intf, hid);
1331 hid->ll_driver = &usb_hid_driver;
1332 hid->hid_get_raw_report = usbhid_get_raw_report;
1333 hid->hid_output_raw_report = usbhid_output_raw_report;
1334 hid->ff_init = hid_pidff_init;
1335 #ifdef CONFIG_USB_HIDDEV
1336 hid->hiddev_connect = hiddev_connect;
1337 hid->hiddev_disconnect = hiddev_disconnect;
1338 hid->hiddev_hid_event = hiddev_hid_event;
1339 hid->hiddev_report_event = hiddev_report_event;
1341 hid->dev.parent = &intf->dev;
1343 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1344 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1346 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
1347 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1348 USB_INTERFACE_PROTOCOL_MOUSE)
1349 hid->type = HID_TYPE_USBMOUSE;
1350 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1351 hid->type = HID_TYPE_USBNONE;
1353 if (dev->manufacturer)
1354 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1357 if (dev->manufacturer)
1358 strlcat(hid->name, " ", sizeof(hid->name));
1359 strlcat(hid->name, dev->product, sizeof(hid->name));
1362 if (!strlen(hid->name))
1363 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1364 le16_to_cpu(dev->descriptor.idVendor),
1365 le16_to_cpu(dev->descriptor.idProduct));
1367 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1368 strlcat(hid->phys, "/input", sizeof(hid->phys));
1369 len = strlen(hid->phys);
1370 if (len < sizeof(hid->phys) - 1)
1371 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1372 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1374 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1377 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1378 if (usbhid == NULL) {
1383 hid->driver_data = usbhid;
1385 usbhid->intf = intf;
1386 usbhid->ifnum = interface->desc.bInterfaceNumber;
1388 init_waitqueue_head(&usbhid->wait);
1389 INIT_WORK(&usbhid->reset_work, hid_reset);
1390 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1391 spin_lock_init(&usbhid->lock);
1393 INIT_WORK(&usbhid->led_work, hid_led);
1395 ret = hid_add_device(hid);
1398 hid_err(intf, "can't add hid device: %d\n", ret);
1406 hid_destroy_device(hid);
1410 static void usbhid_disconnect(struct usb_interface *intf)
1412 struct hid_device *hid = usb_get_intfdata(intf);
1413 struct usbhid_device *usbhid;
1418 usbhid = hid->driver_data;
1419 spin_lock_irq(&usbhid->lock);
1420 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1421 spin_unlock_irq(&usbhid->lock);
1423 hid_destroy_device(hid);
1427 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1429 del_timer_sync(&usbhid->io_retry);
1430 cancel_work_sync(&usbhid->reset_work);
1431 cancel_work_sync(&usbhid->led_work);
1434 static void hid_cease_io(struct usbhid_device *usbhid)
1436 del_timer_sync(&usbhid->io_retry);
1437 usb_kill_urb(usbhid->urbin);
1438 usb_kill_urb(usbhid->urbctrl);
1439 usb_kill_urb(usbhid->urbout);
1442 /* Treat USB reset pretty much the same as suspend/resume */
1443 static int hid_pre_reset(struct usb_interface *intf)
1445 struct hid_device *hid = usb_get_intfdata(intf);
1446 struct usbhid_device *usbhid = hid->driver_data;
1448 spin_lock_irq(&usbhid->lock);
1449 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1450 spin_unlock_irq(&usbhid->lock);
1451 hid_cease_io(usbhid);
1456 /* Same routine used for post_reset and reset_resume */
1457 static int hid_post_reset(struct usb_interface *intf)
1459 struct usb_device *dev = interface_to_usbdev (intf);
1460 struct hid_device *hid = usb_get_intfdata(intf);
1461 struct usbhid_device *usbhid = hid->driver_data;
1462 struct usb_host_interface *interface = intf->cur_altsetting;
1466 /* Fetch and examine the HID report descriptor. If this
1467 * has changed, then rebind. Since usbcore's check of the
1468 * configuration descriptors passed, we already know that
1469 * the size of the HID report descriptor has not changed.
1471 rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
1473 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1476 status = hid_get_class_descriptor(dev,
1477 interface->desc.bInterfaceNumber,
1478 HID_DT_REPORT, rdesc, hid->dev_rsize);
1480 dbg_hid("reading report descriptor failed (post_reset)\n");
1484 status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
1487 dbg_hid("report descriptor changed\n");
1491 spin_lock_irq(&usbhid->lock);
1492 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1493 spin_unlock_irq(&usbhid->lock);
1494 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1495 status = hid_start_in(hid);
1498 usbhid_restart_queues(usbhid);
1503 int usbhid_get_power(struct hid_device *hid)
1505 struct usbhid_device *usbhid = hid->driver_data;
1507 return usb_autopm_get_interface(usbhid->intf);
1510 void usbhid_put_power(struct hid_device *hid)
1512 struct usbhid_device *usbhid = hid->driver_data;
1514 usb_autopm_put_interface(usbhid->intf);
1519 static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1521 struct usbhid_device *usbhid = hid->driver_data;
1524 spin_lock_irq(&usbhid->lock);
1525 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1526 usbhid_mark_busy(usbhid);
1528 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1529 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1530 schedule_work(&usbhid->reset_work);
1531 usbhid->retry_delay = 0;
1533 usbhid_restart_queues(usbhid);
1534 spin_unlock_irq(&usbhid->lock);
1536 status = hid_start_in(hid);
1540 if (driver_suspended && hid->driver && hid->driver->resume)
1541 status = hid->driver->resume(hid);
1545 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1547 struct hid_device *hid = usb_get_intfdata(intf);
1548 struct usbhid_device *usbhid = hid->driver_data;
1550 bool driver_suspended = false;
1552 if (PMSG_IS_AUTO(message)) {
1553 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1554 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1555 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1556 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1557 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1558 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1559 && (!usbhid->ledcount || ignoreled))
1561 set_bit(HID_SUSPENDED, &usbhid->iofl);
1562 spin_unlock_irq(&usbhid->lock);
1563 if (hid->driver && hid->driver->suspend) {
1564 status = hid->driver->suspend(hid, message);
1568 driver_suspended = true;
1570 usbhid_mark_busy(usbhid);
1571 spin_unlock_irq(&usbhid->lock);
1576 /* TODO: resume() might need to handle suspend failure */
1577 if (hid->driver && hid->driver->suspend)
1578 status = hid->driver->suspend(hid, message);
1579 driver_suspended = true;
1580 spin_lock_irq(&usbhid->lock);
1581 set_bit(HID_SUSPENDED, &usbhid->iofl);
1582 spin_unlock_irq(&usbhid->lock);
1583 if (usbhid_wait_io(hid) < 0)
1587 hid_cancel_delayed_stuff(usbhid);
1588 hid_cease_io(usbhid);
1590 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1591 /* lost race against keypresses */
1595 dev_dbg(&intf->dev, "suspend\n");
1599 hid_resume_common(hid, driver_suspended);
1603 static int hid_resume(struct usb_interface *intf)
1605 struct hid_device *hid = usb_get_intfdata (intf);
1606 struct usbhid_device *usbhid = hid->driver_data;
1609 if (!test_bit(HID_STARTED, &usbhid->iofl))
1612 status = hid_resume_common(hid, true);
1613 dev_dbg(&intf->dev, "resume status %d\n", status);
1617 static int hid_reset_resume(struct usb_interface *intf)
1619 struct hid_device *hid = usb_get_intfdata(intf);
1620 struct usbhid_device *usbhid = hid->driver_data;
1623 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1624 status = hid_post_reset(intf);
1625 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1626 int ret = hid->driver->reset_resume(hid);
1633 #endif /* CONFIG_PM */
1635 static const struct usb_device_id hid_usb_ids[] = {
1636 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1637 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1638 { } /* Terminating entry */
1641 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1643 static struct usb_driver hid_driver = {
1645 .probe = usbhid_probe,
1646 .disconnect = usbhid_disconnect,
1648 .suspend = hid_suspend,
1649 .resume = hid_resume,
1650 .reset_resume = hid_reset_resume,
1652 .pre_reset = hid_pre_reset,
1653 .post_reset = hid_post_reset,
1654 .id_table = hid_usb_ids,
1655 .supports_autosuspend = 1,
1658 struct usb_interface *usbhid_find_interface(int minor)
1660 return usb_find_interface(&hid_driver, minor);
1663 static int __init hid_init(void)
1665 int retval = -ENOMEM;
1667 retval = usbhid_quirks_init(quirks_param);
1669 goto usbhid_quirks_init_fail;
1670 retval = usb_register(&hid_driver);
1672 goto usb_register_fail;
1673 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1677 usbhid_quirks_exit();
1678 usbhid_quirks_init_fail:
1682 static void __exit hid_exit(void)
1684 usb_deregister(&hid_driver);
1685 usbhid_quirks_exit();
1688 module_init(hid_init);
1689 module_exit(hid_exit);
1691 MODULE_AUTHOR("Andreas Gal");
1692 MODULE_AUTHOR("Vojtech Pavlik");
1693 MODULE_AUTHOR("Jiri Kosina");
1694 MODULE_DESCRIPTION(DRIVER_DESC);
1695 MODULE_LICENSE(DRIVER_LICENSE);