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