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