]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/hid/usbhid/hid-core.c
HID: usbhid: protect hid disconnect flag
[sojka/nv-tegra/linux-3.10.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) 2007-2008 Oliver Neukum
8  *  Copyright (c) 2006-2010 Jiri Kosina
9  *  Copyright (c) 2013-2014, NVIDIA CORPORATION.  All rights reserved.
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 2 of the License, or (at your option)
16  * any later version.
17  */
18
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/mm.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock.h>
27 #include <asm/unaligned.h>
28 #include <asm/byteorder.h>
29 #include <linux/input.h>
30 #include <linux/wait.h>
31 #include <linux/workqueue.h>
32 #include <linux/string.h>
33
34 #include <linux/usb.h>
35
36 #include <linux/hid.h>
37 #include <linux/hiddev.h>
38 #include <linux/hid-debug.h>
39 #include <linux/hidraw.h>
40 #include "usbhid.h"
41
42 /*
43  * Version Information
44  */
45
46 #define DRIVER_DESC "USB HID core driver"
47 #define DRIVER_LICENSE "GPL"
48
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 static unsigned int ignoreled;
58 module_param_named(ignoreled, ignoreled, uint, 0644);
59 MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
60
61 /* Quirks specified at module load time */
62 static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
63 module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
64 MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
65                 " quirks=vendorID:productID:quirks"
66                 " where vendorID, productID, and quirks are all in"
67                 " 0x-prefixed hex");
68 /*
69  * Input submission and I/O error handler.
70  */
71 static DEFINE_MUTEX(hid_open_mut);
72
73 static void hid_io_error(struct hid_device *hid);
74 static int hid_submit_out(struct hid_device *hid);
75 static int hid_submit_ctrl(struct hid_device *hid);
76 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
77
78 /* Start up the input URB */
79 static int hid_start_in(struct hid_device *hid)
80 {
81         unsigned long flags;
82         int rc = 0;
83         struct usbhid_device *usbhid = hid->driver_data;
84
85         spin_lock_irqsave(&usbhid->lock, flags);
86         if (hid->open > 0 &&
87                         !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
88                         !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
89                         !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
90                 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
91                 if (rc != 0) {
92                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
93                         if (rc == -ENOSPC)
94                                 set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
95                 } else {
96                         clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
97                 }
98         }
99         spin_unlock_irqrestore(&usbhid->lock, flags);
100         return rc;
101 }
102
103 /* I/O retry timer routine */
104 static void hid_retry_timeout(unsigned long _hid)
105 {
106         struct hid_device *hid = (struct hid_device *) _hid;
107         struct usbhid_device *usbhid = hid->driver_data;
108
109         dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
110         if (hid_start_in(hid))
111                 hid_io_error(hid);
112 }
113
114 /* Workqueue routine to reset the device or clear a halt */
115 static void hid_reset(struct work_struct *work)
116 {
117         struct usbhid_device *usbhid =
118                 container_of(work, struct usbhid_device, reset_work);
119         struct hid_device *hid = usbhid->hid;
120         int rc = 0;
121
122         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
123                 dev_dbg(&usbhid->intf->dev, "clear halt\n");
124                 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
125                 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
126                 hid_start_in(hid);
127         }
128
129         else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
130                 dev_dbg(&usbhid->intf->dev, "resetting device\n");
131                 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
132                 if (rc == 0) {
133                         rc = usb_reset_device(hid_to_usb_dev(hid));
134                         usb_unlock_device(hid_to_usb_dev(hid));
135                 }
136                 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
137         }
138
139         switch (rc) {
140         case 0:
141                 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
142                         hid_io_error(hid);
143                 break;
144         default:
145                 hid_err(hid, "can't reset device, %s-%s/input%d, status %d\n",
146                         hid_to_usb_dev(hid)->bus->bus_name,
147                         hid_to_usb_dev(hid)->devpath,
148                         usbhid->ifnum, rc);
149                 /* FALLTHROUGH */
150         case -EHOSTUNREACH:
151         case -ENODEV:
152         case -EINTR:
153                 break;
154         }
155 }
156
157 /* Main I/O error handler */
158 static void hid_io_error(struct hid_device *hid)
159 {
160         unsigned long flags;
161         struct usbhid_device *usbhid = hid->driver_data;
162
163         spin_lock_irqsave(&usbhid->lock, flags);
164
165         /* Stop when disconnected */
166         if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
167                 goto done;
168
169         /* If it has been a while since the last error, we'll assume
170          * this a brand new error and reset the retry timeout. */
171         if (time_after(jiffies, usbhid->stop_retry + HZ/2))
172                 usbhid->retry_delay = 0;
173
174         /* When an error occurs, retry at increasing intervals */
175         if (usbhid->retry_delay == 0) {
176                 usbhid->retry_delay = 13;       /* Then 26, 52, 104, 104, ... */
177                 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
178         } else if (usbhid->retry_delay < 100)
179                 usbhid->retry_delay *= 2;
180
181         if (time_after(jiffies, usbhid->stop_retry)) {
182
183                 /* Retries failed, so do a port reset unless we lack bandwidth*/
184                 if (test_bit(HID_NO_BANDWIDTH, &usbhid->iofl)
185                      && !test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
186
187                         schedule_work(&usbhid->reset_work);
188                         goto done;
189                 }
190         }
191
192         mod_timer(&usbhid->io_retry,
193                         jiffies + msecs_to_jiffies(usbhid->retry_delay));
194 done:
195         spin_unlock_irqrestore(&usbhid->lock, flags);
196 }
197
198 static void usbhid_mark_busy(struct usbhid_device *usbhid)
199 {
200         struct usb_interface *intf = usbhid->intf;
201
202         usb_mark_last_busy(interface_to_usbdev(intf));
203 }
204
205 static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
206 {
207         struct hid_device *hid = usb_get_intfdata(usbhid->intf);
208         int kicked;
209         int r;
210
211         if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
212                         test_bit(HID_SUSPENDED, &usbhid->iofl))
213                 return 0;
214
215         if ((kicked = (usbhid->outhead != usbhid->outtail))) {
216                 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
217
218                 /* Try to wake up from autosuspend... */
219                 r = usb_autopm_get_interface_async(usbhid->intf);
220                 if (r < 0)
221                         return r;
222
223                 /*
224                  * If still suspended, don't submit.  Submission will
225                  * occur if/when resume drains the queue.
226                  */
227                 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
228                         usb_autopm_put_interface_no_suspend(usbhid->intf);
229                         return r;
230                 }
231
232                 /* Asynchronously flush queue. */
233                 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
234                 if (hid_submit_out(hid)) {
235                         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
236                         usb_autopm_put_interface_async(usbhid->intf);
237                 }
238                 wake_up(&usbhid->wait);
239         }
240         return kicked;
241 }
242
243 static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
244 {
245         struct hid_device *hid = usb_get_intfdata(usbhid->intf);
246         int kicked;
247         int r;
248
249         WARN_ON(hid == NULL);
250         if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
251                         test_bit(HID_SUSPENDED, &usbhid->iofl))
252                 return 0;
253
254         if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
255                 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
256
257                 /* Try to wake up from autosuspend... */
258                 r = usb_autopm_get_interface_async(usbhid->intf);
259                 if (r < 0)
260                         return r;
261
262                 /*
263                  * If still suspended, don't submit.  Submission will
264                  * occur if/when resume drains the queue.
265                  */
266                 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
267                         usb_autopm_put_interface_no_suspend(usbhid->intf);
268                         return r;
269                 }
270
271                 /* Asynchronously flush queue. */
272                 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
273                 if (hid_submit_ctrl(hid)) {
274                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
275                         usb_autopm_put_interface_async(usbhid->intf);
276                 }
277                 wake_up(&usbhid->wait);
278         }
279         return kicked;
280 }
281
282 /*
283  * Input interrupt completion handler.
284  */
285
286 static void hid_irq_in(struct urb *urb)
287 {
288         struct hid_device       *hid = urb->context;
289         struct usbhid_device    *usbhid = hid->driver_data;
290         int                     status;
291
292         switch (urb->status) {
293         case 0:                 /* success */
294                 usbhid_mark_busy(usbhid);
295                 usbhid->retry_delay = 0;
296                 hid_input_report(urb->context, HID_INPUT_REPORT,
297                                  urb->transfer_buffer,
298                                  urb->actual_length, 1);
299                 /*
300                  * autosuspend refused while keys are pressed
301                  * because most keyboards don't wake up when
302                  * a key is released
303                  */
304                 if (hid_check_keys_pressed(hid))
305                         set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
306                 else
307                         clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
308                 break;
309         case -EPIPE:            /* stall */
310                 usbhid_mark_busy(usbhid);
311                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
312                 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
313                 schedule_work(&usbhid->reset_work);
314                 return;
315         case -ECONNRESET:       /* unlink */
316         case -ENOENT:
317         case -ESHUTDOWN:        /* unplug */
318                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
319                 return;
320         case -EILSEQ:           /* protocol error or unplug */
321         case -EPROTO:           /* protocol error or unplug */
322         case -ETIME:            /* protocol error or unplug */
323         case -ETIMEDOUT:        /* Should never happen, but... */
324                 usbhid_mark_busy(usbhid);
325                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
326                 hid_io_error(hid);
327                 return;
328         default:                /* error */
329                 hid_warn(urb->dev, "input irq status %d received\n",
330                          urb->status);
331         }
332
333         status = usb_submit_urb(urb, GFP_ATOMIC);
334         if (status) {
335                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
336                 if (status != -EPERM) {
337                         hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
338                                 hid_to_usb_dev(hid)->bus->bus_name,
339                                 hid_to_usb_dev(hid)->devpath,
340                                 usbhid->ifnum, status);
341                         hid_io_error(hid);
342                 }
343         }
344 }
345
346 static int hid_submit_out(struct hid_device *hid)
347 {
348         struct hid_report *report;
349         char *raw_report;
350         struct usbhid_device *usbhid = hid->driver_data;
351         int r;
352
353         report = usbhid->out[usbhid->outtail].report;
354         raw_report = usbhid->out[usbhid->outtail].raw_report;
355
356         usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
357                                                  1 + (report->id > 0);
358         usbhid->urbout->dev = hid_to_usb_dev(hid);
359         if (raw_report) {
360                 memcpy(usbhid->outbuf, raw_report,
361                                 usbhid->urbout->transfer_buffer_length);
362                 kfree(raw_report);
363                 usbhid->out[usbhid->outtail].raw_report = NULL;
364         }
365
366         dbg_hid("submitting out urb\n");
367
368         r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
369         if (r < 0) {
370                 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
371                 return r;
372         }
373         usbhid->last_out = jiffies;
374         return 0;
375 }
376
377 static int hid_submit_ctrl(struct hid_device *hid)
378 {
379         struct hid_report *report;
380         unsigned char dir;
381         char *raw_report;
382         int len, r;
383         struct usbhid_device *usbhid = hid->driver_data;
384
385         report = usbhid->ctrl[usbhid->ctrltail].report;
386         raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
387         dir = usbhid->ctrl[usbhid->ctrltail].dir;
388
389         len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
390         if (dir == USB_DIR_OUT) {
391                 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
392                 usbhid->urbctrl->transfer_buffer_length = len;
393                 if (raw_report) {
394                         memcpy(usbhid->ctrlbuf, raw_report, len);
395                         kfree(raw_report);
396                         usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
397                 }
398         } else {
399                 int maxpacket, padlen;
400
401                 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
402                 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
403                                           usbhid->urbctrl->pipe, 0);
404                 if (maxpacket > 0) {
405                         padlen = DIV_ROUND_UP(len, maxpacket);
406                         padlen *= maxpacket;
407                         if (padlen > usbhid->bufsize)
408                                 padlen = usbhid->bufsize;
409                 } else
410                         padlen = 0;
411                 usbhid->urbctrl->transfer_buffer_length = padlen;
412         }
413         usbhid->urbctrl->dev = hid_to_usb_dev(hid);
414
415         usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
416         usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
417                                                       HID_REQ_GET_REPORT;
418         usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
419                                          report->id);
420         usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
421         usbhid->cr->wLength = cpu_to_le16(len);
422
423         dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
424                 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
425                                                              "Get_Report",
426                 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
427
428         r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
429         if (r < 0) {
430                 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
431                 return r;
432         }
433         usbhid->last_ctrl = jiffies;
434         return 0;
435 }
436
437 /*
438  * Output interrupt completion handler.
439  */
440
441 static void hid_irq_out(struct urb *urb)
442 {
443         struct hid_device *hid = urb->context;
444         struct usbhid_device *usbhid = hid->driver_data;
445         unsigned long flags;
446         int unplug = 0;
447
448         switch (urb->status) {
449         case 0:                 /* success */
450                 break;
451         case -ESHUTDOWN:        /* unplug */
452                 unplug = 1;
453         case -EILSEQ:           /* protocol error or unplug */
454         case -EPROTO:           /* protocol error or unplug */
455         case -ECONNRESET:       /* unlink */
456         case -ENOENT:
457                 break;
458         default:                /* error */
459                 hid_warn(urb->dev, "output irq status %d received\n",
460                          urb->status);
461         }
462
463         spin_lock_irqsave(&usbhid->lock, flags);
464
465         if (unplug) {
466                 usbhid->outtail = usbhid->outhead;
467         } else {
468                 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
469
470                 if (usbhid->outhead != usbhid->outtail &&
471                                 hid_submit_out(hid) == 0) {
472                         /* Successfully submitted next urb in queue */
473                         spin_unlock_irqrestore(&usbhid->lock, flags);
474                         return;
475                 }
476         }
477
478         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
479         spin_unlock_irqrestore(&usbhid->lock, flags);
480         usb_autopm_put_interface_async(usbhid->intf);
481         wake_up(&usbhid->wait);
482 }
483
484 /*
485  * Control pipe completion handler.
486  */
487
488 static void hid_ctrl(struct urb *urb)
489 {
490         struct hid_device *hid = urb->context;
491         struct usbhid_device *usbhid = hid->driver_data;
492         int unplug = 0, status = urb->status;
493
494         spin_lock(&usbhid->lock);
495
496         switch (status) {
497         case 0:                 /* success */
498                 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
499                         hid_input_report(urb->context,
500                                 usbhid->ctrl[usbhid->ctrltail].report->type,
501                                 urb->transfer_buffer, urb->actual_length, 0);
502                 break;
503         case -ESHUTDOWN:        /* unplug */
504                 unplug = 1;
505         case -EILSEQ:           /* protocol error or unplug */
506         case -EPROTO:           /* protocol error or unplug */
507         case -ECONNRESET:       /* unlink */
508         case -ENOENT:
509         case -EPIPE:            /* report not available */
510                 break;
511         default:                /* error */
512                 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
513         }
514
515         if (unplug) {
516                 usbhid->ctrltail = usbhid->ctrlhead;
517         } else {
518                 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
519
520                 if (usbhid->ctrlhead != usbhid->ctrltail &&
521                                 hid_submit_ctrl(hid) == 0) {
522                         /* Successfully submitted next urb in queue */
523                         spin_unlock(&usbhid->lock);
524                         return;
525                 }
526         }
527
528         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
529         spin_unlock(&usbhid->lock);
530         usb_autopm_put_interface_async(usbhid->intf);
531         wake_up(&usbhid->wait);
532 }
533
534 static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
535                                    unsigned char dir)
536 {
537         int head;
538         struct usbhid_device *usbhid = hid->driver_data;
539
540         if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
541                 return;
542
543         if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
544                 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
545                         hid_warn(hid, "output queue full\n");
546                         return;
547                 }
548
549                 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
550                 if (!usbhid->out[usbhid->outhead].raw_report) {
551                         hid_warn(hid, "output queueing failed\n");
552                         return;
553                 }
554                 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
555                 usbhid->out[usbhid->outhead].report = report;
556                 usbhid->outhead = head;
557
558                 /* If the queue isn't running, restart it */
559                 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
560                         usbhid_restart_out_queue(usbhid);
561
562                 /* Otherwise see if an earlier request has timed out */
563                 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
564
565                         /* Prevent autosuspend following the unlink */
566                         usb_autopm_get_interface_no_resume(usbhid->intf);
567
568                         /*
569                          * Prevent resubmission in case the URB completes
570                          * before we can unlink it.  We don't want to cancel
571                          * the wrong transfer!
572                          */
573                         usb_block_urb(usbhid->urbout);
574
575                         /* Drop lock to avoid deadlock if the callback runs */
576                         spin_unlock(&usbhid->lock);
577
578                         usb_unlink_urb(usbhid->urbout);
579                         spin_lock(&usbhid->lock);
580                         usb_unblock_urb(usbhid->urbout);
581
582                         /* Unlink might have stopped the queue */
583                         if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
584                                 usbhid_restart_out_queue(usbhid);
585
586                         /* Now we can allow autosuspend again */
587                         usb_autopm_put_interface_async(usbhid->intf);
588                 }
589                 return;
590         }
591
592         if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
593                 hid_warn(hid, "control queue full\n");
594                 return;
595         }
596
597         if (dir == USB_DIR_OUT) {
598                 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
599                 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
600                         hid_warn(hid, "control queueing failed\n");
601                         return;
602                 }
603                 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
604         }
605         usbhid->ctrl[usbhid->ctrlhead].report = report;
606         usbhid->ctrl[usbhid->ctrlhead].dir = dir;
607         usbhid->ctrlhead = head;
608
609         /* If the queue isn't running, restart it */
610         if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
611                 usbhid_restart_ctrl_queue(usbhid);
612
613         /* Otherwise see if an earlier request has timed out */
614         } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
615
616                 /* Prevent autosuspend following the unlink */
617                 usb_autopm_get_interface_no_resume(usbhid->intf);
618
619                 /*
620                  * Prevent resubmission in case the URB completes
621                  * before we can unlink it.  We don't want to cancel
622                  * the wrong transfer!
623                  */
624                 usb_block_urb(usbhid->urbctrl);
625
626                 /* Drop lock to avoid deadlock if the callback runs */
627                 spin_unlock(&usbhid->lock);
628
629                 usb_unlink_urb(usbhid->urbctrl);
630                 spin_lock(&usbhid->lock);
631                 usb_unblock_urb(usbhid->urbctrl);
632
633                 /* Unlink might have stopped the queue */
634                 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
635                         usbhid_restart_ctrl_queue(usbhid);
636
637                 /* Now we can allow autosuspend again */
638                 usb_autopm_put_interface_async(usbhid->intf);
639         }
640 }
641
642 static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
643 {
644         struct usbhid_device *usbhid = hid->driver_data;
645         unsigned long flags;
646
647         spin_lock_irqsave(&usbhid->lock, flags);
648         __usbhid_submit_report(hid, report, dir);
649         spin_unlock_irqrestore(&usbhid->lock, flags);
650 }
651
652 /* Workqueue routine to send requests to change LEDs */
653 static void hid_led(struct work_struct *work)
654 {
655         struct usbhid_device *usbhid =
656                 container_of(work, struct usbhid_device, led_work);
657         struct hid_device *hid = usbhid->hid;
658         struct hid_field *field;
659         unsigned long flags;
660
661         field = hidinput_get_led_field(hid);
662         if (!field) {
663                 hid_warn(hid, "LED event field not found\n");
664                 return;
665         }
666
667         spin_lock_irqsave(&usbhid->lock, flags);
668         if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
669                 usbhid->ledcount = hidinput_count_leds(hid);
670                 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
671                 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
672         }
673         spin_unlock_irqrestore(&usbhid->lock, flags);
674 }
675
676 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
677 {
678         struct hid_device *hid = input_get_drvdata(dev);
679         struct usbhid_device *usbhid = hid->driver_data;
680         struct hid_field *field;
681         unsigned long flags;
682         int offset;
683
684         if (type == EV_FF)
685                 return input_ff_event(dev, type, code, value);
686
687         if (type != EV_LED)
688                 return -1;
689
690         if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
691                 hid_warn(dev, "event field not found\n");
692                 return -1;
693         }
694
695         spin_lock_irqsave(&usbhid->lock, flags);
696         hid_set_field(field, offset, value);
697         spin_unlock_irqrestore(&usbhid->lock, flags);
698
699         /*
700          * Defer performing requested LED action.
701          * This is more likely gather all LED changes into a single URB.
702          */
703         schedule_work(&usbhid->led_work);
704
705         return 0;
706 }
707
708 static int usbhid_wait_io(struct hid_device *hid)
709 {
710         struct usbhid_device *usbhid = hid->driver_data;
711
712         if (!wait_event_timeout(usbhid->wait,
713                                 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
714                                 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
715                                         10*HZ)) {
716                 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
717                 return -1;
718         }
719
720         return 0;
721 }
722
723 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
724 {
725         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
726                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
727                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
728 }
729
730 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
731                 unsigned char type, void *buf, int size)
732 {
733         int result, retries = 4;
734
735         memset(buf, 0, size);
736
737         do {
738                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
739                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
740                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
741                 retries--;
742         } while (result < size && retries);
743         return result;
744 }
745
746 int usbhid_open(struct hid_device *hid)
747 {
748         struct usbhid_device *usbhid = hid->driver_data;
749         int res = 0;
750
751         mutex_lock(&hid_open_mut);
752         if (!hid->open++) {
753                 res = usb_autopm_get_interface(usbhid->intf);
754                 /* the device must be awake to reliably request remote wakeup */
755                 if (res < 0) {
756                         hid->open--;
757                         res = -EIO;
758                         goto done;
759                 }
760                 usbhid->intf->needs_remote_wakeup = 1;
761                 res = hid_start_in(hid);
762                 if (res) {
763                         if (res != -ENOSPC) {
764                                 hid_io_error(hid);
765                                 res = 0;
766                         } else {
767                                 /* no use opening if resources are insufficient */
768                                 hid->open--;
769                                 res = -EBUSY;
770                                 usbhid->intf->needs_remote_wakeup = 0;
771                         }
772                 }
773                 usb_autopm_put_interface(usbhid->intf);
774         }
775 done:
776         mutex_unlock(&hid_open_mut);
777         return res;
778 }
779
780 void usbhid_close(struct hid_device *hid)
781 {
782         struct usbhid_device *usbhid = hid->driver_data;
783
784         mutex_lock(&hid_open_mut);
785
786         /* protecting hid->open to make sure we don't restart
787          * data acquistion due to a resumption we no longer
788          * care about
789          */
790         spin_lock_irq(&usbhid->lock);
791         if (!--hid->open) {
792                 spin_unlock_irq(&usbhid->lock);
793                 hid_cancel_delayed_stuff(usbhid);
794                 usb_kill_urb(usbhid->urbin);
795                 usbhid->intf->needs_remote_wakeup = 0;
796         } else {
797                 spin_unlock_irq(&usbhid->lock);
798         }
799         mutex_unlock(&hid_open_mut);
800 }
801
802 /*
803  * Initialize all reports
804  */
805
806 void usbhid_init_reports(struct hid_device *hid)
807 {
808         struct hid_report *report;
809         struct usbhid_device *usbhid = hid->driver_data;
810         int err, ret;
811
812         list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
813                 usbhid_submit_report(hid, report, USB_DIR_IN);
814
815         list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
816                 usbhid_submit_report(hid, report, USB_DIR_IN);
817
818         err = 0;
819         ret = usbhid_wait_io(hid);
820         while (ret) {
821                 err |= ret;
822                 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
823                         usb_kill_urb(usbhid->urbctrl);
824                 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
825                         usb_kill_urb(usbhid->urbout);
826                 ret = usbhid_wait_io(hid);
827         }
828
829         if (err)
830                 hid_warn(hid, "timeout initializing reports\n");
831 }
832
833 /*
834  * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
835  */
836 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
837     unsigned int hid_code, struct hid_field **pfield)
838 {
839         struct hid_report *report;
840         struct hid_field *field;
841         struct hid_usage *usage;
842         int i, j;
843
844         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
845                 for (i = 0; i < report->maxfield; i++) {
846                         field = report->field[i];
847                         for (j = 0; j < field->maxusage; j++) {
848                                 usage = &field->usage[j];
849                                 if ((usage->hid & HID_USAGE_PAGE) == page &&
850                                     (usage->hid & 0xFFFF) == hid_code) {
851                                         *pfield = field;
852                                         return j;
853                                 }
854                         }
855                 }
856         }
857         return -1;
858 }
859
860 void usbhid_set_leds(struct hid_device *hid)
861 {
862         struct hid_field *field;
863         int offset;
864
865         if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
866                 hid_set_field(field, offset, 0);
867                 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
868         }
869 }
870 EXPORT_SYMBOL_GPL(usbhid_set_leds);
871
872 /*
873  * Traverse the supplied list of reports and find the longest
874  */
875 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
876                 unsigned int *max)
877 {
878         struct hid_report *report;
879         unsigned int size;
880
881         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
882                 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
883                 if (*max < size)
884                         *max = size;
885         }
886 }
887
888 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
889 {
890         struct usbhid_device *usbhid = hid->driver_data;
891
892         usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
893                         &usbhid->inbuf_dma);
894         usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
895                         &usbhid->outbuf_dma);
896         usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
897         usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
898                         &usbhid->ctrlbuf_dma);
899         if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
900                         !usbhid->ctrlbuf)
901                 return -1;
902
903         return 0;
904 }
905
906 static int usbhid_get_raw_report(struct hid_device *hid,
907                 unsigned char report_number, __u8 *buf, size_t count,
908                 unsigned char report_type)
909 {
910         struct usbhid_device *usbhid = hid->driver_data;
911         struct usb_device *dev;
912         struct usb_interface *intf;
913         struct usb_host_interface *interface;
914         int skipped_report_id = 0;
915         int ret;
916
917         intf = usbhid->intf;
918         if (intf == NULL) {
919                 pr_err("%s: no USB intf\n", __func__);
920                 return -ESHUTDOWN;
921         }
922         spin_lock_irq(&usbhid->lock);
923         if (test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
924                 pr_err("hid device disconnected\n");
925                 spin_unlock_irq(&usbhid->lock);
926                 return -ESHUTDOWN;
927         }
928         spin_unlock_irq(&usbhid->lock);
929
930         dev = hid_to_usb_dev(hid);
931         interface = intf->cur_altsetting;
932
933         /* Byte 0 is the report number. Report data starts at byte 1.*/
934         buf[0] = report_number;
935         if (report_number == 0x0) {
936                 /* Offset the return buffer by 1, so that the report ID
937                    will remain in byte 0. */
938                 buf++;
939                 count--;
940                 skipped_report_id = 1;
941         }
942         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
943                 HID_REQ_GET_REPORT,
944                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
945                 ((report_type + 1) << 8) | report_number,
946                 interface->desc.bInterfaceNumber, buf, count,
947                 USB_CTRL_SET_TIMEOUT);
948
949         /* count also the report id */
950         if (ret > 0 && skipped_report_id)
951                 ret++;
952
953         return ret;
954 }
955
956 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
957                 unsigned char report_type)
958 {
959         struct usbhid_device *usbhid = hid->driver_data;
960         struct usb_device *dev = hid_to_usb_dev(hid);
961         struct usb_interface *intf = usbhid->intf;
962         struct usb_host_interface *interface = intf->cur_altsetting;
963         int ret;
964
965         if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
966                 int actual_length;
967                 int skipped_report_id = 0;
968
969                 if (buf[0] == 0x0) {
970                         /* Don't send the Report ID */
971                         buf++;
972                         count--;
973                         skipped_report_id = 1;
974                 }
975                 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
976                         buf, count, &actual_length,
977                         USB_CTRL_SET_TIMEOUT);
978                 /* return the number of bytes transferred */
979                 if (ret == 0) {
980                         ret = actual_length;
981                         /* count also the report id */
982                         if (skipped_report_id)
983                                 ret++;
984                 }
985         } else {
986                 int skipped_report_id = 0;
987                 int report_id = buf[0];
988                 if (buf[0] == 0x0) {
989                         /* Don't send the Report ID */
990                         buf++;
991                         count--;
992                         skipped_report_id = 1;
993                 }
994                 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
995                         HID_REQ_SET_REPORT,
996                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
997                         ((report_type + 1) << 8) | report_id,
998                         interface->desc.bInterfaceNumber, buf, count,
999                         USB_CTRL_SET_TIMEOUT);
1000                 /* count also the report id, if this was a numbered report. */
1001                 if (ret > 0 && skipped_report_id)
1002                         ret++;
1003         }
1004
1005         return ret;
1006 }
1007
1008 static void usbhid_restart_queues(struct usbhid_device *usbhid)
1009 {
1010         if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
1011                 usbhid_restart_out_queue(usbhid);
1012         if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
1013                 usbhid_restart_ctrl_queue(usbhid);
1014 }
1015
1016 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1017 {
1018         struct usbhid_device *usbhid = hid->driver_data;
1019
1020         usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1021         usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
1022         kfree(usbhid->cr);
1023         usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
1024 }
1025
1026 static int usbhid_parse(struct hid_device *hid)
1027 {
1028         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1029         struct usb_host_interface *interface = intf->cur_altsetting;
1030         struct usb_device *dev = interface_to_usbdev (intf);
1031         struct hid_descriptor *hdesc;
1032         u32 quirks = 0;
1033         unsigned int rsize = 0;
1034         char *rdesc;
1035         int ret, n;
1036
1037         quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
1038                         le16_to_cpu(dev->descriptor.idProduct));
1039
1040         if (quirks & HID_QUIRK_IGNORE)
1041                 return -ENODEV;
1042
1043         /* Many keyboards and mice don't like to be polled for reports,
1044          * so we will always set the HID_QUIRK_NOGET flag for them. */
1045         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1046                 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1047                         interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1048                                 quirks |= HID_QUIRK_NOGET;
1049         }
1050
1051         if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1052             (!interface->desc.bNumEndpoints ||
1053              usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1054                 dbg_hid("class descriptor not present\n");
1055                 return -ENODEV;
1056         }
1057
1058         hid->version = le16_to_cpu(hdesc->bcdHID);
1059         hid->country = hdesc->bCountryCode;
1060
1061         for (n = 0; n < hdesc->bNumDescriptors; n++)
1062                 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1063                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1064
1065         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1066                 dbg_hid("weird size of report descriptor (%u)\n", rsize);
1067                 return -EINVAL;
1068         }
1069
1070         if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1071                 dbg_hid("couldn't allocate rdesc memory\n");
1072                 return -ENOMEM;
1073         }
1074
1075         hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1076
1077         ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1078                         HID_DT_REPORT, rdesc, rsize);
1079         if (ret < 0) {
1080                 dbg_hid("reading report descriptor failed\n");
1081                 kfree(rdesc);
1082                 goto err;
1083         }
1084
1085         ret = hid_parse_report(hid, rdesc, rsize);
1086         kfree(rdesc);
1087         if (ret) {
1088                 dbg_hid("parsing report descriptor failed\n");
1089                 goto err;
1090         }
1091
1092         hid->quirks |= quirks;
1093
1094         return 0;
1095 err:
1096         return ret;
1097 }
1098
1099 static int usbhid_start(struct hid_device *hid)
1100 {
1101         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1102         struct usb_host_interface *interface = intf->cur_altsetting;
1103         struct usb_device *dev = interface_to_usbdev(intf);
1104         struct usbhid_device *usbhid = hid->driver_data;
1105         unsigned int n, insize = 0;
1106         int ret;
1107
1108         clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1109
1110         usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1111         hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1112         hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1113         hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1114
1115         if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1116                 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
1117
1118         hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1119
1120         if (insize > HID_MAX_BUFFER_SIZE)
1121                 insize = HID_MAX_BUFFER_SIZE;
1122
1123         if (hid_alloc_buffers(dev, hid)) {
1124                 ret = -ENOMEM;
1125                 goto fail;
1126         }
1127
1128         for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1129                 struct usb_endpoint_descriptor *endpoint;
1130                 int pipe;
1131                 int interval;
1132
1133                 endpoint = &interface->endpoint[n].desc;
1134                 if (!usb_endpoint_xfer_int(endpoint))
1135                         continue;
1136
1137                 interval = endpoint->bInterval;
1138
1139                 /* Some vendors give fullspeed interval on highspeed devides */
1140                 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
1141                     dev->speed == USB_SPEED_HIGH) {
1142                         interval = fls(endpoint->bInterval*8);
1143                         printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1144                                hid->name, endpoint->bInterval, interval);
1145                 }
1146
1147                 /* Change the polling interval of mice. */
1148                 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1149                         interval = hid_mousepoll_interval;
1150
1151                 ret = -ENOMEM;
1152                 if (usb_endpoint_dir_in(endpoint)) {
1153                         if (usbhid->urbin)
1154                                 continue;
1155                         if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1156                                 goto fail;
1157                         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1158                         usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1159                                          hid_irq_in, hid, interval);
1160                         usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1161                         usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1162                 } else {
1163                         if (usbhid->urbout)
1164                                 continue;
1165                         if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1166                                 goto fail;
1167                         pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1168                         usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1169                                          hid_irq_out, hid, interval);
1170                         usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1171                         usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1172                 }
1173         }
1174
1175         usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1176         if (!usbhid->urbctrl) {
1177                 ret = -ENOMEM;
1178                 goto fail;
1179         }
1180
1181         usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1182                              usbhid->ctrlbuf, 1, hid_ctrl, hid);
1183         usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1184         usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1185
1186         if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1187                 usbhid_init_reports(hid);
1188
1189         set_bit(HID_STARTED, &usbhid->iofl);
1190
1191         /* Some keyboards don't work until their LEDs have been set.
1192          * Since BIOSes do set the LEDs, it must be safe for any device
1193          * that supports the keyboard boot protocol.
1194          * In addition, enable remote wakeup by default for all keyboard
1195          * devices supporting the boot protocol.
1196          */
1197         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1198                         interface->desc.bInterfaceProtocol ==
1199                                 USB_INTERFACE_PROTOCOL_KEYBOARD) {
1200                 usbhid_set_leds(hid);
1201                 device_set_wakeup_enable(&dev->dev, 1);
1202                 usb_disable_autosuspend(dev);
1203         }
1204 #ifdef CONFIG_USB_EHCI_TEGRA
1205         else if (interface->desc.bInterfaceProtocol ==
1206                                 USB_INTERFACE_PROTOCOL_MOUSE)
1207                 usb_disable_autosuspend(dev);
1208 #endif
1209         return 0;
1210
1211 fail:
1212         usb_free_urb(usbhid->urbin);
1213         usb_free_urb(usbhid->urbout);
1214         usb_free_urb(usbhid->urbctrl);
1215         usbhid->urbin = NULL;
1216         usbhid->urbout = NULL;
1217         usbhid->urbctrl = NULL;
1218         hid_free_buffers(dev, hid);
1219         return ret;
1220 }
1221
1222 static void usbhid_stop(struct hid_device *hid)
1223 {
1224         struct usbhid_device *usbhid = hid->driver_data;
1225
1226         if (WARN_ON(!usbhid))
1227                 return;
1228
1229         clear_bit(HID_STARTED, &usbhid->iofl);
1230         spin_lock_irq(&usbhid->lock);   /* Sync with error and led handlers */
1231         set_bit(HID_DISCONNECTED, &usbhid->iofl);
1232         spin_unlock_irq(&usbhid->lock);
1233         usb_kill_urb(usbhid->urbin);
1234         usb_kill_urb(usbhid->urbout);
1235         usb_kill_urb(usbhid->urbctrl);
1236
1237         hid_cancel_delayed_stuff(usbhid);
1238
1239         hid->claimed = 0;
1240
1241         usb_free_urb(usbhid->urbin);
1242         usb_free_urb(usbhid->urbctrl);
1243         usb_free_urb(usbhid->urbout);
1244         usbhid->urbin = NULL; /* don't mess up next start */
1245         usbhid->urbctrl = NULL;
1246         usbhid->urbout = NULL;
1247
1248         hid_free_buffers(hid_to_usb_dev(hid), hid);
1249 }
1250
1251 static int usbhid_power(struct hid_device *hid, int lvl)
1252 {
1253         int r = 0;
1254
1255         switch (lvl) {
1256         case PM_HINT_FULLON:
1257                 r = usbhid_get_power(hid);
1258                 break;
1259         case PM_HINT_NORMAL:
1260                 usbhid_put_power(hid);
1261                 break;
1262         }
1263         return r;
1264 }
1265
1266 static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1267 {
1268         switch (reqtype) {
1269         case HID_REQ_GET_REPORT:
1270                 usbhid_submit_report(hid, rep, USB_DIR_IN);
1271                 break;
1272         case HID_REQ_SET_REPORT:
1273                 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1274                 break;
1275         }
1276 }
1277
1278 static int usbhid_idle(struct hid_device *hid, int report, int idle,
1279                 int reqtype)
1280 {
1281         struct usb_device *dev = hid_to_usb_dev(hid);
1282         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1283         struct usb_host_interface *interface = intf->cur_altsetting;
1284         int ifnum = interface->desc.bInterfaceNumber;
1285
1286         if (reqtype != HID_REQ_SET_IDLE)
1287                 return -EINVAL;
1288
1289         return hid_set_idle(dev, ifnum, report, idle);
1290 }
1291
1292 static struct hid_ll_driver usb_hid_driver = {
1293         .parse = usbhid_parse,
1294         .start = usbhid_start,
1295         .stop = usbhid_stop,
1296         .open = usbhid_open,
1297         .close = usbhid_close,
1298         .power = usbhid_power,
1299         .hidinput_input_event = usb_hidinput_input_event,
1300         .request = usbhid_request,
1301         .wait = usbhid_wait_io,
1302         .idle = usbhid_idle,
1303 };
1304
1305 static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1306 {
1307         struct usb_host_interface *interface = intf->cur_altsetting;
1308         struct usb_device *dev = interface_to_usbdev(intf);
1309         struct usbhid_device *usbhid;
1310         struct hid_device *hid;
1311         unsigned int n, has_in = 0;
1312         size_t len;
1313         int ret;
1314
1315         dbg_hid("HID probe called for ifnum %d\n",
1316                         intf->altsetting->desc.bInterfaceNumber);
1317
1318         for (n = 0; n < interface->desc.bNumEndpoints; n++)
1319                 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1320                         has_in++;
1321         if (!has_in) {
1322                 hid_err(intf, "couldn't find an input interrupt endpoint\n");
1323                 return -ENODEV;
1324         }
1325
1326         hid = hid_allocate_device();
1327         if (IS_ERR(hid))
1328                 return PTR_ERR(hid);
1329
1330         usb_set_intfdata(intf, hid);
1331         hid->ll_driver = &usb_hid_driver;
1332         hid->hid_get_raw_report = usbhid_get_raw_report;
1333         hid->hid_output_raw_report = usbhid_output_raw_report;
1334         hid->ff_init = hid_pidff_init;
1335 #ifdef CONFIG_USB_HIDDEV
1336         hid->hiddev_connect = hiddev_connect;
1337         hid->hiddev_disconnect = hiddev_disconnect;
1338         hid->hiddev_hid_event = hiddev_hid_event;
1339         hid->hiddev_report_event = hiddev_report_event;
1340 #endif
1341         hid->dev.parent = &intf->dev;
1342         hid->bus = BUS_USB;
1343         hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1344         hid->product = le16_to_cpu(dev->descriptor.idProduct);
1345         hid->name[0] = 0;
1346         hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
1347         if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1348                         USB_INTERFACE_PROTOCOL_MOUSE)
1349                 hid->type = HID_TYPE_USBMOUSE;
1350         else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1351                 hid->type = HID_TYPE_USBNONE;
1352
1353         if (dev->manufacturer)
1354                 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1355
1356         if (dev->product) {
1357                 if (dev->manufacturer)
1358                         strlcat(hid->name, " ", sizeof(hid->name));
1359                 strlcat(hid->name, dev->product, sizeof(hid->name));
1360         }
1361
1362         if (!strlen(hid->name))
1363                 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1364                          le16_to_cpu(dev->descriptor.idVendor),
1365                          le16_to_cpu(dev->descriptor.idProduct));
1366
1367         usb_make_path(dev, hid->phys, sizeof(hid->phys));
1368         strlcat(hid->phys, "/input", sizeof(hid->phys));
1369         len = strlen(hid->phys);
1370         if (len < sizeof(hid->phys) - 1)
1371                 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1372                          "%d", intf->altsetting[0].desc.bInterfaceNumber);
1373
1374         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1375                 hid->uniq[0] = 0;
1376
1377         usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1378         if (usbhid == NULL) {
1379                 ret = -ENOMEM;
1380                 goto err;
1381         }
1382
1383         hid->driver_data = usbhid;
1384         usbhid->hid = hid;
1385         usbhid->intf = intf;
1386         usbhid->ifnum = interface->desc.bInterfaceNumber;
1387
1388         init_waitqueue_head(&usbhid->wait);
1389         INIT_WORK(&usbhid->reset_work, hid_reset);
1390         setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1391         spin_lock_init(&usbhid->lock);
1392
1393         INIT_WORK(&usbhid->led_work, hid_led);
1394
1395         ret = hid_add_device(hid);
1396         if (ret) {
1397                 if (ret != -ENODEV)
1398                         hid_err(intf, "can't add hid device: %d\n", ret);
1399                 goto err_free;
1400         }
1401
1402         return 0;
1403 err_free:
1404         kfree(usbhid);
1405 err:
1406         hid_destroy_device(hid);
1407         return ret;
1408 }
1409
1410 static void usbhid_disconnect(struct usb_interface *intf)
1411 {
1412         struct hid_device *hid = usb_get_intfdata(intf);
1413         struct usbhid_device *usbhid;
1414
1415         if (WARN_ON(!hid))
1416                 return;
1417
1418         usbhid = hid->driver_data;
1419         spin_lock_irq(&usbhid->lock);
1420         set_bit(HID_DISCONNECTED, &usbhid->iofl);
1421         spin_unlock_irq(&usbhid->lock);
1422
1423         hid_destroy_device(hid);
1424         kfree(usbhid);
1425 }
1426
1427 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1428 {
1429         del_timer_sync(&usbhid->io_retry);
1430         cancel_work_sync(&usbhid->reset_work);
1431         cancel_work_sync(&usbhid->led_work);
1432 }
1433
1434 static void hid_cease_io(struct usbhid_device *usbhid)
1435 {
1436         del_timer_sync(&usbhid->io_retry);
1437         usb_kill_urb(usbhid->urbin);
1438         usb_kill_urb(usbhid->urbctrl);
1439         usb_kill_urb(usbhid->urbout);
1440 }
1441
1442 /* Treat USB reset pretty much the same as suspend/resume */
1443 static int hid_pre_reset(struct usb_interface *intf)
1444 {
1445         struct hid_device *hid = usb_get_intfdata(intf);
1446         struct usbhid_device *usbhid = hid->driver_data;
1447
1448         spin_lock_irq(&usbhid->lock);
1449         set_bit(HID_RESET_PENDING, &usbhid->iofl);
1450         spin_unlock_irq(&usbhid->lock);
1451         hid_cease_io(usbhid);
1452
1453         return 0;
1454 }
1455
1456 /* Same routine used for post_reset and reset_resume */
1457 static int hid_post_reset(struct usb_interface *intf)
1458 {
1459         struct usb_device *dev = interface_to_usbdev (intf);
1460         struct hid_device *hid = usb_get_intfdata(intf);
1461         struct usbhid_device *usbhid = hid->driver_data;
1462         struct usb_host_interface *interface = intf->cur_altsetting;
1463         int status;
1464         char *rdesc;
1465
1466         /* Fetch and examine the HID report descriptor. If this
1467          * has changed, then rebind. Since usbcore's check of the
1468          * configuration descriptors passed, we already know that
1469          * the size of the HID report descriptor has not changed.
1470          */
1471         rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
1472         if (!rdesc) {
1473                 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1474                 return 1;
1475         }
1476         status = hid_get_class_descriptor(dev,
1477                                 interface->desc.bInterfaceNumber,
1478                                 HID_DT_REPORT, rdesc, hid->dev_rsize);
1479         if (status < 0) {
1480                 dbg_hid("reading report descriptor failed (post_reset)\n");
1481                 kfree(rdesc);
1482                 return 1;
1483         }
1484         status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
1485         kfree(rdesc);
1486         if (status != 0) {
1487                 dbg_hid("report descriptor changed\n");
1488                 return 1;
1489         }
1490
1491         spin_lock_irq(&usbhid->lock);
1492         clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1493         spin_unlock_irq(&usbhid->lock);
1494         hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1495         status = hid_start_in(hid);
1496         if (status < 0)
1497                 hid_io_error(hid);
1498         usbhid_restart_queues(usbhid);
1499
1500         return 0;
1501 }
1502
1503 int usbhid_get_power(struct hid_device *hid)
1504 {
1505         struct usbhid_device *usbhid = hid->driver_data;
1506
1507         return usb_autopm_get_interface(usbhid->intf);
1508 }
1509
1510 void usbhid_put_power(struct hid_device *hid)
1511 {
1512         struct usbhid_device *usbhid = hid->driver_data;
1513
1514         usb_autopm_put_interface(usbhid->intf);
1515 }
1516
1517
1518 #ifdef CONFIG_PM
1519 static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1520 {
1521         struct usbhid_device *usbhid = hid->driver_data;
1522         int status;
1523
1524         spin_lock_irq(&usbhid->lock);
1525         clear_bit(HID_SUSPENDED, &usbhid->iofl);
1526         usbhid_mark_busy(usbhid);
1527
1528         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1529                         test_bit(HID_RESET_PENDING, &usbhid->iofl))
1530                 schedule_work(&usbhid->reset_work);
1531         usbhid->retry_delay = 0;
1532
1533         usbhid_restart_queues(usbhid);
1534         spin_unlock_irq(&usbhid->lock);
1535
1536         status = hid_start_in(hid);
1537         if (status < 0)
1538                 hid_io_error(hid);
1539
1540         if (driver_suspended && hid->driver && hid->driver->resume)
1541                 status = hid->driver->resume(hid);
1542         return status;
1543 }
1544
1545 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1546 {
1547         struct hid_device *hid = usb_get_intfdata(intf);
1548         struct usbhid_device *usbhid = hid->driver_data;
1549         int status = 0;
1550         bool driver_suspended = false;
1551
1552         if (PMSG_IS_AUTO(message)) {
1553                 spin_lock_irq(&usbhid->lock);   /* Sync with error handler */
1554                 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1555                     && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1556                     && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1557                     && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1558                     && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1559                     && (!usbhid->ledcount || ignoreled))
1560                 {
1561                         set_bit(HID_SUSPENDED, &usbhid->iofl);
1562                         spin_unlock_irq(&usbhid->lock);
1563                         if (hid->driver && hid->driver->suspend) {
1564                                 status = hid->driver->suspend(hid, message);
1565                                 if (status < 0)
1566                                         goto failed;
1567                         }
1568                         driver_suspended = true;
1569                 } else {
1570                         usbhid_mark_busy(usbhid);
1571                         spin_unlock_irq(&usbhid->lock);
1572                         return -EBUSY;
1573                 }
1574
1575         } else {
1576                 /* TODO: resume() might need to handle suspend failure */
1577                 if (hid->driver && hid->driver->suspend)
1578                         status = hid->driver->suspend(hid, message);
1579                 driver_suspended = true;
1580                 spin_lock_irq(&usbhid->lock);
1581                 set_bit(HID_SUSPENDED, &usbhid->iofl);
1582                 spin_unlock_irq(&usbhid->lock);
1583                 if (usbhid_wait_io(hid) < 0)
1584                         status = -EIO;
1585         }
1586
1587         hid_cancel_delayed_stuff(usbhid);
1588         hid_cease_io(usbhid);
1589
1590         if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1591                 /* lost race against keypresses */
1592                 status = -EBUSY;
1593                 goto failed;
1594         }
1595         dev_dbg(&intf->dev, "suspend\n");
1596         return status;
1597
1598  failed:
1599         hid_resume_common(hid, driver_suspended);
1600         return status;
1601 }
1602
1603 static int hid_resume(struct usb_interface *intf)
1604 {
1605         struct hid_device *hid = usb_get_intfdata (intf);
1606         struct usbhid_device *usbhid = hid->driver_data;
1607         int status;
1608
1609         if (!test_bit(HID_STARTED, &usbhid->iofl))
1610                 return 0;
1611
1612         status = hid_resume_common(hid, true);
1613         dev_dbg(&intf->dev, "resume status %d\n", status);
1614         return 0;
1615 }
1616
1617 static int hid_reset_resume(struct usb_interface *intf)
1618 {
1619         struct hid_device *hid = usb_get_intfdata(intf);
1620         struct usbhid_device *usbhid = hid->driver_data;
1621         int status;
1622
1623         clear_bit(HID_SUSPENDED, &usbhid->iofl);
1624         status = hid_post_reset(intf);
1625         if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1626                 int ret = hid->driver->reset_resume(hid);
1627                 if (ret < 0)
1628                         status = ret;
1629         }
1630         return status;
1631 }
1632
1633 #endif /* CONFIG_PM */
1634
1635 static const struct usb_device_id hid_usb_ids[] = {
1636         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1637                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1638         { }                                             /* Terminating entry */
1639 };
1640
1641 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1642
1643 static struct usb_driver hid_driver = {
1644         .name =         "usbhid",
1645         .probe =        usbhid_probe,
1646         .disconnect =   usbhid_disconnect,
1647 #ifdef CONFIG_PM
1648         .suspend =      hid_suspend,
1649         .resume =       hid_resume,
1650         .reset_resume = hid_reset_resume,
1651 #endif
1652         .pre_reset =    hid_pre_reset,
1653         .post_reset =   hid_post_reset,
1654         .id_table =     hid_usb_ids,
1655         .supports_autosuspend = 1,
1656 };
1657
1658 struct usb_interface *usbhid_find_interface(int minor)
1659 {
1660         return usb_find_interface(&hid_driver, minor);
1661 }
1662
1663 static int __init hid_init(void)
1664 {
1665         int retval = -ENOMEM;
1666
1667         retval = usbhid_quirks_init(quirks_param);
1668         if (retval)
1669                 goto usbhid_quirks_init_fail;
1670         retval = usb_register(&hid_driver);
1671         if (retval)
1672                 goto usb_register_fail;
1673         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1674
1675         return 0;
1676 usb_register_fail:
1677         usbhid_quirks_exit();
1678 usbhid_quirks_init_fail:
1679         return retval;
1680 }
1681
1682 static void __exit hid_exit(void)
1683 {
1684         usb_deregister(&hid_driver);
1685         usbhid_quirks_exit();
1686 }
1687
1688 module_init(hid_init);
1689 module_exit(hid_exit);
1690
1691 MODULE_AUTHOR("Andreas Gal");
1692 MODULE_AUTHOR("Vojtech Pavlik");
1693 MODULE_AUTHOR("Jiri Kosina");
1694 MODULE_DESCRIPTION(DRIVER_DESC);
1695 MODULE_LICENSE(DRIVER_LICENSE);