xref: /openbmc/linux/drivers/hid/hid-core.c (revision ba61bb17)
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-2012 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/mm.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 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/semaphore.h>
33 
34 #include <linux/hid.h>
35 #include <linux/hiddev.h>
36 #include <linux/hid-debug.h>
37 #include <linux/hidraw.h>
38 
39 #include "hid-ids.h"
40 
41 /*
42  * Version Information
43  */
44 
45 #define DRIVER_DESC "HID core driver"
46 
47 int hid_debug = 0;
48 module_param_named(debug, hid_debug, int, 0600);
49 MODULE_PARM_DESC(debug, "toggle HID debugging messages");
50 EXPORT_SYMBOL_GPL(hid_debug);
51 
52 static int hid_ignore_special_drivers = 0;
53 module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
54 MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
55 
56 /*
57  * Register a new report for a device.
58  */
59 
60 struct hid_report *hid_register_report(struct hid_device *device,
61 				       unsigned int type, unsigned int id,
62 				       unsigned int application)
63 {
64 	struct hid_report_enum *report_enum = device->report_enum + type;
65 	struct hid_report *report;
66 
67 	if (id >= HID_MAX_IDS)
68 		return NULL;
69 	if (report_enum->report_id_hash[id])
70 		return report_enum->report_id_hash[id];
71 
72 	report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
73 	if (!report)
74 		return NULL;
75 
76 	if (id != 0)
77 		report_enum->numbered = 1;
78 
79 	report->id = id;
80 	report->type = type;
81 	report->size = 0;
82 	report->device = device;
83 	report->application = application;
84 	report_enum->report_id_hash[id] = report;
85 
86 	list_add_tail(&report->list, &report_enum->report_list);
87 
88 	return report;
89 }
90 EXPORT_SYMBOL_GPL(hid_register_report);
91 
92 /*
93  * Register a new field for this report.
94  */
95 
96 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
97 {
98 	struct hid_field *field;
99 
100 	if (report->maxfield == HID_MAX_FIELDS) {
101 		hid_err(report->device, "too many fields in report\n");
102 		return NULL;
103 	}
104 
105 	field = kzalloc((sizeof(struct hid_field) +
106 			 usages * sizeof(struct hid_usage) +
107 			 values * sizeof(unsigned)), GFP_KERNEL);
108 	if (!field)
109 		return NULL;
110 
111 	field->index = report->maxfield++;
112 	report->field[field->index] = field;
113 	field->usage = (struct hid_usage *)(field + 1);
114 	field->value = (s32 *)(field->usage + usages);
115 	field->report = report;
116 
117 	return field;
118 }
119 
120 /*
121  * Open a collection. The type/usage is pushed on the stack.
122  */
123 
124 static int open_collection(struct hid_parser *parser, unsigned type)
125 {
126 	struct hid_collection *collection;
127 	unsigned usage;
128 
129 	usage = parser->local.usage[0];
130 
131 	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
132 		hid_err(parser->device, "collection stack overflow\n");
133 		return -EINVAL;
134 	}
135 
136 	if (parser->device->maxcollection == parser->device->collection_size) {
137 		collection = kmalloc(
138 				array3_size(sizeof(struct hid_collection),
139 					    parser->device->collection_size,
140 					    2),
141 				GFP_KERNEL);
142 		if (collection == NULL) {
143 			hid_err(parser->device, "failed to reallocate collection array\n");
144 			return -ENOMEM;
145 		}
146 		memcpy(collection, parser->device->collection,
147 			sizeof(struct hid_collection) *
148 			parser->device->collection_size);
149 		memset(collection + parser->device->collection_size, 0,
150 			sizeof(struct hid_collection) *
151 			parser->device->collection_size);
152 		kfree(parser->device->collection);
153 		parser->device->collection = collection;
154 		parser->device->collection_size *= 2;
155 	}
156 
157 	parser->collection_stack[parser->collection_stack_ptr++] =
158 		parser->device->maxcollection;
159 
160 	collection = parser->device->collection +
161 		parser->device->maxcollection++;
162 	collection->type = type;
163 	collection->usage = usage;
164 	collection->level = parser->collection_stack_ptr - 1;
165 
166 	if (type == HID_COLLECTION_APPLICATION)
167 		parser->device->maxapplication++;
168 
169 	return 0;
170 }
171 
172 /*
173  * Close a collection.
174  */
175 
176 static int close_collection(struct hid_parser *parser)
177 {
178 	if (!parser->collection_stack_ptr) {
179 		hid_err(parser->device, "collection stack underflow\n");
180 		return -EINVAL;
181 	}
182 	parser->collection_stack_ptr--;
183 	return 0;
184 }
185 
186 /*
187  * Climb up the stack, search for the specified collection type
188  * and return the usage.
189  */
190 
191 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
192 {
193 	struct hid_collection *collection = parser->device->collection;
194 	int n;
195 
196 	for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
197 		unsigned index = parser->collection_stack[n];
198 		if (collection[index].type == type)
199 			return collection[index].usage;
200 	}
201 	return 0; /* we know nothing about this usage type */
202 }
203 
204 /*
205  * Add a usage to the temporary parser table.
206  */
207 
208 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
209 {
210 	if (parser->local.usage_index >= HID_MAX_USAGES) {
211 		hid_err(parser->device, "usage index exceeded\n");
212 		return -1;
213 	}
214 	parser->local.usage[parser->local.usage_index] = usage;
215 	parser->local.collection_index[parser->local.usage_index] =
216 		parser->collection_stack_ptr ?
217 		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
218 	parser->local.usage_index++;
219 	return 0;
220 }
221 
222 /*
223  * Register a new field for this report.
224  */
225 
226 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
227 {
228 	struct hid_report *report;
229 	struct hid_field *field;
230 	unsigned int usages;
231 	unsigned int offset;
232 	unsigned int i;
233 	unsigned int application;
234 
235 	application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
236 
237 	report = hid_register_report(parser->device, report_type,
238 				     parser->global.report_id, application);
239 	if (!report) {
240 		hid_err(parser->device, "hid_register_report failed\n");
241 		return -1;
242 	}
243 
244 	/* Handle both signed and unsigned cases properly */
245 	if ((parser->global.logical_minimum < 0 &&
246 		parser->global.logical_maximum <
247 		parser->global.logical_minimum) ||
248 		(parser->global.logical_minimum >= 0 &&
249 		(__u32)parser->global.logical_maximum <
250 		(__u32)parser->global.logical_minimum)) {
251 		dbg_hid("logical range invalid 0x%x 0x%x\n",
252 			parser->global.logical_minimum,
253 			parser->global.logical_maximum);
254 		return -1;
255 	}
256 
257 	offset = report->size;
258 	report->size += parser->global.report_size * parser->global.report_count;
259 
260 	if (!parser->local.usage_index) /* Ignore padding fields */
261 		return 0;
262 
263 	usages = max_t(unsigned, parser->local.usage_index,
264 				 parser->global.report_count);
265 
266 	field = hid_register_field(report, usages, parser->global.report_count);
267 	if (!field)
268 		return 0;
269 
270 	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
271 	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
272 	field->application = application;
273 
274 	for (i = 0; i < usages; i++) {
275 		unsigned j = i;
276 		/* Duplicate the last usage we parsed if we have excess values */
277 		if (i >= parser->local.usage_index)
278 			j = parser->local.usage_index - 1;
279 		field->usage[i].hid = parser->local.usage[j];
280 		field->usage[i].collection_index =
281 			parser->local.collection_index[j];
282 		field->usage[i].usage_index = i;
283 	}
284 
285 	field->maxusage = usages;
286 	field->flags = flags;
287 	field->report_offset = offset;
288 	field->report_type = report_type;
289 	field->report_size = parser->global.report_size;
290 	field->report_count = parser->global.report_count;
291 	field->logical_minimum = parser->global.logical_minimum;
292 	field->logical_maximum = parser->global.logical_maximum;
293 	field->physical_minimum = parser->global.physical_minimum;
294 	field->physical_maximum = parser->global.physical_maximum;
295 	field->unit_exponent = parser->global.unit_exponent;
296 	field->unit = parser->global.unit;
297 
298 	return 0;
299 }
300 
301 /*
302  * Read data value from item.
303  */
304 
305 static u32 item_udata(struct hid_item *item)
306 {
307 	switch (item->size) {
308 	case 1: return item->data.u8;
309 	case 2: return item->data.u16;
310 	case 4: return item->data.u32;
311 	}
312 	return 0;
313 }
314 
315 static s32 item_sdata(struct hid_item *item)
316 {
317 	switch (item->size) {
318 	case 1: return item->data.s8;
319 	case 2: return item->data.s16;
320 	case 4: return item->data.s32;
321 	}
322 	return 0;
323 }
324 
325 /*
326  * Process a global item.
327  */
328 
329 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
330 {
331 	__s32 raw_value;
332 	switch (item->tag) {
333 	case HID_GLOBAL_ITEM_TAG_PUSH:
334 
335 		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
336 			hid_err(parser->device, "global environment stack overflow\n");
337 			return -1;
338 		}
339 
340 		memcpy(parser->global_stack + parser->global_stack_ptr++,
341 			&parser->global, sizeof(struct hid_global));
342 		return 0;
343 
344 	case HID_GLOBAL_ITEM_TAG_POP:
345 
346 		if (!parser->global_stack_ptr) {
347 			hid_err(parser->device, "global environment stack underflow\n");
348 			return -1;
349 		}
350 
351 		memcpy(&parser->global, parser->global_stack +
352 			--parser->global_stack_ptr, sizeof(struct hid_global));
353 		return 0;
354 
355 	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
356 		parser->global.usage_page = item_udata(item);
357 		return 0;
358 
359 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
360 		parser->global.logical_minimum = item_sdata(item);
361 		return 0;
362 
363 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
364 		if (parser->global.logical_minimum < 0)
365 			parser->global.logical_maximum = item_sdata(item);
366 		else
367 			parser->global.logical_maximum = item_udata(item);
368 		return 0;
369 
370 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
371 		parser->global.physical_minimum = item_sdata(item);
372 		return 0;
373 
374 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
375 		if (parser->global.physical_minimum < 0)
376 			parser->global.physical_maximum = item_sdata(item);
377 		else
378 			parser->global.physical_maximum = item_udata(item);
379 		return 0;
380 
381 	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
382 		/* Many devices provide unit exponent as a two's complement
383 		 * nibble due to the common misunderstanding of HID
384 		 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
385 		 * both this and the standard encoding. */
386 		raw_value = item_sdata(item);
387 		if (!(raw_value & 0xfffffff0))
388 			parser->global.unit_exponent = hid_snto32(raw_value, 4);
389 		else
390 			parser->global.unit_exponent = raw_value;
391 		return 0;
392 
393 	case HID_GLOBAL_ITEM_TAG_UNIT:
394 		parser->global.unit = item_udata(item);
395 		return 0;
396 
397 	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
398 		parser->global.report_size = item_udata(item);
399 		if (parser->global.report_size > 128) {
400 			hid_err(parser->device, "invalid report_size %d\n",
401 					parser->global.report_size);
402 			return -1;
403 		}
404 		return 0;
405 
406 	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
407 		parser->global.report_count = item_udata(item);
408 		if (parser->global.report_count > HID_MAX_USAGES) {
409 			hid_err(parser->device, "invalid report_count %d\n",
410 					parser->global.report_count);
411 			return -1;
412 		}
413 		return 0;
414 
415 	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
416 		parser->global.report_id = item_udata(item);
417 		if (parser->global.report_id == 0 ||
418 		    parser->global.report_id >= HID_MAX_IDS) {
419 			hid_err(parser->device, "report_id %u is invalid\n",
420 				parser->global.report_id);
421 			return -1;
422 		}
423 		return 0;
424 
425 	default:
426 		hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
427 		return -1;
428 	}
429 }
430 
431 /*
432  * Process a local item.
433  */
434 
435 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
436 {
437 	__u32 data;
438 	unsigned n;
439 	__u32 count;
440 
441 	data = item_udata(item);
442 
443 	switch (item->tag) {
444 	case HID_LOCAL_ITEM_TAG_DELIMITER:
445 
446 		if (data) {
447 			/*
448 			 * We treat items before the first delimiter
449 			 * as global to all usage sets (branch 0).
450 			 * In the moment we process only these global
451 			 * items and the first delimiter set.
452 			 */
453 			if (parser->local.delimiter_depth != 0) {
454 				hid_err(parser->device, "nested delimiters\n");
455 				return -1;
456 			}
457 			parser->local.delimiter_depth++;
458 			parser->local.delimiter_branch++;
459 		} else {
460 			if (parser->local.delimiter_depth < 1) {
461 				hid_err(parser->device, "bogus close delimiter\n");
462 				return -1;
463 			}
464 			parser->local.delimiter_depth--;
465 		}
466 		return 0;
467 
468 	case HID_LOCAL_ITEM_TAG_USAGE:
469 
470 		if (parser->local.delimiter_branch > 1) {
471 			dbg_hid("alternative usage ignored\n");
472 			return 0;
473 		}
474 
475 		if (item->size <= 2)
476 			data = (parser->global.usage_page << 16) + data;
477 
478 		return hid_add_usage(parser, data);
479 
480 	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
481 
482 		if (parser->local.delimiter_branch > 1) {
483 			dbg_hid("alternative usage ignored\n");
484 			return 0;
485 		}
486 
487 		if (item->size <= 2)
488 			data = (parser->global.usage_page << 16) + data;
489 
490 		parser->local.usage_minimum = data;
491 		return 0;
492 
493 	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
494 
495 		if (parser->local.delimiter_branch > 1) {
496 			dbg_hid("alternative usage ignored\n");
497 			return 0;
498 		}
499 
500 		if (item->size <= 2)
501 			data = (parser->global.usage_page << 16) + data;
502 
503 		count = data - parser->local.usage_minimum;
504 		if (count + parser->local.usage_index >= HID_MAX_USAGES) {
505 			/*
506 			 * We do not warn if the name is not set, we are
507 			 * actually pre-scanning the device.
508 			 */
509 			if (dev_name(&parser->device->dev))
510 				hid_warn(parser->device,
511 					 "ignoring exceeding usage max\n");
512 			data = HID_MAX_USAGES - parser->local.usage_index +
513 				parser->local.usage_minimum - 1;
514 			if (data <= 0) {
515 				hid_err(parser->device,
516 					"no more usage index available\n");
517 				return -1;
518 			}
519 		}
520 
521 		for (n = parser->local.usage_minimum; n <= data; n++)
522 			if (hid_add_usage(parser, n)) {
523 				dbg_hid("hid_add_usage failed\n");
524 				return -1;
525 			}
526 		return 0;
527 
528 	default:
529 
530 		dbg_hid("unknown local item tag 0x%x\n", item->tag);
531 		return 0;
532 	}
533 	return 0;
534 }
535 
536 /*
537  * Process a main item.
538  */
539 
540 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
541 {
542 	__u32 data;
543 	int ret;
544 
545 	data = item_udata(item);
546 
547 	switch (item->tag) {
548 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
549 		ret = open_collection(parser, data & 0xff);
550 		break;
551 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
552 		ret = close_collection(parser);
553 		break;
554 	case HID_MAIN_ITEM_TAG_INPUT:
555 		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
556 		break;
557 	case HID_MAIN_ITEM_TAG_OUTPUT:
558 		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
559 		break;
560 	case HID_MAIN_ITEM_TAG_FEATURE:
561 		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
562 		break;
563 	default:
564 		hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
565 		ret = 0;
566 	}
567 
568 	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
569 
570 	return ret;
571 }
572 
573 /*
574  * Process a reserved item.
575  */
576 
577 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
578 {
579 	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
580 	return 0;
581 }
582 
583 /*
584  * Free a report and all registered fields. The field->usage and
585  * field->value table's are allocated behind the field, so we need
586  * only to free(field) itself.
587  */
588 
589 static void hid_free_report(struct hid_report *report)
590 {
591 	unsigned n;
592 
593 	for (n = 0; n < report->maxfield; n++)
594 		kfree(report->field[n]);
595 	kfree(report);
596 }
597 
598 /*
599  * Close report. This function returns the device
600  * state to the point prior to hid_open_report().
601  */
602 static void hid_close_report(struct hid_device *device)
603 {
604 	unsigned i, j;
605 
606 	for (i = 0; i < HID_REPORT_TYPES; i++) {
607 		struct hid_report_enum *report_enum = device->report_enum + i;
608 
609 		for (j = 0; j < HID_MAX_IDS; j++) {
610 			struct hid_report *report = report_enum->report_id_hash[j];
611 			if (report)
612 				hid_free_report(report);
613 		}
614 		memset(report_enum, 0, sizeof(*report_enum));
615 		INIT_LIST_HEAD(&report_enum->report_list);
616 	}
617 
618 	kfree(device->rdesc);
619 	device->rdesc = NULL;
620 	device->rsize = 0;
621 
622 	kfree(device->collection);
623 	device->collection = NULL;
624 	device->collection_size = 0;
625 	device->maxcollection = 0;
626 	device->maxapplication = 0;
627 
628 	device->status &= ~HID_STAT_PARSED;
629 }
630 
631 /*
632  * Free a device structure, all reports, and all fields.
633  */
634 
635 static void hid_device_release(struct device *dev)
636 {
637 	struct hid_device *hid = to_hid_device(dev);
638 
639 	hid_close_report(hid);
640 	kfree(hid->dev_rdesc);
641 	kfree(hid);
642 }
643 
644 /*
645  * Fetch a report description item from the data stream. We support long
646  * items, though they are not used yet.
647  */
648 
649 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
650 {
651 	u8 b;
652 
653 	if ((end - start) <= 0)
654 		return NULL;
655 
656 	b = *start++;
657 
658 	item->type = (b >> 2) & 3;
659 	item->tag  = (b >> 4) & 15;
660 
661 	if (item->tag == HID_ITEM_TAG_LONG) {
662 
663 		item->format = HID_ITEM_FORMAT_LONG;
664 
665 		if ((end - start) < 2)
666 			return NULL;
667 
668 		item->size = *start++;
669 		item->tag  = *start++;
670 
671 		if ((end - start) < item->size)
672 			return NULL;
673 
674 		item->data.longdata = start;
675 		start += item->size;
676 		return start;
677 	}
678 
679 	item->format = HID_ITEM_FORMAT_SHORT;
680 	item->size = b & 3;
681 
682 	switch (item->size) {
683 	case 0:
684 		return start;
685 
686 	case 1:
687 		if ((end - start) < 1)
688 			return NULL;
689 		item->data.u8 = *start++;
690 		return start;
691 
692 	case 2:
693 		if ((end - start) < 2)
694 			return NULL;
695 		item->data.u16 = get_unaligned_le16(start);
696 		start = (__u8 *)((__le16 *)start + 1);
697 		return start;
698 
699 	case 3:
700 		item->size++;
701 		if ((end - start) < 4)
702 			return NULL;
703 		item->data.u32 = get_unaligned_le32(start);
704 		start = (__u8 *)((__le32 *)start + 1);
705 		return start;
706 	}
707 
708 	return NULL;
709 }
710 
711 static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
712 {
713 	struct hid_device *hid = parser->device;
714 
715 	if (usage == HID_DG_CONTACTID)
716 		hid->group = HID_GROUP_MULTITOUCH;
717 }
718 
719 static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
720 {
721 	if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
722 	    parser->global.report_size == 8)
723 		parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
724 }
725 
726 static void hid_scan_collection(struct hid_parser *parser, unsigned type)
727 {
728 	struct hid_device *hid = parser->device;
729 	int i;
730 
731 	if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
732 	    type == HID_COLLECTION_PHYSICAL)
733 		hid->group = HID_GROUP_SENSOR_HUB;
734 
735 	if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
736 	    hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
737 	    hid->group == HID_GROUP_MULTITOUCH)
738 		hid->group = HID_GROUP_GENERIC;
739 
740 	if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
741 		for (i = 0; i < parser->local.usage_index; i++)
742 			if (parser->local.usage[i] == HID_GD_POINTER)
743 				parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
744 
745 	if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
746 		parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
747 }
748 
749 static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
750 {
751 	__u32 data;
752 	int i;
753 
754 	data = item_udata(item);
755 
756 	switch (item->tag) {
757 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
758 		hid_scan_collection(parser, data & 0xff);
759 		break;
760 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
761 		break;
762 	case HID_MAIN_ITEM_TAG_INPUT:
763 		/* ignore constant inputs, they will be ignored by hid-input */
764 		if (data & HID_MAIN_ITEM_CONSTANT)
765 			break;
766 		for (i = 0; i < parser->local.usage_index; i++)
767 			hid_scan_input_usage(parser, parser->local.usage[i]);
768 		break;
769 	case HID_MAIN_ITEM_TAG_OUTPUT:
770 		break;
771 	case HID_MAIN_ITEM_TAG_FEATURE:
772 		for (i = 0; i < parser->local.usage_index; i++)
773 			hid_scan_feature_usage(parser, parser->local.usage[i]);
774 		break;
775 	}
776 
777 	/* Reset the local parser environment */
778 	memset(&parser->local, 0, sizeof(parser->local));
779 
780 	return 0;
781 }
782 
783 /*
784  * Scan a report descriptor before the device is added to the bus.
785  * Sets device groups and other properties that determine what driver
786  * to load.
787  */
788 static int hid_scan_report(struct hid_device *hid)
789 {
790 	struct hid_parser *parser;
791 	struct hid_item item;
792 	__u8 *start = hid->dev_rdesc;
793 	__u8 *end = start + hid->dev_rsize;
794 	static int (*dispatch_type[])(struct hid_parser *parser,
795 				      struct hid_item *item) = {
796 		hid_scan_main,
797 		hid_parser_global,
798 		hid_parser_local,
799 		hid_parser_reserved
800 	};
801 
802 	parser = vzalloc(sizeof(struct hid_parser));
803 	if (!parser)
804 		return -ENOMEM;
805 
806 	parser->device = hid;
807 	hid->group = HID_GROUP_GENERIC;
808 
809 	/*
810 	 * The parsing is simpler than the one in hid_open_report() as we should
811 	 * be robust against hid errors. Those errors will be raised by
812 	 * hid_open_report() anyway.
813 	 */
814 	while ((start = fetch_item(start, end, &item)) != NULL)
815 		dispatch_type[item.type](parser, &item);
816 
817 	/*
818 	 * Handle special flags set during scanning.
819 	 */
820 	if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
821 	    (hid->group == HID_GROUP_MULTITOUCH))
822 		hid->group = HID_GROUP_MULTITOUCH_WIN_8;
823 
824 	/*
825 	 * Vendor specific handlings
826 	 */
827 	switch (hid->vendor) {
828 	case USB_VENDOR_ID_WACOM:
829 		hid->group = HID_GROUP_WACOM;
830 		break;
831 	case USB_VENDOR_ID_SYNAPTICS:
832 		if (hid->group == HID_GROUP_GENERIC)
833 			if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
834 			    && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
835 				/*
836 				 * hid-rmi should take care of them,
837 				 * not hid-generic
838 				 */
839 				hid->group = HID_GROUP_RMI;
840 		break;
841 	}
842 
843 	vfree(parser);
844 	return 0;
845 }
846 
847 /**
848  * hid_parse_report - parse device report
849  *
850  * @device: hid device
851  * @start: report start
852  * @size: report size
853  *
854  * Allocate the device report as read by the bus driver. This function should
855  * only be called from parse() in ll drivers.
856  */
857 int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
858 {
859 	hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
860 	if (!hid->dev_rdesc)
861 		return -ENOMEM;
862 	hid->dev_rsize = size;
863 	return 0;
864 }
865 EXPORT_SYMBOL_GPL(hid_parse_report);
866 
867 static const char * const hid_report_names[] = {
868 	"HID_INPUT_REPORT",
869 	"HID_OUTPUT_REPORT",
870 	"HID_FEATURE_REPORT",
871 };
872 /**
873  * hid_validate_values - validate existing device report's value indexes
874  *
875  * @device: hid device
876  * @type: which report type to examine
877  * @id: which report ID to examine (0 for first)
878  * @field_index: which report field to examine
879  * @report_counts: expected number of values
880  *
881  * Validate the number of values in a given field of a given report, after
882  * parsing.
883  */
884 struct hid_report *hid_validate_values(struct hid_device *hid,
885 				       unsigned int type, unsigned int id,
886 				       unsigned int field_index,
887 				       unsigned int report_counts)
888 {
889 	struct hid_report *report;
890 
891 	if (type > HID_FEATURE_REPORT) {
892 		hid_err(hid, "invalid HID report type %u\n", type);
893 		return NULL;
894 	}
895 
896 	if (id >= HID_MAX_IDS) {
897 		hid_err(hid, "invalid HID report id %u\n", id);
898 		return NULL;
899 	}
900 
901 	/*
902 	 * Explicitly not using hid_get_report() here since it depends on
903 	 * ->numbered being checked, which may not always be the case when
904 	 * drivers go to access report values.
905 	 */
906 	if (id == 0) {
907 		/*
908 		 * Validating on id 0 means we should examine the first
909 		 * report in the list.
910 		 */
911 		report = list_entry(
912 				hid->report_enum[type].report_list.next,
913 				struct hid_report, list);
914 	} else {
915 		report = hid->report_enum[type].report_id_hash[id];
916 	}
917 	if (!report) {
918 		hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
919 		return NULL;
920 	}
921 	if (report->maxfield <= field_index) {
922 		hid_err(hid, "not enough fields in %s %u\n",
923 			hid_report_names[type], id);
924 		return NULL;
925 	}
926 	if (report->field[field_index]->report_count < report_counts) {
927 		hid_err(hid, "not enough values in %s %u field %u\n",
928 			hid_report_names[type], id, field_index);
929 		return NULL;
930 	}
931 	return report;
932 }
933 EXPORT_SYMBOL_GPL(hid_validate_values);
934 
935 /**
936  * hid_open_report - open a driver-specific device report
937  *
938  * @device: hid device
939  *
940  * Parse a report description into a hid_device structure. Reports are
941  * enumerated, fields are attached to these reports.
942  * 0 returned on success, otherwise nonzero error value.
943  *
944  * This function (or the equivalent hid_parse() macro) should only be
945  * called from probe() in drivers, before starting the device.
946  */
947 int hid_open_report(struct hid_device *device)
948 {
949 	struct hid_parser *parser;
950 	struct hid_item item;
951 	unsigned int size;
952 	__u8 *start;
953 	__u8 *buf;
954 	__u8 *end;
955 	int ret;
956 	static int (*dispatch_type[])(struct hid_parser *parser,
957 				      struct hid_item *item) = {
958 		hid_parser_main,
959 		hid_parser_global,
960 		hid_parser_local,
961 		hid_parser_reserved
962 	};
963 
964 	if (WARN_ON(device->status & HID_STAT_PARSED))
965 		return -EBUSY;
966 
967 	start = device->dev_rdesc;
968 	if (WARN_ON(!start))
969 		return -ENODEV;
970 	size = device->dev_rsize;
971 
972 	buf = kmemdup(start, size, GFP_KERNEL);
973 	if (buf == NULL)
974 		return -ENOMEM;
975 
976 	if (device->driver->report_fixup)
977 		start = device->driver->report_fixup(device, buf, &size);
978 	else
979 		start = buf;
980 
981 	start = kmemdup(start, size, GFP_KERNEL);
982 	kfree(buf);
983 	if (start == NULL)
984 		return -ENOMEM;
985 
986 	device->rdesc = start;
987 	device->rsize = size;
988 
989 	parser = vzalloc(sizeof(struct hid_parser));
990 	if (!parser) {
991 		ret = -ENOMEM;
992 		goto err;
993 	}
994 
995 	parser->device = device;
996 
997 	end = start + size;
998 
999 	device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1000 				     sizeof(struct hid_collection), GFP_KERNEL);
1001 	if (!device->collection) {
1002 		ret = -ENOMEM;
1003 		goto err;
1004 	}
1005 	device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1006 
1007 	ret = -EINVAL;
1008 	while ((start = fetch_item(start, end, &item)) != NULL) {
1009 
1010 		if (item.format != HID_ITEM_FORMAT_SHORT) {
1011 			hid_err(device, "unexpected long global item\n");
1012 			goto err;
1013 		}
1014 
1015 		if (dispatch_type[item.type](parser, &item)) {
1016 			hid_err(device, "item %u %u %u %u parsing failed\n",
1017 				item.format, (unsigned)item.size,
1018 				(unsigned)item.type, (unsigned)item.tag);
1019 			goto err;
1020 		}
1021 
1022 		if (start == end) {
1023 			if (parser->collection_stack_ptr) {
1024 				hid_err(device, "unbalanced collection at end of report description\n");
1025 				goto err;
1026 			}
1027 			if (parser->local.delimiter_depth) {
1028 				hid_err(device, "unbalanced delimiter at end of report description\n");
1029 				goto err;
1030 			}
1031 			vfree(parser);
1032 			device->status |= HID_STAT_PARSED;
1033 			return 0;
1034 		}
1035 	}
1036 
1037 	hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
1038 err:
1039 	vfree(parser);
1040 	hid_close_report(device);
1041 	return ret;
1042 }
1043 EXPORT_SYMBOL_GPL(hid_open_report);
1044 
1045 /*
1046  * Convert a signed n-bit integer to signed 32-bit integer. Common
1047  * cases are done through the compiler, the screwed things has to be
1048  * done by hand.
1049  */
1050 
1051 static s32 snto32(__u32 value, unsigned n)
1052 {
1053 	switch (n) {
1054 	case 8:  return ((__s8)value);
1055 	case 16: return ((__s16)value);
1056 	case 32: return ((__s32)value);
1057 	}
1058 	return value & (1 << (n - 1)) ? value | (~0U << n) : value;
1059 }
1060 
1061 s32 hid_snto32(__u32 value, unsigned n)
1062 {
1063 	return snto32(value, n);
1064 }
1065 EXPORT_SYMBOL_GPL(hid_snto32);
1066 
1067 /*
1068  * Convert a signed 32-bit integer to a signed n-bit integer.
1069  */
1070 
1071 static u32 s32ton(__s32 value, unsigned n)
1072 {
1073 	s32 a = value >> (n - 1);
1074 	if (a && a != -1)
1075 		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1076 	return value & ((1 << n) - 1);
1077 }
1078 
1079 /*
1080  * Extract/implement a data field from/to a little endian report (bit array).
1081  *
1082  * Code sort-of follows HID spec:
1083  *     http://www.usb.org/developers/hidpage/HID1_11.pdf
1084  *
1085  * While the USB HID spec allows unlimited length bit fields in "report
1086  * descriptors", most devices never use more than 16 bits.
1087  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1088  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1089  */
1090 
1091 static u32 __extract(u8 *report, unsigned offset, int n)
1092 {
1093 	unsigned int idx = offset / 8;
1094 	unsigned int bit_nr = 0;
1095 	unsigned int bit_shift = offset % 8;
1096 	int bits_to_copy = 8 - bit_shift;
1097 	u32 value = 0;
1098 	u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
1099 
1100 	while (n > 0) {
1101 		value |= ((u32)report[idx] >> bit_shift) << bit_nr;
1102 		n -= bits_to_copy;
1103 		bit_nr += bits_to_copy;
1104 		bits_to_copy = 8;
1105 		bit_shift = 0;
1106 		idx++;
1107 	}
1108 
1109 	return value & mask;
1110 }
1111 
1112 u32 hid_field_extract(const struct hid_device *hid, u8 *report,
1113 			unsigned offset, unsigned n)
1114 {
1115 	if (n > 32) {
1116 		hid_warn(hid, "hid_field_extract() called with n (%d) > 32! (%s)\n",
1117 			 n, current->comm);
1118 		n = 32;
1119 	}
1120 
1121 	return __extract(report, offset, n);
1122 }
1123 EXPORT_SYMBOL_GPL(hid_field_extract);
1124 
1125 /*
1126  * "implement" : set bits in a little endian bit stream.
1127  * Same concepts as "extract" (see comments above).
1128  * The data mangled in the bit stream remains in little endian
1129  * order the whole time. It make more sense to talk about
1130  * endianness of register values by considering a register
1131  * a "cached" copy of the little endian bit stream.
1132  */
1133 
1134 static void __implement(u8 *report, unsigned offset, int n, u32 value)
1135 {
1136 	unsigned int idx = offset / 8;
1137 	unsigned int bit_shift = offset % 8;
1138 	int bits_to_set = 8 - bit_shift;
1139 
1140 	while (n - bits_to_set >= 0) {
1141 		report[idx] &= ~(0xff << bit_shift);
1142 		report[idx] |= value << bit_shift;
1143 		value >>= bits_to_set;
1144 		n -= bits_to_set;
1145 		bits_to_set = 8;
1146 		bit_shift = 0;
1147 		idx++;
1148 	}
1149 
1150 	/* last nibble */
1151 	if (n) {
1152 		u8 bit_mask = ((1U << n) - 1);
1153 		report[idx] &= ~(bit_mask << bit_shift);
1154 		report[idx] |= value << bit_shift;
1155 	}
1156 }
1157 
1158 static void implement(const struct hid_device *hid, u8 *report,
1159 		      unsigned offset, unsigned n, u32 value)
1160 {
1161 	if (unlikely(n > 32)) {
1162 		hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
1163 			 __func__, n, current->comm);
1164 		n = 32;
1165 	} else if (n < 32) {
1166 		u32 m = (1U << n) - 1;
1167 
1168 		if (unlikely(value > m)) {
1169 			hid_warn(hid,
1170 				 "%s() called with too large value %d (n: %d)! (%s)\n",
1171 				 __func__, value, n, current->comm);
1172 			WARN_ON(1);
1173 			value &= m;
1174 		}
1175 	}
1176 
1177 	__implement(report, offset, n, value);
1178 }
1179 
1180 /*
1181  * Search an array for a value.
1182  */
1183 
1184 static int search(__s32 *array, __s32 value, unsigned n)
1185 {
1186 	while (n--) {
1187 		if (*array++ == value)
1188 			return 0;
1189 	}
1190 	return -1;
1191 }
1192 
1193 /**
1194  * hid_match_report - check if driver's raw_event should be called
1195  *
1196  * @hid: hid device
1197  * @report_type: type to match against
1198  *
1199  * compare hid->driver->report_table->report_type to report->type
1200  */
1201 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1202 {
1203 	const struct hid_report_id *id = hid->driver->report_table;
1204 
1205 	if (!id) /* NULL means all */
1206 		return 1;
1207 
1208 	for (; id->report_type != HID_TERMINATOR; id++)
1209 		if (id->report_type == HID_ANY_ID ||
1210 				id->report_type == report->type)
1211 			return 1;
1212 	return 0;
1213 }
1214 
1215 /**
1216  * hid_match_usage - check if driver's event should be called
1217  *
1218  * @hid: hid device
1219  * @usage: usage to match against
1220  *
1221  * compare hid->driver->usage_table->usage_{type,code} to
1222  * usage->usage_{type,code}
1223  */
1224 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
1225 {
1226 	const struct hid_usage_id *id = hid->driver->usage_table;
1227 
1228 	if (!id) /* NULL means all */
1229 		return 1;
1230 
1231 	for (; id->usage_type != HID_ANY_ID - 1; id++)
1232 		if ((id->usage_hid == HID_ANY_ID ||
1233 				id->usage_hid == usage->hid) &&
1234 				(id->usage_type == HID_ANY_ID ||
1235 				id->usage_type == usage->type) &&
1236 				(id->usage_code == HID_ANY_ID ||
1237 				 id->usage_code == usage->code))
1238 			return 1;
1239 	return 0;
1240 }
1241 
1242 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
1243 		struct hid_usage *usage, __s32 value, int interrupt)
1244 {
1245 	struct hid_driver *hdrv = hid->driver;
1246 	int ret;
1247 
1248 	if (!list_empty(&hid->debug_list))
1249 		hid_dump_input(hid, usage, value);
1250 
1251 	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
1252 		ret = hdrv->event(hid, field, usage, value);
1253 		if (ret != 0) {
1254 			if (ret < 0)
1255 				hid_err(hid, "%s's event failed with %d\n",
1256 						hdrv->name, ret);
1257 			return;
1258 		}
1259 	}
1260 
1261 	if (hid->claimed & HID_CLAIMED_INPUT)
1262 		hidinput_hid_event(hid, field, usage, value);
1263 	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1264 		hid->hiddev_hid_event(hid, field, usage, value);
1265 }
1266 
1267 /*
1268  * Analyse a received field, and fetch the data from it. The field
1269  * content is stored for next report processing (we do differential
1270  * reporting to the layer).
1271  */
1272 
1273 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
1274 			    __u8 *data, int interrupt)
1275 {
1276 	unsigned n;
1277 	unsigned count = field->report_count;
1278 	unsigned offset = field->report_offset;
1279 	unsigned size = field->report_size;
1280 	__s32 min = field->logical_minimum;
1281 	__s32 max = field->logical_maximum;
1282 	__s32 *value;
1283 
1284 	value = kmalloc_array(count, sizeof(__s32), GFP_ATOMIC);
1285 	if (!value)
1286 		return;
1287 
1288 	for (n = 0; n < count; n++) {
1289 
1290 		value[n] = min < 0 ?
1291 			snto32(hid_field_extract(hid, data, offset + n * size,
1292 			       size), size) :
1293 			hid_field_extract(hid, data, offset + n * size, size);
1294 
1295 		/* Ignore report if ErrorRollOver */
1296 		if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
1297 		    value[n] >= min && value[n] <= max &&
1298 		    value[n] - min < field->maxusage &&
1299 		    field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1300 			goto exit;
1301 	}
1302 
1303 	for (n = 0; n < count; n++) {
1304 
1305 		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1306 			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1307 			continue;
1308 		}
1309 
1310 		if (field->value[n] >= min && field->value[n] <= max
1311 			&& field->value[n] - min < field->maxusage
1312 			&& field->usage[field->value[n] - min].hid
1313 			&& search(value, field->value[n], count))
1314 				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1315 
1316 		if (value[n] >= min && value[n] <= max
1317 			&& value[n] - min < field->maxusage
1318 			&& field->usage[value[n] - min].hid
1319 			&& search(field->value, value[n], count))
1320 				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1321 	}
1322 
1323 	memcpy(field->value, value, count * sizeof(__s32));
1324 exit:
1325 	kfree(value);
1326 }
1327 
1328 /*
1329  * Output the field into the report.
1330  */
1331 
1332 static void hid_output_field(const struct hid_device *hid,
1333 			     struct hid_field *field, __u8 *data)
1334 {
1335 	unsigned count = field->report_count;
1336 	unsigned offset = field->report_offset;
1337 	unsigned size = field->report_size;
1338 	unsigned n;
1339 
1340 	for (n = 0; n < count; n++) {
1341 		if (field->logical_minimum < 0)	/* signed values */
1342 			implement(hid, data, offset + n * size, size,
1343 				  s32ton(field->value[n], size));
1344 		else				/* unsigned values */
1345 			implement(hid, data, offset + n * size, size,
1346 				  field->value[n]);
1347 	}
1348 }
1349 
1350 /*
1351  * Create a report. 'data' has to be allocated using
1352  * hid_alloc_report_buf() so that it has proper size.
1353  */
1354 
1355 void hid_output_report(struct hid_report *report, __u8 *data)
1356 {
1357 	unsigned n;
1358 
1359 	if (report->id > 0)
1360 		*data++ = report->id;
1361 
1362 	memset(data, 0, ((report->size - 1) >> 3) + 1);
1363 	for (n = 0; n < report->maxfield; n++)
1364 		hid_output_field(report->device, report->field[n], data);
1365 }
1366 EXPORT_SYMBOL_GPL(hid_output_report);
1367 
1368 /*
1369  * Allocator for buffer that is going to be passed to hid_output_report()
1370  */
1371 u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
1372 {
1373 	/*
1374 	 * 7 extra bytes are necessary to achieve proper functionality
1375 	 * of implement() working on 8 byte chunks
1376 	 */
1377 
1378 	u32 len = hid_report_len(report) + 7;
1379 
1380 	return kmalloc(len, flags);
1381 }
1382 EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
1383 
1384 /*
1385  * Set a field value. The report this field belongs to has to be
1386  * created and transferred to the device, to set this value in the
1387  * device.
1388  */
1389 
1390 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1391 {
1392 	unsigned size;
1393 
1394 	if (!field)
1395 		return -1;
1396 
1397 	size = field->report_size;
1398 
1399 	hid_dump_input(field->report->device, field->usage + offset, value);
1400 
1401 	if (offset >= field->report_count) {
1402 		hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
1403 				offset, field->report_count);
1404 		return -1;
1405 	}
1406 	if (field->logical_minimum < 0) {
1407 		if (value != snto32(s32ton(value, size), size)) {
1408 			hid_err(field->report->device, "value %d is out of range\n", value);
1409 			return -1;
1410 		}
1411 	}
1412 	field->value[offset] = value;
1413 	return 0;
1414 }
1415 EXPORT_SYMBOL_GPL(hid_set_field);
1416 
1417 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1418 		const u8 *data)
1419 {
1420 	struct hid_report *report;
1421 	unsigned int n = 0;	/* Normally report number is 0 */
1422 
1423 	/* Device uses numbered reports, data[0] is report number */
1424 	if (report_enum->numbered)
1425 		n = *data;
1426 
1427 	report = report_enum->report_id_hash[n];
1428 	if (report == NULL)
1429 		dbg_hid("undefined report_id %u received\n", n);
1430 
1431 	return report;
1432 }
1433 
1434 /*
1435  * Implement a generic .request() callback, using .raw_request()
1436  * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
1437  */
1438 void __hid_request(struct hid_device *hid, struct hid_report *report,
1439 		int reqtype)
1440 {
1441 	char *buf;
1442 	int ret;
1443 	u32 len;
1444 
1445 	buf = hid_alloc_report_buf(report, GFP_KERNEL);
1446 	if (!buf)
1447 		return;
1448 
1449 	len = hid_report_len(report);
1450 
1451 	if (reqtype == HID_REQ_SET_REPORT)
1452 		hid_output_report(report, buf);
1453 
1454 	ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
1455 					  report->type, reqtype);
1456 	if (ret < 0) {
1457 		dbg_hid("unable to complete request: %d\n", ret);
1458 		goto out;
1459 	}
1460 
1461 	if (reqtype == HID_REQ_GET_REPORT)
1462 		hid_input_report(hid, report->type, buf, ret, 0);
1463 
1464 out:
1465 	kfree(buf);
1466 }
1467 EXPORT_SYMBOL_GPL(__hid_request);
1468 
1469 int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
1470 		int interrupt)
1471 {
1472 	struct hid_report_enum *report_enum = hid->report_enum + type;
1473 	struct hid_report *report;
1474 	struct hid_driver *hdrv;
1475 	unsigned int a;
1476 	u32 rsize, csize = size;
1477 	u8 *cdata = data;
1478 	int ret = 0;
1479 
1480 	report = hid_get_report(report_enum, data);
1481 	if (!report)
1482 		goto out;
1483 
1484 	if (report_enum->numbered) {
1485 		cdata++;
1486 		csize--;
1487 	}
1488 
1489 	rsize = ((report->size - 1) >> 3) + 1;
1490 
1491 	if (rsize > HID_MAX_BUFFER_SIZE)
1492 		rsize = HID_MAX_BUFFER_SIZE;
1493 
1494 	if (csize < rsize) {
1495 		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1496 				csize, rsize);
1497 		memset(cdata + csize, 0, rsize - csize);
1498 	}
1499 
1500 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1501 		hid->hiddev_report_event(hid, report);
1502 	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1503 		ret = hidraw_report_event(hid, data, size);
1504 		if (ret)
1505 			goto out;
1506 	}
1507 
1508 	if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
1509 		for (a = 0; a < report->maxfield; a++)
1510 			hid_input_field(hid, report->field[a], cdata, interrupt);
1511 		hdrv = hid->driver;
1512 		if (hdrv && hdrv->report)
1513 			hdrv->report(hid, report);
1514 	}
1515 
1516 	if (hid->claimed & HID_CLAIMED_INPUT)
1517 		hidinput_report_event(hid, report);
1518 out:
1519 	return ret;
1520 }
1521 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1522 
1523 /**
1524  * hid_input_report - report data from lower layer (usb, bt...)
1525  *
1526  * @hid: hid device
1527  * @type: HID report type (HID_*_REPORT)
1528  * @data: report contents
1529  * @size: size of data parameter
1530  * @interrupt: distinguish between interrupt and control transfers
1531  *
1532  * This is data entry for lower layers.
1533  */
1534 int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt)
1535 {
1536 	struct hid_report_enum *report_enum;
1537 	struct hid_driver *hdrv;
1538 	struct hid_report *report;
1539 	int ret = 0;
1540 
1541 	if (!hid)
1542 		return -ENODEV;
1543 
1544 	if (down_trylock(&hid->driver_input_lock))
1545 		return -EBUSY;
1546 
1547 	if (!hid->driver) {
1548 		ret = -ENODEV;
1549 		goto unlock;
1550 	}
1551 	report_enum = hid->report_enum + type;
1552 	hdrv = hid->driver;
1553 
1554 	if (!size) {
1555 		dbg_hid("empty report\n");
1556 		ret = -1;
1557 		goto unlock;
1558 	}
1559 
1560 	/* Avoid unnecessary overhead if debugfs is disabled */
1561 	if (!list_empty(&hid->debug_list))
1562 		hid_dump_report(hid, type, data, size);
1563 
1564 	report = hid_get_report(report_enum, data);
1565 
1566 	if (!report) {
1567 		ret = -1;
1568 		goto unlock;
1569 	}
1570 
1571 	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1572 		ret = hdrv->raw_event(hid, report, data, size);
1573 		if (ret < 0)
1574 			goto unlock;
1575 	}
1576 
1577 	ret = hid_report_raw_event(hid, type, data, size, interrupt);
1578 
1579 unlock:
1580 	up(&hid->driver_input_lock);
1581 	return ret;
1582 }
1583 EXPORT_SYMBOL_GPL(hid_input_report);
1584 
1585 bool hid_match_one_id(const struct hid_device *hdev,
1586 		      const struct hid_device_id *id)
1587 {
1588 	return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
1589 		(id->group == HID_GROUP_ANY || id->group == hdev->group) &&
1590 		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1591 		(id->product == HID_ANY_ID || id->product == hdev->product);
1592 }
1593 
1594 const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
1595 		const struct hid_device_id *id)
1596 {
1597 	for (; id->bus; id++)
1598 		if (hid_match_one_id(hdev, id))
1599 			return id;
1600 
1601 	return NULL;
1602 }
1603 
1604 static const struct hid_device_id hid_hiddev_list[] = {
1605 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1606 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1607 	{ }
1608 };
1609 
1610 static bool hid_hiddev(struct hid_device *hdev)
1611 {
1612 	return !!hid_match_id(hdev, hid_hiddev_list);
1613 }
1614 
1615 
1616 static ssize_t
1617 read_report_descriptor(struct file *filp, struct kobject *kobj,
1618 		struct bin_attribute *attr,
1619 		char *buf, loff_t off, size_t count)
1620 {
1621 	struct device *dev = kobj_to_dev(kobj);
1622 	struct hid_device *hdev = to_hid_device(dev);
1623 
1624 	if (off >= hdev->rsize)
1625 		return 0;
1626 
1627 	if (off + count > hdev->rsize)
1628 		count = hdev->rsize - off;
1629 
1630 	memcpy(buf, hdev->rdesc + off, count);
1631 
1632 	return count;
1633 }
1634 
1635 static ssize_t
1636 show_country(struct device *dev, struct device_attribute *attr,
1637 		char *buf)
1638 {
1639 	struct hid_device *hdev = to_hid_device(dev);
1640 
1641 	return sprintf(buf, "%02x\n", hdev->country & 0xff);
1642 }
1643 
1644 static struct bin_attribute dev_bin_attr_report_desc = {
1645 	.attr = { .name = "report_descriptor", .mode = 0444 },
1646 	.read = read_report_descriptor,
1647 	.size = HID_MAX_DESCRIPTOR_SIZE,
1648 };
1649 
1650 static const struct device_attribute dev_attr_country = {
1651 	.attr = { .name = "country", .mode = 0444 },
1652 	.show = show_country,
1653 };
1654 
1655 int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1656 {
1657 	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1658 		"Joystick", "Gamepad", "Keyboard", "Keypad",
1659 		"Multi-Axis Controller"
1660 	};
1661 	const char *type, *bus;
1662 	char buf[64] = "";
1663 	unsigned int i;
1664 	int len;
1665 	int ret;
1666 
1667 	if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1668 		connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
1669 	if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
1670 		connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
1671 	if (hdev->bus != BUS_USB)
1672 		connect_mask &= ~HID_CONNECT_HIDDEV;
1673 	if (hid_hiddev(hdev))
1674 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1675 
1676 	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1677 				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1678 		hdev->claimed |= HID_CLAIMED_INPUT;
1679 
1680 	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1681 			!hdev->hiddev_connect(hdev,
1682 				connect_mask & HID_CONNECT_HIDDEV_FORCE))
1683 		hdev->claimed |= HID_CLAIMED_HIDDEV;
1684 	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1685 		hdev->claimed |= HID_CLAIMED_HIDRAW;
1686 
1687 	if (connect_mask & HID_CONNECT_DRIVER)
1688 		hdev->claimed |= HID_CLAIMED_DRIVER;
1689 
1690 	/* Drivers with the ->raw_event callback set are not required to connect
1691 	 * to any other listener. */
1692 	if (!hdev->claimed && !hdev->driver->raw_event) {
1693 		hid_err(hdev, "device has no listeners, quitting\n");
1694 		return -ENODEV;
1695 	}
1696 
1697 	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1698 			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1699 		hdev->ff_init(hdev);
1700 
1701 	len = 0;
1702 	if (hdev->claimed & HID_CLAIMED_INPUT)
1703 		len += sprintf(buf + len, "input");
1704 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
1705 		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1706 				((struct hiddev *)hdev->hiddev)->minor);
1707 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
1708 		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1709 				((struct hidraw *)hdev->hidraw)->minor);
1710 
1711 	type = "Device";
1712 	for (i = 0; i < hdev->maxcollection; i++) {
1713 		struct hid_collection *col = &hdev->collection[i];
1714 		if (col->type == HID_COLLECTION_APPLICATION &&
1715 		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1716 		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1717 			type = types[col->usage & 0xffff];
1718 			break;
1719 		}
1720 	}
1721 
1722 	switch (hdev->bus) {
1723 	case BUS_USB:
1724 		bus = "USB";
1725 		break;
1726 	case BUS_BLUETOOTH:
1727 		bus = "BLUETOOTH";
1728 		break;
1729 	case BUS_I2C:
1730 		bus = "I2C";
1731 		break;
1732 	default:
1733 		bus = "<UNKNOWN>";
1734 	}
1735 
1736 	ret = device_create_file(&hdev->dev, &dev_attr_country);
1737 	if (ret)
1738 		hid_warn(hdev,
1739 			 "can't create sysfs country code attribute err: %d\n", ret);
1740 
1741 	hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1742 		 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1743 		 type, hdev->name, hdev->phys);
1744 
1745 	return 0;
1746 }
1747 EXPORT_SYMBOL_GPL(hid_connect);
1748 
1749 void hid_disconnect(struct hid_device *hdev)
1750 {
1751 	device_remove_file(&hdev->dev, &dev_attr_country);
1752 	if (hdev->claimed & HID_CLAIMED_INPUT)
1753 		hidinput_disconnect(hdev);
1754 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
1755 		hdev->hiddev_disconnect(hdev);
1756 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
1757 		hidraw_disconnect(hdev);
1758 	hdev->claimed = 0;
1759 }
1760 EXPORT_SYMBOL_GPL(hid_disconnect);
1761 
1762 /**
1763  * hid_hw_start - start underlying HW
1764  * @hdev: hid device
1765  * @connect_mask: which outputs to connect, see HID_CONNECT_*
1766  *
1767  * Call this in probe function *after* hid_parse. This will setup HW
1768  * buffers and start the device (if not defeirred to device open).
1769  * hid_hw_stop must be called if this was successful.
1770  */
1771 int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1772 {
1773 	int error;
1774 
1775 	error = hdev->ll_driver->start(hdev);
1776 	if (error)
1777 		return error;
1778 
1779 	if (connect_mask) {
1780 		error = hid_connect(hdev, connect_mask);
1781 		if (error) {
1782 			hdev->ll_driver->stop(hdev);
1783 			return error;
1784 		}
1785 	}
1786 
1787 	return 0;
1788 }
1789 EXPORT_SYMBOL_GPL(hid_hw_start);
1790 
1791 /**
1792  * hid_hw_stop - stop underlying HW
1793  * @hdev: hid device
1794  *
1795  * This is usually called from remove function or from probe when something
1796  * failed and hid_hw_start was called already.
1797  */
1798 void hid_hw_stop(struct hid_device *hdev)
1799 {
1800 	hid_disconnect(hdev);
1801 	hdev->ll_driver->stop(hdev);
1802 }
1803 EXPORT_SYMBOL_GPL(hid_hw_stop);
1804 
1805 /**
1806  * hid_hw_open - signal underlying HW to start delivering events
1807  * @hdev: hid device
1808  *
1809  * Tell underlying HW to start delivering events from the device.
1810  * This function should be called sometime after successful call
1811  * to hid_hw_start().
1812  */
1813 int hid_hw_open(struct hid_device *hdev)
1814 {
1815 	int ret;
1816 
1817 	ret = mutex_lock_killable(&hdev->ll_open_lock);
1818 	if (ret)
1819 		return ret;
1820 
1821 	if (!hdev->ll_open_count++) {
1822 		ret = hdev->ll_driver->open(hdev);
1823 		if (ret)
1824 			hdev->ll_open_count--;
1825 	}
1826 
1827 	mutex_unlock(&hdev->ll_open_lock);
1828 	return ret;
1829 }
1830 EXPORT_SYMBOL_GPL(hid_hw_open);
1831 
1832 /**
1833  * hid_hw_close - signal underlaying HW to stop delivering events
1834  *
1835  * @hdev: hid device
1836  *
1837  * This function indicates that we are not interested in the events
1838  * from this device anymore. Delivery of events may or may not stop,
1839  * depending on the number of users still outstanding.
1840  */
1841 void hid_hw_close(struct hid_device *hdev)
1842 {
1843 	mutex_lock(&hdev->ll_open_lock);
1844 	if (!--hdev->ll_open_count)
1845 		hdev->ll_driver->close(hdev);
1846 	mutex_unlock(&hdev->ll_open_lock);
1847 }
1848 EXPORT_SYMBOL_GPL(hid_hw_close);
1849 
1850 struct hid_dynid {
1851 	struct list_head list;
1852 	struct hid_device_id id;
1853 };
1854 
1855 /**
1856  * store_new_id - add a new HID device ID to this driver and re-probe devices
1857  * @driver: target device driver
1858  * @buf: buffer for scanning device ID data
1859  * @count: input size
1860  *
1861  * Adds a new dynamic hid device ID to this driver,
1862  * and causes the driver to probe for all devices again.
1863  */
1864 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
1865 		size_t count)
1866 {
1867 	struct hid_driver *hdrv = to_hid_driver(drv);
1868 	struct hid_dynid *dynid;
1869 	__u32 bus, vendor, product;
1870 	unsigned long driver_data = 0;
1871 	int ret;
1872 
1873 	ret = sscanf(buf, "%x %x %x %lx",
1874 			&bus, &vendor, &product, &driver_data);
1875 	if (ret < 3)
1876 		return -EINVAL;
1877 
1878 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1879 	if (!dynid)
1880 		return -ENOMEM;
1881 
1882 	dynid->id.bus = bus;
1883 	dynid->id.group = HID_GROUP_ANY;
1884 	dynid->id.vendor = vendor;
1885 	dynid->id.product = product;
1886 	dynid->id.driver_data = driver_data;
1887 
1888 	spin_lock(&hdrv->dyn_lock);
1889 	list_add_tail(&dynid->list, &hdrv->dyn_list);
1890 	spin_unlock(&hdrv->dyn_lock);
1891 
1892 	ret = driver_attach(&hdrv->driver);
1893 
1894 	return ret ? : count;
1895 }
1896 static DRIVER_ATTR_WO(new_id);
1897 
1898 static struct attribute *hid_drv_attrs[] = {
1899 	&driver_attr_new_id.attr,
1900 	NULL,
1901 };
1902 ATTRIBUTE_GROUPS(hid_drv);
1903 
1904 static void hid_free_dynids(struct hid_driver *hdrv)
1905 {
1906 	struct hid_dynid *dynid, *n;
1907 
1908 	spin_lock(&hdrv->dyn_lock);
1909 	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1910 		list_del(&dynid->list);
1911 		kfree(dynid);
1912 	}
1913 	spin_unlock(&hdrv->dyn_lock);
1914 }
1915 
1916 const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1917 					     struct hid_driver *hdrv)
1918 {
1919 	struct hid_dynid *dynid;
1920 
1921 	spin_lock(&hdrv->dyn_lock);
1922 	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1923 		if (hid_match_one_id(hdev, &dynid->id)) {
1924 			spin_unlock(&hdrv->dyn_lock);
1925 			return &dynid->id;
1926 		}
1927 	}
1928 	spin_unlock(&hdrv->dyn_lock);
1929 
1930 	return hid_match_id(hdev, hdrv->id_table);
1931 }
1932 EXPORT_SYMBOL_GPL(hid_match_device);
1933 
1934 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1935 {
1936 	struct hid_driver *hdrv = to_hid_driver(drv);
1937 	struct hid_device *hdev = to_hid_device(dev);
1938 
1939 	return hid_match_device(hdev, hdrv) != NULL;
1940 }
1941 
1942 static int hid_device_probe(struct device *dev)
1943 {
1944 	struct hid_driver *hdrv = to_hid_driver(dev->driver);
1945 	struct hid_device *hdev = to_hid_device(dev);
1946 	const struct hid_device_id *id;
1947 	int ret = 0;
1948 
1949 	if (down_interruptible(&hdev->driver_input_lock)) {
1950 		ret = -EINTR;
1951 		goto end;
1952 	}
1953 	hdev->io_started = false;
1954 
1955 	if (!hdev->driver) {
1956 		id = hid_match_device(hdev, hdrv);
1957 		if (id == NULL) {
1958 			ret = -ENODEV;
1959 			goto unlock;
1960 		}
1961 
1962 		if (hdrv->match) {
1963 			if (!hdrv->match(hdev, hid_ignore_special_drivers)) {
1964 				ret = -ENODEV;
1965 				goto unlock;
1966 			}
1967 		} else {
1968 			/*
1969 			 * hid-generic implements .match(), so if
1970 			 * hid_ignore_special_drivers is set, we can safely
1971 			 * return.
1972 			 */
1973 			if (hid_ignore_special_drivers) {
1974 				ret = -ENODEV;
1975 				goto unlock;
1976 			}
1977 		}
1978 
1979 		/* reset the quirks that has been previously set */
1980 		hdev->quirks = hid_lookup_quirk(hdev);
1981 		hdev->driver = hdrv;
1982 		if (hdrv->probe) {
1983 			ret = hdrv->probe(hdev, id);
1984 		} else { /* default probe */
1985 			ret = hid_open_report(hdev);
1986 			if (!ret)
1987 				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1988 		}
1989 		if (ret) {
1990 			hid_close_report(hdev);
1991 			hdev->driver = NULL;
1992 		}
1993 	}
1994 unlock:
1995 	if (!hdev->io_started)
1996 		up(&hdev->driver_input_lock);
1997 end:
1998 	return ret;
1999 }
2000 
2001 static int hid_device_remove(struct device *dev)
2002 {
2003 	struct hid_device *hdev = to_hid_device(dev);
2004 	struct hid_driver *hdrv;
2005 	int ret = 0;
2006 
2007 	if (down_interruptible(&hdev->driver_input_lock)) {
2008 		ret = -EINTR;
2009 		goto end;
2010 	}
2011 	hdev->io_started = false;
2012 
2013 	hdrv = hdev->driver;
2014 	if (hdrv) {
2015 		if (hdrv->remove)
2016 			hdrv->remove(hdev);
2017 		else /* default remove */
2018 			hid_hw_stop(hdev);
2019 		hid_close_report(hdev);
2020 		hdev->driver = NULL;
2021 	}
2022 
2023 	if (!hdev->io_started)
2024 		up(&hdev->driver_input_lock);
2025 end:
2026 	return ret;
2027 }
2028 
2029 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
2030 			     char *buf)
2031 {
2032 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
2033 
2034 	return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
2035 			 hdev->bus, hdev->group, hdev->vendor, hdev->product);
2036 }
2037 static DEVICE_ATTR_RO(modalias);
2038 
2039 static struct attribute *hid_dev_attrs[] = {
2040 	&dev_attr_modalias.attr,
2041 	NULL,
2042 };
2043 static struct bin_attribute *hid_dev_bin_attrs[] = {
2044 	&dev_bin_attr_report_desc,
2045 	NULL
2046 };
2047 static const struct attribute_group hid_dev_group = {
2048 	.attrs = hid_dev_attrs,
2049 	.bin_attrs = hid_dev_bin_attrs,
2050 };
2051 __ATTRIBUTE_GROUPS(hid_dev);
2052 
2053 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
2054 {
2055 	struct hid_device *hdev = to_hid_device(dev);
2056 
2057 	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
2058 			hdev->bus, hdev->vendor, hdev->product))
2059 		return -ENOMEM;
2060 
2061 	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
2062 		return -ENOMEM;
2063 
2064 	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
2065 		return -ENOMEM;
2066 
2067 	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
2068 		return -ENOMEM;
2069 
2070 	if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
2071 			   hdev->bus, hdev->group, hdev->vendor, hdev->product))
2072 		return -ENOMEM;
2073 
2074 	return 0;
2075 }
2076 
2077 struct bus_type hid_bus_type = {
2078 	.name		= "hid",
2079 	.dev_groups	= hid_dev_groups,
2080 	.drv_groups	= hid_drv_groups,
2081 	.match		= hid_bus_match,
2082 	.probe		= hid_device_probe,
2083 	.remove		= hid_device_remove,
2084 	.uevent		= hid_uevent,
2085 };
2086 EXPORT_SYMBOL(hid_bus_type);
2087 
2088 int hid_add_device(struct hid_device *hdev)
2089 {
2090 	static atomic_t id = ATOMIC_INIT(0);
2091 	int ret;
2092 
2093 	if (WARN_ON(hdev->status & HID_STAT_ADDED))
2094 		return -EBUSY;
2095 
2096 	hdev->quirks = hid_lookup_quirk(hdev);
2097 
2098 	/* we need to kill them here, otherwise they will stay allocated to
2099 	 * wait for coming driver */
2100 	if (hid_ignore(hdev))
2101 		return -ENODEV;
2102 
2103 	/*
2104 	 * Check for the mandatory transport channel.
2105 	 */
2106 	 if (!hdev->ll_driver->raw_request) {
2107 		hid_err(hdev, "transport driver missing .raw_request()\n");
2108 		return -EINVAL;
2109 	 }
2110 
2111 	/*
2112 	 * Read the device report descriptor once and use as template
2113 	 * for the driver-specific modifications.
2114 	 */
2115 	ret = hdev->ll_driver->parse(hdev);
2116 	if (ret)
2117 		return ret;
2118 	if (!hdev->dev_rdesc)
2119 		return -ENODEV;
2120 
2121 	/*
2122 	 * Scan generic devices for group information
2123 	 */
2124 	if (hid_ignore_special_drivers) {
2125 		hdev->group = HID_GROUP_GENERIC;
2126 	} else if (!hdev->group &&
2127 		   !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
2128 		ret = hid_scan_report(hdev);
2129 		if (ret)
2130 			hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2131 	}
2132 
2133 	/* XXX hack, any other cleaner solution after the driver core
2134 	 * is converted to allow more than 20 bytes as the device name? */
2135 	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
2136 		     hdev->vendor, hdev->product, atomic_inc_return(&id));
2137 
2138 	hid_debug_register(hdev, dev_name(&hdev->dev));
2139 	ret = device_add(&hdev->dev);
2140 	if (!ret)
2141 		hdev->status |= HID_STAT_ADDED;
2142 	else
2143 		hid_debug_unregister(hdev);
2144 
2145 	return ret;
2146 }
2147 EXPORT_SYMBOL_GPL(hid_add_device);
2148 
2149 /**
2150  * hid_allocate_device - allocate new hid device descriptor
2151  *
2152  * Allocate and initialize hid device, so that hid_destroy_device might be
2153  * used to free it.
2154  *
2155  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
2156  * error value.
2157  */
2158 struct hid_device *hid_allocate_device(void)
2159 {
2160 	struct hid_device *hdev;
2161 	int ret = -ENOMEM;
2162 
2163 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
2164 	if (hdev == NULL)
2165 		return ERR_PTR(ret);
2166 
2167 	device_initialize(&hdev->dev);
2168 	hdev->dev.release = hid_device_release;
2169 	hdev->dev.bus = &hid_bus_type;
2170 	device_enable_async_suspend(&hdev->dev);
2171 
2172 	hid_close_report(hdev);
2173 
2174 	init_waitqueue_head(&hdev->debug_wait);
2175 	INIT_LIST_HEAD(&hdev->debug_list);
2176 	spin_lock_init(&hdev->debug_list_lock);
2177 	sema_init(&hdev->driver_input_lock, 1);
2178 	mutex_init(&hdev->ll_open_lock);
2179 
2180 	return hdev;
2181 }
2182 EXPORT_SYMBOL_GPL(hid_allocate_device);
2183 
2184 static void hid_remove_device(struct hid_device *hdev)
2185 {
2186 	if (hdev->status & HID_STAT_ADDED) {
2187 		device_del(&hdev->dev);
2188 		hid_debug_unregister(hdev);
2189 		hdev->status &= ~HID_STAT_ADDED;
2190 	}
2191 	kfree(hdev->dev_rdesc);
2192 	hdev->dev_rdesc = NULL;
2193 	hdev->dev_rsize = 0;
2194 }
2195 
2196 /**
2197  * hid_destroy_device - free previously allocated device
2198  *
2199  * @hdev: hid device
2200  *
2201  * If you allocate hid_device through hid_allocate_device, you should ever
2202  * free by this function.
2203  */
2204 void hid_destroy_device(struct hid_device *hdev)
2205 {
2206 	hid_remove_device(hdev);
2207 	put_device(&hdev->dev);
2208 }
2209 EXPORT_SYMBOL_GPL(hid_destroy_device);
2210 
2211 
2212 static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
2213 {
2214 	struct hid_driver *hdrv = data;
2215 	struct hid_device *hdev = to_hid_device(dev);
2216 
2217 	if (hdev->driver == hdrv &&
2218 	    !hdrv->match(hdev, hid_ignore_special_drivers))
2219 		return device_reprobe(dev);
2220 
2221 	return 0;
2222 }
2223 
2224 static int __hid_bus_driver_added(struct device_driver *drv, void *data)
2225 {
2226 	struct hid_driver *hdrv = to_hid_driver(drv);
2227 
2228 	if (hdrv->match) {
2229 		bus_for_each_dev(&hid_bus_type, NULL, hdrv,
2230 				 __hid_bus_reprobe_drivers);
2231 	}
2232 
2233 	return 0;
2234 }
2235 
2236 static int __bus_removed_driver(struct device_driver *drv, void *data)
2237 {
2238 	return bus_rescan_devices(&hid_bus_type);
2239 }
2240 
2241 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
2242 		const char *mod_name)
2243 {
2244 	int ret;
2245 
2246 	hdrv->driver.name = hdrv->name;
2247 	hdrv->driver.bus = &hid_bus_type;
2248 	hdrv->driver.owner = owner;
2249 	hdrv->driver.mod_name = mod_name;
2250 
2251 	INIT_LIST_HEAD(&hdrv->dyn_list);
2252 	spin_lock_init(&hdrv->dyn_lock);
2253 
2254 	ret = driver_register(&hdrv->driver);
2255 
2256 	if (ret == 0)
2257 		bus_for_each_drv(&hid_bus_type, NULL, NULL,
2258 				 __hid_bus_driver_added);
2259 
2260 	return ret;
2261 }
2262 EXPORT_SYMBOL_GPL(__hid_register_driver);
2263 
2264 void hid_unregister_driver(struct hid_driver *hdrv)
2265 {
2266 	driver_unregister(&hdrv->driver);
2267 	hid_free_dynids(hdrv);
2268 
2269 	bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
2270 }
2271 EXPORT_SYMBOL_GPL(hid_unregister_driver);
2272 
2273 int hid_check_keys_pressed(struct hid_device *hid)
2274 {
2275 	struct hid_input *hidinput;
2276 	int i;
2277 
2278 	if (!(hid->claimed & HID_CLAIMED_INPUT))
2279 		return 0;
2280 
2281 	list_for_each_entry(hidinput, &hid->inputs, list) {
2282 		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
2283 			if (hidinput->input->key[i])
2284 				return 1;
2285 	}
2286 
2287 	return 0;
2288 }
2289 
2290 EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
2291 
2292 static int __init hid_init(void)
2293 {
2294 	int ret;
2295 
2296 	if (hid_debug)
2297 		pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
2298 			"debugfs is now used for inspecting the device (report descriptor, reports)\n");
2299 
2300 	ret = bus_register(&hid_bus_type);
2301 	if (ret) {
2302 		pr_err("can't register hid bus\n");
2303 		goto err;
2304 	}
2305 
2306 	ret = hidraw_init();
2307 	if (ret)
2308 		goto err_bus;
2309 
2310 	hid_debug_init();
2311 
2312 	return 0;
2313 err_bus:
2314 	bus_unregister(&hid_bus_type);
2315 err:
2316 	return ret;
2317 }
2318 
2319 static void __exit hid_exit(void)
2320 {
2321 	hid_debug_exit();
2322 	hidraw_exit();
2323 	bus_unregister(&hid_bus_type);
2324 	hid_quirks_exit(HID_BUS_ANY);
2325 }
2326 
2327 module_init(hid_init);
2328 module_exit(hid_exit);
2329 
2330 MODULE_AUTHOR("Andreas Gal");
2331 MODULE_AUTHOR("Vojtech Pavlik");
2332 MODULE_AUTHOR("Jiri Kosina");
2333 MODULE_LICENSE("GPL");
2334