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