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