]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/hid/usbhid/hid-core.c
HID: remove rdesc quirk support
[linux-2.6-omap-h63xx.git] / drivers / hid / usbhid / hid-core.c
1 /*
2  *  USB HID support for Linux
3  *
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  */
9
10 /*
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.
15  */
16
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>
29
30 #include <linux/usb.h>
31
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"
37
38 /*
39  * Version Information
40  */
41
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"
46
47 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
48                                 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
49 /*
50  * Module parameters.
51  */
52
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");
56
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 /*
65  * Input submission and I/O error handler.
66  */
67
68 static void hid_io_error(struct hid_device *hid);
69
70 /* Start up the input URB */
71 static int hid_start_in(struct hid_device *hid)
72 {
73         unsigned long flags;
74         int rc = 0;
75         struct usbhid_device *usbhid = hid->driver_data;
76
77         spin_lock_irqsave(&usbhid->inlock, flags);
78         if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
79                         !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
80                         !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
81                 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
82                 if (rc != 0)
83                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
84         }
85         spin_unlock_irqrestore(&usbhid->inlock, flags);
86         return rc;
87 }
88
89 /* I/O retry timer routine */
90 static void hid_retry_timeout(unsigned long _hid)
91 {
92         struct hid_device *hid = (struct hid_device *) _hid;
93         struct usbhid_device *usbhid = hid->driver_data;
94
95         dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
96         if (hid_start_in(hid))
97                 hid_io_error(hid);
98 }
99
100 /* Workqueue routine to reset the device or clear a halt */
101 static void hid_reset(struct work_struct *work)
102 {
103         struct usbhid_device *usbhid =
104                 container_of(work, struct usbhid_device, reset_work);
105         struct hid_device *hid = usbhid->hid;
106         int rc_lock, rc = 0;
107
108         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
109                 dev_dbg(&usbhid->intf->dev, "clear halt\n");
110                 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
111                 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
112                 hid_start_in(hid);
113         }
114
115         else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
116                 dev_dbg(&usbhid->intf->dev, "resetting device\n");
117                 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
118                 if (rc_lock >= 0) {
119                         rc = usb_reset_device(hid_to_usb_dev(hid));
120                         if (rc_lock)
121                                 usb_unlock_device(hid_to_usb_dev(hid));
122                 }
123                 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
124         }
125
126         switch (rc) {
127         case 0:
128                 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
129                         hid_io_error(hid);
130                 break;
131         default:
132                 err_hid("can't reset device, %s-%s/input%d, status %d",
133                                 hid_to_usb_dev(hid)->bus->bus_name,
134                                 hid_to_usb_dev(hid)->devpath,
135                                 usbhid->ifnum, rc);
136                 /* FALLTHROUGH */
137         case -EHOSTUNREACH:
138         case -ENODEV:
139         case -EINTR:
140                 break;
141         }
142 }
143
144 /* Main I/O error handler */
145 static void hid_io_error(struct hid_device *hid)
146 {
147         unsigned long flags;
148         struct usbhid_device *usbhid = hid->driver_data;
149
150         spin_lock_irqsave(&usbhid->inlock, flags);
151
152         /* Stop when disconnected */
153         if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
154                 goto done;
155
156         /* If it has been a while since the last error, we'll assume
157          * this a brand new error and reset the retry timeout. */
158         if (time_after(jiffies, usbhid->stop_retry + HZ/2))
159                 usbhid->retry_delay = 0;
160
161         /* When an error occurs, retry at increasing intervals */
162         if (usbhid->retry_delay == 0) {
163                 usbhid->retry_delay = 13;       /* Then 26, 52, 104, 104, ... */
164                 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
165         } else if (usbhid->retry_delay < 100)
166                 usbhid->retry_delay *= 2;
167
168         if (time_after(jiffies, usbhid->stop_retry)) {
169
170                 /* Retries failed, so do a port reset */
171                 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
172                         schedule_work(&usbhid->reset_work);
173                         goto done;
174                 }
175         }
176
177         mod_timer(&usbhid->io_retry,
178                         jiffies + msecs_to_jiffies(usbhid->retry_delay));
179 done:
180         spin_unlock_irqrestore(&usbhid->inlock, flags);
181 }
182
183 /*
184  * Input interrupt completion handler.
185  */
186
187 static void hid_irq_in(struct urb *urb)
188 {
189         struct hid_device       *hid = urb->context;
190         struct usbhid_device    *usbhid = hid->driver_data;
191         int                     status;
192
193         switch (urb->status) {
194         case 0:                 /* success */
195                 usbhid->retry_delay = 0;
196                 hid_input_report(urb->context, HID_INPUT_REPORT,
197                                  urb->transfer_buffer,
198                                  urb->actual_length, 1);
199                 break;
200         case -EPIPE:            /* stall */
201                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
202                 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
203                 schedule_work(&usbhid->reset_work);
204                 return;
205         case -ECONNRESET:       /* unlink */
206         case -ENOENT:
207         case -ESHUTDOWN:        /* unplug */
208                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
209                 return;
210         case -EILSEQ:           /* protocol error or unplug */
211         case -EPROTO:           /* protocol error or unplug */
212         case -ETIME:            /* protocol error or unplug */
213         case -ETIMEDOUT:        /* Should never happen, but... */
214                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
215                 hid_io_error(hid);
216                 return;
217         default:                /* error */
218                 warn("input irq status %d received", urb->status);
219         }
220
221         status = usb_submit_urb(urb, GFP_ATOMIC);
222         if (status) {
223                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
224                 if (status != -EPERM) {
225                         err_hid("can't resubmit intr, %s-%s/input%d, status %d",
226                                         hid_to_usb_dev(hid)->bus->bus_name,
227                                         hid_to_usb_dev(hid)->devpath,
228                                         usbhid->ifnum, status);
229                         hid_io_error(hid);
230                 }
231         }
232 }
233
234 static int hid_submit_out(struct hid_device *hid)
235 {
236         struct hid_report *report;
237         struct usbhid_device *usbhid = hid->driver_data;
238
239         report = usbhid->out[usbhid->outtail];
240
241         hid_output_report(report, usbhid->outbuf);
242         usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
243         usbhid->urbout->dev = hid_to_usb_dev(hid);
244
245         dbg_hid("submitting out urb\n");
246
247         if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
248                 err_hid("usb_submit_urb(out) failed");
249                 return -1;
250         }
251
252         return 0;
253 }
254
255 static int hid_submit_ctrl(struct hid_device *hid)
256 {
257         struct hid_report *report;
258         unsigned char dir;
259         int len;
260         struct usbhid_device *usbhid = hid->driver_data;
261
262         report = usbhid->ctrl[usbhid->ctrltail].report;
263         dir = usbhid->ctrl[usbhid->ctrltail].dir;
264
265         len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
266         if (dir == USB_DIR_OUT) {
267                 hid_output_report(report, usbhid->ctrlbuf);
268                 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
269                 usbhid->urbctrl->transfer_buffer_length = len;
270         } else {
271                 int maxpacket, padlen;
272
273                 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
274                 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
275                 if (maxpacket > 0) {
276                         padlen = DIV_ROUND_UP(len, maxpacket);
277                         padlen *= maxpacket;
278                         if (padlen > usbhid->bufsize)
279                                 padlen = usbhid->bufsize;
280                 } else
281                         padlen = 0;
282                 usbhid->urbctrl->transfer_buffer_length = padlen;
283         }
284         usbhid->urbctrl->dev = hid_to_usb_dev(hid);
285
286         usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
287         usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
288         usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
289         usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
290         usbhid->cr->wLength = cpu_to_le16(len);
291
292         dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
293                 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
294                 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
295
296         if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
297                 err_hid("usb_submit_urb(ctrl) failed");
298                 return -1;
299         }
300
301         return 0;
302 }
303
304 /*
305  * Output interrupt completion handler.
306  */
307
308 static void hid_irq_out(struct urb *urb)
309 {
310         struct hid_device *hid = urb->context;
311         struct usbhid_device *usbhid = hid->driver_data;
312         unsigned long flags;
313         int unplug = 0;
314
315         switch (urb->status) {
316         case 0:                 /* success */
317                 break;
318         case -ESHUTDOWN:        /* unplug */
319                 unplug = 1;
320         case -EILSEQ:           /* protocol error or unplug */
321         case -EPROTO:           /* protocol error or unplug */
322         case -ECONNRESET:       /* unlink */
323         case -ENOENT:
324                 break;
325         default:                /* error */
326                 warn("output irq status %d received", urb->status);
327         }
328
329         spin_lock_irqsave(&usbhid->outlock, flags);
330
331         if (unplug)
332                 usbhid->outtail = usbhid->outhead;
333         else
334                 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
335
336         if (usbhid->outhead != usbhid->outtail) {
337                 if (hid_submit_out(hid)) {
338                         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
339                         wake_up(&usbhid->wait);
340                 }
341                 spin_unlock_irqrestore(&usbhid->outlock, flags);
342                 return;
343         }
344
345         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
346         spin_unlock_irqrestore(&usbhid->outlock, flags);
347         wake_up(&usbhid->wait);
348 }
349
350 /*
351  * Control pipe completion handler.
352  */
353
354 static void hid_ctrl(struct urb *urb)
355 {
356         struct hid_device *hid = urb->context;
357         struct usbhid_device *usbhid = hid->driver_data;
358         unsigned long flags;
359         int unplug = 0;
360
361         spin_lock_irqsave(&usbhid->ctrllock, flags);
362
363         switch (urb->status) {
364         case 0:                 /* success */
365                 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
366                         hid_input_report(urb->context,
367                                 usbhid->ctrl[usbhid->ctrltail].report->type,
368                                 urb->transfer_buffer, urb->actual_length, 0);
369                 break;
370         case -ESHUTDOWN:        /* unplug */
371                 unplug = 1;
372         case -EILSEQ:           /* protocol error or unplug */
373         case -EPROTO:           /* protocol error or unplug */
374         case -ECONNRESET:       /* unlink */
375         case -ENOENT:
376         case -EPIPE:            /* report not available */
377                 break;
378         default:                /* error */
379                 warn("ctrl urb status %d received", urb->status);
380         }
381
382         if (unplug)
383                 usbhid->ctrltail = usbhid->ctrlhead;
384         else
385                 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
386
387         if (usbhid->ctrlhead != usbhid->ctrltail) {
388                 if (hid_submit_ctrl(hid)) {
389                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
390                         wake_up(&usbhid->wait);
391                 }
392                 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
393                 return;
394         }
395
396         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
397         spin_unlock_irqrestore(&usbhid->ctrllock, flags);
398         wake_up(&usbhid->wait);
399 }
400
401 void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
402 {
403         int head;
404         unsigned long flags;
405         struct usbhid_device *usbhid = hid->driver_data;
406
407         if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
408                 return;
409
410         if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
411
412                 spin_lock_irqsave(&usbhid->outlock, flags);
413
414                 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
415                         spin_unlock_irqrestore(&usbhid->outlock, flags);
416                         warn("output queue full");
417                         return;
418                 }
419
420                 usbhid->out[usbhid->outhead] = report;
421                 usbhid->outhead = head;
422
423                 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
424                         if (hid_submit_out(hid))
425                                 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
426
427                 spin_unlock_irqrestore(&usbhid->outlock, flags);
428                 return;
429         }
430
431         spin_lock_irqsave(&usbhid->ctrllock, flags);
432
433         if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
434                 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
435                 warn("control queue full");
436                 return;
437         }
438
439         usbhid->ctrl[usbhid->ctrlhead].report = report;
440         usbhid->ctrl[usbhid->ctrlhead].dir = dir;
441         usbhid->ctrlhead = head;
442
443         if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
444                 if (hid_submit_ctrl(hid))
445                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
446
447         spin_unlock_irqrestore(&usbhid->ctrllock, flags);
448 }
449
450 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
451 {
452         struct hid_device *hid = input_get_drvdata(dev);
453         struct hid_field *field;
454         int offset;
455
456         if (type == EV_FF)
457                 return input_ff_event(dev, type, code, value);
458
459         if (type != EV_LED)
460                 return -1;
461
462         if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
463                 warn("event field not found");
464                 return -1;
465         }
466
467         hid_set_field(field, offset, value);
468         usbhid_submit_report(hid, field->report, USB_DIR_OUT);
469
470         return 0;
471 }
472
473 int usbhid_wait_io(struct hid_device *hid)
474 {
475         struct usbhid_device *usbhid = hid->driver_data;
476
477         if (!wait_event_timeout(usbhid->wait,
478                                 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
479                                 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
480                                         10*HZ)) {
481                 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
482                 return -1;
483         }
484
485         return 0;
486 }
487
488 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
489 {
490         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
491                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
492                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
493 }
494
495 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
496                 unsigned char type, void *buf, int size)
497 {
498         int result, retries = 4;
499
500         memset(buf, 0, size);
501
502         do {
503                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
504                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
505                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
506                 retries--;
507         } while (result < size && retries);
508         return result;
509 }
510
511 int usbhid_open(struct hid_device *hid)
512 {
513         struct usbhid_device *usbhid = hid->driver_data;
514         int res;
515
516         if (!hid->open++) {
517                 res = usb_autopm_get_interface(usbhid->intf);
518                 if (res < 0) {
519                         hid->open--;
520                         return -EIO;
521                 }
522         }
523         if (hid_start_in(hid))
524                 hid_io_error(hid);
525         return 0;
526 }
527
528 void usbhid_close(struct hid_device *hid)
529 {
530         struct usbhid_device *usbhid = hid->driver_data;
531
532         if (!--hid->open) {
533                 usb_kill_urb(usbhid->urbin);
534                 usb_autopm_put_interface(usbhid->intf);
535         }
536 }
537
538 /*
539  * Initialize all reports
540  */
541
542 void usbhid_init_reports(struct hid_device *hid)
543 {
544         struct hid_report *report;
545         struct usbhid_device *usbhid = hid->driver_data;
546         int err, ret;
547
548         list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
549                 usbhid_submit_report(hid, report, USB_DIR_IN);
550
551         list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
552                 usbhid_submit_report(hid, report, USB_DIR_IN);
553
554         err = 0;
555         ret = usbhid_wait_io(hid);
556         while (ret) {
557                 err |= ret;
558                 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
559                         usb_kill_urb(usbhid->urbctrl);
560                 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
561                         usb_kill_urb(usbhid->urbout);
562                 ret = usbhid_wait_io(hid);
563         }
564
565         if (err)
566                 warn("timeout initializing reports");
567 }
568
569 /*
570  * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
571  */
572 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
573     unsigned int hid_code, struct hid_field **pfield)
574 {
575         struct hid_report *report;
576         struct hid_field *field;
577         struct hid_usage *usage;
578         int i, j;
579
580         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
581                 for (i = 0; i < report->maxfield; i++) {
582                         field = report->field[i];
583                         for (j = 0; j < field->maxusage; j++) {
584                                 usage = &field->usage[j];
585                                 if ((usage->hid & HID_USAGE_PAGE) == page &&
586                                     (usage->hid & 0xFFFF) == hid_code) {
587                                         *pfield = field;
588                                         return j;
589                                 }
590                         }
591                 }
592         }
593         return -1;
594 }
595
596 static void usbhid_set_leds(struct hid_device *hid)
597 {
598         struct hid_field *field;
599         int offset;
600
601         if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
602                 hid_set_field(field, offset, 0);
603                 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
604         }
605 }
606
607 /*
608  * Traverse the supplied list of reports and find the longest
609  */
610 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
611                 unsigned int *max)
612 {
613         struct hid_report *report;
614         unsigned int size;
615
616         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
617                 size = ((report->size - 1) >> 3) + 1;
618                 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
619                         size++;
620                 if (*max < size)
621                         *max = size;
622         }
623 }
624
625 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
626 {
627         struct usbhid_device *usbhid = hid->driver_data;
628
629         if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
630                 return -1;
631         if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
632                 return -1;
633         if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
634                 return -1;
635         if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
636                 return -1;
637
638         return 0;
639 }
640
641 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
642 {
643         struct usbhid_device *usbhid = hid->driver_data;
644         struct usb_device *dev = hid_to_usb_dev(hid);
645         struct usb_interface *intf = usbhid->intf;
646         struct usb_host_interface *interface = intf->cur_altsetting;
647         int ret;
648
649         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
650                 HID_REQ_SET_REPORT,
651                 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
652                 ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
653                 interface->desc.bInterfaceNumber, buf + 1, count - 1,
654                 USB_CTRL_SET_TIMEOUT);
655
656         /* count also the report id */
657         if (ret > 0)
658                 ret++;
659
660         return ret;
661 }
662
663 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
664 {
665         struct usbhid_device *usbhid = hid->driver_data;
666
667         usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
668         usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
669         usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
670         usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
671 }
672
673 /*
674  * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
675  * to "operational".  Without this, the ps3 controller will not report any
676  * events.
677  */
678 static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
679 {
680         int result;
681         char *buf = kmalloc(18, GFP_KERNEL);
682
683         if (!buf)
684                 return;
685
686         result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
687                                  HID_REQ_GET_REPORT,
688                                  USB_DIR_IN | USB_TYPE_CLASS |
689                                  USB_RECIP_INTERFACE,
690                                  (3 << 8) | 0xf2, ifnum, buf, 17,
691                                  USB_CTRL_GET_TIMEOUT);
692
693         if (result < 0)
694                 err_hid("%s failed: %d\n", __func__, result);
695
696         kfree(buf);
697 }
698
699 static int usbhid_start_finish(struct hid_device *hid)
700 {
701         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
702         char path[64], *type;
703         unsigned int i;
704
705         usbhid_init_reports(hid);
706         hid_dump_device(hid);
707         if (hid->quirks & HID_QUIRK_RESET_LEDS)
708                 usbhid_set_leds(hid);
709
710         if (!hidinput_connect(hid))
711                 hid->claimed |= HID_CLAIMED_INPUT;
712         if (!hiddev_connect(hid))
713                 hid->claimed |= HID_CLAIMED_HIDDEV;
714         if (!hidraw_connect(hid))
715                 hid->claimed |= HID_CLAIMED_HIDRAW;
716
717         if (!hid->claimed) {
718                 printk(KERN_ERR "HID device claimed by neither input, hiddev "
719                                 "nor hidraw\n");
720                 return -ENODEV;
721         }
722
723         if ((hid->claimed & HID_CLAIMED_INPUT))
724                 hid_ff_init(hid);
725
726         if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
727                 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
728                         intf->cur_altsetting->desc.bInterfaceNumber);
729
730         printk(KERN_INFO);
731
732         if (hid->claimed & HID_CLAIMED_INPUT)
733                 printk("input");
734         if ((hid->claimed & HID_CLAIMED_INPUT) &&
735                         ((hid->claimed & HID_CLAIMED_HIDDEV) ||
736                                 hid->claimed & HID_CLAIMED_HIDRAW))
737                 printk(",");
738         if (hid->claimed & HID_CLAIMED_HIDDEV)
739                 printk("hiddev%d", hid->minor);
740         if ((hid->claimed & HID_CLAIMED_INPUT) &&
741                         (hid->claimed & HID_CLAIMED_HIDDEV) &&
742                         (hid->claimed & HID_CLAIMED_HIDRAW))
743                 printk(",");
744         if (hid->claimed & HID_CLAIMED_HIDRAW)
745                 printk("hidraw%d", ((struct hidraw *)hid->hidraw)->minor);
746
747         type = "Device";
748         for (i = 0; i < hid->maxcollection; i++) {
749                 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
750                     (hid->collection[i].usage & HID_USAGE_PAGE) ==
751                                                 HID_UP_GENDESK &&
752                     (hid->collection[i].usage & 0xffff) <
753                                                 ARRAY_SIZE(hid_types)) {
754                         type = hid_types[hid->collection[i].usage & 0xffff];
755                         break;
756                 }
757         }
758
759         usb_make_path(interface_to_usbdev(intf), path, 63);
760
761         printk(": USB HID v%x.%02x %s [%s] on %s\n",
762                 hid->version >> 8, hid->version & 0xff, type, hid->name, path);
763
764         return 0;
765 }
766
767 static int usbhid_parse(struct hid_device *hid)
768 {
769         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
770         struct usb_host_interface *interface = intf->cur_altsetting;
771         struct usb_device *dev = interface_to_usbdev (intf);
772         struct hid_descriptor *hdesc;
773         u32 quirks = 0;
774         unsigned int rsize = 0;
775         char *rdesc;
776         int ret, n;
777
778         quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
779                         le16_to_cpu(dev->descriptor.idProduct));
780
781         /* Many keyboards and mice don't like to be polled for reports,
782          * so we will always set the HID_QUIRK_NOGET flag for them. */
783         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
784                 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
785                         interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
786                                 quirks |= HID_QUIRK_NOGET;
787         }
788
789         if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
790             (!interface->desc.bNumEndpoints ||
791              usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
792                 dbg_hid("class descriptor not present\n");
793                 return -ENODEV;
794         }
795
796         hid->version = le16_to_cpu(hdesc->bcdHID);
797         hid->country = hdesc->bCountryCode;
798
799         for (n = 0; n < hdesc->bNumDescriptors; n++)
800                 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
801                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
802
803         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
804                 dbg_hid("weird size of report descriptor (%u)\n", rsize);
805                 return -EINVAL;
806         }
807
808         if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
809                 dbg_hid("couldn't allocate rdesc memory\n");
810                 return -ENOMEM;
811         }
812
813         hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
814
815         ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
816                         HID_DT_REPORT, rdesc, rsize);
817         if (ret < 0) {
818                 dbg_hid("reading report descriptor failed\n");
819                 kfree(rdesc);
820                 goto err;
821         }
822
823         dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
824         for (n = 0; n < rsize; n++)
825                 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
826         dbg_hid_line("\n");
827
828         ret = hid_parse_report(hid, rdesc, rsize);
829         kfree(rdesc);
830         if (ret) {
831                 dbg_hid("parsing report descriptor failed\n");
832                 goto err;
833         }
834
835         hid->quirks = quirks;
836
837         return 0;
838 err:
839         return ret;
840 }
841
842 static int usbhid_start(struct hid_device *hid)
843 {
844         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
845         struct usb_host_interface *interface = intf->cur_altsetting;
846         struct usb_device *dev = interface_to_usbdev(intf);
847         struct usbhid_device *usbhid;
848         unsigned int n, insize = 0;
849         int ret;
850
851         WARN_ON(hid->driver_data);
852
853         usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL);
854         if (usbhid == NULL) {
855                 ret = -ENOMEM;
856                 goto err;
857         }
858
859         hid->driver_data = usbhid;
860         usbhid->hid = hid;
861
862         usbhid->bufsize = HID_MIN_BUFFER_SIZE;
863         hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
864         hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
865         hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
866
867         if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
868                 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
869
870         hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
871
872         if (insize > HID_MAX_BUFFER_SIZE)
873                 insize = HID_MAX_BUFFER_SIZE;
874
875         if (hid_alloc_buffers(dev, hid)) {
876                 ret = -ENOMEM;
877                 goto fail;
878         }
879
880         for (n = 0; n < interface->desc.bNumEndpoints; n++) {
881                 struct usb_endpoint_descriptor *endpoint;
882                 int pipe;
883                 int interval;
884
885                 endpoint = &interface->endpoint[n].desc;
886                 if ((endpoint->bmAttributes & 3) != 3)          /* Not an interrupt endpoint */
887                         continue;
888
889                 interval = endpoint->bInterval;
890
891                 /* Some vendors give fullspeed interval on highspeed devides */
892                 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
893                     dev->speed == USB_SPEED_HIGH) {
894                         interval = fls(endpoint->bInterval*8);
895                         printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
896                                hid->name, endpoint->bInterval, interval);
897                 }
898
899                 /* Change the polling interval of mice. */
900                 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
901                         interval = hid_mousepoll_interval;
902
903                 ret = -ENOMEM;
904                 if (usb_endpoint_dir_in(endpoint)) {
905                         if (usbhid->urbin)
906                                 continue;
907                         if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
908                                 goto fail;
909                         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
910                         usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
911                                          hid_irq_in, hid, interval);
912                         usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
913                         usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
914                 } else {
915                         if (usbhid->urbout)
916                                 continue;
917                         if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
918                                 goto fail;
919                         pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
920                         usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
921                                          hid_irq_out, hid, interval);
922                         usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
923                         usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
924                 }
925         }
926
927         if (!usbhid->urbin) {
928                 err_hid("couldn't find an input interrupt endpoint");
929                 ret = -ENODEV;
930                 goto fail;
931         }
932
933         init_waitqueue_head(&usbhid->wait);
934         INIT_WORK(&usbhid->reset_work, hid_reset);
935         setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
936
937         spin_lock_init(&usbhid->inlock);
938         spin_lock_init(&usbhid->outlock);
939         spin_lock_init(&usbhid->ctrllock);
940
941         usbhid->intf = intf;
942         usbhid->ifnum = interface->desc.bInterfaceNumber;
943
944         usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
945         if (!usbhid->urbctrl) {
946                 ret = -ENOMEM;
947                 goto fail;
948         }
949
950         usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
951                              usbhid->ctrlbuf, 1, hid_ctrl, hid);
952         usbhid->urbctrl->setup_dma = usbhid->cr_dma;
953         usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
954         usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
955
956         ret = usbhid_start_finish(hid);
957         if (ret)
958                 goto fail;
959
960         return 0;
961
962 fail:
963         usb_free_urb(usbhid->urbin);
964         usb_free_urb(usbhid->urbout);
965         usb_free_urb(usbhid->urbctrl);
966         hid_free_buffers(dev, hid);
967         kfree(usbhid);
968 err:
969         return ret;
970 }
971
972 static void usbhid_stop(struct hid_device *hid)
973 {
974         struct usbhid_device *usbhid = hid->driver_data;
975
976         if (WARN_ON(!usbhid))
977                 return;
978
979         spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
980         set_bit(HID_DISCONNECTED, &usbhid->iofl);
981         spin_unlock_irq(&usbhid->inlock);
982         usb_kill_urb(usbhid->urbin);
983         usb_kill_urb(usbhid->urbout);
984         usb_kill_urb(usbhid->urbctrl);
985
986         del_timer_sync(&usbhid->io_retry);
987         cancel_work_sync(&usbhid->reset_work);
988
989         if (hid->claimed & HID_CLAIMED_INPUT)
990                 hidinput_disconnect(hid);
991         if (hid->claimed & HID_CLAIMED_HIDDEV)
992                 hiddev_disconnect(hid);
993         if (hid->claimed & HID_CLAIMED_HIDRAW)
994                 hidraw_disconnect(hid);
995
996         hid->claimed = 0;
997
998         usb_free_urb(usbhid->urbin);
999         usb_free_urb(usbhid->urbctrl);
1000         usb_free_urb(usbhid->urbout);
1001
1002         hid_free_buffers(hid_to_usb_dev(hid), hid);
1003         kfree(usbhid);
1004         hid->driver_data = NULL;
1005 }
1006
1007 static struct hid_ll_driver usb_hid_driver = {
1008         .parse = usbhid_parse,
1009         .start = usbhid_start,
1010         .stop = usbhid_stop,
1011         .open = usbhid_open,
1012         .close = usbhid_close,
1013         .hidinput_input_event = usb_hidinput_input_event,
1014 };
1015
1016 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1017 {
1018         struct usb_device *dev = interface_to_usbdev(intf);
1019         struct hid_device *hid;
1020         size_t len;
1021         int ret;
1022
1023         dbg_hid("HID probe called for ifnum %d\n",
1024                         intf->altsetting->desc.bInterfaceNumber);
1025
1026         hid = hid_allocate_device();
1027         if (IS_ERR(hid))
1028                 return PTR_ERR(hid);
1029
1030         usb_set_intfdata(intf, hid);
1031         hid->ll_driver = &usb_hid_driver;
1032         hid->hid_output_raw_report = usbhid_output_raw_report;
1033 #ifdef CONFIG_USB_HIDDEV
1034         hid->hiddev_hid_event = hiddev_hid_event;
1035         hid->hiddev_report_event = hiddev_report_event;
1036 #endif
1037         hid->dev.parent = &intf->dev;
1038         hid->bus = BUS_USB;
1039         hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1040         hid->product = le16_to_cpu(dev->descriptor.idProduct);
1041         hid->name[0] = 0;
1042
1043         if (dev->manufacturer)
1044                 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1045
1046         if (dev->product) {
1047                 if (dev->manufacturer)
1048                         strlcat(hid->name, " ", sizeof(hid->name));
1049                 strlcat(hid->name, dev->product, sizeof(hid->name));
1050         }
1051
1052         if (!strlen(hid->name))
1053                 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1054                          le16_to_cpu(dev->descriptor.idVendor),
1055                          le16_to_cpu(dev->descriptor.idProduct));
1056
1057         usb_make_path(dev, hid->phys, sizeof(hid->phys));
1058         strlcat(hid->phys, "/input", sizeof(hid->phys));
1059         len = strlen(hid->phys);
1060         if (len < sizeof(hid->phys) - 1)
1061                 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1062                          "%d", intf->altsetting[0].desc.bInterfaceNumber);
1063
1064         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1065                 hid->uniq[0] = 0;
1066
1067         ret = hid_add_device(hid);
1068         if (ret) {
1069                 if (ret != -ENODEV)
1070                         dev_err(&intf->dev, "can't add hid device: %d\n", ret);
1071                 goto err;
1072         }
1073
1074         return 0;
1075 err:
1076         hid_destroy_device(hid);
1077         return ret;
1078 }
1079
1080 static void hid_disconnect(struct usb_interface *intf)
1081 {
1082         struct hid_device *hid = usb_get_intfdata(intf);
1083
1084         if (WARN_ON(!hid))
1085                 return;
1086
1087         hid_destroy_device(hid);
1088 }
1089
1090 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1091 {
1092         struct hid_device *hid = usb_get_intfdata (intf);
1093         struct usbhid_device *usbhid = hid->driver_data;
1094
1095         spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1096         set_bit(HID_SUSPENDED, &usbhid->iofl);
1097         spin_unlock_irq(&usbhid->inlock);
1098         del_timer(&usbhid->io_retry);
1099         usb_kill_urb(usbhid->urbin);
1100         dev_dbg(&intf->dev, "suspend\n");
1101         return 0;
1102 }
1103
1104 static int hid_resume(struct usb_interface *intf)
1105 {
1106         struct hid_device *hid = usb_get_intfdata (intf);
1107         struct usbhid_device *usbhid = hid->driver_data;
1108         int status;
1109
1110         clear_bit(HID_SUSPENDED, &usbhid->iofl);
1111         usbhid->retry_delay = 0;
1112         status = hid_start_in(hid);
1113         dev_dbg(&intf->dev, "resume status %d\n", status);
1114         return status;
1115 }
1116
1117 /* Treat USB reset pretty much the same as suspend/resume */
1118 static int hid_pre_reset(struct usb_interface *intf)
1119 {
1120         /* FIXME: What if the interface is already suspended? */
1121         hid_suspend(intf, PMSG_ON);
1122         return 0;
1123 }
1124
1125 /* Same routine used for post_reset and reset_resume */
1126 static int hid_post_reset(struct usb_interface *intf)
1127 {
1128         struct usb_device *dev = interface_to_usbdev (intf);
1129
1130         hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1131         /* FIXME: Any more reinitialization needed? */
1132
1133         return hid_resume(intf);
1134 }
1135
1136 static struct usb_device_id hid_usb_ids [] = {
1137         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1138                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1139         { }                                             /* Terminating entry */
1140 };
1141
1142 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1143
1144 static struct usb_driver hid_driver = {
1145         .name =         "usbhid",
1146         .probe =        hid_probe,
1147         .disconnect =   hid_disconnect,
1148         .suspend =      hid_suspend,
1149         .resume =       hid_resume,
1150         .reset_resume = hid_post_reset,
1151         .pre_reset =    hid_pre_reset,
1152         .post_reset =   hid_post_reset,
1153         .id_table =     hid_usb_ids,
1154         .supports_autosuspend = 1,
1155 };
1156
1157 static const struct hid_device_id hid_usb_table[] = {
1158         { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1159         { }
1160 };
1161
1162 static struct hid_driver hid_usb_driver = {
1163         .name = "generic-usb",
1164         .id_table = hid_usb_table,
1165 };
1166
1167 static int __init hid_init(void)
1168 {
1169         int retval;
1170         retval = hid_register_driver(&hid_usb_driver);
1171         if (retval)
1172                 goto hid_register_fail;
1173         retval = usbhid_quirks_init(quirks_param);
1174         if (retval)
1175                 goto usbhid_quirks_init_fail;
1176         retval = hiddev_init();
1177         if (retval)
1178                 goto hiddev_init_fail;
1179         retval = usb_register(&hid_driver);
1180         if (retval)
1181                 goto usb_register_fail;
1182         info(DRIVER_VERSION ":" DRIVER_DESC);
1183
1184         return 0;
1185 usb_register_fail:
1186         hiddev_exit();
1187 hiddev_init_fail:
1188         usbhid_quirks_exit();
1189 usbhid_quirks_init_fail:
1190         hid_unregister_driver(&hid_usb_driver);
1191 hid_register_fail:
1192         return retval;
1193 }
1194
1195 static void __exit hid_exit(void)
1196 {
1197         usb_deregister(&hid_driver);
1198         hiddev_exit();
1199         usbhid_quirks_exit();
1200         hid_unregister_driver(&hid_usb_driver);
1201 }
1202
1203 module_init(hid_init);
1204 module_exit(hid_exit);
1205
1206 MODULE_AUTHOR(DRIVER_AUTHOR);
1207 MODULE_DESCRIPTION(DRIVER_DESC);
1208 MODULE_LICENSE(DRIVER_LICENSE);