Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6] / drivers / usb / input / hid-core.c
1 /*
2  *  USB HID support for Linux
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@suse.cz>
6  */
7
8 /*
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  */
14
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/list.h>
21 #include <linux/mm.h>
22 #include <linux/smp_lock.h>
23 #include <linux/spinlock.h>
24 #include <asm/unaligned.h>
25 #include <asm/byteorder.h>
26 #include <linux/input.h>
27 #include <linux/wait.h>
28
29 #undef DEBUG
30 #undef DEBUG_DATA
31
32 #include <linux/usb.h>
33
34 #include "hid.h"
35 #include <linux/hiddev.h>
36
37 /*
38  * Version Information
39  */
40
41 #define DRIVER_VERSION "v2.01"
42 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
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  * Register a new report for a device.
58  */
59
60 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
61 {
62         struct hid_report_enum *report_enum = device->report_enum + type;
63         struct hid_report *report;
64
65         if (report_enum->report_id_hash[id])
66                 return report_enum->report_id_hash[id];
67
68         if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
69                 return NULL;
70         memset(report, 0, sizeof(struct hid_report));
71
72         if (id != 0)
73                 report_enum->numbered = 1;
74
75         report->id = id;
76         report->type = type;
77         report->size = 0;
78         report->device = device;
79         report_enum->report_id_hash[id] = report;
80
81         list_add_tail(&report->list, &report_enum->report_list);
82
83         return report;
84 }
85
86 /*
87  * Register a new field for this report.
88  */
89
90 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
91 {
92         struct hid_field *field;
93
94         if (report->maxfield == HID_MAX_FIELDS) {
95                 dbg("too many fields in report");
96                 return NULL;
97         }
98
99         if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
100                 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
101
102         memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
103                 + values * sizeof(unsigned));
104
105         field->index = report->maxfield++;
106         report->field[field->index] = field;
107         field->usage = (struct hid_usage *)(field + 1);
108         field->value = (unsigned *)(field->usage + usages);
109         field->report = report;
110
111         return field;
112 }
113
114 /*
115  * Open a collection. The type/usage is pushed on the stack.
116  */
117
118 static int open_collection(struct hid_parser *parser, unsigned type)
119 {
120         struct hid_collection *collection;
121         unsigned usage;
122
123         usage = parser->local.usage[0];
124
125         if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
126                 dbg("collection stack overflow");
127                 return -1;
128         }
129
130         if (parser->device->maxcollection == parser->device->collection_size) {
131                 collection = kmalloc(sizeof(struct hid_collection) *
132                                 parser->device->collection_size * 2, GFP_KERNEL);
133                 if (collection == NULL) {
134                         dbg("failed to reallocate collection array");
135                         return -1;
136                 }
137                 memcpy(collection, parser->device->collection,
138                         sizeof(struct hid_collection) *
139                         parser->device->collection_size);
140                 memset(collection + parser->device->collection_size, 0,
141                         sizeof(struct hid_collection) *
142                         parser->device->collection_size);
143                 kfree(parser->device->collection);
144                 parser->device->collection = collection;
145                 parser->device->collection_size *= 2;
146         }
147
148         parser->collection_stack[parser->collection_stack_ptr++] =
149                 parser->device->maxcollection;
150
151         collection = parser->device->collection +
152                 parser->device->maxcollection++;
153         collection->type = type;
154         collection->usage = usage;
155         collection->level = parser->collection_stack_ptr - 1;
156
157         if (type == HID_COLLECTION_APPLICATION)
158                 parser->device->maxapplication++;
159
160         return 0;
161 }
162
163 /*
164  * Close a collection.
165  */
166
167 static int close_collection(struct hid_parser *parser)
168 {
169         if (!parser->collection_stack_ptr) {
170                 dbg("collection stack underflow");
171                 return -1;
172         }
173         parser->collection_stack_ptr--;
174         return 0;
175 }
176
177 /*
178  * Climb up the stack, search for the specified collection type
179  * and return the usage.
180  */
181
182 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
183 {
184         int n;
185         for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
186                 if (parser->device->collection[parser->collection_stack[n]].type == type)
187                         return parser->device->collection[parser->collection_stack[n]].usage;
188         return 0; /* we know nothing about this usage type */
189 }
190
191 /*
192  * Add a usage to the temporary parser table.
193  */
194
195 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
196 {
197         if (parser->local.usage_index >= HID_MAX_USAGES) {
198                 dbg("usage index exceeded");
199                 return -1;
200         }
201         parser->local.usage[parser->local.usage_index] = usage;
202         parser->local.collection_index[parser->local.usage_index] =
203                 parser->collection_stack_ptr ?
204                 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
205         parser->local.usage_index++;
206         return 0;
207 }
208
209 /*
210  * Register a new field for this report.
211  */
212
213 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
214 {
215         struct hid_report *report;
216         struct hid_field *field;
217         int usages;
218         unsigned offset;
219         int i;
220
221         if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
222                 dbg("hid_register_report failed");
223                 return -1;
224         }
225
226         if (parser->global.logical_maximum < parser->global.logical_minimum) {
227                 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
228                 return -1;
229         }
230
231         offset = report->size;
232         report->size += parser->global.report_size * parser->global.report_count;
233
234         if (!parser->local.usage_index) /* Ignore padding fields */
235                 return 0;
236
237         usages = max_t(int, parser->local.usage_index, parser->global.report_count);
238
239         if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
240                 return 0;
241
242         field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
243         field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
244         field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
245
246         for (i = 0; i < usages; i++) {
247                 int j = i;
248                 /* Duplicate the last usage we parsed if we have excess values */
249                 if (i >= parser->local.usage_index)
250                         j = parser->local.usage_index - 1;
251                 field->usage[i].hid = parser->local.usage[j];
252                 field->usage[i].collection_index =
253                         parser->local.collection_index[j];
254         }
255
256         field->maxusage = usages;
257         field->flags = flags;
258         field->report_offset = offset;
259         field->report_type = report_type;
260         field->report_size = parser->global.report_size;
261         field->report_count = parser->global.report_count;
262         field->logical_minimum = parser->global.logical_minimum;
263         field->logical_maximum = parser->global.logical_maximum;
264         field->physical_minimum = parser->global.physical_minimum;
265         field->physical_maximum = parser->global.physical_maximum;
266         field->unit_exponent = parser->global.unit_exponent;
267         field->unit = parser->global.unit;
268
269         return 0;
270 }
271
272 /*
273  * Read data value from item.
274  */
275
276 static __inline__ __u32 item_udata(struct hid_item *item)
277 {
278         switch (item->size) {
279                 case 1: return item->data.u8;
280                 case 2: return item->data.u16;
281                 case 4: return item->data.u32;
282         }
283         return 0;
284 }
285
286 static __inline__ __s32 item_sdata(struct hid_item *item)
287 {
288         switch (item->size) {
289                 case 1: return item->data.s8;
290                 case 2: return item->data.s16;
291                 case 4: return item->data.s32;
292         }
293         return 0;
294 }
295
296 /*
297  * Process a global item.
298  */
299
300 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
301 {
302         switch (item->tag) {
303
304                 case HID_GLOBAL_ITEM_TAG_PUSH:
305
306                         if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
307                                 dbg("global enviroment stack overflow");
308                                 return -1;
309                         }
310
311                         memcpy(parser->global_stack + parser->global_stack_ptr++,
312                                 &parser->global, sizeof(struct hid_global));
313                         return 0;
314
315                 case HID_GLOBAL_ITEM_TAG_POP:
316
317                         if (!parser->global_stack_ptr) {
318                                 dbg("global enviroment stack underflow");
319                                 return -1;
320                         }
321
322                         memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
323                                 sizeof(struct hid_global));
324                         return 0;
325
326                 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
327                         parser->global.usage_page = item_udata(item);
328                         return 0;
329
330                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
331                         parser->global.logical_minimum = item_sdata(item);
332                         return 0;
333
334                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
335                         if (parser->global.logical_minimum < 0)
336                                 parser->global.logical_maximum = item_sdata(item);
337                         else
338                                 parser->global.logical_maximum = item_udata(item);
339                         return 0;
340
341                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
342                         parser->global.physical_minimum = item_sdata(item);
343                         return 0;
344
345                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
346                         if (parser->global.physical_minimum < 0)
347                                 parser->global.physical_maximum = item_sdata(item);
348                         else
349                                 parser->global.physical_maximum = item_udata(item);
350                         return 0;
351
352                 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
353                         parser->global.unit_exponent = item_sdata(item);
354                         return 0;
355
356                 case HID_GLOBAL_ITEM_TAG_UNIT:
357                         parser->global.unit = item_udata(item);
358                         return 0;
359
360                 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
361                         if ((parser->global.report_size = item_udata(item)) > 32) {
362                                 dbg("invalid report_size %d", parser->global.report_size);
363                                 return -1;
364                         }
365                         return 0;
366
367                 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
368                         if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
369                                 dbg("invalid report_count %d", parser->global.report_count);
370                                 return -1;
371                         }
372                         return 0;
373
374                 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
375                         if ((parser->global.report_id = item_udata(item)) == 0) {
376                                 dbg("report_id 0 is invalid");
377                                 return -1;
378                         }
379                         return 0;
380
381                 default:
382                         dbg("unknown global tag 0x%x", item->tag);
383                         return -1;
384         }
385 }
386
387 /*
388  * Process a local item.
389  */
390
391 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
392 {
393         __u32 data;
394         unsigned n;
395
396         if (item->size == 0) {
397                 dbg("item data expected for local item");
398                 return -1;
399         }
400
401         data = item_udata(item);
402
403         switch (item->tag) {
404
405                 case HID_LOCAL_ITEM_TAG_DELIMITER:
406
407                         if (data) {
408                                 /*
409                                  * We treat items before the first delimiter
410                                  * as global to all usage sets (branch 0).
411                                  * In the moment we process only these global
412                                  * items and the first delimiter set.
413                                  */
414                                 if (parser->local.delimiter_depth != 0) {
415                                         dbg("nested delimiters");
416                                         return -1;
417                                 }
418                                 parser->local.delimiter_depth++;
419                                 parser->local.delimiter_branch++;
420                         } else {
421                                 if (parser->local.delimiter_depth < 1) {
422                                         dbg("bogus close delimiter");
423                                         return -1;
424                                 }
425                                 parser->local.delimiter_depth--;
426                         }
427                         return 1;
428
429                 case HID_LOCAL_ITEM_TAG_USAGE:
430
431                         if (parser->local.delimiter_branch > 1) {
432                                 dbg("alternative usage ignored");
433                                 return 0;
434                         }
435
436                         if (item->size <= 2)
437                                 data = (parser->global.usage_page << 16) + data;
438
439                         return hid_add_usage(parser, data);
440
441                 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
442
443                         if (parser->local.delimiter_branch > 1) {
444                                 dbg("alternative usage ignored");
445                                 return 0;
446                         }
447
448                         if (item->size <= 2)
449                                 data = (parser->global.usage_page << 16) + data;
450
451                         parser->local.usage_minimum = data;
452                         return 0;
453
454                 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
455
456                         if (parser->local.delimiter_branch > 1) {
457                                 dbg("alternative usage ignored");
458                                 return 0;
459                         }
460
461                         if (item->size <= 2)
462                                 data = (parser->global.usage_page << 16) + data;
463
464                         for (n = parser->local.usage_minimum; n <= data; n++)
465                                 if (hid_add_usage(parser, n)) {
466                                         dbg("hid_add_usage failed\n");
467                                         return -1;
468                                 }
469                         return 0;
470
471                 default:
472
473                         dbg("unknown local item tag 0x%x", item->tag);
474                         return 0;
475         }
476         return 0;
477 }
478
479 /*
480  * Process a main item.
481  */
482
483 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
484 {
485         __u32 data;
486         int ret;
487
488         data = item_udata(item);
489
490         switch (item->tag) {
491                 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
492                         ret = open_collection(parser, data & 0xff);
493                         break;
494                 case HID_MAIN_ITEM_TAG_END_COLLECTION:
495                         ret = close_collection(parser);
496                         break;
497                 case HID_MAIN_ITEM_TAG_INPUT:
498                         ret = hid_add_field(parser, HID_INPUT_REPORT, data);
499                         break;
500                 case HID_MAIN_ITEM_TAG_OUTPUT:
501                         ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
502                         break;
503                 case HID_MAIN_ITEM_TAG_FEATURE:
504                         ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
505                         break;
506                 default:
507                         dbg("unknown main item tag 0x%x", item->tag);
508                         ret = 0;
509         }
510
511         memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
512
513         return ret;
514 }
515
516 /*
517  * Process a reserved item.
518  */
519
520 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
521 {
522         dbg("reserved item type, tag 0x%x", item->tag);
523         return 0;
524 }
525
526 /*
527  * Free a report and all registered fields. The field->usage and
528  * field->value table's are allocated behind the field, so we need
529  * only to free(field) itself.
530  */
531
532 static void hid_free_report(struct hid_report *report)
533 {
534         unsigned n;
535
536         for (n = 0; n < report->maxfield; n++)
537                 kfree(report->field[n]);
538         kfree(report);
539 }
540
541 /*
542  * Free a device structure, all reports, and all fields.
543  */
544
545 static void hid_free_device(struct hid_device *device)
546 {
547         unsigned i,j;
548
549         hid_ff_exit(device);
550
551         for (i = 0; i < HID_REPORT_TYPES; i++) {
552                 struct hid_report_enum *report_enum = device->report_enum + i;
553
554                 for (j = 0; j < 256; j++) {
555                         struct hid_report *report = report_enum->report_id_hash[j];
556                         if (report)
557                                 hid_free_report(report);
558                 }
559         }
560
561         kfree(device->rdesc);
562         kfree(device);
563 }
564
565 /*
566  * Fetch a report description item from the data stream. We support long
567  * items, though they are not used yet.
568  */
569
570 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
571 {
572         u8 b;
573
574         if ((end - start) <= 0)
575                 return NULL;
576
577         b = *start++;
578
579         item->type = (b >> 2) & 3;
580         item->tag  = (b >> 4) & 15;
581
582         if (item->tag == HID_ITEM_TAG_LONG) {
583
584                 item->format = HID_ITEM_FORMAT_LONG;
585
586                 if ((end - start) < 2)
587                         return NULL;
588
589                 item->size = *start++;
590                 item->tag  = *start++;
591
592                 if ((end - start) < item->size)
593                         return NULL;
594
595                 item->data.longdata = start;
596                 start += item->size;
597                 return start;
598         }
599
600         item->format = HID_ITEM_FORMAT_SHORT;
601         item->size = b & 3;
602
603         switch (item->size) {
604
605                 case 0:
606                         return start;
607
608                 case 1:
609                         if ((end - start) < 1)
610                                 return NULL;
611                         item->data.u8 = *start++;
612                         return start;
613
614                 case 2:
615                         if ((end - start) < 2)
616                                 return NULL;
617                         item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
618                         start = (__u8 *)((__le16 *)start + 1);
619                         return start;
620
621                 case 3:
622                         item->size++;
623                         if ((end - start) < 4)
624                                 return NULL;
625                         item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
626                         start = (__u8 *)((__le32 *)start + 1);
627                         return start;
628         }
629
630         return NULL;
631 }
632
633 /*
634  * Parse a report description into a hid_device structure. Reports are
635  * enumerated, fields are attached to these reports.
636  */
637
638 static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
639 {
640         struct hid_device *device;
641         struct hid_parser *parser;
642         struct hid_item item;
643         __u8 *end;
644         unsigned i;
645         static int (*dispatch_type[])(struct hid_parser *parser,
646                                       struct hid_item *item) = {
647                 hid_parser_main,
648                 hid_parser_global,
649                 hid_parser_local,
650                 hid_parser_reserved
651         };
652
653         if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
654                 return NULL;
655         memset(device, 0, sizeof(struct hid_device));
656
657         if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
658                                    HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
659                 kfree(device);
660                 return NULL;
661         }
662         memset(device->collection, 0, sizeof(struct hid_collection) *
663                 HID_DEFAULT_NUM_COLLECTIONS);
664         device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
665
666         for (i = 0; i < HID_REPORT_TYPES; i++)
667                 INIT_LIST_HEAD(&device->report_enum[i].report_list);
668
669         if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
670                 kfree(device->collection);
671                 kfree(device);
672                 return NULL;
673         }
674         memcpy(device->rdesc, start, size);
675         device->rsize = size;
676
677         if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
678                 kfree(device->rdesc);
679                 kfree(device->collection);
680                 kfree(device);
681                 return NULL;
682         }
683         memset(parser, 0, sizeof(struct hid_parser));
684         parser->device = device;
685
686         end = start + size;
687         while ((start = fetch_item(start, end, &item)) != NULL) {
688
689                 if (item.format != HID_ITEM_FORMAT_SHORT) {
690                         dbg("unexpected long global item");
691                         kfree(device->collection);
692                         hid_free_device(device);
693                         kfree(parser);
694                         return NULL;
695                 }
696
697                 if (dispatch_type[item.type](parser, &item)) {
698                         dbg("item %u %u %u %u parsing failed\n",
699                                 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
700                         kfree(device->collection);
701                         hid_free_device(device);
702                         kfree(parser);
703                         return NULL;
704                 }
705
706                 if (start == end) {
707                         if (parser->collection_stack_ptr) {
708                                 dbg("unbalanced collection at end of report description");
709                                 kfree(device->collection);
710                                 hid_free_device(device);
711                                 kfree(parser);
712                                 return NULL;
713                         }
714                         if (parser->local.delimiter_depth) {
715                                 dbg("unbalanced delimiter at end of report description");
716                                 kfree(device->collection);
717                                 hid_free_device(device);
718                                 kfree(parser);
719                                 return NULL;
720                         }
721                         kfree(parser);
722                         return device;
723                 }
724         }
725
726         dbg("item fetching failed at offset %d\n", (int)(end - start));
727         kfree(device->collection);
728         hid_free_device(device);
729         kfree(parser);
730         return NULL;
731 }
732
733 /*
734  * Convert a signed n-bit integer to signed 32-bit integer. Common
735  * cases are done through the compiler, the screwed things has to be
736  * done by hand.
737  */
738
739 static __inline__ __s32 snto32(__u32 value, unsigned n)
740 {
741         switch (n) {
742                 case 8:  return ((__s8)value);
743                 case 16: return ((__s16)value);
744                 case 32: return ((__s32)value);
745         }
746         return value & (1 << (n - 1)) ? value | (-1 << n) : value;
747 }
748
749 /*
750  * Convert a signed 32-bit integer to a signed n-bit integer.
751  */
752
753 static __inline__ __u32 s32ton(__s32 value, unsigned n)
754 {
755         __s32 a = value >> (n - 1);
756         if (a && a != -1)
757                 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
758         return value & ((1 << n) - 1);
759 }
760
761 /*
762  * Extract/implement a data field from/to a report.
763  */
764
765 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
766 {
767         report += (offset >> 5) << 2; offset &= 31;
768         return (le64_to_cpu(get_unaligned((__le64*)report)) >> offset) & ((1ULL << n) - 1);
769 }
770
771 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
772 {
773         report += (offset >> 5) << 2; offset &= 31;
774         put_unaligned((get_unaligned((__le64*)report)
775                 & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
776                 | cpu_to_le64((__u64)value << offset), (__le64*)report);
777 }
778
779 /*
780  * Search an array for a value.
781  */
782
783 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
784 {
785         while (n--) {
786                 if (*array++ == value)
787                         return 0;
788         }
789         return -1;
790 }
791
792 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt, struct pt_regs *regs)
793 {
794         hid_dump_input(usage, value);
795         if (hid->claimed & HID_CLAIMED_INPUT)
796                 hidinput_hid_event(hid, field, usage, value, regs);
797         if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
798                 hiddev_hid_event(hid, field, usage, value, regs);
799 }
800
801 /*
802  * Analyse a received field, and fetch the data from it. The field
803  * content is stored for next report processing (we do differential
804  * reporting to the layer).
805  */
806
807 static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt, struct pt_regs *regs)
808 {
809         unsigned n;
810         unsigned count = field->report_count;
811         unsigned offset = field->report_offset;
812         unsigned size = field->report_size;
813         __s32 min = field->logical_minimum;
814         __s32 max = field->logical_maximum;
815         __s32 *value;
816
817         if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
818                 return;
819
820         for (n = 0; n < count; n++) {
821
822                         value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
823                                                     extract(data, offset + n * size, size);
824
825                         if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
826                             && value[n] >= min && value[n] <= max
827                             && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
828                                 goto exit;
829         }
830
831         for (n = 0; n < count; n++) {
832
833                 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
834                         hid_process_event(hid, field, &field->usage[n], value[n], interrupt, regs);
835                         continue;
836                 }
837
838                 if (field->value[n] >= min && field->value[n] <= max
839                         && field->usage[field->value[n] - min].hid
840                         && search(value, field->value[n], count))
841                                 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt, regs);
842
843                 if (value[n] >= min && value[n] <= max
844                         && field->usage[value[n] - min].hid
845                         && search(field->value, value[n], count))
846                                 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt, regs);
847         }
848
849         memcpy(field->value, value, count * sizeof(__s32));
850 exit:
851         kfree(value);
852 }
853
854 static int hid_input_report(int type, struct urb *urb, int interrupt, struct pt_regs *regs)
855 {
856         struct hid_device *hid = urb->context;
857         struct hid_report_enum *report_enum = hid->report_enum + type;
858         u8 *data = urb->transfer_buffer;
859         int len = urb->actual_length;
860         struct hid_report *report;
861         int n, size;
862
863         if (!len) {
864                 dbg("empty report");
865                 return -1;
866         }
867
868 #ifdef DEBUG_DATA
869         printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
870 #endif
871
872         n = 0;                          /* Normally report number is 0 */
873         if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
874                 n = *data++;
875                 len--;
876         }
877
878 #ifdef DEBUG_DATA
879         {
880                 int i;
881                 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
882                 for (i = 0; i < len; i++)
883                         printk(" %02x", data[i]);
884                 printk("\n");
885         }
886 #endif
887
888         if (!(report = report_enum->report_id_hash[n])) {
889                 dbg("undefined report_id %d received", n);
890                 return -1;
891         }
892
893         size = ((report->size - 1) >> 3) + 1;
894
895         if (len < size)
896                 dbg("report %d is too short, (%d < %d)", report->id, len, size);
897
898         if (hid->claimed & HID_CLAIMED_HIDDEV)
899                 hiddev_report_event(hid, report);
900
901         for (n = 0; n < report->maxfield; n++)
902                 hid_input_field(hid, report->field[n], data, interrupt, regs);
903
904         if (hid->claimed & HID_CLAIMED_INPUT)
905                 hidinput_report_event(hid, report);
906
907         return 0;
908 }
909
910 /*
911  * Input interrupt completion handler.
912  */
913
914 static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
915 {
916         struct hid_device       *hid = urb->context;
917         int                     status;
918
919         switch (urb->status) {
920                 case 0:                 /* success */
921                         hid_input_report(HID_INPUT_REPORT, urb, 1, regs);
922                         break;
923                 case -ECONNRESET:       /* unlink */
924                 case -ENOENT:
925                 case -EPERM:
926                 case -ESHUTDOWN:        /* unplug */
927                 case -EILSEQ:           /* unplug timeout on uhci */
928                         return;
929                 case -ETIMEDOUT:        /* NAK */
930                         break;
931                 default:                /* error */
932                         warn("input irq status %d received", urb->status);
933         }
934
935         status = usb_submit_urb(urb, SLAB_ATOMIC);
936         if (status)
937                 err("can't resubmit intr, %s-%s/input%d, status %d",
938                                 hid->dev->bus->bus_name, hid->dev->devpath,
939                                 hid->ifnum, status);
940 }
941
942 /*
943  * Output the field into the report.
944  */
945
946 static void hid_output_field(struct hid_field *field, __u8 *data)
947 {
948         unsigned count = field->report_count;
949         unsigned offset = field->report_offset;
950         unsigned size = field->report_size;
951         unsigned n;
952
953         for (n = 0; n < count; n++) {
954                 if (field->logical_minimum < 0) /* signed values */
955                         implement(data, offset + n * size, size, s32ton(field->value[n], size));
956                 else                            /* unsigned values */
957                         implement(data, offset + n * size, size, field->value[n]);
958         }
959 }
960
961 /*
962  * Create a report.
963  */
964
965 static void hid_output_report(struct hid_report *report, __u8 *data)
966 {
967         unsigned n;
968
969         if (report->id > 0)
970                 *data++ = report->id;
971
972         for (n = 0; n < report->maxfield; n++)
973                 hid_output_field(report->field[n], data);
974 }
975
976 /*
977  * Set a field value. The report this field belongs to has to be
978  * created and transferred to the device, to set this value in the
979  * device.
980  */
981
982 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
983 {
984         unsigned size = field->report_size;
985
986         hid_dump_input(field->usage + offset, value);
987
988         if (offset >= field->report_count) {
989                 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
990                 hid_dump_field(field, 8);
991                 return -1;
992         }
993         if (field->logical_minimum < 0) {
994                 if (value != snto32(s32ton(value, size), size)) {
995                         dbg("value %d is out of range", value);
996                         return -1;
997                 }
998         }
999         field->value[offset] = value;
1000         return 0;
1001 }
1002
1003 /*
1004  * Find a report field with a specified HID usage.
1005  */
1006
1007 struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
1008 {
1009         struct hid_report *report;
1010         int i;
1011
1012         list_for_each_entry(report, &hid->report_enum[type].report_list, list)
1013                 for (i = 0; i < report->maxfield; i++)
1014                         if (report->field[i]->logical == wanted_usage)
1015                                 return report->field[i];
1016         return NULL;
1017 }
1018
1019 static int hid_submit_out(struct hid_device *hid)
1020 {
1021         struct hid_report *report;
1022
1023         report = hid->out[hid->outtail];
1024
1025         hid_output_report(report, hid->outbuf);
1026         hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1027         hid->urbout->dev = hid->dev;
1028
1029         dbg("submitting out urb");
1030
1031         if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
1032                 err("usb_submit_urb(out) failed");
1033                 return -1;
1034         }
1035
1036         return 0;
1037 }
1038
1039 static int hid_submit_ctrl(struct hid_device *hid)
1040 {
1041         struct hid_report *report;
1042         unsigned char dir;
1043         int len;
1044
1045         report = hid->ctrl[hid->ctrltail].report;
1046         dir = hid->ctrl[hid->ctrltail].dir;
1047
1048         len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1049         if (dir == USB_DIR_OUT) {
1050                 hid_output_report(report, hid->ctrlbuf);
1051                 hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
1052                 hid->urbctrl->transfer_buffer_length = len;
1053         } else {
1054                 int maxpacket, padlen;
1055
1056                 hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
1057                 maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
1058                 if (maxpacket > 0) {
1059                         padlen = (len + maxpacket - 1) / maxpacket;
1060                         padlen *= maxpacket;
1061                         if (padlen > HID_BUFFER_SIZE)
1062                                 padlen = HID_BUFFER_SIZE;
1063                 } else
1064                         padlen = 0;
1065                 hid->urbctrl->transfer_buffer_length = padlen;
1066         }
1067         hid->urbctrl->dev = hid->dev;
1068
1069         hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
1070         hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
1071         hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1072         hid->cr->wIndex = cpu_to_le16(hid->ifnum);
1073         hid->cr->wLength = cpu_to_le16(len);
1074
1075         dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
1076                 hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
1077                 hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
1078
1079         if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
1080                 err("usb_submit_urb(ctrl) failed");
1081                 return -1;
1082         }
1083
1084         return 0;
1085 }
1086
1087 /*
1088  * Output interrupt completion handler.
1089  */
1090
1091 static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1092 {
1093         struct hid_device *hid = urb->context;
1094         unsigned long flags;
1095         int unplug = 0;
1096
1097         switch (urb->status) {
1098                 case 0:                 /* success */
1099                 case -ESHUTDOWN:        /* unplug */
1100                 case -EILSEQ:           /* unplug timeout on uhci */
1101                         unplug = 1;
1102                 case -ECONNRESET:       /* unlink */
1103                 case -ENOENT:
1104                         break;
1105                 default:                /* error */
1106                         warn("output irq status %d received", urb->status);
1107         }
1108
1109         spin_lock_irqsave(&hid->outlock, flags);
1110
1111         if (unplug)
1112                 hid->outtail = hid->outhead;
1113         else
1114                 hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1115
1116         if (hid->outhead != hid->outtail) {
1117                 if (hid_submit_out(hid)) {
1118                         clear_bit(HID_OUT_RUNNING, &hid->iofl);;
1119                         wake_up(&hid->wait);
1120                 }
1121                 spin_unlock_irqrestore(&hid->outlock, flags);
1122                 return;
1123         }
1124
1125         clear_bit(HID_OUT_RUNNING, &hid->iofl);
1126         spin_unlock_irqrestore(&hid->outlock, flags);
1127         wake_up(&hid->wait);
1128 }
1129
1130 /*
1131  * Control pipe completion handler.
1132  */
1133
1134 static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
1135 {
1136         struct hid_device *hid = urb->context;
1137         unsigned long flags;
1138         int unplug = 0;
1139
1140         spin_lock_irqsave(&hid->ctrllock, flags);
1141
1142         switch (urb->status) {
1143                 case 0:                 /* success */
1144                         if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
1145                                 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs);
1146                 case -ESHUTDOWN:        /* unplug */
1147                 case -EILSEQ:           /* unplug timectrl on uhci */
1148                         unplug = 1;
1149                 case -ECONNRESET:       /* unlink */
1150                 case -ENOENT:
1151                 case -EPIPE:            /* report not available */
1152                         break;
1153                 default:                /* error */
1154                         warn("ctrl urb status %d received", urb->status);
1155         }
1156
1157         if (unplug)
1158                 hid->ctrltail = hid->ctrlhead;
1159         else
1160                 hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1161
1162         if (hid->ctrlhead != hid->ctrltail) {
1163                 if (hid_submit_ctrl(hid)) {
1164                         clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1165                         wake_up(&hid->wait);
1166                 }
1167                 spin_unlock_irqrestore(&hid->ctrllock, flags);
1168                 return;
1169         }
1170
1171         clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1172         spin_unlock_irqrestore(&hid->ctrllock, flags);
1173         wake_up(&hid->wait);
1174 }
1175
1176 void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
1177 {
1178         int head;
1179         unsigned long flags;
1180
1181         if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
1182                 return;
1183
1184         if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
1185
1186                 spin_lock_irqsave(&hid->outlock, flags);
1187
1188                 if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
1189                         spin_unlock_irqrestore(&hid->outlock, flags);
1190                         warn("output queue full");
1191                         return;
1192                 }
1193
1194                 hid->out[hid->outhead] = report;
1195                 hid->outhead = head;
1196
1197                 if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
1198                         if (hid_submit_out(hid))
1199                                 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1200
1201                 spin_unlock_irqrestore(&hid->outlock, flags);
1202                 return;
1203         }
1204
1205         spin_lock_irqsave(&hid->ctrllock, flags);
1206
1207         if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
1208                 spin_unlock_irqrestore(&hid->ctrllock, flags);
1209                 warn("control queue full");
1210                 return;
1211         }
1212
1213         hid->ctrl[hid->ctrlhead].report = report;
1214         hid->ctrl[hid->ctrlhead].dir = dir;
1215         hid->ctrlhead = head;
1216
1217         if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
1218                 if (hid_submit_ctrl(hid))
1219                         clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1220
1221         spin_unlock_irqrestore(&hid->ctrllock, flags);
1222 }
1223
1224 int hid_wait_io(struct hid_device *hid)
1225 {
1226         if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
1227                                         !test_bit(HID_OUT_RUNNING, &hid->iofl)),
1228                                         10*HZ)) {
1229                 dbg("timeout waiting for ctrl or out queue to clear");
1230                 return -1;
1231         }
1232
1233         return 0;
1234 }
1235
1236 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
1237 {
1238         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1239                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
1240                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
1241 }
1242
1243 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
1244                 unsigned char type, void *buf, int size)
1245 {
1246         int result, retries = 4;
1247
1248         memset(buf,0,size);     // Make sure we parse really received data
1249
1250         do {
1251                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1252                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
1253                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
1254                 retries--;
1255         } while (result < size && retries);
1256         return result;
1257 }
1258
1259 int hid_open(struct hid_device *hid)
1260 {
1261         if (hid->open++)
1262                 return 0;
1263
1264         hid->urbin->dev = hid->dev;
1265
1266         if (usb_submit_urb(hid->urbin, GFP_KERNEL))
1267                 return -EIO;
1268
1269         return 0;
1270 }
1271
1272 void hid_close(struct hid_device *hid)
1273 {
1274         if (!--hid->open)
1275                 usb_kill_urb(hid->urbin);
1276 }
1277
1278 /*
1279  * Initialize all reports
1280  */
1281
1282 void hid_init_reports(struct hid_device *hid)
1283 {
1284         struct hid_report *report;
1285         int err, ret;
1286
1287         list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) {
1288                 int size = ((report->size - 1) >> 3) + 1 + hid->report_enum[HID_INPUT_REPORT].numbered;
1289                 if (size > HID_BUFFER_SIZE) size = HID_BUFFER_SIZE;
1290                 if (size > hid->urbin->transfer_buffer_length)
1291                         hid->urbin->transfer_buffer_length = size;
1292                 hid_submit_report(hid, report, USB_DIR_IN);
1293         }
1294
1295         list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
1296                 hid_submit_report(hid, report, USB_DIR_IN);
1297
1298         err = 0;
1299         ret = hid_wait_io(hid);
1300         while (ret) {
1301                 err |= ret;
1302                 if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
1303                         usb_kill_urb(hid->urbctrl);
1304                 if (test_bit(HID_OUT_RUNNING, &hid->iofl))
1305                         usb_kill_urb(hid->urbout);
1306                 ret = hid_wait_io(hid);
1307         }
1308
1309         if (err)
1310                 warn("timeout initializing reports\n");
1311 }
1312
1313 #define USB_VENDOR_ID_WACOM             0x056a
1314 #define USB_DEVICE_ID_WACOM_PENPARTNER  0x0000
1315 #define USB_DEVICE_ID_WACOM_GRAPHIRE    0x0010
1316 #define USB_DEVICE_ID_WACOM_INTUOS      0x0020
1317 #define USB_DEVICE_ID_WACOM_PL          0x0030
1318 #define USB_DEVICE_ID_WACOM_INTUOS2     0x0040
1319 #define USB_DEVICE_ID_WACOM_VOLITO      0x0060
1320 #define USB_DEVICE_ID_WACOM_PTU         0x0003
1321 #define USB_DEVICE_ID_WACOM_INTUOS3     0x00B0
1322 #define USB_DEVICE_ID_WACOM_CINTIQ      0x003F
1323
1324 #define USB_VENDOR_ID_ACECAD            0x0460
1325 #define USB_DEVICE_ID_ACECAD_FLAIR      0x0004
1326 #define USB_DEVICE_ID_ACECAD_302        0x0008
1327
1328 #define USB_VENDOR_ID_KBGEAR            0x084e
1329 #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO  0x1001
1330
1331 #define USB_VENDOR_ID_AIPTEK            0x08ca
1332 #define USB_DEVICE_ID_AIPTEK_01         0x0001
1333 #define USB_DEVICE_ID_AIPTEK_10         0x0010
1334 #define USB_DEVICE_ID_AIPTEK_20         0x0020
1335 #define USB_DEVICE_ID_AIPTEK_21         0x0021
1336 #define USB_DEVICE_ID_AIPTEK_22         0x0022
1337 #define USB_DEVICE_ID_AIPTEK_23         0x0023
1338 #define USB_DEVICE_ID_AIPTEK_24         0x0024
1339
1340 #define USB_VENDOR_ID_GRIFFIN           0x077d
1341 #define USB_DEVICE_ID_POWERMATE         0x0410
1342 #define USB_DEVICE_ID_SOUNDKNOB         0x04AA
1343
1344 #define USB_VENDOR_ID_ATEN              0x0557
1345 #define USB_DEVICE_ID_ATEN_UC100KM      0x2004
1346 #define USB_DEVICE_ID_ATEN_CS124U       0x2202
1347 #define USB_DEVICE_ID_ATEN_2PORTKVM     0x2204
1348 #define USB_DEVICE_ID_ATEN_4PORTKVM     0x2205
1349 #define USB_DEVICE_ID_ATEN_4PORTKVMC    0x2208
1350
1351 #define USB_VENDOR_ID_TOPMAX            0x0663
1352 #define USB_DEVICE_ID_TOPMAX_COBRAPAD   0x0103
1353
1354 #define USB_VENDOR_ID_HAPP              0x078b
1355 #define USB_DEVICE_ID_UGCI_DRIVING      0x0010
1356 #define USB_DEVICE_ID_UGCI_FLYING       0x0020
1357 #define USB_DEVICE_ID_UGCI_FIGHTING     0x0030
1358
1359 #define USB_VENDOR_ID_MGE               0x0463
1360 #define USB_DEVICE_ID_MGE_UPS           0xffff
1361 #define USB_DEVICE_ID_MGE_UPS1          0x0001
1362
1363 #define USB_VENDOR_ID_ONTRAK            0x0a07
1364 #define USB_DEVICE_ID_ONTRAK_ADU100     0x0064
1365
1366 #define USB_VENDOR_ID_TANGTOP           0x0d3d
1367 #define USB_DEVICE_ID_TANGTOP_USBPS2    0x0001
1368
1369 #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1370 #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
1371
1372 #define USB_VENDOR_ID_A4TECH            0x09da
1373 #define USB_DEVICE_ID_A4TECH_WCP32PU    0x0006
1374
1375 #define USB_VENDOR_ID_AASHIMA           0x06D6
1376 #define USB_DEVICE_ID_AASHIMA_GAMEPAD   0x0025
1377
1378 #define USB_VENDOR_ID_CYPRESS           0x04b4
1379 #define USB_DEVICE_ID_CYPRESS_MOUSE     0x0001
1380 #define USB_DEVICE_ID_CYPRESS_HIDCOM    0x5500
1381
1382 #define USB_VENDOR_ID_BERKSHIRE         0x0c98
1383 #define USB_DEVICE_ID_BERKSHIRE_PCWD    0x1140
1384
1385 #define USB_VENDOR_ID_ALPS              0x0433
1386 #define USB_DEVICE_ID_IBM_GAMEPAD       0x1101
1387
1388 #define USB_VENDOR_ID_SAITEK            0x06a3
1389 #define USB_DEVICE_ID_SAITEK_RUMBLEPAD  0xff17
1390
1391 #define USB_VENDOR_ID_NEC               0x073e
1392 #define USB_DEVICE_ID_NEC_USB_GAME_PAD  0x0301
1393
1394 #define USB_VENDOR_ID_CHIC              0x05fe
1395 #define USB_DEVICE_ID_CHIC_GAMEPAD      0x0014
1396
1397 #define USB_VENDOR_ID_GLAB              0x06c2
1398 #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
1399 #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
1400 #define USB_DEVICE_ID_8_8_8_IF_KIT      0x0045
1401 #define USB_DEVICE_ID_0_0_4_IF_KIT      0x0040
1402 #define USB_DEVICE_ID_0_8_8_IF_KIT      0x0053
1403
1404 #define USB_VENDOR_ID_WISEGROUP         0x0925
1405 #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
1406 #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
1407
1408 #define USB_VENDOR_ID_CODEMERCS         0x07c0
1409 #define USB_DEVICE_ID_CODEMERCS_IOW40   0x1500
1410 #define USB_DEVICE_ID_CODEMERCS_IOW24   0x1501
1411 #define USB_DEVICE_ID_CODEMERCS_IOW48   0x1502
1412 #define USB_DEVICE_ID_CODEMERCS_IOW28   0x1503
1413
1414 #define USB_VENDOR_ID_DELORME           0x1163
1415 #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
1416 #define USB_DEVICE_ID_DELORME_EM_LT20   0x0200
1417
1418 #define USB_VENDOR_ID_MCC               0x09db
1419 #define USB_DEVICE_ID_MCC_PMD1024LS     0x0076
1420 #define USB_DEVICE_ID_MCC_PMD1208LS     0x007a
1421
1422 #define USB_VENDOR_ID_CHICONY           0x04f2
1423 #define USB_DEVICE_ID_CHICONY_USBHUB_KB 0x0100
1424
1425 #define USB_VENDOR_ID_BTC               0x046e
1426 #define USB_DEVICE_ID_BTC_KEYBOARD      0x5303
1427
1428 #define USB_VENDOR_ID_VERNIER           0x08f7
1429 #define USB_DEVICE_ID_VERNIER_LABPRO    0x0001
1430 #define USB_DEVICE_ID_VERNIER_GOTEMP    0x0002
1431 #define USB_DEVICE_ID_VERNIER_SKIP      0x0003
1432 #define USB_DEVICE_ID_VERNIER_CYCLOPS   0x0004
1433
1434 #define USB_VENDOR_ID_LD                0x0f11
1435 #define USB_DEVICE_ID_CASSY             0x1000
1436 #define USB_DEVICE_ID_POCKETCASSY       0x1010
1437 #define USB_DEVICE_ID_MOBILECASSY       0x1020
1438 #define USB_DEVICE_ID_JWM               0x1080
1439 #define USB_DEVICE_ID_DMMP              0x1081
1440 #define USB_DEVICE_ID_UMIP              0x1090
1441 #define USB_DEVICE_ID_VIDEOCOM          0x1200
1442 #define USB_DEVICE_ID_COM3LAB           0x2000
1443 #define USB_DEVICE_ID_TELEPORT          0x2010
1444 #define USB_DEVICE_ID_NETWORKANALYSER   0x2020
1445 #define USB_DEVICE_ID_POWERCONTROL      0x2030
1446
1447 #define USB_VENDOR_ID_APPLE             0x05ac
1448 #define USB_DEVICE_ID_APPLE_BLUETOOTH   0x1000
1449
1450 /*
1451  * Alphabetically sorted blacklist by quirk type.
1452  */
1453
1454 static struct hid_blacklist {
1455         __u16 idVendor;
1456         __u16 idProduct;
1457         unsigned quirks;
1458 } hid_blacklist[] = {
1459
1460         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1461         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1462         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1463         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1464         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1465         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1466         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1467         { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_BLUETOOTH, HID_QUIRK_IGNORE },
1468         { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
1469         { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
1470         { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
1471         { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
1472         { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
1473         { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
1474         { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
1475         { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
1476         { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1477         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1478         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1479         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
1480         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
1481         { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
1482         { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1483         { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1484         { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1485         { USB_VENDOR_ID_LD, USB_DEVICE_ID_CASSY, HID_QUIRK_IGNORE },
1486         { USB_VENDOR_ID_LD, USB_DEVICE_ID_POCKETCASSY, HID_QUIRK_IGNORE },
1487         { USB_VENDOR_ID_LD, USB_DEVICE_ID_MOBILECASSY, HID_QUIRK_IGNORE },
1488         { USB_VENDOR_ID_LD, USB_DEVICE_ID_JWM, HID_QUIRK_IGNORE },
1489         { USB_VENDOR_ID_LD, USB_DEVICE_ID_DMMP, HID_QUIRK_IGNORE },
1490         { USB_VENDOR_ID_LD, USB_DEVICE_ID_UMIP, HID_QUIRK_IGNORE },
1491         { USB_VENDOR_ID_LD, USB_DEVICE_ID_VIDEOCOM, HID_QUIRK_IGNORE },
1492         { USB_VENDOR_ID_LD, USB_DEVICE_ID_COM3LAB, HID_QUIRK_IGNORE },
1493         { USB_VENDOR_ID_LD, USB_DEVICE_ID_TELEPORT, HID_QUIRK_IGNORE },
1494         { USB_VENDOR_ID_LD, USB_DEVICE_ID_NETWORKANALYSER, HID_QUIRK_IGNORE },
1495         { USB_VENDOR_ID_LD, USB_DEVICE_ID_POWERCONTROL, HID_QUIRK_IGNORE },
1496         { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
1497         { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
1498         { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1499         { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1500         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1501         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1502         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1503         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1504         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1505         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1506         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
1507         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
1508         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
1509         { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
1510         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1511         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1512         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1513         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1514         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
1515         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
1516         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1517         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1518         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1519         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1520         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1521         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1522         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1523         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1524         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1525         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1526         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1527         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1528         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1529         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1530         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1531         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
1532         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
1533         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
1534         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
1535         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3, HID_QUIRK_IGNORE },
1536         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 1, HID_QUIRK_IGNORE },
1537         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 2, HID_QUIRK_IGNORE },
1538         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_CINTIQ, HID_QUIRK_IGNORE },
1539         { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
1540         { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
1541
1542         { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
1543         { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
1544
1545         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1546         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1547         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1548         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1549         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
1550         { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
1551         { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
1552         { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1553
1554         { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
1555         { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
1556
1557         { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
1558         { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
1559         { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
1560         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1561         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1562         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1563         { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1564         { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
1565         { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1566
1567         { 0, 0 }
1568 };
1569
1570 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
1571 {
1572         if (!(hid->inbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->inbuf_dma)))
1573                 return -1;
1574         if (!(hid->outbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->outbuf_dma)))
1575                 return -1;
1576         if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
1577                 return -1;
1578         if (!(hid->ctrlbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
1579                 return -1;
1580
1581         return 0;
1582 }
1583
1584 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1585 {
1586         if (hid->inbuf)
1587                 usb_buffer_free(dev, HID_BUFFER_SIZE, hid->inbuf, hid->inbuf_dma);
1588         if (hid->outbuf)
1589                 usb_buffer_free(dev, HID_BUFFER_SIZE, hid->outbuf, hid->outbuf_dma);
1590         if (hid->cr)
1591                 usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
1592         if (hid->ctrlbuf)
1593                 usb_buffer_free(dev, HID_BUFFER_SIZE, hid->ctrlbuf, hid->ctrlbuf_dma);
1594 }
1595
1596 static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1597 {
1598         struct usb_host_interface *interface = intf->cur_altsetting;
1599         struct usb_device *dev = interface_to_usbdev (intf);
1600         struct hid_descriptor *hdesc;
1601         struct hid_device *hid;
1602         unsigned quirks = 0, rsize = 0;
1603         char *buf, *rdesc;
1604         int n;
1605
1606         for (n = 0; hid_blacklist[n].idVendor; n++)
1607                 if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
1608                         (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
1609                                 quirks = hid_blacklist[n].quirks;
1610
1611         if (quirks & HID_QUIRK_IGNORE)
1612                 return NULL;
1613
1614         if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && ((!interface->desc.bNumEndpoints) ||
1615                 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1616                         dbg("class descriptor not present\n");
1617                         return NULL;
1618         }
1619
1620         for (n = 0; n < hdesc->bNumDescriptors; n++)
1621                 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1622                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1623
1624         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1625                 dbg("weird size of report descriptor (%u)", rsize);
1626                 return NULL;
1627         }
1628
1629         if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1630                 dbg("couldn't allocate rdesc memory");
1631                 return NULL;
1632         }
1633
1634         hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1635
1636         if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1637                 dbg("reading report descriptor failed");
1638                 kfree(rdesc);
1639                 return NULL;
1640         }
1641
1642 #ifdef DEBUG_DATA
1643         printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1644         for (n = 0; n < rsize; n++)
1645                 printk(" %02x", (unsigned char) rdesc[n]);
1646         printk("\n");
1647 #endif
1648
1649         if (!(hid = hid_parse_report(rdesc, n))) {
1650                 dbg("parsing report descriptor failed");
1651                 kfree(rdesc);
1652                 return NULL;
1653         }
1654
1655         kfree(rdesc);
1656         hid->quirks = quirks;
1657
1658         if (hid_alloc_buffers(dev, hid)) {
1659                 hid_free_buffers(dev, hid);
1660                 goto fail;
1661         }
1662
1663         for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1664
1665                 struct usb_endpoint_descriptor *endpoint;
1666                 int pipe;
1667                 int interval;
1668
1669                 endpoint = &interface->endpoint[n].desc;
1670                 if ((endpoint->bmAttributes & 3) != 3)          /* Not an interrupt endpoint */
1671                         continue;
1672
1673                 /* handle potential highspeed HID correctly */
1674                 interval = endpoint->bInterval;
1675                 if (dev->speed == USB_SPEED_HIGH)
1676                         interval = 1 << (interval - 1);
1677
1678                 /* Change the polling interval of mice. */
1679                 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1680                         interval = hid_mousepoll_interval;
1681
1682                 if (endpoint->bEndpointAddress & USB_DIR_IN) {
1683                         if (hid->urbin)
1684                                 continue;
1685                         if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1686                                 goto fail;
1687                         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1688                         usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, 0,
1689                                          hid_irq_in, hid, interval);
1690                         hid->urbin->transfer_dma = hid->inbuf_dma;
1691                         hid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1692                 } else {
1693                         if (hid->urbout)
1694                                 continue;
1695                         if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1696                                 goto fail;
1697                         pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1698                         usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
1699                                          hid_irq_out, hid, interval);
1700                         hid->urbout->transfer_dma = hid->outbuf_dma;
1701                         hid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1702                 }
1703         }
1704
1705         if (!hid->urbin) {
1706                 err("couldn't find an input interrupt endpoint");
1707                 goto fail;
1708         }
1709
1710         init_waitqueue_head(&hid->wait);
1711
1712         spin_lock_init(&hid->outlock);
1713         spin_lock_init(&hid->ctrllock);
1714
1715         hid->version = le16_to_cpu(hdesc->bcdHID);
1716         hid->country = hdesc->bCountryCode;
1717         hid->dev = dev;
1718         hid->intf = intf;
1719         hid->ifnum = interface->desc.bInterfaceNumber;
1720
1721         hid->name[0] = 0;
1722
1723         if (!(buf = kmalloc(64, GFP_KERNEL)))
1724                 goto fail;
1725
1726         if (dev->manufacturer) {
1727                 strcat(hid->name, dev->manufacturer);
1728                 if (dev->product)
1729                         snprintf(hid->name, 64, "%s %s", hid->name, dev->product);
1730         } else if (dev->product) {
1731                         snprintf(hid->name, 128, "%s", dev->product);
1732         } else
1733                 snprintf(hid->name, 128, "%04x:%04x",
1734                         le16_to_cpu(dev->descriptor.idVendor),
1735                         le16_to_cpu(dev->descriptor.idProduct));
1736
1737         usb_make_path(dev, buf, 64);
1738         snprintf(hid->phys, 64, "%s/input%d", buf,
1739                         intf->altsetting[0].desc.bInterfaceNumber);
1740
1741         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1742                 hid->uniq[0] = 0;
1743
1744         kfree(buf);
1745
1746         hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1747         if (!hid->urbctrl)
1748                 goto fail;
1749         usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
1750                              hid->ctrlbuf, 1, hid_ctrl, hid);
1751         hid->urbctrl->setup_dma = hid->cr_dma;
1752         hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
1753         hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
1754
1755         return hid;
1756
1757 fail:
1758
1759         if (hid->urbin)
1760                 usb_free_urb(hid->urbin);
1761         if (hid->urbout)
1762                 usb_free_urb(hid->urbout);
1763         if (hid->urbctrl)
1764                 usb_free_urb(hid->urbctrl);
1765         hid_free_buffers(dev, hid);
1766         hid_free_device(hid);
1767
1768         return NULL;
1769 }
1770
1771 static void hid_disconnect(struct usb_interface *intf)
1772 {
1773         struct hid_device *hid = usb_get_intfdata (intf);
1774
1775         if (!hid)
1776                 return;
1777
1778         usb_set_intfdata(intf, NULL);
1779         usb_kill_urb(hid->urbin);
1780         usb_kill_urb(hid->urbout);
1781         usb_kill_urb(hid->urbctrl);
1782
1783         if (hid->claimed & HID_CLAIMED_INPUT)
1784                 hidinput_disconnect(hid);
1785         if (hid->claimed & HID_CLAIMED_HIDDEV)
1786                 hiddev_disconnect(hid);
1787
1788         usb_free_urb(hid->urbin);
1789         usb_free_urb(hid->urbctrl);
1790         if (hid->urbout)
1791                 usb_free_urb(hid->urbout);
1792
1793         hid_free_buffers(hid->dev, hid);
1794         hid_free_device(hid);
1795 }
1796
1797 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1798 {
1799         struct hid_device *hid;
1800         char path[64];
1801         int i;
1802         char *c;
1803
1804         dbg("HID probe called for ifnum %d",
1805                         intf->altsetting->desc.bInterfaceNumber);
1806
1807         if (!(hid = usb_hid_configure(intf)))
1808                 return -ENODEV;
1809
1810         hid_init_reports(hid);
1811         hid_dump_device(hid);
1812
1813         if (!hidinput_connect(hid))
1814                 hid->claimed |= HID_CLAIMED_INPUT;
1815         if (!hiddev_connect(hid))
1816                 hid->claimed |= HID_CLAIMED_HIDDEV;
1817
1818         usb_set_intfdata(intf, hid);
1819
1820         if (!hid->claimed) {
1821                 printk ("HID device not claimed by input or hiddev\n");
1822                 hid_disconnect(intf);
1823                 return -ENODEV;
1824         }
1825
1826         printk(KERN_INFO);
1827
1828         if (hid->claimed & HID_CLAIMED_INPUT)
1829                 printk("input");
1830         if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1831                 printk(",");
1832         if (hid->claimed & HID_CLAIMED_HIDDEV)
1833                 printk("hiddev%d", hid->minor);
1834
1835         c = "Device";
1836         for (i = 0; i < hid->maxcollection; i++) {
1837                 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1838                     (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1839                     (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1840                         c = hid_types[hid->collection[i].usage & 0xffff];
1841                         break;
1842                 }
1843         }
1844
1845         usb_make_path(interface_to_usbdev(intf), path, 63);
1846
1847         printk(": USB HID v%x.%02x %s [%s] on %s\n",
1848                 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1849
1850         return 0;
1851 }
1852
1853 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1854 {
1855         struct hid_device *hid = usb_get_intfdata (intf);
1856
1857         usb_kill_urb(hid->urbin);
1858         intf->dev.power.power_state = PMSG_SUSPEND;
1859         dev_dbg(&intf->dev, "suspend\n");
1860         return 0;
1861 }
1862
1863 static int hid_resume(struct usb_interface *intf)
1864 {
1865         struct hid_device *hid = usb_get_intfdata (intf);
1866         int status;
1867
1868         intf->dev.power.power_state = PMSG_ON;
1869         if (hid->open)
1870                 status = usb_submit_urb(hid->urbin, GFP_NOIO);
1871         else
1872                 status = 0;
1873         dev_dbg(&intf->dev, "resume status %d\n", status);
1874         return status;
1875 }
1876
1877 static struct usb_device_id hid_usb_ids [] = {
1878         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1879                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1880         { }                                             /* Terminating entry */
1881 };
1882
1883 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1884
1885 static struct usb_driver hid_driver = {
1886         .owner =        THIS_MODULE,
1887         .name =         "usbhid",
1888         .probe =        hid_probe,
1889         .disconnect =   hid_disconnect,
1890         .suspend =      hid_suspend,
1891         .resume =       hid_resume,
1892         .id_table =     hid_usb_ids,
1893 };
1894
1895 static int __init hid_init(void)
1896 {
1897         int retval;
1898         retval = hiddev_init();
1899         if (retval)
1900                 goto hiddev_init_fail;
1901         retval = usb_register(&hid_driver);
1902         if (retval)
1903                 goto usb_register_fail;
1904         info(DRIVER_VERSION ":" DRIVER_DESC);
1905
1906         return 0;
1907 usb_register_fail:
1908         hiddev_exit();
1909 hiddev_init_fail:
1910         return retval;
1911 }
1912
1913 static void __exit hid_exit(void)
1914 {
1915         usb_deregister(&hid_driver);
1916         hiddev_exit();
1917 }
1918
1919 module_init(hid_init);
1920 module_exit(hid_exit);
1921
1922 MODULE_AUTHOR(DRIVER_AUTHOR);
1923 MODULE_DESCRIPTION(DRIVER_DESC);
1924 MODULE_LICENSE(DRIVER_LICENSE);