2 * HID support for Linux
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
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)
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>
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>
30 #include <linux/hid.h>
31 #include <linux/hiddev.h>
32 #include <linux/hid-debug.h>
33 #include <linux/hidraw.h>
39 #define DRIVER_VERSION "v2.6"
40 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
41 #define DRIVER_DESC "HID core driver"
42 #define DRIVER_LICENSE "GPL"
44 #ifdef CONFIG_HID_DEBUG
46 module_param_named(debug
, hid_debug
, bool, 0600);
47 MODULE_PARM_DESC(debug
, "Turn HID debugging mode on and off");
48 EXPORT_SYMBOL_GPL(hid_debug
);
52 * Register a new report for a device.
55 static struct hid_report
*hid_register_report(struct hid_device
*device
, unsigned type
, unsigned id
)
57 struct hid_report_enum
*report_enum
= device
->report_enum
+ type
;
58 struct hid_report
*report
;
60 if (report_enum
->report_id_hash
[id
])
61 return report_enum
->report_id_hash
[id
];
63 if (!(report
= kzalloc(sizeof(struct hid_report
), GFP_KERNEL
)))
67 report_enum
->numbered
= 1;
72 report
->device
= device
;
73 report_enum
->report_id_hash
[id
] = report
;
75 list_add_tail(&report
->list
, &report_enum
->report_list
);
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");
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
= (unsigned *)(field
->usage
+ usages
);
100 field
->report
= report
;
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
;
114 usage
= parser
->local
.usage
[0];
116 if (parser
->collection_stack_ptr
== HID_COLLECTION_STACK_SIZE
) {
117 dbg_hid("collection stack overflow\n");
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");
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
++;
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");
164 parser
->collection_stack_ptr
--;
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
)
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");
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
++;
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
;
212 if (!(report
= hid_register_report(parser
->device
, report_type
, parser
->global
.report_id
))) {
213 dbg_hid("hid_register_report failed\n");
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
);
222 offset
= report
->size
;
223 report
->size
+= parser
->global
.report_size
* parser
->global
.report_count
;
225 if (!parser
->local
.usage_index
) /* Ignore padding fields */
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
)
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
++) {
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
;
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
;
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
;
288 * Process a global item.
291 static int hid_parser_global(struct hid_parser
*parser
, struct hid_item
*item
)
295 case HID_GLOBAL_ITEM_TAG_PUSH
:
297 if (parser
->global_stack_ptr
== HID_GLOBAL_STACK_SIZE
) {
298 dbg_hid("global enviroment stack overflow\n");
302 memcpy(parser
->global_stack
+ parser
->global_stack_ptr
++,
303 &parser
->global
, sizeof(struct hid_global
));
306 case HID_GLOBAL_ITEM_TAG_POP
:
308 if (!parser
->global_stack_ptr
) {
309 dbg_hid("global enviroment stack underflow\n");
313 memcpy(&parser
->global
, parser
->global_stack
+ --parser
->global_stack_ptr
,
314 sizeof(struct hid_global
));
317 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE
:
318 parser
->global
.usage_page
= item_udata(item
);
321 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM
:
322 parser
->global
.logical_minimum
= item_sdata(item
);
325 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM
:
326 if (parser
->global
.logical_minimum
< 0)
327 parser
->global
.logical_maximum
= item_sdata(item
);
329 parser
->global
.logical_maximum
= item_udata(item
);
332 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM
:
333 parser
->global
.physical_minimum
= item_sdata(item
);
336 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM
:
337 if (parser
->global
.physical_minimum
< 0)
338 parser
->global
.physical_maximum
= item_sdata(item
);
340 parser
->global
.physical_maximum
= item_udata(item
);
343 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT
:
344 parser
->global
.unit_exponent
= item_sdata(item
);
347 case HID_GLOBAL_ITEM_TAG_UNIT
:
348 parser
->global
.unit
= item_udata(item
);
351 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE
:
352 if ((parser
->global
.report_size
= item_udata(item
)) > 32) {
353 dbg_hid("invalid report_size %d\n", parser
->global
.report_size
);
358 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT
:
359 if ((parser
->global
.report_count
= item_udata(item
)) > HID_MAX_USAGES
) {
360 dbg_hid("invalid report_count %d\n", parser
->global
.report_count
);
365 case HID_GLOBAL_ITEM_TAG_REPORT_ID
:
366 if ((parser
->global
.report_id
= item_udata(item
)) == 0) {
367 dbg_hid("report_id 0 is invalid\n");
373 dbg_hid("unknown global tag 0x%x\n", item
->tag
);
379 * Process a local item.
382 static int hid_parser_local(struct hid_parser
*parser
, struct hid_item
*item
)
387 if (item
->size
== 0) {
388 dbg_hid("item data expected for local item\n");
392 data
= item_udata(item
);
396 case HID_LOCAL_ITEM_TAG_DELIMITER
:
400 * We treat items before the first delimiter
401 * as global to all usage sets (branch 0).
402 * In the moment we process only these global
403 * items and the first delimiter set.
405 if (parser
->local
.delimiter_depth
!= 0) {
406 dbg_hid("nested delimiters\n");
409 parser
->local
.delimiter_depth
++;
410 parser
->local
.delimiter_branch
++;
412 if (parser
->local
.delimiter_depth
< 1) {
413 dbg_hid("bogus close delimiter\n");
416 parser
->local
.delimiter_depth
--;
420 case HID_LOCAL_ITEM_TAG_USAGE
:
422 if (parser
->local
.delimiter_branch
> 1) {
423 dbg_hid("alternative usage ignored\n");
428 data
= (parser
->global
.usage_page
<< 16) + data
;
430 return hid_add_usage(parser
, data
);
432 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM
:
434 if (parser
->local
.delimiter_branch
> 1) {
435 dbg_hid("alternative usage ignored\n");
440 data
= (parser
->global
.usage_page
<< 16) + data
;
442 parser
->local
.usage_minimum
= data
;
445 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM
:
447 if (parser
->local
.delimiter_branch
> 1) {
448 dbg_hid("alternative usage ignored\n");
453 data
= (parser
->global
.usage_page
<< 16) + data
;
455 for (n
= parser
->local
.usage_minimum
; n
<= data
; n
++)
456 if (hid_add_usage(parser
, n
)) {
457 dbg_hid("hid_add_usage failed\n");
464 dbg_hid("unknown local item tag 0x%x\n", item
->tag
);
471 * Process a main item.
474 static int hid_parser_main(struct hid_parser
*parser
, struct hid_item
*item
)
479 data
= item_udata(item
);
482 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION
:
483 ret
= open_collection(parser
, data
& 0xff);
485 case HID_MAIN_ITEM_TAG_END_COLLECTION
:
486 ret
= close_collection(parser
);
488 case HID_MAIN_ITEM_TAG_INPUT
:
489 ret
= hid_add_field(parser
, HID_INPUT_REPORT
, data
);
491 case HID_MAIN_ITEM_TAG_OUTPUT
:
492 ret
= hid_add_field(parser
, HID_OUTPUT_REPORT
, data
);
494 case HID_MAIN_ITEM_TAG_FEATURE
:
495 ret
= hid_add_field(parser
, HID_FEATURE_REPORT
, data
);
498 dbg_hid("unknown main item tag 0x%x\n", item
->tag
);
502 memset(&parser
->local
, 0, sizeof(parser
->local
)); /* Reset the local parser environment */
508 * Process a reserved item.
511 static int hid_parser_reserved(struct hid_parser
*parser
, struct hid_item
*item
)
513 dbg_hid("reserved item type, tag 0x%x\n", item
->tag
);
518 * Free a report and all registered fields. The field->usage and
519 * field->value table's are allocated behind the field, so we need
520 * only to free(field) itself.
523 static void hid_free_report(struct hid_report
*report
)
527 for (n
= 0; n
< report
->maxfield
; n
++)
528 kfree(report
->field
[n
]);
533 * Free a device structure, all reports, and all fields.
536 void hid_free_device(struct hid_device
*device
)
540 for (i
= 0; i
< HID_REPORT_TYPES
; i
++) {
541 struct hid_report_enum
*report_enum
= device
->report_enum
+ i
;
543 for (j
= 0; j
< 256; j
++) {
544 struct hid_report
*report
= report_enum
->report_id_hash
[j
];
546 hid_free_report(report
);
550 kfree(device
->rdesc
);
551 kfree(device
->collection
);
554 EXPORT_SYMBOL_GPL(hid_free_device
);
557 * Fetch a report description item from the data stream. We support long
558 * items, though they are not used yet.
561 static u8
*fetch_item(__u8
*start
, __u8
*end
, struct hid_item
*item
)
565 if ((end
- start
) <= 0)
570 item
->type
= (b
>> 2) & 3;
571 item
->tag
= (b
>> 4) & 15;
573 if (item
->tag
== HID_ITEM_TAG_LONG
) {
575 item
->format
= HID_ITEM_FORMAT_LONG
;
577 if ((end
- start
) < 2)
580 item
->size
= *start
++;
581 item
->tag
= *start
++;
583 if ((end
- start
) < item
->size
)
586 item
->data
.longdata
= start
;
591 item
->format
= HID_ITEM_FORMAT_SHORT
;
594 switch (item
->size
) {
600 if ((end
- start
) < 1)
602 item
->data
.u8
= *start
++;
606 if ((end
- start
) < 2)
608 item
->data
.u16
= le16_to_cpu(get_unaligned((__le16
*)start
));
609 start
= (__u8
*)((__le16
*)start
+ 1);
614 if ((end
- start
) < 4)
616 item
->data
.u32
= le32_to_cpu(get_unaligned((__le32
*)start
));
617 start
= (__u8
*)((__le32
*)start
+ 1);
625 * Parse a report description into a hid_device structure. Reports are
626 * enumerated, fields are attached to these reports.
629 struct hid_device
*hid_parse_report(__u8
*start
, unsigned size
)
631 struct hid_device
*device
;
632 struct hid_parser
*parser
;
633 struct hid_item item
;
636 static int (*dispatch_type
[])(struct hid_parser
*parser
,
637 struct hid_item
*item
) = {
644 if (!(device
= kzalloc(sizeof(struct hid_device
), GFP_KERNEL
)))
647 if (!(device
->collection
= kzalloc(sizeof(struct hid_collection
) *
648 HID_DEFAULT_NUM_COLLECTIONS
, GFP_KERNEL
))) {
652 device
->collection_size
= HID_DEFAULT_NUM_COLLECTIONS
;
654 for (i
= 0; i
< HID_REPORT_TYPES
; i
++)
655 INIT_LIST_HEAD(&device
->report_enum
[i
].report_list
);
657 if (!(device
->rdesc
= kmalloc(size
, GFP_KERNEL
))) {
658 kfree(device
->collection
);
662 memcpy(device
->rdesc
, start
, size
);
663 device
->rsize
= size
;
665 if (!(parser
= vmalloc(sizeof(struct hid_parser
)))) {
666 kfree(device
->rdesc
);
667 kfree(device
->collection
);
671 memset(parser
, 0, sizeof(struct hid_parser
));
672 parser
->device
= device
;
675 while ((start
= fetch_item(start
, end
, &item
)) != NULL
) {
677 if (item
.format
!= HID_ITEM_FORMAT_SHORT
) {
678 dbg_hid("unexpected long global item\n");
679 hid_free_device(device
);
684 if (dispatch_type
[item
.type
](parser
, &item
)) {
685 dbg_hid("item %u %u %u %u parsing failed\n",
686 item
.format
, (unsigned)item
.size
, (unsigned)item
.type
, (unsigned)item
.tag
);
687 hid_free_device(device
);
693 if (parser
->collection_stack_ptr
) {
694 dbg_hid("unbalanced collection at end of report description\n");
695 hid_free_device(device
);
699 if (parser
->local
.delimiter_depth
) {
700 dbg_hid("unbalanced delimiter at end of report description\n");
701 hid_free_device(device
);
710 dbg_hid("item fetching failed at offset %d\n", (int)(end
- start
));
711 hid_free_device(device
);
715 EXPORT_SYMBOL_GPL(hid_parse_report
);
718 * Convert a signed n-bit integer to signed 32-bit integer. Common
719 * cases are done through the compiler, the screwed things has to be
723 static s32
snto32(__u32 value
, unsigned n
)
726 case 8: return ((__s8
)value
);
727 case 16: return ((__s16
)value
);
728 case 32: return ((__s32
)value
);
730 return value
& (1 << (n
- 1)) ? value
| (-1 << n
) : value
;
734 * Convert a signed 32-bit integer to a signed n-bit integer.
737 static u32
s32ton(__s32 value
, unsigned n
)
739 s32 a
= value
>> (n
- 1);
741 return value
< 0 ? 1 << (n
- 1) : (1 << (n
- 1)) - 1;
742 return value
& ((1 << n
) - 1);
746 * Extract/implement a data field from/to a little endian report (bit array).
748 * Code sort-of follows HID spec:
749 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
751 * While the USB HID spec allows unlimited length bit fields in "report
752 * descriptors", most devices never use more than 16 bits.
753 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
754 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
757 static __inline__ __u32
extract(__u8
*report
, unsigned offset
, unsigned n
)
763 report
+= offset
>> 3; /* adjust byte index */
764 offset
&= 7; /* now only need bit offset into one byte */
765 x
= le64_to_cpu(get_unaligned((__le64
*) report
));
766 x
= (x
>> offset
) & ((1ULL << n
) - 1); /* extract bit field */
771 * "implement" : set bits in a little endian bit stream.
772 * Same concepts as "extract" (see comments above).
773 * The data mangled in the bit stream remains in little endian
774 * order the whole time. It make more sense to talk about
775 * endianness of register values by considering a register
776 * a "cached" copy of the little endiad bit stream.
778 static __inline__
void implement(__u8
*report
, unsigned offset
, unsigned n
, __u32 value
)
781 u64 m
= (1ULL << n
) - 1;
788 report
+= offset
>> 3;
791 x
= get_unaligned((__le64
*)report
);
792 x
&= cpu_to_le64(~(m
<< offset
));
793 x
|= cpu_to_le64(((u64
) value
) << offset
);
794 put_unaligned(x
, (__le64
*) report
);
798 * Search an array for a value.
801 static __inline__
int search(__s32
*array
, __s32 value
, unsigned n
)
804 if (*array
++ == value
)
810 static void hid_process_event(struct hid_device
*hid
, struct hid_field
*field
, struct hid_usage
*usage
, __s32 value
, int interrupt
)
812 hid_dump_input(usage
, value
);
813 if (hid
->claimed
& HID_CLAIMED_INPUT
)
814 hidinput_hid_event(hid
, field
, usage
, value
);
815 if (hid
->claimed
& HID_CLAIMED_HIDDEV
&& interrupt
&& hid
->hiddev_hid_event
)
816 hid
->hiddev_hid_event(hid
, field
, usage
, value
);
820 * Analyse a received field, and fetch the data from it. The field
821 * content is stored for next report processing (we do differential
822 * reporting to the layer).
825 void hid_input_field(struct hid_device
*hid
, struct hid_field
*field
, __u8
*data
, int interrupt
)
828 unsigned count
= field
->report_count
;
829 unsigned offset
= field
->report_offset
;
830 unsigned size
= field
->report_size
;
831 __s32 min
= field
->logical_minimum
;
832 __s32 max
= field
->logical_maximum
;
835 if (!(value
= kmalloc(sizeof(__s32
) * count
, GFP_ATOMIC
)))
838 for (n
= 0; n
< count
; n
++) {
840 value
[n
] = min
< 0 ? snto32(extract(data
, offset
+ n
* size
, size
), size
) :
841 extract(data
, offset
+ n
* size
, size
);
843 if (!(field
->flags
& HID_MAIN_ITEM_VARIABLE
) /* Ignore report if ErrorRollOver */
844 && value
[n
] >= min
&& value
[n
] <= max
845 && field
->usage
[value
[n
] - min
].hid
== HID_UP_KEYBOARD
+ 1)
849 for (n
= 0; n
< count
; n
++) {
851 if (HID_MAIN_ITEM_VARIABLE
& field
->flags
) {
852 hid_process_event(hid
, field
, &field
->usage
[n
], value
[n
], interrupt
);
856 if (field
->value
[n
] >= min
&& field
->value
[n
] <= max
857 && field
->usage
[field
->value
[n
] - min
].hid
858 && search(value
, field
->value
[n
], count
))
859 hid_process_event(hid
, field
, &field
->usage
[field
->value
[n
] - min
], 0, interrupt
);
861 if (value
[n
] >= min
&& value
[n
] <= max
862 && field
->usage
[value
[n
] - min
].hid
863 && search(field
->value
, value
[n
], count
))
864 hid_process_event(hid
, field
, &field
->usage
[value
[n
] - min
], 1, interrupt
);
867 memcpy(field
->value
, value
, count
* sizeof(__s32
));
871 EXPORT_SYMBOL_GPL(hid_input_field
);
874 * Output the field into the report.
877 static void hid_output_field(struct hid_field
*field
, __u8
*data
)
879 unsigned count
= field
->report_count
;
880 unsigned offset
= field
->report_offset
;
881 unsigned size
= field
->report_size
;
882 unsigned bitsused
= offset
+ count
* size
;
885 /* make sure the unused bits in the last byte are zeros */
886 if (count
> 0 && size
> 0 && (bitsused
% 8) != 0)
887 data
[(bitsused
-1)/8] &= (1 << (bitsused
% 8)) - 1;
889 for (n
= 0; n
< count
; n
++) {
890 if (field
->logical_minimum
< 0) /* signed values */
891 implement(data
, offset
+ n
* size
, size
, s32ton(field
->value
[n
], size
));
892 else /* unsigned values */
893 implement(data
, offset
+ n
* size
, size
, field
->value
[n
]);
901 void hid_output_report(struct hid_report
*report
, __u8
*data
)
906 *data
++ = report
->id
;
908 for (n
= 0; n
< report
->maxfield
; n
++)
909 hid_output_field(report
->field
[n
], data
);
911 EXPORT_SYMBOL_GPL(hid_output_report
);
914 * Set a field value. The report this field belongs to has to be
915 * created and transferred to the device, to set this value in the
919 int hid_set_field(struct hid_field
*field
, unsigned offset
, __s32 value
)
921 unsigned size
= field
->report_size
;
923 hid_dump_input(field
->usage
+ offset
, value
);
925 if (offset
>= field
->report_count
) {
926 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset
, field
->report_count
);
927 hid_dump_field(field
, 8);
930 if (field
->logical_minimum
< 0) {
931 if (value
!= snto32(s32ton(value
, size
), size
)) {
932 dbg_hid("value %d is out of range\n", value
);
936 field
->value
[offset
] = value
;
939 EXPORT_SYMBOL_GPL(hid_set_field
);
941 int hid_input_report(struct hid_device
*hid
, int type
, u8
*data
, int size
, int interrupt
)
943 struct hid_report_enum
*report_enum
= hid
->report_enum
+ type
;
944 struct hid_report
*report
;
951 dbg_hid("empty report\n");
955 dbg_hid("report (size %u) (%snumbered)\n", size
, report_enum
->numbered
? "" : "un");
957 n
= 0; /* Normally report number is 0 */
958 if (report_enum
->numbered
) { /* Device uses numbered reports, data[0] is report number */
963 /* dump the report descriptor */
964 dbg_hid("report %d (size %u) = ", n
, size
);
965 for (i
= 0; i
< size
; i
++)
966 dbg_hid_line(" %02x", data
[i
]);
969 if (!(report
= report_enum
->report_id_hash
[n
])) {
970 dbg_hid("undefined report_id %d received\n", n
);
974 rsize
= ((report
->size
- 1) >> 3) + 1;
977 dbg_hid("report %d is too short, (%d < %d)\n", report
->id
, size
, rsize
);
978 memset(data
+ size
, 0, rsize
- size
);
981 if ((hid
->claimed
& HID_CLAIMED_HIDDEV
) && hid
->hiddev_report_event
)
982 hid
->hiddev_report_event(hid
, report
);
983 if (hid
->claimed
& HID_CLAIMED_HIDRAW
)
984 hidraw_report_event(hid
, data
, size
);
986 for (n
= 0; n
< report
->maxfield
; n
++)
987 hid_input_field(hid
, report
->field
[n
], data
, interrupt
);
989 if (hid
->claimed
& HID_CLAIMED_INPUT
)
990 hidinput_report_event(hid
, report
);
994 EXPORT_SYMBOL_GPL(hid_input_report
);
996 static int __init
hid_init(void)
998 return hidraw_init();
1001 static void __exit
hid_exit(void)
1006 module_init(hid_init
);
1007 module_exit(hid_exit
);
1009 MODULE_LICENSE(DRIVER_LICENSE
);