Merge git://git.kernel.org/pub/scm/linux/kernel/git/joern/logfs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-core.c
blob143e788b729b1bdaadb4888e3568dac3a6714b0c
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-2010 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_DESC "HID core driver"
43 #define DRIVER_LICENSE "GPL"
45 int hid_debug = 0;
46 module_param_named(debug, hid_debug, int, 0600);
47 MODULE_PARM_DESC(debug, "toggle HID debugging messages");
48 EXPORT_SYMBOL_GPL(hid_debug);
51 * Register a new report for a device.
54 struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
56 struct hid_report_enum *report_enum = device->report_enum + type;
57 struct hid_report *report;
59 if (report_enum->report_id_hash[id])
60 return report_enum->report_id_hash[id];
62 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
63 return NULL;
65 if (id != 0)
66 report_enum->numbered = 1;
68 report->id = id;
69 report->type = type;
70 report->size = 0;
71 report->device = device;
72 report_enum->report_id_hash[id] = report;
74 list_add_tail(&report->list, &report_enum->report_list);
76 return report;
78 EXPORT_SYMBOL_GPL(hid_register_report);
81 * Register a new field for this report.
84 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
86 struct hid_field *field;
88 if (report->maxfield == HID_MAX_FIELDS) {
89 dbg_hid("too many fields in report\n");
90 return NULL;
93 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
94 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
96 field->index = report->maxfield++;
97 report->field[field->index] = field;
98 field->usage = (struct hid_usage *)(field + 1);
99 field->value = (s32 *)(field->usage + usages);
100 field->report = report;
102 return field;
106 * Open a collection. The type/usage is pushed on the stack.
109 static int open_collection(struct hid_parser *parser, unsigned type)
111 struct hid_collection *collection;
112 unsigned usage;
114 usage = parser->local.usage[0];
116 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
117 dbg_hid("collection stack overflow\n");
118 return -1;
121 if (parser->device->maxcollection == parser->device->collection_size) {
122 collection = kmalloc(sizeof(struct hid_collection) *
123 parser->device->collection_size * 2, GFP_KERNEL);
124 if (collection == NULL) {
125 dbg_hid("failed to reallocate collection array\n");
126 return -1;
128 memcpy(collection, parser->device->collection,
129 sizeof(struct hid_collection) *
130 parser->device->collection_size);
131 memset(collection + parser->device->collection_size, 0,
132 sizeof(struct hid_collection) *
133 parser->device->collection_size);
134 kfree(parser->device->collection);
135 parser->device->collection = collection;
136 parser->device->collection_size *= 2;
139 parser->collection_stack[parser->collection_stack_ptr++] =
140 parser->device->maxcollection;
142 collection = parser->device->collection +
143 parser->device->maxcollection++;
144 collection->type = type;
145 collection->usage = usage;
146 collection->level = parser->collection_stack_ptr - 1;
148 if (type == HID_COLLECTION_APPLICATION)
149 parser->device->maxapplication++;
151 return 0;
155 * Close a collection.
158 static int close_collection(struct hid_parser *parser)
160 if (!parser->collection_stack_ptr) {
161 dbg_hid("collection stack underflow\n");
162 return -1;
164 parser->collection_stack_ptr--;
165 return 0;
169 * Climb up the stack, search for the specified collection type
170 * and return the usage.
173 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
175 int n;
176 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
177 if (parser->device->collection[parser->collection_stack[n]].type == type)
178 return parser->device->collection[parser->collection_stack[n]].usage;
179 return 0; /* we know nothing about this usage type */
183 * Add a usage to the temporary parser table.
186 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
188 if (parser->local.usage_index >= HID_MAX_USAGES) {
189 dbg_hid("usage index exceeded\n");
190 return -1;
192 parser->local.usage[parser->local.usage_index] = usage;
193 parser->local.collection_index[parser->local.usage_index] =
194 parser->collection_stack_ptr ?
195 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
196 parser->local.usage_index++;
197 return 0;
201 * Register a new field for this report.
204 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
206 struct hid_report *report;
207 struct hid_field *field;
208 int usages;
209 unsigned offset;
210 int i;
212 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
213 dbg_hid("hid_register_report failed\n");
214 return -1;
217 if (parser->global.logical_maximum < parser->global.logical_minimum) {
218 dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
219 return -1;
222 offset = report->size;
223 report->size += parser->global.report_size * parser->global.report_count;
225 if (!parser->local.usage_index) /* Ignore padding fields */
226 return 0;
228 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
230 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
231 return 0;
233 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
234 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
235 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
237 for (i = 0; i < usages; i++) {
238 int j = i;
239 /* Duplicate the last usage we parsed if we have excess values */
240 if (i >= parser->local.usage_index)
241 j = parser->local.usage_index - 1;
242 field->usage[i].hid = parser->local.usage[j];
243 field->usage[i].collection_index =
244 parser->local.collection_index[j];
247 field->maxusage = usages;
248 field->flags = flags;
249 field->report_offset = offset;
250 field->report_type = report_type;
251 field->report_size = parser->global.report_size;
252 field->report_count = parser->global.report_count;
253 field->logical_minimum = parser->global.logical_minimum;
254 field->logical_maximum = parser->global.logical_maximum;
255 field->physical_minimum = parser->global.physical_minimum;
256 field->physical_maximum = parser->global.physical_maximum;
257 field->unit_exponent = parser->global.unit_exponent;
258 field->unit = parser->global.unit;
260 return 0;
264 * Read data value from item.
267 static u32 item_udata(struct hid_item *item)
269 switch (item->size) {
270 case 1: return item->data.u8;
271 case 2: return item->data.u16;
272 case 4: return item->data.u32;
274 return 0;
277 static s32 item_sdata(struct hid_item *item)
279 switch (item->size) {
280 case 1: return item->data.s8;
281 case 2: return item->data.s16;
282 case 4: return item->data.s32;
284 return 0;
288 * Process a global item.
291 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
293 switch (item->tag) {
294 case HID_GLOBAL_ITEM_TAG_PUSH:
296 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
297 dbg_hid("global enviroment stack overflow\n");
298 return -1;
301 memcpy(parser->global_stack + parser->global_stack_ptr++,
302 &parser->global, sizeof(struct hid_global));
303 return 0;
305 case HID_GLOBAL_ITEM_TAG_POP:
307 if (!parser->global_stack_ptr) {
308 dbg_hid("global enviroment stack underflow\n");
309 return -1;
312 memcpy(&parser->global, parser->global_stack +
313 --parser->global_stack_ptr, sizeof(struct hid_global));
314 return 0;
316 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
317 parser->global.usage_page = item_udata(item);
318 return 0;
320 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
321 parser->global.logical_minimum = item_sdata(item);
322 return 0;
324 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
325 if (parser->global.logical_minimum < 0)
326 parser->global.logical_maximum = item_sdata(item);
327 else
328 parser->global.logical_maximum = item_udata(item);
329 return 0;
331 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
332 parser->global.physical_minimum = item_sdata(item);
333 return 0;
335 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
336 if (parser->global.physical_minimum < 0)
337 parser->global.physical_maximum = item_sdata(item);
338 else
339 parser->global.physical_maximum = item_udata(item);
340 return 0;
342 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
343 parser->global.unit_exponent = item_sdata(item);
344 return 0;
346 case HID_GLOBAL_ITEM_TAG_UNIT:
347 parser->global.unit = item_udata(item);
348 return 0;
350 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
351 parser->global.report_size = item_udata(item);
352 if (parser->global.report_size > 32) {
353 dbg_hid("invalid report_size %d\n",
354 parser->global.report_size);
355 return -1;
357 return 0;
359 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
360 parser->global.report_count = item_udata(item);
361 if (parser->global.report_count > HID_MAX_USAGES) {
362 dbg_hid("invalid report_count %d\n",
363 parser->global.report_count);
364 return -1;
366 return 0;
368 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
369 parser->global.report_id = item_udata(item);
370 if (parser->global.report_id == 0) {
371 dbg_hid("report_id 0 is invalid\n");
372 return -1;
374 return 0;
376 default:
377 dbg_hid("unknown global tag 0x%x\n", item->tag);
378 return -1;
383 * Process a local item.
386 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
388 __u32 data;
389 unsigned n;
391 /* Local delimiter could have value 0, which allows size to be 0 */
392 if (item->size == 0 && item->tag != HID_LOCAL_ITEM_TAG_DELIMITER) {
393 dbg_hid("item data expected for local item\n");
394 return -1;
397 data = item_udata(item);
399 switch (item->tag) {
400 case HID_LOCAL_ITEM_TAG_DELIMITER:
402 if (data) {
404 * We treat items before the first delimiter
405 * as global to all usage sets (branch 0).
406 * In the moment we process only these global
407 * items and the first delimiter set.
409 if (parser->local.delimiter_depth != 0) {
410 dbg_hid("nested delimiters\n");
411 return -1;
413 parser->local.delimiter_depth++;
414 parser->local.delimiter_branch++;
415 } else {
416 if (parser->local.delimiter_depth < 1) {
417 dbg_hid("bogus close delimiter\n");
418 return -1;
420 parser->local.delimiter_depth--;
422 return 1;
424 case HID_LOCAL_ITEM_TAG_USAGE:
426 if (parser->local.delimiter_branch > 1) {
427 dbg_hid("alternative usage ignored\n");
428 return 0;
431 if (item->size <= 2)
432 data = (parser->global.usage_page << 16) + data;
434 return hid_add_usage(parser, data);
436 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
438 if (parser->local.delimiter_branch > 1) {
439 dbg_hid("alternative usage ignored\n");
440 return 0;
443 if (item->size <= 2)
444 data = (parser->global.usage_page << 16) + data;
446 parser->local.usage_minimum = data;
447 return 0;
449 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
451 if (parser->local.delimiter_branch > 1) {
452 dbg_hid("alternative usage ignored\n");
453 return 0;
456 if (item->size <= 2)
457 data = (parser->global.usage_page << 16) + data;
459 for (n = parser->local.usage_minimum; n <= data; n++)
460 if (hid_add_usage(parser, n)) {
461 dbg_hid("hid_add_usage failed\n");
462 return -1;
464 return 0;
466 default:
468 dbg_hid("unknown local item tag 0x%x\n", item->tag);
469 return 0;
471 return 0;
475 * Process a main item.
478 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
480 __u32 data;
481 int ret;
483 data = item_udata(item);
485 switch (item->tag) {
486 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
487 ret = open_collection(parser, data & 0xff);
488 break;
489 case HID_MAIN_ITEM_TAG_END_COLLECTION:
490 ret = close_collection(parser);
491 break;
492 case HID_MAIN_ITEM_TAG_INPUT:
493 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
494 break;
495 case HID_MAIN_ITEM_TAG_OUTPUT:
496 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
497 break;
498 case HID_MAIN_ITEM_TAG_FEATURE:
499 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
500 break;
501 default:
502 dbg_hid("unknown main item tag 0x%x\n", item->tag);
503 ret = 0;
506 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
508 return ret;
512 * Process a reserved item.
515 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
517 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
518 return 0;
522 * Free a report and all registered fields. The field->usage and
523 * field->value table's are allocated behind the field, so we need
524 * only to free(field) itself.
527 static void hid_free_report(struct hid_report *report)
529 unsigned n;
531 for (n = 0; n < report->maxfield; n++)
532 kfree(report->field[n]);
533 kfree(report);
537 * Free a device structure, all reports, and all fields.
540 static void hid_device_release(struct device *dev)
542 struct hid_device *device = container_of(dev, struct hid_device, dev);
543 unsigned i, j;
545 for (i = 0; i < HID_REPORT_TYPES; i++) {
546 struct hid_report_enum *report_enum = device->report_enum + i;
548 for (j = 0; j < 256; j++) {
549 struct hid_report *report = report_enum->report_id_hash[j];
550 if (report)
551 hid_free_report(report);
555 kfree(device->rdesc);
556 kfree(device->collection);
557 kfree(device);
561 * Fetch a report description item from the data stream. We support long
562 * items, though they are not used yet.
565 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
567 u8 b;
569 if ((end - start) <= 0)
570 return NULL;
572 b = *start++;
574 item->type = (b >> 2) & 3;
575 item->tag = (b >> 4) & 15;
577 if (item->tag == HID_ITEM_TAG_LONG) {
579 item->format = HID_ITEM_FORMAT_LONG;
581 if ((end - start) < 2)
582 return NULL;
584 item->size = *start++;
585 item->tag = *start++;
587 if ((end - start) < item->size)
588 return NULL;
590 item->data.longdata = start;
591 start += item->size;
592 return start;
595 item->format = HID_ITEM_FORMAT_SHORT;
596 item->size = b & 3;
598 switch (item->size) {
599 case 0:
600 return start;
602 case 1:
603 if ((end - start) < 1)
604 return NULL;
605 item->data.u8 = *start++;
606 return start;
608 case 2:
609 if ((end - start) < 2)
610 return NULL;
611 item->data.u16 = get_unaligned_le16(start);
612 start = (__u8 *)((__le16 *)start + 1);
613 return start;
615 case 3:
616 item->size++;
617 if ((end - start) < 4)
618 return NULL;
619 item->data.u32 = get_unaligned_le32(start);
620 start = (__u8 *)((__le32 *)start + 1);
621 return start;
624 return NULL;
628 * hid_parse_report - parse device report
630 * @device: hid device
631 * @start: report start
632 * @size: report size
634 * Parse a report description into a hid_device structure. Reports are
635 * enumerated, fields are attached to these reports.
636 * 0 returned on success, otherwise nonzero error value.
638 int hid_parse_report(struct hid_device *device, __u8 *start,
639 unsigned size)
641 struct hid_parser *parser;
642 struct hid_item item;
643 __u8 *end;
644 int ret;
645 static int (*dispatch_type[])(struct hid_parser *parser,
646 struct hid_item *item) = {
647 hid_parser_main,
648 hid_parser_global,
649 hid_parser_local,
650 hid_parser_reserved
653 if (device->driver->report_fixup)
654 device->driver->report_fixup(device, start, size);
656 device->rdesc = kmalloc(size, GFP_KERNEL);
657 if (device->rdesc == NULL)
658 return -ENOMEM;
659 memcpy(device->rdesc, start, size);
660 device->rsize = size;
662 parser = vmalloc(sizeof(struct hid_parser));
663 if (!parser) {
664 ret = -ENOMEM;
665 goto err;
668 memset(parser, 0, sizeof(struct hid_parser));
669 parser->device = device;
671 end = start + size;
672 ret = -EINVAL;
673 while ((start = fetch_item(start, end, &item)) != NULL) {
675 if (item.format != HID_ITEM_FORMAT_SHORT) {
676 dbg_hid("unexpected long global item\n");
677 goto err;
680 if (dispatch_type[item.type](parser, &item)) {
681 dbg_hid("item %u %u %u %u parsing failed\n",
682 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
683 goto err;
686 if (start == end) {
687 if (parser->collection_stack_ptr) {
688 dbg_hid("unbalanced collection at end of report description\n");
689 goto err;
691 if (parser->local.delimiter_depth) {
692 dbg_hid("unbalanced delimiter at end of report description\n");
693 goto err;
695 vfree(parser);
696 return 0;
700 dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
701 err:
702 vfree(parser);
703 return ret;
705 EXPORT_SYMBOL_GPL(hid_parse_report);
708 * Convert a signed n-bit integer to signed 32-bit integer. Common
709 * cases are done through the compiler, the screwed things has to be
710 * done by hand.
713 static s32 snto32(__u32 value, unsigned n)
715 switch (n) {
716 case 8: return ((__s8)value);
717 case 16: return ((__s16)value);
718 case 32: return ((__s32)value);
720 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
724 * Convert a signed 32-bit integer to a signed n-bit integer.
727 static u32 s32ton(__s32 value, unsigned n)
729 s32 a = value >> (n - 1);
730 if (a && a != -1)
731 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
732 return value & ((1 << n) - 1);
736 * Extract/implement a data field from/to a little endian report (bit array).
738 * Code sort-of follows HID spec:
739 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
741 * While the USB HID spec allows unlimited length bit fields in "report
742 * descriptors", most devices never use more than 16 bits.
743 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
744 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
747 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
749 u64 x;
751 if (n > 32)
752 printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
753 n, current->comm);
755 report += offset >> 3; /* adjust byte index */
756 offset &= 7; /* now only need bit offset into one byte */
757 x = get_unaligned_le64(report);
758 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
759 return (u32) x;
763 * "implement" : set bits in a little endian bit stream.
764 * Same concepts as "extract" (see comments above).
765 * The data mangled in the bit stream remains in little endian
766 * order the whole time. It make more sense to talk about
767 * endianness of register values by considering a register
768 * a "cached" copy of the little endiad bit stream.
770 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
772 u64 x;
773 u64 m = (1ULL << n) - 1;
775 if (n > 32)
776 printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
777 n, current->comm);
779 if (value > m)
780 printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
781 value, current->comm);
782 WARN_ON(value > m);
783 value &= m;
785 report += offset >> 3;
786 offset &= 7;
788 x = get_unaligned_le64(report);
789 x &= ~(m << offset);
790 x |= ((u64)value) << offset;
791 put_unaligned_le64(x, report);
795 * Search an array for a value.
798 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
800 while (n--) {
801 if (*array++ == value)
802 return 0;
804 return -1;
808 * hid_match_report - check if driver's raw_event should be called
810 * @hid: hid device
811 * @report_type: type to match against
813 * compare hid->driver->report_table->report_type to report->type
815 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
817 const struct hid_report_id *id = hid->driver->report_table;
819 if (!id) /* NULL means all */
820 return 1;
822 for (; id->report_type != HID_TERMINATOR; id++)
823 if (id->report_type == HID_ANY_ID ||
824 id->report_type == report->type)
825 return 1;
826 return 0;
830 * hid_match_usage - check if driver's event should be called
832 * @hid: hid device
833 * @usage: usage to match against
835 * compare hid->driver->usage_table->usage_{type,code} to
836 * usage->usage_{type,code}
838 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
840 const struct hid_usage_id *id = hid->driver->usage_table;
842 if (!id) /* NULL means all */
843 return 1;
845 for (; id->usage_type != HID_ANY_ID - 1; id++)
846 if ((id->usage_hid == HID_ANY_ID ||
847 id->usage_hid == usage->hid) &&
848 (id->usage_type == HID_ANY_ID ||
849 id->usage_type == usage->type) &&
850 (id->usage_code == HID_ANY_ID ||
851 id->usage_code == usage->code))
852 return 1;
853 return 0;
856 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
857 struct hid_usage *usage, __s32 value, int interrupt)
859 struct hid_driver *hdrv = hid->driver;
860 int ret;
862 hid_dump_input(hid, usage, value);
864 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
865 ret = hdrv->event(hid, field, usage, value);
866 if (ret != 0) {
867 if (ret < 0)
868 dbg_hid("%s's event failed with %d\n",
869 hdrv->name, ret);
870 return;
874 if (hid->claimed & HID_CLAIMED_INPUT)
875 hidinput_hid_event(hid, field, usage, value);
876 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
877 hid->hiddev_hid_event(hid, field, usage, value);
881 * Analyse a received field, and fetch the data from it. The field
882 * content is stored for next report processing (we do differential
883 * reporting to the layer).
886 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
887 __u8 *data, int interrupt)
889 unsigned n;
890 unsigned count = field->report_count;
891 unsigned offset = field->report_offset;
892 unsigned size = field->report_size;
893 __s32 min = field->logical_minimum;
894 __s32 max = field->logical_maximum;
895 __s32 *value;
897 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
898 return;
900 for (n = 0; n < count; n++) {
902 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
903 extract(data, offset + n * size, size);
905 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
906 && value[n] >= min && value[n] <= max
907 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
908 goto exit;
911 for (n = 0; n < count; n++) {
913 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
914 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
915 continue;
918 if (field->value[n] >= min && field->value[n] <= max
919 && field->usage[field->value[n] - min].hid
920 && search(value, field->value[n], count))
921 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
923 if (value[n] >= min && value[n] <= max
924 && field->usage[value[n] - min].hid
925 && search(field->value, value[n], count))
926 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
929 memcpy(field->value, value, count * sizeof(__s32));
930 exit:
931 kfree(value);
935 * Output the field into the report.
938 static void hid_output_field(struct hid_field *field, __u8 *data)
940 unsigned count = field->report_count;
941 unsigned offset = field->report_offset;
942 unsigned size = field->report_size;
943 unsigned bitsused = offset + count * size;
944 unsigned n;
946 /* make sure the unused bits in the last byte are zeros */
947 if (count > 0 && size > 0 && (bitsused % 8) != 0)
948 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
950 for (n = 0; n < count; n++) {
951 if (field->logical_minimum < 0) /* signed values */
952 implement(data, offset + n * size, size, s32ton(field->value[n], size));
953 else /* unsigned values */
954 implement(data, offset + n * size, size, field->value[n]);
959 * Create a report.
962 void hid_output_report(struct hid_report *report, __u8 *data)
964 unsigned n;
966 if (report->id > 0)
967 *data++ = report->id;
969 for (n = 0; n < report->maxfield; n++)
970 hid_output_field(report->field[n], data);
972 EXPORT_SYMBOL_GPL(hid_output_report);
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.
980 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
982 unsigned size = field->report_size;
984 hid_dump_input(field->report->device, field->usage + offset, value);
986 if (offset >= field->report_count) {
987 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
988 return -1;
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;
996 field->value[offset] = value;
997 return 0;
999 EXPORT_SYMBOL_GPL(hid_set_field);
1001 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1002 const u8 *data)
1004 struct hid_report *report;
1005 unsigned int n = 0; /* Normally report number is 0 */
1007 /* Device uses numbered reports, data[0] is report number */
1008 if (report_enum->numbered)
1009 n = *data;
1011 report = report_enum->report_id_hash[n];
1012 if (report == NULL)
1013 dbg_hid("undefined report_id %u received\n", n);
1015 return report;
1018 void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1019 int interrupt)
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;
1027 report = hid_get_report(report_enum, data);
1028 if (!report)
1029 return;
1031 if (report_enum->numbered) {
1032 cdata++;
1033 csize--;
1036 rsize = ((report->size - 1) >> 3) + 1;
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);
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);
1049 for (a = 0; a < report->maxfield; a++)
1050 hid_input_field(hid, report->field[a], cdata, interrupt);
1052 if (hid->claimed & HID_CLAIMED_INPUT)
1053 hidinput_report_event(hid, report);
1055 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1058 * hid_input_report - report data from lower layer (usb, bt...)
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
1066 * This is data entry for lower layers.
1068 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
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;
1077 if (!hid || !hid->driver)
1078 return -ENODEV;
1079 report_enum = hid->report_enum + type;
1080 hdrv = hid->driver;
1082 if (!size) {
1083 dbg_hid("empty report\n");
1084 return -1;
1087 buf = kmalloc(sizeof(char) * HID_DEBUG_BUFSIZE, GFP_ATOMIC);
1089 if (!buf) {
1090 report = hid_get_report(report_enum, data);
1091 goto nomem;
1094 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1095 "\nreport (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1096 hid_debug_event(hid, buf);
1098 report = hid_get_report(report_enum, data);
1099 if (!report) {
1100 kfree(buf);
1101 return -1;
1104 /* dump the report */
1105 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1106 "report %d (size %u) = ", report->id, size);
1107 hid_debug_event(hid, buf);
1108 for (i = 0; i < size; i++) {
1109 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1110 " %02x", data[i]);
1111 hid_debug_event(hid, buf);
1113 hid_debug_event(hid, "\n");
1115 kfree(buf);
1117 nomem:
1118 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1119 ret = hdrv->raw_event(hid, report, data, size);
1120 if (ret != 0)
1121 return ret < 0 ? ret : 0;
1124 hid_report_raw_event(hid, type, data, size, interrupt);
1126 return 0;
1128 EXPORT_SYMBOL_GPL(hid_input_report);
1130 static bool hid_match_one_id(struct hid_device *hdev,
1131 const struct hid_device_id *id)
1133 return id->bus == hdev->bus &&
1134 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1135 (id->product == HID_ANY_ID || id->product == hdev->product);
1138 static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1139 const struct hid_device_id *id)
1141 for (; id->bus; id++)
1142 if (hid_match_one_id(hdev, id))
1143 return id;
1145 return NULL;
1148 static const struct hid_device_id hid_hiddev_list[] = {
1149 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1150 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1154 static bool hid_hiddev(struct hid_device *hdev)
1156 return !!hid_match_id(hdev, hid_hiddev_list);
1159 int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1161 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1162 "Joystick", "Gamepad", "Keyboard", "Keypad",
1163 "Multi-Axis Controller"
1165 const char *type, *bus;
1166 char buf[64];
1167 unsigned int i;
1168 int len;
1170 if (hdev->bus != BUS_USB)
1171 connect_mask &= ~HID_CONNECT_HIDDEV;
1172 if (hid_hiddev(hdev))
1173 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1175 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1176 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1177 hdev->claimed |= HID_CLAIMED_INPUT;
1178 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1179 !hdev->hiddev_connect(hdev,
1180 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1181 hdev->claimed |= HID_CLAIMED_HIDDEV;
1182 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1183 hdev->claimed |= HID_CLAIMED_HIDRAW;
1185 if (!hdev->claimed) {
1186 dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
1187 "hidraw\n");
1188 return -ENODEV;
1191 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1192 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1193 hdev->ff_init(hdev);
1195 len = 0;
1196 if (hdev->claimed & HID_CLAIMED_INPUT)
1197 len += sprintf(buf + len, "input");
1198 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1199 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1200 hdev->minor);
1201 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1202 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1203 ((struct hidraw *)hdev->hidraw)->minor);
1205 type = "Device";
1206 for (i = 0; i < hdev->maxcollection; i++) {
1207 struct hid_collection *col = &hdev->collection[i];
1208 if (col->type == HID_COLLECTION_APPLICATION &&
1209 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1210 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1211 type = types[col->usage & 0xffff];
1212 break;
1216 switch (hdev->bus) {
1217 case BUS_USB:
1218 bus = "USB";
1219 break;
1220 case BUS_BLUETOOTH:
1221 bus = "BLUETOOTH";
1222 break;
1223 default:
1224 bus = "<UNKNOWN>";
1227 dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1228 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1229 type, hdev->name, hdev->phys);
1231 return 0;
1233 EXPORT_SYMBOL_GPL(hid_connect);
1235 void hid_disconnect(struct hid_device *hdev)
1237 if (hdev->claimed & HID_CLAIMED_INPUT)
1238 hidinput_disconnect(hdev);
1239 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1240 hdev->hiddev_disconnect(hdev);
1241 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1242 hidraw_disconnect(hdev);
1244 EXPORT_SYMBOL_GPL(hid_disconnect);
1246 /* a list of devices for which there is a specialized driver on HID bus */
1247 static const struct hid_device_id hid_blacklist[] = {
1248 { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
1249 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
1250 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1251 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
1252 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1253 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1254 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE) },
1255 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1256 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1257 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1258 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1259 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1260 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1261 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1262 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1263 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1264 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1265 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1266 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1267 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1268 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
1269 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1270 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1271 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1272 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1273 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1274 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1275 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1276 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1277 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
1278 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1279 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1280 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1281 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1282 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1283 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1284 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1285 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1286 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1287 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI) },
1288 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO) },
1289 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS) },
1290 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1291 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1292 { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
1293 { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1294 { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION_SOLAR) },
1295 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1296 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
1297 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1298 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) },
1299 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
1300 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
1301 { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
1302 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1303 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
1304 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
1305 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1306 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
1307 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1308 { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
1309 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
1310 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
1311 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1312 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1313 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1314 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1315 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1316 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1317 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
1318 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1319 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
1320 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1321 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1322 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1323 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1324 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1325 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
1326 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1327 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FLIGHT_SYSTEM_G940) },
1328 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1329 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1330 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
1331 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
1332 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
1333 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) },
1334 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
1335 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
1336 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
1337 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1338 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
1339 { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
1340 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
1341 { HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_WKB2000) },
1342 { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1343 { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) },
1344 { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, USB_DEVICE_ID_PIXART_IMAGING_INC_OPTICAL_TOUCH_SCREEN) },
1345 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1346 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1347 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1348 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
1349 { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, USB_DEVICE_ID_MTP) },
1350 { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1351 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1352 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
1353 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
1354 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
1355 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1356 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653) },
1357 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1358 { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
1359 { HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
1360 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
1361 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
1362 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1363 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1365 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
1369 struct hid_dynid {
1370 struct list_head list;
1371 struct hid_device_id id;
1375 * store_new_id - add a new HID device ID to this driver and re-probe devices
1376 * @driver: target device driver
1377 * @buf: buffer for scanning device ID data
1378 * @count: input size
1380 * Adds a new dynamic hid device ID to this driver,
1381 * and causes the driver to probe for all devices again.
1383 static ssize_t store_new_id(struct device_driver *drv, const char *buf,
1384 size_t count)
1386 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1387 struct hid_dynid *dynid;
1388 __u32 bus, vendor, product;
1389 unsigned long driver_data = 0;
1390 int ret;
1392 ret = sscanf(buf, "%x %x %x %lx",
1393 &bus, &vendor, &product, &driver_data);
1394 if (ret < 3)
1395 return -EINVAL;
1397 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1398 if (!dynid)
1399 return -ENOMEM;
1401 dynid->id.bus = bus;
1402 dynid->id.vendor = vendor;
1403 dynid->id.product = product;
1404 dynid->id.driver_data = driver_data;
1406 spin_lock(&hdrv->dyn_lock);
1407 list_add_tail(&dynid->list, &hdrv->dyn_list);
1408 spin_unlock(&hdrv->dyn_lock);
1410 ret = 0;
1411 if (get_driver(&hdrv->driver)) {
1412 ret = driver_attach(&hdrv->driver);
1413 put_driver(&hdrv->driver);
1416 return ret ? : count;
1418 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1420 static void hid_free_dynids(struct hid_driver *hdrv)
1422 struct hid_dynid *dynid, *n;
1424 spin_lock(&hdrv->dyn_lock);
1425 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1426 list_del(&dynid->list);
1427 kfree(dynid);
1429 spin_unlock(&hdrv->dyn_lock);
1432 static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1433 struct hid_driver *hdrv)
1435 struct hid_dynid *dynid;
1437 spin_lock(&hdrv->dyn_lock);
1438 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1439 if (hid_match_one_id(hdev, &dynid->id)) {
1440 spin_unlock(&hdrv->dyn_lock);
1441 return &dynid->id;
1444 spin_unlock(&hdrv->dyn_lock);
1446 return hid_match_id(hdev, hdrv->id_table);
1449 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1451 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1452 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1454 if (!hid_match_device(hdev, hdrv))
1455 return 0;
1457 /* generic wants all non-blacklisted */
1458 if (!strncmp(hdrv->name, "generic-", 8))
1459 return !hid_match_id(hdev, hid_blacklist);
1461 return 1;
1464 static int hid_device_probe(struct device *dev)
1466 struct hid_driver *hdrv = container_of(dev->driver,
1467 struct hid_driver, driver);
1468 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1469 const struct hid_device_id *id;
1470 int ret = 0;
1472 if (!hdev->driver) {
1473 id = hid_match_device(hdev, hdrv);
1474 if (id == NULL)
1475 return -ENODEV;
1477 hdev->driver = hdrv;
1478 if (hdrv->probe) {
1479 ret = hdrv->probe(hdev, id);
1480 } else { /* default probe */
1481 ret = hid_parse(hdev);
1482 if (!ret)
1483 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1485 if (ret)
1486 hdev->driver = NULL;
1488 return ret;
1491 static int hid_device_remove(struct device *dev)
1493 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1494 struct hid_driver *hdrv = hdev->driver;
1496 if (hdrv) {
1497 if (hdrv->remove)
1498 hdrv->remove(hdev);
1499 else /* default remove */
1500 hid_hw_stop(hdev);
1501 hdev->driver = NULL;
1504 return 0;
1507 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1509 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1511 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1512 hdev->bus, hdev->vendor, hdev->product))
1513 return -ENOMEM;
1515 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1516 return -ENOMEM;
1518 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1519 return -ENOMEM;
1521 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1522 return -ENOMEM;
1524 if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1525 hdev->bus, hdev->vendor, hdev->product))
1526 return -ENOMEM;
1528 return 0;
1531 static struct bus_type hid_bus_type = {
1532 .name = "hid",
1533 .match = hid_bus_match,
1534 .probe = hid_device_probe,
1535 .remove = hid_device_remove,
1536 .uevent = hid_uevent,
1539 /* a list of devices that shouldn't be handled by HID core at all */
1540 static const struct hid_device_id hid_ignore_list[] = {
1541 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1542 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1543 { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1544 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1545 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1546 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1547 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1548 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1549 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1550 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1551 { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1552 { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1553 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_T91MT)},
1554 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM)},
1555 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM2)},
1556 { HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1557 { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1558 { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1559 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1560 { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1561 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1562 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1563 { HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
1564 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1565 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1566 { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1567 { HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC5UH) },
1568 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1569 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1570 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1571 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1572 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1573 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1574 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1575 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1576 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1577 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1578 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1579 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1580 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1581 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1582 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1583 { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1584 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1585 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1586 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1587 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1588 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1589 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1590 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1591 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1592 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1593 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1594 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1595 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1596 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1597 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1598 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1599 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1600 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1601 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1602 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1603 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1604 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1605 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1606 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1607 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1608 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1609 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1610 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1611 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1612 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1613 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1614 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1615 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1616 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1617 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1618 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1619 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1620 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1621 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1622 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1623 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1624 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1625 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1626 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1627 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1628 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1629 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1630 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1631 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1632 { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1633 { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1634 { HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
1635 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1636 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_KYE, 0x0058) },
1637 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1638 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1639 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1640 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1641 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1642 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1643 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1644 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1645 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1646 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1647 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1648 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1649 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1650 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1651 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1652 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1653 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1654 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1655 { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1656 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1657 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1658 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1659 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1660 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1661 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1662 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1663 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1664 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1665 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1666 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1667 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1668 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1669 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1670 { HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
1671 { HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
1672 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1673 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1674 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1675 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1676 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1677 { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1678 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1679 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1680 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1681 { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1686 * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1688 * There are composite devices for which we want to ignore only a certain
1689 * interface. This is a list of devices for which only the mouse interface will
1690 * be ignored. This allows a dedicated driver to take care of the interface.
1692 static const struct hid_device_id hid_mouse_ignore_list[] = {
1693 /* appletouch driver */
1694 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1695 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1696 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1697 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1698 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1699 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1700 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1701 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1702 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1703 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1704 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1705 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1706 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1707 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1708 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1709 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1710 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1711 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1712 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1713 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1714 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1715 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1716 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1717 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1718 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1722 static bool hid_ignore(struct hid_device *hdev)
1724 switch (hdev->vendor) {
1725 case USB_VENDOR_ID_CODEMERCS:
1726 /* ignore all Code Mercenaries IOWarrior devices */
1727 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1728 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1729 return true;
1730 break;
1731 case USB_VENDOR_ID_LOGITECH:
1732 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1733 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1734 return true;
1735 break;
1736 case USB_VENDOR_ID_SOUNDGRAPH:
1737 if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
1738 hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
1739 return true;
1740 break;
1743 if (hdev->type == HID_TYPE_USBMOUSE &&
1744 hid_match_id(hdev, hid_mouse_ignore_list))
1745 return true;
1747 return !!hid_match_id(hdev, hid_ignore_list);
1750 int hid_add_device(struct hid_device *hdev)
1752 static atomic_t id = ATOMIC_INIT(0);
1753 int ret;
1755 if (WARN_ON(hdev->status & HID_STAT_ADDED))
1756 return -EBUSY;
1758 /* we need to kill them here, otherwise they will stay allocated to
1759 * wait for coming driver */
1760 if (hid_ignore(hdev))
1761 return -ENODEV;
1763 /* XXX hack, any other cleaner solution after the driver core
1764 * is converted to allow more than 20 bytes as the device name? */
1765 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
1766 hdev->vendor, hdev->product, atomic_inc_return(&id));
1768 ret = device_add(&hdev->dev);
1769 if (!ret)
1770 hdev->status |= HID_STAT_ADDED;
1772 hid_debug_register(hdev, dev_name(&hdev->dev));
1774 return ret;
1776 EXPORT_SYMBOL_GPL(hid_add_device);
1779 * hid_allocate_device - allocate new hid device descriptor
1781 * Allocate and initialize hid device, so that hid_destroy_device might be
1782 * used to free it.
1784 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1785 * error value.
1787 struct hid_device *hid_allocate_device(void)
1789 struct hid_device *hdev;
1790 unsigned int i;
1791 int ret = -ENOMEM;
1793 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1794 if (hdev == NULL)
1795 return ERR_PTR(ret);
1797 device_initialize(&hdev->dev);
1798 hdev->dev.release = hid_device_release;
1799 hdev->dev.bus = &hid_bus_type;
1801 hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1802 sizeof(struct hid_collection), GFP_KERNEL);
1803 if (hdev->collection == NULL)
1804 goto err;
1805 hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1807 for (i = 0; i < HID_REPORT_TYPES; i++)
1808 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1810 init_waitqueue_head(&hdev->debug_wait);
1811 INIT_LIST_HEAD(&hdev->debug_list);
1813 return hdev;
1814 err:
1815 put_device(&hdev->dev);
1816 return ERR_PTR(ret);
1818 EXPORT_SYMBOL_GPL(hid_allocate_device);
1820 static void hid_remove_device(struct hid_device *hdev)
1822 if (hdev->status & HID_STAT_ADDED) {
1823 device_del(&hdev->dev);
1824 hid_debug_unregister(hdev);
1825 hdev->status &= ~HID_STAT_ADDED;
1830 * hid_destroy_device - free previously allocated device
1832 * @hdev: hid device
1834 * If you allocate hid_device through hid_allocate_device, you should ever
1835 * free by this function.
1837 void hid_destroy_device(struct hid_device *hdev)
1839 hid_remove_device(hdev);
1840 put_device(&hdev->dev);
1842 EXPORT_SYMBOL_GPL(hid_destroy_device);
1844 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1845 const char *mod_name)
1847 int ret;
1849 hdrv->driver.name = hdrv->name;
1850 hdrv->driver.bus = &hid_bus_type;
1851 hdrv->driver.owner = owner;
1852 hdrv->driver.mod_name = mod_name;
1854 INIT_LIST_HEAD(&hdrv->dyn_list);
1855 spin_lock_init(&hdrv->dyn_lock);
1857 ret = driver_register(&hdrv->driver);
1858 if (ret)
1859 return ret;
1861 ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
1862 if (ret)
1863 driver_unregister(&hdrv->driver);
1865 return ret;
1867 EXPORT_SYMBOL_GPL(__hid_register_driver);
1869 void hid_unregister_driver(struct hid_driver *hdrv)
1871 driver_remove_file(&hdrv->driver, &driver_attr_new_id);
1872 driver_unregister(&hdrv->driver);
1873 hid_free_dynids(hdrv);
1875 EXPORT_SYMBOL_GPL(hid_unregister_driver);
1877 int hid_check_keys_pressed(struct hid_device *hid)
1879 struct hid_input *hidinput;
1880 int i;
1882 if (!(hid->claimed & HID_CLAIMED_INPUT))
1883 return 0;
1885 list_for_each_entry(hidinput, &hid->inputs, list) {
1886 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
1887 if (hidinput->input->key[i])
1888 return 1;
1891 return 0;
1894 EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
1896 static int __init hid_init(void)
1898 int ret;
1900 if (hid_debug)
1901 printk(KERN_WARNING "HID: hid_debug is now used solely for parser and driver debugging.\n"
1902 "HID: debugfs is now used for inspecting the device (report descriptor, reports)\n");
1904 ret = bus_register(&hid_bus_type);
1905 if (ret) {
1906 printk(KERN_ERR "HID: can't register hid bus\n");
1907 goto err;
1910 ret = hidraw_init();
1911 if (ret)
1912 goto err_bus;
1914 hid_debug_init();
1916 return 0;
1917 err_bus:
1918 bus_unregister(&hid_bus_type);
1919 err:
1920 return ret;
1923 static void __exit hid_exit(void)
1925 hid_debug_exit();
1926 hidraw_exit();
1927 bus_unregister(&hid_bus_type);
1930 module_init(hid_init);
1931 module_exit(hid_exit);
1933 MODULE_AUTHOR("Andreas Gal");
1934 MODULE_AUTHOR("Vojtech Pavlik");
1935 MODULE_AUTHOR("Jiri Kosina");
1936 MODULE_LICENSE(DRIVER_LICENSE);