xref: /openbmc/linux/drivers/hid/hid-core.c (revision ea427a222d8bdf2bc1a8a6da3ebe247f7dced70c)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2dde5845aSJiri Kosina /*
3229695e5SJiri Kosina  *  HID support for Linux
4dde5845aSJiri Kosina  *
5dde5845aSJiri Kosina  *  Copyright (c) 1999 Andreas Gal
6dde5845aSJiri Kosina  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7dde5845aSJiri Kosina  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
86b1968d5SJiri Kosina  *  Copyright (c) 2006-2012 Jiri Kosina
9dde5845aSJiri Kosina  */
10dde5845aSJiri Kosina 
11dde5845aSJiri Kosina /*
12dde5845aSJiri Kosina  */
13dde5845aSJiri Kosina 
144291ee30SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
154291ee30SJoe Perches 
16dde5845aSJiri Kosina #include <linux/module.h>
17dde5845aSJiri Kosina #include <linux/slab.h>
18dde5845aSJiri Kosina #include <linux/init.h>
19dde5845aSJiri Kosina #include <linux/kernel.h>
20dde5845aSJiri Kosina #include <linux/list.h>
21dde5845aSJiri Kosina #include <linux/mm.h>
22dde5845aSJiri Kosina #include <linux/spinlock.h>
23dde5845aSJiri Kosina #include <asm/unaligned.h>
24dde5845aSJiri Kosina #include <asm/byteorder.h>
25dde5845aSJiri Kosina #include <linux/input.h>
26dde5845aSJiri Kosina #include <linux/wait.h>
2747a80edbSJiri Kosina #include <linux/vmalloc.h>
28c4124c9bSJiri Kosina #include <linux/sched.h>
294ea54542SDavid Herrmann #include <linux/semaphore.h>
30dde5845aSJiri Kosina 
31dde5845aSJiri Kosina #include <linux/hid.h>
32dde5845aSJiri Kosina #include <linux/hiddev.h>
33c080d89aSJiri Kosina #include <linux/hid-debug.h>
3486166b7bSJiri Kosina #include <linux/hidraw.h>
35dde5845aSJiri Kosina 
365f22a799SJiri Slaby #include "hid-ids.h"
375f22a799SJiri Slaby 
38dde5845aSJiri Kosina /*
39dde5845aSJiri Kosina  * Version Information
40dde5845aSJiri Kosina  */
41dde5845aSJiri Kosina 
4253149801SJiri Kosina #define DRIVER_DESC "HID core driver"
43dde5845aSJiri Kosina 
4458037eb9SJiri Kosina int hid_debug = 0;
45377e10fbSAnssi Hannula module_param_named(debug, hid_debug, int, 0600);
46cd667ce2SJiri Kosina MODULE_PARM_DESC(debug, "toggle HID debugging messages");
4758037eb9SJiri Kosina EXPORT_SYMBOL_GPL(hid_debug);
4858037eb9SJiri Kosina 
496b1968d5SJiri Kosina static int hid_ignore_special_drivers = 0;
506b1968d5SJiri Kosina module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
51643727a9SHans Petter Selasky MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
526b1968d5SJiri Kosina 
53dde5845aSJiri Kosina /*
54dde5845aSJiri Kosina  * Register a new report for a device.
55dde5845aSJiri Kosina  */
56dde5845aSJiri Kosina 
57f07b3c1dSBenjamin Tissoires struct hid_report *hid_register_report(struct hid_device *device,
58ead77b65SBenjamin Tissoires 				       enum hid_report_type type, unsigned int id,
59f07b3c1dSBenjamin Tissoires 				       unsigned int application)
60dde5845aSJiri Kosina {
61dde5845aSJiri Kosina 	struct hid_report_enum *report_enum = device->report_enum + type;
62dde5845aSJiri Kosina 	struct hid_report *report;
63dde5845aSJiri Kosina 
6443622021SKees Cook 	if (id >= HID_MAX_IDS)
6543622021SKees Cook 		return NULL;
66dde5845aSJiri Kosina 	if (report_enum->report_id_hash[id])
67dde5845aSJiri Kosina 		return report_enum->report_id_hash[id];
68dde5845aSJiri Kosina 
69a3789a17SJoe Perches 	report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
70a3789a17SJoe Perches 	if (!report)
71dde5845aSJiri Kosina 		return NULL;
72dde5845aSJiri Kosina 
73dde5845aSJiri Kosina 	if (id != 0)
74dde5845aSJiri Kosina 		report_enum->numbered = 1;
75dde5845aSJiri Kosina 
76dde5845aSJiri Kosina 	report->id = id;
77dde5845aSJiri Kosina 	report->type = type;
78dde5845aSJiri Kosina 	report->size = 0;
79dde5845aSJiri Kosina 	report->device = device;
80f07b3c1dSBenjamin Tissoires 	report->application = application;
81dde5845aSJiri Kosina 	report_enum->report_id_hash[id] = report;
82dde5845aSJiri Kosina 
83dde5845aSJiri Kosina 	list_add_tail(&report->list, &report_enum->report_list);
8422f4b026SBenjamin Tissoires 	INIT_LIST_HEAD(&report->field_entry_list);
85dde5845aSJiri Kosina 
86dde5845aSJiri Kosina 	return report;
87dde5845aSJiri Kosina }
8890a006abSMichael Poole EXPORT_SYMBOL_GPL(hid_register_report);
89dde5845aSJiri Kosina 
90dde5845aSJiri Kosina /*
91dde5845aSJiri Kosina  * Register a new field for this report.
92dde5845aSJiri Kosina  */
93dde5845aSJiri Kosina 
94ed9be64eSWill McVicker static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages)
95dde5845aSJiri Kosina {
96dde5845aSJiri Kosina 	struct hid_field *field;
97dde5845aSJiri Kosina 
98dde5845aSJiri Kosina 	if (report->maxfield == HID_MAX_FIELDS) {
998c3d52fcSJiri Kosina 		hid_err(report->device, "too many fields in report\n");
100dde5845aSJiri Kosina 		return NULL;
101dde5845aSJiri Kosina 	}
102dde5845aSJiri Kosina 
103a3789a17SJoe Perches 	field = kzalloc((sizeof(struct hid_field) +
104a3789a17SJoe Perches 			 usages * sizeof(struct hid_usage) +
10522f4b026SBenjamin Tissoires 			 3 * usages * sizeof(unsigned int)), GFP_KERNEL);
106a3789a17SJoe Perches 	if (!field)
107a3789a17SJoe Perches 		return NULL;
108dde5845aSJiri Kosina 
109dde5845aSJiri Kosina 	field->index = report->maxfield++;
110dde5845aSJiri Kosina 	report->field[field->index] = field;
111dde5845aSJiri Kosina 	field->usage = (struct hid_usage *)(field + 1);
112282bfd4cSJiri Slaby 	field->value = (s32 *)(field->usage + usages);
1131c1813a7SBenjamin Tissoires 	field->new_value = (s32 *)(field->value + usages);
11422f4b026SBenjamin Tissoires 	field->usages_priorities = (s32 *)(field->new_value + usages);
115dde5845aSJiri Kosina 	field->report = report;
116dde5845aSJiri Kosina 
117dde5845aSJiri Kosina 	return field;
118dde5845aSJiri Kosina }
119dde5845aSJiri Kosina 
120dde5845aSJiri Kosina /*
121dde5845aSJiri Kosina  * Open a collection. The type/usage is pushed on the stack.
122dde5845aSJiri Kosina  */
123dde5845aSJiri Kosina 
124dde5845aSJiri Kosina static int open_collection(struct hid_parser *parser, unsigned type)
125dde5845aSJiri Kosina {
126dde5845aSJiri Kosina 	struct hid_collection *collection;
127dde5845aSJiri Kosina 	unsigned usage;
128ee46967fSPeter Hutterer 	int collection_index;
129dde5845aSJiri Kosina 
130dde5845aSJiri Kosina 	usage = parser->local.usage[0];
131dde5845aSJiri Kosina 
13208a8a7cfSBenjamin Tissoires 	if (parser->collection_stack_ptr == parser->collection_stack_size) {
13308a8a7cfSBenjamin Tissoires 		unsigned int *collection_stack;
13408a8a7cfSBenjamin Tissoires 		unsigned int new_size = parser->collection_stack_size +
13508a8a7cfSBenjamin Tissoires 					HID_COLLECTION_STACK_SIZE;
13608a8a7cfSBenjamin Tissoires 
13708a8a7cfSBenjamin Tissoires 		collection_stack = krealloc(parser->collection_stack,
13808a8a7cfSBenjamin Tissoires 					    new_size * sizeof(unsigned int),
13908a8a7cfSBenjamin Tissoires 					    GFP_KERNEL);
14008a8a7cfSBenjamin Tissoires 		if (!collection_stack)
14108a8a7cfSBenjamin Tissoires 			return -ENOMEM;
14208a8a7cfSBenjamin Tissoires 
14308a8a7cfSBenjamin Tissoires 		parser->collection_stack = collection_stack;
14408a8a7cfSBenjamin Tissoires 		parser->collection_stack_size = new_size;
145dde5845aSJiri Kosina 	}
146dde5845aSJiri Kosina 
147dde5845aSJiri Kosina 	if (parser->device->maxcollection == parser->device->collection_size) {
1486da2ec56SKees Cook 		collection = kmalloc(
1496da2ec56SKees Cook 				array3_size(sizeof(struct hid_collection),
1506da2ec56SKees Cook 					    parser->device->collection_size,
1516da2ec56SKees Cook 					    2),
1526da2ec56SKees Cook 				GFP_KERNEL);
153dde5845aSJiri Kosina 		if (collection == NULL) {
1548c3d52fcSJiri Kosina 			hid_err(parser->device, "failed to reallocate collection array\n");
155a6fbaacfSSachin Kamat 			return -ENOMEM;
156dde5845aSJiri Kosina 		}
157dde5845aSJiri Kosina 		memcpy(collection, parser->device->collection,
158dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
159dde5845aSJiri Kosina 			parser->device->collection_size);
160dde5845aSJiri Kosina 		memset(collection + parser->device->collection_size, 0,
161dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
162dde5845aSJiri Kosina 			parser->device->collection_size);
163dde5845aSJiri Kosina 		kfree(parser->device->collection);
164dde5845aSJiri Kosina 		parser->device->collection = collection;
165dde5845aSJiri Kosina 		parser->device->collection_size *= 2;
166dde5845aSJiri Kosina 	}
167dde5845aSJiri Kosina 
168dde5845aSJiri Kosina 	parser->collection_stack[parser->collection_stack_ptr++] =
169dde5845aSJiri Kosina 		parser->device->maxcollection;
170dde5845aSJiri Kosina 
171ee46967fSPeter Hutterer 	collection_index = parser->device->maxcollection++;
172ee46967fSPeter Hutterer 	collection = parser->device->collection + collection_index;
173dde5845aSJiri Kosina 	collection->type = type;
174dde5845aSJiri Kosina 	collection->usage = usage;
175dde5845aSJiri Kosina 	collection->level = parser->collection_stack_ptr - 1;
1761950f462SPhilipp Zabel 	collection->parent_idx = (collection->level == 0) ? -1 :
1771950f462SPhilipp Zabel 		parser->collection_stack[collection->level - 1];
178dde5845aSJiri Kosina 
179dde5845aSJiri Kosina 	if (type == HID_COLLECTION_APPLICATION)
180dde5845aSJiri Kosina 		parser->device->maxapplication++;
181dde5845aSJiri Kosina 
182dde5845aSJiri Kosina 	return 0;
183dde5845aSJiri Kosina }
184dde5845aSJiri Kosina 
185dde5845aSJiri Kosina /*
186dde5845aSJiri Kosina  * Close a collection.
187dde5845aSJiri Kosina  */
188dde5845aSJiri Kosina 
189dde5845aSJiri Kosina static int close_collection(struct hid_parser *parser)
190dde5845aSJiri Kosina {
191dde5845aSJiri Kosina 	if (!parser->collection_stack_ptr) {
1928c3d52fcSJiri Kosina 		hid_err(parser->device, "collection stack underflow\n");
193a6fbaacfSSachin Kamat 		return -EINVAL;
194dde5845aSJiri Kosina 	}
195dde5845aSJiri Kosina 	parser->collection_stack_ptr--;
196dde5845aSJiri Kosina 	return 0;
197dde5845aSJiri Kosina }
198dde5845aSJiri Kosina 
199dde5845aSJiri Kosina /*
200dde5845aSJiri Kosina  * Climb up the stack, search for the specified collection type
201dde5845aSJiri Kosina  * and return the usage.
202dde5845aSJiri Kosina  */
203dde5845aSJiri Kosina 
204dde5845aSJiri Kosina static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
205dde5845aSJiri Kosina {
206504499f2SJoe Perches 	struct hid_collection *collection = parser->device->collection;
207dde5845aSJiri Kosina 	int n;
208504499f2SJoe Perches 
209504499f2SJoe Perches 	for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
210504499f2SJoe Perches 		unsigned index = parser->collection_stack[n];
211504499f2SJoe Perches 		if (collection[index].type == type)
212504499f2SJoe Perches 			return collection[index].usage;
213504499f2SJoe Perches 	}
214dde5845aSJiri Kosina 	return 0; /* we know nothing about this usage type */
215dde5845aSJiri Kosina }
216dde5845aSJiri Kosina 
217dde5845aSJiri Kosina /*
2181cb0d2aeSCandle Sun  * Concatenate usage which defines 16 bits or less with the
2191cb0d2aeSCandle Sun  * currently defined usage page to form a 32 bit usage
2201cb0d2aeSCandle Sun  */
2211cb0d2aeSCandle Sun 
2221cb0d2aeSCandle Sun static void complete_usage(struct hid_parser *parser, unsigned int index)
2231cb0d2aeSCandle Sun {
2241cb0d2aeSCandle Sun 	parser->local.usage[index] &= 0xFFFF;
2251cb0d2aeSCandle Sun 	parser->local.usage[index] |=
2261cb0d2aeSCandle Sun 		(parser->global.usage_page & 0xFFFF) << 16;
2271cb0d2aeSCandle Sun }
2281cb0d2aeSCandle Sun 
2291cb0d2aeSCandle Sun /*
230dde5845aSJiri Kosina  * Add a usage to the temporary parser table.
231dde5845aSJiri Kosina  */
232dde5845aSJiri Kosina 
23358e75155SNicolas Saenz Julienne static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
234dde5845aSJiri Kosina {
235dde5845aSJiri Kosina 	if (parser->local.usage_index >= HID_MAX_USAGES) {
2368c3d52fcSJiri Kosina 		hid_err(parser->device, "usage index exceeded\n");
237dde5845aSJiri Kosina 		return -1;
238dde5845aSJiri Kosina 	}
239dde5845aSJiri Kosina 	parser->local.usage[parser->local.usage_index] = usage;
2401cb0d2aeSCandle Sun 
2411cb0d2aeSCandle Sun 	/*
2421cb0d2aeSCandle Sun 	 * If Usage item only includes usage id, concatenate it with
2431cb0d2aeSCandle Sun 	 * currently defined usage page
2441cb0d2aeSCandle Sun 	 */
2451cb0d2aeSCandle Sun 	if (size <= 2)
2461cb0d2aeSCandle Sun 		complete_usage(parser, parser->local.usage_index);
2471cb0d2aeSCandle Sun 
24858e75155SNicolas Saenz Julienne 	parser->local.usage_size[parser->local.usage_index] = size;
249dde5845aSJiri Kosina 	parser->local.collection_index[parser->local.usage_index] =
250dde5845aSJiri Kosina 		parser->collection_stack_ptr ?
251dde5845aSJiri Kosina 		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
252dde5845aSJiri Kosina 	parser->local.usage_index++;
253dde5845aSJiri Kosina 	return 0;
254dde5845aSJiri Kosina }
255dde5845aSJiri Kosina 
256dde5845aSJiri Kosina /*
257dde5845aSJiri Kosina  * Register a new field for this report.
258dde5845aSJiri Kosina  */
259dde5845aSJiri Kosina 
260dde5845aSJiri Kosina static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
261dde5845aSJiri Kosina {
262dde5845aSJiri Kosina 	struct hid_report *report;
263dde5845aSJiri Kosina 	struct hid_field *field;
264f07b3c1dSBenjamin Tissoires 	unsigned int usages;
265f07b3c1dSBenjamin Tissoires 	unsigned int offset;
266f07b3c1dSBenjamin Tissoires 	unsigned int i;
267f07b3c1dSBenjamin Tissoires 	unsigned int application;
268dde5845aSJiri Kosina 
269f07b3c1dSBenjamin Tissoires 	application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
270f07b3c1dSBenjamin Tissoires 
271f07b3c1dSBenjamin Tissoires 	report = hid_register_report(parser->device, report_type,
272f07b3c1dSBenjamin Tissoires 				     parser->global.report_id, application);
273a3789a17SJoe Perches 	if (!report) {
2748c3d52fcSJiri Kosina 		hid_err(parser->device, "hid_register_report failed\n");
275dde5845aSJiri Kosina 		return -1;
276dde5845aSJiri Kosina 	}
277dde5845aSJiri Kosina 
278bb2e1976SJiri Kosina 	/* Handle both signed and unsigned cases properly */
2790cd516c2Ssrinivas pandruvada 	if ((parser->global.logical_minimum < 0 &&
2800cd516c2Ssrinivas pandruvada 		parser->global.logical_maximum <
2810cd516c2Ssrinivas pandruvada 		parser->global.logical_minimum) ||
2820cd516c2Ssrinivas pandruvada 		(parser->global.logical_minimum >= 0 &&
2830cd516c2Ssrinivas pandruvada 		(__u32)parser->global.logical_maximum <
2840cd516c2Ssrinivas pandruvada 		(__u32)parser->global.logical_minimum)) {
2850cd516c2Ssrinivas pandruvada 		dbg_hid("logical range invalid 0x%x 0x%x\n",
2860cd516c2Ssrinivas pandruvada 			parser->global.logical_minimum,
2870cd516c2Ssrinivas pandruvada 			parser->global.logical_maximum);
288dde5845aSJiri Kosina 		return -1;
289dde5845aSJiri Kosina 	}
290dde5845aSJiri Kosina 
291dde5845aSJiri Kosina 	offset = report->size;
292dde5845aSJiri Kosina 	report->size += parser->global.report_size * parser->global.report_count;
293dde5845aSJiri Kosina 
2948ec321e9SAlan Stern 	/* Total size check: Allow for possible report index byte */
2958ec321e9SAlan Stern 	if (report->size > (HID_MAX_BUFFER_SIZE - 1) << 3) {
2968ec321e9SAlan Stern 		hid_err(parser->device, "report is too long\n");
2978ec321e9SAlan Stern 		return -1;
2988ec321e9SAlan Stern 	}
2998ec321e9SAlan Stern 
300dde5845aSJiri Kosina 	if (!parser->local.usage_index) /* Ignore padding fields */
301dde5845aSJiri Kosina 		return 0;
302dde5845aSJiri Kosina 
303cc6b54aaSBenjamin Tissoires 	usages = max_t(unsigned, parser->local.usage_index,
304cc6b54aaSBenjamin Tissoires 				 parser->global.report_count);
305dde5845aSJiri Kosina 
306ed9be64eSWill McVicker 	field = hid_register_field(report, usages);
307a3789a17SJoe Perches 	if (!field)
308dde5845aSJiri Kosina 		return 0;
309dde5845aSJiri Kosina 
310dde5845aSJiri Kosina 	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
311dde5845aSJiri Kosina 	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
312f07b3c1dSBenjamin Tissoires 	field->application = application;
313dde5845aSJiri Kosina 
314dde5845aSJiri Kosina 	for (i = 0; i < usages; i++) {
315cc6b54aaSBenjamin Tissoires 		unsigned j = i;
316dde5845aSJiri Kosina 		/* Duplicate the last usage we parsed if we have excess values */
317dde5845aSJiri Kosina 		if (i >= parser->local.usage_index)
318dde5845aSJiri Kosina 			j = parser->local.usage_index - 1;
319dde5845aSJiri Kosina 		field->usage[i].hid = parser->local.usage[j];
320dde5845aSJiri Kosina 		field->usage[i].collection_index =
321dde5845aSJiri Kosina 			parser->local.collection_index[j];
322cc6b54aaSBenjamin Tissoires 		field->usage[i].usage_index = i;
3235a4abb36SPeter Hutterer 		field->usage[i].resolution_multiplier = 1;
324dde5845aSJiri Kosina 	}
325dde5845aSJiri Kosina 
326dde5845aSJiri Kosina 	field->maxusage = usages;
327dde5845aSJiri Kosina 	field->flags = flags;
328dde5845aSJiri Kosina 	field->report_offset = offset;
329dde5845aSJiri Kosina 	field->report_type = report_type;
330dde5845aSJiri Kosina 	field->report_size = parser->global.report_size;
331dde5845aSJiri Kosina 	field->report_count = parser->global.report_count;
332dde5845aSJiri Kosina 	field->logical_minimum = parser->global.logical_minimum;
333dde5845aSJiri Kosina 	field->logical_maximum = parser->global.logical_maximum;
334dde5845aSJiri Kosina 	field->physical_minimum = parser->global.physical_minimum;
335dde5845aSJiri Kosina 	field->physical_maximum = parser->global.physical_maximum;
336dde5845aSJiri Kosina 	field->unit_exponent = parser->global.unit_exponent;
337dde5845aSJiri Kosina 	field->unit = parser->global.unit;
338dde5845aSJiri Kosina 
339dde5845aSJiri Kosina 	return 0;
340dde5845aSJiri Kosina }
341dde5845aSJiri Kosina 
342dde5845aSJiri Kosina /*
343dde5845aSJiri Kosina  * Read data value from item.
344dde5845aSJiri Kosina  */
345dde5845aSJiri Kosina 
346dde5845aSJiri Kosina static u32 item_udata(struct hid_item *item)
347dde5845aSJiri Kosina {
348dde5845aSJiri Kosina 	switch (item->size) {
349dde5845aSJiri Kosina 	case 1: return item->data.u8;
350dde5845aSJiri Kosina 	case 2: return item->data.u16;
351dde5845aSJiri Kosina 	case 4: return item->data.u32;
352dde5845aSJiri Kosina 	}
353dde5845aSJiri Kosina 	return 0;
354dde5845aSJiri Kosina }
355dde5845aSJiri Kosina 
356dde5845aSJiri Kosina static s32 item_sdata(struct hid_item *item)
357dde5845aSJiri Kosina {
358dde5845aSJiri Kosina 	switch (item->size) {
359dde5845aSJiri Kosina 	case 1: return item->data.s8;
360dde5845aSJiri Kosina 	case 2: return item->data.s16;
361dde5845aSJiri Kosina 	case 4: return item->data.s32;
362dde5845aSJiri Kosina 	}
363dde5845aSJiri Kosina 	return 0;
364dde5845aSJiri Kosina }
365dde5845aSJiri Kosina 
366dde5845aSJiri Kosina /*
367dde5845aSJiri Kosina  * Process a global item.
368dde5845aSJiri Kosina  */
369dde5845aSJiri Kosina 
370dde5845aSJiri Kosina static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
371dde5845aSJiri Kosina {
372ad0e669bSNikolai Kondrashov 	__s32 raw_value;
373dde5845aSJiri Kosina 	switch (item->tag) {
374dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PUSH:
375dde5845aSJiri Kosina 
376dde5845aSJiri Kosina 		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
3778c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack overflow\n");
378dde5845aSJiri Kosina 			return -1;
379dde5845aSJiri Kosina 		}
380dde5845aSJiri Kosina 
381dde5845aSJiri Kosina 		memcpy(parser->global_stack + parser->global_stack_ptr++,
382dde5845aSJiri Kosina 			&parser->global, sizeof(struct hid_global));
383dde5845aSJiri Kosina 		return 0;
384dde5845aSJiri Kosina 
385dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_POP:
386dde5845aSJiri Kosina 
387dde5845aSJiri Kosina 		if (!parser->global_stack_ptr) {
3888c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack underflow\n");
389dde5845aSJiri Kosina 			return -1;
390dde5845aSJiri Kosina 		}
391dde5845aSJiri Kosina 
392880d29f1SJiri Slaby 		memcpy(&parser->global, parser->global_stack +
393880d29f1SJiri Slaby 			--parser->global_stack_ptr, sizeof(struct hid_global));
394dde5845aSJiri Kosina 		return 0;
395dde5845aSJiri Kosina 
396dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
397dde5845aSJiri Kosina 		parser->global.usage_page = item_udata(item);
398dde5845aSJiri Kosina 		return 0;
399dde5845aSJiri Kosina 
400dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
401dde5845aSJiri Kosina 		parser->global.logical_minimum = item_sdata(item);
402dde5845aSJiri Kosina 		return 0;
403dde5845aSJiri Kosina 
404dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
405dde5845aSJiri Kosina 		if (parser->global.logical_minimum < 0)
406dde5845aSJiri Kosina 			parser->global.logical_maximum = item_sdata(item);
407dde5845aSJiri Kosina 		else
408dde5845aSJiri Kosina 			parser->global.logical_maximum = item_udata(item);
409dde5845aSJiri Kosina 		return 0;
410dde5845aSJiri Kosina 
411dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
412dde5845aSJiri Kosina 		parser->global.physical_minimum = item_sdata(item);
413dde5845aSJiri Kosina 		return 0;
414dde5845aSJiri Kosina 
415dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
416dde5845aSJiri Kosina 		if (parser->global.physical_minimum < 0)
417dde5845aSJiri Kosina 			parser->global.physical_maximum = item_sdata(item);
418dde5845aSJiri Kosina 		else
419dde5845aSJiri Kosina 			parser->global.physical_maximum = item_udata(item);
420dde5845aSJiri Kosina 		return 0;
421dde5845aSJiri Kosina 
422dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
423ad0e669bSNikolai Kondrashov 		/* Many devices provide unit exponent as a two's complement
424ad0e669bSNikolai Kondrashov 		 * nibble due to the common misunderstanding of HID
425ad0e669bSNikolai Kondrashov 		 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
426ad0e669bSNikolai Kondrashov 		 * both this and the standard encoding. */
427ad0e669bSNikolai Kondrashov 		raw_value = item_sdata(item);
42877463838SBenjamin Tissoires 		if (!(raw_value & 0xfffffff0))
42977463838SBenjamin Tissoires 			parser->global.unit_exponent = hid_snto32(raw_value, 4);
43077463838SBenjamin Tissoires 		else
43177463838SBenjamin Tissoires 			parser->global.unit_exponent = raw_value;
432dde5845aSJiri Kosina 		return 0;
433dde5845aSJiri Kosina 
434dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT:
435dde5845aSJiri Kosina 		parser->global.unit = item_udata(item);
436dde5845aSJiri Kosina 		return 0;
437dde5845aSJiri Kosina 
438dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
439880d29f1SJiri Slaby 		parser->global.report_size = item_udata(item);
44071f6fa90SSong, Hongyan 		if (parser->global.report_size > 256) {
4418c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_size %d\n",
442880d29f1SJiri Slaby 					parser->global.report_size);
443dde5845aSJiri Kosina 			return -1;
444dde5845aSJiri Kosina 		}
445dde5845aSJiri Kosina 		return 0;
446dde5845aSJiri Kosina 
447dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
448880d29f1SJiri Slaby 		parser->global.report_count = item_udata(item);
449880d29f1SJiri Slaby 		if (parser->global.report_count > HID_MAX_USAGES) {
4508c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_count %d\n",
451880d29f1SJiri Slaby 					parser->global.report_count);
452dde5845aSJiri Kosina 			return -1;
453dde5845aSJiri Kosina 		}
454dde5845aSJiri Kosina 		return 0;
455dde5845aSJiri Kosina 
456dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
457880d29f1SJiri Slaby 		parser->global.report_id = item_udata(item);
45843622021SKees Cook 		if (parser->global.report_id == 0 ||
45943622021SKees Cook 		    parser->global.report_id >= HID_MAX_IDS) {
46043622021SKees Cook 			hid_err(parser->device, "report_id %u is invalid\n",
46143622021SKees Cook 				parser->global.report_id);
462dde5845aSJiri Kosina 			return -1;
463dde5845aSJiri Kosina 		}
464dde5845aSJiri Kosina 		return 0;
465dde5845aSJiri Kosina 
466dde5845aSJiri Kosina 	default:
4678c3d52fcSJiri Kosina 		hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
468dde5845aSJiri Kosina 		return -1;
469dde5845aSJiri Kosina 	}
470dde5845aSJiri Kosina }
471dde5845aSJiri Kosina 
472dde5845aSJiri Kosina /*
473dde5845aSJiri Kosina  * Process a local item.
474dde5845aSJiri Kosina  */
475dde5845aSJiri Kosina 
476dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
477dde5845aSJiri Kosina {
478dde5845aSJiri Kosina 	__u32 data;
479dde5845aSJiri Kosina 	unsigned n;
480ba532198SBenjamin Tissoires 	__u32 count;
481dde5845aSJiri Kosina 
482dde5845aSJiri Kosina 	data = item_udata(item);
483dde5845aSJiri Kosina 
484dde5845aSJiri Kosina 	switch (item->tag) {
485dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_DELIMITER:
486dde5845aSJiri Kosina 
487dde5845aSJiri Kosina 		if (data) {
488dde5845aSJiri Kosina 			/*
489dde5845aSJiri Kosina 			 * We treat items before the first delimiter
490dde5845aSJiri Kosina 			 * as global to all usage sets (branch 0).
491dde5845aSJiri Kosina 			 * In the moment we process only these global
492dde5845aSJiri Kosina 			 * items and the first delimiter set.
493dde5845aSJiri Kosina 			 */
494dde5845aSJiri Kosina 			if (parser->local.delimiter_depth != 0) {
4958c3d52fcSJiri Kosina 				hid_err(parser->device, "nested delimiters\n");
496dde5845aSJiri Kosina 				return -1;
497dde5845aSJiri Kosina 			}
498dde5845aSJiri Kosina 			parser->local.delimiter_depth++;
499dde5845aSJiri Kosina 			parser->local.delimiter_branch++;
500dde5845aSJiri Kosina 		} else {
501dde5845aSJiri Kosina 			if (parser->local.delimiter_depth < 1) {
5028c3d52fcSJiri Kosina 				hid_err(parser->device, "bogus close delimiter\n");
503dde5845aSJiri Kosina 				return -1;
504dde5845aSJiri Kosina 			}
505dde5845aSJiri Kosina 			parser->local.delimiter_depth--;
506dde5845aSJiri Kosina 		}
50738ead6efSPaul Chavent 		return 0;
508dde5845aSJiri Kosina 
509dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE:
510dde5845aSJiri Kosina 
511dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
51258037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
513dde5845aSJiri Kosina 			return 0;
514dde5845aSJiri Kosina 		}
515dde5845aSJiri Kosina 
51658e75155SNicolas Saenz Julienne 		return hid_add_usage(parser, data, item->size);
517dde5845aSJiri Kosina 
518dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
519dde5845aSJiri Kosina 
520dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
52158037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
522dde5845aSJiri Kosina 			return 0;
523dde5845aSJiri Kosina 		}
524dde5845aSJiri Kosina 
525dde5845aSJiri Kosina 		parser->local.usage_minimum = data;
526dde5845aSJiri Kosina 		return 0;
527dde5845aSJiri Kosina 
528dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
529dde5845aSJiri Kosina 
530dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
53158037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
532dde5845aSJiri Kosina 			return 0;
533dde5845aSJiri Kosina 		}
534dde5845aSJiri Kosina 
535ba532198SBenjamin Tissoires 		count = data - parser->local.usage_minimum;
536ba532198SBenjamin Tissoires 		if (count + parser->local.usage_index >= HID_MAX_USAGES) {
537ba532198SBenjamin Tissoires 			/*
538ba532198SBenjamin Tissoires 			 * We do not warn if the name is not set, we are
539ba532198SBenjamin Tissoires 			 * actually pre-scanning the device.
540ba532198SBenjamin Tissoires 			 */
541ba532198SBenjamin Tissoires 			if (dev_name(&parser->device->dev))
542ba532198SBenjamin Tissoires 				hid_warn(parser->device,
543ba532198SBenjamin Tissoires 					 "ignoring exceeding usage max\n");
544ba532198SBenjamin Tissoires 			data = HID_MAX_USAGES - parser->local.usage_index +
545ba532198SBenjamin Tissoires 				parser->local.usage_minimum - 1;
546ba532198SBenjamin Tissoires 			if (data <= 0) {
547ba532198SBenjamin Tissoires 				hid_err(parser->device,
548ba532198SBenjamin Tissoires 					"no more usage index available\n");
549ba532198SBenjamin Tissoires 				return -1;
550ba532198SBenjamin Tissoires 			}
551ba532198SBenjamin Tissoires 		}
552ba532198SBenjamin Tissoires 
553dde5845aSJiri Kosina 		for (n = parser->local.usage_minimum; n <= data; n++)
55458e75155SNicolas Saenz Julienne 			if (hid_add_usage(parser, n, item->size)) {
55558037eb9SJiri Kosina 				dbg_hid("hid_add_usage failed\n");
556dde5845aSJiri Kosina 				return -1;
557dde5845aSJiri Kosina 			}
558dde5845aSJiri Kosina 		return 0;
559dde5845aSJiri Kosina 
560dde5845aSJiri Kosina 	default:
561dde5845aSJiri Kosina 
56258037eb9SJiri Kosina 		dbg_hid("unknown local item tag 0x%x\n", item->tag);
563dde5845aSJiri Kosina 		return 0;
564dde5845aSJiri Kosina 	}
565dde5845aSJiri Kosina 	return 0;
566dde5845aSJiri Kosina }
567dde5845aSJiri Kosina 
568dde5845aSJiri Kosina /*
56958e75155SNicolas Saenz Julienne  * Concatenate Usage Pages into Usages where relevant:
57058e75155SNicolas Saenz Julienne  * As per specification, 6.2.2.8: "When the parser encounters a main item it
57158e75155SNicolas Saenz Julienne  * concatenates the last declared Usage Page with a Usage to form a complete
57258e75155SNicolas Saenz Julienne  * usage value."
57358e75155SNicolas Saenz Julienne  */
57458e75155SNicolas Saenz Julienne 
5751cb0d2aeSCandle Sun static void hid_concatenate_last_usage_page(struct hid_parser *parser)
57658e75155SNicolas Saenz Julienne {
57758e75155SNicolas Saenz Julienne 	int i;
5781cb0d2aeSCandle Sun 	unsigned int usage_page;
5791cb0d2aeSCandle Sun 	unsigned int current_page;
58058e75155SNicolas Saenz Julienne 
5811cb0d2aeSCandle Sun 	if (!parser->local.usage_index)
5821cb0d2aeSCandle Sun 		return;
5831cb0d2aeSCandle Sun 
5841cb0d2aeSCandle Sun 	usage_page = parser->global.usage_page;
5851cb0d2aeSCandle Sun 
5861cb0d2aeSCandle Sun 	/*
5871cb0d2aeSCandle Sun 	 * Concatenate usage page again only if last declared Usage Page
5881cb0d2aeSCandle Sun 	 * has not been already used in previous usages concatenation
5891cb0d2aeSCandle Sun 	 */
5901cb0d2aeSCandle Sun 	for (i = parser->local.usage_index - 1; i >= 0; i--) {
5911cb0d2aeSCandle Sun 		if (parser->local.usage_size[i] > 2)
5921cb0d2aeSCandle Sun 			/* Ignore extended usages */
5931cb0d2aeSCandle Sun 			continue;
5941cb0d2aeSCandle Sun 
5951cb0d2aeSCandle Sun 		current_page = parser->local.usage[i] >> 16;
5961cb0d2aeSCandle Sun 		if (current_page == usage_page)
5971cb0d2aeSCandle Sun 			break;
5981cb0d2aeSCandle Sun 
5991cb0d2aeSCandle Sun 		complete_usage(parser, i);
6001cb0d2aeSCandle Sun 	}
60158e75155SNicolas Saenz Julienne }
60258e75155SNicolas Saenz Julienne 
60358e75155SNicolas Saenz Julienne /*
604dde5845aSJiri Kosina  * Process a main item.
605dde5845aSJiri Kosina  */
606dde5845aSJiri Kosina 
607dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
608dde5845aSJiri Kosina {
609dde5845aSJiri Kosina 	__u32 data;
610dde5845aSJiri Kosina 	int ret;
611dde5845aSJiri Kosina 
6121cb0d2aeSCandle Sun 	hid_concatenate_last_usage_page(parser);
61358e75155SNicolas Saenz Julienne 
614dde5845aSJiri Kosina 	data = item_udata(item);
615dde5845aSJiri Kosina 
616dde5845aSJiri Kosina 	switch (item->tag) {
617dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
618dde5845aSJiri Kosina 		ret = open_collection(parser, data & 0xff);
619dde5845aSJiri Kosina 		break;
620dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
621dde5845aSJiri Kosina 		ret = close_collection(parser);
622dde5845aSJiri Kosina 		break;
623dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_INPUT:
624dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
625dde5845aSJiri Kosina 		break;
626dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_OUTPUT:
627dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
628dde5845aSJiri Kosina 		break;
629dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_FEATURE:
630dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
631dde5845aSJiri Kosina 		break;
632dde5845aSJiri Kosina 	default:
6337cb4774eSHans de Goede 		hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
634dde5845aSJiri Kosina 		ret = 0;
635dde5845aSJiri Kosina 	}
636dde5845aSJiri Kosina 
637dde5845aSJiri Kosina 	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
638dde5845aSJiri Kosina 
639dde5845aSJiri Kosina 	return ret;
640dde5845aSJiri Kosina }
641dde5845aSJiri Kosina 
642dde5845aSJiri Kosina /*
643dde5845aSJiri Kosina  * Process a reserved item.
644dde5845aSJiri Kosina  */
645dde5845aSJiri Kosina 
646dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
647dde5845aSJiri Kosina {
64858037eb9SJiri Kosina 	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
649dde5845aSJiri Kosina 	return 0;
650dde5845aSJiri Kosina }
651dde5845aSJiri Kosina 
652dde5845aSJiri Kosina /*
653dde5845aSJiri Kosina  * Free a report and all registered fields. The field->usage and
654dde5845aSJiri Kosina  * field->value table's are allocated behind the field, so we need
655dde5845aSJiri Kosina  * only to free(field) itself.
656dde5845aSJiri Kosina  */
657dde5845aSJiri Kosina 
658dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report)
659dde5845aSJiri Kosina {
660dde5845aSJiri Kosina 	unsigned n;
661dde5845aSJiri Kosina 
66222f4b026SBenjamin Tissoires 	kfree(report->field_entries);
66322f4b026SBenjamin Tissoires 
664dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
665dde5845aSJiri Kosina 		kfree(report->field[n]);
666dde5845aSJiri Kosina 	kfree(report);
667dde5845aSJiri Kosina }
668dde5845aSJiri Kosina 
669dde5845aSJiri Kosina /*
670a7197c2eSHenrik Rydberg  * Close report. This function returns the device
671a7197c2eSHenrik Rydberg  * state to the point prior to hid_open_report().
672dde5845aSJiri Kosina  */
673a7197c2eSHenrik Rydberg static void hid_close_report(struct hid_device *device)
674dde5845aSJiri Kosina {
675dde5845aSJiri Kosina 	unsigned i, j;
676dde5845aSJiri Kosina 
677dde5845aSJiri Kosina 	for (i = 0; i < HID_REPORT_TYPES; i++) {
678dde5845aSJiri Kosina 		struct hid_report_enum *report_enum = device->report_enum + i;
679dde5845aSJiri Kosina 
68043622021SKees Cook 		for (j = 0; j < HID_MAX_IDS; j++) {
681dde5845aSJiri Kosina 			struct hid_report *report = report_enum->report_id_hash[j];
682dde5845aSJiri Kosina 			if (report)
683dde5845aSJiri Kosina 				hid_free_report(report);
684dde5845aSJiri Kosina 		}
685a7197c2eSHenrik Rydberg 		memset(report_enum, 0, sizeof(*report_enum));
686a7197c2eSHenrik Rydberg 		INIT_LIST_HEAD(&report_enum->report_list);
687dde5845aSJiri Kosina 	}
688dde5845aSJiri Kosina 
689dde5845aSJiri Kosina 	kfree(device->rdesc);
690a7197c2eSHenrik Rydberg 	device->rdesc = NULL;
691a7197c2eSHenrik Rydberg 	device->rsize = 0;
692a7197c2eSHenrik Rydberg 
693767fe787SJiri Kosina 	kfree(device->collection);
694a7197c2eSHenrik Rydberg 	device->collection = NULL;
695a7197c2eSHenrik Rydberg 	device->collection_size = 0;
696a7197c2eSHenrik Rydberg 	device->maxcollection = 0;
697a7197c2eSHenrik Rydberg 	device->maxapplication = 0;
698a7197c2eSHenrik Rydberg 
699a7197c2eSHenrik Rydberg 	device->status &= ~HID_STAT_PARSED;
700a7197c2eSHenrik Rydberg }
701a7197c2eSHenrik Rydberg 
702a7197c2eSHenrik Rydberg /*
703a7197c2eSHenrik Rydberg  * Free a device structure, all reports, and all fields.
704a7197c2eSHenrik Rydberg  */
705a7197c2eSHenrik Rydberg 
706a7197c2eSHenrik Rydberg static void hid_device_release(struct device *dev)
707a7197c2eSHenrik Rydberg {
708ee79a8f8SGeliang Tang 	struct hid_device *hid = to_hid_device(dev);
709a7197c2eSHenrik Rydberg 
710a7197c2eSHenrik Rydberg 	hid_close_report(hid);
711a7197c2eSHenrik Rydberg 	kfree(hid->dev_rdesc);
712a7197c2eSHenrik Rydberg 	kfree(hid);
713dde5845aSJiri Kosina }
714dde5845aSJiri Kosina 
715dde5845aSJiri Kosina /*
716dde5845aSJiri Kosina  * Fetch a report description item from the data stream. We support long
717dde5845aSJiri Kosina  * items, though they are not used yet.
718dde5845aSJiri Kosina  */
719dde5845aSJiri Kosina 
720dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
721dde5845aSJiri Kosina {
722dde5845aSJiri Kosina 	u8 b;
723dde5845aSJiri Kosina 
724dde5845aSJiri Kosina 	if ((end - start) <= 0)
725dde5845aSJiri Kosina 		return NULL;
726dde5845aSJiri Kosina 
727dde5845aSJiri Kosina 	b = *start++;
728dde5845aSJiri Kosina 
729dde5845aSJiri Kosina 	item->type = (b >> 2) & 3;
730dde5845aSJiri Kosina 	item->tag  = (b >> 4) & 15;
731dde5845aSJiri Kosina 
732dde5845aSJiri Kosina 	if (item->tag == HID_ITEM_TAG_LONG) {
733dde5845aSJiri Kosina 
734dde5845aSJiri Kosina 		item->format = HID_ITEM_FORMAT_LONG;
735dde5845aSJiri Kosina 
736dde5845aSJiri Kosina 		if ((end - start) < 2)
737dde5845aSJiri Kosina 			return NULL;
738dde5845aSJiri Kosina 
739dde5845aSJiri Kosina 		item->size = *start++;
740dde5845aSJiri Kosina 		item->tag  = *start++;
741dde5845aSJiri Kosina 
742dde5845aSJiri Kosina 		if ((end - start) < item->size)
743dde5845aSJiri Kosina 			return NULL;
744dde5845aSJiri Kosina 
745dde5845aSJiri Kosina 		item->data.longdata = start;
746dde5845aSJiri Kosina 		start += item->size;
747dde5845aSJiri Kosina 		return start;
748dde5845aSJiri Kosina 	}
749dde5845aSJiri Kosina 
750dde5845aSJiri Kosina 	item->format = HID_ITEM_FORMAT_SHORT;
751dde5845aSJiri Kosina 	item->size = b & 3;
752dde5845aSJiri Kosina 
753dde5845aSJiri Kosina 	switch (item->size) {
754dde5845aSJiri Kosina 	case 0:
755dde5845aSJiri Kosina 		return start;
756dde5845aSJiri Kosina 
757dde5845aSJiri Kosina 	case 1:
758dde5845aSJiri Kosina 		if ((end - start) < 1)
759dde5845aSJiri Kosina 			return NULL;
760dde5845aSJiri Kosina 		item->data.u8 = *start++;
761dde5845aSJiri Kosina 		return start;
762dde5845aSJiri Kosina 
763dde5845aSJiri Kosina 	case 2:
764dde5845aSJiri Kosina 		if ((end - start) < 2)
765dde5845aSJiri Kosina 			return NULL;
766c105068fSHarvey Harrison 		item->data.u16 = get_unaligned_le16(start);
767dde5845aSJiri Kosina 		start = (__u8 *)((__le16 *)start + 1);
768dde5845aSJiri Kosina 		return start;
769dde5845aSJiri Kosina 
770dde5845aSJiri Kosina 	case 3:
771dde5845aSJiri Kosina 		item->size++;
772dde5845aSJiri Kosina 		if ((end - start) < 4)
773dde5845aSJiri Kosina 			return NULL;
774c105068fSHarvey Harrison 		item->data.u32 = get_unaligned_le32(start);
775dde5845aSJiri Kosina 		start = (__u8 *)((__le32 *)start + 1);
776dde5845aSJiri Kosina 		return start;
777dde5845aSJiri Kosina 	}
778dde5845aSJiri Kosina 
779dde5845aSJiri Kosina 	return NULL;
780dde5845aSJiri Kosina }
781dde5845aSJiri Kosina 
7823dc8fc08SBenjamin Tissoires static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
783734c6609SHenrik Rydberg {
7843dc8fc08SBenjamin Tissoires 	struct hid_device *hid = parser->device;
7853dc8fc08SBenjamin Tissoires 
7864fa3a583SHenrik Rydberg 	if (usage == HID_DG_CONTACTID)
7874fa3a583SHenrik Rydberg 		hid->group = HID_GROUP_MULTITOUCH;
788734c6609SHenrik Rydberg }
789734c6609SHenrik Rydberg 
790f961bd35SBenjamin Tissoires static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
791f961bd35SBenjamin Tissoires {
792f961bd35SBenjamin Tissoires 	if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
793f961bd35SBenjamin Tissoires 	    parser->global.report_size == 8)
794f961bd35SBenjamin Tissoires 		parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
7952dbc6f11SBlaž Hrastnik 
7962dbc6f11SBlaž Hrastnik 	if (usage == 0xff0000c6 && parser->global.report_count == 1 &&
7972dbc6f11SBlaž Hrastnik 	    parser->global.report_size == 8)
7982dbc6f11SBlaž Hrastnik 		parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
799f961bd35SBenjamin Tissoires }
800f961bd35SBenjamin Tissoires 
8013dc8fc08SBenjamin Tissoires static void hid_scan_collection(struct hid_parser *parser, unsigned type)
8023dc8fc08SBenjamin Tissoires {
8033dc8fc08SBenjamin Tissoires 	struct hid_device *hid = parser->device;
804e39f2d59SAndrew Duggan 	int i;
8053dc8fc08SBenjamin Tissoires 
8063dc8fc08SBenjamin Tissoires 	if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
8073dc8fc08SBenjamin Tissoires 	    type == HID_COLLECTION_PHYSICAL)
8083dc8fc08SBenjamin Tissoires 		hid->group = HID_GROUP_SENSOR_HUB;
809be3b1634SAlan Wu 
810be3b1634SAlan Wu 	if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
8110a76ac80SDennis Chen 	    hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
812be3b1634SAlan Wu 	    hid->group == HID_GROUP_MULTITOUCH)
813be3b1634SAlan Wu 		hid->group = HID_GROUP_GENERIC;
814e39f2d59SAndrew Duggan 
815e39f2d59SAndrew Duggan 	if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
816e39f2d59SAndrew Duggan 		for (i = 0; i < parser->local.usage_index; i++)
817e39f2d59SAndrew Duggan 			if (parser->local.usage[i] == HID_GD_POINTER)
818e39f2d59SAndrew Duggan 				parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
819e39f2d59SAndrew Duggan 
820e39f2d59SAndrew Duggan 	if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
821e39f2d59SAndrew Duggan 		parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
82214c9c014SSean O'Brien 
82314c9c014SSean O'Brien 	if ((parser->global.usage_page << 16) == HID_UP_GOOGLEVENDOR)
82414c9c014SSean O'Brien 		for (i = 0; i < parser->local.usage_index; i++)
82514c9c014SSean O'Brien 			if (parser->local.usage[i] ==
82614c9c014SSean O'Brien 					(HID_UP_GOOGLEVENDOR | 0x0001))
82714c9c014SSean O'Brien 				parser->device->group =
82814c9c014SSean O'Brien 					HID_GROUP_VIVALDI;
8293dc8fc08SBenjamin Tissoires }
8303dc8fc08SBenjamin Tissoires 
8313dc8fc08SBenjamin Tissoires static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
8323dc8fc08SBenjamin Tissoires {
8333dc8fc08SBenjamin Tissoires 	__u32 data;
8343dc8fc08SBenjamin Tissoires 	int i;
8353dc8fc08SBenjamin Tissoires 
8361cb0d2aeSCandle Sun 	hid_concatenate_last_usage_page(parser);
83758e75155SNicolas Saenz Julienne 
8383dc8fc08SBenjamin Tissoires 	data = item_udata(item);
8393dc8fc08SBenjamin Tissoires 
8403dc8fc08SBenjamin Tissoires 	switch (item->tag) {
8413dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
8423dc8fc08SBenjamin Tissoires 		hid_scan_collection(parser, data & 0xff);
8433dc8fc08SBenjamin Tissoires 		break;
8443dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
8453dc8fc08SBenjamin Tissoires 		break;
8463dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_INPUT:
847e24d0d39SBenjamin Tissoires 		/* ignore constant inputs, they will be ignored by hid-input */
848e24d0d39SBenjamin Tissoires 		if (data & HID_MAIN_ITEM_CONSTANT)
849e24d0d39SBenjamin Tissoires 			break;
8503dc8fc08SBenjamin Tissoires 		for (i = 0; i < parser->local.usage_index; i++)
8513dc8fc08SBenjamin Tissoires 			hid_scan_input_usage(parser, parser->local.usage[i]);
8523dc8fc08SBenjamin Tissoires 		break;
8533dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_OUTPUT:
8543dc8fc08SBenjamin Tissoires 		break;
8553dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_FEATURE:
856f961bd35SBenjamin Tissoires 		for (i = 0; i < parser->local.usage_index; i++)
857f961bd35SBenjamin Tissoires 			hid_scan_feature_usage(parser, parser->local.usage[i]);
8583dc8fc08SBenjamin Tissoires 		break;
8593dc8fc08SBenjamin Tissoires 	}
8603dc8fc08SBenjamin Tissoires 
8613dc8fc08SBenjamin Tissoires 	/* Reset the local parser environment */
8623dc8fc08SBenjamin Tissoires 	memset(&parser->local, 0, sizeof(parser->local));
8633dc8fc08SBenjamin Tissoires 
8643dc8fc08SBenjamin Tissoires 	return 0;
8653dc8fc08SBenjamin Tissoires }
8663dc8fc08SBenjamin Tissoires 
867734c6609SHenrik Rydberg /*
868734c6609SHenrik Rydberg  * Scan a report descriptor before the device is added to the bus.
869734c6609SHenrik Rydberg  * Sets device groups and other properties that determine what driver
870734c6609SHenrik Rydberg  * to load.
871734c6609SHenrik Rydberg  */
872734c6609SHenrik Rydberg static int hid_scan_report(struct hid_device *hid)
873734c6609SHenrik Rydberg {
8743dc8fc08SBenjamin Tissoires 	struct hid_parser *parser;
8753dc8fc08SBenjamin Tissoires 	struct hid_item item;
876734c6609SHenrik Rydberg 	__u8 *start = hid->dev_rdesc;
877734c6609SHenrik Rydberg 	__u8 *end = start + hid->dev_rsize;
8783dc8fc08SBenjamin Tissoires 	static int (*dispatch_type[])(struct hid_parser *parser,
8793dc8fc08SBenjamin Tissoires 				      struct hid_item *item) = {
8803dc8fc08SBenjamin Tissoires 		hid_scan_main,
8813dc8fc08SBenjamin Tissoires 		hid_parser_global,
8823dc8fc08SBenjamin Tissoires 		hid_parser_local,
8833dc8fc08SBenjamin Tissoires 		hid_parser_reserved
8843dc8fc08SBenjamin Tissoires 	};
885734c6609SHenrik Rydberg 
8863dc8fc08SBenjamin Tissoires 	parser = vzalloc(sizeof(struct hid_parser));
8873dc8fc08SBenjamin Tissoires 	if (!parser)
8883dc8fc08SBenjamin Tissoires 		return -ENOMEM;
8893dc8fc08SBenjamin Tissoires 
8903dc8fc08SBenjamin Tissoires 	parser->device = hid;
891734c6609SHenrik Rydberg 	hid->group = HID_GROUP_GENERIC;
892734c6609SHenrik Rydberg 
8933dc8fc08SBenjamin Tissoires 	/*
8943dc8fc08SBenjamin Tissoires 	 * The parsing is simpler than the one in hid_open_report() as we should
8953dc8fc08SBenjamin Tissoires 	 * be robust against hid errors. Those errors will be raised by
8963dc8fc08SBenjamin Tissoires 	 * hid_open_report() anyway.
8973dc8fc08SBenjamin Tissoires 	 */
8983dc8fc08SBenjamin Tissoires 	while ((start = fetch_item(start, end, &item)) != NULL)
8993dc8fc08SBenjamin Tissoires 		dispatch_type[item.type](parser, &item);
9003dc8fc08SBenjamin Tissoires 
901f961bd35SBenjamin Tissoires 	/*
902f961bd35SBenjamin Tissoires 	 * Handle special flags set during scanning.
903f961bd35SBenjamin Tissoires 	 */
904f961bd35SBenjamin Tissoires 	if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
905f961bd35SBenjamin Tissoires 	    (hid->group == HID_GROUP_MULTITOUCH))
906f961bd35SBenjamin Tissoires 		hid->group = HID_GROUP_MULTITOUCH_WIN_8;
907f961bd35SBenjamin Tissoires 
908ba391e5aSBenjamin Tissoires 	/*
909ba391e5aSBenjamin Tissoires 	 * Vendor specific handlings
910ba391e5aSBenjamin Tissoires 	 */
91129b47391SBenjamin Tissoires 	switch (hid->vendor) {
91229b47391SBenjamin Tissoires 	case USB_VENDOR_ID_WACOM:
91329b47391SBenjamin Tissoires 		hid->group = HID_GROUP_WACOM;
91429b47391SBenjamin Tissoires 		break;
915c241c5eeSBenjamin Tissoires 	case USB_VENDOR_ID_SYNAPTICS:
91684379d83SJiri Kosina 		if (hid->group == HID_GROUP_GENERIC)
917e39f2d59SAndrew Duggan 			if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
918e39f2d59SAndrew Duggan 			    && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
919e39f2d59SAndrew Duggan 				/*
920e39f2d59SAndrew Duggan 				 * hid-rmi should take care of them,
921e39f2d59SAndrew Duggan 				 * not hid-generic
922e39f2d59SAndrew Duggan 				 */
923c241c5eeSBenjamin Tissoires 				hid->group = HID_GROUP_RMI;
924c241c5eeSBenjamin Tissoires 		break;
92529b47391SBenjamin Tissoires 	}
92629b47391SBenjamin Tissoires 
92708a8a7cfSBenjamin Tissoires 	kfree(parser->collection_stack);
9283dc8fc08SBenjamin Tissoires 	vfree(parser);
929734c6609SHenrik Rydberg 	return 0;
930734c6609SHenrik Rydberg }
931734c6609SHenrik Rydberg 
93285cdaf52SJiri Slaby /**
93385cdaf52SJiri Slaby  * hid_parse_report - parse device report
93485cdaf52SJiri Slaby  *
9355f94e9c8SXiaofei Tan  * @hid: hid device
93685cdaf52SJiri Slaby  * @start: report start
93785cdaf52SJiri Slaby  * @size: report size
93885cdaf52SJiri Slaby  *
939a7197c2eSHenrik Rydberg  * Allocate the device report as read by the bus driver. This function should
940a7197c2eSHenrik Rydberg  * only be called from parse() in ll drivers.
941a7197c2eSHenrik Rydberg  */
942a7197c2eSHenrik Rydberg int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
943a7197c2eSHenrik Rydberg {
944a7197c2eSHenrik Rydberg 	hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
945a7197c2eSHenrik Rydberg 	if (!hid->dev_rdesc)
946a7197c2eSHenrik Rydberg 		return -ENOMEM;
947a7197c2eSHenrik Rydberg 	hid->dev_rsize = size;
948a7197c2eSHenrik Rydberg 	return 0;
949a7197c2eSHenrik Rydberg }
950a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_parse_report);
951a7197c2eSHenrik Rydberg 
952331415ffSKees Cook static const char * const hid_report_names[] = {
953331415ffSKees Cook 	"HID_INPUT_REPORT",
954331415ffSKees Cook 	"HID_OUTPUT_REPORT",
955331415ffSKees Cook 	"HID_FEATURE_REPORT",
956331415ffSKees Cook };
957331415ffSKees Cook /**
958331415ffSKees Cook  * hid_validate_values - validate existing device report's value indexes
959331415ffSKees Cook  *
9605f94e9c8SXiaofei Tan  * @hid: hid device
961331415ffSKees Cook  * @type: which report type to examine
962331415ffSKees Cook  * @id: which report ID to examine (0 for first)
963331415ffSKees Cook  * @field_index: which report field to examine
964331415ffSKees Cook  * @report_counts: expected number of values
965331415ffSKees Cook  *
966331415ffSKees Cook  * Validate the number of values in a given field of a given report, after
967331415ffSKees Cook  * parsing.
968331415ffSKees Cook  */
969331415ffSKees Cook struct hid_report *hid_validate_values(struct hid_device *hid,
970ead77b65SBenjamin Tissoires 				       enum hid_report_type type, unsigned int id,
971331415ffSKees Cook 				       unsigned int field_index,
972331415ffSKees Cook 				       unsigned int report_counts)
973331415ffSKees Cook {
974331415ffSKees Cook 	struct hid_report *report;
975331415ffSKees Cook 
976331415ffSKees Cook 	if (type > HID_FEATURE_REPORT) {
977331415ffSKees Cook 		hid_err(hid, "invalid HID report type %u\n", type);
978331415ffSKees Cook 		return NULL;
979331415ffSKees Cook 	}
980331415ffSKees Cook 
981331415ffSKees Cook 	if (id >= HID_MAX_IDS) {
982331415ffSKees Cook 		hid_err(hid, "invalid HID report id %u\n", id);
983331415ffSKees Cook 		return NULL;
984331415ffSKees Cook 	}
985331415ffSKees Cook 
986331415ffSKees Cook 	/*
987331415ffSKees Cook 	 * Explicitly not using hid_get_report() here since it depends on
988331415ffSKees Cook 	 * ->numbered being checked, which may not always be the case when
989331415ffSKees Cook 	 * drivers go to access report values.
990331415ffSKees Cook 	 */
9911b15d2e5SKees Cook 	if (id == 0) {
9921b15d2e5SKees Cook 		/*
9931b15d2e5SKees Cook 		 * Validating on id 0 means we should examine the first
9941b15d2e5SKees Cook 		 * report in the list.
9951b15d2e5SKees Cook 		 */
996b12fece4SPietro Borrello 		report = list_first_entry_or_null(
997b12fece4SPietro Borrello 				&hid->report_enum[type].report_list,
9981b15d2e5SKees Cook 				struct hid_report, list);
9991b15d2e5SKees Cook 	} else {
1000331415ffSKees Cook 		report = hid->report_enum[type].report_id_hash[id];
10011b15d2e5SKees Cook 	}
1002331415ffSKees Cook 	if (!report) {
1003331415ffSKees Cook 		hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
1004331415ffSKees Cook 		return NULL;
1005331415ffSKees Cook 	}
1006331415ffSKees Cook 	if (report->maxfield <= field_index) {
1007331415ffSKees Cook 		hid_err(hid, "not enough fields in %s %u\n",
1008331415ffSKees Cook 			hid_report_names[type], id);
1009331415ffSKees Cook 		return NULL;
1010331415ffSKees Cook 	}
1011331415ffSKees Cook 	if (report->field[field_index]->report_count < report_counts) {
1012331415ffSKees Cook 		hid_err(hid, "not enough values in %s %u field %u\n",
1013331415ffSKees Cook 			hid_report_names[type], id, field_index);
1014331415ffSKees Cook 		return NULL;
1015331415ffSKees Cook 	}
1016331415ffSKees Cook 	return report;
1017331415ffSKees Cook }
1018331415ffSKees Cook EXPORT_SYMBOL_GPL(hid_validate_values);
1019331415ffSKees Cook 
10205a4abb36SPeter Hutterer static int hid_calculate_multiplier(struct hid_device *hid,
10215a4abb36SPeter Hutterer 				     struct hid_field *multiplier)
10225a4abb36SPeter Hutterer {
10235a4abb36SPeter Hutterer 	int m;
10245a4abb36SPeter Hutterer 	__s32 v = *multiplier->value;
10255a4abb36SPeter Hutterer 	__s32 lmin = multiplier->logical_minimum;
10265a4abb36SPeter Hutterer 	__s32 lmax = multiplier->logical_maximum;
10275a4abb36SPeter Hutterer 	__s32 pmin = multiplier->physical_minimum;
10285a4abb36SPeter Hutterer 	__s32 pmax = multiplier->physical_maximum;
10295a4abb36SPeter Hutterer 
10305a4abb36SPeter Hutterer 	/*
10315a4abb36SPeter Hutterer 	 * "Because OS implementations will generally divide the control's
10325a4abb36SPeter Hutterer 	 * reported count by the Effective Resolution Multiplier, designers
10335a4abb36SPeter Hutterer 	 * should take care not to establish a potential Effective
10345a4abb36SPeter Hutterer 	 * Resolution Multiplier of zero."
10355a4abb36SPeter Hutterer 	 * HID Usage Table, v1.12, Section 4.3.1, p31
10365a4abb36SPeter Hutterer 	 */
10375a4abb36SPeter Hutterer 	if (lmax - lmin == 0)
10385a4abb36SPeter Hutterer 		return 1;
10395a4abb36SPeter Hutterer 	/*
10405a4abb36SPeter Hutterer 	 * Handling the unit exponent is left as an exercise to whoever
10415a4abb36SPeter Hutterer 	 * finds a device where that exponent is not 0.
10425a4abb36SPeter Hutterer 	 */
10435a4abb36SPeter Hutterer 	m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin);
10445a4abb36SPeter Hutterer 	if (unlikely(multiplier->unit_exponent != 0)) {
10455a4abb36SPeter Hutterer 		hid_warn(hid,
10465a4abb36SPeter Hutterer 			 "unsupported Resolution Multiplier unit exponent %d\n",
10475a4abb36SPeter Hutterer 			 multiplier->unit_exponent);
10485a4abb36SPeter Hutterer 	}
10495a4abb36SPeter Hutterer 
10505a4abb36SPeter Hutterer 	/* There are no devices with an effective multiplier > 255 */
10515a4abb36SPeter Hutterer 	if (unlikely(m == 0 || m > 255 || m < -255)) {
10525a4abb36SPeter Hutterer 		hid_warn(hid, "unsupported Resolution Multiplier %d\n", m);
10535a4abb36SPeter Hutterer 		m = 1;
10545a4abb36SPeter Hutterer 	}
10555a4abb36SPeter Hutterer 
10565a4abb36SPeter Hutterer 	return m;
10575a4abb36SPeter Hutterer }
10585a4abb36SPeter Hutterer 
10595a4abb36SPeter Hutterer static void hid_apply_multiplier_to_field(struct hid_device *hid,
10605a4abb36SPeter Hutterer 					  struct hid_field *field,
10615a4abb36SPeter Hutterer 					  struct hid_collection *multiplier_collection,
10625a4abb36SPeter Hutterer 					  int effective_multiplier)
10635a4abb36SPeter Hutterer {
10645a4abb36SPeter Hutterer 	struct hid_collection *collection;
10655a4abb36SPeter Hutterer 	struct hid_usage *usage;
10665a4abb36SPeter Hutterer 	int i;
10675a4abb36SPeter Hutterer 
10685a4abb36SPeter Hutterer 	/*
10695a4abb36SPeter Hutterer 	 * If multiplier_collection is NULL, the multiplier applies
10705a4abb36SPeter Hutterer 	 * to all fields in the report.
10715a4abb36SPeter Hutterer 	 * Otherwise, it is the Logical Collection the multiplier applies to
10725a4abb36SPeter Hutterer 	 * but our field may be in a subcollection of that collection.
10735a4abb36SPeter Hutterer 	 */
10745a4abb36SPeter Hutterer 	for (i = 0; i < field->maxusage; i++) {
10755a4abb36SPeter Hutterer 		usage = &field->usage[i];
10765a4abb36SPeter Hutterer 
10775a4abb36SPeter Hutterer 		collection = &hid->collection[usage->collection_index];
1078ee46967fSPeter Hutterer 		while (collection->parent_idx != -1 &&
1079ee46967fSPeter Hutterer 		       collection != multiplier_collection)
1080ee46967fSPeter Hutterer 			collection = &hid->collection[collection->parent_idx];
10815a4abb36SPeter Hutterer 
1082ee46967fSPeter Hutterer 		if (collection->parent_idx != -1 ||
1083ee46967fSPeter Hutterer 		    multiplier_collection == NULL)
10845a4abb36SPeter Hutterer 			usage->resolution_multiplier = effective_multiplier;
10855a4abb36SPeter Hutterer 
10865a4abb36SPeter Hutterer 	}
10875a4abb36SPeter Hutterer }
10885a4abb36SPeter Hutterer 
10895a4abb36SPeter Hutterer static void hid_apply_multiplier(struct hid_device *hid,
10905a4abb36SPeter Hutterer 				 struct hid_field *multiplier)
10915a4abb36SPeter Hutterer {
10925a4abb36SPeter Hutterer 	struct hid_report_enum *rep_enum;
10935a4abb36SPeter Hutterer 	struct hid_report *rep;
10945a4abb36SPeter Hutterer 	struct hid_field *field;
10955a4abb36SPeter Hutterer 	struct hid_collection *multiplier_collection;
10965a4abb36SPeter Hutterer 	int effective_multiplier;
10975a4abb36SPeter Hutterer 	int i;
10985a4abb36SPeter Hutterer 
10995a4abb36SPeter Hutterer 	/*
11005a4abb36SPeter Hutterer 	 * "The Resolution Multiplier control must be contained in the same
11015a4abb36SPeter Hutterer 	 * Logical Collection as the control(s) to which it is to be applied.
11025a4abb36SPeter Hutterer 	 * If no Resolution Multiplier is defined, then the Resolution
11035a4abb36SPeter Hutterer 	 * Multiplier defaults to 1.  If more than one control exists in a
11045a4abb36SPeter Hutterer 	 * Logical Collection, the Resolution Multiplier is associated with
11055a4abb36SPeter Hutterer 	 * all controls in the collection. If no Logical Collection is
11065a4abb36SPeter Hutterer 	 * defined, the Resolution Multiplier is associated with all
11075a4abb36SPeter Hutterer 	 * controls in the report."
11085a4abb36SPeter Hutterer 	 * HID Usage Table, v1.12, Section 4.3.1, p30
11095a4abb36SPeter Hutterer 	 *
11105a4abb36SPeter Hutterer 	 * Thus, search from the current collection upwards until we find a
11115a4abb36SPeter Hutterer 	 * logical collection. Then search all fields for that same parent
11125a4abb36SPeter Hutterer 	 * collection. Those are the fields the multiplier applies to.
11135a4abb36SPeter Hutterer 	 *
11145a4abb36SPeter Hutterer 	 * If we have more than one multiplier, it will overwrite the
11155a4abb36SPeter Hutterer 	 * applicable fields later.
11165a4abb36SPeter Hutterer 	 */
11175a4abb36SPeter Hutterer 	multiplier_collection = &hid->collection[multiplier->usage->collection_index];
1118ee46967fSPeter Hutterer 	while (multiplier_collection->parent_idx != -1 &&
11195a4abb36SPeter Hutterer 	       multiplier_collection->type != HID_COLLECTION_LOGICAL)
1120ee46967fSPeter Hutterer 		multiplier_collection = &hid->collection[multiplier_collection->parent_idx];
11215a4abb36SPeter Hutterer 
11225a4abb36SPeter Hutterer 	effective_multiplier = hid_calculate_multiplier(hid, multiplier);
11235a4abb36SPeter Hutterer 
11245a4abb36SPeter Hutterer 	rep_enum = &hid->report_enum[HID_INPUT_REPORT];
11255a4abb36SPeter Hutterer 	list_for_each_entry(rep, &rep_enum->report_list, list) {
11265a4abb36SPeter Hutterer 		for (i = 0; i < rep->maxfield; i++) {
11275a4abb36SPeter Hutterer 			field = rep->field[i];
11285a4abb36SPeter Hutterer 			hid_apply_multiplier_to_field(hid, field,
11295a4abb36SPeter Hutterer 						      multiplier_collection,
11305a4abb36SPeter Hutterer 						      effective_multiplier);
11315a4abb36SPeter Hutterer 		}
11325a4abb36SPeter Hutterer 	}
11335a4abb36SPeter Hutterer }
11345a4abb36SPeter Hutterer 
11355a4abb36SPeter Hutterer /*
11365a4abb36SPeter Hutterer  * hid_setup_resolution_multiplier - set up all resolution multipliers
11375a4abb36SPeter Hutterer  *
11385a4abb36SPeter Hutterer  * @device: hid device
11395a4abb36SPeter Hutterer  *
11405a4abb36SPeter Hutterer  * Search for all Resolution Multiplier Feature Reports and apply their
11415a4abb36SPeter Hutterer  * value to all matching Input items. This only updates the internal struct
11425a4abb36SPeter Hutterer  * fields.
11435a4abb36SPeter Hutterer  *
11445a4abb36SPeter Hutterer  * The Resolution Multiplier is applied by the hardware. If the multiplier
11455a4abb36SPeter Hutterer  * is anything other than 1, the hardware will send pre-multiplied events
11465a4abb36SPeter Hutterer  * so that the same physical interaction generates an accumulated
11475a4abb36SPeter Hutterer  *	accumulated_value = value * * multiplier
11485a4abb36SPeter Hutterer  * This may be achieved by sending
11495a4abb36SPeter Hutterer  * - "value * multiplier" for each event, or
11505a4abb36SPeter Hutterer  * - "value" but "multiplier" times as frequently, or
11515a4abb36SPeter Hutterer  * - a combination of the above
11525a4abb36SPeter Hutterer  * The only guarantee is that the same physical interaction always generates
11535a4abb36SPeter Hutterer  * an accumulated 'value * multiplier'.
11545a4abb36SPeter Hutterer  *
11555a4abb36SPeter Hutterer  * This function must be called before any event processing and after
11565a4abb36SPeter Hutterer  * any SetRequest to the Resolution Multiplier.
11575a4abb36SPeter Hutterer  */
11585a4abb36SPeter Hutterer void hid_setup_resolution_multiplier(struct hid_device *hid)
11595a4abb36SPeter Hutterer {
11605a4abb36SPeter Hutterer 	struct hid_report_enum *rep_enum;
11615a4abb36SPeter Hutterer 	struct hid_report *rep;
11625a4abb36SPeter Hutterer 	struct hid_usage *usage;
11635a4abb36SPeter Hutterer 	int i, j;
11645a4abb36SPeter Hutterer 
11655a4abb36SPeter Hutterer 	rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
11665a4abb36SPeter Hutterer 	list_for_each_entry(rep, &rep_enum->report_list, list) {
11675a4abb36SPeter Hutterer 		for (i = 0; i < rep->maxfield; i++) {
11685a4abb36SPeter Hutterer 			/* Ignore if report count is out of bounds. */
11695a4abb36SPeter Hutterer 			if (rep->field[i]->report_count < 1)
11705a4abb36SPeter Hutterer 				continue;
11715a4abb36SPeter Hutterer 
11725a4abb36SPeter Hutterer 			for (j = 0; j < rep->field[i]->maxusage; j++) {
11735a4abb36SPeter Hutterer 				usage = &rep->field[i]->usage[j];
11745a4abb36SPeter Hutterer 				if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER)
11755a4abb36SPeter Hutterer 					hid_apply_multiplier(hid,
11765a4abb36SPeter Hutterer 							     rep->field[i]);
11775a4abb36SPeter Hutterer 			}
11785a4abb36SPeter Hutterer 		}
11795a4abb36SPeter Hutterer 	}
11805a4abb36SPeter Hutterer }
11815a4abb36SPeter Hutterer EXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier);
11825a4abb36SPeter Hutterer 
1183a7197c2eSHenrik Rydberg /**
1184a7197c2eSHenrik Rydberg  * hid_open_report - open a driver-specific device report
1185a7197c2eSHenrik Rydberg  *
1186a7197c2eSHenrik Rydberg  * @device: hid device
1187a7197c2eSHenrik Rydberg  *
1188dde5845aSJiri Kosina  * Parse a report description into a hid_device structure. Reports are
1189dde5845aSJiri Kosina  * enumerated, fields are attached to these reports.
119085cdaf52SJiri Slaby  * 0 returned on success, otherwise nonzero error value.
1191a7197c2eSHenrik Rydberg  *
1192a7197c2eSHenrik Rydberg  * This function (or the equivalent hid_parse() macro) should only be
1193a7197c2eSHenrik Rydberg  * called from probe() in drivers, before starting the device.
1194dde5845aSJiri Kosina  */
1195a7197c2eSHenrik Rydberg int hid_open_report(struct hid_device *device)
1196dde5845aSJiri Kosina {
1197dde5845aSJiri Kosina 	struct hid_parser *parser;
1198dde5845aSJiri Kosina 	struct hid_item item;
1199a7197c2eSHenrik Rydberg 	unsigned int size;
1200a7197c2eSHenrik Rydberg 	__u8 *start;
120186e6b77eSKevin Daughtridge 	__u8 *buf;
1202dde5845aSJiri Kosina 	__u8 *end;
1203b3a81c77SMichał Mirosław 	__u8 *next;
120485cdaf52SJiri Slaby 	int ret;
1205*ea427a22SXin Zhao 	int i;
1206dde5845aSJiri Kosina 	static int (*dispatch_type[])(struct hid_parser *parser,
1207dde5845aSJiri Kosina 				      struct hid_item *item) = {
1208dde5845aSJiri Kosina 		hid_parser_main,
1209dde5845aSJiri Kosina 		hid_parser_global,
1210dde5845aSJiri Kosina 		hid_parser_local,
1211dde5845aSJiri Kosina 		hid_parser_reserved
1212dde5845aSJiri Kosina 	};
1213dde5845aSJiri Kosina 
1214a7197c2eSHenrik Rydberg 	if (WARN_ON(device->status & HID_STAT_PARSED))
1215a7197c2eSHenrik Rydberg 		return -EBUSY;
1216a7197c2eSHenrik Rydberg 
1217a7197c2eSHenrik Rydberg 	start = device->dev_rdesc;
1218a7197c2eSHenrik Rydberg 	if (WARN_ON(!start))
1219a7197c2eSHenrik Rydberg 		return -ENODEV;
1220a7197c2eSHenrik Rydberg 	size = device->dev_rsize;
1221a7197c2eSHenrik Rydberg 
122286e6b77eSKevin Daughtridge 	buf = kmemdup(start, size, GFP_KERNEL);
122386e6b77eSKevin Daughtridge 	if (buf == NULL)
122485cdaf52SJiri Slaby 		return -ENOMEM;
122586e6b77eSKevin Daughtridge 
122686e6b77eSKevin Daughtridge 	if (device->driver->report_fixup)
122786e6b77eSKevin Daughtridge 		start = device->driver->report_fixup(device, buf, &size);
122886e6b77eSKevin Daughtridge 	else
122986e6b77eSKevin Daughtridge 		start = buf;
123086e6b77eSKevin Daughtridge 
123186e6b77eSKevin Daughtridge 	start = kmemdup(start, size, GFP_KERNEL);
123286e6b77eSKevin Daughtridge 	kfree(buf);
123386e6b77eSKevin Daughtridge 	if (start == NULL)
123486e6b77eSKevin Daughtridge 		return -ENOMEM;
123586e6b77eSKevin Daughtridge 
123686e6b77eSKevin Daughtridge 	device->rdesc = start;
1237dde5845aSJiri Kosina 	device->rsize = size;
1238dde5845aSJiri Kosina 
1239fe258020SJoe Perches 	parser = vzalloc(sizeof(struct hid_parser));
124085cdaf52SJiri Slaby 	if (!parser) {
124185cdaf52SJiri Slaby 		ret = -ENOMEM;
1242b034ed50SGustavo A. R. Silva 		goto alloc_err;
1243dde5845aSJiri Kosina 	}
124485cdaf52SJiri Slaby 
1245dde5845aSJiri Kosina 	parser->device = device;
1246dde5845aSJiri Kosina 
1247dde5845aSJiri Kosina 	end = start + size;
1248a7197c2eSHenrik Rydberg 
1249a7197c2eSHenrik Rydberg 	device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1250a7197c2eSHenrik Rydberg 				     sizeof(struct hid_collection), GFP_KERNEL);
1251a7197c2eSHenrik Rydberg 	if (!device->collection) {
1252a7197c2eSHenrik Rydberg 		ret = -ENOMEM;
1253a7197c2eSHenrik Rydberg 		goto err;
1254a7197c2eSHenrik Rydberg 	}
1255a7197c2eSHenrik Rydberg 	device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1256*ea427a22SXin Zhao 	for (i = 0; i < HID_DEFAULT_NUM_COLLECTIONS; i++)
1257*ea427a22SXin Zhao 		device->collection[i].parent_idx = -1;
1258a7197c2eSHenrik Rydberg 
125985cdaf52SJiri Slaby 	ret = -EINVAL;
1260b3a81c77SMichał Mirosław 	while ((next = fetch_item(start, end, &item)) != NULL) {
1261b3a81c77SMichał Mirosław 		start = next;
1262dde5845aSJiri Kosina 
1263dde5845aSJiri Kosina 		if (item.format != HID_ITEM_FORMAT_SHORT) {
12648c3d52fcSJiri Kosina 			hid_err(device, "unexpected long global item\n");
126585cdaf52SJiri Slaby 			goto err;
1266dde5845aSJiri Kosina 		}
1267dde5845aSJiri Kosina 
1268dde5845aSJiri Kosina 		if (dispatch_type[item.type](parser, &item)) {
12698c3d52fcSJiri Kosina 			hid_err(device, "item %u %u %u %u parsing failed\n",
12704291ee30SJoe Perches 				item.format, (unsigned)item.size,
12714291ee30SJoe Perches 				(unsigned)item.type, (unsigned)item.tag);
127285cdaf52SJiri Slaby 			goto err;
1273dde5845aSJiri Kosina 		}
1274dde5845aSJiri Kosina 
1275dde5845aSJiri Kosina 		if (start == end) {
1276dde5845aSJiri Kosina 			if (parser->collection_stack_ptr) {
12778c3d52fcSJiri Kosina 				hid_err(device, "unbalanced collection at end of report description\n");
127885cdaf52SJiri Slaby 				goto err;
1279dde5845aSJiri Kosina 			}
1280dde5845aSJiri Kosina 			if (parser->local.delimiter_depth) {
12818c3d52fcSJiri Kosina 				hid_err(device, "unbalanced delimiter at end of report description\n");
128285cdaf52SJiri Slaby 				goto err;
1283dde5845aSJiri Kosina 			}
12845a4abb36SPeter Hutterer 
12855a4abb36SPeter Hutterer 			/*
12865a4abb36SPeter Hutterer 			 * fetch initial values in case the device's
12875a4abb36SPeter Hutterer 			 * default multiplier isn't the recommended 1
12885a4abb36SPeter Hutterer 			 */
12895a4abb36SPeter Hutterer 			hid_setup_resolution_multiplier(device);
12905a4abb36SPeter Hutterer 
1291b2dd9f2eSStefan Agner 			kfree(parser->collection_stack);
129247a80edbSJiri Kosina 			vfree(parser);
1293a7197c2eSHenrik Rydberg 			device->status |= HID_STAT_PARSED;
12945a4abb36SPeter Hutterer 
129585cdaf52SJiri Slaby 			return 0;
1296dde5845aSJiri Kosina 		}
1297dde5845aSJiri Kosina 	}
1298dde5845aSJiri Kosina 
1299b3a81c77SMichał Mirosław 	hid_err(device, "item fetching failed at offset %u/%u\n",
1300b3a81c77SMichał Mirosław 		size - (unsigned int)(end - start), size);
130185cdaf52SJiri Slaby err:
1302b2dd9f2eSStefan Agner 	kfree(parser->collection_stack);
1303b034ed50SGustavo A. R. Silva alloc_err:
130447a80edbSJiri Kosina 	vfree(parser);
1305a7197c2eSHenrik Rydberg 	hid_close_report(device);
130685cdaf52SJiri Slaby 	return ret;
1307dde5845aSJiri Kosina }
1308a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_open_report);
1309dde5845aSJiri Kosina 
1310dde5845aSJiri Kosina /*
1311dde5845aSJiri Kosina  * Convert a signed n-bit integer to signed 32-bit integer. Common
1312dde5845aSJiri Kosina  * cases are done through the compiler, the screwed things has to be
1313dde5845aSJiri Kosina  * done by hand.
1314dde5845aSJiri Kosina  */
1315dde5845aSJiri Kosina 
1316dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n)
1317dde5845aSJiri Kosina {
1318a0312af1SRandy Dunlap 	if (!value || !n)
1319a0312af1SRandy Dunlap 		return 0;
1320a0312af1SRandy Dunlap 
1321ec61b419SZhangPeng 	if (n > 32)
1322ec61b419SZhangPeng 		n = 32;
1323ec61b419SZhangPeng 
1324dde5845aSJiri Kosina 	switch (n) {
1325dde5845aSJiri Kosina 	case 8:  return ((__s8)value);
1326dde5845aSJiri Kosina 	case 16: return ((__s16)value);
1327dde5845aSJiri Kosina 	case 32: return ((__s32)value);
1328dde5845aSJiri Kosina 	}
132908585e43SAndy Shevchenko 	return value & (1 << (n - 1)) ? value | (~0U << n) : value;
1330dde5845aSJiri Kosina }
1331dde5845aSJiri Kosina 
133277463838SBenjamin Tissoires s32 hid_snto32(__u32 value, unsigned n)
133377463838SBenjamin Tissoires {
133477463838SBenjamin Tissoires 	return snto32(value, n);
133577463838SBenjamin Tissoires }
133677463838SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_snto32);
133777463838SBenjamin Tissoires 
1338dde5845aSJiri Kosina /*
1339dde5845aSJiri Kosina  * Convert a signed 32-bit integer to a signed n-bit integer.
1340dde5845aSJiri Kosina  */
1341dde5845aSJiri Kosina 
1342dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n)
1343dde5845aSJiri Kosina {
1344dde5845aSJiri Kosina 	s32 a = value >> (n - 1);
1345dde5845aSJiri Kosina 	if (a && a != -1)
1346dde5845aSJiri Kosina 		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1347dde5845aSJiri Kosina 	return value & ((1 << n) - 1);
1348dde5845aSJiri Kosina }
1349dde5845aSJiri Kosina 
1350dde5845aSJiri Kosina /*
1351dde5845aSJiri Kosina  * Extract/implement a data field from/to a little endian report (bit array).
1352dde5845aSJiri Kosina  *
1353dde5845aSJiri Kosina  * Code sort-of follows HID spec:
13545137b354SDmitry Torokhov  *     http://www.usb.org/developers/hidpage/HID1_11.pdf
1355dde5845aSJiri Kosina  *
1356dde5845aSJiri Kosina  * While the USB HID spec allows unlimited length bit fields in "report
1357dde5845aSJiri Kosina  * descriptors", most devices never use more than 16 bits.
1358dde5845aSJiri Kosina  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1359dde5845aSJiri Kosina  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1360dde5845aSJiri Kosina  */
1361dde5845aSJiri Kosina 
13625137b354SDmitry Torokhov static u32 __extract(u8 *report, unsigned offset, int n)
13635137b354SDmitry Torokhov {
13645137b354SDmitry Torokhov 	unsigned int idx = offset / 8;
13655137b354SDmitry Torokhov 	unsigned int bit_nr = 0;
13665137b354SDmitry Torokhov 	unsigned int bit_shift = offset % 8;
13675137b354SDmitry Torokhov 	int bits_to_copy = 8 - bit_shift;
13685137b354SDmitry Torokhov 	u32 value = 0;
13695137b354SDmitry Torokhov 	u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
13705137b354SDmitry Torokhov 
13715137b354SDmitry Torokhov 	while (n > 0) {
13725137b354SDmitry Torokhov 		value |= ((u32)report[idx] >> bit_shift) << bit_nr;
13735137b354SDmitry Torokhov 		n -= bits_to_copy;
13745137b354SDmitry Torokhov 		bit_nr += bits_to_copy;
13755137b354SDmitry Torokhov 		bits_to_copy = 8;
13765137b354SDmitry Torokhov 		bit_shift = 0;
13775137b354SDmitry Torokhov 		idx++;
13785137b354SDmitry Torokhov 	}
13795137b354SDmitry Torokhov 
13805137b354SDmitry Torokhov 	return value & mask;
13815137b354SDmitry Torokhov }
13825137b354SDmitry Torokhov 
13835137b354SDmitry Torokhov u32 hid_field_extract(const struct hid_device *hid, u8 *report,
13844291ee30SJoe Perches 			unsigned offset, unsigned n)
1385dde5845aSJiri Kosina {
138615fc1b5cSBenjamin Tissoires 	if (n > 32) {
13870af10eedSJoshua Clayton 		hid_warn_once(hid, "%s() called with n (%d) > 32! (%s)\n",
13880af10eedSJoshua Clayton 			      __func__, n, current->comm);
138915fc1b5cSBenjamin Tissoires 		n = 32;
13905137b354SDmitry Torokhov 	}
1391dde5845aSJiri Kosina 
13925137b354SDmitry Torokhov 	return __extract(report, offset, n);
1393dde5845aSJiri Kosina }
139404fba786SGoffredo Baroncelli EXPORT_SYMBOL_GPL(hid_field_extract);
1395dde5845aSJiri Kosina 
1396dde5845aSJiri Kosina /*
1397dde5845aSJiri Kosina  * "implement" : set bits in a little endian bit stream.
1398dde5845aSJiri Kosina  * Same concepts as "extract" (see comments above).
1399dde5845aSJiri Kosina  * The data mangled in the bit stream remains in little endian
1400dde5845aSJiri Kosina  * order the whole time. It make more sense to talk about
1401dde5845aSJiri Kosina  * endianness of register values by considering a register
14025137b354SDmitry Torokhov  * a "cached" copy of the little endian bit stream.
1403dde5845aSJiri Kosina  */
1404dde5845aSJiri Kosina 
14055137b354SDmitry Torokhov static void __implement(u8 *report, unsigned offset, int n, u32 value)
14065137b354SDmitry Torokhov {
14075137b354SDmitry Torokhov 	unsigned int idx = offset / 8;
14085137b354SDmitry Torokhov 	unsigned int bit_shift = offset % 8;
14095137b354SDmitry Torokhov 	int bits_to_set = 8 - bit_shift;
14105137b354SDmitry Torokhov 
14115137b354SDmitry Torokhov 	while (n - bits_to_set >= 0) {
141295d1c895SDmitry Torokhov 		report[idx] &= ~(0xff << bit_shift);
14135137b354SDmitry Torokhov 		report[idx] |= value << bit_shift;
14145137b354SDmitry Torokhov 		value >>= bits_to_set;
14155137b354SDmitry Torokhov 		n -= bits_to_set;
14165137b354SDmitry Torokhov 		bits_to_set = 8;
14175137b354SDmitry Torokhov 		bit_shift = 0;
14185137b354SDmitry Torokhov 		idx++;
14195137b354SDmitry Torokhov 	}
14205137b354SDmitry Torokhov 
14215137b354SDmitry Torokhov 	/* last nibble */
14225137b354SDmitry Torokhov 	if (n) {
142395d1c895SDmitry Torokhov 		u8 bit_mask = ((1U << n) - 1);
142495d1c895SDmitry Torokhov 		report[idx] &= ~(bit_mask << bit_shift);
142595d1c895SDmitry Torokhov 		report[idx] |= value << bit_shift;
14265137b354SDmitry Torokhov 	}
14275137b354SDmitry Torokhov }
14285137b354SDmitry Torokhov 
14295137b354SDmitry Torokhov static void implement(const struct hid_device *hid, u8 *report,
14305137b354SDmitry Torokhov 		      unsigned offset, unsigned n, u32 value)
14315137b354SDmitry Torokhov {
143295d1c895SDmitry Torokhov 	if (unlikely(n > 32)) {
14334291ee30SJoe Perches 		hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
14344291ee30SJoe Perches 			 __func__, n, current->comm);
14355137b354SDmitry Torokhov 		n = 32;
143695d1c895SDmitry Torokhov 	} else if (n < 32) {
143795d1c895SDmitry Torokhov 		u32 m = (1U << n) - 1;
1438dde5845aSJiri Kosina 
143995d1c895SDmitry Torokhov 		if (unlikely(value > m)) {
144095d1c895SDmitry Torokhov 			hid_warn(hid,
144195d1c895SDmitry Torokhov 				 "%s() called with too large value %d (n: %d)! (%s)\n",
144295d1c895SDmitry Torokhov 				 __func__, value, n, current->comm);
144395d1c895SDmitry Torokhov 			WARN_ON(1);
1444dde5845aSJiri Kosina 			value &= m;
144595d1c895SDmitry Torokhov 		}
144695d1c895SDmitry Torokhov 	}
1447dde5845aSJiri Kosina 
14485137b354SDmitry Torokhov 	__implement(report, offset, n, value);
1449dde5845aSJiri Kosina }
1450dde5845aSJiri Kosina 
1451dde5845aSJiri Kosina /*
1452dde5845aSJiri Kosina  * Search an array for a value.
1453dde5845aSJiri Kosina  */
1454dde5845aSJiri Kosina 
145516ee4cc8SJoe Perches static int search(__s32 *array, __s32 value, unsigned n)
1456dde5845aSJiri Kosina {
1457dde5845aSJiri Kosina 	while (n--) {
1458dde5845aSJiri Kosina 		if (*array++ == value)
1459dde5845aSJiri Kosina 			return 0;
1460dde5845aSJiri Kosina 	}
1461dde5845aSJiri Kosina 	return -1;
1462dde5845aSJiri Kosina }
1463dde5845aSJiri Kosina 
146485cdaf52SJiri Slaby /**
146585cdaf52SJiri Slaby  * hid_match_report - check if driver's raw_event should be called
146685cdaf52SJiri Slaby  *
146785cdaf52SJiri Slaby  * @hid: hid device
14685f94e9c8SXiaofei Tan  * @report: hid report to match against
146985cdaf52SJiri Slaby  *
147085cdaf52SJiri Slaby  * compare hid->driver->report_table->report_type to report->type
147185cdaf52SJiri Slaby  */
147285cdaf52SJiri Slaby static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1473dde5845aSJiri Kosina {
147485cdaf52SJiri Slaby 	const struct hid_report_id *id = hid->driver->report_table;
147585cdaf52SJiri Slaby 
147685cdaf52SJiri Slaby 	if (!id) /* NULL means all */
147785cdaf52SJiri Slaby 		return 1;
147885cdaf52SJiri Slaby 
147985cdaf52SJiri Slaby 	for (; id->report_type != HID_TERMINATOR; id++)
148085cdaf52SJiri Slaby 		if (id->report_type == HID_ANY_ID ||
148185cdaf52SJiri Slaby 				id->report_type == report->type)
148285cdaf52SJiri Slaby 			return 1;
148385cdaf52SJiri Slaby 	return 0;
148485cdaf52SJiri Slaby }
148585cdaf52SJiri Slaby 
148685cdaf52SJiri Slaby /**
148785cdaf52SJiri Slaby  * hid_match_usage - check if driver's event should be called
148885cdaf52SJiri Slaby  *
148985cdaf52SJiri Slaby  * @hid: hid device
149085cdaf52SJiri Slaby  * @usage: usage to match against
149185cdaf52SJiri Slaby  *
149285cdaf52SJiri Slaby  * compare hid->driver->usage_table->usage_{type,code} to
149385cdaf52SJiri Slaby  * usage->usage_{type,code}
149485cdaf52SJiri Slaby  */
149585cdaf52SJiri Slaby static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
149685cdaf52SJiri Slaby {
149785cdaf52SJiri Slaby 	const struct hid_usage_id *id = hid->driver->usage_table;
149885cdaf52SJiri Slaby 
149985cdaf52SJiri Slaby 	if (!id) /* NULL means all */
150085cdaf52SJiri Slaby 		return 1;
150185cdaf52SJiri Slaby 
150285cdaf52SJiri Slaby 	for (; id->usage_type != HID_ANY_ID - 1; id++)
150385cdaf52SJiri Slaby 		if ((id->usage_hid == HID_ANY_ID ||
150485cdaf52SJiri Slaby 				id->usage_hid == usage->hid) &&
150585cdaf52SJiri Slaby 				(id->usage_type == HID_ANY_ID ||
150685cdaf52SJiri Slaby 				id->usage_type == usage->type) &&
150785cdaf52SJiri Slaby 				(id->usage_code == HID_ANY_ID ||
150885cdaf52SJiri Slaby 				 id->usage_code == usage->code))
150985cdaf52SJiri Slaby 			return 1;
151085cdaf52SJiri Slaby 	return 0;
151185cdaf52SJiri Slaby }
151285cdaf52SJiri Slaby 
151385cdaf52SJiri Slaby static void hid_process_event(struct hid_device *hid, struct hid_field *field,
151485cdaf52SJiri Slaby 		struct hid_usage *usage, __s32 value, int interrupt)
151585cdaf52SJiri Slaby {
151685cdaf52SJiri Slaby 	struct hid_driver *hdrv = hid->driver;
151785cdaf52SJiri Slaby 	int ret;
151885cdaf52SJiri Slaby 
15199bfc8da0SHenrik Rydberg 	if (!list_empty(&hid->debug_list))
1520cd667ce2SJiri Kosina 		hid_dump_input(hid, usage, value);
152185cdaf52SJiri Slaby 
152285cdaf52SJiri Slaby 	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
152385cdaf52SJiri Slaby 		ret = hdrv->event(hid, field, usage, value);
152485cdaf52SJiri Slaby 		if (ret != 0) {
152585cdaf52SJiri Slaby 			if (ret < 0)
15268c3d52fcSJiri Kosina 				hid_err(hid, "%s's event failed with %d\n",
152785cdaf52SJiri Slaby 						hdrv->name, ret);
152885cdaf52SJiri Slaby 			return;
152985cdaf52SJiri Slaby 		}
153085cdaf52SJiri Slaby 	}
153185cdaf52SJiri Slaby 
1532dde5845aSJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1533dde5845aSJiri Kosina 		hidinput_hid_event(hid, field, usage, value);
1534aa938f79SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1535aa938f79SJiri Kosina 		hid->hiddev_hid_event(hid, field, usage, value);
1536dde5845aSJiri Kosina }
1537dde5845aSJiri Kosina 
1538dde5845aSJiri Kosina /*
153974acc277SBenjamin Tissoires  * Checks if the given value is valid within this field
154074acc277SBenjamin Tissoires  */
154174acc277SBenjamin Tissoires static inline int hid_array_value_is_valid(struct hid_field *field,
154274acc277SBenjamin Tissoires 					   __s32 value)
154374acc277SBenjamin Tissoires {
154474acc277SBenjamin Tissoires 	__s32 min = field->logical_minimum;
154574acc277SBenjamin Tissoires 
154674acc277SBenjamin Tissoires 	/*
154774acc277SBenjamin Tissoires 	 * Value needs to be between logical min and max, and
154874acc277SBenjamin Tissoires 	 * (value - min) is used as an index in the usage array.
154974acc277SBenjamin Tissoires 	 * This array is of size field->maxusage
155074acc277SBenjamin Tissoires 	 */
155174acc277SBenjamin Tissoires 	return value >= min &&
155274acc277SBenjamin Tissoires 	       value <= field->logical_maximum &&
155374acc277SBenjamin Tissoires 	       value - min < field->maxusage;
155474acc277SBenjamin Tissoires }
155574acc277SBenjamin Tissoires 
155674acc277SBenjamin Tissoires /*
1557b79c1abaSBenjamin Tissoires  * Fetch the field from the data. The field content is stored for next
1558b79c1abaSBenjamin Tissoires  * report processing (we do differential reporting to the layer).
1559dde5845aSJiri Kosina  */
1560b79c1abaSBenjamin Tissoires static void hid_input_fetch_field(struct hid_device *hid,
1561b79c1abaSBenjamin Tissoires 				  struct hid_field *field,
1562b79c1abaSBenjamin Tissoires 				  __u8 *data)
1563dde5845aSJiri Kosina {
1564dde5845aSJiri Kosina 	unsigned n;
1565dde5845aSJiri Kosina 	unsigned count = field->report_count;
1566dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
1567dde5845aSJiri Kosina 	unsigned size = field->report_size;
1568dde5845aSJiri Kosina 	__s32 min = field->logical_minimum;
1569dde5845aSJiri Kosina 	__s32 *value;
1570dde5845aSJiri Kosina 
15711c1813a7SBenjamin Tissoires 	value = field->new_value;
15721c1813a7SBenjamin Tissoires 	memset(value, 0, count * sizeof(__s32));
1573b79c1abaSBenjamin Tissoires 	field->ignored = false;
1574dde5845aSJiri Kosina 
1575dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1576dde5845aSJiri Kosina 
15774291ee30SJoe Perches 		value[n] = min < 0 ?
157804fba786SGoffredo Baroncelli 			snto32(hid_field_extract(hid, data, offset + n * size,
157904fba786SGoffredo Baroncelli 			       size), size) :
158004fba786SGoffredo Baroncelli 			hid_field_extract(hid, data, offset + n * size, size);
1581dde5845aSJiri Kosina 
15824291ee30SJoe Perches 		/* Ignore report if ErrorRollOver */
15834291ee30SJoe Perches 		if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
158474acc277SBenjamin Tissoires 		    hid_array_value_is_valid(field, value[n]) &&
1585b79c1abaSBenjamin Tissoires 		    field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) {
1586b79c1abaSBenjamin Tissoires 			field->ignored = true;
15871c1813a7SBenjamin Tissoires 			return;
1588dde5845aSJiri Kosina 		}
1589b79c1abaSBenjamin Tissoires 	}
1590b79c1abaSBenjamin Tissoires }
1591dde5845aSJiri Kosina 
1592b79c1abaSBenjamin Tissoires /*
1593b79c1abaSBenjamin Tissoires  * Process a received variable field.
1594b79c1abaSBenjamin Tissoires  */
1595dde5845aSJiri Kosina 
1596b79c1abaSBenjamin Tissoires static void hid_input_var_field(struct hid_device *hid,
1597b79c1abaSBenjamin Tissoires 				struct hid_field *field,
1598b79c1abaSBenjamin Tissoires 				int interrupt)
1599b79c1abaSBenjamin Tissoires {
1600b79c1abaSBenjamin Tissoires 	unsigned int count = field->report_count;
1601b79c1abaSBenjamin Tissoires 	__s32 *value = field->new_value;
1602b79c1abaSBenjamin Tissoires 	unsigned int n;
1603b79c1abaSBenjamin Tissoires 
1604b79c1abaSBenjamin Tissoires 	for (n = 0; n < count; n++)
160574acc277SBenjamin Tissoires 		hid_process_event(hid,
160674acc277SBenjamin Tissoires 				  field,
160774acc277SBenjamin Tissoires 				  &field->usage[n],
160874acc277SBenjamin Tissoires 				  value[n],
160974acc277SBenjamin Tissoires 				  interrupt);
1610b79c1abaSBenjamin Tissoires 
1611b79c1abaSBenjamin Tissoires 	memcpy(field->value, value, count * sizeof(__s32));
1612dde5845aSJiri Kosina }
1613dde5845aSJiri Kosina 
1614b79c1abaSBenjamin Tissoires /*
1615b79c1abaSBenjamin Tissoires  * Process a received array field. The field content is stored for
1616b79c1abaSBenjamin Tissoires  * next report processing (we do differential reporting to the layer).
1617b79c1abaSBenjamin Tissoires  */
1618b79c1abaSBenjamin Tissoires 
1619b79c1abaSBenjamin Tissoires static void hid_input_array_field(struct hid_device *hid,
1620b79c1abaSBenjamin Tissoires 				  struct hid_field *field,
1621b79c1abaSBenjamin Tissoires 				  int interrupt)
1622b79c1abaSBenjamin Tissoires {
1623b79c1abaSBenjamin Tissoires 	unsigned int n;
1624b79c1abaSBenjamin Tissoires 	unsigned int count = field->report_count;
1625b79c1abaSBenjamin Tissoires 	__s32 min = field->logical_minimum;
1626b79c1abaSBenjamin Tissoires 	__s32 *value;
1627b79c1abaSBenjamin Tissoires 
1628b79c1abaSBenjamin Tissoires 	value = field->new_value;
1629b79c1abaSBenjamin Tissoires 
1630b79c1abaSBenjamin Tissoires 	/* ErrorRollOver */
1631b79c1abaSBenjamin Tissoires 	if (field->ignored)
1632b79c1abaSBenjamin Tissoires 		return;
1633b79c1abaSBenjamin Tissoires 
1634b79c1abaSBenjamin Tissoires 	for (n = 0; n < count; n++) {
163574acc277SBenjamin Tissoires 		if (hid_array_value_is_valid(field, field->value[n]) &&
163674acc277SBenjamin Tissoires 		    search(value, field->value[n], count))
163774acc277SBenjamin Tissoires 			hid_process_event(hid,
163874acc277SBenjamin Tissoires 					  field,
163974acc277SBenjamin Tissoires 					  &field->usage[field->value[n] - min],
164074acc277SBenjamin Tissoires 					  0,
164174acc277SBenjamin Tissoires 					  interrupt);
1642dde5845aSJiri Kosina 
164374acc277SBenjamin Tissoires 		if (hid_array_value_is_valid(field, value[n]) &&
164474acc277SBenjamin Tissoires 		    search(field->value, value[n], count))
164574acc277SBenjamin Tissoires 			hid_process_event(hid,
164674acc277SBenjamin Tissoires 					  field,
164774acc277SBenjamin Tissoires 					  &field->usage[value[n] - min],
164874acc277SBenjamin Tissoires 					  1,
164974acc277SBenjamin Tissoires 					  interrupt);
1650dde5845aSJiri Kosina 	}
1651dde5845aSJiri Kosina 
1652dde5845aSJiri Kosina 	memcpy(field->value, value, count * sizeof(__s32));
1653dde5845aSJiri Kosina }
1654dde5845aSJiri Kosina 
1655dde5845aSJiri Kosina /*
1656b79c1abaSBenjamin Tissoires  * Analyse a received report, and fetch the data from it. The field
1657b79c1abaSBenjamin Tissoires  * content is stored for next report processing (we do differential
1658b79c1abaSBenjamin Tissoires  * reporting to the layer).
1659b79c1abaSBenjamin Tissoires  */
1660b79c1abaSBenjamin Tissoires static void hid_process_report(struct hid_device *hid,
1661b79c1abaSBenjamin Tissoires 			       struct hid_report *report,
1662b79c1abaSBenjamin Tissoires 			       __u8 *data,
1663b79c1abaSBenjamin Tissoires 			       int interrupt)
1664b79c1abaSBenjamin Tissoires {
1665b79c1abaSBenjamin Tissoires 	unsigned int a;
1666bebcc522SBenjamin Tissoires 	struct hid_field_entry *entry;
1667b79c1abaSBenjamin Tissoires 	struct hid_field *field;
1668b79c1abaSBenjamin Tissoires 
1669bebcc522SBenjamin Tissoires 	/* first retrieve all incoming values in data */
1670bebcc522SBenjamin Tissoires 	for (a = 0; a < report->maxfield; a++)
16714df4b0feSLukas Bulwahn 		hid_input_fetch_field(hid, report->field[a], data);
1672bebcc522SBenjamin Tissoires 
1673bebcc522SBenjamin Tissoires 	if (!list_empty(&report->field_entry_list)) {
1674bebcc522SBenjamin Tissoires 		/* INPUT_REPORT, we have a priority list of fields */
1675bebcc522SBenjamin Tissoires 		list_for_each_entry(entry,
1676bebcc522SBenjamin Tissoires 				    &report->field_entry_list,
1677bebcc522SBenjamin Tissoires 				    list) {
1678bebcc522SBenjamin Tissoires 			field = entry->field;
1679bebcc522SBenjamin Tissoires 
1680bebcc522SBenjamin Tissoires 			if (field->flags & HID_MAIN_ITEM_VARIABLE)
1681bebcc522SBenjamin Tissoires 				hid_process_event(hid,
1682bebcc522SBenjamin Tissoires 						  field,
1683bebcc522SBenjamin Tissoires 						  &field->usage[entry->index],
1684bebcc522SBenjamin Tissoires 						  field->new_value[entry->index],
1685bebcc522SBenjamin Tissoires 						  interrupt);
1686bebcc522SBenjamin Tissoires 			else
1687bebcc522SBenjamin Tissoires 				hid_input_array_field(hid, field, interrupt);
1688bebcc522SBenjamin Tissoires 		}
1689bebcc522SBenjamin Tissoires 
1690bebcc522SBenjamin Tissoires 		/* we need to do the memcpy at the end for var items */
1691b79c1abaSBenjamin Tissoires 		for (a = 0; a < report->maxfield; a++) {
1692b79c1abaSBenjamin Tissoires 			field = report->field[a];
1693b79c1abaSBenjamin Tissoires 
1694bebcc522SBenjamin Tissoires 			if (field->flags & HID_MAIN_ITEM_VARIABLE)
1695bebcc522SBenjamin Tissoires 				memcpy(field->value, field->new_value,
1696bebcc522SBenjamin Tissoires 				       field->report_count * sizeof(__s32));
1697bebcc522SBenjamin Tissoires 		}
1698bebcc522SBenjamin Tissoires 	} else {
1699bebcc522SBenjamin Tissoires 		/* FEATURE_REPORT, regular processing */
1700bebcc522SBenjamin Tissoires 		for (a = 0; a < report->maxfield; a++) {
1701bebcc522SBenjamin Tissoires 			field = report->field[a];
1702b79c1abaSBenjamin Tissoires 
1703b79c1abaSBenjamin Tissoires 			if (field->flags & HID_MAIN_ITEM_VARIABLE)
1704b79c1abaSBenjamin Tissoires 				hid_input_var_field(hid, field, interrupt);
1705b79c1abaSBenjamin Tissoires 			else
1706b79c1abaSBenjamin Tissoires 				hid_input_array_field(hid, field, interrupt);
1707b79c1abaSBenjamin Tissoires 		}
1708b79c1abaSBenjamin Tissoires 	}
1709bebcc522SBenjamin Tissoires }
1710b79c1abaSBenjamin Tissoires 
1711b79c1abaSBenjamin Tissoires /*
171222f4b026SBenjamin Tissoires  * Insert a given usage_index in a field in the list
171322f4b026SBenjamin Tissoires  * of processed usages in the report.
171422f4b026SBenjamin Tissoires  *
171522f4b026SBenjamin Tissoires  * The elements of lower priority score are processed
171622f4b026SBenjamin Tissoires  * first.
171722f4b026SBenjamin Tissoires  */
171822f4b026SBenjamin Tissoires static void __hid_insert_field_entry(struct hid_device *hid,
171922f4b026SBenjamin Tissoires 				     struct hid_report *report,
172022f4b026SBenjamin Tissoires 				     struct hid_field_entry *entry,
172122f4b026SBenjamin Tissoires 				     struct hid_field *field,
172222f4b026SBenjamin Tissoires 				     unsigned int usage_index)
172322f4b026SBenjamin Tissoires {
172422f4b026SBenjamin Tissoires 	struct hid_field_entry *next;
172522f4b026SBenjamin Tissoires 
172622f4b026SBenjamin Tissoires 	entry->field = field;
172722f4b026SBenjamin Tissoires 	entry->index = usage_index;
172822f4b026SBenjamin Tissoires 	entry->priority = field->usages_priorities[usage_index];
172922f4b026SBenjamin Tissoires 
173022f4b026SBenjamin Tissoires 	/* insert the element at the correct position */
173122f4b026SBenjamin Tissoires 	list_for_each_entry(next,
173222f4b026SBenjamin Tissoires 			    &report->field_entry_list,
173322f4b026SBenjamin Tissoires 			    list) {
173422f4b026SBenjamin Tissoires 		/*
173522f4b026SBenjamin Tissoires 		 * the priority of our element is strictly higher
173622f4b026SBenjamin Tissoires 		 * than the next one, insert it before
173722f4b026SBenjamin Tissoires 		 */
173822f4b026SBenjamin Tissoires 		if (entry->priority > next->priority) {
173922f4b026SBenjamin Tissoires 			list_add_tail(&entry->list, &next->list);
174022f4b026SBenjamin Tissoires 			return;
174122f4b026SBenjamin Tissoires 		}
174222f4b026SBenjamin Tissoires 	}
174322f4b026SBenjamin Tissoires 
174422f4b026SBenjamin Tissoires 	/* lowest priority score: insert at the end */
174522f4b026SBenjamin Tissoires 	list_add_tail(&entry->list, &report->field_entry_list);
174622f4b026SBenjamin Tissoires }
174722f4b026SBenjamin Tissoires 
174822f4b026SBenjamin Tissoires static void hid_report_process_ordering(struct hid_device *hid,
174922f4b026SBenjamin Tissoires 					struct hid_report *report)
175022f4b026SBenjamin Tissoires {
175122f4b026SBenjamin Tissoires 	struct hid_field *field;
175222f4b026SBenjamin Tissoires 	struct hid_field_entry *entries;
175322f4b026SBenjamin Tissoires 	unsigned int a, u, usages;
175422f4b026SBenjamin Tissoires 	unsigned int count = 0;
175522f4b026SBenjamin Tissoires 
175622f4b026SBenjamin Tissoires 	/* count the number of individual fields in the report */
175722f4b026SBenjamin Tissoires 	for (a = 0; a < report->maxfield; a++) {
175822f4b026SBenjamin Tissoires 		field = report->field[a];
175922f4b026SBenjamin Tissoires 
176022f4b026SBenjamin Tissoires 		if (field->flags & HID_MAIN_ITEM_VARIABLE)
176122f4b026SBenjamin Tissoires 			count += field->report_count;
176222f4b026SBenjamin Tissoires 		else
176322f4b026SBenjamin Tissoires 			count++;
176422f4b026SBenjamin Tissoires 	}
176522f4b026SBenjamin Tissoires 
176622f4b026SBenjamin Tissoires 	/* allocate the memory to process the fields */
176722f4b026SBenjamin Tissoires 	entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
176822f4b026SBenjamin Tissoires 	if (!entries)
176922f4b026SBenjamin Tissoires 		return;
177022f4b026SBenjamin Tissoires 
177122f4b026SBenjamin Tissoires 	report->field_entries = entries;
177222f4b026SBenjamin Tissoires 
177322f4b026SBenjamin Tissoires 	/*
177422f4b026SBenjamin Tissoires 	 * walk through all fields in the report and
177522f4b026SBenjamin Tissoires 	 * store them by priority order in report->field_entry_list
177622f4b026SBenjamin Tissoires 	 *
177722f4b026SBenjamin Tissoires 	 * - Var elements are individualized (field + usage_index)
177822f4b026SBenjamin Tissoires 	 * - Arrays are taken as one, we can not chose an order for them
177922f4b026SBenjamin Tissoires 	 */
178022f4b026SBenjamin Tissoires 	usages = 0;
178122f4b026SBenjamin Tissoires 	for (a = 0; a < report->maxfield; a++) {
178222f4b026SBenjamin Tissoires 		field = report->field[a];
178322f4b026SBenjamin Tissoires 
178422f4b026SBenjamin Tissoires 		if (field->flags & HID_MAIN_ITEM_VARIABLE) {
178522f4b026SBenjamin Tissoires 			for (u = 0; u < field->report_count; u++) {
178622f4b026SBenjamin Tissoires 				__hid_insert_field_entry(hid, report,
178722f4b026SBenjamin Tissoires 							 &entries[usages],
178822f4b026SBenjamin Tissoires 							 field, u);
178922f4b026SBenjamin Tissoires 				usages++;
179022f4b026SBenjamin Tissoires 			}
179122f4b026SBenjamin Tissoires 		} else {
179222f4b026SBenjamin Tissoires 			__hid_insert_field_entry(hid, report, &entries[usages],
179322f4b026SBenjamin Tissoires 						 field, 0);
179422f4b026SBenjamin Tissoires 			usages++;
179522f4b026SBenjamin Tissoires 		}
179622f4b026SBenjamin Tissoires 	}
179722f4b026SBenjamin Tissoires }
179822f4b026SBenjamin Tissoires 
179922f4b026SBenjamin Tissoires static void hid_process_ordering(struct hid_device *hid)
180022f4b026SBenjamin Tissoires {
180122f4b026SBenjamin Tissoires 	struct hid_report *report;
180222f4b026SBenjamin Tissoires 	struct hid_report_enum *report_enum = &hid->report_enum[HID_INPUT_REPORT];
180322f4b026SBenjamin Tissoires 
180422f4b026SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list)
180522f4b026SBenjamin Tissoires 		hid_report_process_ordering(hid, report);
180622f4b026SBenjamin Tissoires }
180722f4b026SBenjamin Tissoires 
180822f4b026SBenjamin Tissoires /*
1809dde5845aSJiri Kosina  * Output the field into the report.
1810dde5845aSJiri Kosina  */
1811dde5845aSJiri Kosina 
18124291ee30SJoe Perches static void hid_output_field(const struct hid_device *hid,
18134291ee30SJoe Perches 			     struct hid_field *field, __u8 *data)
1814dde5845aSJiri Kosina {
1815dde5845aSJiri Kosina 	unsigned count = field->report_count;
1816dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
1817dde5845aSJiri Kosina 	unsigned size = field->report_size;
1818dde5845aSJiri Kosina 	unsigned n;
1819dde5845aSJiri Kosina 
1820dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1821dde5845aSJiri Kosina 		if (field->logical_minimum < 0)	/* signed values */
18224291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
18234291ee30SJoe Perches 				  s32ton(field->value[n], size));
1824dde5845aSJiri Kosina 		else				/* unsigned values */
18254291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
18264291ee30SJoe Perches 				  field->value[n]);
1827dde5845aSJiri Kosina 	}
1828dde5845aSJiri Kosina }
1829dde5845aSJiri Kosina 
1830dde5845aSJiri Kosina /*
1831bce1305cSMarc Zyngier  * Compute the size of a report.
1832bce1305cSMarc Zyngier  */
1833bce1305cSMarc Zyngier static size_t hid_compute_report_size(struct hid_report *report)
1834bce1305cSMarc Zyngier {
1835bce1305cSMarc Zyngier 	if (report->size)
1836bce1305cSMarc Zyngier 		return ((report->size - 1) >> 3) + 1;
1837bce1305cSMarc Zyngier 
1838bce1305cSMarc Zyngier 	return 0;
1839bce1305cSMarc Zyngier }
1840bce1305cSMarc Zyngier 
1841bce1305cSMarc Zyngier /*
184227ce4050SJiri Kosina  * Create a report. 'data' has to be allocated using
184327ce4050SJiri Kosina  * hid_alloc_report_buf() so that it has proper size.
1844dde5845aSJiri Kosina  */
1845dde5845aSJiri Kosina 
1846229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data)
1847dde5845aSJiri Kosina {
1848dde5845aSJiri Kosina 	unsigned n;
1849dde5845aSJiri Kosina 
1850dde5845aSJiri Kosina 	if (report->id > 0)
1851dde5845aSJiri Kosina 		*data++ = report->id;
1852dde5845aSJiri Kosina 
1853bce1305cSMarc Zyngier 	memset(data, 0, hid_compute_report_size(report));
1854dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
18554291ee30SJoe Perches 		hid_output_field(report->device, report->field[n], data);
1856dde5845aSJiri Kosina }
1857229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report);
1858dde5845aSJiri Kosina 
1859dde5845aSJiri Kosina /*
186027ce4050SJiri Kosina  * Allocator for buffer that is going to be passed to hid_output_report()
186127ce4050SJiri Kosina  */
186227ce4050SJiri Kosina u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
186327ce4050SJiri Kosina {
186427ce4050SJiri Kosina 	/*
186527ce4050SJiri Kosina 	 * 7 extra bytes are necessary to achieve proper functionality
186627ce4050SJiri Kosina 	 * of implement() working on 8 byte chunks
186727ce4050SJiri Kosina 	 */
186827ce4050SJiri Kosina 
18696de0b13cSAaron Ma 	u32 len = hid_report_len(report) + 7;
187027ce4050SJiri Kosina 
187127ce4050SJiri Kosina 	return kmalloc(len, flags);
187227ce4050SJiri Kosina }
187327ce4050SJiri Kosina EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
187427ce4050SJiri Kosina 
187527ce4050SJiri Kosina /*
1876dde5845aSJiri Kosina  * Set a field value. The report this field belongs to has to be
1877dde5845aSJiri Kosina  * created and transferred to the device, to set this value in the
1878dde5845aSJiri Kosina  * device.
1879dde5845aSJiri Kosina  */
1880dde5845aSJiri Kosina 
1881dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1882dde5845aSJiri Kosina {
1883be67b68dSKees Cook 	unsigned size;
1884be67b68dSKees Cook 
1885be67b68dSKees Cook 	if (!field)
1886be67b68dSKees Cook 		return -1;
1887be67b68dSKees Cook 
1888be67b68dSKees Cook 	size = field->report_size;
1889dde5845aSJiri Kosina 
1890cd667ce2SJiri Kosina 	hid_dump_input(field->report->device, field->usage + offset, value);
1891dde5845aSJiri Kosina 
1892dde5845aSJiri Kosina 	if (offset >= field->report_count) {
18938c3d52fcSJiri Kosina 		hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
18948c3d52fcSJiri Kosina 				offset, field->report_count);
1895dde5845aSJiri Kosina 		return -1;
1896dde5845aSJiri Kosina 	}
1897dde5845aSJiri Kosina 	if (field->logical_minimum < 0) {
1898dde5845aSJiri Kosina 		if (value != snto32(s32ton(value, size), size)) {
18998c3d52fcSJiri Kosina 			hid_err(field->report->device, "value %d is out of range\n", value);
1900dde5845aSJiri Kosina 			return -1;
1901dde5845aSJiri Kosina 		}
1902dde5845aSJiri Kosina 	}
1903dde5845aSJiri Kosina 	field->value[offset] = value;
1904dde5845aSJiri Kosina 	return 0;
1905dde5845aSJiri Kosina }
1906229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field);
1907dde5845aSJiri Kosina 
190885cdaf52SJiri Slaby static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
190985cdaf52SJiri Slaby 		const u8 *data)
191085cdaf52SJiri Slaby {
191185cdaf52SJiri Slaby 	struct hid_report *report;
191285cdaf52SJiri Slaby 	unsigned int n = 0;	/* Normally report number is 0 */
191385cdaf52SJiri Slaby 
191485cdaf52SJiri Slaby 	/* Device uses numbered reports, data[0] is report number */
191585cdaf52SJiri Slaby 	if (report_enum->numbered)
191685cdaf52SJiri Slaby 		n = *data;
191785cdaf52SJiri Slaby 
191885cdaf52SJiri Slaby 	report = report_enum->report_id_hash[n];
191985cdaf52SJiri Slaby 	if (report == NULL)
192085cdaf52SJiri Slaby 		dbg_hid("undefined report_id %u received\n", n);
192185cdaf52SJiri Slaby 
192285cdaf52SJiri Slaby 	return report;
192385cdaf52SJiri Slaby }
192485cdaf52SJiri Slaby 
19254fa5a7f7SBenjamin Tissoires /*
19264fa5a7f7SBenjamin Tissoires  * Implement a generic .request() callback, using .raw_request()
19274fa5a7f7SBenjamin Tissoires  * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
19284fa5a7f7SBenjamin Tissoires  */
1929d43c17eaSBenjamin Tissoires int __hid_request(struct hid_device *hid, struct hid_report *report,
1930735e1bb1SBenjamin Tissoires 		enum hid_class_request reqtype)
19314fa5a7f7SBenjamin Tissoires {
19324fa5a7f7SBenjamin Tissoires 	char *buf;
19334fa5a7f7SBenjamin Tissoires 	int ret;
19346de0b13cSAaron Ma 	u32 len;
19354fa5a7f7SBenjamin Tissoires 
19364fa5a7f7SBenjamin Tissoires 	buf = hid_alloc_report_buf(report, GFP_KERNEL);
19374fa5a7f7SBenjamin Tissoires 	if (!buf)
1938d43c17eaSBenjamin Tissoires 		return -ENOMEM;
19394fa5a7f7SBenjamin Tissoires 
19404fa5a7f7SBenjamin Tissoires 	len = hid_report_len(report);
19414fa5a7f7SBenjamin Tissoires 
19424fa5a7f7SBenjamin Tissoires 	if (reqtype == HID_REQ_SET_REPORT)
19434fa5a7f7SBenjamin Tissoires 		hid_output_report(report, buf);
19444fa5a7f7SBenjamin Tissoires 
19454fa5a7f7SBenjamin Tissoires 	ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
19464fa5a7f7SBenjamin Tissoires 					  report->type, reqtype);
19474fa5a7f7SBenjamin Tissoires 	if (ret < 0) {
19484fa5a7f7SBenjamin Tissoires 		dbg_hid("unable to complete request: %d\n", ret);
19494fa5a7f7SBenjamin Tissoires 		goto out;
19504fa5a7f7SBenjamin Tissoires 	}
19514fa5a7f7SBenjamin Tissoires 
19524fa5a7f7SBenjamin Tissoires 	if (reqtype == HID_REQ_GET_REPORT)
19534fa5a7f7SBenjamin Tissoires 		hid_input_report(hid, report->type, buf, ret, 0);
19544fa5a7f7SBenjamin Tissoires 
1955d43c17eaSBenjamin Tissoires 	ret = 0;
1956d43c17eaSBenjamin Tissoires 
19574fa5a7f7SBenjamin Tissoires out:
19584fa5a7f7SBenjamin Tissoires 	kfree(buf);
1959d43c17eaSBenjamin Tissoires 	return ret;
19604fa5a7f7SBenjamin Tissoires }
19614fa5a7f7SBenjamin Tissoires EXPORT_SYMBOL_GPL(__hid_request);
19624fa5a7f7SBenjamin Tissoires 
1963ead77b65SBenjamin Tissoires int hid_report_raw_event(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
196485cdaf52SJiri Slaby 			 int interrupt)
1965aa8de2f0SJiri Kosina {
1966aa8de2f0SJiri Kosina 	struct hid_report_enum *report_enum = hid->report_enum + type;
1967aa8de2f0SJiri Kosina 	struct hid_report *report;
19686d85d037SBenjamin Tissoires 	struct hid_driver *hdrv;
19696de0b13cSAaron Ma 	u32 rsize, csize = size;
197085cdaf52SJiri Slaby 	u8 *cdata = data;
1971b6787242SJiri Kosina 	int ret = 0;
1972aa8de2f0SJiri Kosina 
197385cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
197485cdaf52SJiri Slaby 	if (!report)
1975b6787242SJiri Kosina 		goto out;
1976aa8de2f0SJiri Kosina 
197785cdaf52SJiri Slaby 	if (report_enum->numbered) {
197885cdaf52SJiri Slaby 		cdata++;
197985cdaf52SJiri Slaby 		csize--;
1980aa8de2f0SJiri Kosina 	}
1981aa8de2f0SJiri Kosina 
1982bce1305cSMarc Zyngier 	rsize = hid_compute_report_size(report);
1983aa8de2f0SJiri Kosina 
19845ebdffd2SJohan Korsnes 	if (report_enum->numbered && rsize >= HID_MAX_BUFFER_SIZE)
19855ebdffd2SJohan Korsnes 		rsize = HID_MAX_BUFFER_SIZE - 1;
19865ebdffd2SJohan Korsnes 	else if (rsize > HID_MAX_BUFFER_SIZE)
1987966922f2SArmando Visconti 		rsize = HID_MAX_BUFFER_SIZE;
1988966922f2SArmando Visconti 
198985cdaf52SJiri Slaby 	if (csize < rsize) {
199085cdaf52SJiri Slaby 		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
199185cdaf52SJiri Slaby 				csize, rsize);
199285cdaf52SJiri Slaby 		memset(cdata + csize, 0, rsize - csize);
1993aa8de2f0SJiri Kosina 	}
1994aa8de2f0SJiri Kosina 
1995aa8de2f0SJiri Kosina 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1996aa8de2f0SJiri Kosina 		hid->hiddev_report_event(hid, report);
1997b6787242SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1998b6787242SJiri Kosina 		ret = hidraw_report_event(hid, data, size);
1999b6787242SJiri Kosina 		if (ret)
2000b6787242SJiri Kosina 			goto out;
2001b6787242SJiri Kosina 	}
2002aa8de2f0SJiri Kosina 
2003cc6b54aaSBenjamin Tissoires 	if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
2004b79c1abaSBenjamin Tissoires 		hid_process_report(hid, report, cdata, interrupt);
20056d85d037SBenjamin Tissoires 		hdrv = hid->driver;
20066d85d037SBenjamin Tissoires 		if (hdrv && hdrv->report)
20076d85d037SBenjamin Tissoires 			hdrv->report(hid, report);
2008b94e3c94SMatthieu CASTET 	}
2009aa8de2f0SJiri Kosina 
2010aa8de2f0SJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
2011aa8de2f0SJiri Kosina 		hidinput_report_event(hid, report);
2012b6787242SJiri Kosina out:
2013b6787242SJiri Kosina 	return ret;
201485cdaf52SJiri Slaby }
201585cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_report_raw_event);
201685cdaf52SJiri Slaby 
201785cdaf52SJiri Slaby /**
201885cdaf52SJiri Slaby  * hid_input_report - report data from lower layer (usb, bt...)
201985cdaf52SJiri Slaby  *
202085cdaf52SJiri Slaby  * @hid: hid device
202185cdaf52SJiri Slaby  * @type: HID report type (HID_*_REPORT)
202285cdaf52SJiri Slaby  * @data: report contents
202385cdaf52SJiri Slaby  * @size: size of data parameter
2024ff9b00a2SJiri Kosina  * @interrupt: distinguish between interrupt and control transfers
202585cdaf52SJiri Slaby  *
202685cdaf52SJiri Slaby  * This is data entry for lower layers.
202785cdaf52SJiri Slaby  */
2028ead77b65SBenjamin Tissoires int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
2029ead77b65SBenjamin Tissoires 		     int interrupt)
203085cdaf52SJiri Slaby {
203176c317d6SJulia Lawall 	struct hid_report_enum *report_enum;
203276c317d6SJulia Lawall 	struct hid_driver *hdrv;
203385cdaf52SJiri Slaby 	struct hid_report *report;
203445dc1ac7SJiri Kosina 	int ret = 0;
203585cdaf52SJiri Slaby 
20364ea54542SDavid Herrmann 	if (!hid)
203785cdaf52SJiri Slaby 		return -ENODEV;
20384ea54542SDavid Herrmann 
2039c849a614SAndrew de los Reyes 	if (down_trylock(&hid->driver_input_lock))
20404ea54542SDavid Herrmann 		return -EBUSY;
20414ea54542SDavid Herrmann 
20424ea54542SDavid Herrmann 	if (!hid->driver) {
20434ea54542SDavid Herrmann 		ret = -ENODEV;
20444ea54542SDavid Herrmann 		goto unlock;
20454ea54542SDavid Herrmann 	}
204676c317d6SJulia Lawall 	report_enum = hid->report_enum + type;
204776c317d6SJulia Lawall 	hdrv = hid->driver;
204885cdaf52SJiri Slaby 
204985cdaf52SJiri Slaby 	if (!size) {
205085cdaf52SJiri Slaby 		dbg_hid("empty report\n");
20514ea54542SDavid Herrmann 		ret = -1;
20524ea54542SDavid Herrmann 		goto unlock;
205385cdaf52SJiri Slaby 	}
205485cdaf52SJiri Slaby 
2055b94e3c94SMatthieu CASTET 	/* Avoid unnecessary overhead if debugfs is disabled */
2056a5f04b9dSBenjamin Tissoires 	if (!list_empty(&hid->debug_list))
2057a5f04b9dSBenjamin Tissoires 		hid_dump_report(hid, type, data, size);
2058b94e3c94SMatthieu CASTET 
2059f77e347bSJiri Kosina 	report = hid_get_report(report_enum, data);
2060f77e347bSJiri Kosina 
20614ea54542SDavid Herrmann 	if (!report) {
20624ea54542SDavid Herrmann 		ret = -1;
20634ea54542SDavid Herrmann 		goto unlock;
20644ea54542SDavid Herrmann 	}
2065f77e347bSJiri Kosina 
206685cdaf52SJiri Slaby 	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
206785cdaf52SJiri Slaby 		ret = hdrv->raw_event(hid, report, data, size);
2068556483e2SFelix Rueegg 		if (ret < 0)
20694ea54542SDavid Herrmann 			goto unlock;
20704ea54542SDavid Herrmann 	}
207185cdaf52SJiri Slaby 
2072b6787242SJiri Kosina 	ret = hid_report_raw_event(hid, type, data, size, interrupt);
2073aa8de2f0SJiri Kosina 
20744ea54542SDavid Herrmann unlock:
2075c849a614SAndrew de los Reyes 	up(&hid->driver_input_lock);
207645dc1ac7SJiri Kosina 	return ret;
2077aa8de2f0SJiri Kosina }
2078aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report);
2079aa8de2f0SJiri Kosina 
2080d5d3e202SBenjamin Tissoires bool hid_match_one_id(const struct hid_device *hdev,
20810f37cd03SJiri Kosina 		      const struct hid_device_id *id)
20820f37cd03SJiri Kosina {
20837431fb76SHenrik Rydberg 	return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
20844d53b801SHenrik Rydberg 		(id->group == HID_GROUP_ANY || id->group == hdev->group) &&
20850f37cd03SJiri Kosina 		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
20860f37cd03SJiri Kosina 		(id->product == HID_ANY_ID || id->product == hdev->product);
20870f37cd03SJiri Kosina }
20880f37cd03SJiri Kosina 
2089d5d3e202SBenjamin Tissoires const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
20900f37cd03SJiri Kosina 		const struct hid_device_id *id)
20910f37cd03SJiri Kosina {
20920f37cd03SJiri Kosina 	for (; id->bus; id++)
20930f37cd03SJiri Kosina 		if (hid_match_one_id(hdev, id))
20940f37cd03SJiri Kosina 			return id;
20950f37cd03SJiri Kosina 
20960f37cd03SJiri Kosina 	return NULL;
20970f37cd03SJiri Kosina }
209880305f97SBastien Nocera EXPORT_SYMBOL_GPL(hid_match_id);
20990f37cd03SJiri Kosina 
21000f37cd03SJiri Kosina static const struct hid_device_id hid_hiddev_list[] = {
2101c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
2102c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
21030f37cd03SJiri Kosina 	{ }
21040f37cd03SJiri Kosina };
21050f37cd03SJiri Kosina 
21060f37cd03SJiri Kosina static bool hid_hiddev(struct hid_device *hdev)
21070f37cd03SJiri Kosina {
21080f37cd03SJiri Kosina 	return !!hid_match_id(hdev, hid_hiddev_list);
21090f37cd03SJiri Kosina }
21100f37cd03SJiri Kosina 
21116d3bfb74SAlan Ott 
21126d3bfb74SAlan Ott static ssize_t
21136d3bfb74SAlan Ott read_report_descriptor(struct file *filp, struct kobject *kobj,
21146d3bfb74SAlan Ott 		struct bin_attribute *attr,
21156d3bfb74SAlan Ott 		char *buf, loff_t off, size_t count)
21166d3bfb74SAlan Ott {
21172cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj);
2118ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
21196d3bfb74SAlan Ott 
21206d3bfb74SAlan Ott 	if (off >= hdev->rsize)
21216d3bfb74SAlan Ott 		return 0;
21226d3bfb74SAlan Ott 
21236d3bfb74SAlan Ott 	if (off + count > hdev->rsize)
21246d3bfb74SAlan Ott 		count = hdev->rsize - off;
21256d3bfb74SAlan Ott 
21266d3bfb74SAlan Ott 	memcpy(buf, hdev->rdesc + off, count);
21276d3bfb74SAlan Ott 
21286d3bfb74SAlan Ott 	return count;
21296d3bfb74SAlan Ott }
21306d3bfb74SAlan Ott 
2131a877417eSOlivier Gay static ssize_t
2132a877417eSOlivier Gay show_country(struct device *dev, struct device_attribute *attr,
2133a877417eSOlivier Gay 		char *buf)
2134a877417eSOlivier Gay {
2135ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
2136a877417eSOlivier Gay 
2137a877417eSOlivier Gay 	return sprintf(buf, "%02x\n", hdev->country & 0xff);
2138a877417eSOlivier Gay }
2139a877417eSOlivier Gay 
21406d3bfb74SAlan Ott static struct bin_attribute dev_bin_attr_report_desc = {
21416d3bfb74SAlan Ott 	.attr = { .name = "report_descriptor", .mode = 0444 },
21426d3bfb74SAlan Ott 	.read = read_report_descriptor,
21436d3bfb74SAlan Ott 	.size = HID_MAX_DESCRIPTOR_SIZE,
21446d3bfb74SAlan Ott };
21456d3bfb74SAlan Ott 
2146ad8378edSBhumika Goyal static const struct device_attribute dev_attr_country = {
2147a877417eSOlivier Gay 	.attr = { .name = "country", .mode = 0444 },
2148a877417eSOlivier Gay 	.show = show_country,
2149a877417eSOlivier Gay };
2150a877417eSOlivier Gay 
215193c10132SJiri Slaby int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
215293c10132SJiri Slaby {
215393c10132SJiri Slaby 	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
215493c10132SJiri Slaby 		"Joystick", "Gamepad", "Keyboard", "Keypad",
215593c10132SJiri Slaby 		"Multi-Axis Controller"
215693c10132SJiri Slaby 	};
215793c10132SJiri Slaby 	const char *type, *bus;
215879b568b9SRichard Purdie 	char buf[64] = "";
215993c10132SJiri Slaby 	unsigned int i;
216093c10132SJiri Slaby 	int len;
21616d3bfb74SAlan Ott 	int ret;
216293c10132SJiri Slaby 
2163b5e5a37eSBastien Nocera 	if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
2164b5e5a37eSBastien Nocera 		connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
21653a343ee4SDaniel Mack 	if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
21663a343ee4SDaniel Mack 		connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
216793c10132SJiri Slaby 	if (hdev->bus != BUS_USB)
216893c10132SJiri Slaby 		connect_mask &= ~HID_CONNECT_HIDDEV;
21690f37cd03SJiri Kosina 	if (hid_hiddev(hdev))
21700f37cd03SJiri Kosina 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
217193c10132SJiri Slaby 
217293c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
217393c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
217493c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_INPUT;
2175b77c3920SBenjamin Tissoires 
217693c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
217793c10132SJiri Slaby 			!hdev->hiddev_connect(hdev,
217893c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDDEV_FORCE))
217993c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDDEV;
218093c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
218193c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDRAW;
218293c10132SJiri Slaby 
21837704ac93SBenjamin Tissoires 	if (connect_mask & HID_CONNECT_DRIVER)
21847704ac93SBenjamin Tissoires 		hdev->claimed |= HID_CLAIMED_DRIVER;
21857704ac93SBenjamin Tissoires 
21864bc19f62SDavid Herrmann 	/* Drivers with the ->raw_event callback set are not required to connect
21874bc19f62SDavid Herrmann 	 * to any other listener. */
21884bc19f62SDavid Herrmann 	if (!hdev->claimed && !hdev->driver->raw_event) {
21894bc19f62SDavid Herrmann 		hid_err(hdev, "device has no listeners, quitting\n");
219093c10132SJiri Slaby 		return -ENODEV;
219193c10132SJiri Slaby 	}
219293c10132SJiri Slaby 
219322f4b026SBenjamin Tissoires 	hid_process_ordering(hdev);
219422f4b026SBenjamin Tissoires 
219593c10132SJiri Slaby 	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
219693c10132SJiri Slaby 			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
219793c10132SJiri Slaby 		hdev->ff_init(hdev);
219893c10132SJiri Slaby 
219993c10132SJiri Slaby 	len = 0;
220093c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_INPUT)
220193c10132SJiri Slaby 		len += sprintf(buf + len, "input");
220293c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
220393c10132SJiri Slaby 		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
2204733aca90SJaejoong Kim 				((struct hiddev *)hdev->hiddev)->minor);
220593c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
220693c10132SJiri Slaby 		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
220793c10132SJiri Slaby 				((struct hidraw *)hdev->hidraw)->minor);
220893c10132SJiri Slaby 
220993c10132SJiri Slaby 	type = "Device";
221093c10132SJiri Slaby 	for (i = 0; i < hdev->maxcollection; i++) {
221193c10132SJiri Slaby 		struct hid_collection *col = &hdev->collection[i];
221293c10132SJiri Slaby 		if (col->type == HID_COLLECTION_APPLICATION &&
221393c10132SJiri Slaby 		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
221493c10132SJiri Slaby 		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
221593c10132SJiri Slaby 			type = types[col->usage & 0xffff];
221693c10132SJiri Slaby 			break;
221793c10132SJiri Slaby 		}
221893c10132SJiri Slaby 	}
221993c10132SJiri Slaby 
222093c10132SJiri Slaby 	switch (hdev->bus) {
222193c10132SJiri Slaby 	case BUS_USB:
222293c10132SJiri Slaby 		bus = "USB";
222393c10132SJiri Slaby 		break;
222493c10132SJiri Slaby 	case BUS_BLUETOOTH:
222593c10132SJiri Slaby 		bus = "BLUETOOTH";
222693c10132SJiri Slaby 		break;
222706780727SDaniel Martin 	case BUS_I2C:
222806780727SDaniel Martin 		bus = "I2C";
222906780727SDaniel Martin 		break;
223048e33befSMark Bolhuis 	case BUS_VIRTUAL:
223148e33befSMark Bolhuis 		bus = "VIRTUAL";
223248e33befSMark Bolhuis 		break;
2233806fc359SBasavaraj Natikar 	case BUS_INTEL_ISHTP:
2234806fc359SBasavaraj Natikar 	case BUS_AMD_SFH:
2235806fc359SBasavaraj Natikar 		bus = "SENSOR HUB";
2236806fc359SBasavaraj Natikar 		break;
223793c10132SJiri Slaby 	default:
223893c10132SJiri Slaby 		bus = "<UNKNOWN>";
223993c10132SJiri Slaby 	}
224093c10132SJiri Slaby 
2241a877417eSOlivier Gay 	ret = device_create_file(&hdev->dev, &dev_attr_country);
2242a877417eSOlivier Gay 	if (ret)
2243a877417eSOlivier Gay 		hid_warn(hdev,
2244a877417eSOlivier Gay 			 "can't create sysfs country code attribute err: %d\n", ret);
2245a877417eSOlivier Gay 
22464291ee30SJoe Perches 	hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
224793c10132SJiri Slaby 		 buf, bus, hdev->version >> 8, hdev->version & 0xff,
224893c10132SJiri Slaby 		 type, hdev->name, hdev->phys);
224993c10132SJiri Slaby 
225093c10132SJiri Slaby 	return 0;
225193c10132SJiri Slaby }
225293c10132SJiri Slaby EXPORT_SYMBOL_GPL(hid_connect);
225393c10132SJiri Slaby 
2254c4c259bcSJiri Kosina void hid_disconnect(struct hid_device *hdev)
2255c4c259bcSJiri Kosina {
2256a877417eSOlivier Gay 	device_remove_file(&hdev->dev, &dev_attr_country);
2257c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_INPUT)
2258c4c259bcSJiri Kosina 		hidinput_disconnect(hdev);
2259c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
2260c4c259bcSJiri Kosina 		hdev->hiddev_disconnect(hdev);
2261c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
2262c4c259bcSJiri Kosina 		hidraw_disconnect(hdev);
22639c5c6ed7SBenjamin Tissoires 	hdev->claimed = 0;
2264c4c259bcSJiri Kosina }
2265c4c259bcSJiri Kosina EXPORT_SYMBOL_GPL(hid_disconnect);
2266c4c259bcSJiri Kosina 
2267aaac082dSDmitry Torokhov /**
2268aaac082dSDmitry Torokhov  * hid_hw_start - start underlying HW
2269aaac082dSDmitry Torokhov  * @hdev: hid device
2270aaac082dSDmitry Torokhov  * @connect_mask: which outputs to connect, see HID_CONNECT_*
2271aaac082dSDmitry Torokhov  *
2272aaac082dSDmitry Torokhov  * Call this in probe function *after* hid_parse. This will setup HW
2273aaac082dSDmitry Torokhov  * buffers and start the device (if not defeirred to device open).
2274aaac082dSDmitry Torokhov  * hid_hw_stop must be called if this was successful.
2275aaac082dSDmitry Torokhov  */
2276aaac082dSDmitry Torokhov int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
2277aaac082dSDmitry Torokhov {
2278aaac082dSDmitry Torokhov 	int error;
2279aaac082dSDmitry Torokhov 
2280aaac082dSDmitry Torokhov 	error = hdev->ll_driver->start(hdev);
2281aaac082dSDmitry Torokhov 	if (error)
2282aaac082dSDmitry Torokhov 		return error;
2283aaac082dSDmitry Torokhov 
2284aaac082dSDmitry Torokhov 	if (connect_mask) {
2285aaac082dSDmitry Torokhov 		error = hid_connect(hdev, connect_mask);
2286aaac082dSDmitry Torokhov 		if (error) {
2287aaac082dSDmitry Torokhov 			hdev->ll_driver->stop(hdev);
2288aaac082dSDmitry Torokhov 			return error;
2289aaac082dSDmitry Torokhov 		}
2290aaac082dSDmitry Torokhov 	}
2291aaac082dSDmitry Torokhov 
2292aaac082dSDmitry Torokhov 	return 0;
2293aaac082dSDmitry Torokhov }
2294aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_start);
2295aaac082dSDmitry Torokhov 
2296aaac082dSDmitry Torokhov /**
2297aaac082dSDmitry Torokhov  * hid_hw_stop - stop underlying HW
2298aaac082dSDmitry Torokhov  * @hdev: hid device
2299aaac082dSDmitry Torokhov  *
2300aaac082dSDmitry Torokhov  * This is usually called from remove function or from probe when something
2301aaac082dSDmitry Torokhov  * failed and hid_hw_start was called already.
2302aaac082dSDmitry Torokhov  */
2303aaac082dSDmitry Torokhov void hid_hw_stop(struct hid_device *hdev)
2304aaac082dSDmitry Torokhov {
2305aaac082dSDmitry Torokhov 	hid_disconnect(hdev);
2306aaac082dSDmitry Torokhov 	hdev->ll_driver->stop(hdev);
2307aaac082dSDmitry Torokhov }
2308aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_stop);
2309aaac082dSDmitry Torokhov 
2310aaac082dSDmitry Torokhov /**
2311aaac082dSDmitry Torokhov  * hid_hw_open - signal underlying HW to start delivering events
2312aaac082dSDmitry Torokhov  * @hdev: hid device
2313aaac082dSDmitry Torokhov  *
2314aaac082dSDmitry Torokhov  * Tell underlying HW to start delivering events from the device.
2315aaac082dSDmitry Torokhov  * This function should be called sometime after successful call
2316d6c70a86SHisao Tanabe  * to hid_hw_start().
2317aaac082dSDmitry Torokhov  */
2318aaac082dSDmitry Torokhov int hid_hw_open(struct hid_device *hdev)
2319aaac082dSDmitry Torokhov {
2320aaac082dSDmitry Torokhov 	int ret;
2321aaac082dSDmitry Torokhov 
2322aaac082dSDmitry Torokhov 	ret = mutex_lock_killable(&hdev->ll_open_lock);
2323aaac082dSDmitry Torokhov 	if (ret)
2324aaac082dSDmitry Torokhov 		return ret;
2325aaac082dSDmitry Torokhov 
2326aaac082dSDmitry Torokhov 	if (!hdev->ll_open_count++) {
2327aaac082dSDmitry Torokhov 		ret = hdev->ll_driver->open(hdev);
2328aaac082dSDmitry Torokhov 		if (ret)
2329aaac082dSDmitry Torokhov 			hdev->ll_open_count--;
2330aaac082dSDmitry Torokhov 	}
2331aaac082dSDmitry Torokhov 
2332aaac082dSDmitry Torokhov 	mutex_unlock(&hdev->ll_open_lock);
2333aaac082dSDmitry Torokhov 	return ret;
2334aaac082dSDmitry Torokhov }
2335aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_open);
2336aaac082dSDmitry Torokhov 
2337aaac082dSDmitry Torokhov /**
2338aaac082dSDmitry Torokhov  * hid_hw_close - signal underlaying HW to stop delivering events
2339aaac082dSDmitry Torokhov  *
2340aaac082dSDmitry Torokhov  * @hdev: hid device
2341aaac082dSDmitry Torokhov  *
2342aaac082dSDmitry Torokhov  * This function indicates that we are not interested in the events
2343aaac082dSDmitry Torokhov  * from this device anymore. Delivery of events may or may not stop,
2344aaac082dSDmitry Torokhov  * depending on the number of users still outstanding.
2345aaac082dSDmitry Torokhov  */
2346aaac082dSDmitry Torokhov void hid_hw_close(struct hid_device *hdev)
2347aaac082dSDmitry Torokhov {
2348aaac082dSDmitry Torokhov 	mutex_lock(&hdev->ll_open_lock);
2349aaac082dSDmitry Torokhov 	if (!--hdev->ll_open_count)
2350aaac082dSDmitry Torokhov 		hdev->ll_driver->close(hdev);
2351aaac082dSDmitry Torokhov 	mutex_unlock(&hdev->ll_open_lock);
2352aaac082dSDmitry Torokhov }
2353aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_close);
2354aaac082dSDmitry Torokhov 
2355f65a0b1fSBenjamin Tissoires /**
2356f65a0b1fSBenjamin Tissoires  * hid_hw_request - send report request to device
2357f65a0b1fSBenjamin Tissoires  *
2358f65a0b1fSBenjamin Tissoires  * @hdev: hid device
2359f65a0b1fSBenjamin Tissoires  * @report: report to send
2360f65a0b1fSBenjamin Tissoires  * @reqtype: hid request type
2361f65a0b1fSBenjamin Tissoires  */
2362f65a0b1fSBenjamin Tissoires void hid_hw_request(struct hid_device *hdev,
2363735e1bb1SBenjamin Tissoires 		    struct hid_report *report, enum hid_class_request reqtype)
2364f65a0b1fSBenjamin Tissoires {
2365f65a0b1fSBenjamin Tissoires 	if (hdev->ll_driver->request)
2366f65a0b1fSBenjamin Tissoires 		return hdev->ll_driver->request(hdev, report, reqtype);
2367f65a0b1fSBenjamin Tissoires 
2368f65a0b1fSBenjamin Tissoires 	__hid_request(hdev, report, reqtype);
2369f65a0b1fSBenjamin Tissoires }
2370f65a0b1fSBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_hw_request);
2371f65a0b1fSBenjamin Tissoires 
2372f65a0b1fSBenjamin Tissoires /**
2373f65a0b1fSBenjamin Tissoires  * hid_hw_raw_request - send report request to device
2374f65a0b1fSBenjamin Tissoires  *
2375f65a0b1fSBenjamin Tissoires  * @hdev: hid device
2376f65a0b1fSBenjamin Tissoires  * @reportnum: report ID
2377f65a0b1fSBenjamin Tissoires  * @buf: in/out data to transfer
2378f65a0b1fSBenjamin Tissoires  * @len: length of buf
2379f65a0b1fSBenjamin Tissoires  * @rtype: HID report type
2380f65a0b1fSBenjamin Tissoires  * @reqtype: HID_REQ_GET_REPORT or HID_REQ_SET_REPORT
2381f65a0b1fSBenjamin Tissoires  *
2382f65a0b1fSBenjamin Tissoires  * Return: count of data transferred, negative if error
2383f65a0b1fSBenjamin Tissoires  *
2384f65a0b1fSBenjamin Tissoires  * Same behavior as hid_hw_request, but with raw buffers instead.
2385f65a0b1fSBenjamin Tissoires  */
2386f65a0b1fSBenjamin Tissoires int hid_hw_raw_request(struct hid_device *hdev,
2387f65a0b1fSBenjamin Tissoires 		       unsigned char reportnum, __u8 *buf,
2388735e1bb1SBenjamin Tissoires 		       size_t len, enum hid_report_type rtype, enum hid_class_request reqtype)
2389f65a0b1fSBenjamin Tissoires {
2390f65a0b1fSBenjamin Tissoires 	if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf)
2391f65a0b1fSBenjamin Tissoires 		return -EINVAL;
2392f65a0b1fSBenjamin Tissoires 
2393f65a0b1fSBenjamin Tissoires 	return hdev->ll_driver->raw_request(hdev, reportnum, buf, len,
2394f65a0b1fSBenjamin Tissoires 					    rtype, reqtype);
2395f65a0b1fSBenjamin Tissoires }
2396f65a0b1fSBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_hw_raw_request);
2397f65a0b1fSBenjamin Tissoires 
2398f65a0b1fSBenjamin Tissoires /**
2399f65a0b1fSBenjamin Tissoires  * hid_hw_output_report - send output report to device
2400f65a0b1fSBenjamin Tissoires  *
2401f65a0b1fSBenjamin Tissoires  * @hdev: hid device
2402f65a0b1fSBenjamin Tissoires  * @buf: raw data to transfer
2403f65a0b1fSBenjamin Tissoires  * @len: length of buf
2404f65a0b1fSBenjamin Tissoires  *
2405f65a0b1fSBenjamin Tissoires  * Return: count of data transferred, negative if error
2406f65a0b1fSBenjamin Tissoires  */
2407f65a0b1fSBenjamin Tissoires int hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len)
2408f65a0b1fSBenjamin Tissoires {
2409f65a0b1fSBenjamin Tissoires 	if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf)
2410f65a0b1fSBenjamin Tissoires 		return -EINVAL;
2411f65a0b1fSBenjamin Tissoires 
2412f65a0b1fSBenjamin Tissoires 	if (hdev->ll_driver->output_report)
2413f65a0b1fSBenjamin Tissoires 		return hdev->ll_driver->output_report(hdev, buf, len);
2414f65a0b1fSBenjamin Tissoires 
2415f65a0b1fSBenjamin Tissoires 	return -ENOSYS;
2416f65a0b1fSBenjamin Tissoires }
2417f65a0b1fSBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_hw_output_report);
2418f65a0b1fSBenjamin Tissoires 
24199e356208SBenjamin Tissoires #ifdef CONFIG_PM
24209e356208SBenjamin Tissoires int hid_driver_suspend(struct hid_device *hdev, pm_message_t state)
24219e356208SBenjamin Tissoires {
24229e356208SBenjamin Tissoires 	if (hdev->driver && hdev->driver->suspend)
24239e356208SBenjamin Tissoires 		return hdev->driver->suspend(hdev, state);
24249e356208SBenjamin Tissoires 
24259e356208SBenjamin Tissoires 	return 0;
24269e356208SBenjamin Tissoires }
24279e356208SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_driver_suspend);
24289e356208SBenjamin Tissoires 
24299e356208SBenjamin Tissoires int hid_driver_reset_resume(struct hid_device *hdev)
24309e356208SBenjamin Tissoires {
24319e356208SBenjamin Tissoires 	if (hdev->driver && hdev->driver->reset_resume)
24329e356208SBenjamin Tissoires 		return hdev->driver->reset_resume(hdev);
24339e356208SBenjamin Tissoires 
24349e356208SBenjamin Tissoires 	return 0;
24359e356208SBenjamin Tissoires }
24369e356208SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_driver_reset_resume);
24379e356208SBenjamin Tissoires 
24389e356208SBenjamin Tissoires int hid_driver_resume(struct hid_device *hdev)
24399e356208SBenjamin Tissoires {
24409e356208SBenjamin Tissoires 	if (hdev->driver && hdev->driver->resume)
24419e356208SBenjamin Tissoires 		return hdev->driver->resume(hdev);
24429e356208SBenjamin Tissoires 
24439e356208SBenjamin Tissoires 	return 0;
24449e356208SBenjamin Tissoires }
24459e356208SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_driver_resume);
24469e356208SBenjamin Tissoires #endif /* CONFIG_PM */
24479e356208SBenjamin Tissoires 
24483a6f82f7SJiri Slaby struct hid_dynid {
24493a6f82f7SJiri Slaby 	struct list_head list;
24503a6f82f7SJiri Slaby 	struct hid_device_id id;
24513a6f82f7SJiri Slaby };
24523a6f82f7SJiri Slaby 
24533a6f82f7SJiri Slaby /**
2454cd2bb7b7SLee Jones  * new_id_store - add a new HID device ID to this driver and re-probe devices
24555f94e9c8SXiaofei Tan  * @drv: target device driver
24563a6f82f7SJiri Slaby  * @buf: buffer for scanning device ID data
24573a6f82f7SJiri Slaby  * @count: input size
24583a6f82f7SJiri Slaby  *
24593a6f82f7SJiri Slaby  * Adds a new dynamic hid device ID to this driver,
24603a6f82f7SJiri Slaby  * and causes the driver to probe for all devices again.
24613a6f82f7SJiri Slaby  */
2462c2810325SGreg Kroah-Hartman static ssize_t new_id_store(struct device_driver *drv, const char *buf,
24633a6f82f7SJiri Slaby 		size_t count)
24643a6f82f7SJiri Slaby {
2465ba91a967SGeliang Tang 	struct hid_driver *hdrv = to_hid_driver(drv);
24663a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
24673a6f82f7SJiri Slaby 	__u32 bus, vendor, product;
24683a6f82f7SJiri Slaby 	unsigned long driver_data = 0;
24693a6f82f7SJiri Slaby 	int ret;
24703a6f82f7SJiri Slaby 
24713a6f82f7SJiri Slaby 	ret = sscanf(buf, "%x %x %x %lx",
24723a6f82f7SJiri Slaby 			&bus, &vendor, &product, &driver_data);
24733a6f82f7SJiri Slaby 	if (ret < 3)
24743a6f82f7SJiri Slaby 		return -EINVAL;
24753a6f82f7SJiri Slaby 
24763a6f82f7SJiri Slaby 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
24773a6f82f7SJiri Slaby 	if (!dynid)
24783a6f82f7SJiri Slaby 		return -ENOMEM;
24793a6f82f7SJiri Slaby 
24803a6f82f7SJiri Slaby 	dynid->id.bus = bus;
24814d53b801SHenrik Rydberg 	dynid->id.group = HID_GROUP_ANY;
24823a6f82f7SJiri Slaby 	dynid->id.vendor = vendor;
24833a6f82f7SJiri Slaby 	dynid->id.product = product;
24843a6f82f7SJiri Slaby 	dynid->id.driver_data = driver_data;
24853a6f82f7SJiri Slaby 
24863a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
24873a6f82f7SJiri Slaby 	list_add_tail(&dynid->list, &hdrv->dyn_list);
24883a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
24893a6f82f7SJiri Slaby 
24903a6f82f7SJiri Slaby 	ret = driver_attach(&hdrv->driver);
24913a6f82f7SJiri Slaby 
24923a6f82f7SJiri Slaby 	return ret ? : count;
24933a6f82f7SJiri Slaby }
2494c2810325SGreg Kroah-Hartman static DRIVER_ATTR_WO(new_id);
2495c2810325SGreg Kroah-Hartman 
2496c2810325SGreg Kroah-Hartman static struct attribute *hid_drv_attrs[] = {
2497c2810325SGreg Kroah-Hartman 	&driver_attr_new_id.attr,
2498c2810325SGreg Kroah-Hartman 	NULL,
2499c2810325SGreg Kroah-Hartman };
2500c2810325SGreg Kroah-Hartman ATTRIBUTE_GROUPS(hid_drv);
25013a6f82f7SJiri Slaby 
25023a6f82f7SJiri Slaby static void hid_free_dynids(struct hid_driver *hdrv)
25033a6f82f7SJiri Slaby {
25043a6f82f7SJiri Slaby 	struct hid_dynid *dynid, *n;
25053a6f82f7SJiri Slaby 
25063a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
25073a6f82f7SJiri Slaby 	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
25083a6f82f7SJiri Slaby 		list_del(&dynid->list);
25093a6f82f7SJiri Slaby 		kfree(dynid);
25103a6f82f7SJiri Slaby 	}
25113a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
25123a6f82f7SJiri Slaby }
25133a6f82f7SJiri Slaby 
2514e04a0442SBenjamin Tissoires const struct hid_device_id *hid_match_device(struct hid_device *hdev,
25153a6f82f7SJiri Slaby 					     struct hid_driver *hdrv)
25163a6f82f7SJiri Slaby {
25173a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
25183a6f82f7SJiri Slaby 
25193a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
25203a6f82f7SJiri Slaby 	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
25213a6f82f7SJiri Slaby 		if (hid_match_one_id(hdev, &dynid->id)) {
25223a6f82f7SJiri Slaby 			spin_unlock(&hdrv->dyn_lock);
25233a6f82f7SJiri Slaby 			return &dynid->id;
25243a6f82f7SJiri Slaby 		}
25253a6f82f7SJiri Slaby 	}
25263a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
25273a6f82f7SJiri Slaby 
25283a6f82f7SJiri Slaby 	return hid_match_id(hdev, hdrv->id_table);
25293a6f82f7SJiri Slaby }
2530e04a0442SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_match_device);
25313a6f82f7SJiri Slaby 
253285cdaf52SJiri Slaby static int hid_bus_match(struct device *dev, struct device_driver *drv)
253385cdaf52SJiri Slaby {
2534ba91a967SGeliang Tang 	struct hid_driver *hdrv = to_hid_driver(drv);
2535ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
253685cdaf52SJiri Slaby 
2537070748edSHenrik Rydberg 	return hid_match_device(hdev, hdrv) != NULL;
253885cdaf52SJiri Slaby }
253985cdaf52SJiri Slaby 
25401a8861f1SDaniel M. Lambea /**
25411a8861f1SDaniel M. Lambea  * hid_compare_device_paths - check if both devices share the same path
25421a8861f1SDaniel M. Lambea  * @hdev_a: hid device
25431a8861f1SDaniel M. Lambea  * @hdev_b: hid device
25441a8861f1SDaniel M. Lambea  * @separator: char to use as separator
25451a8861f1SDaniel M. Lambea  *
25461a8861f1SDaniel M. Lambea  * Check if two devices share the same path up to the last occurrence of
25471a8861f1SDaniel M. Lambea  * the separator char. Both paths must exist (i.e., zero-length paths
25481a8861f1SDaniel M. Lambea  * don't match).
25491a8861f1SDaniel M. Lambea  */
25501a8861f1SDaniel M. Lambea bool hid_compare_device_paths(struct hid_device *hdev_a,
25511a8861f1SDaniel M. Lambea 			      struct hid_device *hdev_b, char separator)
25521a8861f1SDaniel M. Lambea {
25531a8861f1SDaniel M. Lambea 	int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
25541a8861f1SDaniel M. Lambea 	int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
25551a8861f1SDaniel M. Lambea 
25561a8861f1SDaniel M. Lambea 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
25571a8861f1SDaniel M. Lambea 		return false;
25581a8861f1SDaniel M. Lambea 
25591a8861f1SDaniel M. Lambea 	return !strncmp(hdev_a->phys, hdev_b->phys, n1);
25601a8861f1SDaniel M. Lambea }
25611a8861f1SDaniel M. Lambea EXPORT_SYMBOL_GPL(hid_compare_device_paths);
25621a8861f1SDaniel M. Lambea 
256385cdaf52SJiri Slaby static int hid_device_probe(struct device *dev)
256485cdaf52SJiri Slaby {
2565ba91a967SGeliang Tang 	struct hid_driver *hdrv = to_hid_driver(dev->driver);
2566ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
256785cdaf52SJiri Slaby 	const struct hid_device_id *id;
256885cdaf52SJiri Slaby 	int ret = 0;
256985cdaf52SJiri Slaby 
2570c849a614SAndrew de los Reyes 	if (down_interruptible(&hdev->driver_input_lock)) {
2571c849a614SAndrew de los Reyes 		ret = -EINTR;
25726f68f0acSBinoy Jayan 		goto end;
2573c849a614SAndrew de los Reyes 	}
2574c849a614SAndrew de los Reyes 	hdev->io_started = false;
25754ea54542SDavid Herrmann 
25768f732850SBenjamin Tissoires 	clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
25778f732850SBenjamin Tissoires 
257885cdaf52SJiri Slaby 	if (!hdev->driver) {
25793a6f82f7SJiri Slaby 		id = hid_match_device(hdev, hdrv);
2580ba623a77SDan Carpenter 		if (id == NULL) {
2581ba623a77SDan Carpenter 			ret = -ENODEV;
2582ba623a77SDan Carpenter 			goto unlock;
2583ba623a77SDan Carpenter 		}
2584c500c971SJiri Slaby 
2585e04a0442SBenjamin Tissoires 		if (hdrv->match) {
2586e04a0442SBenjamin Tissoires 			if (!hdrv->match(hdev, hid_ignore_special_drivers)) {
2587e04a0442SBenjamin Tissoires 				ret = -ENODEV;
2588e04a0442SBenjamin Tissoires 				goto unlock;
2589e04a0442SBenjamin Tissoires 			}
2590e04a0442SBenjamin Tissoires 		} else {
2591e04a0442SBenjamin Tissoires 			/*
2592e04a0442SBenjamin Tissoires 			 * hid-generic implements .match(), so if
2593e04a0442SBenjamin Tissoires 			 * hid_ignore_special_drivers is set, we can safely
2594e04a0442SBenjamin Tissoires 			 * return.
2595e04a0442SBenjamin Tissoires 			 */
2596e04a0442SBenjamin Tissoires 			if (hid_ignore_special_drivers) {
2597e04a0442SBenjamin Tissoires 				ret = -ENODEV;
2598e04a0442SBenjamin Tissoires 				goto unlock;
2599e04a0442SBenjamin Tissoires 			}
2600e04a0442SBenjamin Tissoires 		}
2601e04a0442SBenjamin Tissoires 
26022904e68fSBenjamin Tissoires 		/* reset the quirks that has been previously set */
26032904e68fSBenjamin Tissoires 		hdev->quirks = hid_lookup_quirk(hdev);
260485cdaf52SJiri Slaby 		hdev->driver = hdrv;
2605c500c971SJiri Slaby 		if (hdrv->probe) {
2606c500c971SJiri Slaby 			ret = hdrv->probe(hdev, id);
2607c500c971SJiri Slaby 		} else { /* default probe */
2608a7197c2eSHenrik Rydberg 			ret = hid_open_report(hdev);
2609c500c971SJiri Slaby 			if (!ret)
261093c10132SJiri Slaby 				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2611c500c971SJiri Slaby 		}
2612a7197c2eSHenrik Rydberg 		if (ret) {
2613a7197c2eSHenrik Rydberg 			hid_close_report(hdev);
2614c500c971SJiri Slaby 			hdev->driver = NULL;
261585cdaf52SJiri Slaby 		}
2616a7197c2eSHenrik Rydberg 	}
2617ba623a77SDan Carpenter unlock:
2618c849a614SAndrew de los Reyes 	if (!hdev->io_started)
2619c849a614SAndrew de los Reyes 		up(&hdev->driver_input_lock);
26206f68f0acSBinoy Jayan end:
262185cdaf52SJiri Slaby 	return ret;
262285cdaf52SJiri Slaby }
262385cdaf52SJiri Slaby 
2624fc7a6209SUwe Kleine-König static void hid_device_remove(struct device *dev)
262585cdaf52SJiri Slaby {
2626ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
26274ea54542SDavid Herrmann 	struct hid_driver *hdrv;
262885cdaf52SJiri Slaby 
2629f2145f8dSDmitry Torokhov 	down(&hdev->driver_input_lock);
2630c849a614SAndrew de los Reyes 	hdev->io_started = false;
26314ea54542SDavid Herrmann 
26324ea54542SDavid Herrmann 	hdrv = hdev->driver;
263385cdaf52SJiri Slaby 	if (hdrv) {
263485cdaf52SJiri Slaby 		if (hdrv->remove)
263585cdaf52SJiri Slaby 			hdrv->remove(hdev);
2636c500c971SJiri Slaby 		else /* default remove */
2637c500c971SJiri Slaby 			hid_hw_stop(hdev);
2638a7197c2eSHenrik Rydberg 		hid_close_report(hdev);
263985cdaf52SJiri Slaby 		hdev->driver = NULL;
264085cdaf52SJiri Slaby 	}
264185cdaf52SJiri Slaby 
2642c849a614SAndrew de los Reyes 	if (!hdev->io_started)
2643c849a614SAndrew de los Reyes 		up(&hdev->driver_input_lock);
264485cdaf52SJiri Slaby }
264585cdaf52SJiri Slaby 
26464d53b801SHenrik Rydberg static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
26474d53b801SHenrik Rydberg 			     char *buf)
26484d53b801SHenrik Rydberg {
26494d53b801SHenrik Rydberg 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
26504d53b801SHenrik Rydberg 
2651dfa0c5faSRasmus Villemoes 	return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
26524d53b801SHenrik Rydberg 			 hdev->bus, hdev->group, hdev->vendor, hdev->product);
26534d53b801SHenrik Rydberg }
26540d4260e0SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
26554d53b801SHenrik Rydberg 
26560d4260e0SGreg Kroah-Hartman static struct attribute *hid_dev_attrs[] = {
26570d4260e0SGreg Kroah-Hartman 	&dev_attr_modalias.attr,
26580d4260e0SGreg Kroah-Hartman 	NULL,
26594d53b801SHenrik Rydberg };
266054f32fd5SAndy Lutomirski static struct bin_attribute *hid_dev_bin_attrs[] = {
266154f32fd5SAndy Lutomirski 	&dev_bin_attr_report_desc,
266254f32fd5SAndy Lutomirski 	NULL
266354f32fd5SAndy Lutomirski };
266454f32fd5SAndy Lutomirski static const struct attribute_group hid_dev_group = {
266554f32fd5SAndy Lutomirski 	.attrs = hid_dev_attrs,
266654f32fd5SAndy Lutomirski 	.bin_attrs = hid_dev_bin_attrs,
266754f32fd5SAndy Lutomirski };
266854f32fd5SAndy Lutomirski __ATTRIBUTE_GROUPS(hid_dev);
26694d53b801SHenrik Rydberg 
267085cdaf52SJiri Slaby static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
267185cdaf52SJiri Slaby {
2672ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
267385cdaf52SJiri Slaby 
267485cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
267585cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
267685cdaf52SJiri Slaby 		return -ENOMEM;
267785cdaf52SJiri Slaby 
267885cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
267985cdaf52SJiri Slaby 		return -ENOMEM;
268085cdaf52SJiri Slaby 
268185cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
268285cdaf52SJiri Slaby 		return -ENOMEM;
268385cdaf52SJiri Slaby 
268485cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
268585cdaf52SJiri Slaby 		return -ENOMEM;
268685cdaf52SJiri Slaby 
26874d53b801SHenrik Rydberg 	if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
26884d53b801SHenrik Rydberg 			   hdev->bus, hdev->group, hdev->vendor, hdev->product))
268985cdaf52SJiri Slaby 		return -ENOMEM;
269085cdaf52SJiri Slaby 
269185cdaf52SJiri Slaby 	return 0;
269285cdaf52SJiri Slaby }
269385cdaf52SJiri Slaby 
2694e04a0442SBenjamin Tissoires struct bus_type hid_bus_type = {
269585cdaf52SJiri Slaby 	.name		= "hid",
26960d4260e0SGreg Kroah-Hartman 	.dev_groups	= hid_dev_groups,
2697c2810325SGreg Kroah-Hartman 	.drv_groups	= hid_drv_groups,
269885cdaf52SJiri Slaby 	.match		= hid_bus_match,
269985cdaf52SJiri Slaby 	.probe		= hid_device_probe,
270085cdaf52SJiri Slaby 	.remove		= hid_device_remove,
270185cdaf52SJiri Slaby 	.uevent		= hid_uevent,
270285cdaf52SJiri Slaby };
2703d5d3e202SBenjamin Tissoires EXPORT_SYMBOL(hid_bus_type);
2704d458a9dfSJiri Slaby 
270585cdaf52SJiri Slaby int hid_add_device(struct hid_device *hdev)
270685cdaf52SJiri Slaby {
270785cdaf52SJiri Slaby 	static atomic_t id = ATOMIC_INIT(0);
270885cdaf52SJiri Slaby 	int ret;
270985cdaf52SJiri Slaby 
271085cdaf52SJiri Slaby 	if (WARN_ON(hdev->status & HID_STAT_ADDED))
271185cdaf52SJiri Slaby 		return -EBUSY;
271285cdaf52SJiri Slaby 
2713d5d3e202SBenjamin Tissoires 	hdev->quirks = hid_lookup_quirk(hdev);
2714d5d3e202SBenjamin Tissoires 
2715d458a9dfSJiri Slaby 	/* we need to kill them here, otherwise they will stay allocated to
2716d458a9dfSJiri Slaby 	 * wait for coming driver */
27174529eefaSLamarque V. Souza 	if (hid_ignore(hdev))
2718d458a9dfSJiri Slaby 		return -ENODEV;
2719d458a9dfSJiri Slaby 
2720a7197c2eSHenrik Rydberg 	/*
27213c86726cSBenjamin Tissoires 	 * Check for the mandatory transport channel.
27223c86726cSBenjamin Tissoires 	 */
27233c86726cSBenjamin Tissoires 	 if (!hdev->ll_driver->raw_request) {
27243c86726cSBenjamin Tissoires 		hid_err(hdev, "transport driver missing .raw_request()\n");
27253c86726cSBenjamin Tissoires 		return -EINVAL;
27263c86726cSBenjamin Tissoires 	 }
27273c86726cSBenjamin Tissoires 
27283c86726cSBenjamin Tissoires 	/*
2729a7197c2eSHenrik Rydberg 	 * Read the device report descriptor once and use as template
2730a7197c2eSHenrik Rydberg 	 * for the driver-specific modifications.
2731a7197c2eSHenrik Rydberg 	 */
2732a7197c2eSHenrik Rydberg 	ret = hdev->ll_driver->parse(hdev);
2733a7197c2eSHenrik Rydberg 	if (ret)
2734a7197c2eSHenrik Rydberg 		return ret;
2735a7197c2eSHenrik Rydberg 	if (!hdev->dev_rdesc)
2736a7197c2eSHenrik Rydberg 		return -ENODEV;
2737a7197c2eSHenrik Rydberg 
2738734c6609SHenrik Rydberg 	/*
2739734c6609SHenrik Rydberg 	 * Scan generic devices for group information
2740734c6609SHenrik Rydberg 	 */
27414392bf33SBenjamin Tissoires 	if (hid_ignore_special_drivers) {
27424392bf33SBenjamin Tissoires 		hdev->group = HID_GROUP_GENERIC;
27434392bf33SBenjamin Tissoires 	} else if (!hdev->group &&
27446e65d9d5SBenjamin Tissoires 		   !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
2745734c6609SHenrik Rydberg 		ret = hid_scan_report(hdev);
2746734c6609SHenrik Rydberg 		if (ret)
2747734c6609SHenrik Rydberg 			hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2748734c6609SHenrik Rydberg 	}
2749734c6609SHenrik Rydberg 
27501e839143SBenjamin Tissoires 	hdev->id = atomic_inc_return(&id);
27511e839143SBenjamin Tissoires 
27526bbe586fSKay Sievers 	/* XXX hack, any other cleaner solution after the driver core
27536bbe586fSKay Sievers 	 * is converted to allow more than 20 bytes as the device name? */
27546bbe586fSKay Sievers 	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
27551e839143SBenjamin Tissoires 		     hdev->vendor, hdev->product, hdev->id);
275685cdaf52SJiri Slaby 
27574da361b6SBruno Prémont 	hid_debug_register(hdev, dev_name(&hdev->dev));
275885cdaf52SJiri Slaby 	ret = device_add(&hdev->dev);
275985cdaf52SJiri Slaby 	if (!ret)
276085cdaf52SJiri Slaby 		hdev->status |= HID_STAT_ADDED;
27614da361b6SBruno Prémont 	else
27624da361b6SBruno Prémont 		hid_debug_unregister(hdev);
2763a635f9ddSJiri Kosina 
276485cdaf52SJiri Slaby 	return ret;
276585cdaf52SJiri Slaby }
276685cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_add_device);
276785cdaf52SJiri Slaby 
276885cdaf52SJiri Slaby /**
276985cdaf52SJiri Slaby  * hid_allocate_device - allocate new hid device descriptor
277085cdaf52SJiri Slaby  *
277185cdaf52SJiri Slaby  * Allocate and initialize hid device, so that hid_destroy_device might be
277285cdaf52SJiri Slaby  * used to free it.
277385cdaf52SJiri Slaby  *
277485cdaf52SJiri Slaby  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
277585cdaf52SJiri Slaby  * error value.
277685cdaf52SJiri Slaby  */
277785cdaf52SJiri Slaby struct hid_device *hid_allocate_device(void)
277885cdaf52SJiri Slaby {
277985cdaf52SJiri Slaby 	struct hid_device *hdev;
278085cdaf52SJiri Slaby 	int ret = -ENOMEM;
278185cdaf52SJiri Slaby 
278285cdaf52SJiri Slaby 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
278385cdaf52SJiri Slaby 	if (hdev == NULL)
278485cdaf52SJiri Slaby 		return ERR_PTR(ret);
278585cdaf52SJiri Slaby 
278685cdaf52SJiri Slaby 	device_initialize(&hdev->dev);
278785cdaf52SJiri Slaby 	hdev->dev.release = hid_device_release;
278885cdaf52SJiri Slaby 	hdev->dev.bus = &hid_bus_type;
278964bebefcSFu, Zhonghui 	device_enable_async_suspend(&hdev->dev);
279085cdaf52SJiri Slaby 
2791a7197c2eSHenrik Rydberg 	hid_close_report(hdev);
279285cdaf52SJiri Slaby 
2793cd667ce2SJiri Kosina 	init_waitqueue_head(&hdev->debug_wait);
2794cd667ce2SJiri Kosina 	INIT_LIST_HEAD(&hdev->debug_list);
27951deb9d34SJiri Kosina 	spin_lock_init(&hdev->debug_list_lock);
2796c849a614SAndrew de los Reyes 	sema_init(&hdev->driver_input_lock, 1);
2797aaac082dSDmitry Torokhov 	mutex_init(&hdev->ll_open_lock);
2798cd667ce2SJiri Kosina 
279985cdaf52SJiri Slaby 	return hdev;
280085cdaf52SJiri Slaby }
280185cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_allocate_device);
280285cdaf52SJiri Slaby 
280385cdaf52SJiri Slaby static void hid_remove_device(struct hid_device *hdev)
280485cdaf52SJiri Slaby {
280585cdaf52SJiri Slaby 	if (hdev->status & HID_STAT_ADDED) {
280685cdaf52SJiri Slaby 		device_del(&hdev->dev);
2807a635f9ddSJiri Kosina 		hid_debug_unregister(hdev);
280885cdaf52SJiri Slaby 		hdev->status &= ~HID_STAT_ADDED;
280985cdaf52SJiri Slaby 	}
2810a7197c2eSHenrik Rydberg 	kfree(hdev->dev_rdesc);
2811a7197c2eSHenrik Rydberg 	hdev->dev_rdesc = NULL;
2812a7197c2eSHenrik Rydberg 	hdev->dev_rsize = 0;
281385cdaf52SJiri Slaby }
281485cdaf52SJiri Slaby 
281585cdaf52SJiri Slaby /**
281685cdaf52SJiri Slaby  * hid_destroy_device - free previously allocated device
281785cdaf52SJiri Slaby  *
281885cdaf52SJiri Slaby  * @hdev: hid device
281985cdaf52SJiri Slaby  *
282085cdaf52SJiri Slaby  * If you allocate hid_device through hid_allocate_device, you should ever
282185cdaf52SJiri Slaby  * free by this function.
282285cdaf52SJiri Slaby  */
282385cdaf52SJiri Slaby void hid_destroy_device(struct hid_device *hdev)
282485cdaf52SJiri Slaby {
282585cdaf52SJiri Slaby 	hid_remove_device(hdev);
282685cdaf52SJiri Slaby 	put_device(&hdev->dev);
282785cdaf52SJiri Slaby }
282885cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_destroy_device);
282985cdaf52SJiri Slaby 
2830e04a0442SBenjamin Tissoires 
2831c17a7476SBenjamin Tissoires static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
2832e04a0442SBenjamin Tissoires {
2833c17a7476SBenjamin Tissoires 	struct hid_driver *hdrv = data;
2834c17a7476SBenjamin Tissoires 	struct hid_device *hdev = to_hid_device(dev);
2835c17a7476SBenjamin Tissoires 
2836c17a7476SBenjamin Tissoires 	if (hdev->driver == hdrv &&
28378f732850SBenjamin Tissoires 	    !hdrv->match(hdev, hid_ignore_special_drivers) &&
28388f732850SBenjamin Tissoires 	    !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
2839c17a7476SBenjamin Tissoires 		return device_reprobe(dev);
2840c17a7476SBenjamin Tissoires 
2841c17a7476SBenjamin Tissoires 	return 0;
2842c17a7476SBenjamin Tissoires }
2843c17a7476SBenjamin Tissoires 
2844c17a7476SBenjamin Tissoires static int __hid_bus_driver_added(struct device_driver *drv, void *data)
2845c17a7476SBenjamin Tissoires {
2846e04a0442SBenjamin Tissoires 	struct hid_driver *hdrv = to_hid_driver(drv);
2847e04a0442SBenjamin Tissoires 
2848c17a7476SBenjamin Tissoires 	if (hdrv->match) {
2849c17a7476SBenjamin Tissoires 		bus_for_each_dev(&hid_bus_type, NULL, hdrv,
2850c17a7476SBenjamin Tissoires 				 __hid_bus_reprobe_drivers);
2851c17a7476SBenjamin Tissoires 	}
2852e04a0442SBenjamin Tissoires 
2853e04a0442SBenjamin Tissoires 	return 0;
2854e04a0442SBenjamin Tissoires }
2855e04a0442SBenjamin Tissoires 
2856e04a0442SBenjamin Tissoires static int __bus_removed_driver(struct device_driver *drv, void *data)
2857e04a0442SBenjamin Tissoires {
2858c17a7476SBenjamin Tissoires 	return bus_rescan_devices(&hid_bus_type);
2859e04a0442SBenjamin Tissoires }
2860e04a0442SBenjamin Tissoires 
286185cdaf52SJiri Slaby int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
286285cdaf52SJiri Slaby 		const char *mod_name)
286385cdaf52SJiri Slaby {
2864c17a7476SBenjamin Tissoires 	int ret;
2865c17a7476SBenjamin Tissoires 
286685cdaf52SJiri Slaby 	hdrv->driver.name = hdrv->name;
286785cdaf52SJiri Slaby 	hdrv->driver.bus = &hid_bus_type;
286885cdaf52SJiri Slaby 	hdrv->driver.owner = owner;
286985cdaf52SJiri Slaby 	hdrv->driver.mod_name = mod_name;
287085cdaf52SJiri Slaby 
28713a6f82f7SJiri Slaby 	INIT_LIST_HEAD(&hdrv->dyn_list);
28723a6f82f7SJiri Slaby 	spin_lock_init(&hdrv->dyn_lock);
28733a6f82f7SJiri Slaby 
2874c17a7476SBenjamin Tissoires 	ret = driver_register(&hdrv->driver);
2875e04a0442SBenjamin Tissoires 
2876c17a7476SBenjamin Tissoires 	if (ret == 0)
2877c17a7476SBenjamin Tissoires 		bus_for_each_drv(&hid_bus_type, NULL, NULL,
2878c17a7476SBenjamin Tissoires 				 __hid_bus_driver_added);
2879c17a7476SBenjamin Tissoires 
2880c17a7476SBenjamin Tissoires 	return ret;
288185cdaf52SJiri Slaby }
288285cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(__hid_register_driver);
288385cdaf52SJiri Slaby 
288485cdaf52SJiri Slaby void hid_unregister_driver(struct hid_driver *hdrv)
288585cdaf52SJiri Slaby {
288685cdaf52SJiri Slaby 	driver_unregister(&hdrv->driver);
28873a6f82f7SJiri Slaby 	hid_free_dynids(hdrv);
2888e04a0442SBenjamin Tissoires 
2889e04a0442SBenjamin Tissoires 	bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
289085cdaf52SJiri Slaby }
289185cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_unregister_driver);
289285cdaf52SJiri Slaby 
28930361a28dSOliver Neukum int hid_check_keys_pressed(struct hid_device *hid)
28940361a28dSOliver Neukum {
28950361a28dSOliver Neukum 	struct hid_input *hidinput;
28960361a28dSOliver Neukum 	int i;
28970361a28dSOliver Neukum 
2898e5288eb5SJiri Kosina 	if (!(hid->claimed & HID_CLAIMED_INPUT))
2899e5288eb5SJiri Kosina 		return 0;
2900e5288eb5SJiri Kosina 
29010361a28dSOliver Neukum 	list_for_each_entry(hidinput, &hid->inputs, list) {
29020361a28dSOliver Neukum 		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
29030361a28dSOliver Neukum 			if (hidinput->input->key[i])
29040361a28dSOliver Neukum 				return 1;
29050361a28dSOliver Neukum 	}
29060361a28dSOliver Neukum 
29070361a28dSOliver Neukum 	return 0;
29080361a28dSOliver Neukum }
29090361a28dSOliver Neukum EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
29100361a28dSOliver Neukum 
291186166b7bSJiri Kosina static int __init hid_init(void)
291286166b7bSJiri Kosina {
291385cdaf52SJiri Slaby 	int ret;
291485cdaf52SJiri Slaby 
2915a635f9ddSJiri Kosina 	if (hid_debug)
29164291ee30SJoe Perches 		pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
29174291ee30SJoe Perches 			"debugfs is now used for inspecting the device (report descriptor, reports)\n");
2918a635f9ddSJiri Kosina 
291985cdaf52SJiri Slaby 	ret = bus_register(&hid_bus_type);
292085cdaf52SJiri Slaby 	if (ret) {
29214291ee30SJoe Perches 		pr_err("can't register hid bus\n");
292285cdaf52SJiri Slaby 		goto err;
292385cdaf52SJiri Slaby 	}
292485cdaf52SJiri Slaby 
292585cdaf52SJiri Slaby 	ret = hidraw_init();
292685cdaf52SJiri Slaby 	if (ret)
292785cdaf52SJiri Slaby 		goto err_bus;
292885cdaf52SJiri Slaby 
2929a635f9ddSJiri Kosina 	hid_debug_init();
2930a635f9ddSJiri Kosina 
293185cdaf52SJiri Slaby 	return 0;
293285cdaf52SJiri Slaby err_bus:
293385cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
293485cdaf52SJiri Slaby err:
293585cdaf52SJiri Slaby 	return ret;
293686166b7bSJiri Kosina }
293786166b7bSJiri Kosina 
293886166b7bSJiri Kosina static void __exit hid_exit(void)
293986166b7bSJiri Kosina {
2940a635f9ddSJiri Kosina 	hid_debug_exit();
294186166b7bSJiri Kosina 	hidraw_exit();
294285cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
2943d5d3e202SBenjamin Tissoires 	hid_quirks_exit(HID_BUS_ANY);
294486166b7bSJiri Kosina }
294586166b7bSJiri Kosina 
294686166b7bSJiri Kosina module_init(hid_init);
294786166b7bSJiri Kosina module_exit(hid_exit);
294886166b7bSJiri Kosina 
294988adb72bSJiri Kosina MODULE_AUTHOR("Andreas Gal");
295088adb72bSJiri Kosina MODULE_AUTHOR("Vojtech Pavlik");
295188adb72bSJiri Kosina MODULE_AUTHOR("Jiri Kosina");
29527021b600SGrant Grundler MODULE_LICENSE("GPL");
2953