]> Pileus Git - ~andy/linux/blob - drivers/hid/usbhid/hid-core.c
HID: usbhid: defer LED setting to a workqueue
[~andy/linux] / drivers / hid / usbhid / hid-core.c
1 /*
2  *  USB HID support for Linux
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7  *  Copyright (c) 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         int r;
201
202         if (!hid)
203                 return 0;
204
205         if ((kicked = (usbhid->outhead != usbhid->outtail))) {
206                 dbg("Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
207
208                 r = usb_autopm_get_interface_async(usbhid->intf);
209                 if (r < 0)
210                         return r;
211                 /* Asynchronously flush queue. */
212                 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
213                 if (hid_submit_out(hid)) {
214                         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
215                         usb_autopm_put_interface_async(usbhid->intf);
216                 }
217                 wake_up(&usbhid->wait);
218         }
219         return kicked;
220 }
221
222 static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
223 {
224         struct hid_device *hid = usb_get_intfdata(usbhid->intf);
225         int kicked;
226         int r;
227
228         WARN_ON(hid == NULL);
229         if (!hid)
230                 return 0;
231
232         if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
233                 dbg("Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
234
235                 r = usb_autopm_get_interface_async(usbhid->intf);
236                 if (r < 0)
237                         return r;
238                 /* Asynchronously flush queue. */
239                 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
240                 if (hid_submit_ctrl(hid)) {
241                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
242                         usb_autopm_put_interface_async(usbhid->intf);
243                 }
244                 wake_up(&usbhid->wait);
245         }
246         return kicked;
247 }
248
249 /*
250  * Input interrupt completion handler.
251  */
252
253 static void hid_irq_in(struct urb *urb)
254 {
255         struct hid_device       *hid = urb->context;
256         struct usbhid_device    *usbhid = hid->driver_data;
257         int                     status;
258
259         switch (urb->status) {
260         case 0:                 /* success */
261                 usbhid_mark_busy(usbhid);
262                 usbhid->retry_delay = 0;
263                 hid_input_report(urb->context, HID_INPUT_REPORT,
264                                  urb->transfer_buffer,
265                                  urb->actual_length, 1);
266                 /*
267                  * autosuspend refused while keys are pressed
268                  * because most keyboards don't wake up when
269                  * a key is released
270                  */
271                 if (hid_check_keys_pressed(hid))
272                         set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
273                 else
274                         clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
275                 break;
276         case -EPIPE:            /* stall */
277                 usbhid_mark_busy(usbhid);
278                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
279                 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
280                 schedule_work(&usbhid->reset_work);
281                 return;
282         case -ECONNRESET:       /* unlink */
283         case -ENOENT:
284         case -ESHUTDOWN:        /* unplug */
285                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
286                 return;
287         case -EILSEQ:           /* protocol error or unplug */
288         case -EPROTO:           /* protocol error or unplug */
289         case -ETIME:            /* protocol error or unplug */
290         case -ETIMEDOUT:        /* Should never happen, but... */
291                 usbhid_mark_busy(usbhid);
292                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
293                 hid_io_error(hid);
294                 return;
295         default:                /* error */
296                 hid_warn(urb->dev, "input irq status %d received\n",
297                          urb->status);
298         }
299
300         status = usb_submit_urb(urb, GFP_ATOMIC);
301         if (status) {
302                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
303                 if (status != -EPERM) {
304                         hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
305                                 hid_to_usb_dev(hid)->bus->bus_name,
306                                 hid_to_usb_dev(hid)->devpath,
307                                 usbhid->ifnum, status);
308                         hid_io_error(hid);
309                 }
310         }
311 }
312
313 static int hid_submit_out(struct hid_device *hid)
314 {
315         struct hid_report *report;
316         char *raw_report;
317         struct usbhid_device *usbhid = hid->driver_data;
318         int r;
319
320         report = usbhid->out[usbhid->outtail].report;
321         raw_report = usbhid->out[usbhid->outtail].raw_report;
322
323         usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
324                                                  1 + (report->id > 0);
325         usbhid->urbout->dev = hid_to_usb_dev(hid);
326         memcpy(usbhid->outbuf, raw_report,
327                usbhid->urbout->transfer_buffer_length);
328         kfree(raw_report);
329
330         dbg_hid("submitting out urb\n");
331
332         r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
333         if (r < 0) {
334                 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
335                 return r;
336         }
337         usbhid->last_out = jiffies;
338         return 0;
339 }
340
341 static int hid_submit_ctrl(struct hid_device *hid)
342 {
343         struct hid_report *report;
344         unsigned char dir;
345         char *raw_report;
346         int len, r;
347         struct usbhid_device *usbhid = hid->driver_data;
348
349         report = usbhid->ctrl[usbhid->ctrltail].report;
350         raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
351         dir = usbhid->ctrl[usbhid->ctrltail].dir;
352
353         len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
354         if (dir == USB_DIR_OUT) {
355                 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
356                 usbhid->urbctrl->transfer_buffer_length = len;
357                 memcpy(usbhid->ctrlbuf, raw_report, len);
358                 kfree(raw_report);
359         } else {
360                 int maxpacket, padlen;
361
362                 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
363                 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
364                                           usbhid->urbctrl->pipe, 0);
365                 if (maxpacket > 0) {
366                         padlen = DIV_ROUND_UP(len, maxpacket);
367                         padlen *= maxpacket;
368                         if (padlen > usbhid->bufsize)
369                                 padlen = usbhid->bufsize;
370                 } else
371                         padlen = 0;
372                 usbhid->urbctrl->transfer_buffer_length = padlen;
373         }
374         usbhid->urbctrl->dev = hid_to_usb_dev(hid);
375
376         usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
377         usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
378                                                       HID_REQ_GET_REPORT;
379         usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
380                                          report->id);
381         usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
382         usbhid->cr->wLength = cpu_to_le16(len);
383
384         dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
385                 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
386                                                              "Get_Report",
387                 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
388
389         r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
390         if (r < 0) {
391                 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
392                 return r;
393         }
394         usbhid->last_ctrl = jiffies;
395         return 0;
396 }
397
398 /*
399  * Output interrupt completion handler.
400  */
401
402 static void hid_irq_out(struct urb *urb)
403 {
404         struct hid_device *hid = urb->context;
405         struct usbhid_device *usbhid = hid->driver_data;
406         unsigned long flags;
407         int unplug = 0;
408
409         switch (urb->status) {
410         case 0:                 /* success */
411                 break;
412         case -ESHUTDOWN:        /* unplug */
413                 unplug = 1;
414         case -EILSEQ:           /* protocol error or unplug */
415         case -EPROTO:           /* protocol error or unplug */
416         case -ECONNRESET:       /* unlink */
417         case -ENOENT:
418                 break;
419         default:                /* error */
420                 hid_warn(urb->dev, "output irq status %d received\n",
421                          urb->status);
422         }
423
424         spin_lock_irqsave(&usbhid->lock, flags);
425
426         if (unplug)
427                 usbhid->outtail = usbhid->outhead;
428         else
429                 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
430
431         if (usbhid->outhead != usbhid->outtail && !hid_submit_out(hid)) {
432                 /* Successfully submitted next urb in queue */
433                 spin_unlock_irqrestore(&usbhid->lock, flags);
434                 return;
435         }
436
437         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
438         spin_unlock_irqrestore(&usbhid->lock, flags);
439         usb_autopm_put_interface_async(usbhid->intf);
440         wake_up(&usbhid->wait);
441 }
442
443 /*
444  * Control pipe completion handler.
445  */
446
447 static void hid_ctrl(struct urb *urb)
448 {
449         struct hid_device *hid = urb->context;
450         struct usbhid_device *usbhid = hid->driver_data;
451         int unplug = 0, status = urb->status;
452
453         spin_lock(&usbhid->lock);
454
455         switch (status) {
456         case 0:                 /* success */
457                 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
458                         hid_input_report(urb->context,
459                                 usbhid->ctrl[usbhid->ctrltail].report->type,
460                                 urb->transfer_buffer, urb->actual_length, 0);
461                 break;
462         case -ESHUTDOWN:        /* unplug */
463                 unplug = 1;
464         case -EILSEQ:           /* protocol error or unplug */
465         case -EPROTO:           /* protocol error or unplug */
466         case -ECONNRESET:       /* unlink */
467         case -ENOENT:
468         case -EPIPE:            /* report not available */
469                 break;
470         default:                /* error */
471                 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
472         }
473
474         if (unplug)
475                 usbhid->ctrltail = usbhid->ctrlhead;
476         else
477                 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
478
479         if (usbhid->ctrlhead != usbhid->ctrltail && !hid_submit_ctrl(hid)) {
480                 /* Successfully submitted next urb in queue */
481                 spin_unlock(&usbhid->lock);
482                 return;
483         }
484
485         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
486         spin_unlock(&usbhid->lock);
487         usb_autopm_put_interface_async(usbhid->intf);
488         wake_up(&usbhid->wait);
489 }
490
491 static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
492                                    unsigned char dir)
493 {
494         int head;
495         struct usbhid_device *usbhid = hid->driver_data;
496         int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
497
498         if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
499                 return;
500
501         if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
502                 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
503                         hid_warn(hid, "output queue full\n");
504                         return;
505                 }
506
507                 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
508                 if (!usbhid->out[usbhid->outhead].raw_report) {
509                         hid_warn(hid, "output queueing failed\n");
510                         return;
511                 }
512                 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
513                 usbhid->out[usbhid->outhead].report = report;
514                 usbhid->outhead = head;
515
516                 /* Try to awake from autosuspend... */
517                 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
518                         return;
519
520                 /*
521                  * But if still suspended, leave urb enqueued, don't submit.
522                  * Submission will occur if/when resume() drains the queue.
523                  */
524                 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
525                         return;
526
527                 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
528                         if (hid_submit_out(hid)) {
529                                 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
530                                 usb_autopm_put_interface_async(usbhid->intf);
531                         }
532                         wake_up(&usbhid->wait);
533                 } else {
534                         /*
535                          * the queue is known to run
536                          * but an earlier request may be stuck
537                          * we may need to time out
538                          * no race because this is called under
539                          * spinlock
540                          */
541                         if (time_after(jiffies, usbhid->last_out + HZ * 5))
542                                 usb_unlink_urb(usbhid->urbout);
543                 }
544                 return;
545         }
546
547         if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
548                 hid_warn(hid, "control queue full\n");
549                 return;
550         }
551
552         if (dir == USB_DIR_OUT) {
553                 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
554                 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
555                         hid_warn(hid, "control queueing failed\n");
556                         return;
557                 }
558                 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
559         }
560         usbhid->ctrl[usbhid->ctrlhead].report = report;
561         usbhid->ctrl[usbhid->ctrlhead].dir = dir;
562         usbhid->ctrlhead = head;
563
564         /* Try to awake from autosuspend... */
565         if (usb_autopm_get_interface_async(usbhid->intf) < 0)
566                 return;
567
568         /*
569          * If already suspended, leave urb enqueued, but don't submit.
570          * Submission will occur if/when resume() drains the queue.
571          */
572         if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
573                 return;
574
575         if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
576                 if (hid_submit_ctrl(hid)) {
577                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
578                         usb_autopm_put_interface_async(usbhid->intf);
579                 }
580                 wake_up(&usbhid->wait);
581         } else {
582                 /*
583                  * the queue is known to run
584                  * but an earlier request may be stuck
585                  * we may need to time out
586                  * no race because this is called under
587                  * spinlock
588                  */
589                 if (time_after(jiffies, usbhid->last_ctrl + HZ * 5))
590                         usb_unlink_urb(usbhid->urbctrl);
591         }
592 }
593
594 void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
595 {
596         struct usbhid_device *usbhid = hid->driver_data;
597         unsigned long flags;
598
599         spin_lock_irqsave(&usbhid->lock, flags);
600         __usbhid_submit_report(hid, report, dir);
601         spin_unlock_irqrestore(&usbhid->lock, flags);
602 }
603 EXPORT_SYMBOL_GPL(usbhid_submit_report);
604
605 /* Workqueue routine to send requests to change LEDs */
606 static void hid_led(struct work_struct *work)
607 {
608         struct usbhid_device *usbhid =
609                 container_of(work, struct usbhid_device, led_work);
610         struct hid_device *hid = usbhid->hid;
611         struct hid_field *field;
612         unsigned long flags;
613
614         field = hidinput_get_led_field(hid);
615         if (!field) {
616                 hid_warn(hid, "LED event field not found\n");
617                 return;
618         }
619
620         spin_lock_irqsave(&usbhid->lock, flags);
621         if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
622                 usbhid->ledcount = hidinput_count_leds(hid);
623                 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
624                 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
625         }
626         spin_unlock_irqrestore(&usbhid->lock, flags);
627 }
628
629 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
630 {
631         struct hid_device *hid = input_get_drvdata(dev);
632         struct usbhid_device *usbhid = hid->driver_data;
633         struct hid_field *field;
634         unsigned long flags;
635         int offset;
636
637         if (type == EV_FF)
638                 return input_ff_event(dev, type, code, value);
639
640         if (type != EV_LED)
641                 return -1;
642
643         if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
644                 hid_warn(dev, "event field not found\n");
645                 return -1;
646         }
647
648         spin_lock_irqsave(&usbhid->lock, flags);
649         hid_set_field(field, offset, value);
650         spin_unlock_irqrestore(&usbhid->lock, flags);
651
652         /*
653          * Defer performing requested LED action.
654          * This is more likely gather all LED changes into a single URB.
655          */
656         schedule_work(&usbhid->led_work);
657
658         return 0;
659 }
660
661 int usbhid_wait_io(struct hid_device *hid)
662 {
663         struct usbhid_device *usbhid = hid->driver_data;
664
665         if (!wait_event_timeout(usbhid->wait,
666                                 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
667                                 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
668                                         10*HZ)) {
669                 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
670                 return -1;
671         }
672
673         return 0;
674 }
675 EXPORT_SYMBOL_GPL(usbhid_wait_io);
676
677 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
678 {
679         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
680                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
681                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
682 }
683
684 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
685                 unsigned char type, void *buf, int size)
686 {
687         int result, retries = 4;
688
689         memset(buf, 0, size);
690
691         do {
692                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
693                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
694                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
695                 retries--;
696         } while (result < size && retries);
697         return result;
698 }
699
700 int usbhid_open(struct hid_device *hid)
701 {
702         struct usbhid_device *usbhid = hid->driver_data;
703         int res;
704
705         mutex_lock(&hid_open_mut);
706         if (!hid->open++) {
707                 res = usb_autopm_get_interface(usbhid->intf);
708                 /* the device must be awake to reliably request remote wakeup */
709                 if (res < 0) {
710                         hid->open--;
711                         mutex_unlock(&hid_open_mut);
712                         return -EIO;
713                 }
714                 usbhid->intf->needs_remote_wakeup = 1;
715                 if (hid_start_in(hid))
716                         hid_io_error(hid);
717  
718                 usb_autopm_put_interface(usbhid->intf);
719         }
720         mutex_unlock(&hid_open_mut);
721         return 0;
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 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 EXPORT_SYMBOL_GPL(usbhid_set_leds);
815
816 /*
817  * Traverse the supplied list of reports and find the longest
818  */
819 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
820                 unsigned int *max)
821 {
822         struct hid_report *report;
823         unsigned int size;
824
825         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
826                 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
827                 if (*max < size)
828                         *max = size;
829         }
830 }
831
832 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
833 {
834         struct usbhid_device *usbhid = hid->driver_data;
835
836         usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
837                         &usbhid->inbuf_dma);
838         usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
839                         &usbhid->outbuf_dma);
840         usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
841         usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
842                         &usbhid->ctrlbuf_dma);
843         if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
844                         !usbhid->ctrlbuf)
845                 return -1;
846
847         return 0;
848 }
849
850 static int usbhid_get_raw_report(struct hid_device *hid,
851                 unsigned char report_number, __u8 *buf, size_t count,
852                 unsigned char report_type)
853 {
854         struct usbhid_device *usbhid = hid->driver_data;
855         struct usb_device *dev = hid_to_usb_dev(hid);
856         struct usb_interface *intf = usbhid->intf;
857         struct usb_host_interface *interface = intf->cur_altsetting;
858         int skipped_report_id = 0;
859         int ret;
860
861         /* Byte 0 is the report number. Report data starts at byte 1.*/
862         buf[0] = report_number;
863         if (report_number == 0x0) {
864                 /* Offset the return buffer by 1, so that the report ID
865                    will remain in byte 0. */
866                 buf++;
867                 count--;
868                 skipped_report_id = 1;
869         }
870         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
871                 HID_REQ_GET_REPORT,
872                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
873                 ((report_type + 1) << 8) | report_number,
874                 interface->desc.bInterfaceNumber, buf, count,
875                 USB_CTRL_SET_TIMEOUT);
876
877         /* count also the report id */
878         if (ret > 0 && skipped_report_id)
879                 ret++;
880
881         return ret;
882 }
883
884 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
885                 unsigned char report_type)
886 {
887         struct usbhid_device *usbhid = hid->driver_data;
888         struct usb_device *dev = hid_to_usb_dev(hid);
889         struct usb_interface *intf = usbhid->intf;
890         struct usb_host_interface *interface = intf->cur_altsetting;
891         int ret;
892
893         if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
894                 int actual_length;
895                 int skipped_report_id = 0;
896
897                 if (buf[0] == 0x0) {
898                         /* Don't send the Report ID */
899                         buf++;
900                         count--;
901                         skipped_report_id = 1;
902                 }
903                 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
904                         buf, count, &actual_length,
905                         USB_CTRL_SET_TIMEOUT);
906                 /* return the number of bytes transferred */
907                 if (ret == 0) {
908                         ret = actual_length;
909                         /* count also the report id */
910                         if (skipped_report_id)
911                                 ret++;
912                 }
913         } else {
914                 int skipped_report_id = 0;
915                 int report_id = buf[0];
916                 if (buf[0] == 0x0) {
917                         /* Don't send the Report ID */
918                         buf++;
919                         count--;
920                         skipped_report_id = 1;
921                 }
922                 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
923                         HID_REQ_SET_REPORT,
924                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
925                         ((report_type + 1) << 8) | report_id,
926                         interface->desc.bInterfaceNumber, buf, count,
927                         USB_CTRL_SET_TIMEOUT);
928                 /* count also the report id, if this was a numbered report. */
929                 if (ret > 0 && skipped_report_id)
930                         ret++;
931         }
932
933         return ret;
934 }
935
936 static void usbhid_restart_queues(struct usbhid_device *usbhid)
937 {
938         if (usbhid->urbout)
939                 usbhid_restart_out_queue(usbhid);
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 struct hid_ll_driver usb_hid_driver = {
1188         .parse = usbhid_parse,
1189         .start = usbhid_start,
1190         .stop = usbhid_stop,
1191         .open = usbhid_open,
1192         .close = usbhid_close,
1193         .power = usbhid_power,
1194         .hidinput_input_event = usb_hidinput_input_event,
1195 };
1196
1197 static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1198 {
1199         struct usb_host_interface *interface = intf->cur_altsetting;
1200         struct usb_device *dev = interface_to_usbdev(intf);
1201         struct usbhid_device *usbhid;
1202         struct hid_device *hid;
1203         unsigned int n, has_in = 0;
1204         size_t len;
1205         int ret;
1206
1207         dbg_hid("HID probe called for ifnum %d\n",
1208                         intf->altsetting->desc.bInterfaceNumber);
1209
1210         for (n = 0; n < interface->desc.bNumEndpoints; n++)
1211                 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1212                         has_in++;
1213         if (!has_in) {
1214                 hid_err(intf, "couldn't find an input interrupt endpoint\n");
1215                 return -ENODEV;
1216         }
1217
1218         hid = hid_allocate_device();
1219         if (IS_ERR(hid))
1220                 return PTR_ERR(hid);
1221
1222         usb_set_intfdata(intf, hid);
1223         hid->ll_driver = &usb_hid_driver;
1224         hid->hid_get_raw_report = usbhid_get_raw_report;
1225         hid->hid_output_raw_report = usbhid_output_raw_report;
1226         hid->ff_init = hid_pidff_init;
1227 #ifdef CONFIG_USB_HIDDEV
1228         hid->hiddev_connect = hiddev_connect;
1229         hid->hiddev_disconnect = hiddev_disconnect;
1230         hid->hiddev_hid_event = hiddev_hid_event;
1231         hid->hiddev_report_event = hiddev_report_event;
1232 #endif
1233         hid->dev.parent = &intf->dev;
1234         hid->bus = BUS_USB;
1235         hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1236         hid->product = le16_to_cpu(dev->descriptor.idProduct);
1237         hid->name[0] = 0;
1238         hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
1239         if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1240                         USB_INTERFACE_PROTOCOL_MOUSE)
1241                 hid->type = HID_TYPE_USBMOUSE;
1242         else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1243                 hid->type = HID_TYPE_USBNONE;
1244
1245         if (dev->manufacturer)
1246                 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1247
1248         if (dev->product) {
1249                 if (dev->manufacturer)
1250                         strlcat(hid->name, " ", sizeof(hid->name));
1251                 strlcat(hid->name, dev->product, sizeof(hid->name));
1252         }
1253
1254         if (!strlen(hid->name))
1255                 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1256                          le16_to_cpu(dev->descriptor.idVendor),
1257                          le16_to_cpu(dev->descriptor.idProduct));
1258
1259         usb_make_path(dev, hid->phys, sizeof(hid->phys));
1260         strlcat(hid->phys, "/input", sizeof(hid->phys));
1261         len = strlen(hid->phys);
1262         if (len < sizeof(hid->phys) - 1)
1263                 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1264                          "%d", intf->altsetting[0].desc.bInterfaceNumber);
1265
1266         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1267                 hid->uniq[0] = 0;
1268
1269         usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1270         if (usbhid == NULL) {
1271                 ret = -ENOMEM;
1272                 goto err;
1273         }
1274
1275         hid->driver_data = usbhid;
1276         usbhid->hid = hid;
1277         usbhid->intf = intf;
1278         usbhid->ifnum = interface->desc.bInterfaceNumber;
1279
1280         init_waitqueue_head(&usbhid->wait);
1281         INIT_WORK(&usbhid->reset_work, hid_reset);
1282         setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1283         spin_lock_init(&usbhid->lock);
1284
1285         INIT_WORK(&usbhid->led_work, hid_led);
1286
1287         ret = hid_add_device(hid);
1288         if (ret) {
1289                 if (ret != -ENODEV)
1290                         hid_err(intf, "can't add hid device: %d\n", ret);
1291                 goto err_free;
1292         }
1293
1294         return 0;
1295 err_free:
1296         kfree(usbhid);
1297 err:
1298         hid_destroy_device(hid);
1299         return ret;
1300 }
1301
1302 static void usbhid_disconnect(struct usb_interface *intf)
1303 {
1304         struct hid_device *hid = usb_get_intfdata(intf);
1305         struct usbhid_device *usbhid;
1306
1307         if (WARN_ON(!hid))
1308                 return;
1309
1310         usbhid = hid->driver_data;
1311         hid_destroy_device(hid);
1312         kfree(usbhid);
1313 }
1314
1315 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1316 {
1317         del_timer_sync(&usbhid->io_retry);
1318         cancel_work_sync(&usbhid->reset_work);
1319         cancel_work_sync(&usbhid->led_work);
1320 }
1321
1322 static void hid_cease_io(struct usbhid_device *usbhid)
1323 {
1324         del_timer_sync(&usbhid->io_retry);
1325         usb_kill_urb(usbhid->urbin);
1326         usb_kill_urb(usbhid->urbctrl);
1327         usb_kill_urb(usbhid->urbout);
1328 }
1329
1330 /* Treat USB reset pretty much the same as suspend/resume */
1331 static int hid_pre_reset(struct usb_interface *intf)
1332 {
1333         struct hid_device *hid = usb_get_intfdata(intf);
1334         struct usbhid_device *usbhid = hid->driver_data;
1335
1336         spin_lock_irq(&usbhid->lock);
1337         set_bit(HID_RESET_PENDING, &usbhid->iofl);
1338         spin_unlock_irq(&usbhid->lock);
1339         hid_cease_io(usbhid);
1340
1341         return 0;
1342 }
1343
1344 /* Same routine used for post_reset and reset_resume */
1345 static int hid_post_reset(struct usb_interface *intf)
1346 {
1347         struct usb_device *dev = interface_to_usbdev (intf);
1348         struct hid_device *hid = usb_get_intfdata(intf);
1349         struct usbhid_device *usbhid = hid->driver_data;
1350         int status;
1351
1352         spin_lock_irq(&usbhid->lock);
1353         clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1354         spin_unlock_irq(&usbhid->lock);
1355         hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1356         status = hid_start_in(hid);
1357         if (status < 0)
1358                 hid_io_error(hid);
1359         usbhid_restart_queues(usbhid);
1360
1361         return 0;
1362 }
1363
1364 int usbhid_get_power(struct hid_device *hid)
1365 {
1366         struct usbhid_device *usbhid = hid->driver_data;
1367
1368         return usb_autopm_get_interface(usbhid->intf);
1369 }
1370
1371 void usbhid_put_power(struct hid_device *hid)
1372 {
1373         struct usbhid_device *usbhid = hid->driver_data;
1374
1375         usb_autopm_put_interface(usbhid->intf);
1376 }
1377
1378
1379 #ifdef CONFIG_PM
1380 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1381 {
1382         struct hid_device *hid = usb_get_intfdata(intf);
1383         struct usbhid_device *usbhid = hid->driver_data;
1384         int status;
1385
1386         if (PMSG_IS_AUTO(message)) {
1387                 spin_lock_irq(&usbhid->lock);   /* Sync with error handler */
1388                 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1389                     && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1390                     && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1391                     && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1392                     && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1393                     && (!usbhid->ledcount || ignoreled))
1394                 {
1395                         set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1396                         spin_unlock_irq(&usbhid->lock);
1397                         if (hid->driver && hid->driver->suspend) {
1398                                 status = hid->driver->suspend(hid, message);
1399                                 if (status < 0)
1400                                         return status;
1401                         }
1402                 } else {
1403                         usbhid_mark_busy(usbhid);
1404                         spin_unlock_irq(&usbhid->lock);
1405                         return -EBUSY;
1406                 }
1407
1408         } else {
1409                 if (hid->driver && hid->driver->suspend) {
1410                         status = hid->driver->suspend(hid, message);
1411                         if (status < 0)
1412                                 return status;
1413                 }
1414                 spin_lock_irq(&usbhid->lock);
1415                 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1416                 spin_unlock_irq(&usbhid->lock);
1417                 if (usbhid_wait_io(hid) < 0)
1418                         return -EIO;
1419         }
1420
1421         hid_cancel_delayed_stuff(usbhid);
1422         hid_cease_io(usbhid);
1423
1424         if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1425                 /* lost race against keypresses */
1426                 status = hid_start_in(hid);
1427                 if (status < 0)
1428                         hid_io_error(hid);
1429                 usbhid_mark_busy(usbhid);
1430                 return -EBUSY;
1431         }
1432         dev_dbg(&intf->dev, "suspend\n");
1433         return 0;
1434 }
1435
1436 static int hid_resume(struct usb_interface *intf)
1437 {
1438         struct hid_device *hid = usb_get_intfdata (intf);
1439         struct usbhid_device *usbhid = hid->driver_data;
1440         int status;
1441
1442         if (!test_bit(HID_STARTED, &usbhid->iofl))
1443                 return 0;
1444
1445         clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1446         usbhid_mark_busy(usbhid);
1447
1448         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1449             test_bit(HID_RESET_PENDING, &usbhid->iofl))
1450                 schedule_work(&usbhid->reset_work);
1451         usbhid->retry_delay = 0;
1452         status = hid_start_in(hid);
1453         if (status < 0)
1454                 hid_io_error(hid);
1455         usbhid_restart_queues(usbhid);
1456
1457         if (status >= 0 && hid->driver && hid->driver->resume) {
1458                 int ret = hid->driver->resume(hid);
1459                 if (ret < 0)
1460                         status = ret;
1461         }
1462         dev_dbg(&intf->dev, "resume status %d\n", status);
1463         return 0;
1464 }
1465
1466 static int hid_reset_resume(struct usb_interface *intf)
1467 {
1468         struct hid_device *hid = usb_get_intfdata(intf);
1469         struct usbhid_device *usbhid = hid->driver_data;
1470         int status;
1471
1472         clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1473         status = hid_post_reset(intf);
1474         if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1475                 int ret = hid->driver->reset_resume(hid);
1476                 if (ret < 0)
1477                         status = ret;
1478         }
1479         return status;
1480 }
1481
1482 #endif /* CONFIG_PM */
1483
1484 static const struct usb_device_id hid_usb_ids[] = {
1485         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1486                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1487         { }                                             /* Terminating entry */
1488 };
1489
1490 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1491
1492 static struct usb_driver hid_driver = {
1493         .name =         "usbhid",
1494         .probe =        usbhid_probe,
1495         .disconnect =   usbhid_disconnect,
1496 #ifdef CONFIG_PM
1497         .suspend =      hid_suspend,
1498         .resume =       hid_resume,
1499         .reset_resume = hid_reset_resume,
1500 #endif
1501         .pre_reset =    hid_pre_reset,
1502         .post_reset =   hid_post_reset,
1503         .id_table =     hid_usb_ids,
1504         .supports_autosuspend = 1,
1505 };
1506
1507 static const struct hid_device_id hid_usb_table[] = {
1508         { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1509         { }
1510 };
1511
1512 struct usb_interface *usbhid_find_interface(int minor)
1513 {
1514         return usb_find_interface(&hid_driver, minor);
1515 }
1516
1517 static struct hid_driver hid_usb_driver = {
1518         .name = "generic-usb",
1519         .id_table = hid_usb_table,
1520 };
1521
1522 static int __init hid_init(void)
1523 {
1524         int retval = -ENOMEM;
1525
1526         retval = hid_register_driver(&hid_usb_driver);
1527         if (retval)
1528                 goto hid_register_fail;
1529         retval = usbhid_quirks_init(quirks_param);
1530         if (retval)
1531                 goto usbhid_quirks_init_fail;
1532         retval = usb_register(&hid_driver);
1533         if (retval)
1534                 goto usb_register_fail;
1535         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1536
1537         return 0;
1538 usb_register_fail:
1539         usbhid_quirks_exit();
1540 usbhid_quirks_init_fail:
1541         hid_unregister_driver(&hid_usb_driver);
1542 hid_register_fail:
1543         return retval;
1544 }
1545
1546 static void __exit hid_exit(void)
1547 {
1548         usb_deregister(&hid_driver);
1549         usbhid_quirks_exit();
1550         hid_unregister_driver(&hid_usb_driver);
1551 }
1552
1553 module_init(hid_init);
1554 module_exit(hid_exit);
1555
1556 MODULE_AUTHOR("Andreas Gal");
1557 MODULE_AUTHOR("Vojtech Pavlik");
1558 MODULE_AUTHOR("Jiri Kosina");
1559 MODULE_DESCRIPTION(DRIVER_DESC);
1560 MODULE_LICENSE(DRIVER_LICENSE);