HID: make debugging output runtime-configurable
[safe/jmp/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-2007 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/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>
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, Jiri Kosina"
40 #define DRIVER_DESC "HID core driver"
41 #define DRIVER_LICENSE "GPL"
42
43 #ifdef CONFIG_HID_DEBUG
44 int hid_debug = 0;
45 module_param_named(debug, hid_debug, bool, 0600);
46 MODULE_PARM_DESC(debug, "Turn HID debugging mode on and off");
47 EXPORT_SYMBOL_GPL(hid_debug);
48 #endif
49
50 /*
51  * Register a new report for a device.
52  */
53
54 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
55 {
56         struct hid_report_enum *report_enum = device->report_enum + type;
57         struct hid_report *report;
58
59         if (report_enum->report_id_hash[id])
60                 return report_enum->report_id_hash[id];
61
62         if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
63                 return NULL;
64
65         if (id != 0)
66                 report_enum->numbered = 1;
67
68         report->id = id;
69         report->type = type;
70         report->size = 0;
71         report->device = device;
72         report_enum->report_id_hash[id] = report;
73
74         list_add_tail(&report->list, &report_enum->report_list);
75
76         return report;
77 }
78
79 /*
80  * Register a new field for this report.
81  */
82
83 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
84 {
85         struct hid_field *field;
86
87         if (report->maxfield == HID_MAX_FIELDS) {
88                 dbg_hid("too many fields in report\n");
89                 return NULL;
90         }
91
92         if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
93                 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
94
95         field->index = report->maxfield++;
96         report->field[field->index] = field;
97         field->usage = (struct hid_usage *)(field + 1);
98         field->value = (unsigned *)(field->usage + usages);
99         field->report = report;
100
101         return field;
102 }
103
104 /*
105  * Open a collection. The type/usage is pushed on the stack.
106  */
107
108 static int open_collection(struct hid_parser *parser, unsigned type)
109 {
110         struct hid_collection *collection;
111         unsigned usage;
112
113         usage = parser->local.usage[0];
114
115         if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
116                 dbg_hid("collection stack overflow\n");
117                 return -1;
118         }
119
120         if (parser->device->maxcollection == parser->device->collection_size) {
121                 collection = kmalloc(sizeof(struct hid_collection) *
122                                 parser->device->collection_size * 2, GFP_KERNEL);
123                 if (collection == NULL) {
124                         dbg_hid("failed to reallocate collection array\n");
125                         return -1;
126                 }
127                 memcpy(collection, parser->device->collection,
128                         sizeof(struct hid_collection) *
129                         parser->device->collection_size);
130                 memset(collection + parser->device->collection_size, 0,
131                         sizeof(struct hid_collection) *
132                         parser->device->collection_size);
133                 kfree(parser->device->collection);
134                 parser->device->collection = collection;
135                 parser->device->collection_size *= 2;
136         }
137
138         parser->collection_stack[parser->collection_stack_ptr++] =
139                 parser->device->maxcollection;
140
141         collection = parser->device->collection +
142                 parser->device->maxcollection++;
143         collection->type = type;
144         collection->usage = usage;
145         collection->level = parser->collection_stack_ptr - 1;
146
147         if (type == HID_COLLECTION_APPLICATION)
148                 parser->device->maxapplication++;
149
150         return 0;
151 }
152
153 /*
154  * Close a collection.
155  */
156
157 static int close_collection(struct hid_parser *parser)
158 {
159         if (!parser->collection_stack_ptr) {
160                 dbg_hid("collection stack underflow\n");
161                 return -1;
162         }
163         parser->collection_stack_ptr--;
164         return 0;
165 }
166
167 /*
168  * Climb up the stack, search for the specified collection type
169  * and return the usage.
170  */
171
172 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
173 {
174         int n;
175         for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
176                 if (parser->device->collection[parser->collection_stack[n]].type == type)
177                         return parser->device->collection[parser->collection_stack[n]].usage;
178         return 0; /* we know nothing about this usage type */
179 }
180
181 /*
182  * Add a usage to the temporary parser table.
183  */
184
185 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
186 {
187         if (parser->local.usage_index >= HID_MAX_USAGES) {
188                 dbg_hid("usage index exceeded\n");
189                 return -1;
190         }
191         parser->local.usage[parser->local.usage_index] = usage;
192         parser->local.collection_index[parser->local.usage_index] =
193                 parser->collection_stack_ptr ?
194                 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
195         parser->local.usage_index++;
196         return 0;
197 }
198
199 /*
200  * Register a new field for this report.
201  */
202
203 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
204 {
205         struct hid_report *report;
206         struct hid_field *field;
207         int usages;
208         unsigned offset;
209         int i;
210
211         if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
212                 dbg_hid("hid_register_report failed\n");
213                 return -1;
214         }
215
216         if (parser->global.logical_maximum < parser->global.logical_minimum) {
217                 dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
218                 return -1;
219         }
220
221         offset = report->size;
222         report->size += parser->global.report_size * parser->global.report_count;
223
224         if (!parser->local.usage_index) /* Ignore padding fields */
225                 return 0;
226
227         usages = max_t(int, parser->local.usage_index, parser->global.report_count);
228
229         if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
230                 return 0;
231
232         field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
233         field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
234         field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
235
236         for (i = 0; i < usages; i++) {
237                 int j = i;
238                 /* Duplicate the last usage we parsed if we have excess values */
239                 if (i >= parser->local.usage_index)
240                         j = parser->local.usage_index - 1;
241                 field->usage[i].hid = parser->local.usage[j];
242                 field->usage[i].collection_index =
243                         parser->local.collection_index[j];
244         }
245
246         field->maxusage = usages;
247         field->flags = flags;
248         field->report_offset = offset;
249         field->report_type = report_type;
250         field->report_size = parser->global.report_size;
251         field->report_count = parser->global.report_count;
252         field->logical_minimum = parser->global.logical_minimum;
253         field->logical_maximum = parser->global.logical_maximum;
254         field->physical_minimum = parser->global.physical_minimum;
255         field->physical_maximum = parser->global.physical_maximum;
256         field->unit_exponent = parser->global.unit_exponent;
257         field->unit = parser->global.unit;
258
259         return 0;
260 }
261
262 /*
263  * Read data value from item.
264  */
265
266 static u32 item_udata(struct hid_item *item)
267 {
268         switch (item->size) {
269                 case 1: return item->data.u8;
270                 case 2: return item->data.u16;
271                 case 4: return item->data.u32;
272         }
273         return 0;
274 }
275
276 static s32 item_sdata(struct hid_item *item)
277 {
278         switch (item->size) {
279                 case 1: return item->data.s8;
280                 case 2: return item->data.s16;
281                 case 4: return item->data.s32;
282         }
283         return 0;
284 }
285
286 /*
287  * Process a global item.
288  */
289
290 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
291 {
292         switch (item->tag) {
293
294                 case HID_GLOBAL_ITEM_TAG_PUSH:
295
296                         if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
297                                 dbg_hid("global enviroment stack overflow\n");
298                                 return -1;
299                         }
300
301                         memcpy(parser->global_stack + parser->global_stack_ptr++,
302                                 &parser->global, sizeof(struct hid_global));
303                         return 0;
304
305                 case HID_GLOBAL_ITEM_TAG_POP:
306
307                         if (!parser->global_stack_ptr) {
308                                 dbg_hid("global enviroment stack underflow\n");
309                                 return -1;
310                         }
311
312                         memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
313                                 sizeof(struct hid_global));
314                         return 0;
315
316                 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
317                         parser->global.usage_page = item_udata(item);
318                         return 0;
319
320                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
321                         parser->global.logical_minimum = item_sdata(item);
322                         return 0;
323
324                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
325                         if (parser->global.logical_minimum < 0)
326                                 parser->global.logical_maximum = item_sdata(item);
327                         else
328                                 parser->global.logical_maximum = item_udata(item);
329                         return 0;
330
331                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
332                         parser->global.physical_minimum = item_sdata(item);
333                         return 0;
334
335                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
336                         if (parser->global.physical_minimum < 0)
337                                 parser->global.physical_maximum = item_sdata(item);
338                         else
339                                 parser->global.physical_maximum = item_udata(item);
340                         return 0;
341
342                 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
343                         parser->global.unit_exponent = item_sdata(item);
344                         return 0;
345
346                 case HID_GLOBAL_ITEM_TAG_UNIT:
347                         parser->global.unit = item_udata(item);
348                         return 0;
349
350                 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
351                         if ((parser->global.report_size = item_udata(item)) > 32) {
352                                 dbg_hid("invalid report_size %d\n", parser->global.report_size);
353                                 return -1;
354                         }
355                         return 0;
356
357                 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
358                         if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
359                                 dbg_hid("invalid report_count %d\n", parser->global.report_count);
360                                 return -1;
361                         }
362                         return 0;
363
364                 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
365                         if ((parser->global.report_id = item_udata(item)) == 0) {
366                                 dbg_hid("report_id 0 is invalid\n");
367                                 return -1;
368                         }
369                         return 0;
370
371                 default:
372                         dbg_hid("unknown global tag 0x%x\n", item->tag);
373                         return -1;
374         }
375 }
376
377 /*
378  * Process a local item.
379  */
380
381 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
382 {
383         __u32 data;
384         unsigned n;
385
386         if (item->size == 0) {
387                 dbg_hid("item data expected for local item\n");
388                 return -1;
389         }
390
391         data = item_udata(item);
392
393         switch (item->tag) {
394
395                 case HID_LOCAL_ITEM_TAG_DELIMITER:
396
397                         if (data) {
398                                 /*
399                                  * We treat items before the first delimiter
400                                  * as global to all usage sets (branch 0).
401                                  * In the moment we process only these global
402                                  * items and the first delimiter set.
403                                  */
404                                 if (parser->local.delimiter_depth != 0) {
405                                         dbg_hid("nested delimiters\n");
406                                         return -1;
407                                 }
408                                 parser->local.delimiter_depth++;
409                                 parser->local.delimiter_branch++;
410                         } else {
411                                 if (parser->local.delimiter_depth < 1) {
412                                         dbg_hid("bogus close delimiter\n");
413                                         return -1;
414                                 }
415                                 parser->local.delimiter_depth--;
416                         }
417                         return 1;
418
419                 case HID_LOCAL_ITEM_TAG_USAGE:
420
421                         if (parser->local.delimiter_branch > 1) {
422                                 dbg_hid("alternative usage ignored\n");
423                                 return 0;
424                         }
425
426                         if (item->size <= 2)
427                                 data = (parser->global.usage_page << 16) + data;
428
429                         return hid_add_usage(parser, data);
430
431                 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
432
433                         if (parser->local.delimiter_branch > 1) {
434                                 dbg_hid("alternative usage ignored\n");
435                                 return 0;
436                         }
437
438                         if (item->size <= 2)
439                                 data = (parser->global.usage_page << 16) + data;
440
441                         parser->local.usage_minimum = data;
442                         return 0;
443
444                 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
445
446                         if (parser->local.delimiter_branch > 1) {
447                                 dbg_hid("alternative usage ignored\n");
448                                 return 0;
449                         }
450
451                         if (item->size <= 2)
452                                 data = (parser->global.usage_page << 16) + data;
453
454                         for (n = parser->local.usage_minimum; n <= data; n++)
455                                 if (hid_add_usage(parser, n)) {
456                                         dbg_hid("hid_add_usage failed\n");
457                                         return -1;
458                                 }
459                         return 0;
460
461                 default:
462
463                         dbg_hid("unknown local item tag 0x%x\n", item->tag);
464                         return 0;
465         }
466         return 0;
467 }
468
469 /*
470  * Process a main item.
471  */
472
473 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
474 {
475         __u32 data;
476         int ret;
477
478         data = item_udata(item);
479
480         switch (item->tag) {
481                 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
482                         ret = open_collection(parser, data & 0xff);
483                         break;
484                 case HID_MAIN_ITEM_TAG_END_COLLECTION:
485                         ret = close_collection(parser);
486                         break;
487                 case HID_MAIN_ITEM_TAG_INPUT:
488                         ret = hid_add_field(parser, HID_INPUT_REPORT, data);
489                         break;
490                 case HID_MAIN_ITEM_TAG_OUTPUT:
491                         ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
492                         break;
493                 case HID_MAIN_ITEM_TAG_FEATURE:
494                         ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
495                         break;
496                 default:
497                         dbg_hid("unknown main item tag 0x%x\n", item->tag);
498                         ret = 0;
499         }
500
501         memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
502
503         return ret;
504 }
505
506 /*
507  * Process a reserved item.
508  */
509
510 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
511 {
512         dbg_hid("reserved item type, tag 0x%x\n", item->tag);
513         return 0;
514 }
515
516 /*
517  * Free a report and all registered fields. The field->usage and
518  * field->value table's are allocated behind the field, so we need
519  * only to free(field) itself.
520  */
521
522 static void hid_free_report(struct hid_report *report)
523 {
524         unsigned n;
525
526         for (n = 0; n < report->maxfield; n++)
527                 kfree(report->field[n]);
528         kfree(report);
529 }
530
531 /*
532  * Free a device structure, all reports, and all fields.
533  */
534
535 void hid_free_device(struct hid_device *device)
536 {
537         unsigned i,j;
538
539         for (i = 0; i < HID_REPORT_TYPES; i++) {
540                 struct hid_report_enum *report_enum = device->report_enum + i;
541
542                 for (j = 0; j < 256; j++) {
543                         struct hid_report *report = report_enum->report_id_hash[j];
544                         if (report)
545                                 hid_free_report(report);
546                 }
547         }
548
549         kfree(device->rdesc);
550         kfree(device->collection);
551         kfree(device);
552 }
553 EXPORT_SYMBOL_GPL(hid_free_device);
554
555 /*
556  * Fetch a report description item from the data stream. We support long
557  * items, though they are not used yet.
558  */
559
560 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
561 {
562         u8 b;
563
564         if ((end - start) <= 0)
565                 return NULL;
566
567         b = *start++;
568
569         item->type = (b >> 2) & 3;
570         item->tag  = (b >> 4) & 15;
571
572         if (item->tag == HID_ITEM_TAG_LONG) {
573
574                 item->format = HID_ITEM_FORMAT_LONG;
575
576                 if ((end - start) < 2)
577                         return NULL;
578
579                 item->size = *start++;
580                 item->tag  = *start++;
581
582                 if ((end - start) < item->size)
583                         return NULL;
584
585                 item->data.longdata = start;
586                 start += item->size;
587                 return start;
588         }
589
590         item->format = HID_ITEM_FORMAT_SHORT;
591         item->size = b & 3;
592
593         switch (item->size) {
594
595                 case 0:
596                         return start;
597
598                 case 1:
599                         if ((end - start) < 1)
600                                 return NULL;
601                         item->data.u8 = *start++;
602                         return start;
603
604                 case 2:
605                         if ((end - start) < 2)
606                                 return NULL;
607                         item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
608                         start = (__u8 *)((__le16 *)start + 1);
609                         return start;
610
611                 case 3:
612                         item->size++;
613                         if ((end - start) < 4)
614                                 return NULL;
615                         item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
616                         start = (__u8 *)((__le32 *)start + 1);
617                         return start;
618         }
619
620         return NULL;
621 }
622
623 /*
624  * Parse a report description into a hid_device structure. Reports are
625  * enumerated, fields are attached to these reports.
626  */
627
628 struct hid_device *hid_parse_report(__u8 *start, unsigned size)
629 {
630         struct hid_device *device;
631         struct hid_parser *parser;
632         struct hid_item item;
633         __u8 *end;
634         unsigned i;
635         static int (*dispatch_type[])(struct hid_parser *parser,
636                                       struct hid_item *item) = {
637                 hid_parser_main,
638                 hid_parser_global,
639                 hid_parser_local,
640                 hid_parser_reserved
641         };
642
643         if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
644                 return NULL;
645
646         if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
647                                    HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
648                 kfree(device);
649                 return NULL;
650         }
651         device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
652
653         for (i = 0; i < HID_REPORT_TYPES; i++)
654                 INIT_LIST_HEAD(&device->report_enum[i].report_list);
655
656         if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
657                 kfree(device->collection);
658                 kfree(device);
659                 return NULL;
660         }
661         memcpy(device->rdesc, start, size);
662         device->rsize = size;
663
664         if (!(parser = vmalloc(sizeof(struct hid_parser)))) {
665                 kfree(device->rdesc);
666                 kfree(device->collection);
667                 kfree(device);
668                 return NULL;
669         }
670         memset(parser, 0, sizeof(struct hid_parser));
671         parser->device = device;
672
673         end = start + size;
674         while ((start = fetch_item(start, end, &item)) != NULL) {
675
676                 if (item.format != HID_ITEM_FORMAT_SHORT) {
677                         dbg_hid("unexpected long global item\n");
678                         hid_free_device(device);
679                         vfree(parser);
680                         return NULL;
681                 }
682
683                 if (dispatch_type[item.type](parser, &item)) {
684                         dbg_hid("item %u %u %u %u parsing failed\n",
685                                 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
686                         hid_free_device(device);
687                         vfree(parser);
688                         return NULL;
689                 }
690
691                 if (start == end) {
692                         if (parser->collection_stack_ptr) {
693                                 dbg_hid("unbalanced collection at end of report description\n");
694                                 hid_free_device(device);
695                                 vfree(parser);
696                                 return NULL;
697                         }
698                         if (parser->local.delimiter_depth) {
699                                 dbg_hid("unbalanced delimiter at end of report description\n");
700                                 hid_free_device(device);
701                                 vfree(parser);
702                                 return NULL;
703                         }
704                         vfree(parser);
705                         return device;
706                 }
707         }
708
709         dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
710         hid_free_device(device);
711         vfree(parser);
712         return NULL;
713 }
714 EXPORT_SYMBOL_GPL(hid_parse_report);
715
716 /*
717  * Convert a signed n-bit integer to signed 32-bit integer. Common
718  * cases are done through the compiler, the screwed things has to be
719  * done by hand.
720  */
721
722 static s32 snto32(__u32 value, unsigned n)
723 {
724         switch (n) {
725                 case 8:  return ((__s8)value);
726                 case 16: return ((__s16)value);
727                 case 32: return ((__s32)value);
728         }
729         return value & (1 << (n - 1)) ? value | (-1 << n) : value;
730 }
731
732 /*
733  * Convert a signed 32-bit integer to a signed n-bit integer.
734  */
735
736 static u32 s32ton(__s32 value, unsigned n)
737 {
738         s32 a = value >> (n - 1);
739         if (a && a != -1)
740                 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
741         return value & ((1 << n) - 1);
742 }
743
744 /*
745  * Extract/implement a data field from/to a little endian report (bit array).
746  *
747  * Code sort-of follows HID spec:
748  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
749  *
750  * While the USB HID spec allows unlimited length bit fields in "report
751  * descriptors", most devices never use more than 16 bits.
752  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
753  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
754  */
755
756 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
757 {
758         u64 x;
759
760         WARN_ON(n > 32);
761
762         report += offset >> 3;  /* adjust byte index */
763         offset &= 7;            /* now only need bit offset into one byte */
764         x = le64_to_cpu(get_unaligned((__le64 *) report));
765         x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
766         return (u32) x;
767 }
768
769 /*
770  * "implement" : set bits in a little endian bit stream.
771  * Same concepts as "extract" (see comments above).
772  * The data mangled in the bit stream remains in little endian
773  * order the whole time. It make more sense to talk about
774  * endianness of register values by considering a register
775  * a "cached" copy of the little endiad bit stream.
776  */
777 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
778 {
779         __le64 x;
780         u64 m = (1ULL << n) - 1;
781
782         WARN_ON(n > 32);
783
784         WARN_ON(value > m);
785         value &= m;
786
787         report += offset >> 3;
788         offset &= 7;
789
790         x = get_unaligned((__le64 *)report);
791         x &= cpu_to_le64(~(m << offset));
792         x |= cpu_to_le64(((u64) value) << offset);
793         put_unaligned(x, (__le64 *) report);
794 }
795
796 /*
797  * Search an array for a value.
798  */
799
800 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
801 {
802         while (n--) {
803                 if (*array++ == value)
804                         return 0;
805         }
806         return -1;
807 }
808
809 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
810 {
811         hid_dump_input(usage, value);
812         if (hid->claimed & HID_CLAIMED_INPUT)
813                 hidinput_hid_event(hid, field, usage, value);
814         if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
815                 hid->hiddev_hid_event(hid, field, usage, value);
816 }
817
818 /*
819  * Analyse a received field, and fetch the data from it. The field
820  * content is stored for next report processing (we do differential
821  * reporting to the layer).
822  */
823
824 void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
825 {
826         unsigned n;
827         unsigned count = field->report_count;
828         unsigned offset = field->report_offset;
829         unsigned size = field->report_size;
830         __s32 min = field->logical_minimum;
831         __s32 max = field->logical_maximum;
832         __s32 *value;
833
834         if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
835                 return;
836
837         for (n = 0; n < count; n++) {
838
839                         value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
840                                                     extract(data, offset + n * size, size);
841
842                         if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
843                             && value[n] >= min && value[n] <= max
844                             && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
845                                 goto exit;
846         }
847
848         for (n = 0; n < count; n++) {
849
850                 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
851                         hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
852                         continue;
853                 }
854
855                 if (field->value[n] >= min && field->value[n] <= max
856                         && field->usage[field->value[n] - min].hid
857                         && search(value, field->value[n], count))
858                                 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
859
860                 if (value[n] >= min && value[n] <= max
861                         && field->usage[value[n] - min].hid
862                         && search(field->value, value[n], count))
863                                 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
864         }
865
866         memcpy(field->value, value, count * sizeof(__s32));
867 exit:
868         kfree(value);
869 }
870 EXPORT_SYMBOL_GPL(hid_input_field);
871
872 /*
873  * Output the field into the report.
874  */
875
876 static void hid_output_field(struct hid_field *field, __u8 *data)
877 {
878         unsigned count = field->report_count;
879         unsigned offset = field->report_offset;
880         unsigned size = field->report_size;
881         unsigned bitsused = offset + count * size;
882         unsigned n;
883
884         /* make sure the unused bits in the last byte are zeros */
885         if (count > 0 && size > 0 && (bitsused % 8) != 0)
886                 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
887
888         for (n = 0; n < count; n++) {
889                 if (field->logical_minimum < 0) /* signed values */
890                         implement(data, offset + n * size, size, s32ton(field->value[n], size));
891                 else                            /* unsigned values */
892                         implement(data, offset + n * size, size, field->value[n]);
893         }
894 }
895
896 /*
897  * Create a report.
898  */
899
900 void hid_output_report(struct hid_report *report, __u8 *data)
901 {
902         unsigned n;
903
904         if (report->id > 0)
905                 *data++ = report->id;
906
907         for (n = 0; n < report->maxfield; n++)
908                 hid_output_field(report->field[n], data);
909 }
910 EXPORT_SYMBOL_GPL(hid_output_report);
911
912 /*
913  * Set a field value. The report this field belongs to has to be
914  * created and transferred to the device, to set this value in the
915  * device.
916  */
917
918 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
919 {
920         unsigned size = field->report_size;
921
922         hid_dump_input(field->usage + offset, value);
923
924         if (offset >= field->report_count) {
925                 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
926                 hid_dump_field(field, 8);
927                 return -1;
928         }
929         if (field->logical_minimum < 0) {
930                 if (value != snto32(s32ton(value, size), size)) {
931                         dbg_hid("value %d is out of range\n", value);
932                         return -1;
933                 }
934         }
935         field->value[offset] = value;
936         return 0;
937 }
938 EXPORT_SYMBOL_GPL(hid_set_field);
939
940 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
941 {
942         struct hid_report_enum *report_enum = hid->report_enum + type;
943         struct hid_report *report;
944         int n, rsize, i;
945
946         if (!hid)
947                 return -ENODEV;
948
949         if (!size) {
950                 dbg_hid("empty report\n");
951                 return -1;
952         }
953
954         dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
955
956         n = 0;                          /* Normally report number is 0 */
957         if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
958                 n = *data++;
959                 size--;
960         }
961
962         /* dump the report descriptor */
963         dbg_hid("report %d (size %u) = ", n, size);
964         for (i = 0; i < size; i++)
965                 dbg_hid_line(" %02x", data[i]);
966         dbg_hid_line("\n");
967
968         if (!(report = report_enum->report_id_hash[n])) {
969                 dbg_hid("undefined report_id %d received\n", n);
970                 return -1;
971         }
972
973         rsize = ((report->size - 1) >> 3) + 1;
974
975         if (size < rsize) {
976                 dbg_hid("report %d is too short, (%d < %d)\n", report->id, size, rsize);
977                 memset(data + size, 0, rsize - size);
978         }
979
980         if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
981                 hid->hiddev_report_event(hid, report);
982
983         for (n = 0; n < report->maxfield; n++)
984                 hid_input_field(hid, report->field[n], data, interrupt);
985
986         if (hid->claimed & HID_CLAIMED_INPUT)
987                 hidinput_report_event(hid, report);
988
989         return 0;
990 }
991 EXPORT_SYMBOL_GPL(hid_input_report);
992
993 MODULE_LICENSE(DRIVER_LICENSE);
994