more changes on original files
[linux-2.4.git] / drivers / usb / hid-core.c
1 /*
2  * $Id: hid-core.c,v 1.8 2001/05/23 12:02:18 vojtech Exp $
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2001 Vojtech Pavlik
6  *
7  *  USB HID support for Linux
8  *
9  *  Sponsored by SuSE
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * Should you need to contact me, the author, you can do so either by
28  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
30  */
31
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/list.h>
38 #include <linux/mm.h>
39 #include <linux/smp_lock.h>
40 #include <linux/spinlock.h>
41 #include <asm/unaligned.h>
42 #include <linux/input.h>
43
44 #undef DEBUG
45 #undef DEBUG_DATA
46
47 #include <linux/usb.h>
48
49 #include "hid.h"
50 #include <linux/hiddev.h>
51
52 /*
53  * Version Information
54  */
55
56 #define DRIVER_VERSION "v1.8.1"
57 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik <vojtech@suse.cz>"
58 #define DRIVER_DESC "USB HID support drivers"
59
60 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
61                                 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
62
63 /*
64  * Register a new report for a device.
65  */
66
67 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
68 {
69         struct hid_report_enum *report_enum = device->report_enum + type;
70         struct hid_report *report;
71
72         if (report_enum->report_id_hash[id])
73                 return report_enum->report_id_hash[id];
74
75         if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
76                 return NULL;
77         memset(report, 0, sizeof(struct hid_report));
78
79         if (id != 0) report_enum->numbered = 1;
80
81         report->id = id;
82         report->type = type;
83         report->size = 0;
84         report->device = device;
85         report_enum->report_id_hash[id] = report;
86
87         list_add_tail(&report->list, &report_enum->report_list);
88
89         return report;
90 }
91
92 /*
93  * Register a new field for this report.
94  */
95
96 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
97 {
98         struct hid_field *field;
99
100         if (report->maxfield == HID_MAX_FIELDS) {
101                 dbg("too many fields in report");
102                 return NULL;
103         }
104
105         if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
106                 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
107
108         memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
109                 + values * sizeof(unsigned));
110
111         report->field[report->maxfield] = field;
112         field->usage = (struct hid_usage *)(field + 1);
113         field->value = (unsigned *)(field->usage + usages);
114         field->report = report;
115         field->index = report->maxfield++;
116
117         return field;
118 }
119
120 /*
121  * Open a collection. The type/usage is pushed on the stack.
122  */
123
124 static int open_collection(struct hid_parser *parser, unsigned type)
125 {
126         struct hid_collection *collection;
127         unsigned usage;
128
129         usage = parser->local.usage[0];
130
131         if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
132                 dbg("collection stack overflow");
133                 return -1;
134         }
135
136         if (parser->device->maxcollection == parser->device->collection_size) {
137                 collection = kmalloc(sizeof(struct hid_collection) *
138                                      parser->device->collection_size * 2,
139                                      GFP_KERNEL);
140                 if (collection == NULL) {
141                         dbg("failed to reallocate collection array");
142                         return -1;
143                 }
144                 memcpy(collection, parser->device->collection,
145                        sizeof(struct hid_collection) *
146                        parser->device->collection_size);
147                 memset(collection + parser->device->collection_size, 0,
148                        sizeof(struct hid_collection) *
149                        parser->device->collection_size);
150                 kfree(parser->device->collection);
151                 parser->device->collection = collection;
152                 parser->device->collection_size *= 2;
153         }
154
155         parser->collection_stack[parser->collection_stack_ptr++] =
156                 parser->device->maxcollection;
157
158         collection = parser->device->collection + 
159                 parser->device->maxcollection++;
160
161         collection->type = type;
162         collection->usage = usage;
163         collection->level = parser->collection_stack_ptr - 1;
164
165         if (type == HID_COLLECTION_APPLICATION)
166                 parser->device->maxapplication++;
167
168         return 0;
169 }
170
171 /*
172  * Close a collection.
173  */
174
175 static int close_collection(struct hid_parser *parser)
176 {
177         if (!parser->collection_stack_ptr) {
178                 dbg("collection stack underflow");
179                 return -1;
180         }
181         parser->collection_stack_ptr--;
182         return 0;
183 }
184
185 /*
186  * Climb up the stack, search for the specified collection type
187  * and return the usage.
188  */
189
190 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
191 {
192         int n;
193         for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
194                 if (parser->device->collection[parser->collection_stack[n]].type == type)
195                         return parser->device->collection[parser->collection_stack[n]].usage;
196
197         return 0; /* we know nothing about this usage type */
198 }
199
200 /*
201  * Add a usage to the temporary parser table.
202  */
203
204 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
205 {
206         if (parser->local.usage_index >= HID_MAX_USAGES) {
207                 dbg("usage index exceeded");
208                 return -1;
209         }
210         parser->local.usage[parser->local.usage_index] = usage;
211         parser->local.collection_index[parser->local.usage_index] =
212                 parser->collection_stack_ptr ? 
213                 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
214         parser->local.usage_index++;
215
216         return 0;
217 }
218
219 /*
220  * Register a new field for this report.
221  */
222
223 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
224 {
225         struct hid_report *report;
226         struct hid_field *field;
227         int usages;
228         unsigned offset;
229         int i;
230
231         if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
232                 dbg("hid_register_report failed");
233                 return -1;
234         }
235
236         if (parser->global.logical_maximum < parser->global.logical_minimum) {
237                 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
238                 return -1;
239         }
240
241         usages = parser->local.usage_index;
242
243         offset = report->size;
244         report->size += parser->global.report_size * parser->global.report_count;
245
246         if (usages < parser->global.report_count)
247                 usages = parser->global.report_count;
248
249         if (usages == 0)
250                 return 0; /* ignore padding fields */
251
252         if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
253                 return 0;
254
255         field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
256         field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
257         field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
258
259         for (i = 0; i < usages; i++) {
260                 int j = i;
261                 /* Duplicate the last usage we parsed if we have excess values */
262                 if (i >= parser->local.usage_index)
263                         j = parser->local.usage_index - 1;
264                 field->usage[i].hid = parser->local.usage[j];
265                 field->usage[i].collection_index =
266                         parser->local.collection_index[j];
267         }
268
269         field->maxusage = usages;
270         field->flags = flags;
271         field->report_offset = offset;
272         field->report_type = report_type;
273         field->report_size = parser->global.report_size;
274         field->report_count = parser->global.report_count;
275         field->logical_minimum = parser->global.logical_minimum;
276         field->logical_maximum = parser->global.logical_maximum;
277         field->physical_minimum = parser->global.physical_minimum;
278         field->physical_maximum = parser->global.physical_maximum;
279         field->unit_exponent = parser->global.unit_exponent;
280         field->unit = parser->global.unit;
281
282         return 0;
283 }
284
285 /*
286  * Read data value from item.
287  */
288
289 static __inline__ __u32 item_udata(struct hid_item *item)
290 {
291         switch (item->size) {
292                 case 1: return item->data.u8;
293                 case 2: return item->data.u16;
294                 case 4: return item->data.u32;
295         }
296         return 0;
297 }
298
299 static __inline__ __s32 item_sdata(struct hid_item *item)
300 {
301         switch (item->size) {
302                 case 1: return item->data.s8;
303                 case 2: return item->data.s16;
304                 case 4: return item->data.s32;
305         }
306         return 0;
307 }
308
309 /*
310  * Process a global item.
311  */
312
313 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
314 {
315         switch (item->tag) {
316
317                 case HID_GLOBAL_ITEM_TAG_PUSH:
318
319                         if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
320                                 dbg("global enviroment stack overflow");
321                                 return -1;
322                         }
323
324                         memcpy(parser->global_stack + parser->global_stack_ptr++,
325                                 &parser->global, sizeof(struct hid_global));
326                         return 0;
327
328                 case HID_GLOBAL_ITEM_TAG_POP:
329
330                         if (!parser->global_stack_ptr) {
331                                 dbg("global enviroment stack underflow");
332                                 return -1;
333                         }
334
335                         memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
336                                 sizeof(struct hid_global));
337                         return 0;
338
339                 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
340                         parser->global.usage_page = item_udata(item);
341                         return 0;
342
343                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
344                         parser->global.logical_minimum = item_sdata(item);
345                         return 0;
346
347                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
348                         if (parser->global.logical_minimum < 0)
349                                 parser->global.logical_maximum = item_sdata(item);
350                         else
351                                 parser->global.logical_maximum = item_udata(item);
352                         return 0;
353
354                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
355                         parser->global.physical_minimum = item_sdata(item);
356                         return 0;
357
358                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
359                         if (parser->global.physical_minimum < 0)
360                                 parser->global.physical_maximum = item_sdata(item);
361                         else
362                                 parser->global.physical_maximum = item_udata(item);
363                         return 0;
364
365                 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
366                         parser->global.unit_exponent = item_udata(item);
367                         return 0;
368
369                 case HID_GLOBAL_ITEM_TAG_UNIT:
370                         parser->global.unit = item_udata(item);
371                         return 0;
372
373                 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
374                         if ((parser->global.report_size = item_udata(item)) > 32) {
375                                 dbg("invalid report_size %d", parser->global.report_size);
376                                 return -1;
377                         }
378                         return 0;
379
380                 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
381                         if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
382                                 dbg("invalid report_count %d", parser->global.report_count);
383                                 return -1;
384                         }
385                         return 0;
386
387                 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
388                         if ((parser->global.report_id = item_udata(item)) == 0) {
389                                 dbg("report_id 0 is invalid");
390                                 return -1;
391                         }
392                         return 0;
393
394                 default:
395                         dbg("unknown global tag 0x%x", item->tag);
396                         return -1;
397         }
398 }
399
400 /*
401  * Process a local item.
402  */
403
404 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
405 {
406         __u32 data;
407         unsigned n;
408
409         if (item->size == 0) {
410                 dbg("item data expected for local item");
411                 return -1;
412         }
413
414         data = item_udata(item);
415
416         switch (item->tag) {
417
418                 case HID_LOCAL_ITEM_TAG_DELIMITER:
419
420                         if (data) {
421                                 /*
422                                  * We treat items before the first delimiter
423                                  * as global to all usage sets (branch 0).
424                                  * In the moment we process only these global
425                                  * items and the first delimiter set.
426                                  */
427                                 if (parser->local.delimiter_depth != 0) {
428                                         dbg("nested delimiters");
429                                         return -1;
430                                 }
431                                 parser->local.delimiter_depth++;
432                                 parser->local.delimiter_branch++;
433                         } else {
434                                 if (parser->local.delimiter_depth < 1) {
435                                         dbg("bogus close delimiter");
436                                         return -1;
437                                 }
438                                 parser->local.delimiter_depth--;
439                         }
440                         return 1;
441
442                 case HID_LOCAL_ITEM_TAG_USAGE:
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                         return hid_add_usage(parser, data);
453
454                 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
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                         parser->local.usage_minimum = data;
465                         return 0;
466
467                 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
468
469                         if (parser->local.delimiter_branch > 1) {
470                                 dbg("alternative usage ignored");
471                                 return 0;
472                         }
473
474                         if (item->size <= 2)
475                                 data = (parser->global.usage_page << 16) + data;
476
477                         for (n = parser->local.usage_minimum; n <= data; n++)
478                                 if (hid_add_usage(parser, n)) {
479                                         dbg("hid_add_usage failed\n");
480                                         return -1;
481                                 }
482                         return 0;
483
484                 default:
485
486                         dbg("unknown local item tag 0x%x", item->tag);
487                         return 0;
488         }
489         return 0;
490 }
491
492 /*
493  * Process a main item.
494  */
495
496 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
497 {
498         __u32 data;
499         int ret;
500
501         data = item_udata(item);
502
503         switch (item->tag) {
504                 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
505                         ret = open_collection(parser, data & 0xff);
506                         break;
507                 case HID_MAIN_ITEM_TAG_END_COLLECTION:
508                         ret = close_collection(parser);
509                         break;
510                 case HID_MAIN_ITEM_TAG_INPUT:
511                         ret = hid_add_field(parser, HID_INPUT_REPORT, data);
512                         break;
513                 case HID_MAIN_ITEM_TAG_OUTPUT:
514                         ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
515                         break;
516                 case HID_MAIN_ITEM_TAG_FEATURE:
517                         ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
518                         break;
519                 default:
520                         dbg("unknown main item tag 0x%x", item->tag);
521                         ret = 0;
522         }
523
524         memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
525
526         return ret;
527 }
528
529 /*
530  * Process a reserved item.
531  */
532
533 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
534 {
535         dbg("reserved item type, tag 0x%x", item->tag);
536         return 0;
537 }
538
539 /*
540  * Free a report and all registered fields. The field->usage and
541  * field->value table's are allocated behind the field, so we need
542  * only to free(field) itself.
543  */
544
545 static void hid_free_report(struct hid_report *report)
546 {
547         unsigned n;
548
549         for (n = 0; n < report->maxfield; n++)
550                 kfree(report->field[n]);
551         if (report->data)
552                 kfree(report->data);
553         kfree(report);
554 }
555
556 /*
557  * Free a device structure, all reports, and all fields.
558  */
559
560 static void hid_free_device(struct hid_device *device)
561 {
562         unsigned i,j;
563
564         for (i = 0; i < HID_REPORT_TYPES; i++) {
565                 struct hid_report_enum *report_enum = device->report_enum + i;
566
567                 for (j = 0; j < 256; j++) {
568                         struct hid_report *report = report_enum->report_id_hash[j];
569                         if (report) hid_free_report(report);
570                 }
571         }
572
573         if (device->rdesc) kfree(device->rdesc);
574         if (device->collection) kfree(device->collection);
575 }
576
577 /*
578  * Fetch a report description item from the data stream. We support long
579  * items, though they are not used yet.
580  */
581
582 static __u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
583 {
584         if ((end - start) > 0) {
585
586                 __u8 b = *start++;
587                 item->type = (b >> 2) & 3;
588                 item->tag  = (b >> 4) & 15;
589
590                 if (item->tag == HID_ITEM_TAG_LONG) {
591
592                         item->format = HID_ITEM_FORMAT_LONG;
593
594                         if ((end - start) >= 2) {
595
596                                 item->size = *start++;
597                                 item->tag  = *start++;
598
599                                 if ((end - start) >= item->size) {
600                                         item->data.longdata = start;
601                                         start += item->size;
602                                         return start;
603                                 }
604                         }
605                 } else {
606
607                         item->format = HID_ITEM_FORMAT_SHORT;
608                         item->size = b & 3;
609                         switch (item->size) {
610
611                                 case 0:
612                                         return start;
613
614                                 case 1:
615                                         if ((end - start) >= 1) {
616                                                 item->data.u8 = *start++;
617                                                 return start;
618                                         }
619                                         break;
620
621                                 case 2:
622                                         if ((end - start) >= 2) {
623                                                 item->data.u16 = le16_to_cpu(get_unaligned((__u16*)start));
624                                                 start = (__u8 *)((__u16 *)start + 1);
625                                                 return start;
626                                         }
627
628                                 case 3:
629                                         item->size++;
630                                         if ((end - start) >= 4) {
631                                                 item->data.u32 = le32_to_cpu(get_unaligned((__u32*)start));
632                                                 start = (__u8 *)((__u32 *)start + 1);
633                                                 return start;
634                                         }
635                         }
636                 }
637         }
638         return NULL;
639 }
640
641 /*
642  * Parse a report description into a hid_device structure. Reports are
643  * enumerated, fields are attached to these reports.
644  */
645
646 static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
647 {
648         struct hid_device *device;
649         struct hid_parser *parser;
650         struct hid_item item;
651         __u8 *end;
652         unsigned i;
653         static int (*dispatch_type[])(struct hid_parser *parser,
654                                       struct hid_item *item) = {
655                 hid_parser_main,
656                 hid_parser_global,
657                 hid_parser_local,
658                 hid_parser_reserved
659         };
660
661         if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
662                 return NULL;
663         memset(device, 0, sizeof(struct hid_device));
664
665         if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
666                                            HID_DEFAULT_NUM_COLLECTIONS,
667                                            GFP_KERNEL))) {
668                 kfree(device);
669                 return NULL;
670         }
671         memset(device->collection, 0, sizeof(struct hid_collection) *
672                HID_DEFAULT_NUM_COLLECTIONS);
673         device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
674
675         for (i = 0; i < HID_REPORT_TYPES; i++)
676                 INIT_LIST_HEAD(&device->report_enum[i].report_list);
677
678         if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
679                 kfree(device->collection);
680                 kfree(device);
681                 return NULL;
682         }
683         memcpy(device->rdesc, start, size);
684         device->rsize = size;
685
686         if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
687                 kfree(device->rdesc);
688                 kfree(device->collection);
689                 kfree(device);
690                 return NULL;
691         }
692         memset(parser, 0, sizeof(struct hid_parser));
693         parser->device = device;
694
695         end = start + size;
696         while ((start = fetch_item(start, end, &item)) != 0) {
697                 if (item.format != HID_ITEM_FORMAT_SHORT) {
698                         dbg("unexpected long global item");
699                         hid_free_device(device);
700                         kfree(parser);
701                         return NULL;
702                 }
703                 if (dispatch_type[item.type](parser, &item)) {
704                         dbg("item %u %u %u %u parsing failed\n",
705                                 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
706                         hid_free_device(device);
707                         kfree(parser);
708                         return NULL;
709                 }
710
711                 if (start == end) {
712                         if (parser->collection_stack_ptr) {
713                                 dbg("unbalanced collection at end of report description");
714                                 hid_free_device(device);
715                                 kfree(parser);
716                                 return NULL;
717                         }
718                         if (parser->local.delimiter_depth) {
719                                 dbg("unbalanced delimiter at end of report description");
720                                 hid_free_device(device);
721                                 kfree(parser);
722                                 return NULL;
723                         }
724                         kfree(parser);
725                         return device;
726                 }
727         }
728
729         dbg("item fetching failed at offset %d\n", (int)(end - start));
730         hid_free_device(device);
731         kfree(parser);
732         return NULL;
733 }
734
735 /*
736  * Convert a signed n-bit integer to signed 32-bit integer. Common
737  * cases are done through the compiler, the screwed things has to be
738  * done by hand.
739  */
740
741 static __inline__ __s32 snto32(__u32 value, unsigned n)
742 {
743         switch (n) {
744                 case 8:  return ((__s8)value);
745                 case 16: return ((__s16)value);
746                 case 32: return ((__s32)value);
747         }
748         return value & (1 << (n - 1)) ? value | (-1 << n) : value;
749 }
750
751 /*
752  * Convert a signed 32-bit integer to a signed n-bit integer.
753  */
754
755 static __inline__ __u32 s32ton(__s32 value, unsigned n)
756 {
757         __s32 a = value >> (n - 1);
758         if (a && a != -1) 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((__u64*)report)) >> offset) & ((1 << 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((__u64*)report)
776                 & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
777                 | cpu_to_le64((__u64)value << offset), (__u64*)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--) if (*array++ == value) return 0;
787         return -1;
788 }
789
790 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
791 {
792         hid_dump_input(usage, value);
793         if (hid->claimed & HID_CLAIMED_INPUT)
794                 hidinput_hid_event(hid, field, usage, value);
795         if (hid->claimed & HID_CLAIMED_HIDDEV)
796                 hiddev_hid_event(hid, field, usage, value);
797 }
798
799
800 /*
801  * Analyse a received field, and fetch the data from it. The field
802  * content is stored for next report processing (we do differential
803  * reporting to the layer).
804  */
805
806 static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data)
807 {
808         unsigned n;
809         unsigned count = field->report_count;
810         unsigned offset = field->report_offset;
811         unsigned size = field->report_size;
812         __s32 min = field->logical_minimum;
813         __s32 max = field->logical_maximum;
814         __s32 value[count]; /* WARNING: gcc specific */
815
816         for (n = 0; n < count; n++) {
817
818                         value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
819                                                     extract(data, offset + n * size, size);
820
821                         if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
822                             && value[n] >= min && value[n] <= max
823                             && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
824                                 return;
825         }
826
827         for (n = 0; n < count; n++) {
828
829                 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
830
831                         if (field->flags & HID_MAIN_ITEM_RELATIVE) {
832                                 if (!value[n]) continue;
833                         } else {
834                                 if (value[n] == field->value[n]) continue;
835                         }       
836                         hid_process_event(hid, field, &field->usage[n], value[n]);
837                         continue;
838                 }
839
840                 if (field->value[n] >= min && field->value[n] <= max
841                         && field->usage[field->value[n] - min].hid
842                         && search(value, field->value[n], count))
843                                 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0);
844
845                 if (value[n] >= min && value[n] <= max
846                         && field->usage[value[n] - min].hid
847                         && search(field->value, value[n], count))
848                                 hid_process_event(hid, field, &field->usage[value[n] - min], 1);
849         }
850
851         memcpy(field->value, value, count * sizeof(__s32));
852 }
853
854 static int hid_input_report(int type, u8 *data, int len, struct hid_device *hid)
855 {
856         struct hid_report_enum *report_enum = hid->report_enum + type;
857         struct hid_report *report;
858         int n, size;
859
860         if (!len) {
861                 dbg("empty report");
862                 return -1;
863         }
864
865 #ifdef DEBUG_DATA
866         printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
867 #endif
868
869         n = 0;                          /* Normally report number is 0 */
870         if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
871                 n = *data++;
872                 len--;
873         }
874
875         if (!(report = report_enum->report_id_hash[n])) {
876                 dbg("undefined report_id %d received", n);
877 #ifdef DEBUG
878                         printk(KERN_DEBUG __FILE__ ": report (size %u) = ", len);
879                         for (n = 0; n < len; n++)
880                                 printk(" %02x", data[n]);
881                         printk("\n");
882 #endif
883
884                 return -1;
885         }
886
887         if (hid->claimed & HID_CLAIMED_HIDDEV)
888                 hiddev_report_event(hid, report);
889
890         size = ((report->size - 1) >> 3) + 1;
891
892         if (len < size) {
893
894                 if (size <= 8) {
895                         dbg("report %d is too short, (%d < %d)", report->id, len, size);
896                         return -1;
897                 }
898
899                 /*
900                  * Some low-speed devices have large reports and maxpacketsize 8.
901                  * We buffer the data in that case and parse it when we got it all.
902                  * Works only for unnumbered reports. Doesn't make sense for numbered
903                  * reports anyway - then they don't need to be large.
904                  */
905
906                 if (!report->data)
907                         if (!(report->data = kmalloc(size, GFP_ATOMIC))) {
908                                 dbg("couldn't allocate report buffer");
909                                 return -1;
910                         }
911
912                 if (report->idx + len > size) {
913                         dbg("report data buffer overflow");
914                         report->idx = 0;
915                         return -1;
916                 }
917
918                 memcpy(report->data + report->idx, data, len);
919                 report->idx += len;
920
921                 if (report->idx < size)
922                         return 0;
923
924                 data = report->data;
925         }
926
927         for (n = 0; n < report->maxfield; n++)
928                 hid_input_field(hid, report->field[n], data);
929
930         report->idx = 0;
931         return 0;
932 }
933
934 /*
935  * Interrupt input handler.
936  */
937
938 static void hid_irq(struct urb *urb)
939 {
940         if (urb->status) {
941                 dbg("nonzero status in irq %d", urb->status);
942                 return;
943         }
944
945         hid_input_report(HID_INPUT_REPORT, urb->transfer_buffer, urb->actual_length, urb->context);
946 }
947
948 /*
949  * hid_read_report() reads in report values without waiting for an irq urb.
950  */
951
952 void hid_read_report(struct hid_device *hid, struct hid_report *report)
953 {
954         int len = ((report->size - 1) >> 3) + 1 + hid->report_enum[report->type].numbered;
955         u8 data[len];
956         int read;
957
958         if (hid->quirks & HID_QUIRK_NOGET)
959                 return;
960
961         if ((read = usb_get_report(hid->dev, hid->ifnum, report->type + 1, report->id, data, len)) != len) {
962                 dbg("reading report type %d id %d failed len %d read %d", report->type + 1, report->id, len, read);
963                 return;
964         }
965
966         hid_input_report(report->type, data, len, hid);
967 }
968
969 /*
970  * Output the field into the report.
971  */
972
973 static void hid_output_field(struct hid_field *field, __u8 *data)
974 {
975         unsigned count = field->report_count;
976         unsigned offset = field->report_offset;
977         unsigned size = field->report_size;
978         unsigned n;
979
980         for (n = 0; n < count; n++) {
981                 if (field->logical_minimum < 0) /* signed values */
982                         implement(data, offset + n * size, size, s32ton(field->value[n], size));
983                  else                           /* unsigned values */
984                         implement(data, offset + n * size, size, field->value[n]);
985         }
986 }
987
988 /*
989  * Create a report.
990  */
991
992 void hid_output_report(struct hid_report *report, __u8 *data)
993 {
994         unsigned n;
995         for (n = 0; n < report->maxfield; n++)
996                 hid_output_field(report->field[n], data);
997 }
998
999 /*
1000  * Set a field value. The report this field belongs to has to be
1001  * created and transfered to the device, to set this value in the
1002  * device.
1003  */
1004
1005 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1006 {
1007         unsigned size = field->report_size;
1008
1009         hid_dump_input(field->usage + offset, value);
1010
1011         if (offset >= field->report_count) {
1012                 dbg("offset exceeds report_count");
1013                 return -1;
1014         }
1015         if (field->logical_minimum < 0) {
1016                 if (value != snto32(s32ton(value, size), size)) {
1017                         dbg("value %d is out of range", value);
1018                         return -1;
1019                 }
1020         }
1021         if (   (value > field->logical_maximum)
1022             || (value < field->logical_minimum)) {
1023                 dbg("value %d is invalid", value);
1024                 return -1;
1025         }
1026         field->value[offset] = value;
1027         return 0;
1028 }
1029
1030 int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1031 {
1032         struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT;
1033         struct list_head *list = report_enum->report_list.next;
1034         int i, j;
1035
1036         while (list != &report_enum->report_list) {
1037                 struct hid_report *report = (struct hid_report *) list;
1038                 list = list->next;
1039                 for (i = 0; i < report->maxfield; i++) {
1040                         *field = report->field[i];
1041                         for (j = 0; j < (*field)->maxusage; j++)
1042                                 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1043                                         return j;
1044                 }
1045         }
1046         return -1;
1047 }
1048
1049 static int hid_submit_out(struct hid_device *hid)
1050 {
1051         hid->urbout.transfer_buffer_length = le16_to_cpup(&hid->out[hid->outtail].dr.wLength);
1052         hid->urbout.transfer_buffer = hid->out[hid->outtail].buffer;
1053         hid->urbout.setup_packet = (void *) &(hid->out[hid->outtail].dr);
1054         hid->urbout.dev = hid->dev;
1055
1056         if (usb_submit_urb(&hid->urbout)) {
1057                 err("usb_submit_urb(out) failed");
1058                 return -1;
1059         }
1060
1061         return 0;
1062 }
1063
1064 static void hid_ctrl(struct urb *urb)
1065 {
1066         struct hid_device *hid = urb->context;
1067         unsigned long flags;
1068
1069         if (urb->status)
1070                 warn("ctrl urb status %d received", urb->status);
1071
1072         spin_lock_irqsave(&hid->outlock, flags);
1073
1074         hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1075
1076         if (hid->outhead != hid->outtail) {
1077                 if (hid_submit_out(hid)) {
1078                         clear_bit(HID_OUT_RUNNING, &hid->iofl);
1079                 }
1080                 spin_unlock_irqrestore(&hid->outlock, flags);
1081                 return;
1082         }
1083
1084         clear_bit(HID_OUT_RUNNING, &hid->iofl);
1085         spin_unlock_irqrestore(&hid->outlock, flags);
1086 }
1087
1088 void hid_write_report(struct hid_device *hid, struct hid_report *report)
1089 {
1090         unsigned long flags;
1091
1092         if (hid->report_enum[report->type].numbered) {
1093                 hid->out[hid->outhead].buffer[0] = report->id;
1094                 hid_output_report(report, hid->out[hid->outhead].buffer + 1);
1095                 hid->out[hid->outhead].dr.wLength = cpu_to_le16(((report->size + 7) >> 3) + 1);
1096         } else {
1097                 hid_output_report(report, hid->out[hid->outhead].buffer);
1098                 hid->out[hid->outhead].dr.wLength = cpu_to_le16((report->size + 7) >> 3);
1099         }
1100
1101         hid->out[hid->outhead].dr.wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1102
1103         spin_lock_irqsave(&hid->outlock, flags);
1104
1105         hid->outhead = (hid->outhead + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1106
1107         if (hid->outhead == hid->outtail)
1108                 hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1109
1110         if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
1111                 if (hid_submit_out(hid))
1112                         clear_bit(HID_OUT_RUNNING, &hid->iofl);
1113
1114         spin_unlock_irqrestore(&hid->outlock, flags);
1115 }
1116
1117 int hid_open(struct hid_device *hid)
1118 {
1119         if (hid->open++)
1120                 return 0;
1121
1122         hid->urb.dev = hid->dev;
1123
1124         if (usb_submit_urb(&hid->urb))
1125                 return -EIO;
1126
1127         return 0;
1128 }
1129
1130 void hid_close(struct hid_device *hid)
1131 {
1132         if (!--hid->open)
1133                 usb_unlink_urb(&hid->urb);
1134 }
1135
1136 /*
1137  * Initialize all readable reports
1138  */
1139 void hid_init_reports(struct hid_device *hid)
1140 {
1141         int i;
1142         struct hid_report *report;
1143         struct hid_report_enum *report_enum;
1144         struct list_head *list;
1145
1146         for (i = 0; i < HID_REPORT_TYPES; i++) {
1147                 if (i == HID_FEATURE_REPORT || i == HID_INPUT_REPORT) {
1148                         report_enum = hid->report_enum + i;
1149                         list = report_enum->report_list.next;
1150                         while (list != &report_enum->report_list) {
1151                                 report = (struct hid_report *) list;
1152                                 hid_read_report(hid, report);
1153                                 usb_set_idle(hid->dev, hid->ifnum, 0, report->id);
1154                                 list = list->next;
1155                         }
1156                 }
1157         }
1158 }
1159
1160 #define USB_VENDOR_ID_WACOM             0x056a
1161 #define USB_DEVICE_ID_WACOM_PENPARTNER  0x0000
1162 #define USB_DEVICE_ID_WACOM_GRAPHIRE    0x0010
1163 #define USB_DEVICE_ID_WACOM_INTUOS      0x0020
1164 #define USB_DEVICE_ID_WACOM_PL          0x0030
1165 #define USB_DEVICE_ID_WACOM_INTUOS2     0x0041
1166
1167 #define USB_VENDOR_ID_KBGEAR            0x084e
1168 #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO  0x1001
1169
1170 #define USB_VENDOR_ID_AIPTEK            0x08ca
1171 #define USB_DEVICE_ID_AIPTEK_01 0x0001
1172 #define USB_DEVICE_ID_AIPTEK_10 0x0010
1173 #define USB_DEVICE_ID_AIPTEK_20 0x0020
1174 #define USB_DEVICE_ID_AIPTEK_21 0x0021
1175 #define USB_DEVICE_ID_AIPTEK_22 0x0022
1176 #define USB_DEVICE_ID_AIPTEK_23 0x0023
1177 #define USB_DEVICE_ID_AIPTEK_24 0x0024
1178
1179 #define USB_VENDOR_ID_ATEN              0x0557
1180 #define USB_DEVICE_ID_ATEN_UC100KM      0x2004
1181 #define USB_DEVICE_ID_ATEN_CS124U       0x2202
1182 #define USB_DEVICE_ID_ATEN_2PORTKVM     0x2204
1183 #define USB_DEVICE_ID_ATEN_4PORTKVM     0x2205
1184
1185 #define USB_VENDOR_ID_TOPMAX            0x0663
1186 #define USB_DEVICE_ID_TOPMAX_COBRAPAD   0x0103
1187
1188 #define USB_VENDOR_ID_HAPP              0x078b
1189 #define USB_DEVICE_ID_UGCI_DRIVING      0x0010
1190 #define USB_DEVICE_ID_UGCI_FLYING       0x0020
1191 #define USB_DEVICE_ID_UGCI_FIGHTING     0x0030
1192
1193 #define USB_VENDOR_ID_GRIFFIN           0x077d
1194 #define USB_DEVICE_ID_POWERMATE         0x0410 /* Griffin PowerMate */
1195 #define USB_DEVICE_ID_SOUNDKNOB         0x04AA /* Griffin SoundKnob */
1196
1197 #define USB_VENDOR_ID_ONTRAK    0x0a07
1198 #define USB_DEVICE_ID_ONTRAK_ADU100     0x0064
1199
1200 #define USB_VENDOR_ID_TANGTOP           0x0d3d
1201 #define USB_DEVICE_ID_TANGTOP_USBPS2    0x0001
1202
1203 #define USB_VENDOR_ID_OKI               0x070a
1204 #define USB_VENDOR_ID_OKI_MULITI        0x0007
1205
1206 #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1207 #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5      0x0100
1208
1209 #define USB_VENDOR_ID_MGE               0x0463
1210 #define USB_DEVICE_ID_MGE_UPS           0xffff
1211 #define USB_DEVICE_ID_MGE_UPS1          0x0001
1212
1213 #define USB_VENDOR_ID_NEC               0x073e
1214 #define USB_DEVICE_ID_NEC_USB_GAME_PAD  0x0301
1215
1216 struct hid_blacklist {
1217         __u16 idVendor;
1218         __u16 idProduct;
1219         unsigned quirks;
1220 } hid_blacklist[] = {
1221         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1222         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1223         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1224         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1225         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1226         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1227         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1228         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1229         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1230         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1231         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1232         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1233         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1234         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1235         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1236         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2, HID_QUIRK_IGNORE },
1237         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1238         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1239         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1240         { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1241         { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1242         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1243         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1244         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1245         { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1246         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1247         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1248         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1249         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1250         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1251         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1252         { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1253         { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1254         { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1255         { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1256         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1257         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1258         { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1259         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1260         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1261         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1262         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1263         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1264         { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1265         { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1266         { USB_VENDOR_ID_OKI, USB_VENDOR_ID_OKI_MULITI, HID_QUIRK_NOGET },
1267         { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1268         { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1269         { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1270         { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1271         { 0, 0 }
1272 };
1273
1274 static struct hid_device *usb_hid_configure(struct usb_device *dev, int ifnum)
1275 {
1276         struct usb_interface_descriptor *interface = dev->actconfig->interface[ifnum].altsetting + 0;
1277         struct hid_descriptor *hdesc;
1278         struct hid_device *hid;
1279         unsigned quirks = 0, rsize = 0;
1280         char *buf;
1281         int n;
1282
1283         for (n = 0; hid_blacklist[n].idVendor; n++)
1284                 if ((hid_blacklist[n].idVendor == dev->descriptor.idVendor) &&
1285                         (hid_blacklist[n].idProduct == dev->descriptor.idProduct))
1286                                 quirks = hid_blacklist[n].quirks;
1287
1288         if (quirks & HID_QUIRK_IGNORE)
1289                 return NULL;
1290
1291         if (usb_get_extra_descriptor(interface, USB_DT_HID, &hdesc) && ((!interface->bNumEndpoints) ||
1292                 usb_get_extra_descriptor(&interface->endpoint[0], USB_DT_HID, &hdesc))) {
1293                         dbg("class descriptor not present\n");
1294                         return NULL;
1295         }
1296
1297         for (n = 0; n < hdesc->bNumDescriptors; n++)
1298                 if (hdesc->desc[n].bDescriptorType == USB_DT_REPORT)
1299                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1300
1301         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1302                 dbg("weird size of report descriptor (%u)", rsize);
1303                 return NULL;
1304         }
1305
1306         {
1307                 __u8 rdesc[rsize];
1308
1309                 if ((n = usb_get_class_descriptor(dev, interface->bInterfaceNumber, USB_DT_REPORT, 0, rdesc, rsize)) < 0) {
1310                         dbg("reading report descriptor failed");
1311                         return NULL;
1312                 }
1313
1314 #ifdef DEBUG_DATA
1315                 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1316                 for (n = 0; n < rsize; n++)
1317                         printk(" %02x", (unsigned) rdesc[n]);
1318                 printk("\n");
1319 #endif
1320
1321                 if (!(hid = hid_parse_report(rdesc, rsize))) {
1322                         dbg("parsing report descriptor failed");
1323                         return NULL;
1324                 }
1325         }
1326
1327         hid->quirks = quirks;
1328
1329         for (n = 0; n < interface->bNumEndpoints; n++) {
1330
1331                 struct usb_endpoint_descriptor *endpoint = &interface->endpoint[n];
1332                 int pipe, maxp, interval;
1333
1334                 if ((endpoint->bmAttributes & 3) != 3)          /* Not an interrupt endpoint */
1335                         continue;
1336
1337                 if (!(endpoint->bEndpointAddress & 0x80))       /* Not an input endpoint */
1338                         continue;
1339
1340                 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1341                 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1342                 interval = endpoint->bInterval;
1343                 if (dev->speed == USB_SPEED_HIGH)
1344                         interval = 1 << (interval - 1);
1345
1346                 FILL_INT_URB(&hid->urb, dev, pipe, hid->buffer, maxp > 32 ? 32 : maxp, hid_irq, hid, interval);
1347
1348                 break;
1349         }
1350
1351         if (n == interface->bNumEndpoints) {
1352                 dbg("couldn't find an input interrupt endpoint");
1353                 hid_free_device(hid);
1354                 return NULL;
1355         }
1356
1357         spin_lock_init(&hid->outlock);
1358
1359         hid->version = hdesc->bcdHID;
1360         hid->country = hdesc->bCountryCode;
1361         hid->dev = dev;
1362         hid->ifnum = interface->bInterfaceNumber;
1363
1364         for (n = 0; n < HID_CONTROL_FIFO_SIZE; n++) {
1365                 hid->out[n].dr.bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1366                 hid->out[n].dr.bRequest = USB_REQ_SET_REPORT;
1367                 hid->out[n].dr.wIndex = cpu_to_le16(hid->ifnum);
1368         }
1369
1370         hid->name[0] = 0;
1371
1372         if (!(buf = kmalloc(63, GFP_KERNEL)))
1373                 return NULL;
1374
1375         if (usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) {
1376                 strcat(hid->name, buf);
1377                 if (usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
1378                         sprintf(hid->name, "%s %s", hid->name, buf);
1379         } else
1380                 sprintf(hid->name, "%04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
1381
1382         kfree(buf);
1383
1384         FILL_CONTROL_URB(&hid->urbout, dev, usb_sndctrlpipe(dev, 0),
1385                 (void*) &hid->out[0].dr, hid->out[0].buffer, 1, hid_ctrl, hid);
1386
1387 /*
1388  * Some devices don't like this and crash. I don't know of any devices
1389  * needing this, so it is disabled for now.
1390  */
1391
1392 #if 0
1393         if (interface->bInterfaceSubClass == 1)
1394                 usb_set_protocol(dev, hid->ifnum, 1);
1395 #endif
1396
1397         return hid;
1398 }
1399
1400 static void* hid_probe(struct usb_device *dev, unsigned int ifnum,
1401                        const struct usb_device_id *id)
1402 {
1403         struct hid_device *hid;
1404         int i;
1405         char *c;
1406
1407         dbg("HID probe called for ifnum %d", ifnum);
1408
1409         if (!(hid = usb_hid_configure(dev, ifnum)))
1410                 return NULL;
1411
1412         hid_init_reports(hid);
1413         hid_dump_device(hid);
1414
1415         if (!hidinput_connect(hid))
1416                 hid->claimed |= HID_CLAIMED_INPUT;
1417         if (!hiddev_connect(hid))
1418                 hid->claimed |= HID_CLAIMED_HIDDEV;
1419         printk(KERN_INFO);
1420
1421         if (hid->claimed & HID_CLAIMED_INPUT)
1422                 printk("input");
1423         if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1424                 printk(",");
1425         if (hid->claimed & HID_CLAIMED_HIDDEV)
1426                 printk("hiddev%d", hid->minor);
1427
1428         c = "Device";
1429         for (i = 0; i < hid->maxcollection; i++) {
1430                 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1431                    (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1432                    (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1433                         c = hid_types[hid->collection[i].usage & 0xffff];
1434                         break;
1435                 }
1436         }
1437
1438         printk(": USB HID v%x.%02x %s [%s] on usb%d:%d.%d\n",
1439                 hid->version >> 8, hid->version & 0xff, c, hid->name,
1440                 dev->bus->busnum, dev->devnum, ifnum);
1441
1442         return hid;
1443 }
1444
1445 static void hid_disconnect(struct usb_device *dev, void *ptr)
1446 {
1447         struct hid_device *hid = ptr;
1448
1449         dbg("cleanup called");
1450         usb_unlink_urb(&hid->urb);
1451         if (hid->claimed & HID_CLAIMED_INPUT)
1452                 hidinput_disconnect(hid);
1453         if (hid->claimed & HID_CLAIMED_HIDDEV)
1454                 hiddev_disconnect(hid);
1455         hid_free_device(hid);
1456 }
1457
1458 static struct usb_device_id hid_usb_ids [] = {
1459         { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
1460             bInterfaceClass: USB_INTERFACE_CLASS_HID },
1461         { }                                             /* Terminating entry */
1462 };
1463
1464 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1465
1466 static struct usb_driver hid_driver = {
1467         name:           "hid",
1468         probe:          hid_probe,
1469         disconnect:     hid_disconnect,
1470         id_table:       hid_usb_ids,
1471 };
1472
1473 static int __init hid_init(void)
1474 {
1475         hiddev_init();
1476         usb_register(&hid_driver);
1477         info(DRIVER_VERSION " " DRIVER_AUTHOR);
1478         info(DRIVER_DESC);
1479
1480         return 0;
1481 }
1482
1483 static void __exit hid_exit(void)
1484 {
1485         usb_deregister(&hid_driver);
1486         hiddev_exit();
1487 }
1488
1489 module_init(hid_init);
1490 module_exit(hid_exit);
1491
1492 MODULE_AUTHOR( DRIVER_AUTHOR );
1493 MODULE_DESCRIPTION( DRIVER_DESC );
1494 MODULE_LICENSE("GPL");