Clean up E7520/7320/7525 quirk printk.
[linux-2.6/mini2440.git] / drivers / hid / hid-core.c
blob6ec04e79f6856c0340f20a31f0b08bb9b6afeeb6
1 /*
2 * HID support for Linux
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 */
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/mm.h>
23 #include <linux/spinlock.h>
24 #include <asm/unaligned.h>
25 #include <asm/byteorder.h>
26 #include <linux/input.h>
27 #include <linux/wait.h>
28 #include <linux/vmalloc.h>
30 #include <linux/hid.h>
31 #include <linux/hiddev.h>
32 #include <linux/hid-debug.h>
35 * Version Information
38 #define DRIVER_VERSION "v2.6"
39 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
40 #define DRIVER_DESC "HID core driver"
41 #define DRIVER_LICENSE "GPL"
44 * Register a new report for a device.
47 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
49 struct hid_report_enum *report_enum = device->report_enum + type;
50 struct hid_report *report;
52 if (report_enum->report_id_hash[id])
53 return report_enum->report_id_hash[id];
55 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
56 return NULL;
58 if (id != 0)
59 report_enum->numbered = 1;
61 report->id = id;
62 report->type = type;
63 report->size = 0;
64 report->device = device;
65 report_enum->report_id_hash[id] = report;
67 list_add_tail(&report->list, &report_enum->report_list);
69 return report;
73 * Register a new field for this report.
76 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
78 struct hid_field *field;
80 if (report->maxfield == HID_MAX_FIELDS) {
81 dbg("too many fields in report");
82 return NULL;
85 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
86 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
88 field->index = report->maxfield++;
89 report->field[field->index] = field;
90 field->usage = (struct hid_usage *)(field + 1);
91 field->value = (unsigned *)(field->usage + usages);
92 field->report = report;
94 return field;
98 * Open a collection. The type/usage is pushed on the stack.
101 static int open_collection(struct hid_parser *parser, unsigned type)
103 struct hid_collection *collection;
104 unsigned usage;
106 usage = parser->local.usage[0];
108 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
109 dbg("collection stack overflow");
110 return -1;
113 if (parser->device->maxcollection == parser->device->collection_size) {
114 collection = kmalloc(sizeof(struct hid_collection) *
115 parser->device->collection_size * 2, GFP_KERNEL);
116 if (collection == NULL) {
117 dbg("failed to reallocate collection array");
118 return -1;
120 memcpy(collection, parser->device->collection,
121 sizeof(struct hid_collection) *
122 parser->device->collection_size);
123 memset(collection + parser->device->collection_size, 0,
124 sizeof(struct hid_collection) *
125 parser->device->collection_size);
126 kfree(parser->device->collection);
127 parser->device->collection = collection;
128 parser->device->collection_size *= 2;
131 parser->collection_stack[parser->collection_stack_ptr++] =
132 parser->device->maxcollection;
134 collection = parser->device->collection +
135 parser->device->maxcollection++;
136 collection->type = type;
137 collection->usage = usage;
138 collection->level = parser->collection_stack_ptr - 1;
140 if (type == HID_COLLECTION_APPLICATION)
141 parser->device->maxapplication++;
143 return 0;
147 * Close a collection.
150 static int close_collection(struct hid_parser *parser)
152 if (!parser->collection_stack_ptr) {
153 dbg("collection stack underflow");
154 return -1;
156 parser->collection_stack_ptr--;
157 return 0;
161 * Climb up the stack, search for the specified collection type
162 * and return the usage.
165 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
167 int n;
168 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
169 if (parser->device->collection[parser->collection_stack[n]].type == type)
170 return parser->device->collection[parser->collection_stack[n]].usage;
171 return 0; /* we know nothing about this usage type */
175 * Add a usage to the temporary parser table.
178 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
180 if (parser->local.usage_index >= HID_MAX_USAGES) {
181 dbg("usage index exceeded");
182 return -1;
184 parser->local.usage[parser->local.usage_index] = usage;
185 parser->local.collection_index[parser->local.usage_index] =
186 parser->collection_stack_ptr ?
187 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
188 parser->local.usage_index++;
189 return 0;
193 * Register a new field for this report.
196 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
198 struct hid_report *report;
199 struct hid_field *field;
200 int usages;
201 unsigned offset;
202 int i;
204 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
205 dbg("hid_register_report failed");
206 return -1;
209 if (parser->global.logical_maximum < parser->global.logical_minimum) {
210 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
211 return -1;
214 offset = report->size;
215 report->size += parser->global.report_size * parser->global.report_count;
217 if (!parser->local.usage_index) /* Ignore padding fields */
218 return 0;
220 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
222 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
223 return 0;
225 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
226 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
227 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
229 for (i = 0; i < usages; i++) {
230 int j = i;
231 /* Duplicate the last usage we parsed if we have excess values */
232 if (i >= parser->local.usage_index)
233 j = parser->local.usage_index - 1;
234 field->usage[i].hid = parser->local.usage[j];
235 field->usage[i].collection_index =
236 parser->local.collection_index[j];
239 field->maxusage = usages;
240 field->flags = flags;
241 field->report_offset = offset;
242 field->report_type = report_type;
243 field->report_size = parser->global.report_size;
244 field->report_count = parser->global.report_count;
245 field->logical_minimum = parser->global.logical_minimum;
246 field->logical_maximum = parser->global.logical_maximum;
247 field->physical_minimum = parser->global.physical_minimum;
248 field->physical_maximum = parser->global.physical_maximum;
249 field->unit_exponent = parser->global.unit_exponent;
250 field->unit = parser->global.unit;
252 return 0;
256 * Read data value from item.
259 static u32 item_udata(struct hid_item *item)
261 switch (item->size) {
262 case 1: return item->data.u8;
263 case 2: return item->data.u16;
264 case 4: return item->data.u32;
266 return 0;
269 static s32 item_sdata(struct hid_item *item)
271 switch (item->size) {
272 case 1: return item->data.s8;
273 case 2: return item->data.s16;
274 case 4: return item->data.s32;
276 return 0;
280 * Process a global item.
283 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
285 switch (item->tag) {
287 case HID_GLOBAL_ITEM_TAG_PUSH:
289 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
290 dbg("global enviroment stack overflow");
291 return -1;
294 memcpy(parser->global_stack + parser->global_stack_ptr++,
295 &parser->global, sizeof(struct hid_global));
296 return 0;
298 case HID_GLOBAL_ITEM_TAG_POP:
300 if (!parser->global_stack_ptr) {
301 dbg("global enviroment stack underflow");
302 return -1;
305 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
306 sizeof(struct hid_global));
307 return 0;
309 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
310 parser->global.usage_page = item_udata(item);
311 return 0;
313 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
314 parser->global.logical_minimum = item_sdata(item);
315 return 0;
317 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
318 if (parser->global.logical_minimum < 0)
319 parser->global.logical_maximum = item_sdata(item);
320 else
321 parser->global.logical_maximum = item_udata(item);
322 return 0;
324 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
325 parser->global.physical_minimum = item_sdata(item);
326 return 0;
328 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
329 if (parser->global.physical_minimum < 0)
330 parser->global.physical_maximum = item_sdata(item);
331 else
332 parser->global.physical_maximum = item_udata(item);
333 return 0;
335 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
336 parser->global.unit_exponent = item_sdata(item);
337 return 0;
339 case HID_GLOBAL_ITEM_TAG_UNIT:
340 parser->global.unit = item_udata(item);
341 return 0;
343 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
344 if ((parser->global.report_size = item_udata(item)) > 32) {
345 dbg("invalid report_size %d", parser->global.report_size);
346 return -1;
348 return 0;
350 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
351 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
352 dbg("invalid report_count %d", parser->global.report_count);
353 return -1;
355 return 0;
357 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
358 if ((parser->global.report_id = item_udata(item)) == 0) {
359 dbg("report_id 0 is invalid");
360 return -1;
362 return 0;
364 default:
365 dbg("unknown global tag 0x%x", item->tag);
366 return -1;
371 * Process a local item.
374 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
376 __u32 data;
377 unsigned n;
379 if (item->size == 0) {
380 dbg("item data expected for local item");
381 return -1;
384 data = item_udata(item);
386 switch (item->tag) {
388 case HID_LOCAL_ITEM_TAG_DELIMITER:
390 if (data) {
392 * We treat items before the first delimiter
393 * as global to all usage sets (branch 0).
394 * In the moment we process only these global
395 * items and the first delimiter set.
397 if (parser->local.delimiter_depth != 0) {
398 dbg("nested delimiters");
399 return -1;
401 parser->local.delimiter_depth++;
402 parser->local.delimiter_branch++;
403 } else {
404 if (parser->local.delimiter_depth < 1) {
405 dbg("bogus close delimiter");
406 return -1;
408 parser->local.delimiter_depth--;
410 return 1;
412 case HID_LOCAL_ITEM_TAG_USAGE:
414 if (parser->local.delimiter_branch > 1) {
415 dbg("alternative usage ignored");
416 return 0;
419 if (item->size <= 2)
420 data = (parser->global.usage_page << 16) + data;
422 return hid_add_usage(parser, data);
424 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
426 if (parser->local.delimiter_branch > 1) {
427 dbg("alternative usage ignored");
428 return 0;
431 if (item->size <= 2)
432 data = (parser->global.usage_page << 16) + data;
434 parser->local.usage_minimum = data;
435 return 0;
437 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
439 if (parser->local.delimiter_branch > 1) {
440 dbg("alternative usage ignored");
441 return 0;
444 if (item->size <= 2)
445 data = (parser->global.usage_page << 16) + data;
447 for (n = parser->local.usage_minimum; n <= data; n++)
448 if (hid_add_usage(parser, n)) {
449 dbg("hid_add_usage failed\n");
450 return -1;
452 return 0;
454 default:
456 dbg("unknown local item tag 0x%x", item->tag);
457 return 0;
459 return 0;
463 * Process a main item.
466 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
468 __u32 data;
469 int ret;
471 data = item_udata(item);
473 switch (item->tag) {
474 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
475 ret = open_collection(parser, data & 0xff);
476 break;
477 case HID_MAIN_ITEM_TAG_END_COLLECTION:
478 ret = close_collection(parser);
479 break;
480 case HID_MAIN_ITEM_TAG_INPUT:
481 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
482 break;
483 case HID_MAIN_ITEM_TAG_OUTPUT:
484 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
485 break;
486 case HID_MAIN_ITEM_TAG_FEATURE:
487 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
488 break;
489 default:
490 dbg("unknown main item tag 0x%x", item->tag);
491 ret = 0;
494 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
496 return ret;
500 * Process a reserved item.
503 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
505 dbg("reserved item type, tag 0x%x", item->tag);
506 return 0;
510 * Free a report and all registered fields. The field->usage and
511 * field->value table's are allocated behind the field, so we need
512 * only to free(field) itself.
515 static void hid_free_report(struct hid_report *report)
517 unsigned n;
519 for (n = 0; n < report->maxfield; n++)
520 kfree(report->field[n]);
521 kfree(report);
525 * Free a device structure, all reports, and all fields.
528 void hid_free_device(struct hid_device *device)
530 unsigned i,j;
532 for (i = 0; i < HID_REPORT_TYPES; i++) {
533 struct hid_report_enum *report_enum = device->report_enum + i;
535 for (j = 0; j < 256; j++) {
536 struct hid_report *report = report_enum->report_id_hash[j];
537 if (report)
538 hid_free_report(report);
542 kfree(device->rdesc);
543 kfree(device->collection);
544 kfree(device);
546 EXPORT_SYMBOL_GPL(hid_free_device);
549 * Fetch a report description item from the data stream. We support long
550 * items, though they are not used yet.
553 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
555 u8 b;
557 if ((end - start) <= 0)
558 return NULL;
560 b = *start++;
562 item->type = (b >> 2) & 3;
563 item->tag = (b >> 4) & 15;
565 if (item->tag == HID_ITEM_TAG_LONG) {
567 item->format = HID_ITEM_FORMAT_LONG;
569 if ((end - start) < 2)
570 return NULL;
572 item->size = *start++;
573 item->tag = *start++;
575 if ((end - start) < item->size)
576 return NULL;
578 item->data.longdata = start;
579 start += item->size;
580 return start;
583 item->format = HID_ITEM_FORMAT_SHORT;
584 item->size = b & 3;
586 switch (item->size) {
588 case 0:
589 return start;
591 case 1:
592 if ((end - start) < 1)
593 return NULL;
594 item->data.u8 = *start++;
595 return start;
597 case 2:
598 if ((end - start) < 2)
599 return NULL;
600 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
601 start = (__u8 *)((__le16 *)start + 1);
602 return start;
604 case 3:
605 item->size++;
606 if ((end - start) < 4)
607 return NULL;
608 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
609 start = (__u8 *)((__le32 *)start + 1);
610 return start;
613 return NULL;
617 * Parse a report description into a hid_device structure. Reports are
618 * enumerated, fields are attached to these reports.
621 struct hid_device *hid_parse_report(__u8 *start, unsigned size)
623 struct hid_device *device;
624 struct hid_parser *parser;
625 struct hid_item item;
626 __u8 *end;
627 unsigned i;
628 static int (*dispatch_type[])(struct hid_parser *parser,
629 struct hid_item *item) = {
630 hid_parser_main,
631 hid_parser_global,
632 hid_parser_local,
633 hid_parser_reserved
636 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
637 return NULL;
639 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
640 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
641 kfree(device);
642 return NULL;
644 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
646 for (i = 0; i < HID_REPORT_TYPES; i++)
647 INIT_LIST_HEAD(&device->report_enum[i].report_list);
649 if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
650 kfree(device->collection);
651 kfree(device);
652 return NULL;
654 memcpy(device->rdesc, start, size);
655 device->rsize = size;
657 if (!(parser = vmalloc(sizeof(struct hid_parser)))) {
658 kfree(device->rdesc);
659 kfree(device->collection);
660 kfree(device);
661 return NULL;
663 memset(parser, 0, sizeof(struct hid_parser));
664 parser->device = device;
666 end = start + size;
667 while ((start = fetch_item(start, end, &item)) != NULL) {
669 if (item.format != HID_ITEM_FORMAT_SHORT) {
670 dbg("unexpected long global item");
671 hid_free_device(device);
672 vfree(parser);
673 return NULL;
676 if (dispatch_type[item.type](parser, &item)) {
677 dbg("item %u %u %u %u parsing failed\n",
678 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
679 hid_free_device(device);
680 vfree(parser);
681 return NULL;
684 if (start == end) {
685 if (parser->collection_stack_ptr) {
686 dbg("unbalanced collection at end of report description");
687 hid_free_device(device);
688 vfree(parser);
689 return NULL;
691 if (parser->local.delimiter_depth) {
692 dbg("unbalanced delimiter at end of report description");
693 hid_free_device(device);
694 vfree(parser);
695 return NULL;
697 vfree(parser);
698 return device;
702 dbg("item fetching failed at offset %d\n", (int)(end - start));
703 hid_free_device(device);
704 vfree(parser);
705 return NULL;
707 EXPORT_SYMBOL_GPL(hid_parse_report);
710 * Convert a signed n-bit integer to signed 32-bit integer. Common
711 * cases are done through the compiler, the screwed things has to be
712 * done by hand.
715 static s32 snto32(__u32 value, unsigned n)
717 switch (n) {
718 case 8: return ((__s8)value);
719 case 16: return ((__s16)value);
720 case 32: return ((__s32)value);
722 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
726 * Convert a signed 32-bit integer to a signed n-bit integer.
729 static u32 s32ton(__s32 value, unsigned n)
731 s32 a = value >> (n - 1);
732 if (a && a != -1)
733 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
734 return value & ((1 << n) - 1);
738 * Extract/implement a data field from/to a little endian report (bit array).
740 * Code sort-of follows HID spec:
741 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
743 * While the USB HID spec allows unlimited length bit fields in "report
744 * descriptors", most devices never use more than 16 bits.
745 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
746 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
749 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
751 u64 x;
753 WARN_ON(n > 32);
755 report += offset >> 3; /* adjust byte index */
756 offset &= 7; /* now only need bit offset into one byte */
757 x = le64_to_cpu(get_unaligned((__le64 *) report));
758 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
759 return (u32) x;
763 * "implement" : set bits in a little endian bit stream.
764 * Same concepts as "extract" (see comments above).
765 * The data mangled in the bit stream remains in little endian
766 * order the whole time. It make more sense to talk about
767 * endianness of register values by considering a register
768 * a "cached" copy of the little endiad bit stream.
770 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
772 __le64 x;
773 u64 m = (1ULL << n) - 1;
775 WARN_ON(n > 32);
777 WARN_ON(value > m);
778 value &= m;
780 report += offset >> 3;
781 offset &= 7;
783 x = get_unaligned((__le64 *)report);
784 x &= cpu_to_le64(~(m << offset));
785 x |= cpu_to_le64(((u64) value) << offset);
786 put_unaligned(x, (__le64 *) report);
790 * Search an array for a value.
793 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
795 while (n--) {
796 if (*array++ == value)
797 return 0;
799 return -1;
802 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
804 hid_dump_input(usage, value);
805 if (hid->claimed & HID_CLAIMED_INPUT)
806 hidinput_hid_event(hid, field, usage, value);
807 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
808 hid->hiddev_hid_event(hid, field, usage, value);
812 * Analyse a received field, and fetch the data from it. The field
813 * content is stored for next report processing (we do differential
814 * reporting to the layer).
817 void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
819 unsigned n;
820 unsigned count = field->report_count;
821 unsigned offset = field->report_offset;
822 unsigned size = field->report_size;
823 __s32 min = field->logical_minimum;
824 __s32 max = field->logical_maximum;
825 __s32 *value;
827 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
828 return;
830 for (n = 0; n < count; n++) {
832 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
833 extract(data, offset + n * size, size);
835 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
836 && value[n] >= min && value[n] <= max
837 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
838 goto exit;
841 for (n = 0; n < count; n++) {
843 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
844 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
845 continue;
848 if (field->value[n] >= min && field->value[n] <= max
849 && field->usage[field->value[n] - min].hid
850 && search(value, field->value[n], count))
851 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
853 if (value[n] >= min && value[n] <= max
854 && field->usage[value[n] - min].hid
855 && search(field->value, value[n], count))
856 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
859 memcpy(field->value, value, count * sizeof(__s32));
860 exit:
861 kfree(value);
863 EXPORT_SYMBOL_GPL(hid_input_field);
866 * Output the field into the report.
869 static void hid_output_field(struct hid_field *field, __u8 *data)
871 unsigned count = field->report_count;
872 unsigned offset = field->report_offset;
873 unsigned size = field->report_size;
874 unsigned bitsused = offset + count * size;
875 unsigned n;
877 /* make sure the unused bits in the last byte are zeros */
878 if (count > 0 && size > 0 && (bitsused % 8) != 0)
879 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
881 for (n = 0; n < count; n++) {
882 if (field->logical_minimum < 0) /* signed values */
883 implement(data, offset + n * size, size, s32ton(field->value[n], size));
884 else /* unsigned values */
885 implement(data, offset + n * size, size, field->value[n]);
890 * Create a report.
893 void hid_output_report(struct hid_report *report, __u8 *data)
895 unsigned n;
897 if (report->id > 0)
898 *data++ = report->id;
900 for (n = 0; n < report->maxfield; n++)
901 hid_output_field(report->field[n], data);
903 EXPORT_SYMBOL_GPL(hid_output_report);
906 * Set a field value. The report this field belongs to has to be
907 * created and transferred to the device, to set this value in the
908 * device.
911 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
913 unsigned size = field->report_size;
915 hid_dump_input(field->usage + offset, value);
917 if (offset >= field->report_count) {
918 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
919 hid_dump_field(field, 8);
920 return -1;
922 if (field->logical_minimum < 0) {
923 if (value != snto32(s32ton(value, size), size)) {
924 dbg("value %d is out of range", value);
925 return -1;
928 field->value[offset] = value;
929 return 0;
931 EXPORT_SYMBOL_GPL(hid_set_field);
933 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
935 struct hid_report_enum *report_enum = hid->report_enum + type;
936 struct hid_report *report;
937 int n, rsize;
939 if (!hid)
940 return -ENODEV;
942 if (!size) {
943 dbg("empty report");
944 return -1;
947 #ifdef CONFIG_HID_DEBUG
948 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
949 #endif
951 n = 0; /* Normally report number is 0 */
952 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
953 n = *data++;
954 size--;
957 #ifdef CONFIG_HID_DEBUG
959 int i;
960 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, size);
961 for (i = 0; i < size; i++)
962 printk(" %02x", data[i]);
963 printk("\n");
965 #endif
967 if (!(report = report_enum->report_id_hash[n])) {
968 dbg("undefined report_id %d received", n);
969 return -1;
972 rsize = ((report->size - 1) >> 3) + 1;
974 if (size < rsize) {
975 dbg("report %d is too short, (%d < %d)", report->id, size, rsize);
976 memset(data + size, 0, rsize - size);
979 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
980 hid->hiddev_report_event(hid, report);
982 for (n = 0; n < report->maxfield; n++)
983 hid_input_field(hid, report->field[n], data, interrupt);
985 if (hid->claimed & HID_CLAIMED_INPUT)
986 hidinput_report_event(hid, report);
988 return 0;
990 EXPORT_SYMBOL_GPL(hid_input_report);
992 MODULE_LICENSE(DRIVER_LICENSE);