[PATCH] Generic HID layer - API
[linux-2.6/btrfs-unstable.git] / drivers / hid / hid-core.c
blob8474a7923322186e6da86ad5ef07456443f2c693
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 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/sched.h>
22 #include <linux/list.h>
23 #include <linux/mm.h>
24 #include <linux/smp_lock.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>
31 #undef DEBUG
32 #undef DEBUG_DATA
34 #include <linux/hid.h>
35 #include <linux/hiddev.h>
38 * Version Information
41 #define DRIVER_VERSION "v2.6"
42 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
43 #define DRIVER_DESC "USB HID core driver"
44 #define DRIVER_LICENSE "GPL"
47 * Module parameters.
50 static unsigned int hid_mousepoll_interval;
51 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
52 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55 * Register a new report for a device.
58 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
60 struct hid_report_enum *report_enum = device->report_enum + type;
61 struct hid_report *report;
63 if (report_enum->report_id_hash[id])
64 return report_enum->report_id_hash[id];
66 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
67 return NULL;
69 if (id != 0)
70 report_enum->numbered = 1;
72 report->id = id;
73 report->type = type;
74 report->size = 0;
75 report->device = device;
76 report_enum->report_id_hash[id] = report;
78 list_add_tail(&report->list, &report_enum->report_list);
80 return report;
84 * Register a new field for this report.
87 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
89 struct hid_field *field;
91 if (report->maxfield == HID_MAX_FIELDS) {
92 dbg("too many fields in report");
93 return NULL;
96 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
99 field->index = report->maxfield++;
100 report->field[field->index] = field;
101 field->usage = (struct hid_usage *)(field + 1);
102 field->value = (unsigned *)(field->usage + usages);
103 field->report = report;
105 return field;
109 * Open a collection. The type/usage is pushed on the stack.
112 static int open_collection(struct hid_parser *parser, unsigned type)
114 struct hid_collection *collection;
115 unsigned usage;
117 usage = parser->local.usage[0];
119 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
120 dbg("collection stack overflow");
121 return -1;
124 if (parser->device->maxcollection == parser->device->collection_size) {
125 collection = kmalloc(sizeof(struct hid_collection) *
126 parser->device->collection_size * 2, GFP_KERNEL);
127 if (collection == NULL) {
128 dbg("failed to reallocate collection array");
129 return -1;
131 memcpy(collection, parser->device->collection,
132 sizeof(struct hid_collection) *
133 parser->device->collection_size);
134 memset(collection + parser->device->collection_size, 0,
135 sizeof(struct hid_collection) *
136 parser->device->collection_size);
137 kfree(parser->device->collection);
138 parser->device->collection = collection;
139 parser->device->collection_size *= 2;
142 parser->collection_stack[parser->collection_stack_ptr++] =
143 parser->device->maxcollection;
145 collection = parser->device->collection +
146 parser->device->maxcollection++;
147 collection->type = type;
148 collection->usage = usage;
149 collection->level = parser->collection_stack_ptr - 1;
151 if (type == HID_COLLECTION_APPLICATION)
152 parser->device->maxapplication++;
154 return 0;
158 * Close a collection.
161 static int close_collection(struct hid_parser *parser)
163 if (!parser->collection_stack_ptr) {
164 dbg("collection stack underflow");
165 return -1;
167 parser->collection_stack_ptr--;
168 return 0;
172 * Climb up the stack, search for the specified collection type
173 * and return the usage.
176 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
178 int n;
179 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
180 if (parser->device->collection[parser->collection_stack[n]].type == type)
181 return parser->device->collection[parser->collection_stack[n]].usage;
182 return 0; /* we know nothing about this usage type */
186 * Add a usage to the temporary parser table.
189 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
191 if (parser->local.usage_index >= HID_MAX_USAGES) {
192 dbg("usage index exceeded");
193 return -1;
195 parser->local.usage[parser->local.usage_index] = usage;
196 parser->local.collection_index[parser->local.usage_index] =
197 parser->collection_stack_ptr ?
198 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
199 parser->local.usage_index++;
200 return 0;
204 * Register a new field for this report.
207 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
209 struct hid_report *report;
210 struct hid_field *field;
211 int usages;
212 unsigned offset;
213 int i;
215 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
216 dbg("hid_register_report failed");
217 return -1;
220 if (parser->global.logical_maximum < parser->global.logical_minimum) {
221 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
222 return -1;
225 offset = report->size;
226 report->size += parser->global.report_size * parser->global.report_count;
228 if (!parser->local.usage_index) /* Ignore padding fields */
229 return 0;
231 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
233 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
234 return 0;
236 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
237 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
238 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
240 for (i = 0; i < usages; i++) {
241 int j = i;
242 /* Duplicate the last usage we parsed if we have excess values */
243 if (i >= parser->local.usage_index)
244 j = parser->local.usage_index - 1;
245 field->usage[i].hid = parser->local.usage[j];
246 field->usage[i].collection_index =
247 parser->local.collection_index[j];
250 field->maxusage = usages;
251 field->flags = flags;
252 field->report_offset = offset;
253 field->report_type = report_type;
254 field->report_size = parser->global.report_size;
255 field->report_count = parser->global.report_count;
256 field->logical_minimum = parser->global.logical_minimum;
257 field->logical_maximum = parser->global.logical_maximum;
258 field->physical_minimum = parser->global.physical_minimum;
259 field->physical_maximum = parser->global.physical_maximum;
260 field->unit_exponent = parser->global.unit_exponent;
261 field->unit = parser->global.unit;
263 return 0;
267 * Read data value from item.
270 static u32 item_udata(struct hid_item *item)
272 switch (item->size) {
273 case 1: return item->data.u8;
274 case 2: return item->data.u16;
275 case 4: return item->data.u32;
277 return 0;
280 static s32 item_sdata(struct hid_item *item)
282 switch (item->size) {
283 case 1: return item->data.s8;
284 case 2: return item->data.s16;
285 case 4: return item->data.s32;
287 return 0;
291 * Process a global item.
294 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
296 switch (item->tag) {
298 case HID_GLOBAL_ITEM_TAG_PUSH:
300 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
301 dbg("global enviroment stack overflow");
302 return -1;
305 memcpy(parser->global_stack + parser->global_stack_ptr++,
306 &parser->global, sizeof(struct hid_global));
307 return 0;
309 case HID_GLOBAL_ITEM_TAG_POP:
311 if (!parser->global_stack_ptr) {
312 dbg("global enviroment stack underflow");
313 return -1;
316 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
317 sizeof(struct hid_global));
318 return 0;
320 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
321 parser->global.usage_page = item_udata(item);
322 return 0;
324 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
325 parser->global.logical_minimum = item_sdata(item);
326 return 0;
328 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
329 if (parser->global.logical_minimum < 0)
330 parser->global.logical_maximum = item_sdata(item);
331 else
332 parser->global.logical_maximum = item_udata(item);
333 return 0;
335 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
336 parser->global.physical_minimum = item_sdata(item);
337 return 0;
339 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
340 if (parser->global.physical_minimum < 0)
341 parser->global.physical_maximum = item_sdata(item);
342 else
343 parser->global.physical_maximum = item_udata(item);
344 return 0;
346 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
347 parser->global.unit_exponent = item_sdata(item);
348 return 0;
350 case HID_GLOBAL_ITEM_TAG_UNIT:
351 parser->global.unit = item_udata(item);
352 return 0;
354 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
355 if ((parser->global.report_size = item_udata(item)) > 32) {
356 dbg("invalid report_size %d", parser->global.report_size);
357 return -1;
359 return 0;
361 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
362 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
363 dbg("invalid report_count %d", parser->global.report_count);
364 return -1;
366 return 0;
368 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
369 if ((parser->global.report_id = item_udata(item)) == 0) {
370 dbg("report_id 0 is invalid");
371 return -1;
373 return 0;
375 default:
376 dbg("unknown global tag 0x%x", item->tag);
377 return -1;
382 * Process a local item.
385 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
387 __u32 data;
388 unsigned n;
390 if (item->size == 0) {
391 dbg("item data expected for local item");
392 return -1;
395 data = item_udata(item);
397 switch (item->tag) {
399 case HID_LOCAL_ITEM_TAG_DELIMITER:
401 if (data) {
403 * We treat items before the first delimiter
404 * as global to all usage sets (branch 0).
405 * In the moment we process only these global
406 * items and the first delimiter set.
408 if (parser->local.delimiter_depth != 0) {
409 dbg("nested delimiters");
410 return -1;
412 parser->local.delimiter_depth++;
413 parser->local.delimiter_branch++;
414 } else {
415 if (parser->local.delimiter_depth < 1) {
416 dbg("bogus close delimiter");
417 return -1;
419 parser->local.delimiter_depth--;
421 return 1;
423 case HID_LOCAL_ITEM_TAG_USAGE:
425 if (parser->local.delimiter_branch > 1) {
426 dbg("alternative usage ignored");
427 return 0;
430 if (item->size <= 2)
431 data = (parser->global.usage_page << 16) + data;
433 return hid_add_usage(parser, data);
435 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
437 if (parser->local.delimiter_branch > 1) {
438 dbg("alternative usage ignored");
439 return 0;
442 if (item->size <= 2)
443 data = (parser->global.usage_page << 16) + data;
445 parser->local.usage_minimum = data;
446 return 0;
448 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
450 if (parser->local.delimiter_branch > 1) {
451 dbg("alternative usage ignored");
452 return 0;
455 if (item->size <= 2)
456 data = (parser->global.usage_page << 16) + data;
458 for (n = parser->local.usage_minimum; n <= data; n++)
459 if (hid_add_usage(parser, n)) {
460 dbg("hid_add_usage failed\n");
461 return -1;
463 return 0;
465 default:
467 dbg("unknown local item tag 0x%x", item->tag);
468 return 0;
470 return 0;
474 * Process a main item.
477 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
479 __u32 data;
480 int ret;
482 data = item_udata(item);
484 switch (item->tag) {
485 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
486 ret = open_collection(parser, data & 0xff);
487 break;
488 case HID_MAIN_ITEM_TAG_END_COLLECTION:
489 ret = close_collection(parser);
490 break;
491 case HID_MAIN_ITEM_TAG_INPUT:
492 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
493 break;
494 case HID_MAIN_ITEM_TAG_OUTPUT:
495 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
496 break;
497 case HID_MAIN_ITEM_TAG_FEATURE:
498 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
499 break;
500 default:
501 dbg("unknown main item tag 0x%x", item->tag);
502 ret = 0;
505 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
507 return ret;
511 * Process a reserved item.
514 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
516 dbg("reserved item type, tag 0x%x", item->tag);
517 return 0;
521 * Free a report and all registered fields. The field->usage and
522 * field->value table's are allocated behind the field, so we need
523 * only to free(field) itself.
526 static void hid_free_report(struct hid_report *report)
528 unsigned n;
530 for (n = 0; n < report->maxfield; n++)
531 kfree(report->field[n]);
532 kfree(report);
536 * Free a device structure, all reports, and all fields.
539 void hid_free_device(struct hid_device *device)
541 unsigned i,j;
543 for (i = 0; i < HID_REPORT_TYPES; i++) {
544 struct hid_report_enum *report_enum = device->report_enum + i;
546 for (j = 0; j < 256; j++) {
547 struct hid_report *report = report_enum->report_id_hash[j];
548 if (report)
549 hid_free_report(report);
553 kfree(device->rdesc);
554 kfree(device);
556 EXPORT_SYMBOL_GPL(hid_free_device);
559 * Fetch a report description item from the data stream. We support long
560 * items, though they are not used yet.
563 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
565 u8 b;
567 if ((end - start) <= 0)
568 return NULL;
570 b = *start++;
572 item->type = (b >> 2) & 3;
573 item->tag = (b >> 4) & 15;
575 if (item->tag == HID_ITEM_TAG_LONG) {
577 item->format = HID_ITEM_FORMAT_LONG;
579 if ((end - start) < 2)
580 return NULL;
582 item->size = *start++;
583 item->tag = *start++;
585 if ((end - start) < item->size)
586 return NULL;
588 item->data.longdata = start;
589 start += item->size;
590 return start;
593 item->format = HID_ITEM_FORMAT_SHORT;
594 item->size = b & 3;
596 switch (item->size) {
598 case 0:
599 return start;
601 case 1:
602 if ((end - start) < 1)
603 return NULL;
604 item->data.u8 = *start++;
605 return start;
607 case 2:
608 if ((end - start) < 2)
609 return NULL;
610 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
611 start = (__u8 *)((__le16 *)start + 1);
612 return start;
614 case 3:
615 item->size++;
616 if ((end - start) < 4)
617 return NULL;
618 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
619 start = (__u8 *)((__le32 *)start + 1);
620 return start;
623 return NULL;
627 * Parse a report description into a hid_device structure. Reports are
628 * enumerated, fields are attached to these reports.
631 struct hid_device *hid_parse_report(__u8 *start, unsigned size)
633 struct hid_device *device;
634 struct hid_parser *parser;
635 struct hid_item item;
636 __u8 *end;
637 unsigned i;
638 static int (*dispatch_type[])(struct hid_parser *parser,
639 struct hid_item *item) = {
640 hid_parser_main,
641 hid_parser_global,
642 hid_parser_local,
643 hid_parser_reserved
646 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
647 return NULL;
649 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
650 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
651 kfree(device);
652 return NULL;
654 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
656 for (i = 0; i < HID_REPORT_TYPES; i++)
657 INIT_LIST_HEAD(&device->report_enum[i].report_list);
659 if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
660 kfree(device->collection);
661 kfree(device);
662 return NULL;
664 memcpy(device->rdesc, start, size);
665 device->rsize = size;
667 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
668 kfree(device->rdesc);
669 kfree(device->collection);
670 kfree(device);
671 return NULL;
673 parser->device = device;
675 end = start + size;
676 while ((start = fetch_item(start, end, &item)) != NULL) {
678 if (item.format != HID_ITEM_FORMAT_SHORT) {
679 dbg("unexpected long global item");
680 kfree(device->collection);
681 hid_free_device(device);
682 kfree(parser);
683 return NULL;
686 if (dispatch_type[item.type](parser, &item)) {
687 dbg("item %u %u %u %u parsing failed\n",
688 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
689 kfree(device->collection);
690 hid_free_device(device);
691 kfree(parser);
692 return NULL;
695 if (start == end) {
696 if (parser->collection_stack_ptr) {
697 dbg("unbalanced collection at end of report description");
698 kfree(device->collection);
699 hid_free_device(device);
700 kfree(parser);
701 return NULL;
703 if (parser->local.delimiter_depth) {
704 dbg("unbalanced delimiter at end of report description");
705 kfree(device->collection);
706 hid_free_device(device);
707 kfree(parser);
708 return NULL;
710 kfree(parser);
711 return device;
715 dbg("item fetching failed at offset %d\n", (int)(end - start));
716 kfree(device->collection);
717 hid_free_device(device);
718 kfree(parser);
719 return NULL;
721 EXPORT_SYMBOL_GPL(hid_parse_report);
724 * Convert a signed n-bit integer to signed 32-bit integer. Common
725 * cases are done through the compiler, the screwed things has to be
726 * done by hand.
729 static s32 snto32(__u32 value, unsigned n)
731 switch (n) {
732 case 8: return ((__s8)value);
733 case 16: return ((__s16)value);
734 case 32: return ((__s32)value);
736 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
740 * Convert a signed 32-bit integer to a signed n-bit integer.
743 static u32 s32ton(__s32 value, unsigned n)
745 s32 a = value >> (n - 1);
746 if (a && a != -1)
747 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
748 return value & ((1 << n) - 1);
752 * Extract/implement a data field from/to a little endian report (bit array).
754 * Code sort-of follows HID spec:
755 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
757 * While the USB HID spec allows unlimited length bit fields in "report
758 * descriptors", most devices never use more than 16 bits.
759 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
760 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
763 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
765 u64 x;
767 WARN_ON(n > 32);
769 report += offset >> 3; /* adjust byte index */
770 offset &= 7; /* now only need bit offset into one byte */
771 x = get_unaligned((u64 *) report);
772 x = le64_to_cpu(x);
773 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
774 return (u32) x;
778 * "implement" : set bits in a little endian bit stream.
779 * Same concepts as "extract" (see comments above).
780 * The data mangled in the bit stream remains in little endian
781 * order the whole time. It make more sense to talk about
782 * endianness of register values by considering a register
783 * a "cached" copy of the little endiad bit stream.
785 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
787 u64 x;
788 u64 m = (1ULL << n) - 1;
790 WARN_ON(n > 32);
792 WARN_ON(value > m);
793 value &= m;
795 report += offset >> 3;
796 offset &= 7;
798 x = get_unaligned((u64 *)report);
799 x &= cpu_to_le64(~(m << offset));
800 x |= cpu_to_le64(((u64) value) << offset);
801 put_unaligned(x, (u64 *) report);
805 * Search an array for a value.
808 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
810 while (n--) {
811 if (*array++ == value)
812 return 0;
814 return -1;
817 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
819 hid_dump_input(usage, value);
820 if (hid->claimed & HID_CLAIMED_INPUT)
821 hidinput_hid_event(hid, field, usage, value);
822 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
823 hiddev_hid_event(hid, field, usage, value);
827 * Analyse a received field, and fetch the data from it. The field
828 * content is stored for next report processing (we do differential
829 * reporting to the layer).
832 void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
834 unsigned n;
835 unsigned count = field->report_count;
836 unsigned offset = field->report_offset;
837 unsigned size = field->report_size;
838 __s32 min = field->logical_minimum;
839 __s32 max = field->logical_maximum;
840 __s32 *value;
842 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
843 return;
845 for (n = 0; n < count; n++) {
847 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
848 extract(data, offset + n * size, size);
850 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
851 && value[n] >= min && value[n] <= max
852 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
853 goto exit;
856 for (n = 0; n < count; n++) {
858 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
859 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
860 continue;
863 if (field->value[n] >= min && field->value[n] <= max
864 && field->usage[field->value[n] - min].hid
865 && search(value, field->value[n], count))
866 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
868 if (value[n] >= min && value[n] <= max
869 && field->usage[value[n] - min].hid
870 && search(field->value, value[n], count))
871 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
874 memcpy(field->value, value, count * sizeof(__s32));
875 exit:
876 kfree(value);
878 EXPORT_SYMBOL_GPL(hid_input_field);
881 * Output the field into the report.
884 static void hid_output_field(struct hid_field *field, __u8 *data)
886 unsigned count = field->report_count;
887 unsigned offset = field->report_offset;
888 unsigned size = field->report_size;
889 unsigned n;
891 for (n = 0; n < count; n++) {
892 if (field->logical_minimum < 0) /* signed values */
893 implement(data, offset + n * size, size, s32ton(field->value[n], size));
894 else /* unsigned values */
895 implement(data, offset + n * size, size, field->value[n]);
900 * Create a report.
903 void hid_output_report(struct hid_report *report, __u8 *data)
905 unsigned n;
907 if (report->id > 0)
908 *data++ = report->id;
910 for (n = 0; n < report->maxfield; n++)
911 hid_output_field(report->field[n], data);
913 EXPORT_SYMBOL_GPL(hid_output_report);
916 * Set a field value. The report this field belongs to has to be
917 * created and transferred to the device, to set this value in the
918 * device.
921 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
923 unsigned size = field->report_size;
925 hid_dump_input(field->usage + offset, value);
927 if (offset >= field->report_count) {
928 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
929 hid_dump_field(field, 8);
930 return -1;
932 if (field->logical_minimum < 0) {
933 if (value != snto32(s32ton(value, size), size)) {
934 dbg("value %d is out of range", value);
935 return -1;
938 field->value[offset] = value;
939 return 0;
941 EXPORT_SYMBOL_GPL(hid_set_field);