mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / hid / usbhid / hid-core.c
blobf623ef0f695d618cf4a416ddac133b1cd6c6401e
1 /*
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 */
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)
15 * any later version.
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>
23 #include <linux/mm.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>
39 #include "usbhid.h"
42 * Version Information
45 #define DRIVER_DESC "USB HID core driver"
46 #define DRIVER_LICENSE "GPL"
49 * Module parameters.
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 static unsigned int ignoreled;
57 module_param_named(ignoreled, ignoreled, uint, 0644);
58 MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
60 /* Quirks specified at module load time */
61 static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
62 module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
63 MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
64 " quirks=vendorID:productID:quirks"
65 " where vendorID, productID, and quirks are all in"
66 " 0x-prefixed hex");
68 * Input submission and I/O error handler.
70 static DEFINE_MUTEX(hid_open_mut);
72 static void hid_io_error(struct hid_device *hid);
73 static int hid_submit_out(struct hid_device *hid);
74 static int hid_submit_ctrl(struct hid_device *hid);
75 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
77 /* Start up the input URB */
78 static int hid_start_in(struct hid_device *hid)
80 unsigned long flags;
81 int rc = 0;
82 struct usbhid_device *usbhid = hid->driver_data;
84 spin_lock_irqsave(&usbhid->lock, flags);
85 if ((hid->open > 0 || hid->quirks & HID_QUIRK_ALWAYS_POLL) &&
86 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
87 !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
88 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
89 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
90 if (rc != 0) {
91 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
92 if (rc == -ENOSPC)
93 set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
94 } else {
95 clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
98 spin_unlock_irqrestore(&usbhid->lock, flags);
99 return rc;
102 /* I/O retry timer routine */
103 static void hid_retry_timeout(unsigned long _hid)
105 struct hid_device *hid = (struct hid_device *) _hid;
106 struct usbhid_device *usbhid = hid->driver_data;
108 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
109 if (hid_start_in(hid))
110 hid_io_error(hid);
113 /* Workqueue routine to reset the device or clear a halt */
114 static void hid_reset(struct work_struct *work)
116 struct usbhid_device *usbhid =
117 container_of(work, struct usbhid_device, reset_work);
118 struct hid_device *hid = usbhid->hid;
119 int rc = 0;
121 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
122 dev_dbg(&usbhid->intf->dev, "clear halt\n");
123 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
124 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
125 hid_start_in(hid);
128 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
129 dev_dbg(&usbhid->intf->dev, "resetting device\n");
130 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
131 if (rc == 0) {
132 rc = usb_reset_device(hid_to_usb_dev(hid));
133 usb_unlock_device(hid_to_usb_dev(hid));
135 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
138 switch (rc) {
139 case 0:
140 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
141 hid_io_error(hid);
142 break;
143 default:
144 hid_err(hid, "can't reset device, %s-%s/input%d, status %d\n",
145 hid_to_usb_dev(hid)->bus->bus_name,
146 hid_to_usb_dev(hid)->devpath,
147 usbhid->ifnum, rc);
148 /* FALLTHROUGH */
149 case -EHOSTUNREACH:
150 case -ENODEV:
151 case -EINTR:
152 break;
156 /* Main I/O error handler */
157 static void hid_io_error(struct hid_device *hid)
159 unsigned long flags;
160 struct usbhid_device *usbhid = hid->driver_data;
162 spin_lock_irqsave(&usbhid->lock, flags);
164 /* Stop when disconnected */
165 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
166 goto done;
168 /* If it has been a while since the last error, we'll assume
169 * this a brand new error and reset the retry timeout. */
170 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
171 usbhid->retry_delay = 0;
173 /* When an error occurs, retry at increasing intervals */
174 if (usbhid->retry_delay == 0) {
175 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
176 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
177 } else if (usbhid->retry_delay < 100)
178 usbhid->retry_delay *= 2;
180 if (time_after(jiffies, usbhid->stop_retry)) {
182 /* Retries failed, so do a port reset unless we lack bandwidth*/
183 if (!test_bit(HID_NO_BANDWIDTH, &usbhid->iofl)
184 && !test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
186 schedule_work(&usbhid->reset_work);
187 goto done;
191 mod_timer(&usbhid->io_retry,
192 jiffies + msecs_to_jiffies(usbhid->retry_delay));
193 done:
194 spin_unlock_irqrestore(&usbhid->lock, flags);
197 static void usbhid_mark_busy(struct usbhid_device *usbhid)
199 struct usb_interface *intf = usbhid->intf;
201 usb_mark_last_busy(interface_to_usbdev(intf));
204 static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
206 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
207 int kicked;
208 int r;
210 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
211 test_bit(HID_SUSPENDED, &usbhid->iofl))
212 return 0;
214 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
215 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
217 /* Try to wake up from autosuspend... */
218 r = usb_autopm_get_interface_async(usbhid->intf);
219 if (r < 0)
220 return r;
223 * If still suspended, don't submit. Submission will
224 * occur if/when resume drains the queue.
226 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
227 usb_autopm_put_interface_no_suspend(usbhid->intf);
228 return r;
231 /* Asynchronously flush queue. */
232 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
233 if (hid_submit_out(hid)) {
234 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
235 usb_autopm_put_interface_async(usbhid->intf);
237 wake_up(&usbhid->wait);
239 return kicked;
242 static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
244 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
245 int kicked;
246 int r;
248 WARN_ON(hid == NULL);
249 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
250 test_bit(HID_SUSPENDED, &usbhid->iofl))
251 return 0;
253 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
254 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
256 /* Try to wake up from autosuspend... */
257 r = usb_autopm_get_interface_async(usbhid->intf);
258 if (r < 0)
259 return r;
262 * If still suspended, don't submit. Submission will
263 * occur if/when resume drains the queue.
265 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
266 usb_autopm_put_interface_no_suspend(usbhid->intf);
267 return r;
270 /* Asynchronously flush queue. */
271 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
272 if (hid_submit_ctrl(hid)) {
273 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
274 usb_autopm_put_interface_async(usbhid->intf);
276 wake_up(&usbhid->wait);
278 return kicked;
282 * Input interrupt completion handler.
285 static void hid_irq_in(struct urb *urb)
287 struct hid_device *hid = urb->context;
288 struct usbhid_device *usbhid = hid->driver_data;
289 int status;
291 switch (urb->status) {
292 case 0: /* success */
293 usbhid_mark_busy(usbhid);
294 usbhid->retry_delay = 0;
295 if ((hid->quirks & HID_QUIRK_ALWAYS_POLL) && !hid->open)
296 break;
297 hid_input_report(urb->context, HID_INPUT_REPORT,
298 urb->transfer_buffer,
299 urb->actual_length, 1);
301 * autosuspend refused while keys are pressed
302 * because most keyboards don't wake up when
303 * a key is released
305 if (hid_check_keys_pressed(hid))
306 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
307 else
308 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
309 break;
310 case -EPIPE: /* stall */
311 usbhid_mark_busy(usbhid);
312 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
313 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
314 schedule_work(&usbhid->reset_work);
315 return;
316 case -ECONNRESET: /* unlink */
317 case -ENOENT:
318 case -ESHUTDOWN: /* unplug */
319 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
320 return;
321 case -EILSEQ: /* protocol error or unplug */
322 case -EPROTO: /* protocol error or unplug */
323 case -ETIME: /* protocol error or unplug */
324 case -ETIMEDOUT: /* Should never happen, but... */
325 usbhid_mark_busy(usbhid);
326 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
327 hid_io_error(hid);
328 return;
329 default: /* error */
330 hid_warn(urb->dev, "input irq status %d received\n",
331 urb->status);
334 status = usb_submit_urb(urb, GFP_ATOMIC);
335 if (status) {
336 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
337 if (status != -EPERM) {
338 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
339 hid_to_usb_dev(hid)->bus->bus_name,
340 hid_to_usb_dev(hid)->devpath,
341 usbhid->ifnum, status);
342 hid_io_error(hid);
347 static int hid_submit_out(struct hid_device *hid)
349 struct hid_report *report;
350 char *raw_report;
351 struct usbhid_device *usbhid = hid->driver_data;
352 int r;
354 report = usbhid->out[usbhid->outtail].report;
355 raw_report = usbhid->out[usbhid->outtail].raw_report;
357 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
358 1 + (report->id > 0);
359 usbhid->urbout->dev = hid_to_usb_dev(hid);
360 if (raw_report) {
361 memcpy(usbhid->outbuf, raw_report,
362 usbhid->urbout->transfer_buffer_length);
363 kfree(raw_report);
364 usbhid->out[usbhid->outtail].raw_report = NULL;
367 dbg_hid("submitting out urb\n");
369 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
370 if (r < 0) {
371 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
372 return r;
374 usbhid->last_out = jiffies;
375 return 0;
378 static int hid_submit_ctrl(struct hid_device *hid)
380 struct hid_report *report;
381 unsigned char dir;
382 char *raw_report;
383 int len, r;
384 struct usbhid_device *usbhid = hid->driver_data;
386 report = usbhid->ctrl[usbhid->ctrltail].report;
387 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
388 dir = usbhid->ctrl[usbhid->ctrltail].dir;
390 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
391 if (dir == USB_DIR_OUT) {
392 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
393 usbhid->urbctrl->transfer_buffer_length = len;
394 if (raw_report) {
395 memcpy(usbhid->ctrlbuf, raw_report, len);
396 kfree(raw_report);
397 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
399 } else {
400 int maxpacket, padlen;
402 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
403 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
404 usbhid->urbctrl->pipe, 0);
405 if (maxpacket > 0) {
406 padlen = DIV_ROUND_UP(len, maxpacket);
407 padlen *= maxpacket;
408 if (padlen > usbhid->bufsize)
409 padlen = usbhid->bufsize;
410 } else
411 padlen = 0;
412 usbhid->urbctrl->transfer_buffer_length = padlen;
414 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
416 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
417 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
418 HID_REQ_GET_REPORT;
419 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
420 report->id);
421 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
422 usbhid->cr->wLength = cpu_to_le16(len);
424 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
425 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
426 "Get_Report",
427 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
429 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
430 if (r < 0) {
431 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
432 return r;
434 usbhid->last_ctrl = jiffies;
435 return 0;
439 * Output interrupt completion handler.
442 static void hid_irq_out(struct urb *urb)
444 struct hid_device *hid = urb->context;
445 struct usbhid_device *usbhid = hid->driver_data;
446 unsigned long flags;
447 int unplug = 0;
449 switch (urb->status) {
450 case 0: /* success */
451 break;
452 case -ESHUTDOWN: /* unplug */
453 unplug = 1;
454 case -EILSEQ: /* protocol error or unplug */
455 case -EPROTO: /* protocol error or unplug */
456 case -ECONNRESET: /* unlink */
457 case -ENOENT:
458 break;
459 default: /* error */
460 hid_warn(urb->dev, "output irq status %d received\n",
461 urb->status);
464 spin_lock_irqsave(&usbhid->lock, flags);
466 if (unplug) {
467 usbhid->outtail = usbhid->outhead;
468 } else {
469 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
471 if (usbhid->outhead != usbhid->outtail &&
472 hid_submit_out(hid) == 0) {
473 /* Successfully submitted next urb in queue */
474 spin_unlock_irqrestore(&usbhid->lock, flags);
475 return;
479 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
480 spin_unlock_irqrestore(&usbhid->lock, flags);
481 usb_autopm_put_interface_async(usbhid->intf);
482 wake_up(&usbhid->wait);
486 * Control pipe completion handler.
489 static void hid_ctrl(struct urb *urb)
491 struct hid_device *hid = urb->context;
492 struct usbhid_device *usbhid = hid->driver_data;
493 int unplug = 0, status = urb->status;
495 switch (status) {
496 case 0: /* success */
497 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
498 hid_input_report(urb->context,
499 usbhid->ctrl[usbhid->ctrltail].report->type,
500 urb->transfer_buffer, urb->actual_length, 0);
501 break;
502 case -ESHUTDOWN: /* unplug */
503 unplug = 1;
504 case -EILSEQ: /* protocol error or unplug */
505 case -EPROTO: /* protocol error or unplug */
506 case -ECONNRESET: /* unlink */
507 case -ENOENT:
508 case -EPIPE: /* report not available */
509 break;
510 default: /* error */
511 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
514 spin_lock(&usbhid->lock);
516 if (unplug) {
517 usbhid->ctrltail = usbhid->ctrlhead;
518 } else {
519 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
521 if (usbhid->ctrlhead != usbhid->ctrltail &&
522 hid_submit_ctrl(hid) == 0) {
523 /* Successfully submitted next urb in queue */
524 spin_unlock(&usbhid->lock);
525 return;
529 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
530 spin_unlock(&usbhid->lock);
531 usb_autopm_put_interface_async(usbhid->intf);
532 wake_up(&usbhid->wait);
535 static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
536 unsigned char dir)
538 int head;
539 struct usbhid_device *usbhid = hid->driver_data;
541 if (((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) ||
542 test_bit(HID_DISCONNECTED, &usbhid->iofl))
543 return;
545 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
546 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
547 hid_warn(hid, "output queue full\n");
548 return;
551 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
552 if (!usbhid->out[usbhid->outhead].raw_report) {
553 hid_warn(hid, "output queueing failed\n");
554 return;
556 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
557 usbhid->out[usbhid->outhead].report = report;
558 usbhid->outhead = head;
560 /* If the queue isn't running, restart it */
561 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
562 usbhid_restart_out_queue(usbhid);
564 /* Otherwise see if an earlier request has timed out */
565 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
567 /* Prevent autosuspend following the unlink */
568 usb_autopm_get_interface_no_resume(usbhid->intf);
571 * Prevent resubmission in case the URB completes
572 * before we can unlink it. We don't want to cancel
573 * the wrong transfer!
575 usb_block_urb(usbhid->urbout);
577 /* Drop lock to avoid deadlock if the callback runs */
578 spin_unlock(&usbhid->lock);
580 usb_unlink_urb(usbhid->urbout);
581 spin_lock(&usbhid->lock);
582 usb_unblock_urb(usbhid->urbout);
584 /* Unlink might have stopped the queue */
585 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
586 usbhid_restart_out_queue(usbhid);
588 /* Now we can allow autosuspend again */
589 usb_autopm_put_interface_async(usbhid->intf);
591 return;
594 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
595 hid_warn(hid, "control queue full\n");
596 return;
599 if (dir == USB_DIR_OUT) {
600 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
601 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
602 hid_warn(hid, "control queueing failed\n");
603 return;
605 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
607 usbhid->ctrl[usbhid->ctrlhead].report = report;
608 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
609 usbhid->ctrlhead = head;
611 /* If the queue isn't running, restart it */
612 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
613 usbhid_restart_ctrl_queue(usbhid);
615 /* Otherwise see if an earlier request has timed out */
616 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
618 /* Prevent autosuspend following the unlink */
619 usb_autopm_get_interface_no_resume(usbhid->intf);
622 * Prevent resubmission in case the URB completes
623 * before we can unlink it. We don't want to cancel
624 * the wrong transfer!
626 usb_block_urb(usbhid->urbctrl);
628 /* Drop lock to avoid deadlock if the callback runs */
629 spin_unlock(&usbhid->lock);
631 usb_unlink_urb(usbhid->urbctrl);
632 spin_lock(&usbhid->lock);
633 usb_unblock_urb(usbhid->urbctrl);
635 /* Unlink might have stopped the queue */
636 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
637 usbhid_restart_ctrl_queue(usbhid);
639 /* Now we can allow autosuspend again */
640 usb_autopm_put_interface_async(usbhid->intf);
644 static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
646 struct usbhid_device *usbhid = hid->driver_data;
647 unsigned long flags;
649 spin_lock_irqsave(&usbhid->lock, flags);
650 __usbhid_submit_report(hid, report, dir);
651 spin_unlock_irqrestore(&usbhid->lock, flags);
654 static int usbhid_wait_io(struct hid_device *hid)
656 struct usbhid_device *usbhid = hid->driver_data;
658 if (!wait_event_timeout(usbhid->wait,
659 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
660 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
661 10*HZ)) {
662 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
663 return -1;
666 return 0;
669 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
671 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
672 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
673 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
676 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
677 unsigned char type, void *buf, int size)
679 int result, retries = 4;
681 memset(buf, 0, size);
683 do {
684 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
685 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
686 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
687 retries--;
688 } while (result < size && retries);
689 return result;
692 int usbhid_open(struct hid_device *hid)
694 struct usbhid_device *usbhid = hid->driver_data;
695 int res = 0;
697 mutex_lock(&hid_open_mut);
698 if (!hid->open++) {
699 res = usb_autopm_get_interface(usbhid->intf);
700 /* the device must be awake to reliably request remote wakeup */
701 if (res < 0) {
702 hid->open--;
703 res = -EIO;
704 goto done;
706 usbhid->intf->needs_remote_wakeup = 1;
707 res = hid_start_in(hid);
708 if (res) {
709 if (res != -ENOSPC) {
710 hid_io_error(hid);
711 res = 0;
712 } else {
713 /* no use opening if resources are insufficient */
714 hid->open--;
715 res = -EBUSY;
716 usbhid->intf->needs_remote_wakeup = 0;
719 usb_autopm_put_interface(usbhid->intf);
721 done:
722 mutex_unlock(&hid_open_mut);
723 return res;
726 void usbhid_close(struct hid_device *hid)
728 struct usbhid_device *usbhid = hid->driver_data;
730 mutex_lock(&hid_open_mut);
732 /* protecting hid->open to make sure we don't restart
733 * data acquistion due to a resumption we no longer
734 * care about
736 spin_lock_irq(&usbhid->lock);
737 if (!--hid->open) {
738 spin_unlock_irq(&usbhid->lock);
739 hid_cancel_delayed_stuff(usbhid);
740 if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL)) {
741 usb_kill_urb(usbhid->urbin);
742 usbhid->intf->needs_remote_wakeup = 0;
744 } else {
745 spin_unlock_irq(&usbhid->lock);
747 mutex_unlock(&hid_open_mut);
751 * Initialize all reports
754 void usbhid_init_reports(struct hid_device *hid)
756 struct hid_report *report;
757 struct usbhid_device *usbhid = hid->driver_data;
758 struct hid_report_enum *report_enum;
759 int err, ret;
761 if (!(hid->quirks & HID_QUIRK_NO_INIT_INPUT_REPORTS)) {
762 report_enum = &hid->report_enum[HID_INPUT_REPORT];
763 list_for_each_entry(report, &report_enum->report_list, list)
764 usbhid_submit_report(hid, report, USB_DIR_IN);
767 report_enum = &hid->report_enum[HID_FEATURE_REPORT];
768 list_for_each_entry(report, &report_enum->report_list, list)
769 usbhid_submit_report(hid, report, USB_DIR_IN);
771 err = 0;
772 ret = usbhid_wait_io(hid);
773 while (ret) {
774 err |= ret;
775 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
776 usb_kill_urb(usbhid->urbctrl);
777 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
778 usb_kill_urb(usbhid->urbout);
779 ret = usbhid_wait_io(hid);
782 if (err)
783 hid_warn(hid, "timeout initializing reports\n");
787 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
789 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
790 unsigned int hid_code, struct hid_field **pfield)
792 struct hid_report *report;
793 struct hid_field *field;
794 struct hid_usage *usage;
795 int i, j;
797 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
798 for (i = 0; i < report->maxfield; i++) {
799 field = report->field[i];
800 for (j = 0; j < field->maxusage; j++) {
801 usage = &field->usage[j];
802 if ((usage->hid & HID_USAGE_PAGE) == page &&
803 (usage->hid & 0xFFFF) == hid_code) {
804 *pfield = field;
805 return j;
810 return -1;
813 static void usbhid_set_leds(struct hid_device *hid)
815 struct hid_field *field;
816 int offset;
818 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
819 hid_set_field(field, offset, 0);
820 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
825 * Traverse the supplied list of reports and find the longest
827 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
828 unsigned int *max)
830 struct hid_report *report;
831 unsigned int size;
833 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
834 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
835 if (*max < size)
836 *max = size;
840 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
842 struct usbhid_device *usbhid = hid->driver_data;
844 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
845 &usbhid->inbuf_dma);
846 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
847 &usbhid->outbuf_dma);
848 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
849 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
850 &usbhid->ctrlbuf_dma);
851 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
852 !usbhid->ctrlbuf)
853 return -1;
855 return 0;
858 static int usbhid_get_raw_report(struct hid_device *hid,
859 unsigned char report_number, __u8 *buf, size_t count,
860 unsigned char report_type)
862 struct usbhid_device *usbhid = hid->driver_data;
863 struct usb_device *dev = hid_to_usb_dev(hid);
864 struct usb_interface *intf = usbhid->intf;
865 struct usb_host_interface *interface = intf->cur_altsetting;
866 int skipped_report_id = 0;
867 int ret;
869 /* Byte 0 is the report number. Report data starts at byte 1.*/
870 buf[0] = report_number;
871 if (report_number == 0x0) {
872 /* Offset the return buffer by 1, so that the report ID
873 will remain in byte 0. */
874 buf++;
875 count--;
876 skipped_report_id = 1;
878 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
879 HID_REQ_GET_REPORT,
880 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
881 ((report_type + 1) << 8) | report_number,
882 interface->desc.bInterfaceNumber, buf, count,
883 USB_CTRL_SET_TIMEOUT);
885 /* count also the report id */
886 if (ret > 0 && skipped_report_id)
887 ret++;
889 return ret;
892 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
893 unsigned char report_type)
895 struct usbhid_device *usbhid = hid->driver_data;
896 struct usb_device *dev = hid_to_usb_dev(hid);
897 struct usb_interface *intf = usbhid->intf;
898 struct usb_host_interface *interface = intf->cur_altsetting;
899 int ret;
901 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
902 int actual_length;
903 int skipped_report_id = 0;
905 if (buf[0] == 0x0) {
906 /* Don't send the Report ID */
907 buf++;
908 count--;
909 skipped_report_id = 1;
911 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
912 buf, count, &actual_length,
913 USB_CTRL_SET_TIMEOUT);
914 /* return the number of bytes transferred */
915 if (ret == 0) {
916 ret = actual_length;
917 /* count also the report id */
918 if (skipped_report_id)
919 ret++;
921 } else {
922 int skipped_report_id = 0;
923 int report_id = buf[0];
924 if (buf[0] == 0x0) {
925 /* Don't send the Report ID */
926 buf++;
927 count--;
928 skipped_report_id = 1;
930 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
931 HID_REQ_SET_REPORT,
932 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
933 ((report_type + 1) << 8) | report_id,
934 interface->desc.bInterfaceNumber, buf, count,
935 USB_CTRL_SET_TIMEOUT);
936 /* count also the report id, if this was a numbered report. */
937 if (ret > 0 && skipped_report_id)
938 ret++;
941 return ret;
944 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
946 struct usbhid_device *usbhid = hid->driver_data;
948 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
949 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
950 kfree(usbhid->cr);
951 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
954 static int usbhid_parse(struct hid_device *hid)
956 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
957 struct usb_host_interface *interface = intf->cur_altsetting;
958 struct usb_device *dev = interface_to_usbdev (intf);
959 struct hid_descriptor *hdesc;
960 u32 quirks = 0;
961 unsigned int rsize = 0;
962 char *rdesc;
963 int ret, n;
965 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
966 le16_to_cpu(dev->descriptor.idProduct));
968 if (quirks & HID_QUIRK_IGNORE)
969 return -ENODEV;
971 /* Many keyboards and mice don't like to be polled for reports,
972 * so we will always set the HID_QUIRK_NOGET flag for them. */
973 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
974 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
975 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
976 quirks |= HID_QUIRK_NOGET;
979 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
980 (!interface->desc.bNumEndpoints ||
981 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
982 dbg_hid("class descriptor not present\n");
983 return -ENODEV;
986 hid->version = le16_to_cpu(hdesc->bcdHID);
987 hid->country = hdesc->bCountryCode;
989 for (n = 0; n < hdesc->bNumDescriptors; n++)
990 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
991 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
993 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
994 dbg_hid("weird size of report descriptor (%u)\n", rsize);
995 return -EINVAL;
998 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
999 dbg_hid("couldn't allocate rdesc memory\n");
1000 return -ENOMEM;
1003 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1005 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1006 HID_DT_REPORT, rdesc, rsize);
1007 if (ret < 0) {
1008 dbg_hid("reading report descriptor failed\n");
1009 kfree(rdesc);
1010 goto err;
1013 ret = hid_parse_report(hid, rdesc, rsize);
1014 kfree(rdesc);
1015 if (ret) {
1016 dbg_hid("parsing report descriptor failed\n");
1017 goto err;
1020 hid->quirks |= quirks;
1022 return 0;
1023 err:
1024 return ret;
1027 static int usbhid_start(struct hid_device *hid)
1029 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1030 struct usb_host_interface *interface = intf->cur_altsetting;
1031 struct usb_device *dev = interface_to_usbdev(intf);
1032 struct usbhid_device *usbhid = hid->driver_data;
1033 unsigned int n, insize = 0;
1034 int ret;
1036 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1038 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1039 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1040 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1041 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1043 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1044 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
1046 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1048 if (insize > HID_MAX_BUFFER_SIZE)
1049 insize = HID_MAX_BUFFER_SIZE;
1051 if (hid_alloc_buffers(dev, hid)) {
1052 ret = -ENOMEM;
1053 goto fail;
1056 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1057 struct usb_endpoint_descriptor *endpoint;
1058 int pipe;
1059 int interval;
1061 endpoint = &interface->endpoint[n].desc;
1062 if (!usb_endpoint_xfer_int(endpoint))
1063 continue;
1065 interval = endpoint->bInterval;
1067 /* Some vendors give fullspeed interval on highspeed devides */
1068 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
1069 dev->speed == USB_SPEED_HIGH) {
1070 interval = fls(endpoint->bInterval*8);
1071 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1072 hid->name, endpoint->bInterval, interval);
1075 /* Change the polling interval of mice. */
1076 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1077 interval = hid_mousepoll_interval;
1079 ret = -ENOMEM;
1080 if (usb_endpoint_dir_in(endpoint)) {
1081 if (usbhid->urbin)
1082 continue;
1083 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1084 goto fail;
1085 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1086 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1087 hid_irq_in, hid, interval);
1088 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1089 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1090 } else {
1091 if (usbhid->urbout)
1092 continue;
1093 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1094 goto fail;
1095 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1096 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1097 hid_irq_out, hid, interval);
1098 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1099 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1103 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1104 if (!usbhid->urbctrl) {
1105 ret = -ENOMEM;
1106 goto fail;
1109 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1110 usbhid->ctrlbuf, 1, hid_ctrl, hid);
1111 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1112 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1114 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1115 usbhid_init_reports(hid);
1117 set_bit(HID_STARTED, &usbhid->iofl);
1119 if (hid->quirks & HID_QUIRK_ALWAYS_POLL) {
1120 ret = usb_autopm_get_interface(usbhid->intf);
1121 if (ret)
1122 goto fail;
1123 usbhid->intf->needs_remote_wakeup = 1;
1124 ret = hid_start_in(hid);
1125 if (ret) {
1126 dev_err(&hid->dev,
1127 "failed to start in urb: %d\n", ret);
1129 usb_autopm_put_interface(usbhid->intf);
1132 /* Some keyboards don't work until their LEDs have been set.
1133 * Since BIOSes do set the LEDs, it must be safe for any device
1134 * that supports the keyboard boot protocol.
1135 * In addition, enable remote wakeup by default for all keyboard
1136 * devices supporting the boot protocol.
1138 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1139 interface->desc.bInterfaceProtocol ==
1140 USB_INTERFACE_PROTOCOL_KEYBOARD) {
1141 usbhid_set_leds(hid);
1142 device_set_wakeup_enable(&dev->dev, 1);
1144 return 0;
1146 fail:
1147 usb_free_urb(usbhid->urbin);
1148 usb_free_urb(usbhid->urbout);
1149 usb_free_urb(usbhid->urbctrl);
1150 usbhid->urbin = NULL;
1151 usbhid->urbout = NULL;
1152 usbhid->urbctrl = NULL;
1153 hid_free_buffers(dev, hid);
1154 return ret;
1157 static void usbhid_stop(struct hid_device *hid)
1159 struct usbhid_device *usbhid = hid->driver_data;
1161 if (WARN_ON(!usbhid))
1162 return;
1164 if (hid->quirks & HID_QUIRK_ALWAYS_POLL)
1165 usbhid->intf->needs_remote_wakeup = 0;
1167 clear_bit(HID_STARTED, &usbhid->iofl);
1168 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1169 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1170 spin_unlock_irq(&usbhid->lock);
1171 usb_kill_urb(usbhid->urbin);
1172 usb_kill_urb(usbhid->urbout);
1173 usb_kill_urb(usbhid->urbctrl);
1175 hid_cancel_delayed_stuff(usbhid);
1177 hid->claimed = 0;
1179 usb_free_urb(usbhid->urbin);
1180 usb_free_urb(usbhid->urbctrl);
1181 usb_free_urb(usbhid->urbout);
1182 usbhid->urbin = NULL; /* don't mess up next start */
1183 usbhid->urbctrl = NULL;
1184 usbhid->urbout = NULL;
1186 hid_free_buffers(hid_to_usb_dev(hid), hid);
1189 static int usbhid_power(struct hid_device *hid, int lvl)
1191 int r = 0;
1193 switch (lvl) {
1194 case PM_HINT_FULLON:
1195 r = usbhid_get_power(hid);
1196 break;
1197 case PM_HINT_NORMAL:
1198 usbhid_put_power(hid);
1199 break;
1201 return r;
1204 static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1206 switch (reqtype) {
1207 case HID_REQ_GET_REPORT:
1208 usbhid_submit_report(hid, rep, USB_DIR_IN);
1209 break;
1210 case HID_REQ_SET_REPORT:
1211 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1212 break;
1216 static int usbhid_idle(struct hid_device *hid, int report, int idle,
1217 int reqtype)
1219 struct usb_device *dev = hid_to_usb_dev(hid);
1220 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1221 struct usb_host_interface *interface = intf->cur_altsetting;
1222 int ifnum = interface->desc.bInterfaceNumber;
1224 if (reqtype != HID_REQ_SET_IDLE)
1225 return -EINVAL;
1227 return hid_set_idle(dev, ifnum, report, idle);
1230 static struct hid_ll_driver usb_hid_driver = {
1231 .parse = usbhid_parse,
1232 .start = usbhid_start,
1233 .stop = usbhid_stop,
1234 .open = usbhid_open,
1235 .close = usbhid_close,
1236 .power = usbhid_power,
1237 .request = usbhid_request,
1238 .wait = usbhid_wait_io,
1239 .idle = usbhid_idle,
1242 static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1244 struct usb_host_interface *interface = intf->cur_altsetting;
1245 struct usb_device *dev = interface_to_usbdev(intf);
1246 struct usbhid_device *usbhid;
1247 struct hid_device *hid;
1248 unsigned int n, has_in = 0;
1249 size_t len;
1250 int ret;
1252 dbg_hid("HID probe called for ifnum %d\n",
1253 intf->altsetting->desc.bInterfaceNumber);
1255 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1256 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1257 has_in++;
1258 if (!has_in) {
1259 hid_err(intf, "couldn't find an input interrupt endpoint\n");
1260 return -ENODEV;
1263 hid = hid_allocate_device();
1264 if (IS_ERR(hid))
1265 return PTR_ERR(hid);
1267 usb_set_intfdata(intf, hid);
1268 hid->ll_driver = &usb_hid_driver;
1269 hid->hid_get_raw_report = usbhid_get_raw_report;
1270 hid->hid_output_raw_report = usbhid_output_raw_report;
1271 hid->ff_init = hid_pidff_init;
1272 #ifdef CONFIG_USB_HIDDEV
1273 hid->hiddev_connect = hiddev_connect;
1274 hid->hiddev_disconnect = hiddev_disconnect;
1275 hid->hiddev_hid_event = hiddev_hid_event;
1276 hid->hiddev_report_event = hiddev_report_event;
1277 #endif
1278 hid->dev.parent = &intf->dev;
1279 hid->bus = BUS_USB;
1280 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1281 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1282 hid->name[0] = 0;
1283 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
1284 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1285 USB_INTERFACE_PROTOCOL_MOUSE)
1286 hid->type = HID_TYPE_USBMOUSE;
1287 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1288 hid->type = HID_TYPE_USBNONE;
1290 if (dev->manufacturer)
1291 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1293 if (dev->product) {
1294 if (dev->manufacturer)
1295 strlcat(hid->name, " ", sizeof(hid->name));
1296 strlcat(hid->name, dev->product, sizeof(hid->name));
1299 if (!strlen(hid->name))
1300 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1301 le16_to_cpu(dev->descriptor.idVendor),
1302 le16_to_cpu(dev->descriptor.idProduct));
1304 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1305 strlcat(hid->phys, "/input", sizeof(hid->phys));
1306 len = strlen(hid->phys);
1307 if (len < sizeof(hid->phys) - 1)
1308 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1309 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1311 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1312 hid->uniq[0] = 0;
1314 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1315 if (usbhid == NULL) {
1316 ret = -ENOMEM;
1317 goto err;
1320 hid->driver_data = usbhid;
1321 usbhid->hid = hid;
1322 usbhid->intf = intf;
1323 usbhid->ifnum = interface->desc.bInterfaceNumber;
1325 init_waitqueue_head(&usbhid->wait);
1326 INIT_WORK(&usbhid->reset_work, hid_reset);
1327 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1328 spin_lock_init(&usbhid->lock);
1330 ret = hid_add_device(hid);
1331 if (ret) {
1332 if (ret != -ENODEV)
1333 hid_err(intf, "can't add hid device: %d\n", ret);
1334 goto err_free;
1337 return 0;
1338 err_free:
1339 kfree(usbhid);
1340 err:
1341 hid_destroy_device(hid);
1342 return ret;
1345 static void usbhid_disconnect(struct usb_interface *intf)
1347 struct hid_device *hid = usb_get_intfdata(intf);
1348 struct usbhid_device *usbhid;
1350 if (WARN_ON(!hid))
1351 return;
1353 usbhid = hid->driver_data;
1354 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1355 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1356 spin_unlock_irq(&usbhid->lock);
1357 hid_destroy_device(hid);
1358 kfree(usbhid);
1361 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1363 del_timer_sync(&usbhid->io_retry);
1364 cancel_work_sync(&usbhid->reset_work);
1367 static void hid_cease_io(struct usbhid_device *usbhid)
1369 del_timer_sync(&usbhid->io_retry);
1370 usb_kill_urb(usbhid->urbin);
1371 usb_kill_urb(usbhid->urbctrl);
1372 usb_kill_urb(usbhid->urbout);
1375 static void hid_restart_io(struct hid_device *hid)
1377 struct usbhid_device *usbhid = hid->driver_data;
1378 int clear_halt = test_bit(HID_CLEAR_HALT, &usbhid->iofl);
1379 int reset_pending = test_bit(HID_RESET_PENDING, &usbhid->iofl);
1381 spin_lock_irq(&usbhid->lock);
1382 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1383 usbhid_mark_busy(usbhid);
1385 if (clear_halt || reset_pending)
1386 schedule_work(&usbhid->reset_work);
1387 usbhid->retry_delay = 0;
1388 spin_unlock_irq(&usbhid->lock);
1390 if (reset_pending || !test_bit(HID_STARTED, &usbhid->iofl))
1391 return;
1393 if (!clear_halt) {
1394 if (hid_start_in(hid) < 0)
1395 hid_io_error(hid);
1398 spin_lock_irq(&usbhid->lock);
1399 if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
1400 usbhid_restart_out_queue(usbhid);
1401 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
1402 usbhid_restart_ctrl_queue(usbhid);
1403 spin_unlock_irq(&usbhid->lock);
1406 /* Treat USB reset pretty much the same as suspend/resume */
1407 static int hid_pre_reset(struct usb_interface *intf)
1409 struct hid_device *hid = usb_get_intfdata(intf);
1410 struct usbhid_device *usbhid = hid->driver_data;
1412 spin_lock_irq(&usbhid->lock);
1413 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1414 spin_unlock_irq(&usbhid->lock);
1415 hid_cease_io(usbhid);
1417 return 0;
1420 /* Same routine used for post_reset and reset_resume */
1421 static int hid_post_reset(struct usb_interface *intf)
1423 struct usb_device *dev = interface_to_usbdev (intf);
1424 struct hid_device *hid = usb_get_intfdata(intf);
1425 struct usbhid_device *usbhid = hid->driver_data;
1426 struct usb_host_interface *interface = intf->cur_altsetting;
1427 int status;
1428 char *rdesc;
1430 /* Fetch and examine the HID report descriptor. If this
1431 * has changed, then rebind. Since usbcore's check of the
1432 * configuration descriptors passed, we already know that
1433 * the size of the HID report descriptor has not changed.
1435 rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
1436 if (!rdesc) {
1437 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1438 return 1;
1440 status = hid_get_class_descriptor(dev,
1441 interface->desc.bInterfaceNumber,
1442 HID_DT_REPORT, rdesc, hid->dev_rsize);
1443 if (status < 0) {
1444 dbg_hid("reading report descriptor failed (post_reset)\n");
1445 kfree(rdesc);
1446 return 1;
1448 status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
1449 kfree(rdesc);
1450 if (status != 0) {
1451 dbg_hid("report descriptor changed\n");
1452 return 1;
1455 /* No need to do another reset or clear a halted endpoint */
1456 spin_lock_irq(&usbhid->lock);
1457 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1458 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
1459 spin_unlock_irq(&usbhid->lock);
1460 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1462 hid_restart_io(hid);
1464 return 0;
1467 int usbhid_get_power(struct hid_device *hid)
1469 struct usbhid_device *usbhid = hid->driver_data;
1471 return usb_autopm_get_interface(usbhid->intf);
1474 void usbhid_put_power(struct hid_device *hid)
1476 struct usbhid_device *usbhid = hid->driver_data;
1478 usb_autopm_put_interface(usbhid->intf);
1482 #ifdef CONFIG_PM
1483 static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1485 int status = 0;
1487 hid_restart_io(hid);
1488 if (driver_suspended && hid->driver && hid->driver->resume)
1489 status = hid->driver->resume(hid);
1490 return status;
1493 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1495 struct hid_device *hid = usb_get_intfdata(intf);
1496 struct usbhid_device *usbhid = hid->driver_data;
1497 int status = 0;
1498 bool driver_suspended = false;
1499 unsigned int ledcount;
1501 if (PMSG_IS_AUTO(message)) {
1502 ledcount = hidinput_count_leds(hid);
1503 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1504 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1505 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1506 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1507 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1508 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1509 && (!ledcount || ignoreled))
1511 set_bit(HID_SUSPENDED, &usbhid->iofl);
1512 spin_unlock_irq(&usbhid->lock);
1513 if (hid->driver && hid->driver->suspend) {
1514 status = hid->driver->suspend(hid, message);
1515 if (status < 0)
1516 goto failed;
1518 driver_suspended = true;
1519 } else {
1520 usbhid_mark_busy(usbhid);
1521 spin_unlock_irq(&usbhid->lock);
1522 return -EBUSY;
1525 } else {
1526 /* TODO: resume() might need to handle suspend failure */
1527 if (hid->driver && hid->driver->suspend)
1528 status = hid->driver->suspend(hid, message);
1529 driver_suspended = true;
1530 spin_lock_irq(&usbhid->lock);
1531 set_bit(HID_SUSPENDED, &usbhid->iofl);
1532 spin_unlock_irq(&usbhid->lock);
1533 if (usbhid_wait_io(hid) < 0)
1534 status = -EIO;
1537 hid_cancel_delayed_stuff(usbhid);
1538 hid_cease_io(usbhid);
1540 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1541 /* lost race against keypresses */
1542 status = -EBUSY;
1543 goto failed;
1545 dev_dbg(&intf->dev, "suspend\n");
1546 return status;
1548 failed:
1549 hid_resume_common(hid, driver_suspended);
1550 return status;
1553 static int hid_resume(struct usb_interface *intf)
1555 struct hid_device *hid = usb_get_intfdata (intf);
1556 int status;
1558 status = hid_resume_common(hid, true);
1559 dev_dbg(&intf->dev, "resume status %d\n", status);
1560 return 0;
1563 static int hid_reset_resume(struct usb_interface *intf)
1565 struct hid_device *hid = usb_get_intfdata(intf);
1566 int status;
1568 status = hid_post_reset(intf);
1569 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1570 int ret = hid->driver->reset_resume(hid);
1571 if (ret < 0)
1572 status = ret;
1574 return status;
1577 #endif /* CONFIG_PM */
1579 static const struct usb_device_id hid_usb_ids[] = {
1580 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1581 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1582 { } /* Terminating entry */
1585 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1587 static struct usb_driver hid_driver = {
1588 .name = "usbhid",
1589 .probe = usbhid_probe,
1590 .disconnect = usbhid_disconnect,
1591 #ifdef CONFIG_PM
1592 .suspend = hid_suspend,
1593 .resume = hid_resume,
1594 .reset_resume = hid_reset_resume,
1595 #endif
1596 .pre_reset = hid_pre_reset,
1597 .post_reset = hid_post_reset,
1598 .id_table = hid_usb_ids,
1599 .supports_autosuspend = 1,
1602 struct usb_interface *usbhid_find_interface(int minor)
1604 return usb_find_interface(&hid_driver, minor);
1607 static int __init hid_init(void)
1609 int retval = -ENOMEM;
1611 retval = usbhid_quirks_init(quirks_param);
1612 if (retval)
1613 goto usbhid_quirks_init_fail;
1614 retval = usb_register(&hid_driver);
1615 if (retval)
1616 goto usb_register_fail;
1617 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1619 return 0;
1620 usb_register_fail:
1621 usbhid_quirks_exit();
1622 usbhid_quirks_init_fail:
1623 return retval;
1626 static void __exit hid_exit(void)
1628 usb_deregister(&hid_driver);
1629 usbhid_quirks_exit();
1632 module_init(hid_init);
1633 module_exit(hid_exit);
1635 MODULE_AUTHOR("Andreas Gal");
1636 MODULE_AUTHOR("Vojtech Pavlik");
1637 MODULE_AUTHOR("Jiri Kosina");
1638 MODULE_DESCRIPTION(DRIVER_DESC);
1639 MODULE_LICENSE(DRIVER_LICENSE);