Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[linux-2.6/btrfs-unstable.git] / drivers / hid / hid-core.c
blob3942ee61bd1c17e57867a7d8e5f521b5f8eae9b8
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-2012 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/mm.h>
25 #include <linux/spinlock.h>
26 #include <asm/unaligned.h>
27 #include <asm/byteorder.h>
28 #include <linux/input.h>
29 #include <linux/wait.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/semaphore.h>
34 #include <linux/hid.h>
35 #include <linux/hiddev.h>
36 #include <linux/hid-debug.h>
37 #include <linux/hidraw.h>
39 #include "hid-ids.h"
42 * Version Information
45 #define DRIVER_DESC "HID core driver"
47 int hid_debug = 0;
48 module_param_named(debug, hid_debug, int, 0600);
49 MODULE_PARM_DESC(debug, "toggle HID debugging messages");
50 EXPORT_SYMBOL_GPL(hid_debug);
52 static int hid_ignore_special_drivers = 0;
53 module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
54 MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
57 * Register a new report for a device.
60 struct hid_report *hid_register_report(struct hid_device *device,
61 unsigned int type, unsigned int id,
62 unsigned int application)
64 struct hid_report_enum *report_enum = device->report_enum + type;
65 struct hid_report *report;
67 if (id >= HID_MAX_IDS)
68 return NULL;
69 if (report_enum->report_id_hash[id])
70 return report_enum->report_id_hash[id];
72 report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
73 if (!report)
74 return NULL;
76 if (id != 0)
77 report_enum->numbered = 1;
79 report->id = id;
80 report->type = type;
81 report->size = 0;
82 report->device = device;
83 report->application = application;
84 report_enum->report_id_hash[id] = report;
86 list_add_tail(&report->list, &report_enum->report_list);
88 return report;
90 EXPORT_SYMBOL_GPL(hid_register_report);
93 * Register a new field for this report.
96 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
98 struct hid_field *field;
100 if (report->maxfield == HID_MAX_FIELDS) {
101 hid_err(report->device, "too many fields in report\n");
102 return NULL;
105 field = kzalloc((sizeof(struct hid_field) +
106 usages * sizeof(struct hid_usage) +
107 values * sizeof(unsigned)), GFP_KERNEL);
108 if (!field)
109 return NULL;
111 field->index = report->maxfield++;
112 report->field[field->index] = field;
113 field->usage = (struct hid_usage *)(field + 1);
114 field->value = (s32 *)(field->usage + usages);
115 field->report = report;
117 return field;
121 * Open a collection. The type/usage is pushed on the stack.
124 static int open_collection(struct hid_parser *parser, unsigned type)
126 struct hid_collection *collection;
127 unsigned usage;
129 usage = parser->local.usage[0];
131 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
132 hid_err(parser->device, "collection stack overflow\n");
133 return -EINVAL;
136 if (parser->device->maxcollection == parser->device->collection_size) {
137 collection = kmalloc(
138 array3_size(sizeof(struct hid_collection),
139 parser->device->collection_size,
141 GFP_KERNEL);
142 if (collection == NULL) {
143 hid_err(parser->device, "failed to reallocate collection array\n");
144 return -ENOMEM;
146 memcpy(collection, parser->device->collection,
147 sizeof(struct hid_collection) *
148 parser->device->collection_size);
149 memset(collection + parser->device->collection_size, 0,
150 sizeof(struct hid_collection) *
151 parser->device->collection_size);
152 kfree(parser->device->collection);
153 parser->device->collection = collection;
154 parser->device->collection_size *= 2;
157 parser->collection_stack[parser->collection_stack_ptr++] =
158 parser->device->maxcollection;
160 collection = parser->device->collection +
161 parser->device->maxcollection++;
162 collection->type = type;
163 collection->usage = usage;
164 collection->level = parser->collection_stack_ptr - 1;
166 if (type == HID_COLLECTION_APPLICATION)
167 parser->device->maxapplication++;
169 return 0;
173 * Close a collection.
176 static int close_collection(struct hid_parser *parser)
178 if (!parser->collection_stack_ptr) {
179 hid_err(parser->device, "collection stack underflow\n");
180 return -EINVAL;
182 parser->collection_stack_ptr--;
183 return 0;
187 * Climb up the stack, search for the specified collection type
188 * and return the usage.
191 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
193 struct hid_collection *collection = parser->device->collection;
194 int n;
196 for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
197 unsigned index = parser->collection_stack[n];
198 if (collection[index].type == type)
199 return collection[index].usage;
201 return 0; /* we know nothing about this usage type */
205 * Add a usage to the temporary parser table.
208 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
210 if (parser->local.usage_index >= HID_MAX_USAGES) {
211 hid_err(parser->device, "usage index exceeded\n");
212 return -1;
214 parser->local.usage[parser->local.usage_index] = usage;
215 parser->local.collection_index[parser->local.usage_index] =
216 parser->collection_stack_ptr ?
217 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
218 parser->local.usage_index++;
219 return 0;
223 * Register a new field for this report.
226 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
228 struct hid_report *report;
229 struct hid_field *field;
230 unsigned int usages;
231 unsigned int offset;
232 unsigned int i;
233 unsigned int application;
235 application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
237 report = hid_register_report(parser->device, report_type,
238 parser->global.report_id, application);
239 if (!report) {
240 hid_err(parser->device, "hid_register_report failed\n");
241 return -1;
244 /* Handle both signed and unsigned cases properly */
245 if ((parser->global.logical_minimum < 0 &&
246 parser->global.logical_maximum <
247 parser->global.logical_minimum) ||
248 (parser->global.logical_minimum >= 0 &&
249 (__u32)parser->global.logical_maximum <
250 (__u32)parser->global.logical_minimum)) {
251 dbg_hid("logical range invalid 0x%x 0x%x\n",
252 parser->global.logical_minimum,
253 parser->global.logical_maximum);
254 return -1;
257 offset = report->size;
258 report->size += parser->global.report_size * parser->global.report_count;
260 if (!parser->local.usage_index) /* Ignore padding fields */
261 return 0;
263 usages = max_t(unsigned, parser->local.usage_index,
264 parser->global.report_count);
266 field = hid_register_field(report, usages, parser->global.report_count);
267 if (!field)
268 return 0;
270 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
271 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
272 field->application = application;
274 for (i = 0; i < usages; i++) {
275 unsigned j = i;
276 /* Duplicate the last usage we parsed if we have excess values */
277 if (i >= parser->local.usage_index)
278 j = parser->local.usage_index - 1;
279 field->usage[i].hid = parser->local.usage[j];
280 field->usage[i].collection_index =
281 parser->local.collection_index[j];
282 field->usage[i].usage_index = i;
285 field->maxusage = usages;
286 field->flags = flags;
287 field->report_offset = offset;
288 field->report_type = report_type;
289 field->report_size = parser->global.report_size;
290 field->report_count = parser->global.report_count;
291 field->logical_minimum = parser->global.logical_minimum;
292 field->logical_maximum = parser->global.logical_maximum;
293 field->physical_minimum = parser->global.physical_minimum;
294 field->physical_maximum = parser->global.physical_maximum;
295 field->unit_exponent = parser->global.unit_exponent;
296 field->unit = parser->global.unit;
298 return 0;
302 * Read data value from item.
305 static u32 item_udata(struct hid_item *item)
307 switch (item->size) {
308 case 1: return item->data.u8;
309 case 2: return item->data.u16;
310 case 4: return item->data.u32;
312 return 0;
315 static s32 item_sdata(struct hid_item *item)
317 switch (item->size) {
318 case 1: return item->data.s8;
319 case 2: return item->data.s16;
320 case 4: return item->data.s32;
322 return 0;
326 * Process a global item.
329 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
331 __s32 raw_value;
332 switch (item->tag) {
333 case HID_GLOBAL_ITEM_TAG_PUSH:
335 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
336 hid_err(parser->device, "global environment stack overflow\n");
337 return -1;
340 memcpy(parser->global_stack + parser->global_stack_ptr++,
341 &parser->global, sizeof(struct hid_global));
342 return 0;
344 case HID_GLOBAL_ITEM_TAG_POP:
346 if (!parser->global_stack_ptr) {
347 hid_err(parser->device, "global environment stack underflow\n");
348 return -1;
351 memcpy(&parser->global, parser->global_stack +
352 --parser->global_stack_ptr, sizeof(struct hid_global));
353 return 0;
355 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
356 parser->global.usage_page = item_udata(item);
357 return 0;
359 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
360 parser->global.logical_minimum = item_sdata(item);
361 return 0;
363 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
364 if (parser->global.logical_minimum < 0)
365 parser->global.logical_maximum = item_sdata(item);
366 else
367 parser->global.logical_maximum = item_udata(item);
368 return 0;
370 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
371 parser->global.physical_minimum = item_sdata(item);
372 return 0;
374 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
375 if (parser->global.physical_minimum < 0)
376 parser->global.physical_maximum = item_sdata(item);
377 else
378 parser->global.physical_maximum = item_udata(item);
379 return 0;
381 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
382 /* Many devices provide unit exponent as a two's complement
383 * nibble due to the common misunderstanding of HID
384 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
385 * both this and the standard encoding. */
386 raw_value = item_sdata(item);
387 if (!(raw_value & 0xfffffff0))
388 parser->global.unit_exponent = hid_snto32(raw_value, 4);
389 else
390 parser->global.unit_exponent = raw_value;
391 return 0;
393 case HID_GLOBAL_ITEM_TAG_UNIT:
394 parser->global.unit = item_udata(item);
395 return 0;
397 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
398 parser->global.report_size = item_udata(item);
399 if (parser->global.report_size > 128) {
400 hid_err(parser->device, "invalid report_size %d\n",
401 parser->global.report_size);
402 return -1;
404 return 0;
406 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
407 parser->global.report_count = item_udata(item);
408 if (parser->global.report_count > HID_MAX_USAGES) {
409 hid_err(parser->device, "invalid report_count %d\n",
410 parser->global.report_count);
411 return -1;
413 return 0;
415 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
416 parser->global.report_id = item_udata(item);
417 if (parser->global.report_id == 0 ||
418 parser->global.report_id >= HID_MAX_IDS) {
419 hid_err(parser->device, "report_id %u is invalid\n",
420 parser->global.report_id);
421 return -1;
423 return 0;
425 default:
426 hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
427 return -1;
432 * Process a local item.
435 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
437 __u32 data;
438 unsigned n;
439 __u32 count;
441 data = item_udata(item);
443 switch (item->tag) {
444 case HID_LOCAL_ITEM_TAG_DELIMITER:
446 if (data) {
448 * We treat items before the first delimiter
449 * as global to all usage sets (branch 0).
450 * In the moment we process only these global
451 * items and the first delimiter set.
453 if (parser->local.delimiter_depth != 0) {
454 hid_err(parser->device, "nested delimiters\n");
455 return -1;
457 parser->local.delimiter_depth++;
458 parser->local.delimiter_branch++;
459 } else {
460 if (parser->local.delimiter_depth < 1) {
461 hid_err(parser->device, "bogus close delimiter\n");
462 return -1;
464 parser->local.delimiter_depth--;
466 return 0;
468 case HID_LOCAL_ITEM_TAG_USAGE:
470 if (parser->local.delimiter_branch > 1) {
471 dbg_hid("alternative usage ignored\n");
472 return 0;
475 if (item->size <= 2)
476 data = (parser->global.usage_page << 16) + data;
478 return hid_add_usage(parser, data);
480 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
482 if (parser->local.delimiter_branch > 1) {
483 dbg_hid("alternative usage ignored\n");
484 return 0;
487 if (item->size <= 2)
488 data = (parser->global.usage_page << 16) + data;
490 parser->local.usage_minimum = data;
491 return 0;
493 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
495 if (parser->local.delimiter_branch > 1) {
496 dbg_hid("alternative usage ignored\n");
497 return 0;
500 if (item->size <= 2)
501 data = (parser->global.usage_page << 16) + data;
503 count = data - parser->local.usage_minimum;
504 if (count + parser->local.usage_index >= HID_MAX_USAGES) {
506 * We do not warn if the name is not set, we are
507 * actually pre-scanning the device.
509 if (dev_name(&parser->device->dev))
510 hid_warn(parser->device,
511 "ignoring exceeding usage max\n");
512 data = HID_MAX_USAGES - parser->local.usage_index +
513 parser->local.usage_minimum - 1;
514 if (data <= 0) {
515 hid_err(parser->device,
516 "no more usage index available\n");
517 return -1;
521 for (n = parser->local.usage_minimum; n <= data; n++)
522 if (hid_add_usage(parser, n)) {
523 dbg_hid("hid_add_usage failed\n");
524 return -1;
526 return 0;
528 default:
530 dbg_hid("unknown local item tag 0x%x\n", item->tag);
531 return 0;
533 return 0;
537 * Process a main item.
540 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
542 __u32 data;
543 int ret;
545 data = item_udata(item);
547 switch (item->tag) {
548 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
549 ret = open_collection(parser, data & 0xff);
550 break;
551 case HID_MAIN_ITEM_TAG_END_COLLECTION:
552 ret = close_collection(parser);
553 break;
554 case HID_MAIN_ITEM_TAG_INPUT:
555 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
556 break;
557 case HID_MAIN_ITEM_TAG_OUTPUT:
558 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
559 break;
560 case HID_MAIN_ITEM_TAG_FEATURE:
561 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
562 break;
563 default:
564 hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
565 ret = 0;
568 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
570 return ret;
574 * Process a reserved item.
577 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
579 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
580 return 0;
584 * Free a report and all registered fields. The field->usage and
585 * field->value table's are allocated behind the field, so we need
586 * only to free(field) itself.
589 static void hid_free_report(struct hid_report *report)
591 unsigned n;
593 for (n = 0; n < report->maxfield; n++)
594 kfree(report->field[n]);
595 kfree(report);
599 * Close report. This function returns the device
600 * state to the point prior to hid_open_report().
602 static void hid_close_report(struct hid_device *device)
604 unsigned i, j;
606 for (i = 0; i < HID_REPORT_TYPES; i++) {
607 struct hid_report_enum *report_enum = device->report_enum + i;
609 for (j = 0; j < HID_MAX_IDS; j++) {
610 struct hid_report *report = report_enum->report_id_hash[j];
611 if (report)
612 hid_free_report(report);
614 memset(report_enum, 0, sizeof(*report_enum));
615 INIT_LIST_HEAD(&report_enum->report_list);
618 kfree(device->rdesc);
619 device->rdesc = NULL;
620 device->rsize = 0;
622 kfree(device->collection);
623 device->collection = NULL;
624 device->collection_size = 0;
625 device->maxcollection = 0;
626 device->maxapplication = 0;
628 device->status &= ~HID_STAT_PARSED;
632 * Free a device structure, all reports, and all fields.
635 static void hid_device_release(struct device *dev)
637 struct hid_device *hid = to_hid_device(dev);
639 hid_close_report(hid);
640 kfree(hid->dev_rdesc);
641 kfree(hid);
645 * Fetch a report description item from the data stream. We support long
646 * items, though they are not used yet.
649 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
651 u8 b;
653 if ((end - start) <= 0)
654 return NULL;
656 b = *start++;
658 item->type = (b >> 2) & 3;
659 item->tag = (b >> 4) & 15;
661 if (item->tag == HID_ITEM_TAG_LONG) {
663 item->format = HID_ITEM_FORMAT_LONG;
665 if ((end - start) < 2)
666 return NULL;
668 item->size = *start++;
669 item->tag = *start++;
671 if ((end - start) < item->size)
672 return NULL;
674 item->data.longdata = start;
675 start += item->size;
676 return start;
679 item->format = HID_ITEM_FORMAT_SHORT;
680 item->size = b & 3;
682 switch (item->size) {
683 case 0:
684 return start;
686 case 1:
687 if ((end - start) < 1)
688 return NULL;
689 item->data.u8 = *start++;
690 return start;
692 case 2:
693 if ((end - start) < 2)
694 return NULL;
695 item->data.u16 = get_unaligned_le16(start);
696 start = (__u8 *)((__le16 *)start + 1);
697 return start;
699 case 3:
700 item->size++;
701 if ((end - start) < 4)
702 return NULL;
703 item->data.u32 = get_unaligned_le32(start);
704 start = (__u8 *)((__le32 *)start + 1);
705 return start;
708 return NULL;
711 static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
713 struct hid_device *hid = parser->device;
715 if (usage == HID_DG_CONTACTID)
716 hid->group = HID_GROUP_MULTITOUCH;
719 static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
721 if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
722 parser->global.report_size == 8)
723 parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
726 static void hid_scan_collection(struct hid_parser *parser, unsigned type)
728 struct hid_device *hid = parser->device;
729 int i;
731 if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
732 type == HID_COLLECTION_PHYSICAL)
733 hid->group = HID_GROUP_SENSOR_HUB;
735 if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
736 hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
737 hid->group == HID_GROUP_MULTITOUCH)
738 hid->group = HID_GROUP_GENERIC;
740 if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
741 for (i = 0; i < parser->local.usage_index; i++)
742 if (parser->local.usage[i] == HID_GD_POINTER)
743 parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
745 if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
746 parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
749 static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
751 __u32 data;
752 int i;
754 data = item_udata(item);
756 switch (item->tag) {
757 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
758 hid_scan_collection(parser, data & 0xff);
759 break;
760 case HID_MAIN_ITEM_TAG_END_COLLECTION:
761 break;
762 case HID_MAIN_ITEM_TAG_INPUT:
763 /* ignore constant inputs, they will be ignored by hid-input */
764 if (data & HID_MAIN_ITEM_CONSTANT)
765 break;
766 for (i = 0; i < parser->local.usage_index; i++)
767 hid_scan_input_usage(parser, parser->local.usage[i]);
768 break;
769 case HID_MAIN_ITEM_TAG_OUTPUT:
770 break;
771 case HID_MAIN_ITEM_TAG_FEATURE:
772 for (i = 0; i < parser->local.usage_index; i++)
773 hid_scan_feature_usage(parser, parser->local.usage[i]);
774 break;
777 /* Reset the local parser environment */
778 memset(&parser->local, 0, sizeof(parser->local));
780 return 0;
784 * Scan a report descriptor before the device is added to the bus.
785 * Sets device groups and other properties that determine what driver
786 * to load.
788 static int hid_scan_report(struct hid_device *hid)
790 struct hid_parser *parser;
791 struct hid_item item;
792 __u8 *start = hid->dev_rdesc;
793 __u8 *end = start + hid->dev_rsize;
794 static int (*dispatch_type[])(struct hid_parser *parser,
795 struct hid_item *item) = {
796 hid_scan_main,
797 hid_parser_global,
798 hid_parser_local,
799 hid_parser_reserved
802 parser = vzalloc(sizeof(struct hid_parser));
803 if (!parser)
804 return -ENOMEM;
806 parser->device = hid;
807 hid->group = HID_GROUP_GENERIC;
810 * The parsing is simpler than the one in hid_open_report() as we should
811 * be robust against hid errors. Those errors will be raised by
812 * hid_open_report() anyway.
814 while ((start = fetch_item(start, end, &item)) != NULL)
815 dispatch_type[item.type](parser, &item);
818 * Handle special flags set during scanning.
820 if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
821 (hid->group == HID_GROUP_MULTITOUCH))
822 hid->group = HID_GROUP_MULTITOUCH_WIN_8;
825 * Vendor specific handlings
827 switch (hid->vendor) {
828 case USB_VENDOR_ID_WACOM:
829 hid->group = HID_GROUP_WACOM;
830 break;
831 case USB_VENDOR_ID_SYNAPTICS:
832 if (hid->group == HID_GROUP_GENERIC)
833 if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
834 && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
836 * hid-rmi should take care of them,
837 * not hid-generic
839 hid->group = HID_GROUP_RMI;
840 break;
843 vfree(parser);
844 return 0;
848 * hid_parse_report - parse device report
850 * @device: hid device
851 * @start: report start
852 * @size: report size
854 * Allocate the device report as read by the bus driver. This function should
855 * only be called from parse() in ll drivers.
857 int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
859 hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
860 if (!hid->dev_rdesc)
861 return -ENOMEM;
862 hid->dev_rsize = size;
863 return 0;
865 EXPORT_SYMBOL_GPL(hid_parse_report);
867 static const char * const hid_report_names[] = {
868 "HID_INPUT_REPORT",
869 "HID_OUTPUT_REPORT",
870 "HID_FEATURE_REPORT",
873 * hid_validate_values - validate existing device report's value indexes
875 * @device: hid device
876 * @type: which report type to examine
877 * @id: which report ID to examine (0 for first)
878 * @field_index: which report field to examine
879 * @report_counts: expected number of values
881 * Validate the number of values in a given field of a given report, after
882 * parsing.
884 struct hid_report *hid_validate_values(struct hid_device *hid,
885 unsigned int type, unsigned int id,
886 unsigned int field_index,
887 unsigned int report_counts)
889 struct hid_report *report;
891 if (type > HID_FEATURE_REPORT) {
892 hid_err(hid, "invalid HID report type %u\n", type);
893 return NULL;
896 if (id >= HID_MAX_IDS) {
897 hid_err(hid, "invalid HID report id %u\n", id);
898 return NULL;
902 * Explicitly not using hid_get_report() here since it depends on
903 * ->numbered being checked, which may not always be the case when
904 * drivers go to access report values.
906 if (id == 0) {
908 * Validating on id 0 means we should examine the first
909 * report in the list.
911 report = list_entry(
912 hid->report_enum[type].report_list.next,
913 struct hid_report, list);
914 } else {
915 report = hid->report_enum[type].report_id_hash[id];
917 if (!report) {
918 hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
919 return NULL;
921 if (report->maxfield <= field_index) {
922 hid_err(hid, "not enough fields in %s %u\n",
923 hid_report_names[type], id);
924 return NULL;
926 if (report->field[field_index]->report_count < report_counts) {
927 hid_err(hid, "not enough values in %s %u field %u\n",
928 hid_report_names[type], id, field_index);
929 return NULL;
931 return report;
933 EXPORT_SYMBOL_GPL(hid_validate_values);
936 * hid_open_report - open a driver-specific device report
938 * @device: hid device
940 * Parse a report description into a hid_device structure. Reports are
941 * enumerated, fields are attached to these reports.
942 * 0 returned on success, otherwise nonzero error value.
944 * This function (or the equivalent hid_parse() macro) should only be
945 * called from probe() in drivers, before starting the device.
947 int hid_open_report(struct hid_device *device)
949 struct hid_parser *parser;
950 struct hid_item item;
951 unsigned int size;
952 __u8 *start;
953 __u8 *buf;
954 __u8 *end;
955 int ret;
956 static int (*dispatch_type[])(struct hid_parser *parser,
957 struct hid_item *item) = {
958 hid_parser_main,
959 hid_parser_global,
960 hid_parser_local,
961 hid_parser_reserved
964 if (WARN_ON(device->status & HID_STAT_PARSED))
965 return -EBUSY;
967 start = device->dev_rdesc;
968 if (WARN_ON(!start))
969 return -ENODEV;
970 size = device->dev_rsize;
972 buf = kmemdup(start, size, GFP_KERNEL);
973 if (buf == NULL)
974 return -ENOMEM;
976 if (device->driver->report_fixup)
977 start = device->driver->report_fixup(device, buf, &size);
978 else
979 start = buf;
981 start = kmemdup(start, size, GFP_KERNEL);
982 kfree(buf);
983 if (start == NULL)
984 return -ENOMEM;
986 device->rdesc = start;
987 device->rsize = size;
989 parser = vzalloc(sizeof(struct hid_parser));
990 if (!parser) {
991 ret = -ENOMEM;
992 goto err;
995 parser->device = device;
997 end = start + size;
999 device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1000 sizeof(struct hid_collection), GFP_KERNEL);
1001 if (!device->collection) {
1002 ret = -ENOMEM;
1003 goto err;
1005 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1007 ret = -EINVAL;
1008 while ((start = fetch_item(start, end, &item)) != NULL) {
1010 if (item.format != HID_ITEM_FORMAT_SHORT) {
1011 hid_err(device, "unexpected long global item\n");
1012 goto err;
1015 if (dispatch_type[item.type](parser, &item)) {
1016 hid_err(device, "item %u %u %u %u parsing failed\n",
1017 item.format, (unsigned)item.size,
1018 (unsigned)item.type, (unsigned)item.tag);
1019 goto err;
1022 if (start == end) {
1023 if (parser->collection_stack_ptr) {
1024 hid_err(device, "unbalanced collection at end of report description\n");
1025 goto err;
1027 if (parser->local.delimiter_depth) {
1028 hid_err(device, "unbalanced delimiter at end of report description\n");
1029 goto err;
1031 vfree(parser);
1032 device->status |= HID_STAT_PARSED;
1033 return 0;
1037 hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
1038 err:
1039 vfree(parser);
1040 hid_close_report(device);
1041 return ret;
1043 EXPORT_SYMBOL_GPL(hid_open_report);
1046 * Convert a signed n-bit integer to signed 32-bit integer. Common
1047 * cases are done through the compiler, the screwed things has to be
1048 * done by hand.
1051 static s32 snto32(__u32 value, unsigned n)
1053 switch (n) {
1054 case 8: return ((__s8)value);
1055 case 16: return ((__s16)value);
1056 case 32: return ((__s32)value);
1058 return value & (1 << (n - 1)) ? value | (~0U << n) : value;
1061 s32 hid_snto32(__u32 value, unsigned n)
1063 return snto32(value, n);
1065 EXPORT_SYMBOL_GPL(hid_snto32);
1068 * Convert a signed 32-bit integer to a signed n-bit integer.
1071 static u32 s32ton(__s32 value, unsigned n)
1073 s32 a = value >> (n - 1);
1074 if (a && a != -1)
1075 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1076 return value & ((1 << n) - 1);
1080 * Extract/implement a data field from/to a little endian report (bit array).
1082 * Code sort-of follows HID spec:
1083 * http://www.usb.org/developers/hidpage/HID1_11.pdf
1085 * While the USB HID spec allows unlimited length bit fields in "report
1086 * descriptors", most devices never use more than 16 bits.
1087 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1088 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1091 static u32 __extract(u8 *report, unsigned offset, int n)
1093 unsigned int idx = offset / 8;
1094 unsigned int bit_nr = 0;
1095 unsigned int bit_shift = offset % 8;
1096 int bits_to_copy = 8 - bit_shift;
1097 u32 value = 0;
1098 u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
1100 while (n > 0) {
1101 value |= ((u32)report[idx] >> bit_shift) << bit_nr;
1102 n -= bits_to_copy;
1103 bit_nr += bits_to_copy;
1104 bits_to_copy = 8;
1105 bit_shift = 0;
1106 idx++;
1109 return value & mask;
1112 u32 hid_field_extract(const struct hid_device *hid, u8 *report,
1113 unsigned offset, unsigned n)
1115 if (n > 32) {
1116 hid_warn(hid, "hid_field_extract() called with n (%d) > 32! (%s)\n",
1117 n, current->comm);
1118 n = 32;
1121 return __extract(report, offset, n);
1123 EXPORT_SYMBOL_GPL(hid_field_extract);
1126 * "implement" : set bits in a little endian bit stream.
1127 * Same concepts as "extract" (see comments above).
1128 * The data mangled in the bit stream remains in little endian
1129 * order the whole time. It make more sense to talk about
1130 * endianness of register values by considering a register
1131 * a "cached" copy of the little endian bit stream.
1134 static void __implement(u8 *report, unsigned offset, int n, u32 value)
1136 unsigned int idx = offset / 8;
1137 unsigned int bit_shift = offset % 8;
1138 int bits_to_set = 8 - bit_shift;
1140 while (n - bits_to_set >= 0) {
1141 report[idx] &= ~(0xff << bit_shift);
1142 report[idx] |= value << bit_shift;
1143 value >>= bits_to_set;
1144 n -= bits_to_set;
1145 bits_to_set = 8;
1146 bit_shift = 0;
1147 idx++;
1150 /* last nibble */
1151 if (n) {
1152 u8 bit_mask = ((1U << n) - 1);
1153 report[idx] &= ~(bit_mask << bit_shift);
1154 report[idx] |= value << bit_shift;
1158 static void implement(const struct hid_device *hid, u8 *report,
1159 unsigned offset, unsigned n, u32 value)
1161 if (unlikely(n > 32)) {
1162 hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
1163 __func__, n, current->comm);
1164 n = 32;
1165 } else if (n < 32) {
1166 u32 m = (1U << n) - 1;
1168 if (unlikely(value > m)) {
1169 hid_warn(hid,
1170 "%s() called with too large value %d (n: %d)! (%s)\n",
1171 __func__, value, n, current->comm);
1172 WARN_ON(1);
1173 value &= m;
1177 __implement(report, offset, n, value);
1181 * Search an array for a value.
1184 static int search(__s32 *array, __s32 value, unsigned n)
1186 while (n--) {
1187 if (*array++ == value)
1188 return 0;
1190 return -1;
1194 * hid_match_report - check if driver's raw_event should be called
1196 * @hid: hid device
1197 * @report_type: type to match against
1199 * compare hid->driver->report_table->report_type to report->type
1201 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1203 const struct hid_report_id *id = hid->driver->report_table;
1205 if (!id) /* NULL means all */
1206 return 1;
1208 for (; id->report_type != HID_TERMINATOR; id++)
1209 if (id->report_type == HID_ANY_ID ||
1210 id->report_type == report->type)
1211 return 1;
1212 return 0;
1216 * hid_match_usage - check if driver's event should be called
1218 * @hid: hid device
1219 * @usage: usage to match against
1221 * compare hid->driver->usage_table->usage_{type,code} to
1222 * usage->usage_{type,code}
1224 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
1226 const struct hid_usage_id *id = hid->driver->usage_table;
1228 if (!id) /* NULL means all */
1229 return 1;
1231 for (; id->usage_type != HID_ANY_ID - 1; id++)
1232 if ((id->usage_hid == HID_ANY_ID ||
1233 id->usage_hid == usage->hid) &&
1234 (id->usage_type == HID_ANY_ID ||
1235 id->usage_type == usage->type) &&
1236 (id->usage_code == HID_ANY_ID ||
1237 id->usage_code == usage->code))
1238 return 1;
1239 return 0;
1242 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
1243 struct hid_usage *usage, __s32 value, int interrupt)
1245 struct hid_driver *hdrv = hid->driver;
1246 int ret;
1248 if (!list_empty(&hid->debug_list))
1249 hid_dump_input(hid, usage, value);
1251 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
1252 ret = hdrv->event(hid, field, usage, value);
1253 if (ret != 0) {
1254 if (ret < 0)
1255 hid_err(hid, "%s's event failed with %d\n",
1256 hdrv->name, ret);
1257 return;
1261 if (hid->claimed & HID_CLAIMED_INPUT)
1262 hidinput_hid_event(hid, field, usage, value);
1263 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1264 hid->hiddev_hid_event(hid, field, usage, value);
1268 * Analyse a received field, and fetch the data from it. The field
1269 * content is stored for next report processing (we do differential
1270 * reporting to the layer).
1273 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
1274 __u8 *data, int interrupt)
1276 unsigned n;
1277 unsigned count = field->report_count;
1278 unsigned offset = field->report_offset;
1279 unsigned size = field->report_size;
1280 __s32 min = field->logical_minimum;
1281 __s32 max = field->logical_maximum;
1282 __s32 *value;
1284 value = kmalloc_array(count, sizeof(__s32), GFP_ATOMIC);
1285 if (!value)
1286 return;
1288 for (n = 0; n < count; n++) {
1290 value[n] = min < 0 ?
1291 snto32(hid_field_extract(hid, data, offset + n * size,
1292 size), size) :
1293 hid_field_extract(hid, data, offset + n * size, size);
1295 /* Ignore report if ErrorRollOver */
1296 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
1297 value[n] >= min && value[n] <= max &&
1298 value[n] - min < field->maxusage &&
1299 field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1300 goto exit;
1303 for (n = 0; n < count; n++) {
1305 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1306 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1307 continue;
1310 if (field->value[n] >= min && field->value[n] <= max
1311 && field->value[n] - min < field->maxusage
1312 && field->usage[field->value[n] - min].hid
1313 && search(value, field->value[n], count))
1314 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1316 if (value[n] >= min && value[n] <= max
1317 && value[n] - min < field->maxusage
1318 && field->usage[value[n] - min].hid
1319 && search(field->value, value[n], count))
1320 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1323 memcpy(field->value, value, count * sizeof(__s32));
1324 exit:
1325 kfree(value);
1329 * Output the field into the report.
1332 static void hid_output_field(const struct hid_device *hid,
1333 struct hid_field *field, __u8 *data)
1335 unsigned count = field->report_count;
1336 unsigned offset = field->report_offset;
1337 unsigned size = field->report_size;
1338 unsigned n;
1340 for (n = 0; n < count; n++) {
1341 if (field->logical_minimum < 0) /* signed values */
1342 implement(hid, data, offset + n * size, size,
1343 s32ton(field->value[n], size));
1344 else /* unsigned values */
1345 implement(hid, data, offset + n * size, size,
1346 field->value[n]);
1351 * Create a report. 'data' has to be allocated using
1352 * hid_alloc_report_buf() so that it has proper size.
1355 void hid_output_report(struct hid_report *report, __u8 *data)
1357 unsigned n;
1359 if (report->id > 0)
1360 *data++ = report->id;
1362 memset(data, 0, ((report->size - 1) >> 3) + 1);
1363 for (n = 0; n < report->maxfield; n++)
1364 hid_output_field(report->device, report->field[n], data);
1366 EXPORT_SYMBOL_GPL(hid_output_report);
1369 * Allocator for buffer that is going to be passed to hid_output_report()
1371 u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
1374 * 7 extra bytes are necessary to achieve proper functionality
1375 * of implement() working on 8 byte chunks
1378 u32 len = hid_report_len(report) + 7;
1380 return kmalloc(len, flags);
1382 EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
1385 * Set a field value. The report this field belongs to has to be
1386 * created and transferred to the device, to set this value in the
1387 * device.
1390 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1392 unsigned size;
1394 if (!field)
1395 return -1;
1397 size = field->report_size;
1399 hid_dump_input(field->report->device, field->usage + offset, value);
1401 if (offset >= field->report_count) {
1402 hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
1403 offset, field->report_count);
1404 return -1;
1406 if (field->logical_minimum < 0) {
1407 if (value != snto32(s32ton(value, size), size)) {
1408 hid_err(field->report->device, "value %d is out of range\n", value);
1409 return -1;
1412 field->value[offset] = value;
1413 return 0;
1415 EXPORT_SYMBOL_GPL(hid_set_field);
1417 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1418 const u8 *data)
1420 struct hid_report *report;
1421 unsigned int n = 0; /* Normally report number is 0 */
1423 /* Device uses numbered reports, data[0] is report number */
1424 if (report_enum->numbered)
1425 n = *data;
1427 report = report_enum->report_id_hash[n];
1428 if (report == NULL)
1429 dbg_hid("undefined report_id %u received\n", n);
1431 return report;
1435 * Implement a generic .request() callback, using .raw_request()
1436 * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
1438 void __hid_request(struct hid_device *hid, struct hid_report *report,
1439 int reqtype)
1441 char *buf;
1442 int ret;
1443 u32 len;
1445 buf = hid_alloc_report_buf(report, GFP_KERNEL);
1446 if (!buf)
1447 return;
1449 len = hid_report_len(report);
1451 if (reqtype == HID_REQ_SET_REPORT)
1452 hid_output_report(report, buf);
1454 ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
1455 report->type, reqtype);
1456 if (ret < 0) {
1457 dbg_hid("unable to complete request: %d\n", ret);
1458 goto out;
1461 if (reqtype == HID_REQ_GET_REPORT)
1462 hid_input_report(hid, report->type, buf, ret, 0);
1464 out:
1465 kfree(buf);
1467 EXPORT_SYMBOL_GPL(__hid_request);
1469 int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
1470 int interrupt)
1472 struct hid_report_enum *report_enum = hid->report_enum + type;
1473 struct hid_report *report;
1474 struct hid_driver *hdrv;
1475 unsigned int a;
1476 u32 rsize, csize = size;
1477 u8 *cdata = data;
1478 int ret = 0;
1480 report = hid_get_report(report_enum, data);
1481 if (!report)
1482 goto out;
1484 if (report_enum->numbered) {
1485 cdata++;
1486 csize--;
1489 rsize = ((report->size - 1) >> 3) + 1;
1491 if (rsize > HID_MAX_BUFFER_SIZE)
1492 rsize = HID_MAX_BUFFER_SIZE;
1494 if (csize < rsize) {
1495 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1496 csize, rsize);
1497 memset(cdata + csize, 0, rsize - csize);
1500 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1501 hid->hiddev_report_event(hid, report);
1502 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1503 ret = hidraw_report_event(hid, data, size);
1504 if (ret)
1505 goto out;
1508 if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
1509 for (a = 0; a < report->maxfield; a++)
1510 hid_input_field(hid, report->field[a], cdata, interrupt);
1511 hdrv = hid->driver;
1512 if (hdrv && hdrv->report)
1513 hdrv->report(hid, report);
1516 if (hid->claimed & HID_CLAIMED_INPUT)
1517 hidinput_report_event(hid, report);
1518 out:
1519 return ret;
1521 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1524 * hid_input_report - report data from lower layer (usb, bt...)
1526 * @hid: hid device
1527 * @type: HID report type (HID_*_REPORT)
1528 * @data: report contents
1529 * @size: size of data parameter
1530 * @interrupt: distinguish between interrupt and control transfers
1532 * This is data entry for lower layers.
1534 int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt)
1536 struct hid_report_enum *report_enum;
1537 struct hid_driver *hdrv;
1538 struct hid_report *report;
1539 int ret = 0;
1541 if (!hid)
1542 return -ENODEV;
1544 if (down_trylock(&hid->driver_input_lock))
1545 return -EBUSY;
1547 if (!hid->driver) {
1548 ret = -ENODEV;
1549 goto unlock;
1551 report_enum = hid->report_enum + type;
1552 hdrv = hid->driver;
1554 if (!size) {
1555 dbg_hid("empty report\n");
1556 ret = -1;
1557 goto unlock;
1560 /* Avoid unnecessary overhead if debugfs is disabled */
1561 if (!list_empty(&hid->debug_list))
1562 hid_dump_report(hid, type, data, size);
1564 report = hid_get_report(report_enum, data);
1566 if (!report) {
1567 ret = -1;
1568 goto unlock;
1571 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1572 ret = hdrv->raw_event(hid, report, data, size);
1573 if (ret < 0)
1574 goto unlock;
1577 ret = hid_report_raw_event(hid, type, data, size, interrupt);
1579 unlock:
1580 up(&hid->driver_input_lock);
1581 return ret;
1583 EXPORT_SYMBOL_GPL(hid_input_report);
1585 bool hid_match_one_id(const struct hid_device *hdev,
1586 const struct hid_device_id *id)
1588 return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
1589 (id->group == HID_GROUP_ANY || id->group == hdev->group) &&
1590 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1591 (id->product == HID_ANY_ID || id->product == hdev->product);
1594 const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
1595 const struct hid_device_id *id)
1597 for (; id->bus; id++)
1598 if (hid_match_one_id(hdev, id))
1599 return id;
1601 return NULL;
1604 static const struct hid_device_id hid_hiddev_list[] = {
1605 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1606 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1610 static bool hid_hiddev(struct hid_device *hdev)
1612 return !!hid_match_id(hdev, hid_hiddev_list);
1616 static ssize_t
1617 read_report_descriptor(struct file *filp, struct kobject *kobj,
1618 struct bin_attribute *attr,
1619 char *buf, loff_t off, size_t count)
1621 struct device *dev = kobj_to_dev(kobj);
1622 struct hid_device *hdev = to_hid_device(dev);
1624 if (off >= hdev->rsize)
1625 return 0;
1627 if (off + count > hdev->rsize)
1628 count = hdev->rsize - off;
1630 memcpy(buf, hdev->rdesc + off, count);
1632 return count;
1635 static ssize_t
1636 show_country(struct device *dev, struct device_attribute *attr,
1637 char *buf)
1639 struct hid_device *hdev = to_hid_device(dev);
1641 return sprintf(buf, "%02x\n", hdev->country & 0xff);
1644 static struct bin_attribute dev_bin_attr_report_desc = {
1645 .attr = { .name = "report_descriptor", .mode = 0444 },
1646 .read = read_report_descriptor,
1647 .size = HID_MAX_DESCRIPTOR_SIZE,
1650 static const struct device_attribute dev_attr_country = {
1651 .attr = { .name = "country", .mode = 0444 },
1652 .show = show_country,
1655 int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1657 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1658 "Joystick", "Gamepad", "Keyboard", "Keypad",
1659 "Multi-Axis Controller"
1661 const char *type, *bus;
1662 char buf[64] = "";
1663 unsigned int i;
1664 int len;
1665 int ret;
1667 if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1668 connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
1669 if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
1670 connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
1671 if (hdev->bus != BUS_USB)
1672 connect_mask &= ~HID_CONNECT_HIDDEV;
1673 if (hid_hiddev(hdev))
1674 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1676 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1677 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1678 hdev->claimed |= HID_CLAIMED_INPUT;
1680 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1681 !hdev->hiddev_connect(hdev,
1682 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1683 hdev->claimed |= HID_CLAIMED_HIDDEV;
1684 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1685 hdev->claimed |= HID_CLAIMED_HIDRAW;
1687 if (connect_mask & HID_CONNECT_DRIVER)
1688 hdev->claimed |= HID_CLAIMED_DRIVER;
1690 /* Drivers with the ->raw_event callback set are not required to connect
1691 * to any other listener. */
1692 if (!hdev->claimed && !hdev->driver->raw_event) {
1693 hid_err(hdev, "device has no listeners, quitting\n");
1694 return -ENODEV;
1697 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1698 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1699 hdev->ff_init(hdev);
1701 len = 0;
1702 if (hdev->claimed & HID_CLAIMED_INPUT)
1703 len += sprintf(buf + len, "input");
1704 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1705 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1706 ((struct hiddev *)hdev->hiddev)->minor);
1707 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1708 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1709 ((struct hidraw *)hdev->hidraw)->minor);
1711 type = "Device";
1712 for (i = 0; i < hdev->maxcollection; i++) {
1713 struct hid_collection *col = &hdev->collection[i];
1714 if (col->type == HID_COLLECTION_APPLICATION &&
1715 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1716 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1717 type = types[col->usage & 0xffff];
1718 break;
1722 switch (hdev->bus) {
1723 case BUS_USB:
1724 bus = "USB";
1725 break;
1726 case BUS_BLUETOOTH:
1727 bus = "BLUETOOTH";
1728 break;
1729 case BUS_I2C:
1730 bus = "I2C";
1731 break;
1732 default:
1733 bus = "<UNKNOWN>";
1736 ret = device_create_file(&hdev->dev, &dev_attr_country);
1737 if (ret)
1738 hid_warn(hdev,
1739 "can't create sysfs country code attribute err: %d\n", ret);
1741 hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1742 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1743 type, hdev->name, hdev->phys);
1745 return 0;
1747 EXPORT_SYMBOL_GPL(hid_connect);
1749 void hid_disconnect(struct hid_device *hdev)
1751 device_remove_file(&hdev->dev, &dev_attr_country);
1752 if (hdev->claimed & HID_CLAIMED_INPUT)
1753 hidinput_disconnect(hdev);
1754 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1755 hdev->hiddev_disconnect(hdev);
1756 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1757 hidraw_disconnect(hdev);
1758 hdev->claimed = 0;
1760 EXPORT_SYMBOL_GPL(hid_disconnect);
1763 * hid_hw_start - start underlying HW
1764 * @hdev: hid device
1765 * @connect_mask: which outputs to connect, see HID_CONNECT_*
1767 * Call this in probe function *after* hid_parse. This will setup HW
1768 * buffers and start the device (if not defeirred to device open).
1769 * hid_hw_stop must be called if this was successful.
1771 int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1773 int error;
1775 error = hdev->ll_driver->start(hdev);
1776 if (error)
1777 return error;
1779 if (connect_mask) {
1780 error = hid_connect(hdev, connect_mask);
1781 if (error) {
1782 hdev->ll_driver->stop(hdev);
1783 return error;
1787 return 0;
1789 EXPORT_SYMBOL_GPL(hid_hw_start);
1792 * hid_hw_stop - stop underlying HW
1793 * @hdev: hid device
1795 * This is usually called from remove function or from probe when something
1796 * failed and hid_hw_start was called already.
1798 void hid_hw_stop(struct hid_device *hdev)
1800 hid_disconnect(hdev);
1801 hdev->ll_driver->stop(hdev);
1803 EXPORT_SYMBOL_GPL(hid_hw_stop);
1806 * hid_hw_open - signal underlying HW to start delivering events
1807 * @hdev: hid device
1809 * Tell underlying HW to start delivering events from the device.
1810 * This function should be called sometime after successful call
1811 * to hid_hw_start().
1813 int hid_hw_open(struct hid_device *hdev)
1815 int ret;
1817 ret = mutex_lock_killable(&hdev->ll_open_lock);
1818 if (ret)
1819 return ret;
1821 if (!hdev->ll_open_count++) {
1822 ret = hdev->ll_driver->open(hdev);
1823 if (ret)
1824 hdev->ll_open_count--;
1827 mutex_unlock(&hdev->ll_open_lock);
1828 return ret;
1830 EXPORT_SYMBOL_GPL(hid_hw_open);
1833 * hid_hw_close - signal underlaying HW to stop delivering events
1835 * @hdev: hid device
1837 * This function indicates that we are not interested in the events
1838 * from this device anymore. Delivery of events may or may not stop,
1839 * depending on the number of users still outstanding.
1841 void hid_hw_close(struct hid_device *hdev)
1843 mutex_lock(&hdev->ll_open_lock);
1844 if (!--hdev->ll_open_count)
1845 hdev->ll_driver->close(hdev);
1846 mutex_unlock(&hdev->ll_open_lock);
1848 EXPORT_SYMBOL_GPL(hid_hw_close);
1850 struct hid_dynid {
1851 struct list_head list;
1852 struct hid_device_id id;
1856 * store_new_id - add a new HID device ID to this driver and re-probe devices
1857 * @driver: target device driver
1858 * @buf: buffer for scanning device ID data
1859 * @count: input size
1861 * Adds a new dynamic hid device ID to this driver,
1862 * and causes the driver to probe for all devices again.
1864 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
1865 size_t count)
1867 struct hid_driver *hdrv = to_hid_driver(drv);
1868 struct hid_dynid *dynid;
1869 __u32 bus, vendor, product;
1870 unsigned long driver_data = 0;
1871 int ret;
1873 ret = sscanf(buf, "%x %x %x %lx",
1874 &bus, &vendor, &product, &driver_data);
1875 if (ret < 3)
1876 return -EINVAL;
1878 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1879 if (!dynid)
1880 return -ENOMEM;
1882 dynid->id.bus = bus;
1883 dynid->id.group = HID_GROUP_ANY;
1884 dynid->id.vendor = vendor;
1885 dynid->id.product = product;
1886 dynid->id.driver_data = driver_data;
1888 spin_lock(&hdrv->dyn_lock);
1889 list_add_tail(&dynid->list, &hdrv->dyn_list);
1890 spin_unlock(&hdrv->dyn_lock);
1892 ret = driver_attach(&hdrv->driver);
1894 return ret ? : count;
1896 static DRIVER_ATTR_WO(new_id);
1898 static struct attribute *hid_drv_attrs[] = {
1899 &driver_attr_new_id.attr,
1900 NULL,
1902 ATTRIBUTE_GROUPS(hid_drv);
1904 static void hid_free_dynids(struct hid_driver *hdrv)
1906 struct hid_dynid *dynid, *n;
1908 spin_lock(&hdrv->dyn_lock);
1909 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1910 list_del(&dynid->list);
1911 kfree(dynid);
1913 spin_unlock(&hdrv->dyn_lock);
1916 const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1917 struct hid_driver *hdrv)
1919 struct hid_dynid *dynid;
1921 spin_lock(&hdrv->dyn_lock);
1922 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1923 if (hid_match_one_id(hdev, &dynid->id)) {
1924 spin_unlock(&hdrv->dyn_lock);
1925 return &dynid->id;
1928 spin_unlock(&hdrv->dyn_lock);
1930 return hid_match_id(hdev, hdrv->id_table);
1932 EXPORT_SYMBOL_GPL(hid_match_device);
1934 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1936 struct hid_driver *hdrv = to_hid_driver(drv);
1937 struct hid_device *hdev = to_hid_device(dev);
1939 return hid_match_device(hdev, hdrv) != NULL;
1942 static int hid_device_probe(struct device *dev)
1944 struct hid_driver *hdrv = to_hid_driver(dev->driver);
1945 struct hid_device *hdev = to_hid_device(dev);
1946 const struct hid_device_id *id;
1947 int ret = 0;
1949 if (down_interruptible(&hdev->driver_input_lock)) {
1950 ret = -EINTR;
1951 goto end;
1953 hdev->io_started = false;
1955 clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
1957 if (!hdev->driver) {
1958 id = hid_match_device(hdev, hdrv);
1959 if (id == NULL) {
1960 ret = -ENODEV;
1961 goto unlock;
1964 if (hdrv->match) {
1965 if (!hdrv->match(hdev, hid_ignore_special_drivers)) {
1966 ret = -ENODEV;
1967 goto unlock;
1969 } else {
1971 * hid-generic implements .match(), so if
1972 * hid_ignore_special_drivers is set, we can safely
1973 * return.
1975 if (hid_ignore_special_drivers) {
1976 ret = -ENODEV;
1977 goto unlock;
1981 /* reset the quirks that has been previously set */
1982 hdev->quirks = hid_lookup_quirk(hdev);
1983 hdev->driver = hdrv;
1984 if (hdrv->probe) {
1985 ret = hdrv->probe(hdev, id);
1986 } else { /* default probe */
1987 ret = hid_open_report(hdev);
1988 if (!ret)
1989 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1991 if (ret) {
1992 hid_close_report(hdev);
1993 hdev->driver = NULL;
1996 unlock:
1997 if (!hdev->io_started)
1998 up(&hdev->driver_input_lock);
1999 end:
2000 return ret;
2003 static int hid_device_remove(struct device *dev)
2005 struct hid_device *hdev = to_hid_device(dev);
2006 struct hid_driver *hdrv;
2007 int ret = 0;
2009 if (down_interruptible(&hdev->driver_input_lock)) {
2010 ret = -EINTR;
2011 goto end;
2013 hdev->io_started = false;
2015 hdrv = hdev->driver;
2016 if (hdrv) {
2017 if (hdrv->remove)
2018 hdrv->remove(hdev);
2019 else /* default remove */
2020 hid_hw_stop(hdev);
2021 hid_close_report(hdev);
2022 hdev->driver = NULL;
2025 if (!hdev->io_started)
2026 up(&hdev->driver_input_lock);
2027 end:
2028 return ret;
2031 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
2032 char *buf)
2034 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
2036 return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
2037 hdev->bus, hdev->group, hdev->vendor, hdev->product);
2039 static DEVICE_ATTR_RO(modalias);
2041 static struct attribute *hid_dev_attrs[] = {
2042 &dev_attr_modalias.attr,
2043 NULL,
2045 static struct bin_attribute *hid_dev_bin_attrs[] = {
2046 &dev_bin_attr_report_desc,
2047 NULL
2049 static const struct attribute_group hid_dev_group = {
2050 .attrs = hid_dev_attrs,
2051 .bin_attrs = hid_dev_bin_attrs,
2053 __ATTRIBUTE_GROUPS(hid_dev);
2055 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
2057 struct hid_device *hdev = to_hid_device(dev);
2059 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
2060 hdev->bus, hdev->vendor, hdev->product))
2061 return -ENOMEM;
2063 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
2064 return -ENOMEM;
2066 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
2067 return -ENOMEM;
2069 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
2070 return -ENOMEM;
2072 if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
2073 hdev->bus, hdev->group, hdev->vendor, hdev->product))
2074 return -ENOMEM;
2076 return 0;
2079 struct bus_type hid_bus_type = {
2080 .name = "hid",
2081 .dev_groups = hid_dev_groups,
2082 .drv_groups = hid_drv_groups,
2083 .match = hid_bus_match,
2084 .probe = hid_device_probe,
2085 .remove = hid_device_remove,
2086 .uevent = hid_uevent,
2088 EXPORT_SYMBOL(hid_bus_type);
2090 int hid_add_device(struct hid_device *hdev)
2092 static atomic_t id = ATOMIC_INIT(0);
2093 int ret;
2095 if (WARN_ON(hdev->status & HID_STAT_ADDED))
2096 return -EBUSY;
2098 hdev->quirks = hid_lookup_quirk(hdev);
2100 /* we need to kill them here, otherwise they will stay allocated to
2101 * wait for coming driver */
2102 if (hid_ignore(hdev))
2103 return -ENODEV;
2106 * Check for the mandatory transport channel.
2108 if (!hdev->ll_driver->raw_request) {
2109 hid_err(hdev, "transport driver missing .raw_request()\n");
2110 return -EINVAL;
2114 * Read the device report descriptor once and use as template
2115 * for the driver-specific modifications.
2117 ret = hdev->ll_driver->parse(hdev);
2118 if (ret)
2119 return ret;
2120 if (!hdev->dev_rdesc)
2121 return -ENODEV;
2124 * Scan generic devices for group information
2126 if (hid_ignore_special_drivers) {
2127 hdev->group = HID_GROUP_GENERIC;
2128 } else if (!hdev->group &&
2129 !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
2130 ret = hid_scan_report(hdev);
2131 if (ret)
2132 hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2135 /* XXX hack, any other cleaner solution after the driver core
2136 * is converted to allow more than 20 bytes as the device name? */
2137 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
2138 hdev->vendor, hdev->product, atomic_inc_return(&id));
2140 hid_debug_register(hdev, dev_name(&hdev->dev));
2141 ret = device_add(&hdev->dev);
2142 if (!ret)
2143 hdev->status |= HID_STAT_ADDED;
2144 else
2145 hid_debug_unregister(hdev);
2147 return ret;
2149 EXPORT_SYMBOL_GPL(hid_add_device);
2152 * hid_allocate_device - allocate new hid device descriptor
2154 * Allocate and initialize hid device, so that hid_destroy_device might be
2155 * used to free it.
2157 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
2158 * error value.
2160 struct hid_device *hid_allocate_device(void)
2162 struct hid_device *hdev;
2163 int ret = -ENOMEM;
2165 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
2166 if (hdev == NULL)
2167 return ERR_PTR(ret);
2169 device_initialize(&hdev->dev);
2170 hdev->dev.release = hid_device_release;
2171 hdev->dev.bus = &hid_bus_type;
2172 device_enable_async_suspend(&hdev->dev);
2174 hid_close_report(hdev);
2176 init_waitqueue_head(&hdev->debug_wait);
2177 INIT_LIST_HEAD(&hdev->debug_list);
2178 spin_lock_init(&hdev->debug_list_lock);
2179 sema_init(&hdev->driver_input_lock, 1);
2180 mutex_init(&hdev->ll_open_lock);
2182 return hdev;
2184 EXPORT_SYMBOL_GPL(hid_allocate_device);
2186 static void hid_remove_device(struct hid_device *hdev)
2188 if (hdev->status & HID_STAT_ADDED) {
2189 device_del(&hdev->dev);
2190 hid_debug_unregister(hdev);
2191 hdev->status &= ~HID_STAT_ADDED;
2193 kfree(hdev->dev_rdesc);
2194 hdev->dev_rdesc = NULL;
2195 hdev->dev_rsize = 0;
2199 * hid_destroy_device - free previously allocated device
2201 * @hdev: hid device
2203 * If you allocate hid_device through hid_allocate_device, you should ever
2204 * free by this function.
2206 void hid_destroy_device(struct hid_device *hdev)
2208 hid_remove_device(hdev);
2209 put_device(&hdev->dev);
2211 EXPORT_SYMBOL_GPL(hid_destroy_device);
2214 static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
2216 struct hid_driver *hdrv = data;
2217 struct hid_device *hdev = to_hid_device(dev);
2219 if (hdev->driver == hdrv &&
2220 !hdrv->match(hdev, hid_ignore_special_drivers) &&
2221 !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
2222 return device_reprobe(dev);
2224 return 0;
2227 static int __hid_bus_driver_added(struct device_driver *drv, void *data)
2229 struct hid_driver *hdrv = to_hid_driver(drv);
2231 if (hdrv->match) {
2232 bus_for_each_dev(&hid_bus_type, NULL, hdrv,
2233 __hid_bus_reprobe_drivers);
2236 return 0;
2239 static int __bus_removed_driver(struct device_driver *drv, void *data)
2241 return bus_rescan_devices(&hid_bus_type);
2244 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
2245 const char *mod_name)
2247 int ret;
2249 hdrv->driver.name = hdrv->name;
2250 hdrv->driver.bus = &hid_bus_type;
2251 hdrv->driver.owner = owner;
2252 hdrv->driver.mod_name = mod_name;
2254 INIT_LIST_HEAD(&hdrv->dyn_list);
2255 spin_lock_init(&hdrv->dyn_lock);
2257 ret = driver_register(&hdrv->driver);
2259 if (ret == 0)
2260 bus_for_each_drv(&hid_bus_type, NULL, NULL,
2261 __hid_bus_driver_added);
2263 return ret;
2265 EXPORT_SYMBOL_GPL(__hid_register_driver);
2267 void hid_unregister_driver(struct hid_driver *hdrv)
2269 driver_unregister(&hdrv->driver);
2270 hid_free_dynids(hdrv);
2272 bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
2274 EXPORT_SYMBOL_GPL(hid_unregister_driver);
2276 int hid_check_keys_pressed(struct hid_device *hid)
2278 struct hid_input *hidinput;
2279 int i;
2281 if (!(hid->claimed & HID_CLAIMED_INPUT))
2282 return 0;
2284 list_for_each_entry(hidinput, &hid->inputs, list) {
2285 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
2286 if (hidinput->input->key[i])
2287 return 1;
2290 return 0;
2293 EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
2295 static int __init hid_init(void)
2297 int ret;
2299 if (hid_debug)
2300 pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
2301 "debugfs is now used for inspecting the device (report descriptor, reports)\n");
2303 ret = bus_register(&hid_bus_type);
2304 if (ret) {
2305 pr_err("can't register hid bus\n");
2306 goto err;
2309 ret = hidraw_init();
2310 if (ret)
2311 goto err_bus;
2313 hid_debug_init();
2315 return 0;
2316 err_bus:
2317 bus_unregister(&hid_bus_type);
2318 err:
2319 return ret;
2322 static void __exit hid_exit(void)
2324 hid_debug_exit();
2325 hidraw_exit();
2326 bus_unregister(&hid_bus_type);
2327 hid_quirks_exit(HID_BUS_ANY);
2330 module_init(hid_init);
2331 module_exit(hid_exit);
2333 MODULE_AUTHOR("Andreas Gal");
2334 MODULE_AUTHOR("Vojtech Pavlik");
2335 MODULE_AUTHOR("Jiri Kosina");
2336 MODULE_LICENSE("GPL");