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