]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/hid/usbhid/hid-core.c
HID: usbhid: use generic hidinput_input_event()
[karo-tx-linux.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 static int usbhid_wait_io(struct hid_device *hid)
653 {
654         struct usbhid_device *usbhid = hid->driver_data;
655
656         if (!wait_event_timeout(usbhid->wait,
657                                 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
658                                 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
659                                         10*HZ)) {
660                 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
661                 return -1;
662         }
663
664         return 0;
665 }
666
667 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
668 {
669         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
670                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
671                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
672 }
673
674 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
675                 unsigned char type, void *buf, int size)
676 {
677         int result, retries = 4;
678
679         memset(buf, 0, size);
680
681         do {
682                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
683                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
684                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
685                 retries--;
686         } while (result < size && retries);
687         return result;
688 }
689
690 int usbhid_open(struct hid_device *hid)
691 {
692         struct usbhid_device *usbhid = hid->driver_data;
693         int res = 0;
694
695         mutex_lock(&hid_open_mut);
696         if (!hid->open++) {
697                 res = usb_autopm_get_interface(usbhid->intf);
698                 /* the device must be awake to reliably request remote wakeup */
699                 if (res < 0) {
700                         hid->open--;
701                         res = -EIO;
702                         goto done;
703                 }
704                 usbhid->intf->needs_remote_wakeup = 1;
705                 res = hid_start_in(hid);
706                 if (res) {
707                         if (res != -ENOSPC) {
708                                 hid_io_error(hid);
709                                 res = 0;
710                         } else {
711                                 /* no use opening if resources are insufficient */
712                                 hid->open--;
713                                 res = -EBUSY;
714                                 usbhid->intf->needs_remote_wakeup = 0;
715                         }
716                 }
717                 usb_autopm_put_interface(usbhid->intf);
718         }
719 done:
720         mutex_unlock(&hid_open_mut);
721         return res;
722 }
723
724 void usbhid_close(struct hid_device *hid)
725 {
726         struct usbhid_device *usbhid = hid->driver_data;
727
728         mutex_lock(&hid_open_mut);
729
730         /* protecting hid->open to make sure we don't restart
731          * data acquistion due to a resumption we no longer
732          * care about
733          */
734         spin_lock_irq(&usbhid->lock);
735         if (!--hid->open) {
736                 spin_unlock_irq(&usbhid->lock);
737                 hid_cancel_delayed_stuff(usbhid);
738                 usb_kill_urb(usbhid->urbin);
739                 usbhid->intf->needs_remote_wakeup = 0;
740         } else {
741                 spin_unlock_irq(&usbhid->lock);
742         }
743         mutex_unlock(&hid_open_mut);
744 }
745
746 /*
747  * Initialize all reports
748  */
749
750 void usbhid_init_reports(struct hid_device *hid)
751 {
752         struct hid_report *report;
753         struct usbhid_device *usbhid = hid->driver_data;
754         int err, ret;
755
756         list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
757                 usbhid_submit_report(hid, report, USB_DIR_IN);
758
759         list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
760                 usbhid_submit_report(hid, report, USB_DIR_IN);
761
762         err = 0;
763         ret = usbhid_wait_io(hid);
764         while (ret) {
765                 err |= ret;
766                 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
767                         usb_kill_urb(usbhid->urbctrl);
768                 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
769                         usb_kill_urb(usbhid->urbout);
770                 ret = usbhid_wait_io(hid);
771         }
772
773         if (err)
774                 hid_warn(hid, "timeout initializing reports\n");
775 }
776
777 /*
778  * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
779  */
780 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
781     unsigned int hid_code, struct hid_field **pfield)
782 {
783         struct hid_report *report;
784         struct hid_field *field;
785         struct hid_usage *usage;
786         int i, j;
787
788         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
789                 for (i = 0; i < report->maxfield; i++) {
790                         field = report->field[i];
791                         for (j = 0; j < field->maxusage; j++) {
792                                 usage = &field->usage[j];
793                                 if ((usage->hid & HID_USAGE_PAGE) == page &&
794                                     (usage->hid & 0xFFFF) == hid_code) {
795                                         *pfield = field;
796                                         return j;
797                                 }
798                         }
799                 }
800         }
801         return -1;
802 }
803
804 static void usbhid_set_leds(struct hid_device *hid)
805 {
806         struct hid_field *field;
807         int offset;
808
809         if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
810                 hid_set_field(field, offset, 0);
811                 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
812         }
813 }
814
815 /*
816  * Traverse the supplied list of reports and find the longest
817  */
818 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
819                 unsigned int *max)
820 {
821         struct hid_report *report;
822         unsigned int size;
823
824         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
825                 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
826                 if (*max < size)
827                         *max = size;
828         }
829 }
830
831 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
832 {
833         struct usbhid_device *usbhid = hid->driver_data;
834
835         usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
836                         &usbhid->inbuf_dma);
837         usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
838                         &usbhid->outbuf_dma);
839         usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
840         usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
841                         &usbhid->ctrlbuf_dma);
842         if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
843                         !usbhid->ctrlbuf)
844                 return -1;
845
846         return 0;
847 }
848
849 static int usbhid_get_raw_report(struct hid_device *hid,
850                 unsigned char report_number, __u8 *buf, size_t count,
851                 unsigned char report_type)
852 {
853         struct usbhid_device *usbhid = hid->driver_data;
854         struct usb_device *dev = hid_to_usb_dev(hid);
855         struct usb_interface *intf = usbhid->intf;
856         struct usb_host_interface *interface = intf->cur_altsetting;
857         int skipped_report_id = 0;
858         int ret;
859
860         /* Byte 0 is the report number. Report data starts at byte 1.*/
861         buf[0] = report_number;
862         if (report_number == 0x0) {
863                 /* Offset the return buffer by 1, so that the report ID
864                    will remain in byte 0. */
865                 buf++;
866                 count--;
867                 skipped_report_id = 1;
868         }
869         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
870                 HID_REQ_GET_REPORT,
871                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
872                 ((report_type + 1) << 8) | report_number,
873                 interface->desc.bInterfaceNumber, buf, count,
874                 USB_CTRL_SET_TIMEOUT);
875
876         /* count also the report id */
877         if (ret > 0 && skipped_report_id)
878                 ret++;
879
880         return ret;
881 }
882
883 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
884                 unsigned char report_type)
885 {
886         struct usbhid_device *usbhid = hid->driver_data;
887         struct usb_device *dev = hid_to_usb_dev(hid);
888         struct usb_interface *intf = usbhid->intf;
889         struct usb_host_interface *interface = intf->cur_altsetting;
890         int ret;
891
892         if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
893                 int actual_length;
894                 int skipped_report_id = 0;
895
896                 if (buf[0] == 0x0) {
897                         /* Don't send the Report ID */
898                         buf++;
899                         count--;
900                         skipped_report_id = 1;
901                 }
902                 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
903                         buf, count, &actual_length,
904                         USB_CTRL_SET_TIMEOUT);
905                 /* return the number of bytes transferred */
906                 if (ret == 0) {
907                         ret = actual_length;
908                         /* count also the report id */
909                         if (skipped_report_id)
910                                 ret++;
911                 }
912         } else {
913                 int skipped_report_id = 0;
914                 int report_id = buf[0];
915                 if (buf[0] == 0x0) {
916                         /* Don't send the Report ID */
917                         buf++;
918                         count--;
919                         skipped_report_id = 1;
920                 }
921                 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
922                         HID_REQ_SET_REPORT,
923                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
924                         ((report_type + 1) << 8) | report_id,
925                         interface->desc.bInterfaceNumber, buf, count,
926                         USB_CTRL_SET_TIMEOUT);
927                 /* count also the report id, if this was a numbered report. */
928                 if (ret > 0 && skipped_report_id)
929                         ret++;
930         }
931
932         return ret;
933 }
934
935 static void usbhid_restart_queues(struct usbhid_device *usbhid)
936 {
937         if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
938                 usbhid_restart_out_queue(usbhid);
939         if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
940                 usbhid_restart_ctrl_queue(usbhid);
941 }
942
943 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
944 {
945         struct usbhid_device *usbhid = hid->driver_data;
946
947         usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
948         usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
949         kfree(usbhid->cr);
950         usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
951 }
952
953 static int usbhid_parse(struct hid_device *hid)
954 {
955         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
956         struct usb_host_interface *interface = intf->cur_altsetting;
957         struct usb_device *dev = interface_to_usbdev (intf);
958         struct hid_descriptor *hdesc;
959         u32 quirks = 0;
960         unsigned int rsize = 0;
961         char *rdesc;
962         int ret, n;
963
964         quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
965                         le16_to_cpu(dev->descriptor.idProduct));
966
967         if (quirks & HID_QUIRK_IGNORE)
968                 return -ENODEV;
969
970         /* Many keyboards and mice don't like to be polled for reports,
971          * so we will always set the HID_QUIRK_NOGET flag for them. */
972         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
973                 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
974                         interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
975                                 quirks |= HID_QUIRK_NOGET;
976         }
977
978         if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
979             (!interface->desc.bNumEndpoints ||
980              usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
981                 dbg_hid("class descriptor not present\n");
982                 return -ENODEV;
983         }
984
985         hid->version = le16_to_cpu(hdesc->bcdHID);
986         hid->country = hdesc->bCountryCode;
987
988         for (n = 0; n < hdesc->bNumDescriptors; n++)
989                 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
990                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
991
992         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
993                 dbg_hid("weird size of report descriptor (%u)\n", rsize);
994                 return -EINVAL;
995         }
996
997         if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
998                 dbg_hid("couldn't allocate rdesc memory\n");
999                 return -ENOMEM;
1000         }
1001
1002         hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1003
1004         ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1005                         HID_DT_REPORT, rdesc, rsize);
1006         if (ret < 0) {
1007                 dbg_hid("reading report descriptor failed\n");
1008                 kfree(rdesc);
1009                 goto err;
1010         }
1011
1012         ret = hid_parse_report(hid, rdesc, rsize);
1013         kfree(rdesc);
1014         if (ret) {
1015                 dbg_hid("parsing report descriptor failed\n");
1016                 goto err;
1017         }
1018
1019         hid->quirks |= quirks;
1020
1021         return 0;
1022 err:
1023         return ret;
1024 }
1025
1026 static int usbhid_start(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 usbhid_device *usbhid = hid->driver_data;
1032         unsigned int n, insize = 0;
1033         int ret;
1034
1035         clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1036
1037         usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1038         hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1039         hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1040         hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1041
1042         if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1043                 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
1044
1045         hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1046
1047         if (insize > HID_MAX_BUFFER_SIZE)
1048                 insize = HID_MAX_BUFFER_SIZE;
1049
1050         if (hid_alloc_buffers(dev, hid)) {
1051                 ret = -ENOMEM;
1052                 goto fail;
1053         }
1054
1055         for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1056                 struct usb_endpoint_descriptor *endpoint;
1057                 int pipe;
1058                 int interval;
1059
1060                 endpoint = &interface->endpoint[n].desc;
1061                 if (!usb_endpoint_xfer_int(endpoint))
1062                         continue;
1063
1064                 interval = endpoint->bInterval;
1065
1066                 /* Some vendors give fullspeed interval on highspeed devides */
1067                 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
1068                     dev->speed == USB_SPEED_HIGH) {
1069                         interval = fls(endpoint->bInterval*8);
1070                         printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1071                                hid->name, endpoint->bInterval, interval);
1072                 }
1073
1074                 /* Change the polling interval of mice. */
1075                 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1076                         interval = hid_mousepoll_interval;
1077
1078                 ret = -ENOMEM;
1079                 if (usb_endpoint_dir_in(endpoint)) {
1080                         if (usbhid->urbin)
1081                                 continue;
1082                         if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1083                                 goto fail;
1084                         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1085                         usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1086                                          hid_irq_in, hid, interval);
1087                         usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1088                         usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1089                 } else {
1090                         if (usbhid->urbout)
1091                                 continue;
1092                         if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1093                                 goto fail;
1094                         pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1095                         usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1096                                          hid_irq_out, hid, interval);
1097                         usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1098                         usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1099                 }
1100         }
1101
1102         usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1103         if (!usbhid->urbctrl) {
1104                 ret = -ENOMEM;
1105                 goto fail;
1106         }
1107
1108         usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1109                              usbhid->ctrlbuf, 1, hid_ctrl, hid);
1110         usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1111         usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1112
1113         if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1114                 usbhid_init_reports(hid);
1115
1116         set_bit(HID_STARTED, &usbhid->iofl);
1117
1118         /* Some keyboards don't work until their LEDs have been set.
1119          * Since BIOSes do set the LEDs, it must be safe for any device
1120          * that supports the keyboard boot protocol.
1121          * In addition, enable remote wakeup by default for all keyboard
1122          * devices supporting the boot protocol.
1123          */
1124         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1125                         interface->desc.bInterfaceProtocol ==
1126                                 USB_INTERFACE_PROTOCOL_KEYBOARD) {
1127                 usbhid_set_leds(hid);
1128                 device_set_wakeup_enable(&dev->dev, 1);
1129         }
1130         return 0;
1131
1132 fail:
1133         usb_free_urb(usbhid->urbin);
1134         usb_free_urb(usbhid->urbout);
1135         usb_free_urb(usbhid->urbctrl);
1136         usbhid->urbin = NULL;
1137         usbhid->urbout = NULL;
1138         usbhid->urbctrl = NULL;
1139         hid_free_buffers(dev, hid);
1140         return ret;
1141 }
1142
1143 static void usbhid_stop(struct hid_device *hid)
1144 {
1145         struct usbhid_device *usbhid = hid->driver_data;
1146
1147         if (WARN_ON(!usbhid))
1148                 return;
1149
1150         clear_bit(HID_STARTED, &usbhid->iofl);
1151         spin_lock_irq(&usbhid->lock);   /* Sync with error and led handlers */
1152         set_bit(HID_DISCONNECTED, &usbhid->iofl);
1153         spin_unlock_irq(&usbhid->lock);
1154         usb_kill_urb(usbhid->urbin);
1155         usb_kill_urb(usbhid->urbout);
1156         usb_kill_urb(usbhid->urbctrl);
1157
1158         hid_cancel_delayed_stuff(usbhid);
1159
1160         hid->claimed = 0;
1161
1162         usb_free_urb(usbhid->urbin);
1163         usb_free_urb(usbhid->urbctrl);
1164         usb_free_urb(usbhid->urbout);
1165         usbhid->urbin = NULL; /* don't mess up next start */
1166         usbhid->urbctrl = NULL;
1167         usbhid->urbout = NULL;
1168
1169         hid_free_buffers(hid_to_usb_dev(hid), hid);
1170 }
1171
1172 static int usbhid_power(struct hid_device *hid, int lvl)
1173 {
1174         int r = 0;
1175
1176         switch (lvl) {
1177         case PM_HINT_FULLON:
1178                 r = usbhid_get_power(hid);
1179                 break;
1180         case PM_HINT_NORMAL:
1181                 usbhid_put_power(hid);
1182                 break;
1183         }
1184         return r;
1185 }
1186
1187 static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1188 {
1189         switch (reqtype) {
1190         case HID_REQ_GET_REPORT:
1191                 usbhid_submit_report(hid, rep, USB_DIR_IN);
1192                 break;
1193         case HID_REQ_SET_REPORT:
1194                 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1195                 break;
1196         }
1197 }
1198
1199 static int usbhid_idle(struct hid_device *hid, int report, int idle,
1200                 int reqtype)
1201 {
1202         struct usb_device *dev = hid_to_usb_dev(hid);
1203         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1204         struct usb_host_interface *interface = intf->cur_altsetting;
1205         int ifnum = interface->desc.bInterfaceNumber;
1206
1207         if (reqtype != HID_REQ_SET_IDLE)
1208                 return -EINVAL;
1209
1210         return hid_set_idle(dev, ifnum, report, idle);
1211 }
1212
1213 static struct hid_ll_driver usb_hid_driver = {
1214         .parse = usbhid_parse,
1215         .start = usbhid_start,
1216         .stop = usbhid_stop,
1217         .open = usbhid_open,
1218         .close = usbhid_close,
1219         .power = usbhid_power,
1220         .request = usbhid_request,
1221         .wait = usbhid_wait_io,
1222         .idle = usbhid_idle,
1223 };
1224
1225 static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1226 {
1227         struct usb_host_interface *interface = intf->cur_altsetting;
1228         struct usb_device *dev = interface_to_usbdev(intf);
1229         struct usbhid_device *usbhid;
1230         struct hid_device *hid;
1231         unsigned int n, has_in = 0;
1232         size_t len;
1233         int ret;
1234
1235         dbg_hid("HID probe called for ifnum %d\n",
1236                         intf->altsetting->desc.bInterfaceNumber);
1237
1238         for (n = 0; n < interface->desc.bNumEndpoints; n++)
1239                 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1240                         has_in++;
1241         if (!has_in) {
1242                 hid_err(intf, "couldn't find an input interrupt endpoint\n");
1243                 return -ENODEV;
1244         }
1245
1246         hid = hid_allocate_device();
1247         if (IS_ERR(hid))
1248                 return PTR_ERR(hid);
1249
1250         usb_set_intfdata(intf, hid);
1251         hid->ll_driver = &usb_hid_driver;
1252         hid->hid_get_raw_report = usbhid_get_raw_report;
1253         hid->hid_output_raw_report = usbhid_output_raw_report;
1254         hid->ff_init = hid_pidff_init;
1255 #ifdef CONFIG_USB_HIDDEV
1256         hid->hiddev_connect = hiddev_connect;
1257         hid->hiddev_disconnect = hiddev_disconnect;
1258         hid->hiddev_hid_event = hiddev_hid_event;
1259         hid->hiddev_report_event = hiddev_report_event;
1260 #endif
1261         hid->dev.parent = &intf->dev;
1262         hid->bus = BUS_USB;
1263         hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1264         hid->product = le16_to_cpu(dev->descriptor.idProduct);
1265         hid->name[0] = 0;
1266         hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
1267         if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1268                         USB_INTERFACE_PROTOCOL_MOUSE)
1269                 hid->type = HID_TYPE_USBMOUSE;
1270         else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1271                 hid->type = HID_TYPE_USBNONE;
1272
1273         if (dev->manufacturer)
1274                 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1275
1276         if (dev->product) {
1277                 if (dev->manufacturer)
1278                         strlcat(hid->name, " ", sizeof(hid->name));
1279                 strlcat(hid->name, dev->product, sizeof(hid->name));
1280         }
1281
1282         if (!strlen(hid->name))
1283                 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1284                          le16_to_cpu(dev->descriptor.idVendor),
1285                          le16_to_cpu(dev->descriptor.idProduct));
1286
1287         usb_make_path(dev, hid->phys, sizeof(hid->phys));
1288         strlcat(hid->phys, "/input", sizeof(hid->phys));
1289         len = strlen(hid->phys);
1290         if (len < sizeof(hid->phys) - 1)
1291                 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1292                          "%d", intf->altsetting[0].desc.bInterfaceNumber);
1293
1294         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1295                 hid->uniq[0] = 0;
1296
1297         usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1298         if (usbhid == NULL) {
1299                 ret = -ENOMEM;
1300                 goto err;
1301         }
1302
1303         hid->driver_data = usbhid;
1304         usbhid->hid = hid;
1305         usbhid->intf = intf;
1306         usbhid->ifnum = interface->desc.bInterfaceNumber;
1307
1308         init_waitqueue_head(&usbhid->wait);
1309         INIT_WORK(&usbhid->reset_work, hid_reset);
1310         setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1311         spin_lock_init(&usbhid->lock);
1312
1313         ret = hid_add_device(hid);
1314         if (ret) {
1315                 if (ret != -ENODEV)
1316                         hid_err(intf, "can't add hid device: %d\n", ret);
1317                 goto err_free;
1318         }
1319
1320         return 0;
1321 err_free:
1322         kfree(usbhid);
1323 err:
1324         hid_destroy_device(hid);
1325         return ret;
1326 }
1327
1328 static void usbhid_disconnect(struct usb_interface *intf)
1329 {
1330         struct hid_device *hid = usb_get_intfdata(intf);
1331         struct usbhid_device *usbhid;
1332
1333         if (WARN_ON(!hid))
1334                 return;
1335
1336         usbhid = hid->driver_data;
1337         hid_destroy_device(hid);
1338         kfree(usbhid);
1339 }
1340
1341 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1342 {
1343         del_timer_sync(&usbhid->io_retry);
1344         cancel_work_sync(&usbhid->reset_work);
1345 }
1346
1347 static void hid_cease_io(struct usbhid_device *usbhid)
1348 {
1349         del_timer_sync(&usbhid->io_retry);
1350         usb_kill_urb(usbhid->urbin);
1351         usb_kill_urb(usbhid->urbctrl);
1352         usb_kill_urb(usbhid->urbout);
1353 }
1354
1355 /* Treat USB reset pretty much the same as suspend/resume */
1356 static int hid_pre_reset(struct usb_interface *intf)
1357 {
1358         struct hid_device *hid = usb_get_intfdata(intf);
1359         struct usbhid_device *usbhid = hid->driver_data;
1360
1361         spin_lock_irq(&usbhid->lock);
1362         set_bit(HID_RESET_PENDING, &usbhid->iofl);
1363         spin_unlock_irq(&usbhid->lock);
1364         hid_cease_io(usbhid);
1365
1366         return 0;
1367 }
1368
1369 /* Same routine used for post_reset and reset_resume */
1370 static int hid_post_reset(struct usb_interface *intf)
1371 {
1372         struct usb_device *dev = interface_to_usbdev (intf);
1373         struct hid_device *hid = usb_get_intfdata(intf);
1374         struct usbhid_device *usbhid = hid->driver_data;
1375         struct usb_host_interface *interface = intf->cur_altsetting;
1376         int status;
1377         char *rdesc;
1378
1379         /* Fetch and examine the HID report descriptor. If this
1380          * has changed, then rebind. Since usbcore's check of the
1381          * configuration descriptors passed, we already know that
1382          * the size of the HID report descriptor has not changed.
1383          */
1384         rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
1385         if (!rdesc) {
1386                 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1387                 return 1;
1388         }
1389         status = hid_get_class_descriptor(dev,
1390                                 interface->desc.bInterfaceNumber,
1391                                 HID_DT_REPORT, rdesc, hid->dev_rsize);
1392         if (status < 0) {
1393                 dbg_hid("reading report descriptor failed (post_reset)\n");
1394                 kfree(rdesc);
1395                 return 1;
1396         }
1397         status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
1398         kfree(rdesc);
1399         if (status != 0) {
1400                 dbg_hid("report descriptor changed\n");
1401                 return 1;
1402         }
1403
1404         spin_lock_irq(&usbhid->lock);
1405         clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1406         spin_unlock_irq(&usbhid->lock);
1407         hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1408         status = hid_start_in(hid);
1409         if (status < 0)
1410                 hid_io_error(hid);
1411         usbhid_restart_queues(usbhid);
1412
1413         return 0;
1414 }
1415
1416 int usbhid_get_power(struct hid_device *hid)
1417 {
1418         struct usbhid_device *usbhid = hid->driver_data;
1419
1420         return usb_autopm_get_interface(usbhid->intf);
1421 }
1422
1423 void usbhid_put_power(struct hid_device *hid)
1424 {
1425         struct usbhid_device *usbhid = hid->driver_data;
1426
1427         usb_autopm_put_interface(usbhid->intf);
1428 }
1429
1430
1431 #ifdef CONFIG_PM
1432 static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1433 {
1434         struct usbhid_device *usbhid = hid->driver_data;
1435         int status;
1436
1437         spin_lock_irq(&usbhid->lock);
1438         clear_bit(HID_SUSPENDED, &usbhid->iofl);
1439         usbhid_mark_busy(usbhid);
1440
1441         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1442                         test_bit(HID_RESET_PENDING, &usbhid->iofl))
1443                 schedule_work(&usbhid->reset_work);
1444         usbhid->retry_delay = 0;
1445
1446         usbhid_restart_queues(usbhid);
1447         spin_unlock_irq(&usbhid->lock);
1448
1449         status = hid_start_in(hid);
1450         if (status < 0)
1451                 hid_io_error(hid);
1452
1453         if (driver_suspended && hid->driver && hid->driver->resume)
1454                 status = hid->driver->resume(hid);
1455         return status;
1456 }
1457
1458 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1459 {
1460         struct hid_device *hid = usb_get_intfdata(intf);
1461         struct usbhid_device *usbhid = hid->driver_data;
1462         int status = 0;
1463         bool driver_suspended = false;
1464         unsigned int ledcount;
1465
1466         if (PMSG_IS_AUTO(message)) {
1467                 ledcount = hidinput_count_leds(hid);
1468                 spin_lock_irq(&usbhid->lock);   /* Sync with error handler */
1469                 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1470                     && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1471                     && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1472                     && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1473                     && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1474                     && (!ledcount || ignoreled))
1475                 {
1476                         set_bit(HID_SUSPENDED, &usbhid->iofl);
1477                         spin_unlock_irq(&usbhid->lock);
1478                         if (hid->driver && hid->driver->suspend) {
1479                                 status = hid->driver->suspend(hid, message);
1480                                 if (status < 0)
1481                                         goto failed;
1482                         }
1483                         driver_suspended = true;
1484                 } else {
1485                         usbhid_mark_busy(usbhid);
1486                         spin_unlock_irq(&usbhid->lock);
1487                         return -EBUSY;
1488                 }
1489
1490         } else {
1491                 /* TODO: resume() might need to handle suspend failure */
1492                 if (hid->driver && hid->driver->suspend)
1493                         status = hid->driver->suspend(hid, message);
1494                 driver_suspended = true;
1495                 spin_lock_irq(&usbhid->lock);
1496                 set_bit(HID_SUSPENDED, &usbhid->iofl);
1497                 spin_unlock_irq(&usbhid->lock);
1498                 if (usbhid_wait_io(hid) < 0)
1499                         status = -EIO;
1500         }
1501
1502         hid_cancel_delayed_stuff(usbhid);
1503         hid_cease_io(usbhid);
1504
1505         if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1506                 /* lost race against keypresses */
1507                 status = -EBUSY;
1508                 goto failed;
1509         }
1510         dev_dbg(&intf->dev, "suspend\n");
1511         return status;
1512
1513  failed:
1514         hid_resume_common(hid, driver_suspended);
1515         return status;
1516 }
1517
1518 static int hid_resume(struct usb_interface *intf)
1519 {
1520         struct hid_device *hid = usb_get_intfdata (intf);
1521         struct usbhid_device *usbhid = hid->driver_data;
1522         int status;
1523
1524         if (!test_bit(HID_STARTED, &usbhid->iofl))
1525                 return 0;
1526
1527         status = hid_resume_common(hid, true);
1528         dev_dbg(&intf->dev, "resume status %d\n", status);
1529         return 0;
1530 }
1531
1532 static int hid_reset_resume(struct usb_interface *intf)
1533 {
1534         struct hid_device *hid = usb_get_intfdata(intf);
1535         struct usbhid_device *usbhid = hid->driver_data;
1536         int status;
1537
1538         clear_bit(HID_SUSPENDED, &usbhid->iofl);
1539         status = hid_post_reset(intf);
1540         if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1541                 int ret = hid->driver->reset_resume(hid);
1542                 if (ret < 0)
1543                         status = ret;
1544         }
1545         return status;
1546 }
1547
1548 #endif /* CONFIG_PM */
1549
1550 static const struct usb_device_id hid_usb_ids[] = {
1551         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1552                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1553         { }                                             /* Terminating entry */
1554 };
1555
1556 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1557
1558 static struct usb_driver hid_driver = {
1559         .name =         "usbhid",
1560         .probe =        usbhid_probe,
1561         .disconnect =   usbhid_disconnect,
1562 #ifdef CONFIG_PM
1563         .suspend =      hid_suspend,
1564         .resume =       hid_resume,
1565         .reset_resume = hid_reset_resume,
1566 #endif
1567         .pre_reset =    hid_pre_reset,
1568         .post_reset =   hid_post_reset,
1569         .id_table =     hid_usb_ids,
1570         .supports_autosuspend = 1,
1571 };
1572
1573 struct usb_interface *usbhid_find_interface(int minor)
1574 {
1575         return usb_find_interface(&hid_driver, minor);
1576 }
1577
1578 static int __init hid_init(void)
1579 {
1580         int retval = -ENOMEM;
1581
1582         retval = usbhid_quirks_init(quirks_param);
1583         if (retval)
1584                 goto usbhid_quirks_init_fail;
1585         retval = usb_register(&hid_driver);
1586         if (retval)
1587                 goto usb_register_fail;
1588         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1589
1590         return 0;
1591 usb_register_fail:
1592         usbhid_quirks_exit();
1593 usbhid_quirks_init_fail:
1594         return retval;
1595 }
1596
1597 static void __exit hid_exit(void)
1598 {
1599         usb_deregister(&hid_driver);
1600         usbhid_quirks_exit();
1601 }
1602
1603 module_init(hid_init);
1604 module_exit(hid_exit);
1605
1606 MODULE_AUTHOR("Andreas Gal");
1607 MODULE_AUTHOR("Vojtech Pavlik");
1608 MODULE_AUTHOR("Jiri Kosina");
1609 MODULE_DESCRIPTION(DRIVER_DESC);
1610 MODULE_LICENSE(DRIVER_LICENSE);