Blackfin arch: cleanup bf54x ifdef mess in gpio code
[linux-2.6/mini2440.git] / drivers / hid / hid-core.c
blob6cad69ed21c558c908b68fb9751a381a92cf0eae
1 /*
2 * HID support for Linux
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 */
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.
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>
31 #include <linux/hid.h>
32 #include <linux/hiddev.h>
33 #include <linux/hid-debug.h>
34 #include <linux/hidraw.h>
36 #include "hid-ids.h"
39 * Version Information
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"
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
55 * Register a new report for a device.
58 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
60 struct hid_report_enum *report_enum = device->report_enum + type;
61 struct hid_report *report;
63 if (report_enum->report_id_hash[id])
64 return report_enum->report_id_hash[id];
66 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
67 return NULL;
69 if (id != 0)
70 report_enum->numbered = 1;
72 report->id = id;
73 report->type = type;
74 report->size = 0;
75 report->device = device;
76 report_enum->report_id_hash[id] = report;
78 list_add_tail(&report->list, &report_enum->report_list);
80 return report;
84 * Register a new field for this report.
87 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
89 struct hid_field *field;
91 if (report->maxfield == HID_MAX_FIELDS) {
92 dbg_hid("too many fields in report\n");
93 return NULL;
96 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
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;
105 return field;
109 * Open a collection. The type/usage is pushed on the stack.
112 static int open_collection(struct hid_parser *parser, unsigned type)
114 struct hid_collection *collection;
115 unsigned usage;
117 usage = parser->local.usage[0];
119 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
120 dbg_hid("collection stack overflow\n");
121 return -1;
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;
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;
142 parser->collection_stack[parser->collection_stack_ptr++] =
143 parser->device->maxcollection;
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;
151 if (type == HID_COLLECTION_APPLICATION)
152 parser->device->maxapplication++;
154 return 0;
158 * Close a collection.
161 static int close_collection(struct hid_parser *parser)
163 if (!parser->collection_stack_ptr) {
164 dbg_hid("collection stack underflow\n");
165 return -1;
167 parser->collection_stack_ptr--;
168 return 0;
172 * Climb up the stack, search for the specified collection type
173 * and return the usage.
176 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
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 */
186 * Add a usage to the temporary parser table.
189 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
191 if (parser->local.usage_index >= HID_MAX_USAGES) {
192 dbg_hid("usage index exceeded\n");
193 return -1;
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;
204 * Register a new field for this report.
207 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
209 struct hid_report *report;
210 struct hid_field *field;
211 int usages;
212 unsigned offset;
213 int i;
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;
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;
225 offset = report->size;
226 report->size += parser->global.report_size * parser->global.report_count;
228 if (!parser->local.usage_index) /* Ignore padding fields */
229 return 0;
231 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
233 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
234 return 0;
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);
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];
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;
263 return 0;
267 * Read data value from item.
270 static u32 item_udata(struct hid_item *item)
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;
277 return 0;
280 static s32 item_sdata(struct hid_item *item)
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;
287 return 0;
291 * Process a global item.
294 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
296 switch (item->tag) {
297 case HID_GLOBAL_ITEM_TAG_PUSH:
299 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
300 dbg_hid("global enviroment stack overflow\n");
301 return -1;
304 memcpy(parser->global_stack + parser->global_stack_ptr++,
305 &parser->global, sizeof(struct hid_global));
306 return 0;
308 case HID_GLOBAL_ITEM_TAG_POP:
310 if (!parser->global_stack_ptr) {
311 dbg_hid("global enviroment stack underflow\n");
312 return -1;
315 memcpy(&parser->global, parser->global_stack +
316 --parser->global_stack_ptr, sizeof(struct hid_global));
317 return 0;
319 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
320 parser->global.usage_page = item_udata(item);
321 return 0;
323 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
324 parser->global.logical_minimum = item_sdata(item);
325 return 0;
327 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
328 if (parser->global.logical_minimum < 0)
329 parser->global.logical_maximum = item_sdata(item);
330 else
331 parser->global.logical_maximum = item_udata(item);
332 return 0;
334 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
335 parser->global.physical_minimum = item_sdata(item);
336 return 0;
338 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
339 if (parser->global.physical_minimum < 0)
340 parser->global.physical_maximum = item_sdata(item);
341 else
342 parser->global.physical_maximum = item_udata(item);
343 return 0;
345 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
346 parser->global.unit_exponent = item_sdata(item);
347 return 0;
349 case HID_GLOBAL_ITEM_TAG_UNIT:
350 parser->global.unit = item_udata(item);
351 return 0;
353 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
354 parser->global.report_size = item_udata(item);
355 if (parser->global.report_size > 32) {
356 dbg_hid("invalid report_size %d\n",
357 parser->global.report_size);
358 return -1;
360 return 0;
362 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
363 parser->global.report_count = item_udata(item);
364 if (parser->global.report_count > HID_MAX_USAGES) {
365 dbg_hid("invalid report_count %d\n",
366 parser->global.report_count);
367 return -1;
369 return 0;
371 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
372 parser->global.report_id = item_udata(item);
373 if (parser->global.report_id == 0) {
374 dbg_hid("report_id 0 is invalid\n");
375 return -1;
377 return 0;
379 default:
380 dbg_hid("unknown global tag 0x%x\n", item->tag);
381 return -1;
386 * Process a local item.
389 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
391 __u32 data;
392 unsigned n;
394 if (item->size == 0) {
395 dbg_hid("item data expected for local item\n");
396 return -1;
399 data = item_udata(item);
401 switch (item->tag) {
402 case HID_LOCAL_ITEM_TAG_DELIMITER:
404 if (data) {
406 * We treat items before the first delimiter
407 * as global to all usage sets (branch 0).
408 * In the moment we process only these global
409 * items and the first delimiter set.
411 if (parser->local.delimiter_depth != 0) {
412 dbg_hid("nested delimiters\n");
413 return -1;
415 parser->local.delimiter_depth++;
416 parser->local.delimiter_branch++;
417 } else {
418 if (parser->local.delimiter_depth < 1) {
419 dbg_hid("bogus close delimiter\n");
420 return -1;
422 parser->local.delimiter_depth--;
424 return 1;
426 case HID_LOCAL_ITEM_TAG_USAGE:
428 if (parser->local.delimiter_branch > 1) {
429 dbg_hid("alternative usage ignored\n");
430 return 0;
433 if (item->size <= 2)
434 data = (parser->global.usage_page << 16) + data;
436 return hid_add_usage(parser, data);
438 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
440 if (parser->local.delimiter_branch > 1) {
441 dbg_hid("alternative usage ignored\n");
442 return 0;
445 if (item->size <= 2)
446 data = (parser->global.usage_page << 16) + data;
448 parser->local.usage_minimum = data;
449 return 0;
451 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
453 if (parser->local.delimiter_branch > 1) {
454 dbg_hid("alternative usage ignored\n");
455 return 0;
458 if (item->size <= 2)
459 data = (parser->global.usage_page << 16) + data;
461 for (n = parser->local.usage_minimum; n <= data; n++)
462 if (hid_add_usage(parser, n)) {
463 dbg_hid("hid_add_usage failed\n");
464 return -1;
466 return 0;
468 default:
470 dbg_hid("unknown local item tag 0x%x\n", item->tag);
471 return 0;
473 return 0;
477 * Process a main item.
480 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
482 __u32 data;
483 int ret;
485 data = item_udata(item);
487 switch (item->tag) {
488 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
489 ret = open_collection(parser, data & 0xff);
490 break;
491 case HID_MAIN_ITEM_TAG_END_COLLECTION:
492 ret = close_collection(parser);
493 break;
494 case HID_MAIN_ITEM_TAG_INPUT:
495 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
496 break;
497 case HID_MAIN_ITEM_TAG_OUTPUT:
498 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
499 break;
500 case HID_MAIN_ITEM_TAG_FEATURE:
501 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
502 break;
503 default:
504 dbg_hid("unknown main item tag 0x%x\n", item->tag);
505 ret = 0;
508 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
510 return ret;
514 * Process a reserved item.
517 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
519 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
520 return 0;
524 * Free a report and all registered fields. The field->usage and
525 * field->value table's are allocated behind the field, so we need
526 * only to free(field) itself.
529 static void hid_free_report(struct hid_report *report)
531 unsigned n;
533 for (n = 0; n < report->maxfield; n++)
534 kfree(report->field[n]);
535 kfree(report);
539 * Free a device structure, all reports, and all fields.
542 static void hid_device_release(struct device *dev)
544 struct hid_device *device = container_of(dev, struct hid_device, dev);
545 unsigned i, j;
547 for (i = 0; i < HID_REPORT_TYPES; i++) {
548 struct hid_report_enum *report_enum = device->report_enum + i;
550 for (j = 0; j < 256; j++) {
551 struct hid_report *report = report_enum->report_id_hash[j];
552 if (report)
553 hid_free_report(report);
557 kfree(device->rdesc);
558 kfree(device->collection);
559 kfree(device);
563 * Fetch a report description item from the data stream. We support long
564 * items, though they are not used yet.
567 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
569 u8 b;
571 if ((end - start) <= 0)
572 return NULL;
574 b = *start++;
576 item->type = (b >> 2) & 3;
577 item->tag = (b >> 4) & 15;
579 if (item->tag == HID_ITEM_TAG_LONG) {
581 item->format = HID_ITEM_FORMAT_LONG;
583 if ((end - start) < 2)
584 return NULL;
586 item->size = *start++;
587 item->tag = *start++;
589 if ((end - start) < item->size)
590 return NULL;
592 item->data.longdata = start;
593 start += item->size;
594 return start;
597 item->format = HID_ITEM_FORMAT_SHORT;
598 item->size = b & 3;
600 switch (item->size) {
601 case 0:
602 return start;
604 case 1:
605 if ((end - start) < 1)
606 return NULL;
607 item->data.u8 = *start++;
608 return start;
610 case 2:
611 if ((end - start) < 2)
612 return NULL;
613 item->data.u16 = get_unaligned_le16(start);
614 start = (__u8 *)((__le16 *)start + 1);
615 return start;
617 case 3:
618 item->size++;
619 if ((end - start) < 4)
620 return NULL;
621 item->data.u32 = get_unaligned_le32(start);
622 start = (__u8 *)((__le32 *)start + 1);
623 return start;
626 return NULL;
630 * hid_parse_report - parse device report
632 * @device: hid device
633 * @start: report start
634 * @size: report size
636 * Parse a report description into a hid_device structure. Reports are
637 * enumerated, fields are attached to these reports.
638 * 0 returned on success, otherwise nonzero error value.
640 int hid_parse_report(struct hid_device *device, __u8 *start,
641 unsigned size)
643 struct hid_parser *parser;
644 struct hid_item item;
645 __u8 *end;
646 int ret;
647 static int (*dispatch_type[])(struct hid_parser *parser,
648 struct hid_item *item) = {
649 hid_parser_main,
650 hid_parser_global,
651 hid_parser_local,
652 hid_parser_reserved
655 if (device->driver->report_fixup)
656 device->driver->report_fixup(device, start, size);
658 device->rdesc = kmalloc(size, GFP_KERNEL);
659 if (device->rdesc == NULL)
660 return -ENOMEM;
661 memcpy(device->rdesc, start, size);
662 device->rsize = size;
664 parser = vmalloc(sizeof(struct hid_parser));
665 if (!parser) {
666 ret = -ENOMEM;
667 goto err;
670 memset(parser, 0, sizeof(struct hid_parser));
671 parser->device = device;
673 end = start + size;
674 ret = -EINVAL;
675 while ((start = fetch_item(start, end, &item)) != NULL) {
677 if (item.format != HID_ITEM_FORMAT_SHORT) {
678 dbg_hid("unexpected long global item\n");
679 goto err;
682 if (dispatch_type[item.type](parser, &item)) {
683 dbg_hid("item %u %u %u %u parsing failed\n",
684 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
685 goto err;
688 if (start == end) {
689 if (parser->collection_stack_ptr) {
690 dbg_hid("unbalanced collection at end of report description\n");
691 goto err;
693 if (parser->local.delimiter_depth) {
694 dbg_hid("unbalanced delimiter at end of report description\n");
695 goto err;
697 vfree(parser);
698 return 0;
702 dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
703 err:
704 vfree(parser);
705 return ret;
707 EXPORT_SYMBOL_GPL(hid_parse_report);
710 * Convert a signed n-bit integer to signed 32-bit integer. Common
711 * cases are done through the compiler, the screwed things has to be
712 * done by hand.
715 static s32 snto32(__u32 value, unsigned n)
717 switch (n) {
718 case 8: return ((__s8)value);
719 case 16: return ((__s16)value);
720 case 32: return ((__s32)value);
722 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
726 * Convert a signed 32-bit integer to a signed n-bit integer.
729 static u32 s32ton(__s32 value, unsigned n)
731 s32 a = value >> (n - 1);
732 if (a && a != -1)
733 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
734 return value & ((1 << n) - 1);
738 * Extract/implement a data field from/to a little endian report (bit array).
740 * Code sort-of follows HID spec:
741 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
743 * While the USB HID spec allows unlimited length bit fields in "report
744 * descriptors", most devices never use more than 16 bits.
745 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
746 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
749 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
751 u64 x;
753 if (n > 32)
754 printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
755 n, current->comm);
757 report += offset >> 3; /* adjust byte index */
758 offset &= 7; /* now only need bit offset into one byte */
759 x = get_unaligned_le64(report);
760 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
761 return (u32) x;
765 * "implement" : set bits in a little endian bit stream.
766 * Same concepts as "extract" (see comments above).
767 * The data mangled in the bit stream remains in little endian
768 * order the whole time. It make more sense to talk about
769 * endianness of register values by considering a register
770 * a "cached" copy of the little endiad bit stream.
772 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
774 u64 x;
775 u64 m = (1ULL << n) - 1;
777 if (n > 32)
778 printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
779 n, current->comm);
781 if (value > m)
782 printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
783 value, current->comm);
784 WARN_ON(value > m);
785 value &= m;
787 report += offset >> 3;
788 offset &= 7;
790 x = get_unaligned_le64(report);
791 x &= ~(m << offset);
792 x |= ((u64)value) << offset;
793 put_unaligned_le64(x, report);
797 * Search an array for a value.
800 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
802 while (n--) {
803 if (*array++ == value)
804 return 0;
806 return -1;
810 * hid_match_report - check if driver's raw_event should be called
812 * @hid: hid device
813 * @report_type: type to match against
815 * compare hid->driver->report_table->report_type to report->type
817 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
819 const struct hid_report_id *id = hid->driver->report_table;
821 if (!id) /* NULL means all */
822 return 1;
824 for (; id->report_type != HID_TERMINATOR; id++)
825 if (id->report_type == HID_ANY_ID ||
826 id->report_type == report->type)
827 return 1;
828 return 0;
832 * hid_match_usage - check if driver's event should be called
834 * @hid: hid device
835 * @usage: usage to match against
837 * compare hid->driver->usage_table->usage_{type,code} to
838 * usage->usage_{type,code}
840 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
842 const struct hid_usage_id *id = hid->driver->usage_table;
844 if (!id) /* NULL means all */
845 return 1;
847 for (; id->usage_type != HID_ANY_ID - 1; id++)
848 if ((id->usage_hid == HID_ANY_ID ||
849 id->usage_hid == usage->hid) &&
850 (id->usage_type == HID_ANY_ID ||
851 id->usage_type == usage->type) &&
852 (id->usage_code == HID_ANY_ID ||
853 id->usage_code == usage->code))
854 return 1;
855 return 0;
858 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
859 struct hid_usage *usage, __s32 value, int interrupt)
861 struct hid_driver *hdrv = hid->driver;
862 int ret;
864 hid_dump_input(usage, value);
866 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
867 ret = hdrv->event(hid, field, usage, value);
868 if (ret != 0) {
869 if (ret < 0)
870 dbg_hid("%s's event failed with %d\n",
871 hdrv->name, ret);
872 return;
876 if (hid->claimed & HID_CLAIMED_INPUT)
877 hidinput_hid_event(hid, field, usage, value);
878 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
879 hid->hiddev_hid_event(hid, field, usage, value);
883 * Analyse a received field, and fetch the data from it. The field
884 * content is stored for next report processing (we do differential
885 * reporting to the layer).
888 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
889 __u8 *data, int interrupt)
891 unsigned n;
892 unsigned count = field->report_count;
893 unsigned offset = field->report_offset;
894 unsigned size = field->report_size;
895 __s32 min = field->logical_minimum;
896 __s32 max = field->logical_maximum;
897 __s32 *value;
899 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
900 return;
902 for (n = 0; n < count; n++) {
904 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
905 extract(data, offset + n * size, size);
907 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
908 && value[n] >= min && value[n] <= max
909 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
910 goto exit;
913 for (n = 0; n < count; n++) {
915 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
916 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
917 continue;
920 if (field->value[n] >= min && field->value[n] <= max
921 && field->usage[field->value[n] - min].hid
922 && search(value, field->value[n], count))
923 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
925 if (value[n] >= min && value[n] <= max
926 && field->usage[value[n] - min].hid
927 && search(field->value, value[n], count))
928 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
931 memcpy(field->value, value, count * sizeof(__s32));
932 exit:
933 kfree(value);
937 * Output the field into the report.
940 static void hid_output_field(struct hid_field *field, __u8 *data)
942 unsigned count = field->report_count;
943 unsigned offset = field->report_offset;
944 unsigned size = field->report_size;
945 unsigned bitsused = offset + count * size;
946 unsigned n;
948 /* make sure the unused bits in the last byte are zeros */
949 if (count > 0 && size > 0 && (bitsused % 8) != 0)
950 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
952 for (n = 0; n < count; n++) {
953 if (field->logical_minimum < 0) /* signed values */
954 implement(data, offset + n * size, size, s32ton(field->value[n], size));
955 else /* unsigned values */
956 implement(data, offset + n * size, size, field->value[n]);
961 * Create a report.
964 void hid_output_report(struct hid_report *report, __u8 *data)
966 unsigned n;
968 if (report->id > 0)
969 *data++ = report->id;
971 for (n = 0; n < report->maxfield; n++)
972 hid_output_field(report->field[n], data);
974 EXPORT_SYMBOL_GPL(hid_output_report);
977 * Set a field value. The report this field belongs to has to be
978 * created and transferred to the device, to set this value in the
979 * device.
982 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
984 unsigned size = field->report_size;
986 hid_dump_input(field->usage + offset, value);
988 if (offset >= field->report_count) {
989 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
990 hid_dump_field(field, 8);
991 return -1;
993 if (field->logical_minimum < 0) {
994 if (value != snto32(s32ton(value, size), size)) {
995 dbg_hid("value %d is out of range\n", value);
996 return -1;
999 field->value[offset] = value;
1000 return 0;
1002 EXPORT_SYMBOL_GPL(hid_set_field);
1004 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1005 const u8 *data)
1007 struct hid_report *report;
1008 unsigned int n = 0; /* Normally report number is 0 */
1010 /* Device uses numbered reports, data[0] is report number */
1011 if (report_enum->numbered)
1012 n = *data;
1014 report = report_enum->report_id_hash[n];
1015 if (report == NULL)
1016 dbg_hid("undefined report_id %u received\n", n);
1018 return report;
1021 void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1022 int interrupt)
1024 struct hid_report_enum *report_enum = hid->report_enum + type;
1025 struct hid_report *report;
1026 unsigned int a;
1027 int rsize, csize = size;
1028 u8 *cdata = data;
1030 report = hid_get_report(report_enum, data);
1031 if (!report)
1032 return;
1034 if (report_enum->numbered) {
1035 cdata++;
1036 csize--;
1039 rsize = ((report->size - 1) >> 3) + 1;
1041 if (csize < rsize) {
1042 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1043 csize, rsize);
1044 memset(cdata + csize, 0, rsize - csize);
1047 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1048 hid->hiddev_report_event(hid, report);
1049 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1050 /* numbered reports need to be passed with the report num */
1051 if (report_enum->numbered)
1052 hidraw_report_event(hid, data - 1, size + 1);
1053 else
1054 hidraw_report_event(hid, data, size);
1057 for (a = 0; a < report->maxfield; a++)
1058 hid_input_field(hid, report->field[a], cdata, interrupt);
1060 if (hid->claimed & HID_CLAIMED_INPUT)
1061 hidinput_report_event(hid, report);
1063 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1066 * hid_input_report - report data from lower layer (usb, bt...)
1068 * @hid: hid device
1069 * @type: HID report type (HID_*_REPORT)
1070 * @data: report contents
1071 * @size: size of data parameter
1072 * @interrupt: called from atomic?
1074 * This is data entry for lower layers.
1076 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1078 struct hid_report_enum *report_enum = hid->report_enum + type;
1079 struct hid_driver *hdrv = hid->driver;
1080 struct hid_report *report;
1081 unsigned int i;
1082 int ret;
1084 if (!hid || !hid->driver)
1085 return -ENODEV;
1087 if (!size) {
1088 dbg_hid("empty report\n");
1089 return -1;
1092 dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1094 report = hid_get_report(report_enum, data);
1095 if (!report)
1096 return -1;
1098 /* dump the report */
1099 dbg_hid("report %d (size %u) = ", report->id, size);
1100 for (i = 0; i < size; i++)
1101 dbg_hid_line(" %02x", data[i]);
1102 dbg_hid_line("\n");
1104 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1105 ret = hdrv->raw_event(hid, report, data, size);
1106 if (ret != 0)
1107 return ret < 0 ? ret : 0;
1110 hid_report_raw_event(hid, type, data, size, interrupt);
1112 return 0;
1114 EXPORT_SYMBOL_GPL(hid_input_report);
1116 static bool hid_match_one_id(struct hid_device *hdev,
1117 const struct hid_device_id *id)
1119 return id->bus == hdev->bus &&
1120 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1121 (id->product == HID_ANY_ID || id->product == hdev->product);
1124 static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1125 const struct hid_device_id *id)
1127 for (; id->bus; id++)
1128 if (hid_match_one_id(hdev, id))
1129 return id;
1131 return NULL;
1134 static const struct hid_device_id hid_hiddev_list[] = {
1135 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1136 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1140 static bool hid_hiddev(struct hid_device *hdev)
1142 return !!hid_match_id(hdev, hid_hiddev_list);
1145 int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1147 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1148 "Joystick", "Gamepad", "Keyboard", "Keypad",
1149 "Multi-Axis Controller"
1151 const char *type, *bus;
1152 char buf[64];
1153 unsigned int i;
1154 int len;
1156 if (hdev->bus != BUS_USB)
1157 connect_mask &= ~HID_CONNECT_HIDDEV;
1158 if (hid_hiddev(hdev))
1159 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1161 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1162 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1163 hdev->claimed |= HID_CLAIMED_INPUT;
1164 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1165 !hdev->hiddev_connect(hdev,
1166 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1167 hdev->claimed |= HID_CLAIMED_HIDDEV;
1168 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1169 hdev->claimed |= HID_CLAIMED_HIDRAW;
1171 if (!hdev->claimed) {
1172 dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
1173 "hidraw\n");
1174 return -ENODEV;
1177 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1178 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1179 hdev->ff_init(hdev);
1181 len = 0;
1182 if (hdev->claimed & HID_CLAIMED_INPUT)
1183 len += sprintf(buf + len, "input");
1184 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1185 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1186 hdev->minor);
1187 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1188 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1189 ((struct hidraw *)hdev->hidraw)->minor);
1191 type = "Device";
1192 for (i = 0; i < hdev->maxcollection; i++) {
1193 struct hid_collection *col = &hdev->collection[i];
1194 if (col->type == HID_COLLECTION_APPLICATION &&
1195 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1196 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1197 type = types[col->usage & 0xffff];
1198 break;
1202 switch (hdev->bus) {
1203 case BUS_USB:
1204 bus = "USB";
1205 break;
1206 case BUS_BLUETOOTH:
1207 bus = "BLUETOOTH";
1208 break;
1209 default:
1210 bus = "<UNKNOWN>";
1213 dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1214 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1215 type, hdev->name, hdev->phys);
1217 return 0;
1219 EXPORT_SYMBOL_GPL(hid_connect);
1221 /* a list of devices for which there is a specialized driver on HID bus */
1222 static const struct hid_device_id hid_blacklist[] = {
1223 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
1224 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1225 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
1226 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1227 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1228 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1229 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1230 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1231 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1232 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1233 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1234 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1235 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1236 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1237 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1238 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1239 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1240 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1241 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1242 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1243 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1244 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1245 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1246 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1247 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
1248 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1249 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1250 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1251 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1252 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1253 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1254 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1255 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1256 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1257 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1258 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1259 { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
1260 { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1261 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1262 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
1263 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1264 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
1265 { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
1266 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1267 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
1268 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
1269 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1270 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
1271 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1272 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
1273 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1274 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1275 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1276 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1277 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1278 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1279 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
1280 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1281 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
1282 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1283 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1284 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1285 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1286 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1287 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1288 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1289 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1290 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
1291 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
1292 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
1293 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
1294 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1295 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
1296 { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
1297 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
1298 { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1299 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1300 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1301 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
1302 { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1303 { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
1305 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
1306 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
1310 struct hid_dynid {
1311 struct list_head list;
1312 struct hid_device_id id;
1316 * store_new_id - add a new HID device ID to this driver and re-probe devices
1317 * @driver: target device driver
1318 * @buf: buffer for scanning device ID data
1319 * @count: input size
1321 * Adds a new dynamic hid device ID to this driver,
1322 * and causes the driver to probe for all devices again.
1324 static ssize_t store_new_id(struct device_driver *drv, const char *buf,
1325 size_t count)
1327 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1328 struct hid_dynid *dynid;
1329 __u32 bus, vendor, product;
1330 unsigned long driver_data = 0;
1331 int ret;
1333 ret = sscanf(buf, "%x %x %x %lx",
1334 &bus, &vendor, &product, &driver_data);
1335 if (ret < 3)
1336 return -EINVAL;
1338 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1339 if (!dynid)
1340 return -ENOMEM;
1342 dynid->id.bus = bus;
1343 dynid->id.vendor = vendor;
1344 dynid->id.product = product;
1345 dynid->id.driver_data = driver_data;
1347 spin_lock(&hdrv->dyn_lock);
1348 list_add_tail(&dynid->list, &hdrv->dyn_list);
1349 spin_unlock(&hdrv->dyn_lock);
1351 ret = 0;
1352 if (get_driver(&hdrv->driver)) {
1353 ret = driver_attach(&hdrv->driver);
1354 put_driver(&hdrv->driver);
1357 return ret ? : count;
1359 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1361 static void hid_free_dynids(struct hid_driver *hdrv)
1363 struct hid_dynid *dynid, *n;
1365 spin_lock(&hdrv->dyn_lock);
1366 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1367 list_del(&dynid->list);
1368 kfree(dynid);
1370 spin_unlock(&hdrv->dyn_lock);
1373 static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1374 struct hid_driver *hdrv)
1376 struct hid_dynid *dynid;
1378 spin_lock(&hdrv->dyn_lock);
1379 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1380 if (hid_match_one_id(hdev, &dynid->id)) {
1381 spin_unlock(&hdrv->dyn_lock);
1382 return &dynid->id;
1385 spin_unlock(&hdrv->dyn_lock);
1387 return hid_match_id(hdev, hdrv->id_table);
1390 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1392 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1393 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1395 if (!hid_match_device(hdev, hdrv))
1396 return 0;
1398 /* generic wants all non-blacklisted */
1399 if (!strncmp(hdrv->name, "generic-", 8))
1400 return !hid_match_id(hdev, hid_blacklist);
1402 return 1;
1405 static int hid_device_probe(struct device *dev)
1407 struct hid_driver *hdrv = container_of(dev->driver,
1408 struct hid_driver, driver);
1409 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1410 const struct hid_device_id *id;
1411 int ret = 0;
1413 if (!hdev->driver) {
1414 id = hid_match_device(hdev, hdrv);
1415 if (id == NULL)
1416 return -ENODEV;
1418 hdev->driver = hdrv;
1419 if (hdrv->probe) {
1420 ret = hdrv->probe(hdev, id);
1421 } else { /* default probe */
1422 ret = hid_parse(hdev);
1423 if (!ret)
1424 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1426 if (ret)
1427 hdev->driver = NULL;
1429 return ret;
1432 static int hid_device_remove(struct device *dev)
1434 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1435 struct hid_driver *hdrv = hdev->driver;
1437 if (hdrv) {
1438 if (hdrv->remove)
1439 hdrv->remove(hdev);
1440 else /* default remove */
1441 hid_hw_stop(hdev);
1442 hdev->driver = NULL;
1445 return 0;
1448 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1450 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1452 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1453 hdev->bus, hdev->vendor, hdev->product))
1454 return -ENOMEM;
1456 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1457 return -ENOMEM;
1459 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1460 return -ENOMEM;
1462 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1463 return -ENOMEM;
1465 if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1466 hdev->bus, hdev->vendor, hdev->product))
1467 return -ENOMEM;
1469 return 0;
1472 static struct bus_type hid_bus_type = {
1473 .name = "hid",
1474 .match = hid_bus_match,
1475 .probe = hid_device_probe,
1476 .remove = hid_device_remove,
1477 .uevent = hid_uevent,
1480 /* a list of devices that shouldn't be handled by HID core at all */
1481 static const struct hid_device_id hid_ignore_list[] = {
1482 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1483 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1484 { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1485 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1486 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1487 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1488 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1489 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1490 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1491 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1492 { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1493 { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1494 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1495 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2)},
1496 { HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1497 { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1498 { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1499 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1500 { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1501 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1502 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1503 { HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
1504 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1505 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1506 { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1507 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1508 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1509 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1510 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1511 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1512 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1513 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1514 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1515 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1516 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1517 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1518 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1519 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1520 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1521 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1522 { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1523 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1524 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1525 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1526 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1527 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1528 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1529 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1530 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1531 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1532 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1533 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1534 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1535 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1536 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1537 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1538 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1539 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1540 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1541 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1542 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1543 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1544 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1545 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1546 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1547 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1548 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1549 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1550 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1551 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1552 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1553 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1554 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1555 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1556 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1557 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1558 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1559 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1560 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1561 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1562 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1563 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1564 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1565 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1566 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1567 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1568 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1569 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1570 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1571 { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1572 { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1573 { HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
1574 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1575 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1576 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1577 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1578 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1579 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1580 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1581 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1582 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1583 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1584 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1585 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1586 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1587 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1588 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1589 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1590 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1591 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1592 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1593 { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1594 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1595 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1596 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1597 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1598 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1599 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1600 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1601 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1602 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1603 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1604 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1605 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1606 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1607 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1608 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD) },
1609 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD2) },
1610 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD3) },
1611 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD4) },
1612 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD5) },
1613 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY1) },
1614 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY2) },
1615 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1616 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
1617 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1618 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1619 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1620 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1621 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1622 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1623 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1624 { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1625 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1626 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1627 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1628 { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1629 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1630 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1635 * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1637 * There are composite devices for which we want to ignore only a certain
1638 * interface. This is a list of devices for which only the mouse interface will
1639 * be ignored. This allows a dedicated driver to take care of the interface.
1641 static const struct hid_device_id hid_mouse_ignore_list[] = {
1642 /* appletouch driver */
1643 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1644 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1645 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1646 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1647 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1648 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1649 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1650 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1651 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1652 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1653 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1654 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1655 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1656 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1657 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1658 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1659 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1660 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1661 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1662 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1663 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1664 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1665 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1666 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1667 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1671 static bool hid_ignore(struct hid_device *hdev)
1673 switch (hdev->vendor) {
1674 case USB_VENDOR_ID_CODEMERCS:
1675 /* ignore all Code Mercenaries IOWarrior devices */
1676 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1677 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1678 return true;
1679 break;
1680 case USB_VENDOR_ID_LOGITECH:
1681 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1682 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1683 return true;
1684 break;
1687 if (hdev->type == HID_TYPE_USBMOUSE &&
1688 hid_match_id(hdev, hid_mouse_ignore_list))
1689 return true;
1691 return !!hid_match_id(hdev, hid_ignore_list);
1694 int hid_add_device(struct hid_device *hdev)
1696 static atomic_t id = ATOMIC_INIT(0);
1697 int ret;
1699 if (WARN_ON(hdev->status & HID_STAT_ADDED))
1700 return -EBUSY;
1702 /* we need to kill them here, otherwise they will stay allocated to
1703 * wait for coming driver */
1704 if (hid_ignore(hdev))
1705 return -ENODEV;
1707 /* XXX hack, any other cleaner solution after the driver core
1708 * is converted to allow more than 20 bytes as the device name? */
1709 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
1710 hdev->vendor, hdev->product, atomic_inc_return(&id));
1712 ret = device_add(&hdev->dev);
1713 if (!ret)
1714 hdev->status |= HID_STAT_ADDED;
1716 return ret;
1718 EXPORT_SYMBOL_GPL(hid_add_device);
1721 * hid_allocate_device - allocate new hid device descriptor
1723 * Allocate and initialize hid device, so that hid_destroy_device might be
1724 * used to free it.
1726 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1727 * error value.
1729 struct hid_device *hid_allocate_device(void)
1731 struct hid_device *hdev;
1732 unsigned int i;
1733 int ret = -ENOMEM;
1735 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1736 if (hdev == NULL)
1737 return ERR_PTR(ret);
1739 device_initialize(&hdev->dev);
1740 hdev->dev.release = hid_device_release;
1741 hdev->dev.bus = &hid_bus_type;
1743 hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1744 sizeof(struct hid_collection), GFP_KERNEL);
1745 if (hdev->collection == NULL)
1746 goto err;
1747 hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1749 for (i = 0; i < HID_REPORT_TYPES; i++)
1750 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1752 return hdev;
1753 err:
1754 put_device(&hdev->dev);
1755 return ERR_PTR(ret);
1757 EXPORT_SYMBOL_GPL(hid_allocate_device);
1759 static void hid_remove_device(struct hid_device *hdev)
1761 if (hdev->status & HID_STAT_ADDED) {
1762 device_del(&hdev->dev);
1763 hdev->status &= ~HID_STAT_ADDED;
1768 * hid_destroy_device - free previously allocated device
1770 * @hdev: hid device
1772 * If you allocate hid_device through hid_allocate_device, you should ever
1773 * free by this function.
1775 void hid_destroy_device(struct hid_device *hdev)
1777 hid_remove_device(hdev);
1778 put_device(&hdev->dev);
1780 EXPORT_SYMBOL_GPL(hid_destroy_device);
1782 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1783 const char *mod_name)
1785 int ret;
1787 hdrv->driver.name = hdrv->name;
1788 hdrv->driver.bus = &hid_bus_type;
1789 hdrv->driver.owner = owner;
1790 hdrv->driver.mod_name = mod_name;
1792 INIT_LIST_HEAD(&hdrv->dyn_list);
1793 spin_lock_init(&hdrv->dyn_lock);
1795 ret = driver_register(&hdrv->driver);
1796 if (ret)
1797 return ret;
1799 ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
1800 if (ret)
1801 driver_unregister(&hdrv->driver);
1803 return ret;
1805 EXPORT_SYMBOL_GPL(__hid_register_driver);
1807 void hid_unregister_driver(struct hid_driver *hdrv)
1809 driver_remove_file(&hdrv->driver, &driver_attr_new_id);
1810 driver_unregister(&hdrv->driver);
1811 hid_free_dynids(hdrv);
1813 EXPORT_SYMBOL_GPL(hid_unregister_driver);
1815 #ifdef CONFIG_HID_COMPAT
1816 static void hid_compat_load(struct work_struct *ws)
1818 request_module("hid-dummy");
1820 static DECLARE_WORK(hid_compat_work, hid_compat_load);
1821 static struct workqueue_struct *hid_compat_wq;
1822 #endif
1824 static int __init hid_init(void)
1826 int ret;
1828 ret = bus_register(&hid_bus_type);
1829 if (ret) {
1830 printk(KERN_ERR "HID: can't register hid bus\n");
1831 goto err;
1834 ret = hidraw_init();
1835 if (ret)
1836 goto err_bus;
1838 #ifdef CONFIG_HID_COMPAT
1839 hid_compat_wq = create_singlethread_workqueue("hid_compat");
1840 if (!hid_compat_wq) {
1841 hidraw_exit();
1842 goto err;
1844 queue_work(hid_compat_wq, &hid_compat_work);
1845 #endif
1847 return 0;
1848 err_bus:
1849 bus_unregister(&hid_bus_type);
1850 err:
1851 return ret;
1854 static void __exit hid_exit(void)
1856 #ifdef CONFIG_HID_COMPAT
1857 destroy_workqueue(hid_compat_wq);
1858 #endif
1859 hidraw_exit();
1860 bus_unregister(&hid_bus_type);
1863 module_init(hid_init);
1864 module_exit(hid_exit);
1866 MODULE_LICENSE(DRIVER_LICENSE);