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