blob: 1dd9e4f0df1eb2cee1ee7a097f499b9ac9013ab7 [file] [log] [blame]
Jiri Kosinadde58452006-12-08 18:40:44 +01001/*
Jiri Kosina229695e2006-12-08 18:40:53 +01002 * HID support for Linux
Jiri Kosinadde58452006-12-08 18:40:44 +01003 *
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 */
9
10/*
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.
15 */
16
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>
30
31#undef DEBUG
32#undef DEBUG_DATA
33
Jiri Kosinadde58452006-12-08 18:40:44 +010034#include <linux/hid.h>
35#include <linux/hiddev.h>
36
37/*
38 * Version Information
39 */
40
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"
45
46/*
47 * Module parameters.
48 */
49
50static unsigned int hid_mousepoll_interval;
51module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
52MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
53
54/*
55 * Register a new report for a device.
56 */
57
58static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
59{
60 struct hid_report_enum *report_enum = device->report_enum + type;
61 struct hid_report *report;
62
63 if (report_enum->report_id_hash[id])
64 return report_enum->report_id_hash[id];
65
66 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
67 return NULL;
68
69 if (id != 0)
70 report_enum->numbered = 1;
71
72 report->id = id;
73 report->type = type;
74 report->size = 0;
75 report->device = device;
76 report_enum->report_id_hash[id] = report;
77
78 list_add_tail(&report->list, &report_enum->report_list);
79
80 return report;
81}
82
83/*
84 * Register a new field for this report.
85 */
86
87static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
88{
89 struct hid_field *field;
90
91 if (report->maxfield == HID_MAX_FIELDS) {
92 dbg("too many fields in report");
93 return NULL;
94 }
95
96 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
98
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;
104
105 return field;
106}
107
108/*
109 * Open a collection. The type/usage is pushed on the stack.
110 */
111
112static int open_collection(struct hid_parser *parser, unsigned type)
113{
114 struct hid_collection *collection;
115 unsigned usage;
116
117 usage = parser->local.usage[0];
118
119 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
120 dbg("collection stack overflow");
121 return -1;
122 }
123
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;
130 }
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;
140 }
141
142 parser->collection_stack[parser->collection_stack_ptr++] =
143 parser->device->maxcollection;
144
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;
150
151 if (type == HID_COLLECTION_APPLICATION)
152 parser->device->maxapplication++;
153
154 return 0;
155}
156
157/*
158 * Close a collection.
159 */
160
161static int close_collection(struct hid_parser *parser)
162{
163 if (!parser->collection_stack_ptr) {
164 dbg("collection stack underflow");
165 return -1;
166 }
167 parser->collection_stack_ptr--;
168 return 0;
169}
170
171/*
172 * Climb up the stack, search for the specified collection type
173 * and return the usage.
174 */
175
176static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
177{
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 */
183}
184
185/*
186 * Add a usage to the temporary parser table.
187 */
188
189static int hid_add_usage(struct hid_parser *parser, unsigned usage)
190{
191 if (parser->local.usage_index >= HID_MAX_USAGES) {
192 dbg("usage index exceeded");
193 return -1;
194 }
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;
201}
202
203/*
204 * Register a new field for this report.
205 */
206
207static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
208{
209 struct hid_report *report;
210 struct hid_field *field;
211 int usages;
212 unsigned offset;
213 int i;
214
215 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
216 dbg("hid_register_report failed");
217 return -1;
218 }
219
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;
223 }
224
225 offset = report->size;
226 report->size += parser->global.report_size * parser->global.report_count;
227
228 if (!parser->local.usage_index) /* Ignore padding fields */
229 return 0;
230
231 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
232
233 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
234 return 0;
235
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);
239
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];
248 }
249
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;
262
263 return 0;
264}
265
266/*
267 * Read data value from item.
268 */
269
270static u32 item_udata(struct hid_item *item)
271{
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;
276 }
277 return 0;
278}
279
280static s32 item_sdata(struct hid_item *item)
281{
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;
286 }
287 return 0;
288}
289
290/*
291 * Process a global item.
292 */
293
294static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
295{
296 switch (item->tag) {
297
298 case HID_GLOBAL_ITEM_TAG_PUSH:
299
300 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
301 dbg("global enviroment stack overflow");
302 return -1;
303 }
304
305 memcpy(parser->global_stack + parser->global_stack_ptr++,
306 &parser->global, sizeof(struct hid_global));
307 return 0;
308
309 case HID_GLOBAL_ITEM_TAG_POP:
310
311 if (!parser->global_stack_ptr) {
312 dbg("global enviroment stack underflow");
313 return -1;
314 }
315
316 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
317 sizeof(struct hid_global));
318 return 0;
319
320 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
321 parser->global.usage_page = item_udata(item);
322 return 0;
323
324 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
325 parser->global.logical_minimum = item_sdata(item);
326 return 0;
327
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;
334
335 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
336 parser->global.physical_minimum = item_sdata(item);
337 return 0;
338
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;
345
346 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
347 parser->global.unit_exponent = item_sdata(item);
348 return 0;
349
350 case HID_GLOBAL_ITEM_TAG_UNIT:
351 parser->global.unit = item_udata(item);
352 return 0;
353
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;
358 }
359 return 0;
360
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;
365 }
366 return 0;
367
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;
372 }
373 return 0;
374
375 default:
376 dbg("unknown global tag 0x%x", item->tag);
377 return -1;
378 }
379}
380
381/*
382 * Process a local item.
383 */
384
385static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
386{
387 __u32 data;
388 unsigned n;
389
390 if (item->size == 0) {
391 dbg("item data expected for local item");
392 return -1;
393 }
394
395 data = item_udata(item);
396
397 switch (item->tag) {
398
399 case HID_LOCAL_ITEM_TAG_DELIMITER:
400
401 if (data) {
402 /*
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.
407 */
408 if (parser->local.delimiter_depth != 0) {
409 dbg("nested delimiters");
410 return -1;
411 }
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;
418 }
419 parser->local.delimiter_depth--;
420 }
421 return 1;
422
423 case HID_LOCAL_ITEM_TAG_USAGE:
424
425 if (parser->local.delimiter_branch > 1) {
426 dbg("alternative usage ignored");
427 return 0;
428 }
429
430 if (item->size <= 2)
431 data = (parser->global.usage_page << 16) + data;
432
433 return hid_add_usage(parser, data);
434
435 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
436
437 if (parser->local.delimiter_branch > 1) {
438 dbg("alternative usage ignored");
439 return 0;
440 }
441
442 if (item->size <= 2)
443 data = (parser->global.usage_page << 16) + data;
444
445 parser->local.usage_minimum = data;
446 return 0;
447
448 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
449
450 if (parser->local.delimiter_branch > 1) {
451 dbg("alternative usage ignored");
452 return 0;
453 }
454
455 if (item->size <= 2)
456 data = (parser->global.usage_page << 16) + data;
457
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;
462 }
463 return 0;
464
465 default:
466
467 dbg("unknown local item tag 0x%x", item->tag);
468 return 0;
469 }
470 return 0;
471}
472
473/*
474 * Process a main item.
475 */
476
477static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
478{
479 __u32 data;
480 int ret;
481
482 data = item_udata(item);
483
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;
503 }
504
505 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
506
507 return ret;
508}
509
510/*
511 * Process a reserved item.
512 */
513
514static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
515{
516 dbg("reserved item type, tag 0x%x", item->tag);
517 return 0;
518}
519
520/*
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.
524 */
525
526static void hid_free_report(struct hid_report *report)
527{
528 unsigned n;
529
530 for (n = 0; n < report->maxfield; n++)
531 kfree(report->field[n]);
532 kfree(report);
533}
534
535/*
536 * Free a device structure, all reports, and all fields.
537 */
538
Jiri Kosina229695e2006-12-08 18:40:53 +0100539void hid_free_device(struct hid_device *device)
Jiri Kosinadde58452006-12-08 18:40:44 +0100540{
541 unsigned i,j;
542
543 for (i = 0; i < HID_REPORT_TYPES; i++) {
544 struct hid_report_enum *report_enum = device->report_enum + i;
545
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);
550 }
551 }
552
553 kfree(device->rdesc);
554 kfree(device);
555}
Jiri Kosina229695e2006-12-08 18:40:53 +0100556EXPORT_SYMBOL_GPL(hid_free_device);
Jiri Kosinadde58452006-12-08 18:40:44 +0100557
558/*
559 * Fetch a report description item from the data stream. We support long
560 * items, though they are not used yet.
561 */
562
563static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
564{
565 u8 b;
566
567 if ((end - start) <= 0)
568 return NULL;
569
570 b = *start++;
571
572 item->type = (b >> 2) & 3;
573 item->tag = (b >> 4) & 15;
574
575 if (item->tag == HID_ITEM_TAG_LONG) {
576
577 item->format = HID_ITEM_FORMAT_LONG;
578
579 if ((end - start) < 2)
580 return NULL;
581
582 item->size = *start++;
583 item->tag = *start++;
584
585 if ((end - start) < item->size)
586 return NULL;
587
588 item->data.longdata = start;
589 start += item->size;
590 return start;
591 }
592
593 item->format = HID_ITEM_FORMAT_SHORT;
594 item->size = b & 3;
595
596 switch (item->size) {
597
598 case 0:
599 return start;
600
601 case 1:
602 if ((end - start) < 1)
603 return NULL;
604 item->data.u8 = *start++;
605 return start;
606
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;
613
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;
621 }
622
623 return NULL;
624}
625
626/*
627 * Parse a report description into a hid_device structure. Reports are
628 * enumerated, fields are attached to these reports.
629 */
630
Jiri Kosina229695e2006-12-08 18:40:53 +0100631struct hid_device *hid_parse_report(__u8 *start, unsigned size)
Jiri Kosinadde58452006-12-08 18:40:44 +0100632{
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
644 };
645
646 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
647 return NULL;
648
649 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
650 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
651 kfree(device);
652 return NULL;
653 }
654 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
655
656 for (i = 0; i < HID_REPORT_TYPES; i++)
657 INIT_LIST_HEAD(&device->report_enum[i].report_list);
658
659 if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
660 kfree(device->collection);
661 kfree(device);
662 return NULL;
663 }
664 memcpy(device->rdesc, start, size);
665 device->rsize = size;
666
667 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
668 kfree(device->rdesc);
669 kfree(device->collection);
670 kfree(device);
671 return NULL;
672 }
673 parser->device = device;
674
675 end = start + size;
676 while ((start = fetch_item(start, end, &item)) != NULL) {
677
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;
684 }
685
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;
693 }
694
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;
702 }
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;
709 }
710 kfree(parser);
711 return device;
712 }
713 }
714
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;
720}
Jiri Kosina229695e2006-12-08 18:40:53 +0100721EXPORT_SYMBOL_GPL(hid_parse_report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100722
723/*
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.
727 */
728
729static s32 snto32(__u32 value, unsigned n)
730{
731 switch (n) {
732 case 8: return ((__s8)value);
733 case 16: return ((__s16)value);
734 case 32: return ((__s32)value);
735 }
736 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
737}
738
739/*
740 * Convert a signed 32-bit integer to a signed n-bit integer.
741 */
742
743static u32 s32ton(__s32 value, unsigned n)
744{
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);
749}
750
751/*
752 * Extract/implement a data field from/to a little endian report (bit array).
753 *
754 * Code sort-of follows HID spec:
755 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
756 *
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".
761 */
762
763static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
764{
765 u64 x;
766
767 WARN_ON(n > 32);
768
769 report += offset >> 3; /* adjust byte index */
Jiri Kosina229695e2006-12-08 18:40:53 +0100770 offset &= 7; /* now only need bit offset into one byte */
Jiri Kosinadde58452006-12-08 18:40:44 +0100771 x = get_unaligned((u64 *) report);
772 x = le64_to_cpu(x);
Jiri Kosina229695e2006-12-08 18:40:53 +0100773 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
Jiri Kosinadde58452006-12-08 18:40:44 +0100774 return (u32) x;
775}
776
777/*
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.
784 */
785static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
786{
787 u64 x;
788 u64 m = (1ULL << n) - 1;
789
790 WARN_ON(n > 32);
791
792 WARN_ON(value > m);
793 value &= m;
794
795 report += offset >> 3;
796 offset &= 7;
797
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);
802}
803
804/*
805 * Search an array for a value.
806 */
807
808static __inline__ int search(__s32 *array, __s32 value, unsigned n)
809{
810 while (n--) {
811 if (*array++ == value)
812 return 0;
813 }
814 return -1;
815}
816
817static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
818{
819 hid_dump_input(usage, value);
820 if (hid->claimed & HID_CLAIMED_INPUT)
821 hidinput_hid_event(hid, field, usage, value);
Jiri Kosinaaa938f72006-12-08 18:41:10 +0100822 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
823 hid->hiddev_hid_event(hid, field, usage, value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100824}
825
826/*
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).
830 */
831
Jiri Kosina229695e2006-12-08 18:40:53 +0100832void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
Jiri Kosinadde58452006-12-08 18:40:44 +0100833{
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;
841
842 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
843 return;
844
845 for (n = 0; n < count; n++) {
846
847 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
848 extract(data, offset + n * size, size);
849
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;
854 }
855
856 for (n = 0; n < count; n++) {
857
858 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
859 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
860 continue;
861 }
862
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);
867
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);
872 }
873
874 memcpy(field->value, value, count * sizeof(__s32));
875exit:
876 kfree(value);
877}
Jiri Kosina229695e2006-12-08 18:40:53 +0100878EXPORT_SYMBOL_GPL(hid_input_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100879
880/*
881 * Output the field into the report.
882 */
883
884static void hid_output_field(struct hid_field *field, __u8 *data)
885{
886 unsigned count = field->report_count;
887 unsigned offset = field->report_offset;
888 unsigned size = field->report_size;
889 unsigned n;
890
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]);
896 }
897}
898
899/*
900 * Create a report.
901 */
902
Jiri Kosina229695e2006-12-08 18:40:53 +0100903void hid_output_report(struct hid_report *report, __u8 *data)
Jiri Kosinadde58452006-12-08 18:40:44 +0100904{
905 unsigned n;
906
907 if (report->id > 0)
908 *data++ = report->id;
909
910 for (n = 0; n < report->maxfield; n++)
911 hid_output_field(report->field[n], data);
912}
Jiri Kosina229695e2006-12-08 18:40:53 +0100913EXPORT_SYMBOL_GPL(hid_output_report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100914
915/*
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.
919 */
920
921int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
922{
923 unsigned size = field->report_size;
924
925 hid_dump_input(field->usage + offset, value);
926
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;
931 }
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;
936 }
937 }
938 field->value[offset] = value;
939 return 0;
940}
Jiri Kosina229695e2006-12-08 18:40:53 +0100941EXPORT_SYMBOL_GPL(hid_set_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100942
Jiri Kosinaaa938f72006-12-08 18:41:10 +0100943MODULE_LICENSE(DRIVER_LICENSE);
944