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