Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hid / usbhid / hid-core.c
blobcbd1bf31fce091c6f9780efdd88574899658fd0c
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) 2006-2007 Jiri Kosina
8 */
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/mm.h>
23 #include <linux/smp_lock.h>
24 #include <linux/spinlock.h>
25 #include <asm/unaligned.h>
26 #include <asm/byteorder.h>
27 #include <linux/input.h>
28 #include <linux/wait.h>
30 #include <linux/usb.h>
32 #include <linux/hid.h>
33 #include <linux/hiddev.h>
34 #include <linux/hid-debug.h>
35 #include <linux/hidraw.h>
36 #include "usbhid.h"
39 * Version Information
42 #define DRIVER_VERSION "v2.6"
43 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
44 #define DRIVER_DESC "USB HID core driver"
45 #define DRIVER_LICENSE "GPL"
47 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
48 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
50 * Module parameters.
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 /* Quirks specified at module load time */
58 static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
59 module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
60 MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
61 " quirks=vendorID:productID:quirks"
62 " where vendorID, productID, and quirks are all in"
63 " 0x-prefixed hex");
64 static char *rdesc_quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
65 module_param_array_named(rdesc_quirks, rdesc_quirks_param, charp, NULL, 0444);
66 MODULE_PARM_DESC(rdesc_quirks, "Add/modify report descriptor quirks by specifying "
67 " rdesc_quirks=vendorID:productID:rdesc_quirks"
68 " where vendorID, productID, and rdesc_quirks are all in"
69 " 0x-prefixed hex");
71 * Input submission and I/O error handler.
74 static void hid_io_error(struct hid_device *hid);
76 /* Start up the input URB */
77 static int hid_start_in(struct hid_device *hid)
79 unsigned long flags;
80 int rc = 0;
81 struct usbhid_device *usbhid = hid->driver_data;
83 spin_lock_irqsave(&usbhid->inlock, flags);
84 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
85 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
86 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
87 if (rc != 0)
88 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
90 spin_unlock_irqrestore(&usbhid->inlock, flags);
91 return rc;
94 /* I/O retry timer routine */
95 static void hid_retry_timeout(unsigned long _hid)
97 struct hid_device *hid = (struct hid_device *) _hid;
98 struct usbhid_device *usbhid = hid->driver_data;
100 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
101 if (hid_start_in(hid))
102 hid_io_error(hid);
105 /* Workqueue routine to reset the device or clear a halt */
106 static void hid_reset(struct work_struct *work)
108 struct usbhid_device *usbhid =
109 container_of(work, struct usbhid_device, reset_work);
110 struct hid_device *hid = usbhid->hid;
111 int rc_lock, rc = 0;
113 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
114 dev_dbg(&usbhid->intf->dev, "clear halt\n");
115 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
116 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
117 hid_start_in(hid);
120 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
121 dev_dbg(&usbhid->intf->dev, "resetting device\n");
122 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
123 if (rc_lock >= 0) {
124 rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
125 if (rc_lock)
126 usb_unlock_device(hid_to_usb_dev(hid));
128 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
131 switch (rc) {
132 case 0:
133 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
134 hid_io_error(hid);
135 break;
136 default:
137 err_hid("can't reset device, %s-%s/input%d, status %d",
138 hid_to_usb_dev(hid)->bus->bus_name,
139 hid_to_usb_dev(hid)->devpath,
140 usbhid->ifnum, rc);
141 /* FALLTHROUGH */
142 case -EHOSTUNREACH:
143 case -ENODEV:
144 case -EINTR:
145 break;
149 /* Main I/O error handler */
150 static void hid_io_error(struct hid_device *hid)
152 unsigned long flags;
153 struct usbhid_device *usbhid = hid->driver_data;
155 spin_lock_irqsave(&usbhid->inlock, flags);
157 /* Stop when disconnected */
158 if (usb_get_intfdata(usbhid->intf) == NULL)
159 goto done;
161 /* If it has been a while since the last error, we'll assume
162 * this a brand new error and reset the retry timeout. */
163 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
164 usbhid->retry_delay = 0;
166 /* When an error occurs, retry at increasing intervals */
167 if (usbhid->retry_delay == 0) {
168 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
169 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
170 } else if (usbhid->retry_delay < 100)
171 usbhid->retry_delay *= 2;
173 if (time_after(jiffies, usbhid->stop_retry)) {
175 /* Retries failed, so do a port reset */
176 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
177 schedule_work(&usbhid->reset_work);
178 goto done;
182 mod_timer(&usbhid->io_retry,
183 jiffies + msecs_to_jiffies(usbhid->retry_delay));
184 done:
185 spin_unlock_irqrestore(&usbhid->inlock, flags);
189 * Input interrupt completion handler.
192 static void hid_irq_in(struct urb *urb)
194 struct hid_device *hid = urb->context;
195 struct usbhid_device *usbhid = hid->driver_data;
196 int status;
198 switch (urb->status) {
199 case 0: /* success */
200 usbhid->retry_delay = 0;
201 hid_input_report(urb->context, HID_INPUT_REPORT,
202 urb->transfer_buffer,
203 urb->actual_length, 1);
204 break;
205 case -EPIPE: /* stall */
206 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
207 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
208 schedule_work(&usbhid->reset_work);
209 return;
210 case -ECONNRESET: /* unlink */
211 case -ENOENT:
212 case -ESHUTDOWN: /* unplug */
213 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
214 return;
215 case -EILSEQ: /* protocol error or unplug */
216 case -EPROTO: /* protocol error or unplug */
217 case -ETIME: /* protocol error or unplug */
218 case -ETIMEDOUT: /* Should never happen, but... */
219 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
220 hid_io_error(hid);
221 return;
222 default: /* error */
223 warn("input irq status %d received", urb->status);
226 status = usb_submit_urb(urb, GFP_ATOMIC);
227 if (status) {
228 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
229 if (status != -EPERM) {
230 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
231 hid_to_usb_dev(hid)->bus->bus_name,
232 hid_to_usb_dev(hid)->devpath,
233 usbhid->ifnum, status);
234 hid_io_error(hid);
239 static int hid_submit_out(struct hid_device *hid)
241 struct hid_report *report;
242 struct usbhid_device *usbhid = hid->driver_data;
244 report = usbhid->out[usbhid->outtail];
246 hid_output_report(report, usbhid->outbuf);
247 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
248 usbhid->urbout->dev = hid_to_usb_dev(hid);
250 dbg_hid("submitting out urb\n");
252 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
253 err_hid("usb_submit_urb(out) failed");
254 return -1;
257 return 0;
260 static int hid_submit_ctrl(struct hid_device *hid)
262 struct hid_report *report;
263 unsigned char dir;
264 int len;
265 struct usbhid_device *usbhid = hid->driver_data;
267 report = usbhid->ctrl[usbhid->ctrltail].report;
268 dir = usbhid->ctrl[usbhid->ctrltail].dir;
270 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
271 if (dir == USB_DIR_OUT) {
272 hid_output_report(report, usbhid->ctrlbuf);
273 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
274 usbhid->urbctrl->transfer_buffer_length = len;
275 } else {
276 int maxpacket, padlen;
278 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
279 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
280 if (maxpacket > 0) {
281 <<<<<<< HEAD:drivers/hid/usbhid/hid-core.c
282 padlen = (len + maxpacket - 1) / maxpacket;
283 =======
284 padlen = DIV_ROUND_UP(len, maxpacket);
285 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hid/usbhid/hid-core.c
286 padlen *= maxpacket;
287 if (padlen > usbhid->bufsize)
288 padlen = usbhid->bufsize;
289 } else
290 padlen = 0;
291 usbhid->urbctrl->transfer_buffer_length = padlen;
293 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
295 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
296 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
297 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
298 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
299 usbhid->cr->wLength = cpu_to_le16(len);
301 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
302 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
303 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
305 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
306 err_hid("usb_submit_urb(ctrl) failed");
307 return -1;
310 return 0;
314 * Output interrupt completion handler.
317 static void hid_irq_out(struct urb *urb)
319 struct hid_device *hid = urb->context;
320 struct usbhid_device *usbhid = hid->driver_data;
321 unsigned long flags;
322 int unplug = 0;
324 switch (urb->status) {
325 case 0: /* success */
326 break;
327 case -ESHUTDOWN: /* unplug */
328 unplug = 1;
329 case -EILSEQ: /* protocol error or unplug */
330 case -EPROTO: /* protocol error or unplug */
331 case -ECONNRESET: /* unlink */
332 case -ENOENT:
333 break;
334 default: /* error */
335 warn("output irq status %d received", urb->status);
338 spin_lock_irqsave(&usbhid->outlock, flags);
340 if (unplug)
341 usbhid->outtail = usbhid->outhead;
342 else
343 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
345 if (usbhid->outhead != usbhid->outtail) {
346 if (hid_submit_out(hid)) {
347 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
348 wake_up(&hid->wait);
350 spin_unlock_irqrestore(&usbhid->outlock, flags);
351 return;
354 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
355 spin_unlock_irqrestore(&usbhid->outlock, flags);
356 wake_up(&hid->wait);
360 * Control pipe completion handler.
363 static void hid_ctrl(struct urb *urb)
365 struct hid_device *hid = urb->context;
366 struct usbhid_device *usbhid = hid->driver_data;
367 unsigned long flags;
368 int unplug = 0;
370 spin_lock_irqsave(&usbhid->ctrllock, flags);
372 switch (urb->status) {
373 case 0: /* success */
374 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
375 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
376 urb->transfer_buffer, urb->actual_length, 0);
377 break;
378 case -ESHUTDOWN: /* unplug */
379 unplug = 1;
380 case -EILSEQ: /* protocol error or unplug */
381 case -EPROTO: /* protocol error or unplug */
382 case -ECONNRESET: /* unlink */
383 case -ENOENT:
384 case -EPIPE: /* report not available */
385 break;
386 default: /* error */
387 warn("ctrl urb status %d received", urb->status);
390 if (unplug)
391 usbhid->ctrltail = usbhid->ctrlhead;
392 else
393 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
395 if (usbhid->ctrlhead != usbhid->ctrltail) {
396 if (hid_submit_ctrl(hid)) {
397 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
398 wake_up(&hid->wait);
400 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
401 return;
404 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
405 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
406 wake_up(&hid->wait);
409 void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
411 int head;
412 unsigned long flags;
413 struct usbhid_device *usbhid = hid->driver_data;
415 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
416 return;
418 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
420 spin_lock_irqsave(&usbhid->outlock, flags);
422 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
423 spin_unlock_irqrestore(&usbhid->outlock, flags);
424 warn("output queue full");
425 return;
428 usbhid->out[usbhid->outhead] = report;
429 usbhid->outhead = head;
431 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
432 if (hid_submit_out(hid))
433 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
435 spin_unlock_irqrestore(&usbhid->outlock, flags);
436 return;
439 spin_lock_irqsave(&usbhid->ctrllock, flags);
441 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
442 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
443 warn("control queue full");
444 return;
447 usbhid->ctrl[usbhid->ctrlhead].report = report;
448 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
449 usbhid->ctrlhead = head;
451 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
452 if (hid_submit_ctrl(hid))
453 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
455 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
458 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
460 struct hid_device *hid = input_get_drvdata(dev);
461 struct hid_field *field;
462 int offset;
464 if (type == EV_FF)
465 return input_ff_event(dev, type, code, value);
467 if (type != EV_LED)
468 return -1;
470 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
471 warn("event field not found");
472 return -1;
475 hid_set_field(field, offset, value);
476 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
478 return 0;
481 int usbhid_wait_io(struct hid_device *hid)
483 struct usbhid_device *usbhid = hid->driver_data;
485 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
486 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
487 10*HZ)) {
488 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
489 return -1;
492 return 0;
495 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
497 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
498 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
499 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
502 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
503 unsigned char type, void *buf, int size)
505 int result, retries = 4;
507 memset(buf, 0, size);
509 do {
510 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
511 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
512 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
513 retries--;
514 } while (result < size && retries);
515 return result;
518 int usbhid_open(struct hid_device *hid)
520 struct usbhid_device *usbhid = hid->driver_data;
521 int res;
523 if (!hid->open++) {
524 res = usb_autopm_get_interface(usbhid->intf);
525 if (res < 0) {
526 hid->open--;
527 return -EIO;
530 if (hid_start_in(hid))
531 hid_io_error(hid);
532 return 0;
535 void usbhid_close(struct hid_device *hid)
537 struct usbhid_device *usbhid = hid->driver_data;
539 if (!--hid->open) {
540 usb_kill_urb(usbhid->urbin);
541 usb_autopm_put_interface(usbhid->intf);
546 * Initialize all reports
549 void usbhid_init_reports(struct hid_device *hid)
551 struct hid_report *report;
552 struct usbhid_device *usbhid = hid->driver_data;
553 int err, ret;
555 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
556 usbhid_submit_report(hid, report, USB_DIR_IN);
558 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
559 usbhid_submit_report(hid, report, USB_DIR_IN);
561 err = 0;
562 ret = usbhid_wait_io(hid);
563 while (ret) {
564 err |= ret;
565 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
566 usb_kill_urb(usbhid->urbctrl);
567 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
568 usb_kill_urb(usbhid->urbout);
569 ret = usbhid_wait_io(hid);
572 if (err)
573 warn("timeout initializing reports");
577 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
579 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
580 unsigned int hid_code, struct hid_field **pfield)
582 struct hid_report *report;
583 struct hid_field *field;
584 struct hid_usage *usage;
585 int i, j;
587 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
588 for (i = 0; i < report->maxfield; i++) {
589 field = report->field[i];
590 for (j = 0; j < field->maxusage; j++) {
591 usage = &field->usage[j];
592 if ((usage->hid & HID_USAGE_PAGE) == page &&
593 (usage->hid & 0xFFFF) == hid_code) {
594 *pfield = field;
595 return j;
600 return -1;
603 static void usbhid_set_leds(struct hid_device *hid)
605 struct hid_field *field;
606 int offset;
608 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
609 hid_set_field(field, offset, 0);
610 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
615 * Traverse the supplied list of reports and find the longest
617 static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
619 struct hid_report *report;
620 int size;
622 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
623 size = ((report->size - 1) >> 3) + 1;
624 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
625 size++;
626 if (*max < size)
627 *max = size;
631 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
633 struct usbhid_device *usbhid = hid->driver_data;
635 if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
636 return -1;
637 if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
638 return -1;
639 if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
640 return -1;
641 if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
642 return -1;
644 return 0;
647 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
649 struct usbhid_device *usbhid = hid->driver_data;
650 struct usb_device *dev = hid_to_usb_dev(hid);
651 struct usb_interface *intf = usbhid->intf;
652 struct usb_host_interface *interface = intf->cur_altsetting;
653 int ret;
655 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
656 HID_REQ_SET_REPORT,
657 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
658 cpu_to_le16(((HID_OUTPUT_REPORT + 1) << 8) | *buf),
659 interface->desc.bInterfaceNumber, buf + 1, count - 1,
660 USB_CTRL_SET_TIMEOUT);
662 /* count also the report id */
663 if (ret > 0)
664 ret++;
666 return ret;
669 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
671 struct usbhid_device *usbhid = hid->driver_data;
673 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
674 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
675 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
676 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
680 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
681 * to "operational". Without this, the ps3 controller will not report any
682 * events.
684 static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
686 int result;
687 char *buf = kmalloc(18, GFP_KERNEL);
689 if (!buf)
690 return;
692 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
693 HID_REQ_GET_REPORT,
694 USB_DIR_IN | USB_TYPE_CLASS |
695 USB_RECIP_INTERFACE,
696 (3 << 8) | 0xf2, ifnum, buf, 17,
697 USB_CTRL_GET_TIMEOUT);
699 if (result < 0)
700 err_hid("%s failed: %d\n", __func__, result);
702 kfree(buf);
705 static struct hid_device *usb_hid_configure(struct usb_interface *intf)
707 struct usb_host_interface *interface = intf->cur_altsetting;
708 struct usb_device *dev = interface_to_usbdev (intf);
709 struct hid_descriptor *hdesc;
710 struct hid_device *hid;
711 u32 quirks = 0;
712 unsigned rsize = 0;
713 char *rdesc;
714 int n, len, insize = 0;
715 struct usbhid_device *usbhid;
717 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
718 le16_to_cpu(dev->descriptor.idProduct));
720 /* Many keyboards and mice don't like to be polled for reports,
721 * so we will always set the HID_QUIRK_NOGET flag for them. */
722 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
723 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
724 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
725 quirks |= HID_QUIRK_NOGET;
728 if (quirks & HID_QUIRK_IGNORE)
729 return NULL;
731 if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
732 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
733 return NULL;
736 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
737 (!interface->desc.bNumEndpoints ||
738 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
739 dbg_hid("class descriptor not present\n");
740 return NULL;
743 for (n = 0; n < hdesc->bNumDescriptors; n++)
744 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
745 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
747 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
748 dbg_hid("weird size of report descriptor (%u)\n", rsize);
749 return NULL;
752 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
753 dbg_hid("couldn't allocate rdesc memory\n");
754 return NULL;
757 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
759 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
760 dbg_hid("reading report descriptor failed\n");
761 kfree(rdesc);
762 return NULL;
765 usbhid_fixup_report_descriptor(le16_to_cpu(dev->descriptor.idVendor),
766 le16_to_cpu(dev->descriptor.idProduct), rdesc,
767 rsize, rdesc_quirks_param);
769 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
770 for (n = 0; n < rsize; n++)
771 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
772 dbg_hid_line("\n");
774 if (!(hid = hid_parse_report(rdesc, n))) {
775 dbg_hid("parsing report descriptor failed\n");
776 kfree(rdesc);
777 return NULL;
780 kfree(rdesc);
781 hid->quirks = quirks;
783 if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
784 goto fail_no_usbhid;
786 hid->driver_data = usbhid;
787 usbhid->hid = hid;
789 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
790 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
791 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
792 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
794 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
795 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
797 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
799 if (insize > HID_MAX_BUFFER_SIZE)
800 insize = HID_MAX_BUFFER_SIZE;
802 if (hid_alloc_buffers(dev, hid)) {
803 hid_free_buffers(dev, hid);
804 goto fail;
807 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
809 struct usb_endpoint_descriptor *endpoint;
810 int pipe;
811 int interval;
813 endpoint = &interface->endpoint[n].desc;
814 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
815 continue;
817 interval = endpoint->bInterval;
819 /* Change the polling interval of mice. */
820 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
821 interval = hid_mousepoll_interval;
823 if (usb_endpoint_dir_in(endpoint)) {
824 if (usbhid->urbin)
825 continue;
826 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
827 goto fail;
828 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
829 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
830 hid_irq_in, hid, interval);
831 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
832 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
833 } else {
834 if (usbhid->urbout)
835 continue;
836 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
837 goto fail;
838 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
839 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
840 hid_irq_out, hid, interval);
841 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
842 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
846 if (!usbhid->urbin) {
847 err_hid("couldn't find an input interrupt endpoint");
848 goto fail;
851 init_waitqueue_head(&hid->wait);
853 INIT_WORK(&usbhid->reset_work, hid_reset);
854 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
856 spin_lock_init(&usbhid->inlock);
857 spin_lock_init(&usbhid->outlock);
858 spin_lock_init(&usbhid->ctrllock);
860 hid->version = le16_to_cpu(hdesc->bcdHID);
861 hid->country = hdesc->bCountryCode;
862 hid->dev = &intf->dev;
863 usbhid->intf = intf;
864 usbhid->ifnum = interface->desc.bInterfaceNumber;
866 hid->name[0] = 0;
868 if (dev->manufacturer)
869 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
871 if (dev->product) {
872 if (dev->manufacturer)
873 strlcat(hid->name, " ", sizeof(hid->name));
874 strlcat(hid->name, dev->product, sizeof(hid->name));
877 if (!strlen(hid->name))
878 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
879 le16_to_cpu(dev->descriptor.idVendor),
880 le16_to_cpu(dev->descriptor.idProduct));
882 hid->bus = BUS_USB;
883 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
884 hid->product = le16_to_cpu(dev->descriptor.idProduct);
886 usb_make_path(dev, hid->phys, sizeof(hid->phys));
887 strlcat(hid->phys, "/input", sizeof(hid->phys));
888 len = strlen(hid->phys);
889 if (len < sizeof(hid->phys) - 1)
890 snprintf(hid->phys + len, sizeof(hid->phys) - len,
891 "%d", intf->altsetting[0].desc.bInterfaceNumber);
893 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
894 hid->uniq[0] = 0;
896 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
897 if (!usbhid->urbctrl)
898 goto fail;
900 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
901 usbhid->ctrlbuf, 1, hid_ctrl, hid);
902 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
903 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
904 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
905 hid->hidinput_input_event = usb_hidinput_input_event;
906 hid->hid_open = usbhid_open;
907 hid->hid_close = usbhid_close;
908 #ifdef CONFIG_USB_HIDDEV
909 hid->hiddev_hid_event = hiddev_hid_event;
910 hid->hiddev_report_event = hiddev_report_event;
911 #endif
912 hid->hid_output_raw_report = usbhid_output_raw_report;
913 return hid;
915 fail:
916 usb_free_urb(usbhid->urbin);
917 usb_free_urb(usbhid->urbout);
918 usb_free_urb(usbhid->urbctrl);
919 hid_free_buffers(dev, hid);
920 kfree(usbhid);
921 fail_no_usbhid:
922 hid_free_device(hid);
924 return NULL;
927 static void hid_disconnect(struct usb_interface *intf)
929 struct hid_device *hid = usb_get_intfdata (intf);
930 struct usbhid_device *usbhid;
932 if (!hid)
933 return;
935 usbhid = hid->driver_data;
937 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
938 usb_set_intfdata(intf, NULL);
939 spin_unlock_irq(&usbhid->inlock);
940 usb_kill_urb(usbhid->urbin);
941 usb_kill_urb(usbhid->urbout);
942 usb_kill_urb(usbhid->urbctrl);
944 del_timer_sync(&usbhid->io_retry);
945 cancel_work_sync(&usbhid->reset_work);
947 if (hid->claimed & HID_CLAIMED_INPUT)
948 hidinput_disconnect(hid);
949 if (hid->claimed & HID_CLAIMED_HIDDEV)
950 hiddev_disconnect(hid);
951 if (hid->claimed & HID_CLAIMED_HIDRAW)
952 hidraw_disconnect(hid);
954 usb_free_urb(usbhid->urbin);
955 usb_free_urb(usbhid->urbctrl);
956 usb_free_urb(usbhid->urbout);
958 hid_free_buffers(hid_to_usb_dev(hid), hid);
959 kfree(usbhid);
960 hid_free_device(hid);
963 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
965 struct hid_device *hid;
966 char path[64];
967 int i;
968 char *c;
970 dbg_hid("HID probe called for ifnum %d\n",
971 intf->altsetting->desc.bInterfaceNumber);
973 if (!(hid = usb_hid_configure(intf)))
974 return -ENODEV;
976 usbhid_init_reports(hid);
977 hid_dump_device(hid);
978 if (hid->quirks & HID_QUIRK_RESET_LEDS)
979 usbhid_set_leds(hid);
981 if (!hidinput_connect(hid))
982 hid->claimed |= HID_CLAIMED_INPUT;
983 if (!hiddev_connect(hid))
984 hid->claimed |= HID_CLAIMED_HIDDEV;
985 if (!hidraw_connect(hid))
986 hid->claimed |= HID_CLAIMED_HIDRAW;
988 usb_set_intfdata(intf, hid);
990 if (!hid->claimed) {
991 printk ("HID device claimed by neither input, hiddev nor hidraw\n");
992 hid_disconnect(intf);
993 return -ENODEV;
996 if ((hid->claimed & HID_CLAIMED_INPUT))
997 hid_ff_init(hid);
999 if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
1000 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
1001 intf->cur_altsetting->desc.bInterfaceNumber);
1003 printk(KERN_INFO);
1005 if (hid->claimed & HID_CLAIMED_INPUT)
1006 printk("input");
1007 if ((hid->claimed & HID_CLAIMED_INPUT) && ((hid->claimed & HID_CLAIMED_HIDDEV) ||
1008 hid->claimed & HID_CLAIMED_HIDRAW))
1009 printk(",");
1010 if (hid->claimed & HID_CLAIMED_HIDDEV)
1011 printk("hiddev%d", hid->minor);
1012 if ((hid->claimed & HID_CLAIMED_INPUT) && (hid->claimed & HID_CLAIMED_HIDDEV) &&
1013 (hid->claimed & HID_CLAIMED_HIDRAW))
1014 printk(",");
1015 if (hid->claimed & HID_CLAIMED_HIDRAW)
1016 printk("hidraw%d", ((struct hidraw*)hid->hidraw)->minor);
1018 c = "Device";
1019 for (i = 0; i < hid->maxcollection; i++) {
1020 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1021 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1022 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1023 c = hid_types[hid->collection[i].usage & 0xffff];
1024 break;
1028 usb_make_path(interface_to_usbdev(intf), path, 63);
1030 printk(": USB HID v%x.%02x %s [%s] on %s\n",
1031 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1033 return 0;
1036 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1038 struct hid_device *hid = usb_get_intfdata (intf);
1039 struct usbhid_device *usbhid = hid->driver_data;
1041 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1042 set_bit(HID_SUSPENDED, &usbhid->iofl);
1043 spin_unlock_irq(&usbhid->inlock);
1044 del_timer(&usbhid->io_retry);
1045 usb_kill_urb(usbhid->urbin);
1046 dev_dbg(&intf->dev, "suspend\n");
1047 return 0;
1050 static int hid_resume(struct usb_interface *intf)
1052 struct hid_device *hid = usb_get_intfdata (intf);
1053 struct usbhid_device *usbhid = hid->driver_data;
1054 int status;
1056 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1057 usbhid->retry_delay = 0;
1058 status = hid_start_in(hid);
1059 dev_dbg(&intf->dev, "resume status %d\n", status);
1060 return status;
1063 /* Treat USB reset pretty much the same as suspend/resume */
1064 static int hid_pre_reset(struct usb_interface *intf)
1066 /* FIXME: What if the interface is already suspended? */
1067 hid_suspend(intf, PMSG_ON);
1068 return 0;
1071 /* Same routine used for post_reset and reset_resume */
1072 static int hid_post_reset(struct usb_interface *intf)
1074 struct usb_device *dev = interface_to_usbdev (intf);
1076 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1077 /* FIXME: Any more reinitialization needed? */
1079 return hid_resume(intf);
1082 static struct usb_device_id hid_usb_ids [] = {
1083 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1084 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1085 { } /* Terminating entry */
1088 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1090 static struct usb_driver hid_driver = {
1091 .name = "usbhid",
1092 .probe = hid_probe,
1093 .disconnect = hid_disconnect,
1094 .suspend = hid_suspend,
1095 .resume = hid_resume,
1096 .reset_resume = hid_post_reset,
1097 .pre_reset = hid_pre_reset,
1098 .post_reset = hid_post_reset,
1099 .id_table = hid_usb_ids,
1100 .supports_autosuspend = 1,
1103 static int __init hid_init(void)
1105 int retval;
1106 retval = usbhid_quirks_init(quirks_param);
1107 if (retval)
1108 goto usbhid_quirks_init_fail;
1109 retval = hiddev_init();
1110 if (retval)
1111 goto hiddev_init_fail;
1112 retval = usb_register(&hid_driver);
1113 if (retval)
1114 goto usb_register_fail;
1115 info(DRIVER_VERSION ":" DRIVER_DESC);
1117 return 0;
1118 usb_register_fail:
1119 hiddev_exit();
1120 hiddev_init_fail:
1121 usbhid_quirks_exit();
1122 usbhid_quirks_init_fail:
1123 return retval;
1126 static void __exit hid_exit(void)
1128 usb_deregister(&hid_driver);
1129 hiddev_exit();
1130 usbhid_quirks_exit();
1133 module_init(hid_init);
1134 module_exit(hid_exit);
1136 MODULE_AUTHOR(DRIVER_AUTHOR);
1137 MODULE_DESCRIPTION(DRIVER_DESC);
1138 MODULE_LICENSE(DRIVER_LICENSE);