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