Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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