Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / drivers / hid / hid-core.c
1 /*
2  *  HID support for Linux
3  *
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/list.h>
22 #include <linux/mm.h>
23 #include <linux/smp_lock.h>
24 #include <linux/spinlock.h>
25 #include <asm/unaligned.h>
26 #include <asm/byteorder.h>
27 #include <linux/input.h>
28 #include <linux/wait.h>
29
30 #include <linux/hid.h>
31 #include <linux/hiddev.h>
32 #include <linux/hid-debug.h>
33
34 /*
35  * Version Information
36  */
37
38 #define DRIVER_VERSION "v2.6"
39 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
40 #define DRIVER_DESC "HID core driver"
41 #define DRIVER_LICENSE "GPL"
42
43 /*
44  * Register a new report for a device.
45  */
46
47 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
48 {
49         struct hid_report_enum *report_enum = device->report_enum + type;
50         struct hid_report *report;
51
52         if (report_enum->report_id_hash[id])
53                 return report_enum->report_id_hash[id];
54
55         if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
56                 return NULL;
57
58         if (id != 0)
59                 report_enum->numbered = 1;
60
61         report->id = id;
62         report->type = type;
63         report->size = 0;
64         report->device = device;
65         report_enum->report_id_hash[id] = report;
66
67         list_add_tail(&report->list, &report_enum->report_list);
68
69         return report;
70 }
71
72 /*
73  * Register a new field for this report.
74  */
75
76 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
77 {
78         struct hid_field *field;
79
80         if (report->maxfield == HID_MAX_FIELDS) {
81                 dbg("too many fields in report");
82                 return NULL;
83         }
84
85         if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
86                 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
87
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;
93
94         return field;
95 }
96
97 /*
98  * Open a collection. The type/usage is pushed on the stack.
99  */
100
101 static int open_collection(struct hid_parser *parser, unsigned type)
102 {
103         struct hid_collection *collection;
104         unsigned usage;
105
106         usage = parser->local.usage[0];
107
108         if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
109                 dbg("collection stack overflow");
110                 return -1;
111         }
112
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;
119                 }
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;
129         }
130
131         parser->collection_stack[parser->collection_stack_ptr++] =
132                 parser->device->maxcollection;
133
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;
139
140         if (type == HID_COLLECTION_APPLICATION)
141                 parser->device->maxapplication++;
142
143         return 0;
144 }
145
146 /*
147  * Close a collection.
148  */
149
150 static int close_collection(struct hid_parser *parser)
151 {
152         if (!parser->collection_stack_ptr) {
153                 dbg("collection stack underflow");
154                 return -1;
155         }
156         parser->collection_stack_ptr--;
157         return 0;
158 }
159
160 /*
161  * Climb up the stack, search for the specified collection type
162  * and return the usage.
163  */
164
165 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
166 {
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 */
172 }
173
174 /*
175  * Add a usage to the temporary parser table.
176  */
177
178 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
179 {
180         if (parser->local.usage_index >= HID_MAX_USAGES) {
181                 dbg("usage index exceeded");
182                 return -1;
183         }
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;
190 }
191
192 /*
193  * Register a new field for this report.
194  */
195
196 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
197 {
198         struct hid_report *report;
199         struct hid_field *field;
200         int usages;
201         unsigned offset;
202         int i;
203
204         if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
205                 dbg("hid_register_report failed");
206                 return -1;
207         }
208
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;
212         }
213
214         offset = report->size;
215         report->size += parser->global.report_size * parser->global.report_count;
216
217         if (!parser->local.usage_index) /* Ignore padding fields */
218                 return 0;
219
220         usages = max_t(int, parser->local.usage_index, parser->global.report_count);
221
222         if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
223                 return 0;
224
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);
228
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];
237         }
238
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;
251
252         return 0;
253 }
254
255 /*
256  * Read data value from item.
257  */
258
259 static u32 item_udata(struct hid_item *item)
260 {
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;
265         }
266         return 0;
267 }
268
269 static s32 item_sdata(struct hid_item *item)
270 {
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;
275         }
276         return 0;
277 }
278
279 /*
280  * Process a global item.
281  */
282
283 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
284 {
285         switch (item->tag) {
286
287                 case HID_GLOBAL_ITEM_TAG_PUSH:
288
289                         if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
290                                 dbg("global enviroment stack overflow");
291                                 return -1;
292                         }
293
294                         memcpy(parser->global_stack + parser->global_stack_ptr++,
295                                 &parser->global, sizeof(struct hid_global));
296                         return 0;
297
298                 case HID_GLOBAL_ITEM_TAG_POP:
299
300                         if (!parser->global_stack_ptr) {
301                                 dbg("global enviroment stack underflow");
302                                 return -1;
303                         }
304
305                         memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
306                                 sizeof(struct hid_global));
307                         return 0;
308
309                 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
310                         parser->global.usage_page = item_udata(item);
311                         return 0;
312
313                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
314                         parser->global.logical_minimum = item_sdata(item);
315                         return 0;
316
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;
323
324                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
325                         parser->global.physical_minimum = item_sdata(item);
326                         return 0;
327
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;
334
335                 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
336                         parser->global.unit_exponent = item_sdata(item);
337                         return 0;
338
339                 case HID_GLOBAL_ITEM_TAG_UNIT:
340                         parser->global.unit = item_udata(item);
341                         return 0;
342
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;
347                         }
348                         return 0;
349
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;
354                         }
355                         return 0;
356
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;
361                         }
362                         return 0;
363
364                 default:
365                         dbg("unknown global tag 0x%x", item->tag);
366                         return -1;
367         }
368 }
369
370 /*
371  * Process a local item.
372  */
373
374 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
375 {
376         __u32 data;
377         unsigned n;
378
379         if (item->size == 0) {
380                 dbg("item data expected for local item");
381                 return -1;
382         }
383
384         data = item_udata(item);
385
386         switch (item->tag) {
387
388                 case HID_LOCAL_ITEM_TAG_DELIMITER:
389
390                         if (data) {
391                                 /*
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.
396                                  */
397                                 if (parser->local.delimiter_depth != 0) {
398                                         dbg("nested delimiters");
399                                         return -1;
400                                 }
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;
407                                 }
408                                 parser->local.delimiter_depth--;
409                         }
410                         return 1;
411
412                 case HID_LOCAL_ITEM_TAG_USAGE:
413
414                         if (parser->local.delimiter_branch > 1) {
415                                 dbg("alternative usage ignored");
416                                 return 0;
417                         }
418
419                         if (item->size <= 2)
420                                 data = (parser->global.usage_page << 16) + data;
421
422                         return hid_add_usage(parser, data);
423
424                 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
425
426                         if (parser->local.delimiter_branch > 1) {
427                                 dbg("alternative usage ignored");
428                                 return 0;
429                         }
430
431                         if (item->size <= 2)
432                                 data = (parser->global.usage_page << 16) + data;
433
434                         parser->local.usage_minimum = data;
435                         return 0;
436
437                 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
438
439                         if (parser->local.delimiter_branch > 1) {
440                                 dbg("alternative usage ignored");
441                                 return 0;
442                         }
443
444                         if (item->size <= 2)
445                                 data = (parser->global.usage_page << 16) + data;
446
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;
451                                 }
452                         return 0;
453
454                 default:
455
456                         dbg("unknown local item tag 0x%x", item->tag);
457                         return 0;
458         }
459         return 0;
460 }
461
462 /*
463  * Process a main item.
464  */
465
466 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
467 {
468         __u32 data;
469         int ret;
470
471         data = item_udata(item);
472
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;
492         }
493
494         memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
495
496         return ret;
497 }
498
499 /*
500  * Process a reserved item.
501  */
502
503 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
504 {
505         dbg("reserved item type, tag 0x%x", item->tag);
506         return 0;
507 }
508
509 /*
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.
513  */
514
515 static void hid_free_report(struct hid_report *report)
516 {
517         unsigned n;
518
519         for (n = 0; n < report->maxfield; n++)
520                 kfree(report->field[n]);
521         kfree(report);
522 }
523
524 /*
525  * Free a device structure, all reports, and all fields.
526  */
527
528 void hid_free_device(struct hid_device *device)
529 {
530         unsigned i,j;
531
532         for (i = 0; i < HID_REPORT_TYPES; i++) {
533                 struct hid_report_enum *report_enum = device->report_enum + i;
534
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);
539                 }
540         }
541
542         kfree(device->rdesc);
543         kfree(device->collection);
544         kfree(device);
545 }
546 EXPORT_SYMBOL_GPL(hid_free_device);
547
548 /*
549  * Fetch a report description item from the data stream. We support long
550  * items, though they are not used yet.
551  */
552
553 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
554 {
555         u8 b;
556
557         if ((end - start) <= 0)
558                 return NULL;
559
560         b = *start++;
561
562         item->type = (b >> 2) & 3;
563         item->tag  = (b >> 4) & 15;
564
565         if (item->tag == HID_ITEM_TAG_LONG) {
566
567                 item->format = HID_ITEM_FORMAT_LONG;
568
569                 if ((end - start) < 2)
570                         return NULL;
571
572                 item->size = *start++;
573                 item->tag  = *start++;
574
575                 if ((end - start) < item->size)
576                         return NULL;
577
578                 item->data.longdata = start;
579                 start += item->size;
580                 return start;
581         }
582
583         item->format = HID_ITEM_FORMAT_SHORT;
584         item->size = b & 3;
585
586         switch (item->size) {
587
588                 case 0:
589                         return start;
590
591                 case 1:
592                         if ((end - start) < 1)
593                                 return NULL;
594                         item->data.u8 = *start++;
595                         return start;
596
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;
603
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;
611         }
612
613         return NULL;
614 }
615
616 /*
617  * Parse a report description into a hid_device structure. Reports are
618  * enumerated, fields are attached to these reports.
619  */
620
621 struct hid_device *hid_parse_report(__u8 *start, unsigned size)
622 {
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
634         };
635
636         if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
637                 return NULL;
638
639         if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
640                                    HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
641                 kfree(device);
642                 return NULL;
643         }
644         device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
645
646         for (i = 0; i < HID_REPORT_TYPES; i++)
647                 INIT_LIST_HEAD(&device->report_enum[i].report_list);
648
649         if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
650                 kfree(device->collection);
651                 kfree(device);
652                 return NULL;
653         }
654         memcpy(device->rdesc, start, size);
655         device->rsize = size;
656
657         if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
658                 kfree(device->rdesc);
659                 kfree(device->collection);
660                 kfree(device);
661                 return NULL;
662         }
663         parser->device = device;
664
665         end = start + size;
666         while ((start = fetch_item(start, end, &item)) != NULL) {
667
668                 if (item.format != HID_ITEM_FORMAT_SHORT) {
669                         dbg("unexpected long global item");
670                         hid_free_device(device);
671                         kfree(parser);
672                         return NULL;
673                 }
674
675                 if (dispatch_type[item.type](parser, &item)) {
676                         dbg("item %u %u %u %u parsing failed\n",
677                                 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
678                         hid_free_device(device);
679                         kfree(parser);
680                         return NULL;
681                 }
682
683                 if (start == end) {
684                         if (parser->collection_stack_ptr) {
685                                 dbg("unbalanced collection at end of report description");
686                                 hid_free_device(device);
687                                 kfree(parser);
688                                 return NULL;
689                         }
690                         if (parser->local.delimiter_depth) {
691                                 dbg("unbalanced delimiter at end of report description");
692                                 hid_free_device(device);
693                                 kfree(parser);
694                                 return NULL;
695                         }
696                         kfree(parser);
697                         return device;
698                 }
699         }
700
701         dbg("item fetching failed at offset %d\n", (int)(end - start));
702         hid_free_device(device);
703         kfree(parser);
704         return NULL;
705 }
706 EXPORT_SYMBOL_GPL(hid_parse_report);
707
708 /*
709  * Convert a signed n-bit integer to signed 32-bit integer. Common
710  * cases are done through the compiler, the screwed things has to be
711  * done by hand.
712  */
713
714 static s32 snto32(__u32 value, unsigned n)
715 {
716         switch (n) {
717                 case 8:  return ((__s8)value);
718                 case 16: return ((__s16)value);
719                 case 32: return ((__s32)value);
720         }
721         return value & (1 << (n - 1)) ? value | (-1 << n) : value;
722 }
723
724 /*
725  * Convert a signed 32-bit integer to a signed n-bit integer.
726  */
727
728 static u32 s32ton(__s32 value, unsigned n)
729 {
730         s32 a = value >> (n - 1);
731         if (a && a != -1)
732                 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
733         return value & ((1 << n) - 1);
734 }
735
736 /*
737  * Extract/implement a data field from/to a little endian report (bit array).
738  *
739  * Code sort-of follows HID spec:
740  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
741  *
742  * While the USB HID spec allows unlimited length bit fields in "report
743  * descriptors", most devices never use more than 16 bits.
744  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
745  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
746  */
747
748 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
749 {
750         u64 x;
751
752         WARN_ON(n > 32);
753
754         report += offset >> 3;  /* adjust byte index */
755         offset &= 7;            /* now only need bit offset into one byte */
756         x = get_unaligned((u64 *) report);
757         x = le64_to_cpu(x);
758         x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
759         return (u32) x;
760 }
761
762 /*
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.
769  */
770 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
771 {
772         u64 x;
773         u64 m = (1ULL << n) - 1;
774
775         WARN_ON(n > 32);
776
777         WARN_ON(value > m);
778         value &= m;
779
780         report += offset >> 3;
781         offset &= 7;
782
783         x = get_unaligned((u64 *)report);
784         x &= cpu_to_le64(~(m << offset));
785         x |= cpu_to_le64(((u64) value) << offset);
786         put_unaligned(x, (u64 *) report);
787 }
788
789 /*
790  * Search an array for a value.
791  */
792
793 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
794 {
795         while (n--) {
796                 if (*array++ == value)
797                         return 0;
798         }
799         return -1;
800 }
801
802 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
803 {
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);
809 }
810
811 /*
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).
815  */
816
817 void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
818 {
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;
826
827         if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
828                 return;
829
830         for (n = 0; n < count; n++) {
831
832                         value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
833                                                     extract(data, offset + n * size, size);
834
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;
839         }
840
841         for (n = 0; n < count; n++) {
842
843                 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
844                         hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
845                         continue;
846                 }
847
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);
852
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);
857         }
858
859         memcpy(field->value, value, count * sizeof(__s32));
860 exit:
861         kfree(value);
862 }
863 EXPORT_SYMBOL_GPL(hid_input_field);
864
865 /*
866  * Output the field into the report.
867  */
868
869 static void hid_output_field(struct hid_field *field, __u8 *data)
870 {
871         unsigned count = field->report_count;
872         unsigned offset = field->report_offset;
873         unsigned size = field->report_size;
874         unsigned n;
875
876         /* make sure the unused bits in the last byte are zeros */
877         if (count > 0 && size > 0)
878                 data[(offset+count*size-1)/8] = 0;
879
880         for (n = 0; n < count; n++) {
881                 if (field->logical_minimum < 0) /* signed values */
882                         implement(data, offset + n * size, size, s32ton(field->value[n], size));
883                 else                            /* unsigned values */
884                         implement(data, offset + n * size, size, field->value[n]);
885         }
886 }
887
888 /*
889  * Create a report.
890  */
891
892 void hid_output_report(struct hid_report *report, __u8 *data)
893 {
894         unsigned n;
895
896         if (report->id > 0)
897                 *data++ = report->id;
898
899         for (n = 0; n < report->maxfield; n++)
900                 hid_output_field(report->field[n], data);
901 }
902 EXPORT_SYMBOL_GPL(hid_output_report);
903
904 /*
905  * Set a field value. The report this field belongs to has to be
906  * created and transferred to the device, to set this value in the
907  * device.
908  */
909
910 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
911 {
912         unsigned size = field->report_size;
913
914         hid_dump_input(field->usage + offset, value);
915
916         if (offset >= field->report_count) {
917                 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
918                 hid_dump_field(field, 8);
919                 return -1;
920         }
921         if (field->logical_minimum < 0) {
922                 if (value != snto32(s32ton(value, size), size)) {
923                         dbg("value %d is out of range", value);
924                         return -1;
925                 }
926         }
927         field->value[offset] = value;
928         return 0;
929 }
930 EXPORT_SYMBOL_GPL(hid_set_field);
931
932 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
933 {
934         struct hid_report_enum *report_enum = hid->report_enum + type;
935         struct hid_report *report;
936         int n, rsize;
937
938         if (!hid)
939                 return -ENODEV;
940
941         if (!size) {
942                 dbg("empty report");
943                 return -1;
944         }
945
946 #ifdef CONFIG_HID_DEBUG
947         printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
948 #endif
949
950         n = 0;                          /* Normally report number is 0 */
951         if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
952                 n = *data++;
953                 size--;
954         }
955
956 #ifdef CONFIG_HID_DEBUG
957         {
958                 int i;
959                 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, size);
960                 for (i = 0; i < size; i++)
961                         printk(" %02x", data[i]);
962                 printk("\n");
963         }
964 #endif
965
966         if (!(report = report_enum->report_id_hash[n])) {
967                 dbg("undefined report_id %d received", n);
968                 return -1;
969         }
970
971         rsize = ((report->size - 1) >> 3) + 1;
972
973         if (size < rsize) {
974                 dbg("report %d is too short, (%d < %d)", report->id, size, rsize);
975                 return -1;
976         }
977
978         if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
979                 hid->hiddev_report_event(hid, report);
980
981         for (n = 0; n < report->maxfield; n++)
982                 hid_input_field(hid, report->field[n], data, interrupt);
983
984         if (hid->claimed & HID_CLAIMED_INPUT)
985                 hidinput_report_event(hid, report);
986
987         return 0;
988 }
989 EXPORT_SYMBOL_GPL(hid_input_report);
990
991 MODULE_LICENSE(DRIVER_LICENSE);
992