HID: Add and use hid_<level>: dev_<level> equivalents
[linux-2.6.git] / 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-2010 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/mm.h>
25 #include <linux/spinlock.h>
26 #include <asm/unaligned.h>
27 #include <asm/byteorder.h>
28 #include <linux/input.h>
29 #include <linux/wait.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32
33 #include <linux/hid.h>
34 #include <linux/hiddev.h>
35 #include <linux/hid-debug.h>
36 #include <linux/hidraw.h>
37
38 #include "hid-ids.h"
39
40 /*
41  * Version Information
42  */
43
44 #define DRIVER_DESC "HID core driver"
45 #define DRIVER_LICENSE "GPL"
46
47 int hid_debug = 0;
48 module_param_named(debug, hid_debug, int, 0600);
49 MODULE_PARM_DESC(debug, "toggle HID debugging messages");
50 EXPORT_SYMBOL_GPL(hid_debug);
51
52 /*
53  * Register a new report for a device.
54  */
55
56 struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
57 {
58         struct hid_report_enum *report_enum = device->report_enum + type;
59         struct hid_report *report;
60
61         if (report_enum->report_id_hash[id])
62                 return report_enum->report_id_hash[id];
63
64         if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
65                 return NULL;
66
67         if (id != 0)
68                 report_enum->numbered = 1;
69
70         report->id = id;
71         report->type = type;
72         report->size = 0;
73         report->device = device;
74         report_enum->report_id_hash[id] = report;
75
76         list_add_tail(&report->list, &report_enum->report_list);
77
78         return report;
79 }
80 EXPORT_SYMBOL_GPL(hid_register_report);
81
82 /*
83  * Register a new field for this report.
84  */
85
86 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
87 {
88         struct hid_field *field;
89
90         if (report->maxfield == HID_MAX_FIELDS) {
91                 dbg_hid("too many fields in report\n");
92                 return NULL;
93         }
94
95         if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
96                 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
97
98         field->index = report->maxfield++;
99         report->field[field->index] = field;
100         field->usage = (struct hid_usage *)(field + 1);
101         field->value = (s32 *)(field->usage + usages);
102         field->report = report;
103
104         return field;
105 }
106
107 /*
108  * Open a collection. The type/usage is pushed on the stack.
109  */
110
111 static int open_collection(struct hid_parser *parser, unsigned type)
112 {
113         struct hid_collection *collection;
114         unsigned usage;
115
116         usage = parser->local.usage[0];
117
118         if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
119                 dbg_hid("collection stack overflow\n");
120                 return -1;
121         }
122
123         if (parser->device->maxcollection == parser->device->collection_size) {
124                 collection = kmalloc(sizeof(struct hid_collection) *
125                                 parser->device->collection_size * 2, GFP_KERNEL);
126                 if (collection == NULL) {
127                         dbg_hid("failed to reallocate collection array\n");
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_hid("collection stack underflow\n");
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_hid("usage index exceeded\n");
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("hid_register_report failed\n");
216                 return -1;
217         }
218
219         if (parser->global.logical_maximum < parser->global.logical_minimum) {
220                 dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
221                 return -1;
222         }
223
224         offset = report->size;
225         report->size += parser->global.report_size * parser->global.report_count;
226
227         if (!parser->local.usage_index) /* Ignore padding fields */
228                 return 0;
229
230         usages = max_t(int, parser->local.usage_index, parser->global.report_count);
231
232         if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
233                 return 0;
234
235         field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
236         field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
237         field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
238
239         for (i = 0; i < usages; i++) {
240                 int j = i;
241                 /* Duplicate the last usage we parsed if we have excess values */
242                 if (i >= parser->local.usage_index)
243                         j = parser->local.usage_index - 1;
244                 field->usage[i].hid = parser->local.usage[j];
245                 field->usage[i].collection_index =
246                         parser->local.collection_index[j];
247         }
248
249         field->maxusage = usages;
250         field->flags = flags;
251         field->report_offset = offset;
252         field->report_type = report_type;
253         field->report_size = parser->global.report_size;
254         field->report_count = parser->global.report_count;
255         field->logical_minimum = parser->global.logical_minimum;
256         field->logical_maximum = parser->global.logical_maximum;
257         field->physical_minimum = parser->global.physical_minimum;
258         field->physical_maximum = parser->global.physical_maximum;
259         field->unit_exponent = parser->global.unit_exponent;
260         field->unit = parser->global.unit;
261
262         return 0;
263 }
264
265 /*
266  * Read data value from item.
267  */
268
269 static u32 item_udata(struct hid_item *item)
270 {
271         switch (item->size) {
272         case 1: return item->data.u8;
273         case 2: return item->data.u16;
274         case 4: return item->data.u32;
275         }
276         return 0;
277 }
278
279 static s32 item_sdata(struct hid_item *item)
280 {
281         switch (item->size) {
282         case 1: return item->data.s8;
283         case 2: return item->data.s16;
284         case 4: return item->data.s32;
285         }
286         return 0;
287 }
288
289 /*
290  * Process a global item.
291  */
292
293 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
294 {
295         switch (item->tag) {
296         case HID_GLOBAL_ITEM_TAG_PUSH:
297
298                 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
299                         dbg_hid("global enviroment stack overflow\n");
300                         return -1;
301                 }
302
303                 memcpy(parser->global_stack + parser->global_stack_ptr++,
304                         &parser->global, sizeof(struct hid_global));
305                 return 0;
306
307         case HID_GLOBAL_ITEM_TAG_POP:
308
309                 if (!parser->global_stack_ptr) {
310                         dbg_hid("global enviroment stack underflow\n");
311                         return -1;
312                 }
313
314                 memcpy(&parser->global, parser->global_stack +
315                         --parser->global_stack_ptr, sizeof(struct hid_global));
316                 return 0;
317
318         case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
319                 parser->global.usage_page = item_udata(item);
320                 return 0;
321
322         case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
323                 parser->global.logical_minimum = item_sdata(item);
324                 return 0;
325
326         case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
327                 if (parser->global.logical_minimum < 0)
328                         parser->global.logical_maximum = item_sdata(item);
329                 else
330                         parser->global.logical_maximum = item_udata(item);
331                 return 0;
332
333         case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
334                 parser->global.physical_minimum = item_sdata(item);
335                 return 0;
336
337         case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
338                 if (parser->global.physical_minimum < 0)
339                         parser->global.physical_maximum = item_sdata(item);
340                 else
341                         parser->global.physical_maximum = item_udata(item);
342                 return 0;
343
344         case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
345                 parser->global.unit_exponent = item_sdata(item);
346                 return 0;
347
348         case HID_GLOBAL_ITEM_TAG_UNIT:
349                 parser->global.unit = item_udata(item);
350                 return 0;
351
352         case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
353                 parser->global.report_size = item_udata(item);
354                 if (parser->global.report_size > 32) {
355                         dbg_hid("invalid report_size %d\n",
356                                         parser->global.report_size);
357                         return -1;
358                 }
359                 return 0;
360
361         case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
362                 parser->global.report_count = item_udata(item);
363                 if (parser->global.report_count > HID_MAX_USAGES) {
364                         dbg_hid("invalid report_count %d\n",
365                                         parser->global.report_count);
366                         return -1;
367                 }
368                 return 0;
369
370         case HID_GLOBAL_ITEM_TAG_REPORT_ID:
371                 parser->global.report_id = item_udata(item);
372                 if (parser->global.report_id == 0) {
373                         dbg_hid("report_id 0 is invalid\n");
374                         return -1;
375                 }
376                 return 0;
377
378         default:
379                 dbg_hid("unknown global tag 0x%x\n", item->tag);
380                 return -1;
381         }
382 }
383
384 /*
385  * Process a local item.
386  */
387
388 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
389 {
390         __u32 data;
391         unsigned n;
392
393         data = item_udata(item);
394
395         switch (item->tag) {
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_hid("nested delimiters\n");
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_hid("bogus close delimiter\n");
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_hid("alternative usage ignored\n");
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_hid("alternative usage ignored\n");
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_hid("alternative usage ignored\n");
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("hid_add_usage failed\n");
458                                 return -1;
459                         }
460                 return 0;
461
462         default:
463
464                 dbg_hid("unknown local item tag 0x%x\n", 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_hid("unknown main item tag 0x%x\n", 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_hid("reserved item type, tag 0x%x\n", 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_device_release(struct device *dev)
537 {
538         struct hid_device *device = container_of(dev, struct hid_device, dev);
539         unsigned i, j;
540
541         for (i = 0; i < HID_REPORT_TYPES; i++) {
542                 struct hid_report_enum *report_enum = device->report_enum + i;
543
544                 for (j = 0; j < 256; j++) {
545                         struct hid_report *report = report_enum->report_id_hash[j];
546                         if (report)
547                                 hid_free_report(report);
548                 }
549         }
550
551         kfree(device->rdesc);
552         kfree(device->collection);
553         kfree(device);
554 }
555
556 /*
557  * Fetch a report description item from the data stream. We support long
558  * items, though they are not used yet.
559  */
560
561 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
562 {
563         u8 b;
564
565         if ((end - start) <= 0)
566                 return NULL;
567
568         b = *start++;
569
570         item->type = (b >> 2) & 3;
571         item->tag  = (b >> 4) & 15;
572
573         if (item->tag == HID_ITEM_TAG_LONG) {
574
575                 item->format = HID_ITEM_FORMAT_LONG;
576
577                 if ((end - start) < 2)
578                         return NULL;
579
580                 item->size = *start++;
581                 item->tag  = *start++;
582
583                 if ((end - start) < item->size)
584                         return NULL;
585
586                 item->data.longdata = start;
587                 start += item->size;
588                 return start;
589         }
590
591         item->format = HID_ITEM_FORMAT_SHORT;
592         item->size = b & 3;
593
594         switch (item->size) {
595         case 0:
596                 return start;
597
598         case 1:
599                 if ((end - start) < 1)
600                         return NULL;
601                 item->data.u8 = *start++;
602                 return start;
603
604         case 2:
605                 if ((end - start) < 2)
606                         return NULL;
607                 item->data.u16 = get_unaligned_le16(start);
608                 start = (__u8 *)((__le16 *)start + 1);
609                 return start;
610
611         case 3:
612                 item->size++;
613                 if ((end - start) < 4)
614                         return NULL;
615                 item->data.u32 = get_unaligned_le32(start);
616                 start = (__u8 *)((__le32 *)start + 1);
617                 return start;
618         }
619
620         return NULL;
621 }
622
623 /**
624  * hid_parse_report - parse device report
625  *
626  * @device: hid device
627  * @start: report start
628  * @size: report size
629  *
630  * Parse a report description into a hid_device structure. Reports are
631  * enumerated, fields are attached to these reports.
632  * 0 returned on success, otherwise nonzero error value.
633  */
634 int hid_parse_report(struct hid_device *device, __u8 *start,
635                 unsigned size)
636 {
637         struct hid_parser *parser;
638         struct hid_item item;
639         __u8 *end;
640         int ret;
641         static int (*dispatch_type[])(struct hid_parser *parser,
642                                       struct hid_item *item) = {
643                 hid_parser_main,
644                 hid_parser_global,
645                 hid_parser_local,
646                 hid_parser_reserved
647         };
648
649         if (device->driver->report_fixup)
650                 start = device->driver->report_fixup(device, start, &size);
651
652         device->rdesc = kmemdup(start, size, GFP_KERNEL);
653         if (device->rdesc == NULL)
654                 return -ENOMEM;
655         device->rsize = size;
656
657         parser = vmalloc(sizeof(struct hid_parser));
658         if (!parser) {
659                 ret = -ENOMEM;
660                 goto err;
661         }
662
663         memset(parser, 0, sizeof(struct hid_parser));
664         parser->device = device;
665
666         end = start + size;
667         ret = -EINVAL;
668         while ((start = fetch_item(start, end, &item)) != NULL) {
669
670                 if (item.format != HID_ITEM_FORMAT_SHORT) {
671                         dbg_hid("unexpected long global item\n");
672                         goto err;
673                 }
674
675                 if (dispatch_type[item.type](parser, &item)) {
676                         dbg_hid("item %u %u %u %u parsing failed\n",
677                                 item.format, (unsigned)item.size,
678                                 (unsigned)item.type, (unsigned)item.tag);
679                         goto err;
680                 }
681
682                 if (start == end) {
683                         if (parser->collection_stack_ptr) {
684                                 dbg_hid("unbalanced collection at end of report description\n");
685                                 goto err;
686                         }
687                         if (parser->local.delimiter_depth) {
688                                 dbg_hid("unbalanced delimiter at end of report description\n");
689                                 goto err;
690                         }
691                         vfree(parser);
692                         return 0;
693                 }
694         }
695
696         dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
697 err:
698         vfree(parser);
699         return ret;
700 }
701 EXPORT_SYMBOL_GPL(hid_parse_report);
702
703 /*
704  * Convert a signed n-bit integer to signed 32-bit integer. Common
705  * cases are done through the compiler, the screwed things has to be
706  * done by hand.
707  */
708
709 static s32 snto32(__u32 value, unsigned n)
710 {
711         switch (n) {
712         case 8:  return ((__s8)value);
713         case 16: return ((__s16)value);
714         case 32: return ((__s32)value);
715         }
716         return value & (1 << (n - 1)) ? value | (-1 << n) : value;
717 }
718
719 /*
720  * Convert a signed 32-bit integer to a signed n-bit integer.
721  */
722
723 static u32 s32ton(__s32 value, unsigned n)
724 {
725         s32 a = value >> (n - 1);
726         if (a && a != -1)
727                 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
728         return value & ((1 << n) - 1);
729 }
730
731 /*
732  * Extract/implement a data field from/to a little endian report (bit array).
733  *
734  * Code sort-of follows HID spec:
735  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
736  *
737  * While the USB HID spec allows unlimited length bit fields in "report
738  * descriptors", most devices never use more than 16 bits.
739  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
740  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
741  */
742
743 static __inline__ __u32 extract(const struct hid_device *hid, __u8 *report,
744                                 unsigned offset, unsigned n)
745 {
746         u64 x;
747
748         if (n > 32)
749                 hid_warn(hid, "extract() called with n (%d) > 32! (%s)\n",
750                          n, current->comm);
751
752         report += offset >> 3;  /* adjust byte index */
753         offset &= 7;            /* now only need bit offset into one byte */
754         x = get_unaligned_le64(report);
755         x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
756         return (u32) x;
757 }
758
759 /*
760  * "implement" : set bits in a little endian bit stream.
761  * Same concepts as "extract" (see comments above).
762  * The data mangled in the bit stream remains in little endian
763  * order the whole time. It make more sense to talk about
764  * endianness of register values by considering a register
765  * a "cached" copy of the little endiad bit stream.
766  */
767 static __inline__ void implement(const struct hid_device *hid, __u8 *report,
768                                  unsigned offset, unsigned n, __u32 value)
769 {
770         u64 x;
771         u64 m = (1ULL << n) - 1;
772
773         if (n > 32)
774                 hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
775                          __func__, n, current->comm);
776
777         if (value > m)
778                 hid_warn(hid, "%s() called with too large value %d! (%s)\n",
779                          __func__, value, current->comm);
780         WARN_ON(value > m);
781         value &= m;
782
783         report += offset >> 3;
784         offset &= 7;
785
786         x = get_unaligned_le64(report);
787         x &= ~(m << offset);
788         x |= ((u64)value) << offset;
789         put_unaligned_le64(x, report);
790 }
791
792 /*
793  * Search an array for a value.
794  */
795
796 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
797 {
798         while (n--) {
799                 if (*array++ == value)
800                         return 0;
801         }
802         return -1;
803 }
804
805 /**
806  * hid_match_report - check if driver's raw_event should be called
807  *
808  * @hid: hid device
809  * @report_type: type to match against
810  *
811  * compare hid->driver->report_table->report_type to report->type
812  */
813 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
814 {
815         const struct hid_report_id *id = hid->driver->report_table;
816
817         if (!id) /* NULL means all */
818                 return 1;
819
820         for (; id->report_type != HID_TERMINATOR; id++)
821                 if (id->report_type == HID_ANY_ID ||
822                                 id->report_type == report->type)
823                         return 1;
824         return 0;
825 }
826
827 /**
828  * hid_match_usage - check if driver's event should be called
829  *
830  * @hid: hid device
831  * @usage: usage to match against
832  *
833  * compare hid->driver->usage_table->usage_{type,code} to
834  * usage->usage_{type,code}
835  */
836 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
837 {
838         const struct hid_usage_id *id = hid->driver->usage_table;
839
840         if (!id) /* NULL means all */
841                 return 1;
842
843         for (; id->usage_type != HID_ANY_ID - 1; id++)
844                 if ((id->usage_hid == HID_ANY_ID ||
845                                 id->usage_hid == usage->hid) &&
846                                 (id->usage_type == HID_ANY_ID ||
847                                 id->usage_type == usage->type) &&
848                                 (id->usage_code == HID_ANY_ID ||
849                                  id->usage_code == usage->code))
850                         return 1;
851         return 0;
852 }
853
854 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
855                 struct hid_usage *usage, __s32 value, int interrupt)
856 {
857         struct hid_driver *hdrv = hid->driver;
858         int ret;
859
860         hid_dump_input(hid, usage, value);
861
862         if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
863                 ret = hdrv->event(hid, field, usage, value);
864                 if (ret != 0) {
865                         if (ret < 0)
866                                 dbg_hid("%s's event failed with %d\n",
867                                                 hdrv->name, ret);
868                         return;
869                 }
870         }
871
872         if (hid->claimed & HID_CLAIMED_INPUT)
873                 hidinput_hid_event(hid, field, usage, value);
874         if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
875                 hid->hiddev_hid_event(hid, field, usage, value);
876 }
877
878 /*
879  * Analyse a received field, and fetch the data from it. The field
880  * content is stored for next report processing (we do differential
881  * reporting to the layer).
882  */
883
884 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
885                             __u8 *data, int interrupt)
886 {
887         unsigned n;
888         unsigned count = field->report_count;
889         unsigned offset = field->report_offset;
890         unsigned size = field->report_size;
891         __s32 min = field->logical_minimum;
892         __s32 max = field->logical_maximum;
893         __s32 *value;
894
895         if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
896                 return;
897
898         for (n = 0; n < count; n++) {
899
900                 value[n] = min < 0 ?
901                         snto32(extract(hid, data, offset + n * size, size),
902                                size) :
903                         extract(hid, data, offset + n * size, size);
904
905                 /* Ignore report if ErrorRollOver */
906                 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
907                     value[n] >= min && value[n] <= max &&
908                     field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
909                         goto exit;
910         }
911
912         for (n = 0; n < count; n++) {
913
914                 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
915                         hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
916                         continue;
917                 }
918
919                 if (field->value[n] >= min && field->value[n] <= max
920                         && field->usage[field->value[n] - min].hid
921                         && search(value, field->value[n], count))
922                                 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
923
924                 if (value[n] >= min && value[n] <= max
925                         && field->usage[value[n] - min].hid
926                         && search(field->value, value[n], count))
927                                 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
928         }
929
930         memcpy(field->value, value, count * sizeof(__s32));
931 exit:
932         kfree(value);
933 }
934
935 /*
936  * Output the field into the report.
937  */
938
939 static void hid_output_field(const struct hid_device *hid,
940                              struct hid_field *field, __u8 *data)
941 {
942         unsigned count = field->report_count;
943         unsigned offset = field->report_offset;
944         unsigned size = field->report_size;
945         unsigned n;
946
947         for (n = 0; n < count; n++) {
948                 if (field->logical_minimum < 0) /* signed values */
949                         implement(hid, data, offset + n * size, size,
950                                   s32ton(field->value[n], size));
951                 else                            /* unsigned values */
952                         implement(hid, data, offset + n * size, size,
953                                   field->value[n]);
954         }
955 }
956
957 /*
958  * Create a report.
959  */
960
961 void hid_output_report(struct hid_report *report, __u8 *data)
962 {
963         unsigned n;
964
965         if (report->id > 0)
966                 *data++ = report->id;
967
968         memset(data, 0, ((report->size - 1) >> 3) + 1);
969         for (n = 0; n < report->maxfield; n++)
970                 hid_output_field(report->device, 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->report->device, 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                 return -1;
989         }
990         if (field->logical_minimum < 0) {
991                 if (value != snto32(s32ton(value, size), size)) {
992                         dbg_hid("value %d is out of range\n", value);
993                         return -1;
994                 }
995         }
996         field->value[offset] = value;
997         return 0;
998 }
999 EXPORT_SYMBOL_GPL(hid_set_field);
1000
1001 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1002                 const u8 *data)
1003 {
1004         struct hid_report *report;
1005         unsigned int n = 0;     /* Normally report number is 0 */
1006
1007         /* Device uses numbered reports, data[0] is report number */
1008         if (report_enum->numbered)
1009                 n = *data;
1010
1011         report = report_enum->report_id_hash[n];
1012         if (report == NULL)
1013                 dbg_hid("undefined report_id %u received\n", n);
1014
1015         return report;
1016 }
1017
1018 void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1019                 int interrupt)
1020 {
1021         struct hid_report_enum *report_enum = hid->report_enum + type;
1022         struct hid_report *report;
1023         unsigned int a;
1024         int rsize, csize = size;
1025         u8 *cdata = data;
1026
1027         report = hid_get_report(report_enum, data);
1028         if (!report)
1029                 return;
1030
1031         if (report_enum->numbered) {
1032                 cdata++;
1033                 csize--;
1034         }
1035
1036         rsize = ((report->size - 1) >> 3) + 1;
1037
1038         if (csize < rsize) {
1039                 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1040                                 csize, rsize);
1041                 memset(cdata + csize, 0, rsize - csize);
1042         }
1043
1044         if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1045                 hid->hiddev_report_event(hid, report);
1046         if (hid->claimed & HID_CLAIMED_HIDRAW)
1047                 hidraw_report_event(hid, data, size);
1048
1049         for (a = 0; a < report->maxfield; a++)
1050                 hid_input_field(hid, report->field[a], cdata, interrupt);
1051
1052         if (hid->claimed & HID_CLAIMED_INPUT)
1053                 hidinput_report_event(hid, report);
1054 }
1055 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1056
1057 /**
1058  * hid_input_report - report data from lower layer (usb, bt...)
1059  *
1060  * @hid: hid device
1061  * @type: HID report type (HID_*_REPORT)
1062  * @data: report contents
1063  * @size: size of data parameter
1064  * @interrupt: distinguish between interrupt and control transfers
1065  *
1066  * This is data entry for lower layers.
1067  */
1068 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1069 {
1070         struct hid_report_enum *report_enum;
1071         struct hid_driver *hdrv;
1072         struct hid_report *report;
1073         char *buf;
1074         unsigned int i;
1075         int ret;
1076
1077         if (!hid || !hid->driver)
1078                 return -ENODEV;
1079         report_enum = hid->report_enum + type;
1080         hdrv = hid->driver;
1081
1082         if (!size) {
1083                 dbg_hid("empty report\n");
1084                 return -1;
1085         }
1086
1087         buf = kmalloc(sizeof(char) * HID_DEBUG_BUFSIZE, GFP_ATOMIC);
1088
1089         if (!buf)
1090                 goto nomem;
1091
1092         /* dump the report */
1093         snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1094                         "\nreport (size %u) (%snumbered) = ", size, report_enum->numbered ? "" : "un");
1095         hid_debug_event(hid, buf);
1096
1097         for (i = 0; i < size; i++) {
1098                 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1099                                 " %02x", data[i]);
1100                 hid_debug_event(hid, buf);
1101         }
1102         hid_debug_event(hid, "\n");
1103         kfree(buf);
1104
1105 nomem:
1106         report = hid_get_report(report_enum, data);
1107
1108         if (!report)
1109                 return -1;
1110
1111         if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1112                 ret = hdrv->raw_event(hid, report, data, size);
1113                 if (ret != 0)
1114                         return ret < 0 ? ret : 0;
1115         }
1116
1117         hid_report_raw_event(hid, type, data, size, interrupt);
1118
1119         return 0;
1120 }
1121 EXPORT_SYMBOL_GPL(hid_input_report);
1122
1123 static bool hid_match_one_id(struct hid_device *hdev,
1124                 const struct hid_device_id *id)
1125 {
1126         return id->bus == hdev->bus &&
1127                 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1128                 (id->product == HID_ANY_ID || id->product == hdev->product);
1129 }
1130
1131 static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1132                 const struct hid_device_id *id)
1133 {
1134         for (; id->bus; id++)
1135                 if (hid_match_one_id(hdev, id))
1136                         return id;
1137
1138         return NULL;
1139 }
1140
1141 static const struct hid_device_id hid_hiddev_list[] = {
1142         { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1143         { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1144         { }
1145 };
1146
1147 static bool hid_hiddev(struct hid_device *hdev)
1148 {
1149         return !!hid_match_id(hdev, hid_hiddev_list);
1150 }
1151
1152 int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1153 {
1154         static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1155                 "Joystick", "Gamepad", "Keyboard", "Keypad",
1156                 "Multi-Axis Controller"
1157         };
1158         const char *type, *bus;
1159         char buf[64];
1160         unsigned int i;
1161         int len;
1162
1163         if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1164                 connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
1165         if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
1166                 connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
1167         if (hdev->bus != BUS_USB)
1168                 connect_mask &= ~HID_CONNECT_HIDDEV;
1169         if (hid_hiddev(hdev))
1170                 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1171
1172         if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1173                                 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1174                 hdev->claimed |= HID_CLAIMED_INPUT;
1175         if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1176                         !hdev->hiddev_connect(hdev,
1177                                 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1178                 hdev->claimed |= HID_CLAIMED_HIDDEV;
1179         if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1180                 hdev->claimed |= HID_CLAIMED_HIDRAW;
1181
1182         if (!hdev->claimed) {
1183                 hid_err(hdev, "claimed by neither input, hiddev nor hidraw\n");
1184                 return -ENODEV;
1185         }
1186
1187         if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1188                         (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1189                 hdev->ff_init(hdev);
1190
1191         len = 0;
1192         if (hdev->claimed & HID_CLAIMED_INPUT)
1193                 len += sprintf(buf + len, "input");
1194         if (hdev->claimed & HID_CLAIMED_HIDDEV)
1195                 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1196                                 hdev->minor);
1197         if (hdev->claimed & HID_CLAIMED_HIDRAW)
1198                 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1199                                 ((struct hidraw *)hdev->hidraw)->minor);
1200
1201         type = "Device";
1202         for (i = 0; i < hdev->maxcollection; i++) {
1203                 struct hid_collection *col = &hdev->collection[i];
1204                 if (col->type == HID_COLLECTION_APPLICATION &&
1205                    (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1206                    (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1207                         type = types[col->usage & 0xffff];
1208                         break;
1209                 }
1210         }
1211
1212         switch (hdev->bus) {
1213         case BUS_USB:
1214                 bus = "USB";
1215                 break;
1216         case BUS_BLUETOOTH:
1217                 bus = "BLUETOOTH";
1218                 break;
1219         default:
1220                 bus = "<UNKNOWN>";
1221         }
1222
1223         hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1224                  buf, bus, hdev->version >> 8, hdev->version & 0xff,
1225                  type, hdev->name, hdev->phys);
1226
1227         return 0;
1228 }
1229 EXPORT_SYMBOL_GPL(hid_connect);
1230
1231 void hid_disconnect(struct hid_device *hdev)
1232 {
1233         if (hdev->claimed & HID_CLAIMED_INPUT)
1234                 hidinput_disconnect(hdev);
1235         if (hdev->claimed & HID_CLAIMED_HIDDEV)
1236                 hdev->hiddev_disconnect(hdev);
1237         if (hdev->claimed & HID_CLAIMED_HIDRAW)
1238                 hidraw_disconnect(hdev);
1239 }
1240 EXPORT_SYMBOL_GPL(hid_disconnect);
1241
1242 /* a list of devices for which there is a specialized driver on HID bus */
1243 static const struct hid_device_id hid_have_special_driver[] = {
1244         { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
1245         { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M2256) },
1246         { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
1247         { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1248         { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649) },
1249 #if defined(CONFIG_HID_ACRUX_FF) || defined(CONFIG_HID_ACRUX_FF_MODULE)
1250         { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802) },
1251 #endif
1252         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
1253         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1254         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1255         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE) },
1256         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICTRACKPAD) },
1257         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1258         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1259         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1260         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1261         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1262         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1263         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1264         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1265         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1266         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1267         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1268         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1269         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1270         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
1271         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1272         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1273         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1274         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1275         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1276         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1277         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1278         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1279         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
1280         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1281         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1282         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1283         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1284         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1285         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1286         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1287         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1288         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1289         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
1290         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
1291         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
1292         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
1293         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
1294         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
1295         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI) },
1296         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO) },
1297         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS) },
1298         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1299         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1300         { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_T91MT) },
1301         { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
1302         { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
1303         { HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE) },
1304         { HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE_2) },
1305         { HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
1306         { HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
1307         { HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
1308         { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1309         { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION_SOLAR) },
1310         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1311         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS) },
1312         { HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_PRODIKEYS_PCMIDI) },
1313         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
1314         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1315         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) },
1316         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
1317         { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
1318         { HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
1319         { HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
1320         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
1321         { HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
1322         { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
1323         { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1324         { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
1325         { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
1326         { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1327         { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
1328         { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1329         { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
1330         { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
1331         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
1332         { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
1333         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1334         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1335         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1336         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1337         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1338         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1339         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
1340         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1341         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
1342         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1343         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1344         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD_CORD) },
1345         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1346         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1347         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1348         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
1349         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1350         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FLIGHT_SYSTEM_G940) },
1351         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1352         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1353         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
1354         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
1355         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
1356         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
1357         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) },
1358         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
1359         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
1360         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
1361         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
1362         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
1363         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1364         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
1365         { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
1366         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
1367         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1) },
1368         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2) },
1369         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3) },
1370         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4) },
1371         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5) },
1372         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6) },
1373         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7) },
1374         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8) },
1375         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9) },
1376         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10) },
1377         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11) },
1378         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12) },
1379         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13) },
1380         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14) },
1381         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15) },
1382         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16) },
1383         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17) },
1384         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18) },
1385         { HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_WKB2000) },
1386         { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1387         { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) },
1388         { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, USB_DEVICE_ID_PIXART_IMAGING_INC_OPTICAL_TOUCH_SCREEN) },
1389         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
1390         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
1391         { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1392         { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_WIRELESS_KBD_MOUSE) },
1393         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1394         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1395         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
1396         { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, USB_DEVICE_ID_MTP) },
1397         { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM, USB_DEVICE_ID_MTP_STM) },
1398         { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX, USB_DEVICE_ID_MTP_SITRONIX) },
1399         { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1400         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1401         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
1402         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
1403         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
1404         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1405         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653) },
1406         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1407         { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
1408         { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED2, USB_DEVICE_ID_TOPSEED2_RF_COMBO) },
1409         { HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
1410         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
1411         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
1412         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
1413         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
1414         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
1415         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
1416         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_5_8_INCH) },
1417         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_12_1_INCH) },
1418         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_10_6_INCH) },
1419         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_14_1_INCH) },
1420         { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1421         { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1422         { HID_USB_DEVICE(USB_VENDOR_ID_ZYDACRON, USB_DEVICE_ID_ZYDACRON_REMOTE_CONTROL) },
1423
1424         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
1425         { }
1426 };
1427
1428 struct hid_dynid {
1429         struct list_head list;
1430         struct hid_device_id id;
1431 };
1432
1433 /**
1434  * store_new_id - add a new HID device ID to this driver and re-probe devices
1435  * @driver: target device driver
1436  * @buf: buffer for scanning device ID data
1437  * @count: input size
1438  *
1439  * Adds a new dynamic hid device ID to this driver,
1440  * and causes the driver to probe for all devices again.
1441  */
1442 static ssize_t store_new_id(struct device_driver *drv, const char *buf,
1443                 size_t count)
1444 {
1445         struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1446         struct hid_dynid *dynid;
1447         __u32 bus, vendor, product;
1448         unsigned long driver_data = 0;
1449         int ret;
1450
1451         ret = sscanf(buf, "%x %x %x %lx",
1452                         &bus, &vendor, &product, &driver_data);
1453         if (ret < 3)
1454                 return -EINVAL;
1455
1456         dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1457         if (!dynid)
1458                 return -ENOMEM;
1459
1460         dynid->id.bus = bus;
1461         dynid->id.vendor = vendor;
1462         dynid->id.product = product;
1463         dynid->id.driver_data = driver_data;
1464
1465         spin_lock(&hdrv->dyn_lock);
1466         list_add_tail(&dynid->list, &hdrv->dyn_list);
1467         spin_unlock(&hdrv->dyn_lock);
1468
1469         ret = 0;
1470         if (get_driver(&hdrv->driver)) {
1471                 ret = driver_attach(&hdrv->driver);
1472                 put_driver(&hdrv->driver);
1473         }
1474
1475         return ret ? : count;
1476 }
1477 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1478
1479 static void hid_free_dynids(struct hid_driver *hdrv)
1480 {
1481         struct hid_dynid *dynid, *n;
1482
1483         spin_lock(&hdrv->dyn_lock);
1484         list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1485                 list_del(&dynid->list);
1486                 kfree(dynid);
1487         }
1488         spin_unlock(&hdrv->dyn_lock);
1489 }
1490
1491 static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1492                 struct hid_driver *hdrv)
1493 {
1494         struct hid_dynid *dynid;
1495
1496         spin_lock(&hdrv->dyn_lock);
1497         list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1498                 if (hid_match_one_id(hdev, &dynid->id)) {
1499                         spin_unlock(&hdrv->dyn_lock);
1500                         return &dynid->id;
1501                 }
1502         }
1503         spin_unlock(&hdrv->dyn_lock);
1504
1505         return hid_match_id(hdev, hdrv->id_table);
1506 }
1507
1508 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1509 {
1510         struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1511         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1512
1513         if (!hid_match_device(hdev, hdrv))
1514                 return 0;
1515
1516         /* generic wants all that don't have specialized driver */
1517         if (!strncmp(hdrv->name, "generic-", 8))
1518                 return !hid_match_id(hdev, hid_have_special_driver);
1519
1520         return 1;
1521 }
1522
1523 static int hid_device_probe(struct device *dev)
1524 {
1525         struct hid_driver *hdrv = container_of(dev->driver,
1526                         struct hid_driver, driver);
1527         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1528         const struct hid_device_id *id;
1529         int ret = 0;
1530
1531         if (!hdev->driver) {
1532                 id = hid_match_device(hdev, hdrv);
1533                 if (id == NULL)
1534                         return -ENODEV;
1535
1536                 hdev->driver = hdrv;
1537                 if (hdrv->probe) {
1538                         ret = hdrv->probe(hdev, id);
1539                 } else { /* default probe */
1540                         ret = hid_parse(hdev);
1541                         if (!ret)
1542                                 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1543                 }
1544                 if (ret)
1545                         hdev->driver = NULL;
1546         }
1547         return ret;
1548 }
1549
1550 static int hid_device_remove(struct device *dev)
1551 {
1552         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1553         struct hid_driver *hdrv = hdev->driver;
1554
1555         if (hdrv) {
1556                 if (hdrv->remove)
1557                         hdrv->remove(hdev);
1558                 else /* default remove */
1559                         hid_hw_stop(hdev);
1560                 hdev->driver = NULL;
1561         }
1562
1563         return 0;
1564 }
1565
1566 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1567 {
1568         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1569
1570         if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1571                         hdev->bus, hdev->vendor, hdev->product))
1572                 return -ENOMEM;
1573
1574         if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1575                 return -ENOMEM;
1576
1577         if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1578                 return -ENOMEM;
1579
1580         if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1581                 return -ENOMEM;
1582
1583         if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1584                         hdev->bus, hdev->vendor, hdev->product))
1585                 return -ENOMEM;
1586
1587         return 0;
1588 }
1589
1590 static struct bus_type hid_bus_type = {
1591         .name           = "hid",
1592         .match          = hid_bus_match,
1593         .probe          = hid_device_probe,
1594         .remove         = hid_device_remove,
1595         .uevent         = hid_uevent,
1596 };
1597
1598 /* a list of devices that shouldn't be handled by HID core at all */
1599 static const struct hid_device_id hid_ignore_list[] = {
1600         { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1601         { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1602         { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1603         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1604         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1605         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1606         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1607         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1608         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1609         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1610         { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1611         { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1612         { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM)},
1613         { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM2)},
1614         { HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1615         { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1616         { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1617         { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1618         { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1619         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1620         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1621         { HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
1622         { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1623         { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1624         { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1625         { HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC5UH) },
1626         { HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC4UM) },
1627         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1628         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1629         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1630         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1631         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1632         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1633         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1634         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1635         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1636         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1637         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1638         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1639         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1640         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1641         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1642         { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1643         { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1644         { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1645         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1646         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1647         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1648         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1649         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1650         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1651         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1652         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1653         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1654         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1655         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1656         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1657         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1658         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1659         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1660         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1661         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1662         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1663         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1664         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1665         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1666         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1667         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1668         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1669         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1670         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1671         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1672         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1673         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1674         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1675         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1676         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1677         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1678         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1679         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1680         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1681         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1682         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1683         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1684         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1685         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1686         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1687         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1688         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1689         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1690         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1691         { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1692         { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_JESS_YUREX) },
1693         { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1694         { HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
1695         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1696         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_KYE, 0x0058) },
1697         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1698         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1699         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1700         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1701         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1702         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1703         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1704         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1705         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1706         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1707         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1708         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1709         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1710         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1711         { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1712         { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1713         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1714         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1715         { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1716         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1717         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1718         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1719         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1720         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1721         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1722         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1723         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1724         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1725         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1726         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1727         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1728         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1729         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1730         { HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
1731         { HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
1732         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1733         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1734         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1735         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1736         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1737         { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1738         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1739         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1740         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1741         { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1742         { }
1743 };
1744
1745 /**
1746  * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1747  *
1748  * There are composite devices for which we want to ignore only a certain
1749  * interface. This is a list of devices for which only the mouse interface will
1750  * be ignored. This allows a dedicated driver to take care of the interface.
1751  */
1752 static const struct hid_device_id hid_mouse_ignore_list[] = {
1753         /* appletouch driver */
1754         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1755         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1756         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1757         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1758         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1759         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1760         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1761         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1762         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1763         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1764         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1765         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1766         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1767         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1768         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1769         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1770         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1771         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1772         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1773         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1774         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1775         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1776         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1777         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
1778         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
1779         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
1780         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
1781         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
1782         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
1783         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1784         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1785         { }
1786 };
1787
1788 static bool hid_ignore(struct hid_device *hdev)
1789 {
1790         switch (hdev->vendor) {
1791         case USB_VENDOR_ID_CODEMERCS:
1792                 /* ignore all Code Mercenaries IOWarrior devices */
1793                 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1794                                 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1795                         return true;
1796                 break;
1797         case USB_VENDOR_ID_LOGITECH:
1798                 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1799                                 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1800                         return true;
1801                 break;
1802         case USB_VENDOR_ID_SOUNDGRAPH:
1803                 if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
1804                     hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
1805                         return true;
1806                 break;
1807         }
1808
1809         if (hdev->type == HID_TYPE_USBMOUSE &&
1810                         hid_match_id(hdev, hid_mouse_ignore_list))
1811                 return true;
1812
1813         return !!hid_match_id(hdev, hid_ignore_list);
1814 }
1815
1816 int hid_add_device(struct hid_device *hdev)
1817 {
1818         static atomic_t id = ATOMIC_INIT(0);
1819         int ret;
1820
1821         if (WARN_ON(hdev->status & HID_STAT_ADDED))
1822                 return -EBUSY;
1823
1824         /* we need to kill them here, otherwise they will stay allocated to
1825          * wait for coming driver */
1826         if (!(hdev->quirks & HID_QUIRK_NO_IGNORE)
1827             && (hid_ignore(hdev) || (hdev->quirks & HID_QUIRK_IGNORE)))
1828                 return -ENODEV;
1829
1830         /* XXX hack, any other cleaner solution after the driver core
1831          * is converted to allow more than 20 bytes as the device name? */
1832         dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
1833                      hdev->vendor, hdev->product, atomic_inc_return(&id));
1834
1835         hid_debug_register(hdev, dev_name(&hdev->dev));
1836         ret = device_add(&hdev->dev);
1837         if (!ret)
1838                 hdev->status |= HID_STAT_ADDED;
1839         else
1840                 hid_debug_unregister(hdev);
1841
1842         return ret;
1843 }
1844 EXPORT_SYMBOL_GPL(hid_add_device);
1845
1846 /**
1847  * hid_allocate_device - allocate new hid device descriptor
1848  *
1849  * Allocate and initialize hid device, so that hid_destroy_device might be
1850  * used to free it.
1851  *
1852  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1853  * error value.
1854  */
1855 struct hid_device *hid_allocate_device(void)
1856 {
1857         struct hid_device *hdev;
1858         unsigned int i;
1859         int ret = -ENOMEM;
1860
1861         hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1862         if (hdev == NULL)
1863                 return ERR_PTR(ret);
1864
1865         device_initialize(&hdev->dev);
1866         hdev->dev.release = hid_device_release;
1867         hdev->dev.bus = &hid_bus_type;
1868
1869         hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1870                         sizeof(struct hid_collection), GFP_KERNEL);
1871         if (hdev->collection == NULL)
1872                 goto err;
1873         hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1874
1875         for (i = 0; i < HID_REPORT_TYPES; i++)
1876                 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1877
1878         init_waitqueue_head(&hdev->debug_wait);
1879         INIT_LIST_HEAD(&hdev->debug_list);
1880
1881         return hdev;
1882 err:
1883         put_device(&hdev->dev);
1884         return ERR_PTR(ret);
1885 }
1886 EXPORT_SYMBOL_GPL(hid_allocate_device);
1887
1888 static void hid_remove_device(struct hid_device *hdev)
1889 {
1890         if (hdev->status & HID_STAT_ADDED) {
1891                 device_del(&hdev->dev);
1892                 hid_debug_unregister(hdev);
1893                 hdev->status &= ~HID_STAT_ADDED;
1894         }
1895 }
1896
1897 /**
1898  * hid_destroy_device - free previously allocated device
1899  *
1900  * @hdev: hid device
1901  *
1902  * If you allocate hid_device through hid_allocate_device, you should ever
1903  * free by this function.
1904  */
1905 void hid_destroy_device(struct hid_device *hdev)
1906 {
1907         hid_remove_device(hdev);
1908         put_device(&hdev->dev);
1909 }
1910 EXPORT_SYMBOL_GPL(hid_destroy_device);
1911
1912 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1913                 const char *mod_name)
1914 {
1915         int ret;
1916
1917         hdrv->driver.name = hdrv->name;
1918         hdrv->driver.bus = &hid_bus_type;
1919         hdrv->driver.owner = owner;
1920         hdrv->driver.mod_name = mod_name;
1921
1922         INIT_LIST_HEAD(&hdrv->dyn_list);
1923         spin_lock_init(&hdrv->dyn_lock);
1924
1925         ret = driver_register(&hdrv->driver);
1926         if (ret)
1927                 return ret;
1928
1929         ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
1930         if (ret)
1931                 driver_unregister(&hdrv->driver);
1932
1933         return ret;
1934 }
1935 EXPORT_SYMBOL_GPL(__hid_register_driver);
1936
1937 void hid_unregister_driver(struct hid_driver *hdrv)
1938 {
1939         driver_remove_file(&hdrv->driver, &driver_attr_new_id);
1940         driver_unregister(&hdrv->driver);
1941         hid_free_dynids(hdrv);
1942 }
1943 EXPORT_SYMBOL_GPL(hid_unregister_driver);
1944
1945 int hid_check_keys_pressed(struct hid_device *hid)
1946 {
1947         struct hid_input *hidinput;
1948         int i;
1949
1950         if (!(hid->claimed & HID_CLAIMED_INPUT))
1951                 return 0;
1952
1953         list_for_each_entry(hidinput, &hid->inputs, list) {
1954                 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
1955                         if (hidinput->input->key[i])
1956                                 return 1;
1957         }
1958
1959         return 0;
1960 }
1961
1962 EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
1963
1964 static int __init hid_init(void)
1965 {
1966         int ret;
1967
1968         if (hid_debug)
1969                 pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
1970                         "debugfs is now used for inspecting the device (report descriptor, reports)\n");
1971
1972         ret = bus_register(&hid_bus_type);
1973         if (ret) {
1974                 pr_err("can't register hid bus\n");
1975                 goto err;
1976         }
1977
1978         ret = hidraw_init();
1979         if (ret)
1980                 goto err_bus;
1981
1982         hid_debug_init();
1983
1984         return 0;
1985 err_bus:
1986         bus_unregister(&hid_bus_type);
1987 err:
1988         return ret;
1989 }
1990
1991 static void __exit hid_exit(void)
1992 {
1993         hid_debug_exit();
1994         hidraw_exit();
1995         bus_unregister(&hid_bus_type);
1996 }
1997
1998 module_init(hid_init);
1999 module_exit(hid_exit);
2000
2001 MODULE_AUTHOR("Andreas Gal");
2002 MODULE_AUTHOR("Vojtech Pavlik");
2003 MODULE_AUTHOR("Jiri Kosina");
2004 MODULE_LICENSE(DRIVER_LICENSE);
2005