HID: add compat support
[safe/jmp/linux-2.6] / drivers / hid / hid-core.c
1 /*
2  *  HID support for Linux
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7  *  Copyright (c) 2006-2007 Jiri Kosina
8  */
9
10 /*
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  */
16
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/mm.h>
23 #include <linux/spinlock.h>
24 #include <asm/unaligned.h>
25 #include <asm/byteorder.h>
26 #include <linux/input.h>
27 #include <linux/wait.h>
28 #include <linux/vmalloc.h>
29 #include <linux/sched.h>
30
31 #include <linux/hid.h>
32 #include <linux/hiddev.h>
33 #include <linux/hid-debug.h>
34 #include <linux/hidraw.h>
35
36 #include "hid-ids.h"
37
38 /*
39  * Version Information
40  */
41
42 #define DRIVER_VERSION "v2.6"
43 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
44 #define DRIVER_DESC "HID core driver"
45 #define DRIVER_LICENSE "GPL"
46
47 #ifdef CONFIG_HID_DEBUG
48 int hid_debug = 0;
49 module_param_named(debug, hid_debug, int, 0600);
50 MODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
51 EXPORT_SYMBOL_GPL(hid_debug);
52 #endif
53
54 /*
55  * Register a new report for a device.
56  */
57
58 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
59 {
60         struct hid_report_enum *report_enum = device->report_enum + type;
61         struct hid_report *report;
62
63         if (report_enum->report_id_hash[id])
64                 return report_enum->report_id_hash[id];
65
66         if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
67                 return NULL;
68
69         if (id != 0)
70                 report_enum->numbered = 1;
71
72         report->id = id;
73         report->type = type;
74         report->size = 0;
75         report->device = device;
76         report_enum->report_id_hash[id] = report;
77
78         list_add_tail(&report->list, &report_enum->report_list);
79
80         return report;
81 }
82
83 /*
84  * Register a new field for this report.
85  */
86
87 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
88 {
89         struct hid_field *field;
90
91         if (report->maxfield == HID_MAX_FIELDS) {
92                 dbg_hid("too many fields in report\n");
93                 return NULL;
94         }
95
96         if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97                 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
98
99         field->index = report->maxfield++;
100         report->field[field->index] = field;
101         field->usage = (struct hid_usage *)(field + 1);
102         field->value = (s32 *)(field->usage + usages);
103         field->report = report;
104
105         return field;
106 }
107
108 /*
109  * Open a collection. The type/usage is pushed on the stack.
110  */
111
112 static int open_collection(struct hid_parser *parser, unsigned type)
113 {
114         struct hid_collection *collection;
115         unsigned usage;
116
117         usage = parser->local.usage[0];
118
119         if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
120                 dbg_hid("collection stack overflow\n");
121                 return -1;
122         }
123
124         if (parser->device->maxcollection == parser->device->collection_size) {
125                 collection = kmalloc(sizeof(struct hid_collection) *
126                                 parser->device->collection_size * 2, GFP_KERNEL);
127                 if (collection == NULL) {
128                         dbg_hid("failed to reallocate collection array\n");
129                         return -1;
130                 }
131                 memcpy(collection, parser->device->collection,
132                         sizeof(struct hid_collection) *
133                         parser->device->collection_size);
134                 memset(collection + parser->device->collection_size, 0,
135                         sizeof(struct hid_collection) *
136                         parser->device->collection_size);
137                 kfree(parser->device->collection);
138                 parser->device->collection = collection;
139                 parser->device->collection_size *= 2;
140         }
141
142         parser->collection_stack[parser->collection_stack_ptr++] =
143                 parser->device->maxcollection;
144
145         collection = parser->device->collection +
146                 parser->device->maxcollection++;
147         collection->type = type;
148         collection->usage = usage;
149         collection->level = parser->collection_stack_ptr - 1;
150
151         if (type == HID_COLLECTION_APPLICATION)
152                 parser->device->maxapplication++;
153
154         return 0;
155 }
156
157 /*
158  * Close a collection.
159  */
160
161 static int close_collection(struct hid_parser *parser)
162 {
163         if (!parser->collection_stack_ptr) {
164                 dbg_hid("collection stack underflow\n");
165                 return -1;
166         }
167         parser->collection_stack_ptr--;
168         return 0;
169 }
170
171 /*
172  * Climb up the stack, search for the specified collection type
173  * and return the usage.
174  */
175
176 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
177 {
178         int n;
179         for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
180                 if (parser->device->collection[parser->collection_stack[n]].type == type)
181                         return parser->device->collection[parser->collection_stack[n]].usage;
182         return 0; /* we know nothing about this usage type */
183 }
184
185 /*
186  * Add a usage to the temporary parser table.
187  */
188
189 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
190 {
191         if (parser->local.usage_index >= HID_MAX_USAGES) {
192                 dbg_hid("usage index exceeded\n");
193                 return -1;
194         }
195         parser->local.usage[parser->local.usage_index] = usage;
196         parser->local.collection_index[parser->local.usage_index] =
197                 parser->collection_stack_ptr ?
198                 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
199         parser->local.usage_index++;
200         return 0;
201 }
202
203 /*
204  * Register a new field for this report.
205  */
206
207 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
208 {
209         struct hid_report *report;
210         struct hid_field *field;
211         int usages;
212         unsigned offset;
213         int i;
214
215         if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
216                 dbg_hid("hid_register_report failed\n");
217                 return -1;
218         }
219
220         if (parser->global.logical_maximum < parser->global.logical_minimum) {
221                 dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
222                 return -1;
223         }
224
225         offset = report->size;
226         report->size += parser->global.report_size * parser->global.report_count;
227
228         if (!parser->local.usage_index) /* Ignore padding fields */
229                 return 0;
230
231         usages = max_t(int, parser->local.usage_index, parser->global.report_count);
232
233         if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
234                 return 0;
235
236         field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
237         field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
238         field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
239
240         for (i = 0; i < usages; i++) {
241                 int j = i;
242                 /* Duplicate the last usage we parsed if we have excess values */
243                 if (i >= parser->local.usage_index)
244                         j = parser->local.usage_index - 1;
245                 field->usage[i].hid = parser->local.usage[j];
246                 field->usage[i].collection_index =
247                         parser->local.collection_index[j];
248         }
249
250         field->maxusage = usages;
251         field->flags = flags;
252         field->report_offset = offset;
253         field->report_type = report_type;
254         field->report_size = parser->global.report_size;
255         field->report_count = parser->global.report_count;
256         field->logical_minimum = parser->global.logical_minimum;
257         field->logical_maximum = parser->global.logical_maximum;
258         field->physical_minimum = parser->global.physical_minimum;
259         field->physical_maximum = parser->global.physical_maximum;
260         field->unit_exponent = parser->global.unit_exponent;
261         field->unit = parser->global.unit;
262
263         return 0;
264 }
265
266 /*
267  * Read data value from item.
268  */
269
270 static u32 item_udata(struct hid_item *item)
271 {
272         switch (item->size) {
273                 case 1: return item->data.u8;
274                 case 2: return item->data.u16;
275                 case 4: return item->data.u32;
276         }
277         return 0;
278 }
279
280 static s32 item_sdata(struct hid_item *item)
281 {
282         switch (item->size) {
283                 case 1: return item->data.s8;
284                 case 2: return item->data.s16;
285                 case 4: return item->data.s32;
286         }
287         return 0;
288 }
289
290 /*
291  * Process a global item.
292  */
293
294 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
295 {
296         switch (item->tag) {
297
298                 case HID_GLOBAL_ITEM_TAG_PUSH:
299
300                         if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
301                                 dbg_hid("global enviroment stack overflow\n");
302                                 return -1;
303                         }
304
305                         memcpy(parser->global_stack + parser->global_stack_ptr++,
306                                 &parser->global, sizeof(struct hid_global));
307                         return 0;
308
309                 case HID_GLOBAL_ITEM_TAG_POP:
310
311                         if (!parser->global_stack_ptr) {
312                                 dbg_hid("global enviroment stack underflow\n");
313                                 return -1;
314                         }
315
316                         memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
317                                 sizeof(struct hid_global));
318                         return 0;
319
320                 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
321                         parser->global.usage_page = item_udata(item);
322                         return 0;
323
324                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
325                         parser->global.logical_minimum = item_sdata(item);
326                         return 0;
327
328                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
329                         if (parser->global.logical_minimum < 0)
330                                 parser->global.logical_maximum = item_sdata(item);
331                         else
332                                 parser->global.logical_maximum = item_udata(item);
333                         return 0;
334
335                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
336                         parser->global.physical_minimum = item_sdata(item);
337                         return 0;
338
339                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
340                         if (parser->global.physical_minimum < 0)
341                                 parser->global.physical_maximum = item_sdata(item);
342                         else
343                                 parser->global.physical_maximum = item_udata(item);
344                         return 0;
345
346                 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
347                         parser->global.unit_exponent = item_sdata(item);
348                         return 0;
349
350                 case HID_GLOBAL_ITEM_TAG_UNIT:
351                         parser->global.unit = item_udata(item);
352                         return 0;
353
354                 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
355                         if ((parser->global.report_size = item_udata(item)) > 32) {
356                                 dbg_hid("invalid report_size %d\n", parser->global.report_size);
357                                 return -1;
358                         }
359                         return 0;
360
361                 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
362                         if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
363                                 dbg_hid("invalid report_count %d\n", parser->global.report_count);
364                                 return -1;
365                         }
366                         return 0;
367
368                 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
369                         if ((parser->global.report_id = item_udata(item)) == 0) {
370                                 dbg_hid("report_id 0 is invalid\n");
371                                 return -1;
372                         }
373                         return 0;
374
375                 default:
376                         dbg_hid("unknown global tag 0x%x\n", item->tag);
377                         return -1;
378         }
379 }
380
381 /*
382  * Process a local item.
383  */
384
385 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
386 {
387         __u32 data;
388         unsigned n;
389
390         if (item->size == 0) {
391                 dbg_hid("item data expected for local item\n");
392                 return -1;
393         }
394
395         data = item_udata(item);
396
397         switch (item->tag) {
398
399                 case HID_LOCAL_ITEM_TAG_DELIMITER:
400
401                         if (data) {
402                                 /*
403                                  * We treat items before the first delimiter
404                                  * as global to all usage sets (branch 0).
405                                  * In the moment we process only these global
406                                  * items and the first delimiter set.
407                                  */
408                                 if (parser->local.delimiter_depth != 0) {
409                                         dbg_hid("nested delimiters\n");
410                                         return -1;
411                                 }
412                                 parser->local.delimiter_depth++;
413                                 parser->local.delimiter_branch++;
414                         } else {
415                                 if (parser->local.delimiter_depth < 1) {
416                                         dbg_hid("bogus close delimiter\n");
417                                         return -1;
418                                 }
419                                 parser->local.delimiter_depth--;
420                         }
421                         return 1;
422
423                 case HID_LOCAL_ITEM_TAG_USAGE:
424
425                         if (parser->local.delimiter_branch > 1) {
426                                 dbg_hid("alternative usage ignored\n");
427                                 return 0;
428                         }
429
430                         if (item->size <= 2)
431                                 data = (parser->global.usage_page << 16) + data;
432
433                         return hid_add_usage(parser, data);
434
435                 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
436
437                         if (parser->local.delimiter_branch > 1) {
438                                 dbg_hid("alternative usage ignored\n");
439                                 return 0;
440                         }
441
442                         if (item->size <= 2)
443                                 data = (parser->global.usage_page << 16) + data;
444
445                         parser->local.usage_minimum = data;
446                         return 0;
447
448                 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
449
450                         if (parser->local.delimiter_branch > 1) {
451                                 dbg_hid("alternative usage ignored\n");
452                                 return 0;
453                         }
454
455                         if (item->size <= 2)
456                                 data = (parser->global.usage_page << 16) + data;
457
458                         for (n = parser->local.usage_minimum; n <= data; n++)
459                                 if (hid_add_usage(parser, n)) {
460                                         dbg_hid("hid_add_usage failed\n");
461                                         return -1;
462                                 }
463                         return 0;
464
465                 default:
466
467                         dbg_hid("unknown local item tag 0x%x\n", item->tag);
468                         return 0;
469         }
470         return 0;
471 }
472
473 /*
474  * Process a main item.
475  */
476
477 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
478 {
479         __u32 data;
480         int ret;
481
482         data = item_udata(item);
483
484         switch (item->tag) {
485                 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
486                         ret = open_collection(parser, data & 0xff);
487                         break;
488                 case HID_MAIN_ITEM_TAG_END_COLLECTION:
489                         ret = close_collection(parser);
490                         break;
491                 case HID_MAIN_ITEM_TAG_INPUT:
492                         ret = hid_add_field(parser, HID_INPUT_REPORT, data);
493                         break;
494                 case HID_MAIN_ITEM_TAG_OUTPUT:
495                         ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
496                         break;
497                 case HID_MAIN_ITEM_TAG_FEATURE:
498                         ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
499                         break;
500                 default:
501                         dbg_hid("unknown main item tag 0x%x\n", item->tag);
502                         ret = 0;
503         }
504
505         memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
506
507         return ret;
508 }
509
510 /*
511  * Process a reserved item.
512  */
513
514 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
515 {
516         dbg_hid("reserved item type, tag 0x%x\n", item->tag);
517         return 0;
518 }
519
520 /*
521  * Free a report and all registered fields. The field->usage and
522  * field->value table's are allocated behind the field, so we need
523  * only to free(field) itself.
524  */
525
526 static void hid_free_report(struct hid_report *report)
527 {
528         unsigned n;
529
530         for (n = 0; n < report->maxfield; n++)
531                 kfree(report->field[n]);
532         kfree(report);
533 }
534
535 /*
536  * Free a device structure, all reports, and all fields.
537  */
538
539 static void hid_device_release(struct device *dev)
540 {
541         struct hid_device *device = container_of(dev, struct hid_device, dev);
542         unsigned i, j;
543
544         for (i = 0; i < HID_REPORT_TYPES; i++) {
545                 struct hid_report_enum *report_enum = device->report_enum + i;
546
547                 for (j = 0; j < 256; j++) {
548                         struct hid_report *report = report_enum->report_id_hash[j];
549                         if (report)
550                                 hid_free_report(report);
551                 }
552         }
553
554         kfree(device->rdesc);
555         kfree(device->collection);
556         kfree(device);
557 }
558
559 /*
560  * Fetch a report description item from the data stream. We support long
561  * items, though they are not used yet.
562  */
563
564 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
565 {
566         u8 b;
567
568         if ((end - start) <= 0)
569                 return NULL;
570
571         b = *start++;
572
573         item->type = (b >> 2) & 3;
574         item->tag  = (b >> 4) & 15;
575
576         if (item->tag == HID_ITEM_TAG_LONG) {
577
578                 item->format = HID_ITEM_FORMAT_LONG;
579
580                 if ((end - start) < 2)
581                         return NULL;
582
583                 item->size = *start++;
584                 item->tag  = *start++;
585
586                 if ((end - start) < item->size)
587                         return NULL;
588
589                 item->data.longdata = start;
590                 start += item->size;
591                 return start;
592         }
593
594         item->format = HID_ITEM_FORMAT_SHORT;
595         item->size = b & 3;
596
597         switch (item->size) {
598
599                 case 0:
600                         return start;
601
602                 case 1:
603                         if ((end - start) < 1)
604                                 return NULL;
605                         item->data.u8 = *start++;
606                         return start;
607
608                 case 2:
609                         if ((end - start) < 2)
610                                 return NULL;
611                         item->data.u16 = get_unaligned_le16(start);
612                         start = (__u8 *)((__le16 *)start + 1);
613                         return start;
614
615                 case 3:
616                         item->size++;
617                         if ((end - start) < 4)
618                                 return NULL;
619                         item->data.u32 = get_unaligned_le32(start);
620                         start = (__u8 *)((__le32 *)start + 1);
621                         return start;
622         }
623
624         return NULL;
625 }
626
627 /**
628  * hid_parse_report - parse device report
629  *
630  * @device: hid device
631  * @start: report start
632  * @size: report size
633  *
634  * Parse a report description into a hid_device structure. Reports are
635  * enumerated, fields are attached to these reports.
636  * 0 returned on success, otherwise nonzero error value.
637  */
638 int hid_parse_report(struct hid_device *device, __u8 *start,
639                 unsigned size)
640 {
641         struct hid_parser *parser;
642         struct hid_item item;
643         __u8 *end;
644         int ret;
645         static int (*dispatch_type[])(struct hid_parser *parser,
646                                       struct hid_item *item) = {
647                 hid_parser_main,
648                 hid_parser_global,
649                 hid_parser_local,
650                 hid_parser_reserved
651         };
652
653         if (device->driver->report_fixup)
654                 device->driver->report_fixup(device, start, size);
655
656         device->rdesc = kmalloc(size, GFP_KERNEL);
657         if (device->rdesc == NULL)
658                 return -ENOMEM;
659         memcpy(device->rdesc, start, size);
660         device->rsize = size;
661
662         parser = vmalloc(sizeof(struct hid_parser));
663         if (!parser) {
664                 ret = -ENOMEM;
665                 goto err;
666         }
667
668         memset(parser, 0, sizeof(struct hid_parser));
669         parser->device = device;
670
671         end = start + size;
672         ret = -EINVAL;
673         while ((start = fetch_item(start, end, &item)) != NULL) {
674
675                 if (item.format != HID_ITEM_FORMAT_SHORT) {
676                         dbg_hid("unexpected long global item\n");
677                         goto err;
678                 }
679
680                 if (dispatch_type[item.type](parser, &item)) {
681                         dbg_hid("item %u %u %u %u parsing failed\n",
682                                 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
683                         goto err;
684                 }
685
686                 if (start == end) {
687                         if (parser->collection_stack_ptr) {
688                                 dbg_hid("unbalanced collection at end of report description\n");
689                                 goto err;
690                         }
691                         if (parser->local.delimiter_depth) {
692                                 dbg_hid("unbalanced delimiter at end of report description\n");
693                                 goto err;
694                         }
695                         vfree(parser);
696                         return 0;
697                 }
698         }
699
700         dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
701 err:
702         vfree(parser);
703         return ret;
704 }
705 EXPORT_SYMBOL_GPL(hid_parse_report);
706
707 /*
708  * Convert a signed n-bit integer to signed 32-bit integer. Common
709  * cases are done through the compiler, the screwed things has to be
710  * done by hand.
711  */
712
713 static s32 snto32(__u32 value, unsigned n)
714 {
715         switch (n) {
716                 case 8:  return ((__s8)value);
717                 case 16: return ((__s16)value);
718                 case 32: return ((__s32)value);
719         }
720         return value & (1 << (n - 1)) ? value | (-1 << n) : value;
721 }
722
723 /*
724  * Convert a signed 32-bit integer to a signed n-bit integer.
725  */
726
727 static u32 s32ton(__s32 value, unsigned n)
728 {
729         s32 a = value >> (n - 1);
730         if (a && a != -1)
731                 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
732         return value & ((1 << n) - 1);
733 }
734
735 /*
736  * Extract/implement a data field from/to a little endian report (bit array).
737  *
738  * Code sort-of follows HID spec:
739  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
740  *
741  * While the USB HID spec allows unlimited length bit fields in "report
742  * descriptors", most devices never use more than 16 bits.
743  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
744  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
745  */
746
747 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
748 {
749         u64 x;
750
751         if (n > 32)
752                 printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
753                                 n, current->comm);
754
755         report += offset >> 3;  /* adjust byte index */
756         offset &= 7;            /* now only need bit offset into one byte */
757         x = get_unaligned_le64(report);
758         x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
759         return (u32) x;
760 }
761
762 /*
763  * "implement" : set bits in a little endian bit stream.
764  * Same concepts as "extract" (see comments above).
765  * The data mangled in the bit stream remains in little endian
766  * order the whole time. It make more sense to talk about
767  * endianness of register values by considering a register
768  * a "cached" copy of the little endiad bit stream.
769  */
770 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
771 {
772         u64 x;
773         u64 m = (1ULL << n) - 1;
774
775         if (n > 32)
776                 printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
777                                 n, current->comm);
778
779         if (value > m)
780                 printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
781                                 value, current->comm);
782         WARN_ON(value > m);
783         value &= m;
784
785         report += offset >> 3;
786         offset &= 7;
787
788         x = get_unaligned_le64(report);
789         x &= ~(m << offset);
790         x |= ((u64)value) << offset;
791         put_unaligned_le64(x, report);
792 }
793
794 /*
795  * Search an array for a value.
796  */
797
798 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
799 {
800         while (n--) {
801                 if (*array++ == value)
802                         return 0;
803         }
804         return -1;
805 }
806
807 /**
808  * hid_match_report - check if driver's raw_event should be called
809  *
810  * @hid: hid device
811  * @report_type: type to match against
812  *
813  * compare hid->driver->report_table->report_type to report->type
814  */
815 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
816 {
817         const struct hid_report_id *id = hid->driver->report_table;
818
819         if (!id) /* NULL means all */
820                 return 1;
821
822         for (; id->report_type != HID_TERMINATOR; id++)
823                 if (id->report_type == HID_ANY_ID ||
824                                 id->report_type == report->type)
825                         return 1;
826         return 0;
827 }
828
829 /**
830  * hid_match_usage - check if driver's event should be called
831  *
832  * @hid: hid device
833  * @usage: usage to match against
834  *
835  * compare hid->driver->usage_table->usage_{type,code} to
836  * usage->usage_{type,code}
837  */
838 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
839 {
840         const struct hid_usage_id *id = hid->driver->usage_table;
841
842         if (!id) /* NULL means all */
843                 return 1;
844
845         for (; id->usage_type != HID_ANY_ID - 1; id++)
846                 if ((id->usage_hid == HID_ANY_ID ||
847                                 id->usage_hid == usage->hid) &&
848                                 (id->usage_type == HID_ANY_ID ||
849                                 id->usage_type == usage->type) &&
850                                 (id->usage_code == HID_ANY_ID ||
851                                  id->usage_code == usage->code))
852                         return 1;
853         return 0;
854 }
855
856 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
857                 struct hid_usage *usage, __s32 value, int interrupt)
858 {
859         struct hid_driver *hdrv = hid->driver;
860         int ret;
861
862         hid_dump_input(usage, value);
863
864         if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
865                 ret = hdrv->event(hid, field, usage, value);
866                 if (ret != 0) {
867                         if (ret < 0)
868                                 dbg_hid("%s's event failed with %d\n",
869                                                 hdrv->name, ret);
870                         return;
871                 }
872         }
873
874         if (hid->claimed & HID_CLAIMED_INPUT)
875                 hidinput_hid_event(hid, field, usage, value);
876         if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
877                 hid->hiddev_hid_event(hid, field, usage, value);
878 }
879
880 /*
881  * Analyse a received field, and fetch the data from it. The field
882  * content is stored for next report processing (we do differential
883  * reporting to the layer).
884  */
885
886 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
887                             __u8 *data, int interrupt)
888 {
889         unsigned n;
890         unsigned count = field->report_count;
891         unsigned offset = field->report_offset;
892         unsigned size = field->report_size;
893         __s32 min = field->logical_minimum;
894         __s32 max = field->logical_maximum;
895         __s32 *value;
896
897         if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
898                 return;
899
900         for (n = 0; n < count; n++) {
901
902                         value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
903                                                     extract(data, offset + n * size, size);
904
905                         if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
906                             && value[n] >= min && value[n] <= max
907                             && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
908                                 goto exit;
909         }
910
911         for (n = 0; n < count; n++) {
912
913                 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
914                         hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
915                         continue;
916                 }
917
918                 if (field->value[n] >= min && field->value[n] <= max
919                         && field->usage[field->value[n] - min].hid
920                         && search(value, field->value[n], count))
921                                 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
922
923                 if (value[n] >= min && value[n] <= max
924                         && field->usage[value[n] - min].hid
925                         && search(field->value, value[n], count))
926                                 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
927         }
928
929         memcpy(field->value, value, count * sizeof(__s32));
930 exit:
931         kfree(value);
932 }
933
934 /*
935  * Output the field into the report.
936  */
937
938 static void hid_output_field(struct hid_field *field, __u8 *data)
939 {
940         unsigned count = field->report_count;
941         unsigned offset = field->report_offset;
942         unsigned size = field->report_size;
943         unsigned bitsused = offset + count * size;
944         unsigned n;
945
946         /* make sure the unused bits in the last byte are zeros */
947         if (count > 0 && size > 0 && (bitsused % 8) != 0)
948                 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
949
950         for (n = 0; n < count; n++) {
951                 if (field->logical_minimum < 0) /* signed values */
952                         implement(data, offset + n * size, size, s32ton(field->value[n], size));
953                 else                            /* unsigned values */
954                         implement(data, offset + n * size, size, field->value[n]);
955         }
956 }
957
958 /*
959  * Create a report.
960  */
961
962 void hid_output_report(struct hid_report *report, __u8 *data)
963 {
964         unsigned n;
965
966         if (report->id > 0)
967                 *data++ = report->id;
968
969         for (n = 0; n < report->maxfield; n++)
970                 hid_output_field(report->field[n], data);
971 }
972 EXPORT_SYMBOL_GPL(hid_output_report);
973
974 /*
975  * Set a field value. The report this field belongs to has to be
976  * created and transferred to the device, to set this value in the
977  * device.
978  */
979
980 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
981 {
982         unsigned size = field->report_size;
983
984         hid_dump_input(field->usage + offset, value);
985
986         if (offset >= field->report_count) {
987                 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
988                 hid_dump_field(field, 8);
989                 return -1;
990         }
991         if (field->logical_minimum < 0) {
992                 if (value != snto32(s32ton(value, size), size)) {
993                         dbg_hid("value %d is out of range\n", value);
994                         return -1;
995                 }
996         }
997         field->value[offset] = value;
998         return 0;
999 }
1000 EXPORT_SYMBOL_GPL(hid_set_field);
1001
1002 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1003                 const u8 *data)
1004 {
1005         struct hid_report *report;
1006         unsigned int n = 0;     /* Normally report number is 0 */
1007
1008         /* Device uses numbered reports, data[0] is report number */
1009         if (report_enum->numbered)
1010                 n = *data;
1011
1012         report = report_enum->report_id_hash[n];
1013         if (report == NULL)
1014                 dbg_hid("undefined report_id %u received\n", n);
1015
1016         return report;
1017 }
1018
1019 void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1020                 int interrupt)
1021 {
1022         struct hid_report_enum *report_enum = hid->report_enum + type;
1023         struct hid_report *report;
1024         unsigned int a;
1025         int rsize, csize = size;
1026         u8 *cdata = data;
1027
1028         report = hid_get_report(report_enum, data);
1029         if (!report)
1030                 return;
1031
1032         if (report_enum->numbered) {
1033                 cdata++;
1034                 csize--;
1035         }
1036
1037         rsize = ((report->size - 1) >> 3) + 1;
1038
1039         if (csize < rsize) {
1040                 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1041                                 csize, rsize);
1042                 memset(cdata + csize, 0, rsize - csize);
1043         }
1044
1045         if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1046                 hid->hiddev_report_event(hid, report);
1047         if (hid->claimed & HID_CLAIMED_HIDRAW) {
1048                 /* numbered reports need to be passed with the report num */
1049                 if (report_enum->numbered)
1050                         hidraw_report_event(hid, data - 1, size + 1);
1051                 else
1052                         hidraw_report_event(hid, data, size);
1053         }
1054
1055         for (a = 0; a < report->maxfield; a++)
1056                 hid_input_field(hid, report->field[a], cdata, interrupt);
1057
1058         if (hid->claimed & HID_CLAIMED_INPUT)
1059                 hidinput_report_event(hid, report);
1060 }
1061 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1062
1063 /**
1064  * hid_input_report - report data from lower layer (usb, bt...)
1065  *
1066  * @hid: hid device
1067  * @type: HID report type (HID_*_REPORT)
1068  * @data: report contents
1069  * @size: size of data parameter
1070  * @interrupt: called from atomic?
1071  *
1072  * This is data entry for lower layers.
1073  */
1074 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1075 {
1076         struct hid_report_enum *report_enum = hid->report_enum + type;
1077         struct hid_driver *hdrv = hid->driver;
1078         struct hid_report *report;
1079         unsigned int i;
1080         int ret;
1081
1082         if (!hid || !hid->driver)
1083                 return -ENODEV;
1084
1085         if (!size) {
1086                 dbg_hid("empty report\n");
1087                 return -1;
1088         }
1089
1090         dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1091
1092         report = hid_get_report(report_enum, data);
1093         if (!report)
1094                 return -1;
1095
1096         /* dump the report */
1097         dbg_hid("report %d (size %u) = ", report->id, size);
1098         for (i = 0; i < size; i++)
1099                 dbg_hid_line(" %02x", data[i]);
1100         dbg_hid_line("\n");
1101
1102         if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1103                 ret = hdrv->raw_event(hid, report, data, size);
1104                 if (ret != 0)
1105                         return ret < 0 ? ret : 0;
1106         }
1107
1108         hid_report_raw_event(hid, type, data, size, interrupt);
1109
1110         return 0;
1111 }
1112 EXPORT_SYMBOL_GPL(hid_input_report);
1113
1114 static bool hid_match_one_id(struct hid_device *hdev,
1115                 const struct hid_device_id *id)
1116 {
1117         return id->bus == hdev->bus &&
1118                 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1119                 (id->product == HID_ANY_ID || id->product == hdev->product);
1120 }
1121
1122 static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1123                 const struct hid_device_id *id)
1124 {
1125         for (; id->bus; id++)
1126                 if (hid_match_one_id(hdev, id))
1127                         return id;
1128
1129         return NULL;
1130 }
1131
1132 static const struct hid_device_id hid_blacklist[] = {
1133         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1134         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1135         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1136         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1137         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1138         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1139         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1140         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1141         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1142         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1143         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1144         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1145         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1146         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1147         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1148         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1149         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1150         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1151         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1152         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1153         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1154         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
1155         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1156         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1157         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1158         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1159         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1160         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1161         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1162         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1163         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1164         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1165         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1166         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1167         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1168         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1169         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
1170         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_KBD) },
1171         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1172         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
1173         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_LX3) },
1174         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_V150) },
1175         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1176         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1177
1178         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
1179         { }
1180 };
1181
1182 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1183 {
1184         struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1185         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1186
1187         if (!hid_match_id(hdev, hdrv->id_table))
1188                 return 0;
1189
1190         /* generic wants all non-blacklisted */
1191         if (!strncmp(hdrv->name, "generic-", 8))
1192                 return !hid_match_id(hdev, hid_blacklist);
1193
1194         return 1;
1195 }
1196
1197 static int hid_device_probe(struct device *dev)
1198 {
1199         struct hid_driver *hdrv = container_of(dev->driver,
1200                         struct hid_driver, driver);
1201         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1202         const struct hid_device_id *id;
1203         int ret = 0;
1204
1205         if (!hdev->driver) {
1206                 id = hid_match_id(hdev, hdrv->id_table);
1207                 if (id == NULL)
1208                         return -ENODEV;
1209
1210                 hdev->driver = hdrv;
1211                 if (hdrv->probe) {
1212                         ret = hdrv->probe(hdev, id);
1213                 } else { /* default probe */
1214                         ret = hid_parse(hdev);
1215                         if (!ret)
1216                                 ret = hid_hw_start(hdev);
1217                 }
1218                 if (ret)
1219                         hdev->driver = NULL;
1220         }
1221         return ret;
1222 }
1223
1224 static int hid_device_remove(struct device *dev)
1225 {
1226         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1227         struct hid_driver *hdrv = hdev->driver;
1228
1229         if (hdrv) {
1230                 if (hdrv->remove)
1231                         hdrv->remove(hdev);
1232                 else /* default remove */
1233                         hid_hw_stop(hdev);
1234                 hdev->driver = NULL;
1235         }
1236
1237         return 0;
1238 }
1239
1240 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1241 {
1242         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1243
1244         if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1245                         hdev->bus, hdev->vendor, hdev->product))
1246                 return -ENOMEM;
1247
1248         if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1249                 return -ENOMEM;
1250
1251         if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1252                 return -ENOMEM;
1253
1254         if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1255                 return -ENOMEM;
1256
1257         if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1258                         hdev->bus, hdev->vendor, hdev->product))
1259                 return -ENOMEM;
1260
1261         return 0;
1262 }
1263
1264 static struct bus_type hid_bus_type = {
1265         .name           = "hid",
1266         .match          = hid_bus_match,
1267         .probe          = hid_device_probe,
1268         .remove         = hid_device_remove,
1269         .uevent         = hid_uevent,
1270 };
1271
1272 static const struct hid_device_id hid_ignore_list[] = {
1273         { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1274         { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1275         { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1276         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1277         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1278         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1279         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1280         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1281         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1282         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1283         { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1284         { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1285         { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1286         { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1287         { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1288         { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1289         { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1290         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1291         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1292         { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1293         { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1294         { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1295         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1296         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1297         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1298         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1299         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1300         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1301         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1302         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1303         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1304         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1305         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1306         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1307         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1308         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1309         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1310         { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1311         { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1312         { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1313         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1314         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1315         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1316         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1317         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1318         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1319         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1320         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1321         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1322         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1323         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1324         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1325         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1326         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1327         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1328         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1329         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1330         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1331         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1332         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1333         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1334         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1335         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1336         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1337         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1338         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1339         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1340         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1341         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1342         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1343         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1344         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1345         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1346         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1347         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1348         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1349         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1350         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1351         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1352         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1353         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1354         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1355         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1356         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1357         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1358         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1359         { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1360         { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1361         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1362         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1363         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1364         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1365         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1366         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1367         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1368         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1369         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1370         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1371         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1372         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1373         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1374         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1375         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1376         { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1377         { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1378         { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1379         { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1380         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1381         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1382         { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1383         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1384         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1385         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1386         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1387         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1388         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1389         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1390         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1391         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1392         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1393         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1394         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1395         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1396         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1397         { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD) },
1398         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1399         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1400         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1401         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1402         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1403         { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1404         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1405         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1406         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1407         { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1408         { }
1409 };
1410
1411 static bool hid_ignore(struct hid_device *hdev)
1412 {
1413         switch (hdev->vendor) {
1414         case USB_VENDOR_ID_CODEMERCS:
1415                 /* ignore all Code Mercenaries IOWarrior devices */
1416                 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1417                                 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1418                         return true;
1419                 break;
1420         case USB_VENDOR_ID_LOGITECH:
1421                 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1422                                 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1423                         return true;
1424                 break;
1425         }
1426
1427         return !!hid_match_id(hdev, hid_ignore_list);
1428 }
1429
1430 int hid_add_device(struct hid_device *hdev)
1431 {
1432         static atomic_t id = ATOMIC_INIT(0);
1433         int ret;
1434
1435         if (WARN_ON(hdev->status & HID_STAT_ADDED))
1436                 return -EBUSY;
1437
1438         /* we need to kill them here, otherwise they will stay allocated to
1439          * wait for coming driver */
1440         if (hid_ignore(hdev))
1441                 return -ENODEV;
1442
1443         /* XXX hack, any other cleaner solution < 20 bus_id bytes? */
1444         sprintf(hdev->dev.bus_id, "%04X:%04X:%04X.%04X", hdev->bus,
1445                         hdev->vendor, hdev->product, atomic_inc_return(&id));
1446
1447         ret = device_add(&hdev->dev);
1448         if (!ret)
1449                 hdev->status |= HID_STAT_ADDED;
1450
1451         return ret;
1452 }
1453 EXPORT_SYMBOL_GPL(hid_add_device);
1454
1455 /**
1456  * hid_allocate_device - allocate new hid device descriptor
1457  *
1458  * Allocate and initialize hid device, so that hid_destroy_device might be
1459  * used to free it.
1460  *
1461  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1462  * error value.
1463  */
1464 struct hid_device *hid_allocate_device(void)
1465 {
1466         struct hid_device *hdev;
1467         unsigned int i;
1468         int ret = -ENOMEM;
1469
1470         hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1471         if (hdev == NULL)
1472                 return ERR_PTR(ret);
1473
1474         device_initialize(&hdev->dev);
1475         hdev->dev.release = hid_device_release;
1476         hdev->dev.bus = &hid_bus_type;
1477
1478         hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1479                         sizeof(struct hid_collection), GFP_KERNEL);
1480         if (hdev->collection == NULL)
1481                 goto err;
1482         hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1483
1484         for (i = 0; i < HID_REPORT_TYPES; i++)
1485                 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1486
1487         return hdev;
1488 err:
1489         put_device(&hdev->dev);
1490         return ERR_PTR(ret);
1491 }
1492 EXPORT_SYMBOL_GPL(hid_allocate_device);
1493
1494 static void hid_remove_device(struct hid_device *hdev)
1495 {
1496         if (hdev->status & HID_STAT_ADDED) {
1497                 device_del(&hdev->dev);
1498                 hdev->status &= ~HID_STAT_ADDED;
1499         }
1500 }
1501
1502 /**
1503  * hid_destroy_device - free previously allocated device
1504  *
1505  * @hdev: hid device
1506  *
1507  * If you allocate hid_device through hid_allocate_device, you should ever
1508  * free by this function.
1509  */
1510 void hid_destroy_device(struct hid_device *hdev)
1511 {
1512         hid_remove_device(hdev);
1513         put_device(&hdev->dev);
1514 }
1515 EXPORT_SYMBOL_GPL(hid_destroy_device);
1516
1517 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1518                 const char *mod_name)
1519 {
1520         hdrv->driver.name = hdrv->name;
1521         hdrv->driver.bus = &hid_bus_type;
1522         hdrv->driver.owner = owner;
1523         hdrv->driver.mod_name = mod_name;
1524
1525         return driver_register(&hdrv->driver);
1526 }
1527 EXPORT_SYMBOL_GPL(__hid_register_driver);
1528
1529 void hid_unregister_driver(struct hid_driver *hdrv)
1530 {
1531         driver_unregister(&hdrv->driver);
1532 }
1533 EXPORT_SYMBOL_GPL(hid_unregister_driver);
1534
1535 #ifdef CONFIG_HID_COMPAT
1536 static void hid_compat_load(struct work_struct *ws)
1537 {
1538         request_module("hid-dummy");
1539 }
1540 static DECLARE_WORK(hid_compat_work, hid_compat_load);
1541 #endif
1542
1543 static int __init hid_init(void)
1544 {
1545         int ret;
1546
1547         ret = bus_register(&hid_bus_type);
1548         if (ret) {
1549                 printk(KERN_ERR "HID: can't register hid bus\n");
1550                 goto err;
1551         }
1552
1553         ret = hidraw_init();
1554         if (ret)
1555                 goto err_bus;
1556
1557 #ifdef CONFIG_HID_COMPAT
1558         schedule_work(&hid_compat_work);
1559 #endif
1560
1561         return 0;
1562 err_bus:
1563         bus_unregister(&hid_bus_type);
1564 err:
1565         return ret;
1566 }
1567
1568 static void __exit hid_exit(void)
1569 {
1570         hidraw_exit();
1571         bus_unregister(&hid_bus_type);
1572 }
1573
1574 module_init(hid_init);
1575 module_exit(hid_exit);
1576
1577 MODULE_LICENSE(DRIVER_LICENSE);
1578