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