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