]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/hid/usbhid/hid-core.c
USB HID: encapsulate quirk handling into hid-quirks.c
[linux-2.6-omap-h63xx.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) 2006-2007 Jiri Kosina
8  */
9
10 /*
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  */
16
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/mm.h>
23 #include <linux/smp_lock.h>
24 #include <linux/spinlock.h>
25 #include <asm/unaligned.h>
26 #include <asm/byteorder.h>
27 #include <linux/input.h>
28 #include <linux/wait.h>
29
30 #include <linux/usb.h>
31
32 #include <linux/hid.h>
33 #include <linux/hiddev.h>
34 #include <linux/hid-debug.h>
35 #include "usbhid.h"
36
37 /*
38  * Version Information
39  */
40
41 #define DRIVER_VERSION "v2.6"
42 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
43 #define DRIVER_DESC "USB HID core driver"
44 #define DRIVER_LICENSE "GPL"
45
46 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
47                                 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
48 /*
49  * Module parameters.
50  */
51
52 static unsigned int hid_mousepoll_interval;
53 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55
56 /*
57  * Input submission and I/O error handler.
58  */
59
60 static void hid_io_error(struct hid_device *hid);
61
62 /* Start up the input URB */
63 static int hid_start_in(struct hid_device *hid)
64 {
65         unsigned long flags;
66         int rc = 0;
67         struct usbhid_device *usbhid = hid->driver_data;
68
69         spin_lock_irqsave(&usbhid->inlock, flags);
70         if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
71                         !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
72                 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
73                 if (rc != 0)
74                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
75         }
76         spin_unlock_irqrestore(&usbhid->inlock, flags);
77         return rc;
78 }
79
80 /* I/O retry timer routine */
81 static void hid_retry_timeout(unsigned long _hid)
82 {
83         struct hid_device *hid = (struct hid_device *) _hid;
84         struct usbhid_device *usbhid = hid->driver_data;
85
86         dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
87         if (hid_start_in(hid))
88                 hid_io_error(hid);
89 }
90
91 /* Workqueue routine to reset the device or clear a halt */
92 static void hid_reset(struct work_struct *work)
93 {
94         struct usbhid_device *usbhid =
95                 container_of(work, struct usbhid_device, reset_work);
96         struct hid_device *hid = usbhid->hid;
97         int rc_lock, rc = 0;
98
99         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
100                 dev_dbg(&usbhid->intf->dev, "clear halt\n");
101                 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
102                 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
103                 hid_start_in(hid);
104         }
105
106         else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
107                 dev_dbg(&usbhid->intf->dev, "resetting device\n");
108                 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
109                 if (rc_lock >= 0) {
110                         rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
111                         if (rc_lock)
112                                 usb_unlock_device(hid_to_usb_dev(hid));
113                 }
114                 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
115         }
116
117         switch (rc) {
118         case 0:
119                 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
120                         hid_io_error(hid);
121                 break;
122         default:
123                 err("can't reset device, %s-%s/input%d, status %d",
124                                 hid_to_usb_dev(hid)->bus->bus_name,
125                                 hid_to_usb_dev(hid)->devpath,
126                                 usbhid->ifnum, rc);
127                 /* FALLTHROUGH */
128         case -EHOSTUNREACH:
129         case -ENODEV:
130         case -EINTR:
131                 break;
132         }
133 }
134
135 /* Main I/O error handler */
136 static void hid_io_error(struct hid_device *hid)
137 {
138         unsigned long flags;
139         struct usbhid_device *usbhid = hid->driver_data;
140
141         spin_lock_irqsave(&usbhid->inlock, flags);
142
143         /* Stop when disconnected */
144         if (usb_get_intfdata(usbhid->intf) == NULL)
145                 goto done;
146
147         /* If it has been a while since the last error, we'll assume
148          * this a brand new error and reset the retry timeout. */
149         if (time_after(jiffies, usbhid->stop_retry + HZ/2))
150                 usbhid->retry_delay = 0;
151
152         /* When an error occurs, retry at increasing intervals */
153         if (usbhid->retry_delay == 0) {
154                 usbhid->retry_delay = 13;       /* Then 26, 52, 104, 104, ... */
155                 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
156         } else if (usbhid->retry_delay < 100)
157                 usbhid->retry_delay *= 2;
158
159         if (time_after(jiffies, usbhid->stop_retry)) {
160
161                 /* Retries failed, so do a port reset */
162                 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
163                         schedule_work(&usbhid->reset_work);
164                         goto done;
165                 }
166         }
167
168         mod_timer(&usbhid->io_retry,
169                         jiffies + msecs_to_jiffies(usbhid->retry_delay));
170 done:
171         spin_unlock_irqrestore(&usbhid->inlock, flags);
172 }
173
174 /*
175  * Input interrupt completion handler.
176  */
177
178 static void hid_irq_in(struct urb *urb)
179 {
180         struct hid_device       *hid = urb->context;
181         struct usbhid_device    *usbhid = hid->driver_data;
182         int                     status;
183
184         switch (urb->status) {
185                 case 0:                 /* success */
186                         usbhid->retry_delay = 0;
187                         hid_input_report(urb->context, HID_INPUT_REPORT,
188                                          urb->transfer_buffer,
189                                          urb->actual_length, 1);
190                         break;
191                 case -EPIPE:            /* stall */
192                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
193                         set_bit(HID_CLEAR_HALT, &usbhid->iofl);
194                         schedule_work(&usbhid->reset_work);
195                         return;
196                 case -ECONNRESET:       /* unlink */
197                 case -ENOENT:
198                 case -ESHUTDOWN:        /* unplug */
199                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
200                         return;
201                 case -EILSEQ:           /* protocol error or unplug */
202                 case -EPROTO:           /* protocol error or unplug */
203                 case -ETIME:            /* protocol error or unplug */
204                 case -ETIMEDOUT:        /* Should never happen, but... */
205                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
206                         hid_io_error(hid);
207                         return;
208                 default:                /* error */
209                         warn("input irq status %d received", urb->status);
210         }
211
212         status = usb_submit_urb(urb, GFP_ATOMIC);
213         if (status) {
214                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
215                 if (status != -EPERM) {
216                         err("can't resubmit intr, %s-%s/input%d, status %d",
217                                         hid_to_usb_dev(hid)->bus->bus_name,
218                                         hid_to_usb_dev(hid)->devpath,
219                                         usbhid->ifnum, status);
220                         hid_io_error(hid);
221                 }
222         }
223 }
224
225 static int hid_submit_out(struct hid_device *hid)
226 {
227         struct hid_report *report;
228         struct usbhid_device *usbhid = hid->driver_data;
229
230         report = usbhid->out[usbhid->outtail];
231
232         hid_output_report(report, usbhid->outbuf);
233         usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
234         usbhid->urbout->dev = hid_to_usb_dev(hid);
235
236         dbg("submitting out urb");
237
238         if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
239                 err("usb_submit_urb(out) failed");
240                 return -1;
241         }
242
243         return 0;
244 }
245
246 static int hid_submit_ctrl(struct hid_device *hid)
247 {
248         struct hid_report *report;
249         unsigned char dir;
250         int len;
251         struct usbhid_device *usbhid = hid->driver_data;
252
253         report = usbhid->ctrl[usbhid->ctrltail].report;
254         dir = usbhid->ctrl[usbhid->ctrltail].dir;
255
256         len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
257         if (dir == USB_DIR_OUT) {
258                 hid_output_report(report, usbhid->ctrlbuf);
259                 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
260                 usbhid->urbctrl->transfer_buffer_length = len;
261         } else {
262                 int maxpacket, padlen;
263
264                 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
265                 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
266                 if (maxpacket > 0) {
267                         padlen = (len + maxpacket - 1) / maxpacket;
268                         padlen *= maxpacket;
269                         if (padlen > usbhid->bufsize)
270                                 padlen = usbhid->bufsize;
271                 } else
272                         padlen = 0;
273                 usbhid->urbctrl->transfer_buffer_length = padlen;
274         }
275         usbhid->urbctrl->dev = hid_to_usb_dev(hid);
276
277         usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
278         usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
279         usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
280         usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
281         usbhid->cr->wLength = cpu_to_le16(len);
282
283         dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
284                 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
285                 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
286
287         if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
288                 err("usb_submit_urb(ctrl) failed");
289                 return -1;
290         }
291
292         return 0;
293 }
294
295 /*
296  * Output interrupt completion handler.
297  */
298
299 static void hid_irq_out(struct urb *urb)
300 {
301         struct hid_device *hid = urb->context;
302         struct usbhid_device *usbhid = hid->driver_data;
303         unsigned long flags;
304         int unplug = 0;
305
306         switch (urb->status) {
307                 case 0:                 /* success */
308                         break;
309                 case -ESHUTDOWN:        /* unplug */
310                         unplug = 1;
311                 case -EILSEQ:           /* protocol error or unplug */
312                 case -EPROTO:           /* protocol error or unplug */
313                 case -ECONNRESET:       /* unlink */
314                 case -ENOENT:
315                         break;
316                 default:                /* error */
317                         warn("output irq status %d received", urb->status);
318         }
319
320         spin_lock_irqsave(&usbhid->outlock, flags);
321
322         if (unplug)
323                 usbhid->outtail = usbhid->outhead;
324         else
325                 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
326
327         if (usbhid->outhead != usbhid->outtail) {
328                 if (hid_submit_out(hid)) {
329                         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
330                         wake_up(&hid->wait);
331                 }
332                 spin_unlock_irqrestore(&usbhid->outlock, flags);
333                 return;
334         }
335
336         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
337         spin_unlock_irqrestore(&usbhid->outlock, flags);
338         wake_up(&hid->wait);
339 }
340
341 /*
342  * Control pipe completion handler.
343  */
344
345 static void hid_ctrl(struct urb *urb)
346 {
347         struct hid_device *hid = urb->context;
348         struct usbhid_device *usbhid = hid->driver_data;
349         unsigned long flags;
350         int unplug = 0;
351
352         spin_lock_irqsave(&usbhid->ctrllock, flags);
353
354         switch (urb->status) {
355                 case 0:                 /* success */
356                         if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
357                                 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
358                                                 urb->transfer_buffer, urb->actual_length, 0);
359                         break;
360                 case -ESHUTDOWN:        /* unplug */
361                         unplug = 1;
362                 case -EILSEQ:           /* protocol error or unplug */
363                 case -EPROTO:           /* protocol error or unplug */
364                 case -ECONNRESET:       /* unlink */
365                 case -ENOENT:
366                 case -EPIPE:            /* report not available */
367                         break;
368                 default:                /* error */
369                         warn("ctrl urb status %d received", urb->status);
370         }
371
372         if (unplug)
373                 usbhid->ctrltail = usbhid->ctrlhead;
374         else
375                 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
376
377         if (usbhid->ctrlhead != usbhid->ctrltail) {
378                 if (hid_submit_ctrl(hid)) {
379                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
380                         wake_up(&hid->wait);
381                 }
382                 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
383                 return;
384         }
385
386         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
387         spin_unlock_irqrestore(&usbhid->ctrllock, flags);
388         wake_up(&hid->wait);
389 }
390
391 void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
392 {
393         int head;
394         unsigned long flags;
395         struct usbhid_device *usbhid = hid->driver_data;
396
397         if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
398                 return;
399
400         if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
401
402                 spin_lock_irqsave(&usbhid->outlock, flags);
403
404                 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
405                         spin_unlock_irqrestore(&usbhid->outlock, flags);
406                         warn("output queue full");
407                         return;
408                 }
409
410                 usbhid->out[usbhid->outhead] = report;
411                 usbhid->outhead = head;
412
413                 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
414                         if (hid_submit_out(hid))
415                                 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
416
417                 spin_unlock_irqrestore(&usbhid->outlock, flags);
418                 return;
419         }
420
421         spin_lock_irqsave(&usbhid->ctrllock, flags);
422
423         if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
424                 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
425                 warn("control queue full");
426                 return;
427         }
428
429         usbhid->ctrl[usbhid->ctrlhead].report = report;
430         usbhid->ctrl[usbhid->ctrlhead].dir = dir;
431         usbhid->ctrlhead = head;
432
433         if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
434                 if (hid_submit_ctrl(hid))
435                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
436
437         spin_unlock_irqrestore(&usbhid->ctrllock, flags);
438 }
439
440 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
441 {
442         struct hid_device *hid = dev->private;
443         struct hid_field *field;
444         int offset;
445
446         if (type == EV_FF)
447                 return input_ff_event(dev, type, code, value);
448
449         if (type != EV_LED)
450                 return -1;
451
452         if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
453                 warn("event field not found");
454                 return -1;
455         }
456
457         hid_set_field(field, offset, value);
458         usbhid_submit_report(hid, field->report, USB_DIR_OUT);
459
460         return 0;
461 }
462
463 int usbhid_wait_io(struct hid_device *hid)
464 {
465         struct usbhid_device *usbhid = hid->driver_data;
466
467         if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
468                                         !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
469                                         10*HZ)) {
470                 dbg("timeout waiting for ctrl or out queue to clear");
471                 return -1;
472         }
473
474         return 0;
475 }
476
477 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
478 {
479         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
480                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
481                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
482 }
483
484 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
485                 unsigned char type, void *buf, int size)
486 {
487         int result, retries = 4;
488
489         memset(buf, 0, size);
490
491         do {
492                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
493                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
494                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
495                 retries--;
496         } while (result < size && retries);
497         return result;
498 }
499
500 int usbhid_open(struct hid_device *hid)
501 {
502         ++hid->open;
503         if (hid_start_in(hid))
504                 hid_io_error(hid);
505         return 0;
506 }
507
508 void usbhid_close(struct hid_device *hid)
509 {
510         struct usbhid_device *usbhid = hid->driver_data;
511
512         if (!--hid->open)
513                 usb_kill_urb(usbhid->urbin);
514 }
515
516 /*
517  * Initialize all reports
518  */
519
520 void usbhid_init_reports(struct hid_device *hid)
521 {
522         struct hid_report *report;
523         struct usbhid_device *usbhid = hid->driver_data;
524         int err, ret;
525
526         list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
527                 usbhid_submit_report(hid, report, USB_DIR_IN);
528
529         list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
530                 usbhid_submit_report(hid, report, USB_DIR_IN);
531
532         err = 0;
533         ret = usbhid_wait_io(hid);
534         while (ret) {
535                 err |= ret;
536                 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
537                         usb_kill_urb(usbhid->urbctrl);
538                 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
539                         usb_kill_urb(usbhid->urbout);
540                 ret = usbhid_wait_io(hid);
541         }
542
543         if (err)
544                 warn("timeout initializing reports");
545 }
546
547 /*
548  * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
549  */
550 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
551     unsigned int hid_code, struct hid_field **pfield)
552 {
553         struct hid_report *report;
554         struct hid_field *field;
555         struct hid_usage *usage;
556         int i, j;
557
558         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
559                 for (i = 0; i < report->maxfield; i++) {
560                         field = report->field[i];
561                         for (j = 0; j < field->maxusage; j++) {
562                                 usage = &field->usage[j];
563                                 if ((usage->hid & HID_USAGE_PAGE) == page &&
564                                     (usage->hid & 0xFFFF) == hid_code) {
565                                         *pfield = field;
566                                         return j;
567                                 }
568                         }
569                 }
570         }
571         return -1;
572 }
573
574 static void usbhid_set_leds(struct hid_device *hid)
575 {
576         struct hid_field *field;
577         int offset;
578
579         if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
580                 hid_set_field(field, offset, 0);
581                 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
582         }
583 }
584
585 /*
586  * Traverse the supplied list of reports and find the longest
587  */
588 static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
589 {
590         struct hid_report *report;
591         int size;
592
593         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
594                 size = ((report->size - 1) >> 3) + 1;
595                 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
596                         size++;
597                 if (*max < size)
598                         *max = size;
599         }
600 }
601
602 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
603 {
604         struct usbhid_device *usbhid = hid->driver_data;
605
606         if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
607                 return -1;
608         if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
609                 return -1;
610         if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
611                 return -1;
612         if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
613                 return -1;
614
615         return 0;
616 }
617
618 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
619 {
620         struct usbhid_device *usbhid = hid->driver_data;
621
622         if (usbhid->inbuf)
623                 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
624         if (usbhid->outbuf)
625                 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
626         if (usbhid->cr)
627                 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
628         if (usbhid->ctrlbuf)
629                 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
630 }
631
632 /*
633  * Cherry Cymotion keyboard have an invalid HID report descriptor,
634  * that needs fixing before we can parse it.
635  */
636
637 static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
638 {
639         if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
640                 info("Fixing up Cherry Cymotion report descriptor");
641                 rdesc[11] = rdesc[16] = 0xff;
642                 rdesc[12] = rdesc[17] = 0x03;
643         }
644 }
645
646 /*
647  * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
648  * to "operational".  Without this, the ps3 controller will not report any
649  * events.
650  */
651 static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
652 {
653         int result;
654         char *buf = kmalloc(18, GFP_KERNEL);
655
656         if (!buf)
657                 return;
658
659         result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
660                                  HID_REQ_GET_REPORT,
661                                  USB_DIR_IN | USB_TYPE_CLASS |
662                                  USB_RECIP_INTERFACE,
663                                  (3 << 8) | 0xf2, ifnum, buf, 17,
664                                  USB_CTRL_GET_TIMEOUT);
665
666         if (result < 0)
667                 err("%s failed: %d\n", __func__, result);
668
669         kfree(buf);
670 }
671
672 /*
673  * Certain Logitech keyboards send in report #3 keys which are far
674  * above the logical maximum described in descriptor. This extends
675  * the original value of 0x28c of logical maximum to 0x104d
676  */
677 static void hid_fixup_logitech_descriptor(unsigned char *rdesc, int rsize)
678 {
679         if (rsize >= 90 && rdesc[83] == 0x26
680                         && rdesc[84] == 0x8c
681                         && rdesc[85] == 0x02) {
682                 info("Fixing up Logitech keyboard report descriptor");
683                 rdesc[84] = rdesc[89] = 0x4d;
684                 rdesc[85] = rdesc[90] = 0x10;
685         }
686 }
687
688 static struct hid_device *usb_hid_configure(struct usb_interface *intf)
689 {
690         struct usb_host_interface *interface = intf->cur_altsetting;
691         struct usb_device *dev = interface_to_usbdev (intf);
692         struct hid_descriptor *hdesc;
693         struct hid_device *hid;
694         u32 quirks = 0;
695         unsigned rsize = 0;
696         char *rdesc;
697         int n, len, insize = 0;
698         struct usbhid_device *usbhid;
699
700         quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
701                         le16_to_cpu(dev->descriptor.idProduct));
702
703         /* Many keyboards and mice don't like to be polled for reports,
704          * so we will always set the HID_QUIRK_NOGET flag for them. */
705         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
706                 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
707                         interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
708                                 quirks |= HID_QUIRK_NOGET;
709         }
710
711         if (quirks & HID_QUIRK_IGNORE)
712                 return NULL;
713
714         if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
715                 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
716                         return NULL;
717
718
719         if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
720             (!interface->desc.bNumEndpoints ||
721              usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
722                 dbg("class descriptor not present\n");
723                 return NULL;
724         }
725
726         for (n = 0; n < hdesc->bNumDescriptors; n++)
727                 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
728                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
729
730         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
731                 dbg("weird size of report descriptor (%u)", rsize);
732                 return NULL;
733         }
734
735         if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
736                 dbg("couldn't allocate rdesc memory");
737                 return NULL;
738         }
739
740         hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
741
742         if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
743                 dbg("reading report descriptor failed");
744                 kfree(rdesc);
745                 return NULL;
746         }
747
748         if ((quirks & HID_QUIRK_CYMOTION))
749                 hid_fixup_cymotion_descriptor(rdesc, rsize);
750
751         if (quirks & HID_QUIRK_LOGITECH_DESCRIPTOR)
752                 hid_fixup_logitech_descriptor(rdesc, rsize);
753
754 #ifdef CONFIG_HID_DEBUG
755         printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
756         for (n = 0; n < rsize; n++)
757                 printk(" %02x", (unsigned char) rdesc[n]);
758         printk("\n");
759 #endif
760
761         if (!(hid = hid_parse_report(rdesc, n))) {
762                 dbg("parsing report descriptor failed");
763                 kfree(rdesc);
764                 return NULL;
765         }
766
767         kfree(rdesc);
768         hid->quirks = quirks;
769
770         if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
771                 goto fail;
772
773         hid->driver_data = usbhid;
774         usbhid->hid = hid;
775
776         usbhid->bufsize = HID_MIN_BUFFER_SIZE;
777         hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
778         hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
779         hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
780
781         if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
782                 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
783
784         hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
785
786         if (insize > HID_MAX_BUFFER_SIZE)
787                 insize = HID_MAX_BUFFER_SIZE;
788
789         if (hid_alloc_buffers(dev, hid)) {
790                 hid_free_buffers(dev, hid);
791                 goto fail;
792         }
793
794         for (n = 0; n < interface->desc.bNumEndpoints; n++) {
795
796                 struct usb_endpoint_descriptor *endpoint;
797                 int pipe;
798                 int interval;
799
800                 endpoint = &interface->endpoint[n].desc;
801                 if ((endpoint->bmAttributes & 3) != 3)          /* Not an interrupt endpoint */
802                         continue;
803
804                 interval = endpoint->bInterval;
805
806                 /* Change the polling interval of mice. */
807                 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
808                         interval = hid_mousepoll_interval;
809
810                 if (usb_endpoint_dir_in(endpoint)) {
811                         if (usbhid->urbin)
812                                 continue;
813                         if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
814                                 goto fail;
815                         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
816                         usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
817                                          hid_irq_in, hid, interval);
818                         usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
819                         usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
820                 } else {
821                         if (usbhid->urbout)
822                                 continue;
823                         if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
824                                 goto fail;
825                         pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
826                         usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
827                                          hid_irq_out, hid, interval);
828                         usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
829                         usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
830                 }
831         }
832
833         if (!usbhid->urbin) {
834                 err("couldn't find an input interrupt endpoint");
835                 goto fail;
836         }
837
838         init_waitqueue_head(&hid->wait);
839
840         INIT_WORK(&usbhid->reset_work, hid_reset);
841         setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
842
843         spin_lock_init(&usbhid->inlock);
844         spin_lock_init(&usbhid->outlock);
845         spin_lock_init(&usbhid->ctrllock);
846
847         hid->version = le16_to_cpu(hdesc->bcdHID);
848         hid->country = hdesc->bCountryCode;
849         hid->dev = &intf->dev;
850         usbhid->intf = intf;
851         usbhid->ifnum = interface->desc.bInterfaceNumber;
852
853         hid->name[0] = 0;
854
855         if (dev->manufacturer)
856                 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
857
858         if (dev->product) {
859                 if (dev->manufacturer)
860                         strlcat(hid->name, " ", sizeof(hid->name));
861                 strlcat(hid->name, dev->product, sizeof(hid->name));
862         }
863
864         if (!strlen(hid->name))
865                 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
866                          le16_to_cpu(dev->descriptor.idVendor),
867                          le16_to_cpu(dev->descriptor.idProduct));
868
869         hid->bus = BUS_USB;
870         hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
871         hid->product = le16_to_cpu(dev->descriptor.idProduct);
872
873         usb_make_path(dev, hid->phys, sizeof(hid->phys));
874         strlcat(hid->phys, "/input", sizeof(hid->phys));
875         len = strlen(hid->phys);
876         if (len < sizeof(hid->phys) - 1)
877                 snprintf(hid->phys + len, sizeof(hid->phys) - len,
878                          "%d", intf->altsetting[0].desc.bInterfaceNumber);
879
880         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
881                 hid->uniq[0] = 0;
882
883         usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
884         if (!usbhid->urbctrl)
885                 goto fail;
886
887         usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
888                              usbhid->ctrlbuf, 1, hid_ctrl, hid);
889         usbhid->urbctrl->setup_dma = usbhid->cr_dma;
890         usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
891         usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
892         hid->hidinput_input_event = usb_hidinput_input_event;
893         hid->hid_open = usbhid_open;
894         hid->hid_close = usbhid_close;
895 #ifdef CONFIG_USB_HIDDEV
896         hid->hiddev_hid_event = hiddev_hid_event;
897         hid->hiddev_report_event = hiddev_report_event;
898 #endif
899         return hid;
900
901 fail:
902         usb_free_urb(usbhid->urbin);
903         usb_free_urb(usbhid->urbout);
904         usb_free_urb(usbhid->urbctrl);
905         hid_free_buffers(dev, hid);
906         hid_free_device(hid);
907
908         return NULL;
909 }
910
911 static void hid_disconnect(struct usb_interface *intf)
912 {
913         struct hid_device *hid = usb_get_intfdata (intf);
914         struct usbhid_device *usbhid;
915
916         if (!hid)
917                 return;
918
919         usbhid = hid->driver_data;
920
921         spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
922         usb_set_intfdata(intf, NULL);
923         spin_unlock_irq(&usbhid->inlock);
924         usb_kill_urb(usbhid->urbin);
925         usb_kill_urb(usbhid->urbout);
926         usb_kill_urb(usbhid->urbctrl);
927
928         del_timer_sync(&usbhid->io_retry);
929         flush_scheduled_work();
930
931         if (hid->claimed & HID_CLAIMED_INPUT)
932                 hidinput_disconnect(hid);
933         if (hid->claimed & HID_CLAIMED_HIDDEV)
934                 hiddev_disconnect(hid);
935
936         usb_free_urb(usbhid->urbin);
937         usb_free_urb(usbhid->urbctrl);
938         usb_free_urb(usbhid->urbout);
939
940         hid_free_buffers(hid_to_usb_dev(hid), hid);
941         hid_free_device(hid);
942 }
943
944 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
945 {
946         struct hid_device *hid;
947         char path[64];
948         int i;
949         char *c;
950
951         dbg("HID probe called for ifnum %d",
952                         intf->altsetting->desc.bInterfaceNumber);
953
954         if (!(hid = usb_hid_configure(intf)))
955                 return -ENODEV;
956
957         usbhid_init_reports(hid);
958         hid_dump_device(hid);
959         if (hid->quirks & HID_QUIRK_RESET_LEDS)
960                 usbhid_set_leds(hid);
961
962         if (!hidinput_connect(hid))
963                 hid->claimed |= HID_CLAIMED_INPUT;
964         if (!hiddev_connect(hid))
965                 hid->claimed |= HID_CLAIMED_HIDDEV;
966
967         usb_set_intfdata(intf, hid);
968
969         if (!hid->claimed) {
970                 printk ("HID device not claimed by input or hiddev\n");
971                 hid_disconnect(intf);
972                 return -ENODEV;
973         }
974
975         if ((hid->claimed & HID_CLAIMED_INPUT))
976                 hid_ff_init(hid);
977
978         if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
979                 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
980                         intf->cur_altsetting->desc.bInterfaceNumber);
981
982         printk(KERN_INFO);
983
984         if (hid->claimed & HID_CLAIMED_INPUT)
985                 printk("input");
986         if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
987                 printk(",");
988         if (hid->claimed & HID_CLAIMED_HIDDEV)
989                 printk("hiddev%d", hid->minor);
990
991         c = "Device";
992         for (i = 0; i < hid->maxcollection; i++) {
993                 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
994                     (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
995                     (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
996                         c = hid_types[hid->collection[i].usage & 0xffff];
997                         break;
998                 }
999         }
1000
1001         usb_make_path(interface_to_usbdev(intf), path, 63);
1002
1003         printk(": USB HID v%x.%02x %s [%s] on %s\n",
1004                 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1005
1006         return 0;
1007 }
1008
1009 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1010 {
1011         struct hid_device *hid = usb_get_intfdata (intf);
1012         struct usbhid_device *usbhid = hid->driver_data;
1013
1014         spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1015         set_bit(HID_SUSPENDED, &usbhid->iofl);
1016         spin_unlock_irq(&usbhid->inlock);
1017         del_timer(&usbhid->io_retry);
1018         usb_kill_urb(usbhid->urbin);
1019         dev_dbg(&intf->dev, "suspend\n");
1020         return 0;
1021 }
1022
1023 static int hid_resume(struct usb_interface *intf)
1024 {
1025         struct hid_device *hid = usb_get_intfdata (intf);
1026         struct usbhid_device *usbhid = hid->driver_data;
1027         int status;
1028
1029         clear_bit(HID_SUSPENDED, &usbhid->iofl);
1030         usbhid->retry_delay = 0;
1031         status = hid_start_in(hid);
1032         dev_dbg(&intf->dev, "resume status %d\n", status);
1033         return status;
1034 }
1035
1036 /* Treat USB reset pretty much the same as suspend/resume */
1037 static void hid_pre_reset(struct usb_interface *intf)
1038 {
1039         /* FIXME: What if the interface is already suspended? */
1040         hid_suspend(intf, PMSG_ON);
1041 }
1042
1043 static void hid_post_reset(struct usb_interface *intf)
1044 {
1045         struct usb_device *dev = interface_to_usbdev (intf);
1046
1047         hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1048         /* FIXME: Any more reinitialization needed? */
1049
1050         hid_resume(intf);
1051 }
1052
1053 static struct usb_device_id hid_usb_ids [] = {
1054         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1055                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1056         { }                                             /* Terminating entry */
1057 };
1058
1059 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1060
1061 static struct usb_driver hid_driver = {
1062         .name =         "usbhid",
1063         .probe =        hid_probe,
1064         .disconnect =   hid_disconnect,
1065         .suspend =      hid_suspend,
1066         .resume =       hid_resume,
1067         .pre_reset =    hid_pre_reset,
1068         .post_reset =   hid_post_reset,
1069         .id_table =     hid_usb_ids,
1070 };
1071
1072 static int __init hid_init(void)
1073 {
1074         int retval;
1075         retval = hiddev_init();
1076         if (retval)
1077                 goto hiddev_init_fail;
1078         retval = usb_register(&hid_driver);
1079         if (retval)
1080                 goto usb_register_fail;
1081         info(DRIVER_VERSION ":" DRIVER_DESC);
1082
1083         return 0;
1084 usb_register_fail:
1085         hiddev_exit();
1086 hiddev_init_fail:
1087         return retval;
1088 }
1089
1090 static void __exit hid_exit(void)
1091 {
1092         usb_deregister(&hid_driver);
1093         hiddev_exit();
1094 }
1095
1096 module_init(hid_init);
1097 module_exit(hid_exit);
1098
1099 MODULE_AUTHOR(DRIVER_AUTHOR);
1100 MODULE_DESCRIPTION(DRIVER_DESC);
1101 MODULE_LICENSE(DRIVER_LICENSE);