xref: /openbmc/linux/drivers/hid/hid-core.c (revision 0bd88dd3dd5e73e5d43b8d1e7a96b841978df562)
1dde5845aSJiri Kosina /*
2229695e5SJiri Kosina  *  HID support for Linux
3dde5845aSJiri Kosina  *
4dde5845aSJiri Kosina  *  Copyright (c) 1999 Andreas Gal
5dde5845aSJiri Kosina  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6dde5845aSJiri Kosina  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
76b1968d5SJiri Kosina  *  Copyright (c) 2006-2012 Jiri Kosina
8dde5845aSJiri Kosina  */
9dde5845aSJiri Kosina 
10dde5845aSJiri Kosina /*
11dde5845aSJiri Kosina  * This program is free software; you can redistribute it and/or modify it
12dde5845aSJiri Kosina  * under the terms of the GNU General Public License as published by the Free
13dde5845aSJiri Kosina  * Software Foundation; either version 2 of the License, or (at your option)
14dde5845aSJiri Kosina  * any later version.
15dde5845aSJiri Kosina  */
16dde5845aSJiri Kosina 
174291ee30SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
184291ee30SJoe Perches 
19dde5845aSJiri Kosina #include <linux/module.h>
20dde5845aSJiri Kosina #include <linux/slab.h>
21dde5845aSJiri Kosina #include <linux/init.h>
22dde5845aSJiri Kosina #include <linux/kernel.h>
23dde5845aSJiri Kosina #include <linux/list.h>
24dde5845aSJiri Kosina #include <linux/mm.h>
25dde5845aSJiri Kosina #include <linux/spinlock.h>
26dde5845aSJiri Kosina #include <asm/unaligned.h>
27dde5845aSJiri Kosina #include <asm/byteorder.h>
28dde5845aSJiri Kosina #include <linux/input.h>
29dde5845aSJiri Kosina #include <linux/wait.h>
3047a80edbSJiri Kosina #include <linux/vmalloc.h>
31c4124c9bSJiri Kosina #include <linux/sched.h>
324ea54542SDavid Herrmann #include <linux/semaphore.h>
33dde5845aSJiri Kosina 
34dde5845aSJiri Kosina #include <linux/hid.h>
35dde5845aSJiri Kosina #include <linux/hiddev.h>
36c080d89aSJiri Kosina #include <linux/hid-debug.h>
3786166b7bSJiri Kosina #include <linux/hidraw.h>
38dde5845aSJiri Kosina 
395f22a799SJiri Slaby #include "hid-ids.h"
405f22a799SJiri Slaby 
41dde5845aSJiri Kosina /*
42dde5845aSJiri Kosina  * Version Information
43dde5845aSJiri Kosina  */
44dde5845aSJiri Kosina 
4553149801SJiri Kosina #define DRIVER_DESC "HID core driver"
46dde5845aSJiri Kosina #define DRIVER_LICENSE "GPL"
47dde5845aSJiri Kosina 
4858037eb9SJiri Kosina int hid_debug = 0;
49377e10fbSAnssi Hannula module_param_named(debug, hid_debug, int, 0600);
50cd667ce2SJiri Kosina MODULE_PARM_DESC(debug, "toggle HID debugging messages");
5158037eb9SJiri Kosina EXPORT_SYMBOL_GPL(hid_debug);
5258037eb9SJiri Kosina 
536b1968d5SJiri Kosina static int hid_ignore_special_drivers = 0;
546b1968d5SJiri Kosina module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
556b1968d5SJiri Kosina MODULE_PARM_DESC(debug, "Ignore any special drivers and handle all devices by generic driver");
566b1968d5SJiri Kosina 
57dde5845aSJiri Kosina /*
58dde5845aSJiri Kosina  * Register a new report for a device.
59dde5845aSJiri Kosina  */
60dde5845aSJiri Kosina 
6190a006abSMichael Poole struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
62dde5845aSJiri Kosina {
63dde5845aSJiri Kosina 	struct hid_report_enum *report_enum = device->report_enum + type;
64dde5845aSJiri Kosina 	struct hid_report *report;
65dde5845aSJiri Kosina 
6643622021SKees Cook 	if (id >= HID_MAX_IDS)
6743622021SKees Cook 		return NULL;
68dde5845aSJiri Kosina 	if (report_enum->report_id_hash[id])
69dde5845aSJiri Kosina 		return report_enum->report_id_hash[id];
70dde5845aSJiri Kosina 
71a3789a17SJoe Perches 	report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
72a3789a17SJoe Perches 	if (!report)
73dde5845aSJiri Kosina 		return NULL;
74dde5845aSJiri Kosina 
75dde5845aSJiri Kosina 	if (id != 0)
76dde5845aSJiri Kosina 		report_enum->numbered = 1;
77dde5845aSJiri Kosina 
78dde5845aSJiri Kosina 	report->id = id;
79dde5845aSJiri Kosina 	report->type = type;
80dde5845aSJiri Kosina 	report->size = 0;
81dde5845aSJiri Kosina 	report->device = device;
82dde5845aSJiri Kosina 	report_enum->report_id_hash[id] = report;
83dde5845aSJiri Kosina 
84dde5845aSJiri Kosina 	list_add_tail(&report->list, &report_enum->report_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 
94dde5845aSJiri Kosina static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
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) +
105a3789a17SJoe Perches 			 values * sizeof(unsigned)), 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);
113dde5845aSJiri Kosina 	field->report = report;
114dde5845aSJiri Kosina 
115dde5845aSJiri Kosina 	return field;
116dde5845aSJiri Kosina }
117dde5845aSJiri Kosina 
118dde5845aSJiri Kosina /*
119dde5845aSJiri Kosina  * Open a collection. The type/usage is pushed on the stack.
120dde5845aSJiri Kosina  */
121dde5845aSJiri Kosina 
122dde5845aSJiri Kosina static int open_collection(struct hid_parser *parser, unsigned type)
123dde5845aSJiri Kosina {
124dde5845aSJiri Kosina 	struct hid_collection *collection;
125dde5845aSJiri Kosina 	unsigned usage;
126dde5845aSJiri Kosina 
127dde5845aSJiri Kosina 	usage = parser->local.usage[0];
128dde5845aSJiri Kosina 
129dde5845aSJiri Kosina 	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
1308c3d52fcSJiri Kosina 		hid_err(parser->device, "collection stack overflow\n");
131a6fbaacfSSachin Kamat 		return -EINVAL;
132dde5845aSJiri Kosina 	}
133dde5845aSJiri Kosina 
134dde5845aSJiri Kosina 	if (parser->device->maxcollection == parser->device->collection_size) {
135dde5845aSJiri Kosina 		collection = kmalloc(sizeof(struct hid_collection) *
136dde5845aSJiri Kosina 				parser->device->collection_size * 2, GFP_KERNEL);
137dde5845aSJiri Kosina 		if (collection == NULL) {
1388c3d52fcSJiri Kosina 			hid_err(parser->device, "failed to reallocate collection array\n");
139a6fbaacfSSachin Kamat 			return -ENOMEM;
140dde5845aSJiri Kosina 		}
141dde5845aSJiri Kosina 		memcpy(collection, parser->device->collection,
142dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
143dde5845aSJiri Kosina 			parser->device->collection_size);
144dde5845aSJiri Kosina 		memset(collection + parser->device->collection_size, 0,
145dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
146dde5845aSJiri Kosina 			parser->device->collection_size);
147dde5845aSJiri Kosina 		kfree(parser->device->collection);
148dde5845aSJiri Kosina 		parser->device->collection = collection;
149dde5845aSJiri Kosina 		parser->device->collection_size *= 2;
150dde5845aSJiri Kosina 	}
151dde5845aSJiri Kosina 
152dde5845aSJiri Kosina 	parser->collection_stack[parser->collection_stack_ptr++] =
153dde5845aSJiri Kosina 		parser->device->maxcollection;
154dde5845aSJiri Kosina 
155dde5845aSJiri Kosina 	collection = parser->device->collection +
156dde5845aSJiri Kosina 		parser->device->maxcollection++;
157dde5845aSJiri Kosina 	collection->type = type;
158dde5845aSJiri Kosina 	collection->usage = usage;
159dde5845aSJiri Kosina 	collection->level = parser->collection_stack_ptr - 1;
160dde5845aSJiri Kosina 
161dde5845aSJiri Kosina 	if (type == HID_COLLECTION_APPLICATION)
162dde5845aSJiri Kosina 		parser->device->maxapplication++;
163dde5845aSJiri Kosina 
164dde5845aSJiri Kosina 	return 0;
165dde5845aSJiri Kosina }
166dde5845aSJiri Kosina 
167dde5845aSJiri Kosina /*
168dde5845aSJiri Kosina  * Close a collection.
169dde5845aSJiri Kosina  */
170dde5845aSJiri Kosina 
171dde5845aSJiri Kosina static int close_collection(struct hid_parser *parser)
172dde5845aSJiri Kosina {
173dde5845aSJiri Kosina 	if (!parser->collection_stack_ptr) {
1748c3d52fcSJiri Kosina 		hid_err(parser->device, "collection stack underflow\n");
175a6fbaacfSSachin Kamat 		return -EINVAL;
176dde5845aSJiri Kosina 	}
177dde5845aSJiri Kosina 	parser->collection_stack_ptr--;
178dde5845aSJiri Kosina 	return 0;
179dde5845aSJiri Kosina }
180dde5845aSJiri Kosina 
181dde5845aSJiri Kosina /*
182dde5845aSJiri Kosina  * Climb up the stack, search for the specified collection type
183dde5845aSJiri Kosina  * and return the usage.
184dde5845aSJiri Kosina  */
185dde5845aSJiri Kosina 
186dde5845aSJiri Kosina static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
187dde5845aSJiri Kosina {
188504499f2SJoe Perches 	struct hid_collection *collection = parser->device->collection;
189dde5845aSJiri Kosina 	int n;
190504499f2SJoe Perches 
191504499f2SJoe Perches 	for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
192504499f2SJoe Perches 		unsigned index = parser->collection_stack[n];
193504499f2SJoe Perches 		if (collection[index].type == type)
194504499f2SJoe Perches 			return collection[index].usage;
195504499f2SJoe Perches 	}
196dde5845aSJiri Kosina 	return 0; /* we know nothing about this usage type */
197dde5845aSJiri Kosina }
198dde5845aSJiri Kosina 
199dde5845aSJiri Kosina /*
200dde5845aSJiri Kosina  * Add a usage to the temporary parser table.
201dde5845aSJiri Kosina  */
202dde5845aSJiri Kosina 
203dde5845aSJiri Kosina static int hid_add_usage(struct hid_parser *parser, unsigned usage)
204dde5845aSJiri Kosina {
205dde5845aSJiri Kosina 	if (parser->local.usage_index >= HID_MAX_USAGES) {
2068c3d52fcSJiri Kosina 		hid_err(parser->device, "usage index exceeded\n");
207dde5845aSJiri Kosina 		return -1;
208dde5845aSJiri Kosina 	}
209dde5845aSJiri Kosina 	parser->local.usage[parser->local.usage_index] = usage;
210dde5845aSJiri Kosina 	parser->local.collection_index[parser->local.usage_index] =
211dde5845aSJiri Kosina 		parser->collection_stack_ptr ?
212dde5845aSJiri Kosina 		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
213dde5845aSJiri Kosina 	parser->local.usage_index++;
214dde5845aSJiri Kosina 	return 0;
215dde5845aSJiri Kosina }
216dde5845aSJiri Kosina 
217dde5845aSJiri Kosina /*
218dde5845aSJiri Kosina  * Register a new field for this report.
219dde5845aSJiri Kosina  */
220dde5845aSJiri Kosina 
221dde5845aSJiri Kosina static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
222dde5845aSJiri Kosina {
223dde5845aSJiri Kosina 	struct hid_report *report;
224dde5845aSJiri Kosina 	struct hid_field *field;
225cc6b54aaSBenjamin Tissoires 	unsigned usages;
226dde5845aSJiri Kosina 	unsigned offset;
227cc6b54aaSBenjamin Tissoires 	unsigned i;
228dde5845aSJiri Kosina 
229a3789a17SJoe Perches 	report = hid_register_report(parser->device, report_type, parser->global.report_id);
230a3789a17SJoe Perches 	if (!report) {
2318c3d52fcSJiri Kosina 		hid_err(parser->device, "hid_register_report failed\n");
232dde5845aSJiri Kosina 		return -1;
233dde5845aSJiri Kosina 	}
234dde5845aSJiri Kosina 
235bb2e1976SJiri Kosina 	/* Handle both signed and unsigned cases properly */
2360cd516c2Ssrinivas pandruvada 	if ((parser->global.logical_minimum < 0 &&
2370cd516c2Ssrinivas pandruvada 		parser->global.logical_maximum <
2380cd516c2Ssrinivas pandruvada 		parser->global.logical_minimum) ||
2390cd516c2Ssrinivas pandruvada 		(parser->global.logical_minimum >= 0 &&
2400cd516c2Ssrinivas pandruvada 		(__u32)parser->global.logical_maximum <
2410cd516c2Ssrinivas pandruvada 		(__u32)parser->global.logical_minimum)) {
2420cd516c2Ssrinivas pandruvada 		dbg_hid("logical range invalid 0x%x 0x%x\n",
2430cd516c2Ssrinivas pandruvada 			parser->global.logical_minimum,
2440cd516c2Ssrinivas pandruvada 			parser->global.logical_maximum);
245dde5845aSJiri Kosina 		return -1;
246dde5845aSJiri Kosina 	}
247dde5845aSJiri Kosina 
248dde5845aSJiri Kosina 	offset = report->size;
249dde5845aSJiri Kosina 	report->size += parser->global.report_size * parser->global.report_count;
250dde5845aSJiri Kosina 
251dde5845aSJiri Kosina 	if (!parser->local.usage_index) /* Ignore padding fields */
252dde5845aSJiri Kosina 		return 0;
253dde5845aSJiri Kosina 
254cc6b54aaSBenjamin Tissoires 	usages = max_t(unsigned, parser->local.usage_index,
255cc6b54aaSBenjamin Tissoires 				 parser->global.report_count);
256dde5845aSJiri Kosina 
257a3789a17SJoe Perches 	field = hid_register_field(report, usages, parser->global.report_count);
258a3789a17SJoe Perches 	if (!field)
259dde5845aSJiri Kosina 		return 0;
260dde5845aSJiri Kosina 
261dde5845aSJiri Kosina 	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
262dde5845aSJiri Kosina 	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
263dde5845aSJiri Kosina 	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
264dde5845aSJiri Kosina 
265dde5845aSJiri Kosina 	for (i = 0; i < usages; i++) {
266cc6b54aaSBenjamin Tissoires 		unsigned j = i;
267dde5845aSJiri Kosina 		/* Duplicate the last usage we parsed if we have excess values */
268dde5845aSJiri Kosina 		if (i >= parser->local.usage_index)
269dde5845aSJiri Kosina 			j = parser->local.usage_index - 1;
270dde5845aSJiri Kosina 		field->usage[i].hid = parser->local.usage[j];
271dde5845aSJiri Kosina 		field->usage[i].collection_index =
272dde5845aSJiri Kosina 			parser->local.collection_index[j];
273cc6b54aaSBenjamin Tissoires 		field->usage[i].usage_index = i;
274dde5845aSJiri Kosina 	}
275dde5845aSJiri Kosina 
276dde5845aSJiri Kosina 	field->maxusage = usages;
277dde5845aSJiri Kosina 	field->flags = flags;
278dde5845aSJiri Kosina 	field->report_offset = offset;
279dde5845aSJiri Kosina 	field->report_type = report_type;
280dde5845aSJiri Kosina 	field->report_size = parser->global.report_size;
281dde5845aSJiri Kosina 	field->report_count = parser->global.report_count;
282dde5845aSJiri Kosina 	field->logical_minimum = parser->global.logical_minimum;
283dde5845aSJiri Kosina 	field->logical_maximum = parser->global.logical_maximum;
284dde5845aSJiri Kosina 	field->physical_minimum = parser->global.physical_minimum;
285dde5845aSJiri Kosina 	field->physical_maximum = parser->global.physical_maximum;
286dde5845aSJiri Kosina 	field->unit_exponent = parser->global.unit_exponent;
287dde5845aSJiri Kosina 	field->unit = parser->global.unit;
288dde5845aSJiri Kosina 
289dde5845aSJiri Kosina 	return 0;
290dde5845aSJiri Kosina }
291dde5845aSJiri Kosina 
292dde5845aSJiri Kosina /*
293dde5845aSJiri Kosina  * Read data value from item.
294dde5845aSJiri Kosina  */
295dde5845aSJiri Kosina 
296dde5845aSJiri Kosina static u32 item_udata(struct hid_item *item)
297dde5845aSJiri Kosina {
298dde5845aSJiri Kosina 	switch (item->size) {
299dde5845aSJiri Kosina 	case 1: return item->data.u8;
300dde5845aSJiri Kosina 	case 2: return item->data.u16;
301dde5845aSJiri Kosina 	case 4: return item->data.u32;
302dde5845aSJiri Kosina 	}
303dde5845aSJiri Kosina 	return 0;
304dde5845aSJiri Kosina }
305dde5845aSJiri Kosina 
306dde5845aSJiri Kosina static s32 item_sdata(struct hid_item *item)
307dde5845aSJiri Kosina {
308dde5845aSJiri Kosina 	switch (item->size) {
309dde5845aSJiri Kosina 	case 1: return item->data.s8;
310dde5845aSJiri Kosina 	case 2: return item->data.s16;
311dde5845aSJiri Kosina 	case 4: return item->data.s32;
312dde5845aSJiri Kosina 	}
313dde5845aSJiri Kosina 	return 0;
314dde5845aSJiri Kosina }
315dde5845aSJiri Kosina 
316dde5845aSJiri Kosina /*
317dde5845aSJiri Kosina  * Process a global item.
318dde5845aSJiri Kosina  */
319dde5845aSJiri Kosina 
320dde5845aSJiri Kosina static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
321dde5845aSJiri Kosina {
322ad0e669bSNikolai Kondrashov 	__s32 raw_value;
323dde5845aSJiri Kosina 	switch (item->tag) {
324dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PUSH:
325dde5845aSJiri Kosina 
326dde5845aSJiri Kosina 		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
3278c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack overflow\n");
328dde5845aSJiri Kosina 			return -1;
329dde5845aSJiri Kosina 		}
330dde5845aSJiri Kosina 
331dde5845aSJiri Kosina 		memcpy(parser->global_stack + parser->global_stack_ptr++,
332dde5845aSJiri Kosina 			&parser->global, sizeof(struct hid_global));
333dde5845aSJiri Kosina 		return 0;
334dde5845aSJiri Kosina 
335dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_POP:
336dde5845aSJiri Kosina 
337dde5845aSJiri Kosina 		if (!parser->global_stack_ptr) {
3388c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack underflow\n");
339dde5845aSJiri Kosina 			return -1;
340dde5845aSJiri Kosina 		}
341dde5845aSJiri Kosina 
342880d29f1SJiri Slaby 		memcpy(&parser->global, parser->global_stack +
343880d29f1SJiri Slaby 			--parser->global_stack_ptr, sizeof(struct hid_global));
344dde5845aSJiri Kosina 		return 0;
345dde5845aSJiri Kosina 
346dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
347dde5845aSJiri Kosina 		parser->global.usage_page = item_udata(item);
348dde5845aSJiri Kosina 		return 0;
349dde5845aSJiri Kosina 
350dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
351dde5845aSJiri Kosina 		parser->global.logical_minimum = item_sdata(item);
352dde5845aSJiri Kosina 		return 0;
353dde5845aSJiri Kosina 
354dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
355dde5845aSJiri Kosina 		if (parser->global.logical_minimum < 0)
356dde5845aSJiri Kosina 			parser->global.logical_maximum = item_sdata(item);
357dde5845aSJiri Kosina 		else
358dde5845aSJiri Kosina 			parser->global.logical_maximum = item_udata(item);
359dde5845aSJiri Kosina 		return 0;
360dde5845aSJiri Kosina 
361dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
362dde5845aSJiri Kosina 		parser->global.physical_minimum = item_sdata(item);
363dde5845aSJiri Kosina 		return 0;
364dde5845aSJiri Kosina 
365dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
366dde5845aSJiri Kosina 		if (parser->global.physical_minimum < 0)
367dde5845aSJiri Kosina 			parser->global.physical_maximum = item_sdata(item);
368dde5845aSJiri Kosina 		else
369dde5845aSJiri Kosina 			parser->global.physical_maximum = item_udata(item);
370dde5845aSJiri Kosina 		return 0;
371dde5845aSJiri Kosina 
372dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
373ad0e669bSNikolai Kondrashov 		/* Many devices provide unit exponent as a two's complement
374ad0e669bSNikolai Kondrashov 		 * nibble due to the common misunderstanding of HID
375ad0e669bSNikolai Kondrashov 		 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
376ad0e669bSNikolai Kondrashov 		 * both this and the standard encoding. */
377ad0e669bSNikolai Kondrashov 		raw_value = item_sdata(item);
37877463838SBenjamin Tissoires 		if (!(raw_value & 0xfffffff0))
37977463838SBenjamin Tissoires 			parser->global.unit_exponent = hid_snto32(raw_value, 4);
38077463838SBenjamin Tissoires 		else
38177463838SBenjamin Tissoires 			parser->global.unit_exponent = raw_value;
382dde5845aSJiri Kosina 		return 0;
383dde5845aSJiri Kosina 
384dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT:
385dde5845aSJiri Kosina 		parser->global.unit = item_udata(item);
386dde5845aSJiri Kosina 		return 0;
387dde5845aSJiri Kosina 
388dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
389880d29f1SJiri Slaby 		parser->global.report_size = item_udata(item);
39023408f95SMarek Vasut 		if (parser->global.report_size > 128) {
3918c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_size %d\n",
392880d29f1SJiri Slaby 					parser->global.report_size);
393dde5845aSJiri Kosina 			return -1;
394dde5845aSJiri Kosina 		}
395dde5845aSJiri Kosina 		return 0;
396dde5845aSJiri Kosina 
397dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
398880d29f1SJiri Slaby 		parser->global.report_count = item_udata(item);
399880d29f1SJiri Slaby 		if (parser->global.report_count > HID_MAX_USAGES) {
4008c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_count %d\n",
401880d29f1SJiri Slaby 					parser->global.report_count);
402dde5845aSJiri Kosina 			return -1;
403dde5845aSJiri Kosina 		}
404dde5845aSJiri Kosina 		return 0;
405dde5845aSJiri Kosina 
406dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
407880d29f1SJiri Slaby 		parser->global.report_id = item_udata(item);
40843622021SKees Cook 		if (parser->global.report_id == 0 ||
40943622021SKees Cook 		    parser->global.report_id >= HID_MAX_IDS) {
41043622021SKees Cook 			hid_err(parser->device, "report_id %u is invalid\n",
41143622021SKees Cook 				parser->global.report_id);
412dde5845aSJiri Kosina 			return -1;
413dde5845aSJiri Kosina 		}
414dde5845aSJiri Kosina 		return 0;
415dde5845aSJiri Kosina 
416dde5845aSJiri Kosina 	default:
4178c3d52fcSJiri Kosina 		hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
418dde5845aSJiri Kosina 		return -1;
419dde5845aSJiri Kosina 	}
420dde5845aSJiri Kosina }
421dde5845aSJiri Kosina 
422dde5845aSJiri Kosina /*
423dde5845aSJiri Kosina  * Process a local item.
424dde5845aSJiri Kosina  */
425dde5845aSJiri Kosina 
426dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
427dde5845aSJiri Kosina {
428dde5845aSJiri Kosina 	__u32 data;
429dde5845aSJiri Kosina 	unsigned n;
430dde5845aSJiri Kosina 
431dde5845aSJiri Kosina 	data = item_udata(item);
432dde5845aSJiri Kosina 
433dde5845aSJiri Kosina 	switch (item->tag) {
434dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_DELIMITER:
435dde5845aSJiri Kosina 
436dde5845aSJiri Kosina 		if (data) {
437dde5845aSJiri Kosina 			/*
438dde5845aSJiri Kosina 			 * We treat items before the first delimiter
439dde5845aSJiri Kosina 			 * as global to all usage sets (branch 0).
440dde5845aSJiri Kosina 			 * In the moment we process only these global
441dde5845aSJiri Kosina 			 * items and the first delimiter set.
442dde5845aSJiri Kosina 			 */
443dde5845aSJiri Kosina 			if (parser->local.delimiter_depth != 0) {
4448c3d52fcSJiri Kosina 				hid_err(parser->device, "nested delimiters\n");
445dde5845aSJiri Kosina 				return -1;
446dde5845aSJiri Kosina 			}
447dde5845aSJiri Kosina 			parser->local.delimiter_depth++;
448dde5845aSJiri Kosina 			parser->local.delimiter_branch++;
449dde5845aSJiri Kosina 		} else {
450dde5845aSJiri Kosina 			if (parser->local.delimiter_depth < 1) {
4518c3d52fcSJiri Kosina 				hid_err(parser->device, "bogus close delimiter\n");
452dde5845aSJiri Kosina 				return -1;
453dde5845aSJiri Kosina 			}
454dde5845aSJiri Kosina 			parser->local.delimiter_depth--;
455dde5845aSJiri Kosina 		}
45638ead6efSPaul Chavent 		return 0;
457dde5845aSJiri Kosina 
458dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE:
459dde5845aSJiri Kosina 
460dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
46158037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
462dde5845aSJiri Kosina 			return 0;
463dde5845aSJiri Kosina 		}
464dde5845aSJiri Kosina 
465dde5845aSJiri Kosina 		if (item->size <= 2)
466dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
467dde5845aSJiri Kosina 
468dde5845aSJiri Kosina 		return hid_add_usage(parser, data);
469dde5845aSJiri Kosina 
470dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
471dde5845aSJiri Kosina 
472dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
47358037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
474dde5845aSJiri Kosina 			return 0;
475dde5845aSJiri Kosina 		}
476dde5845aSJiri Kosina 
477dde5845aSJiri Kosina 		if (item->size <= 2)
478dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
479dde5845aSJiri Kosina 
480dde5845aSJiri Kosina 		parser->local.usage_minimum = data;
481dde5845aSJiri Kosina 		return 0;
482dde5845aSJiri Kosina 
483dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
484dde5845aSJiri Kosina 
485dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
48658037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
487dde5845aSJiri Kosina 			return 0;
488dde5845aSJiri Kosina 		}
489dde5845aSJiri Kosina 
490dde5845aSJiri Kosina 		if (item->size <= 2)
491dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
492dde5845aSJiri Kosina 
493dde5845aSJiri Kosina 		for (n = parser->local.usage_minimum; n <= data; n++)
494dde5845aSJiri Kosina 			if (hid_add_usage(parser, n)) {
49558037eb9SJiri Kosina 				dbg_hid("hid_add_usage failed\n");
496dde5845aSJiri Kosina 				return -1;
497dde5845aSJiri Kosina 			}
498dde5845aSJiri Kosina 		return 0;
499dde5845aSJiri Kosina 
500dde5845aSJiri Kosina 	default:
501dde5845aSJiri Kosina 
50258037eb9SJiri Kosina 		dbg_hid("unknown local item tag 0x%x\n", item->tag);
503dde5845aSJiri Kosina 		return 0;
504dde5845aSJiri Kosina 	}
505dde5845aSJiri Kosina 	return 0;
506dde5845aSJiri Kosina }
507dde5845aSJiri Kosina 
508dde5845aSJiri Kosina /*
509dde5845aSJiri Kosina  * Process a main item.
510dde5845aSJiri Kosina  */
511dde5845aSJiri Kosina 
512dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
513dde5845aSJiri Kosina {
514dde5845aSJiri Kosina 	__u32 data;
515dde5845aSJiri Kosina 	int ret;
516dde5845aSJiri Kosina 
517dde5845aSJiri Kosina 	data = item_udata(item);
518dde5845aSJiri Kosina 
519dde5845aSJiri Kosina 	switch (item->tag) {
520dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
521dde5845aSJiri Kosina 		ret = open_collection(parser, data & 0xff);
522dde5845aSJiri Kosina 		break;
523dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
524dde5845aSJiri Kosina 		ret = close_collection(parser);
525dde5845aSJiri Kosina 		break;
526dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_INPUT:
527dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
528dde5845aSJiri Kosina 		break;
529dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_OUTPUT:
530dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
531dde5845aSJiri Kosina 		break;
532dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_FEATURE:
533dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
534dde5845aSJiri Kosina 		break;
535dde5845aSJiri Kosina 	default:
5368c3d52fcSJiri Kosina 		hid_err(parser->device, "unknown main item tag 0x%x\n", item->tag);
537dde5845aSJiri Kosina 		ret = 0;
538dde5845aSJiri Kosina 	}
539dde5845aSJiri Kosina 
540dde5845aSJiri Kosina 	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
541dde5845aSJiri Kosina 
542dde5845aSJiri Kosina 	return ret;
543dde5845aSJiri Kosina }
544dde5845aSJiri Kosina 
545dde5845aSJiri Kosina /*
546dde5845aSJiri Kosina  * Process a reserved item.
547dde5845aSJiri Kosina  */
548dde5845aSJiri Kosina 
549dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
550dde5845aSJiri Kosina {
55158037eb9SJiri Kosina 	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
552dde5845aSJiri Kosina 	return 0;
553dde5845aSJiri Kosina }
554dde5845aSJiri Kosina 
555dde5845aSJiri Kosina /*
556dde5845aSJiri Kosina  * Free a report and all registered fields. The field->usage and
557dde5845aSJiri Kosina  * field->value table's are allocated behind the field, so we need
558dde5845aSJiri Kosina  * only to free(field) itself.
559dde5845aSJiri Kosina  */
560dde5845aSJiri Kosina 
561dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report)
562dde5845aSJiri Kosina {
563dde5845aSJiri Kosina 	unsigned n;
564dde5845aSJiri Kosina 
565dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
566dde5845aSJiri Kosina 		kfree(report->field[n]);
567dde5845aSJiri Kosina 	kfree(report);
568dde5845aSJiri Kosina }
569dde5845aSJiri Kosina 
570dde5845aSJiri Kosina /*
571a7197c2eSHenrik Rydberg  * Close report. This function returns the device
572a7197c2eSHenrik Rydberg  * state to the point prior to hid_open_report().
573dde5845aSJiri Kosina  */
574a7197c2eSHenrik Rydberg static void hid_close_report(struct hid_device *device)
575dde5845aSJiri Kosina {
576dde5845aSJiri Kosina 	unsigned i, j;
577dde5845aSJiri Kosina 
578dde5845aSJiri Kosina 	for (i = 0; i < HID_REPORT_TYPES; i++) {
579dde5845aSJiri Kosina 		struct hid_report_enum *report_enum = device->report_enum + i;
580dde5845aSJiri Kosina 
58143622021SKees Cook 		for (j = 0; j < HID_MAX_IDS; j++) {
582dde5845aSJiri Kosina 			struct hid_report *report = report_enum->report_id_hash[j];
583dde5845aSJiri Kosina 			if (report)
584dde5845aSJiri Kosina 				hid_free_report(report);
585dde5845aSJiri Kosina 		}
586a7197c2eSHenrik Rydberg 		memset(report_enum, 0, sizeof(*report_enum));
587a7197c2eSHenrik Rydberg 		INIT_LIST_HEAD(&report_enum->report_list);
588dde5845aSJiri Kosina 	}
589dde5845aSJiri Kosina 
590dde5845aSJiri Kosina 	kfree(device->rdesc);
591a7197c2eSHenrik Rydberg 	device->rdesc = NULL;
592a7197c2eSHenrik Rydberg 	device->rsize = 0;
593a7197c2eSHenrik Rydberg 
594767fe787SJiri Kosina 	kfree(device->collection);
595a7197c2eSHenrik Rydberg 	device->collection = NULL;
596a7197c2eSHenrik Rydberg 	device->collection_size = 0;
597a7197c2eSHenrik Rydberg 	device->maxcollection = 0;
598a7197c2eSHenrik Rydberg 	device->maxapplication = 0;
599a7197c2eSHenrik Rydberg 
600a7197c2eSHenrik Rydberg 	device->status &= ~HID_STAT_PARSED;
601a7197c2eSHenrik Rydberg }
602a7197c2eSHenrik Rydberg 
603a7197c2eSHenrik Rydberg /*
604a7197c2eSHenrik Rydberg  * Free a device structure, all reports, and all fields.
605a7197c2eSHenrik Rydberg  */
606a7197c2eSHenrik Rydberg 
607a7197c2eSHenrik Rydberg static void hid_device_release(struct device *dev)
608a7197c2eSHenrik Rydberg {
609a7197c2eSHenrik Rydberg 	struct hid_device *hid = container_of(dev, struct hid_device, dev);
610a7197c2eSHenrik Rydberg 
611a7197c2eSHenrik Rydberg 	hid_close_report(hid);
612a7197c2eSHenrik Rydberg 	kfree(hid->dev_rdesc);
613a7197c2eSHenrik Rydberg 	kfree(hid);
614dde5845aSJiri Kosina }
615dde5845aSJiri Kosina 
616dde5845aSJiri Kosina /*
617dde5845aSJiri Kosina  * Fetch a report description item from the data stream. We support long
618dde5845aSJiri Kosina  * items, though they are not used yet.
619dde5845aSJiri Kosina  */
620dde5845aSJiri Kosina 
621dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
622dde5845aSJiri Kosina {
623dde5845aSJiri Kosina 	u8 b;
624dde5845aSJiri Kosina 
625dde5845aSJiri Kosina 	if ((end - start) <= 0)
626dde5845aSJiri Kosina 		return NULL;
627dde5845aSJiri Kosina 
628dde5845aSJiri Kosina 	b = *start++;
629dde5845aSJiri Kosina 
630dde5845aSJiri Kosina 	item->type = (b >> 2) & 3;
631dde5845aSJiri Kosina 	item->tag  = (b >> 4) & 15;
632dde5845aSJiri Kosina 
633dde5845aSJiri Kosina 	if (item->tag == HID_ITEM_TAG_LONG) {
634dde5845aSJiri Kosina 
635dde5845aSJiri Kosina 		item->format = HID_ITEM_FORMAT_LONG;
636dde5845aSJiri Kosina 
637dde5845aSJiri Kosina 		if ((end - start) < 2)
638dde5845aSJiri Kosina 			return NULL;
639dde5845aSJiri Kosina 
640dde5845aSJiri Kosina 		item->size = *start++;
641dde5845aSJiri Kosina 		item->tag  = *start++;
642dde5845aSJiri Kosina 
643dde5845aSJiri Kosina 		if ((end - start) < item->size)
644dde5845aSJiri Kosina 			return NULL;
645dde5845aSJiri Kosina 
646dde5845aSJiri Kosina 		item->data.longdata = start;
647dde5845aSJiri Kosina 		start += item->size;
648dde5845aSJiri Kosina 		return start;
649dde5845aSJiri Kosina 	}
650dde5845aSJiri Kosina 
651dde5845aSJiri Kosina 	item->format = HID_ITEM_FORMAT_SHORT;
652dde5845aSJiri Kosina 	item->size = b & 3;
653dde5845aSJiri Kosina 
654dde5845aSJiri Kosina 	switch (item->size) {
655dde5845aSJiri Kosina 	case 0:
656dde5845aSJiri Kosina 		return start;
657dde5845aSJiri Kosina 
658dde5845aSJiri Kosina 	case 1:
659dde5845aSJiri Kosina 		if ((end - start) < 1)
660dde5845aSJiri Kosina 			return NULL;
661dde5845aSJiri Kosina 		item->data.u8 = *start++;
662dde5845aSJiri Kosina 		return start;
663dde5845aSJiri Kosina 
664dde5845aSJiri Kosina 	case 2:
665dde5845aSJiri Kosina 		if ((end - start) < 2)
666dde5845aSJiri Kosina 			return NULL;
667c105068fSHarvey Harrison 		item->data.u16 = get_unaligned_le16(start);
668dde5845aSJiri Kosina 		start = (__u8 *)((__le16 *)start + 1);
669dde5845aSJiri Kosina 		return start;
670dde5845aSJiri Kosina 
671dde5845aSJiri Kosina 	case 3:
672dde5845aSJiri Kosina 		item->size++;
673dde5845aSJiri Kosina 		if ((end - start) < 4)
674dde5845aSJiri Kosina 			return NULL;
675c105068fSHarvey Harrison 		item->data.u32 = get_unaligned_le32(start);
676dde5845aSJiri Kosina 		start = (__u8 *)((__le32 *)start + 1);
677dde5845aSJiri Kosina 		return start;
678dde5845aSJiri Kosina 	}
679dde5845aSJiri Kosina 
680dde5845aSJiri Kosina 	return NULL;
681dde5845aSJiri Kosina }
682dde5845aSJiri Kosina 
6833dc8fc08SBenjamin Tissoires static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
684734c6609SHenrik Rydberg {
6853dc8fc08SBenjamin Tissoires 	struct hid_device *hid = parser->device;
6863dc8fc08SBenjamin Tissoires 
6874fa3a583SHenrik Rydberg 	if (usage == HID_DG_CONTACTID)
6884fa3a583SHenrik Rydberg 		hid->group = HID_GROUP_MULTITOUCH;
689734c6609SHenrik Rydberg }
690734c6609SHenrik Rydberg 
691f961bd35SBenjamin Tissoires static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
692f961bd35SBenjamin Tissoires {
693f961bd35SBenjamin Tissoires 	if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
694f961bd35SBenjamin Tissoires 	    parser->global.report_size == 8)
695f961bd35SBenjamin Tissoires 		parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
696f961bd35SBenjamin Tissoires }
697f961bd35SBenjamin Tissoires 
6983dc8fc08SBenjamin Tissoires static void hid_scan_collection(struct hid_parser *parser, unsigned type)
6993dc8fc08SBenjamin Tissoires {
7003dc8fc08SBenjamin Tissoires 	struct hid_device *hid = parser->device;
7013dc8fc08SBenjamin Tissoires 
7023dc8fc08SBenjamin Tissoires 	if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
7033dc8fc08SBenjamin Tissoires 	    type == HID_COLLECTION_PHYSICAL)
7043dc8fc08SBenjamin Tissoires 		hid->group = HID_GROUP_SENSOR_HUB;
7053dc8fc08SBenjamin Tissoires }
7063dc8fc08SBenjamin Tissoires 
7073dc8fc08SBenjamin Tissoires static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
7083dc8fc08SBenjamin Tissoires {
7093dc8fc08SBenjamin Tissoires 	__u32 data;
7103dc8fc08SBenjamin Tissoires 	int i;
7113dc8fc08SBenjamin Tissoires 
7123dc8fc08SBenjamin Tissoires 	data = item_udata(item);
7133dc8fc08SBenjamin Tissoires 
7143dc8fc08SBenjamin Tissoires 	switch (item->tag) {
7153dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
7163dc8fc08SBenjamin Tissoires 		hid_scan_collection(parser, data & 0xff);
7173dc8fc08SBenjamin Tissoires 		break;
7183dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
7193dc8fc08SBenjamin Tissoires 		break;
7203dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_INPUT:
7213dc8fc08SBenjamin Tissoires 		for (i = 0; i < parser->local.usage_index; i++)
7223dc8fc08SBenjamin Tissoires 			hid_scan_input_usage(parser, parser->local.usage[i]);
7233dc8fc08SBenjamin Tissoires 		break;
7243dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_OUTPUT:
7253dc8fc08SBenjamin Tissoires 		break;
7263dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_FEATURE:
727f961bd35SBenjamin Tissoires 		for (i = 0; i < parser->local.usage_index; i++)
728f961bd35SBenjamin Tissoires 			hid_scan_feature_usage(parser, parser->local.usage[i]);
7293dc8fc08SBenjamin Tissoires 		break;
7303dc8fc08SBenjamin Tissoires 	}
7313dc8fc08SBenjamin Tissoires 
7323dc8fc08SBenjamin Tissoires 	/* Reset the local parser environment */
7333dc8fc08SBenjamin Tissoires 	memset(&parser->local, 0, sizeof(parser->local));
7343dc8fc08SBenjamin Tissoires 
7353dc8fc08SBenjamin Tissoires 	return 0;
7363dc8fc08SBenjamin Tissoires }
7373dc8fc08SBenjamin Tissoires 
738734c6609SHenrik Rydberg /*
739734c6609SHenrik Rydberg  * Scan a report descriptor before the device is added to the bus.
740734c6609SHenrik Rydberg  * Sets device groups and other properties that determine what driver
741734c6609SHenrik Rydberg  * to load.
742734c6609SHenrik Rydberg  */
743734c6609SHenrik Rydberg static int hid_scan_report(struct hid_device *hid)
744734c6609SHenrik Rydberg {
7453dc8fc08SBenjamin Tissoires 	struct hid_parser *parser;
7463dc8fc08SBenjamin Tissoires 	struct hid_item item;
747734c6609SHenrik Rydberg 	__u8 *start = hid->dev_rdesc;
748734c6609SHenrik Rydberg 	__u8 *end = start + hid->dev_rsize;
7493dc8fc08SBenjamin Tissoires 	static int (*dispatch_type[])(struct hid_parser *parser,
7503dc8fc08SBenjamin Tissoires 				      struct hid_item *item) = {
7513dc8fc08SBenjamin Tissoires 		hid_scan_main,
7523dc8fc08SBenjamin Tissoires 		hid_parser_global,
7533dc8fc08SBenjamin Tissoires 		hid_parser_local,
7543dc8fc08SBenjamin Tissoires 		hid_parser_reserved
7553dc8fc08SBenjamin Tissoires 	};
756734c6609SHenrik Rydberg 
7573dc8fc08SBenjamin Tissoires 	parser = vzalloc(sizeof(struct hid_parser));
7583dc8fc08SBenjamin Tissoires 	if (!parser)
7593dc8fc08SBenjamin Tissoires 		return -ENOMEM;
7603dc8fc08SBenjamin Tissoires 
7613dc8fc08SBenjamin Tissoires 	parser->device = hid;
762734c6609SHenrik Rydberg 	hid->group = HID_GROUP_GENERIC;
763734c6609SHenrik Rydberg 
7643dc8fc08SBenjamin Tissoires 	/*
7653dc8fc08SBenjamin Tissoires 	 * The parsing is simpler than the one in hid_open_report() as we should
7663dc8fc08SBenjamin Tissoires 	 * be robust against hid errors. Those errors will be raised by
7673dc8fc08SBenjamin Tissoires 	 * hid_open_report() anyway.
7683dc8fc08SBenjamin Tissoires 	 */
7693dc8fc08SBenjamin Tissoires 	while ((start = fetch_item(start, end, &item)) != NULL)
7703dc8fc08SBenjamin Tissoires 		dispatch_type[item.type](parser, &item);
7713dc8fc08SBenjamin Tissoires 
772f961bd35SBenjamin Tissoires 	/*
773f961bd35SBenjamin Tissoires 	 * Handle special flags set during scanning.
774f961bd35SBenjamin Tissoires 	 */
775f961bd35SBenjamin Tissoires 	if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
776f961bd35SBenjamin Tissoires 	    (hid->group == HID_GROUP_MULTITOUCH))
777f961bd35SBenjamin Tissoires 		hid->group = HID_GROUP_MULTITOUCH_WIN_8;
778f961bd35SBenjamin Tissoires 
7793dc8fc08SBenjamin Tissoires 	vfree(parser);
780734c6609SHenrik Rydberg 	return 0;
781734c6609SHenrik Rydberg }
782734c6609SHenrik Rydberg 
78385cdaf52SJiri Slaby /**
78485cdaf52SJiri Slaby  * hid_parse_report - parse device report
78585cdaf52SJiri Slaby  *
78685cdaf52SJiri Slaby  * @device: hid device
78785cdaf52SJiri Slaby  * @start: report start
78885cdaf52SJiri Slaby  * @size: report size
78985cdaf52SJiri Slaby  *
790a7197c2eSHenrik Rydberg  * Allocate the device report as read by the bus driver. This function should
791a7197c2eSHenrik Rydberg  * only be called from parse() in ll drivers.
792a7197c2eSHenrik Rydberg  */
793a7197c2eSHenrik Rydberg int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
794a7197c2eSHenrik Rydberg {
795a7197c2eSHenrik Rydberg 	hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
796a7197c2eSHenrik Rydberg 	if (!hid->dev_rdesc)
797a7197c2eSHenrik Rydberg 		return -ENOMEM;
798a7197c2eSHenrik Rydberg 	hid->dev_rsize = size;
799a7197c2eSHenrik Rydberg 	return 0;
800a7197c2eSHenrik Rydberg }
801a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_parse_report);
802a7197c2eSHenrik Rydberg 
803331415ffSKees Cook static const char * const hid_report_names[] = {
804331415ffSKees Cook 	"HID_INPUT_REPORT",
805331415ffSKees Cook 	"HID_OUTPUT_REPORT",
806331415ffSKees Cook 	"HID_FEATURE_REPORT",
807331415ffSKees Cook };
808331415ffSKees Cook /**
809331415ffSKees Cook  * hid_validate_values - validate existing device report's value indexes
810331415ffSKees Cook  *
811331415ffSKees Cook  * @device: hid device
812331415ffSKees Cook  * @type: which report type to examine
813331415ffSKees Cook  * @id: which report ID to examine (0 for first)
814331415ffSKees Cook  * @field_index: which report field to examine
815331415ffSKees Cook  * @report_counts: expected number of values
816331415ffSKees Cook  *
817331415ffSKees Cook  * Validate the number of values in a given field of a given report, after
818331415ffSKees Cook  * parsing.
819331415ffSKees Cook  */
820331415ffSKees Cook struct hid_report *hid_validate_values(struct hid_device *hid,
821331415ffSKees Cook 				       unsigned int type, unsigned int id,
822331415ffSKees Cook 				       unsigned int field_index,
823331415ffSKees Cook 				       unsigned int report_counts)
824331415ffSKees Cook {
825331415ffSKees Cook 	struct hid_report *report;
826331415ffSKees Cook 
827331415ffSKees Cook 	if (type > HID_FEATURE_REPORT) {
828331415ffSKees Cook 		hid_err(hid, "invalid HID report type %u\n", type);
829331415ffSKees Cook 		return NULL;
830331415ffSKees Cook 	}
831331415ffSKees Cook 
832331415ffSKees Cook 	if (id >= HID_MAX_IDS) {
833331415ffSKees Cook 		hid_err(hid, "invalid HID report id %u\n", id);
834331415ffSKees Cook 		return NULL;
835331415ffSKees Cook 	}
836331415ffSKees Cook 
837331415ffSKees Cook 	/*
838331415ffSKees Cook 	 * Explicitly not using hid_get_report() here since it depends on
839331415ffSKees Cook 	 * ->numbered being checked, which may not always be the case when
840331415ffSKees Cook 	 * drivers go to access report values.
841331415ffSKees Cook 	 */
842331415ffSKees Cook 	report = hid->report_enum[type].report_id_hash[id];
843331415ffSKees Cook 	if (!report) {
844331415ffSKees Cook 		hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
845331415ffSKees Cook 		return NULL;
846331415ffSKees Cook 	}
847331415ffSKees Cook 	if (report->maxfield <= field_index) {
848331415ffSKees Cook 		hid_err(hid, "not enough fields in %s %u\n",
849331415ffSKees Cook 			hid_report_names[type], id);
850331415ffSKees Cook 		return NULL;
851331415ffSKees Cook 	}
852331415ffSKees Cook 	if (report->field[field_index]->report_count < report_counts) {
853331415ffSKees Cook 		hid_err(hid, "not enough values in %s %u field %u\n",
854331415ffSKees Cook 			hid_report_names[type], id, field_index);
855331415ffSKees Cook 		return NULL;
856331415ffSKees Cook 	}
857331415ffSKees Cook 	return report;
858331415ffSKees Cook }
859331415ffSKees Cook EXPORT_SYMBOL_GPL(hid_validate_values);
860331415ffSKees Cook 
861a7197c2eSHenrik Rydberg /**
862a7197c2eSHenrik Rydberg  * hid_open_report - open a driver-specific device report
863a7197c2eSHenrik Rydberg  *
864a7197c2eSHenrik Rydberg  * @device: hid device
865a7197c2eSHenrik Rydberg  *
866dde5845aSJiri Kosina  * Parse a report description into a hid_device structure. Reports are
867dde5845aSJiri Kosina  * enumerated, fields are attached to these reports.
86885cdaf52SJiri Slaby  * 0 returned on success, otherwise nonzero error value.
869a7197c2eSHenrik Rydberg  *
870a7197c2eSHenrik Rydberg  * This function (or the equivalent hid_parse() macro) should only be
871a7197c2eSHenrik Rydberg  * called from probe() in drivers, before starting the device.
872dde5845aSJiri Kosina  */
873a7197c2eSHenrik Rydberg int hid_open_report(struct hid_device *device)
874dde5845aSJiri Kosina {
875dde5845aSJiri Kosina 	struct hid_parser *parser;
876dde5845aSJiri Kosina 	struct hid_item item;
877a7197c2eSHenrik Rydberg 	unsigned int size;
878a7197c2eSHenrik Rydberg 	__u8 *start;
87986e6b77eSKevin Daughtridge 	__u8 *buf;
880dde5845aSJiri Kosina 	__u8 *end;
88185cdaf52SJiri Slaby 	int ret;
882dde5845aSJiri Kosina 	static int (*dispatch_type[])(struct hid_parser *parser,
883dde5845aSJiri Kosina 				      struct hid_item *item) = {
884dde5845aSJiri Kosina 		hid_parser_main,
885dde5845aSJiri Kosina 		hid_parser_global,
886dde5845aSJiri Kosina 		hid_parser_local,
887dde5845aSJiri Kosina 		hid_parser_reserved
888dde5845aSJiri Kosina 	};
889dde5845aSJiri Kosina 
890a7197c2eSHenrik Rydberg 	if (WARN_ON(device->status & HID_STAT_PARSED))
891a7197c2eSHenrik Rydberg 		return -EBUSY;
892a7197c2eSHenrik Rydberg 
893a7197c2eSHenrik Rydberg 	start = device->dev_rdesc;
894a7197c2eSHenrik Rydberg 	if (WARN_ON(!start))
895a7197c2eSHenrik Rydberg 		return -ENODEV;
896a7197c2eSHenrik Rydberg 	size = device->dev_rsize;
897a7197c2eSHenrik Rydberg 
89886e6b77eSKevin Daughtridge 	buf = kmemdup(start, size, GFP_KERNEL);
89986e6b77eSKevin Daughtridge 	if (buf == NULL)
90085cdaf52SJiri Slaby 		return -ENOMEM;
90186e6b77eSKevin Daughtridge 
90286e6b77eSKevin Daughtridge 	if (device->driver->report_fixup)
90386e6b77eSKevin Daughtridge 		start = device->driver->report_fixup(device, buf, &size);
90486e6b77eSKevin Daughtridge 	else
90586e6b77eSKevin Daughtridge 		start = buf;
90686e6b77eSKevin Daughtridge 
90786e6b77eSKevin Daughtridge 	start = kmemdup(start, size, GFP_KERNEL);
90886e6b77eSKevin Daughtridge 	kfree(buf);
90986e6b77eSKevin Daughtridge 	if (start == NULL)
91086e6b77eSKevin Daughtridge 		return -ENOMEM;
91186e6b77eSKevin Daughtridge 
91286e6b77eSKevin Daughtridge 	device->rdesc = start;
913dde5845aSJiri Kosina 	device->rsize = size;
914dde5845aSJiri Kosina 
915fe258020SJoe Perches 	parser = vzalloc(sizeof(struct hid_parser));
91685cdaf52SJiri Slaby 	if (!parser) {
91785cdaf52SJiri Slaby 		ret = -ENOMEM;
91885cdaf52SJiri Slaby 		goto err;
919dde5845aSJiri Kosina 	}
92085cdaf52SJiri Slaby 
921dde5845aSJiri Kosina 	parser->device = device;
922dde5845aSJiri Kosina 
923dde5845aSJiri Kosina 	end = start + size;
924a7197c2eSHenrik Rydberg 
925a7197c2eSHenrik Rydberg 	device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
926a7197c2eSHenrik Rydberg 				     sizeof(struct hid_collection), GFP_KERNEL);
927a7197c2eSHenrik Rydberg 	if (!device->collection) {
928a7197c2eSHenrik Rydberg 		ret = -ENOMEM;
929a7197c2eSHenrik Rydberg 		goto err;
930a7197c2eSHenrik Rydberg 	}
931a7197c2eSHenrik Rydberg 	device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
932a7197c2eSHenrik Rydberg 
93385cdaf52SJiri Slaby 	ret = -EINVAL;
934dde5845aSJiri Kosina 	while ((start = fetch_item(start, end, &item)) != NULL) {
935dde5845aSJiri Kosina 
936dde5845aSJiri Kosina 		if (item.format != HID_ITEM_FORMAT_SHORT) {
9378c3d52fcSJiri Kosina 			hid_err(device, "unexpected long global item\n");
93885cdaf52SJiri Slaby 			goto err;
939dde5845aSJiri Kosina 		}
940dde5845aSJiri Kosina 
941dde5845aSJiri Kosina 		if (dispatch_type[item.type](parser, &item)) {
9428c3d52fcSJiri Kosina 			hid_err(device, "item %u %u %u %u parsing failed\n",
9434291ee30SJoe Perches 				item.format, (unsigned)item.size,
9444291ee30SJoe Perches 				(unsigned)item.type, (unsigned)item.tag);
94585cdaf52SJiri Slaby 			goto err;
946dde5845aSJiri Kosina 		}
947dde5845aSJiri Kosina 
948dde5845aSJiri Kosina 		if (start == end) {
949dde5845aSJiri Kosina 			if (parser->collection_stack_ptr) {
9508c3d52fcSJiri Kosina 				hid_err(device, "unbalanced collection at end of report description\n");
95185cdaf52SJiri Slaby 				goto err;
952dde5845aSJiri Kosina 			}
953dde5845aSJiri Kosina 			if (parser->local.delimiter_depth) {
9548c3d52fcSJiri Kosina 				hid_err(device, "unbalanced delimiter at end of report description\n");
95585cdaf52SJiri Slaby 				goto err;
956dde5845aSJiri Kosina 			}
95747a80edbSJiri Kosina 			vfree(parser);
958a7197c2eSHenrik Rydberg 			device->status |= HID_STAT_PARSED;
95985cdaf52SJiri Slaby 			return 0;
960dde5845aSJiri Kosina 		}
961dde5845aSJiri Kosina 	}
962dde5845aSJiri Kosina 
9638c3d52fcSJiri Kosina 	hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
96485cdaf52SJiri Slaby err:
96547a80edbSJiri Kosina 	vfree(parser);
966a7197c2eSHenrik Rydberg 	hid_close_report(device);
96785cdaf52SJiri Slaby 	return ret;
968dde5845aSJiri Kosina }
969a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_open_report);
970dde5845aSJiri Kosina 
971dde5845aSJiri Kosina /*
972dde5845aSJiri Kosina  * Convert a signed n-bit integer to signed 32-bit integer. Common
973dde5845aSJiri Kosina  * cases are done through the compiler, the screwed things has to be
974dde5845aSJiri Kosina  * done by hand.
975dde5845aSJiri Kosina  */
976dde5845aSJiri Kosina 
977dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n)
978dde5845aSJiri Kosina {
979dde5845aSJiri Kosina 	switch (n) {
980dde5845aSJiri Kosina 	case 8:  return ((__s8)value);
981dde5845aSJiri Kosina 	case 16: return ((__s16)value);
982dde5845aSJiri Kosina 	case 32: return ((__s32)value);
983dde5845aSJiri Kosina 	}
984dde5845aSJiri Kosina 	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
985dde5845aSJiri Kosina }
986dde5845aSJiri Kosina 
98777463838SBenjamin Tissoires s32 hid_snto32(__u32 value, unsigned n)
98877463838SBenjamin Tissoires {
98977463838SBenjamin Tissoires 	return snto32(value, n);
99077463838SBenjamin Tissoires }
99177463838SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_snto32);
99277463838SBenjamin Tissoires 
993dde5845aSJiri Kosina /*
994dde5845aSJiri Kosina  * Convert a signed 32-bit integer to a signed n-bit integer.
995dde5845aSJiri Kosina  */
996dde5845aSJiri Kosina 
997dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n)
998dde5845aSJiri Kosina {
999dde5845aSJiri Kosina 	s32 a = value >> (n - 1);
1000dde5845aSJiri Kosina 	if (a && a != -1)
1001dde5845aSJiri Kosina 		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1002dde5845aSJiri Kosina 	return value & ((1 << n) - 1);
1003dde5845aSJiri Kosina }
1004dde5845aSJiri Kosina 
1005dde5845aSJiri Kosina /*
1006dde5845aSJiri Kosina  * Extract/implement a data field from/to a little endian report (bit array).
1007dde5845aSJiri Kosina  *
1008dde5845aSJiri Kosina  * Code sort-of follows HID spec:
1009dde5845aSJiri Kosina  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
1010dde5845aSJiri Kosina  *
1011dde5845aSJiri Kosina  * While the USB HID spec allows unlimited length bit fields in "report
1012dde5845aSJiri Kosina  * descriptors", most devices never use more than 16 bits.
1013dde5845aSJiri Kosina  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1014dde5845aSJiri Kosina  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1015dde5845aSJiri Kosina  */
1016dde5845aSJiri Kosina 
101716ee4cc8SJoe Perches static __u32 extract(const struct hid_device *hid, __u8 *report,
10184291ee30SJoe Perches 		     unsigned offset, unsigned n)
1019dde5845aSJiri Kosina {
1020dde5845aSJiri Kosina 	u64 x;
1021dde5845aSJiri Kosina 
1022c4124c9bSJiri Kosina 	if (n > 32)
10234291ee30SJoe Perches 		hid_warn(hid, "extract() called with n (%d) > 32! (%s)\n",
1024c4124c9bSJiri Kosina 			 n, current->comm);
1025dde5845aSJiri Kosina 
1026dde5845aSJiri Kosina 	report += offset >> 3;  /* adjust byte index */
1027dde5845aSJiri Kosina 	offset &= 7;            /* now only need bit offset into one byte */
1028c105068fSHarvey Harrison 	x = get_unaligned_le64(report);
1029dde5845aSJiri Kosina 	x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
1030dde5845aSJiri Kosina 	return (u32) x;
1031dde5845aSJiri Kosina }
1032dde5845aSJiri Kosina 
1033dde5845aSJiri Kosina /*
1034dde5845aSJiri Kosina  * "implement" : set bits in a little endian bit stream.
1035dde5845aSJiri Kosina  * Same concepts as "extract" (see comments above).
1036dde5845aSJiri Kosina  * The data mangled in the bit stream remains in little endian
1037dde5845aSJiri Kosina  * order the whole time. It make more sense to talk about
1038dde5845aSJiri Kosina  * endianness of register values by considering a register
1039dde5845aSJiri Kosina  * a "cached" copy of the little endiad bit stream.
1040dde5845aSJiri Kosina  */
104116ee4cc8SJoe Perches static void implement(const struct hid_device *hid, __u8 *report,
10424291ee30SJoe Perches 		      unsigned offset, unsigned n, __u32 value)
1043dde5845aSJiri Kosina {
10446f0168d2SHarvey Harrison 	u64 x;
1045dde5845aSJiri Kosina 	u64 m = (1ULL << n) - 1;
1046dde5845aSJiri Kosina 
1047c4124c9bSJiri Kosina 	if (n > 32)
10484291ee30SJoe Perches 		hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
10494291ee30SJoe Perches 			 __func__, n, current->comm);
1050dde5845aSJiri Kosina 
1051c4124c9bSJiri Kosina 	if (value > m)
10524291ee30SJoe Perches 		hid_warn(hid, "%s() called with too large value %d! (%s)\n",
10534291ee30SJoe Perches 			 __func__, value, current->comm);
1054dde5845aSJiri Kosina 	WARN_ON(value > m);
1055dde5845aSJiri Kosina 	value &= m;
1056dde5845aSJiri Kosina 
1057dde5845aSJiri Kosina 	report += offset >> 3;
1058dde5845aSJiri Kosina 	offset &= 7;
1059dde5845aSJiri Kosina 
10606f0168d2SHarvey Harrison 	x = get_unaligned_le64(report);
10616f0168d2SHarvey Harrison 	x &= ~(m << offset);
10626f0168d2SHarvey Harrison 	x |= ((u64)value) << offset;
10636f0168d2SHarvey Harrison 	put_unaligned_le64(x, report);
1064dde5845aSJiri Kosina }
1065dde5845aSJiri Kosina 
1066dde5845aSJiri Kosina /*
1067dde5845aSJiri Kosina  * Search an array for a value.
1068dde5845aSJiri Kosina  */
1069dde5845aSJiri Kosina 
107016ee4cc8SJoe Perches static int search(__s32 *array, __s32 value, unsigned n)
1071dde5845aSJiri Kosina {
1072dde5845aSJiri Kosina 	while (n--) {
1073dde5845aSJiri Kosina 		if (*array++ == value)
1074dde5845aSJiri Kosina 			return 0;
1075dde5845aSJiri Kosina 	}
1076dde5845aSJiri Kosina 	return -1;
1077dde5845aSJiri Kosina }
1078dde5845aSJiri Kosina 
107985cdaf52SJiri Slaby /**
108085cdaf52SJiri Slaby  * hid_match_report - check if driver's raw_event should be called
108185cdaf52SJiri Slaby  *
108285cdaf52SJiri Slaby  * @hid: hid device
108385cdaf52SJiri Slaby  * @report_type: type to match against
108485cdaf52SJiri Slaby  *
108585cdaf52SJiri Slaby  * compare hid->driver->report_table->report_type to report->type
108685cdaf52SJiri Slaby  */
108785cdaf52SJiri Slaby static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1088dde5845aSJiri Kosina {
108985cdaf52SJiri Slaby 	const struct hid_report_id *id = hid->driver->report_table;
109085cdaf52SJiri Slaby 
109185cdaf52SJiri Slaby 	if (!id) /* NULL means all */
109285cdaf52SJiri Slaby 		return 1;
109385cdaf52SJiri Slaby 
109485cdaf52SJiri Slaby 	for (; id->report_type != HID_TERMINATOR; id++)
109585cdaf52SJiri Slaby 		if (id->report_type == HID_ANY_ID ||
109685cdaf52SJiri Slaby 				id->report_type == report->type)
109785cdaf52SJiri Slaby 			return 1;
109885cdaf52SJiri Slaby 	return 0;
109985cdaf52SJiri Slaby }
110085cdaf52SJiri Slaby 
110185cdaf52SJiri Slaby /**
110285cdaf52SJiri Slaby  * hid_match_usage - check if driver's event should be called
110385cdaf52SJiri Slaby  *
110485cdaf52SJiri Slaby  * @hid: hid device
110585cdaf52SJiri Slaby  * @usage: usage to match against
110685cdaf52SJiri Slaby  *
110785cdaf52SJiri Slaby  * compare hid->driver->usage_table->usage_{type,code} to
110885cdaf52SJiri Slaby  * usage->usage_{type,code}
110985cdaf52SJiri Slaby  */
111085cdaf52SJiri Slaby static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
111185cdaf52SJiri Slaby {
111285cdaf52SJiri Slaby 	const struct hid_usage_id *id = hid->driver->usage_table;
111385cdaf52SJiri Slaby 
111485cdaf52SJiri Slaby 	if (!id) /* NULL means all */
111585cdaf52SJiri Slaby 		return 1;
111685cdaf52SJiri Slaby 
111785cdaf52SJiri Slaby 	for (; id->usage_type != HID_ANY_ID - 1; id++)
111885cdaf52SJiri Slaby 		if ((id->usage_hid == HID_ANY_ID ||
111985cdaf52SJiri Slaby 				id->usage_hid == usage->hid) &&
112085cdaf52SJiri Slaby 				(id->usage_type == HID_ANY_ID ||
112185cdaf52SJiri Slaby 				id->usage_type == usage->type) &&
112285cdaf52SJiri Slaby 				(id->usage_code == HID_ANY_ID ||
112385cdaf52SJiri Slaby 				 id->usage_code == usage->code))
112485cdaf52SJiri Slaby 			return 1;
112585cdaf52SJiri Slaby 	return 0;
112685cdaf52SJiri Slaby }
112785cdaf52SJiri Slaby 
112885cdaf52SJiri Slaby static void hid_process_event(struct hid_device *hid, struct hid_field *field,
112985cdaf52SJiri Slaby 		struct hid_usage *usage, __s32 value, int interrupt)
113085cdaf52SJiri Slaby {
113185cdaf52SJiri Slaby 	struct hid_driver *hdrv = hid->driver;
113285cdaf52SJiri Slaby 	int ret;
113385cdaf52SJiri Slaby 
11349bfc8da0SHenrik Rydberg 	if (!list_empty(&hid->debug_list))
1135cd667ce2SJiri Kosina 		hid_dump_input(hid, usage, value);
113685cdaf52SJiri Slaby 
113785cdaf52SJiri Slaby 	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
113885cdaf52SJiri Slaby 		ret = hdrv->event(hid, field, usage, value);
113985cdaf52SJiri Slaby 		if (ret != 0) {
114085cdaf52SJiri Slaby 			if (ret < 0)
11418c3d52fcSJiri Kosina 				hid_err(hid, "%s's event failed with %d\n",
114285cdaf52SJiri Slaby 						hdrv->name, ret);
114385cdaf52SJiri Slaby 			return;
114485cdaf52SJiri Slaby 		}
114585cdaf52SJiri Slaby 	}
114685cdaf52SJiri Slaby 
1147dde5845aSJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1148dde5845aSJiri Kosina 		hidinput_hid_event(hid, field, usage, value);
1149aa938f79SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1150aa938f79SJiri Kosina 		hid->hiddev_hid_event(hid, field, usage, value);
1151dde5845aSJiri Kosina }
1152dde5845aSJiri Kosina 
1153dde5845aSJiri Kosina /*
1154dde5845aSJiri Kosina  * Analyse a received field, and fetch the data from it. The field
1155dde5845aSJiri Kosina  * content is stored for next report processing (we do differential
1156dde5845aSJiri Kosina  * reporting to the layer).
1157dde5845aSJiri Kosina  */
1158dde5845aSJiri Kosina 
1159abdff0f7SAdrian Bunk static void hid_input_field(struct hid_device *hid, struct hid_field *field,
1160abdff0f7SAdrian Bunk 			    __u8 *data, int interrupt)
1161dde5845aSJiri Kosina {
1162dde5845aSJiri Kosina 	unsigned n;
1163dde5845aSJiri Kosina 	unsigned count = field->report_count;
1164dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
1165dde5845aSJiri Kosina 	unsigned size = field->report_size;
1166dde5845aSJiri Kosina 	__s32 min = field->logical_minimum;
1167dde5845aSJiri Kosina 	__s32 max = field->logical_maximum;
1168dde5845aSJiri Kosina 	__s32 *value;
1169dde5845aSJiri Kosina 
1170a3789a17SJoe Perches 	value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC);
1171a3789a17SJoe Perches 	if (!value)
1172dde5845aSJiri Kosina 		return;
1173dde5845aSJiri Kosina 
1174dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1175dde5845aSJiri Kosina 
11764291ee30SJoe Perches 		value[n] = min < 0 ?
11774291ee30SJoe Perches 			snto32(extract(hid, data, offset + n * size, size),
11784291ee30SJoe Perches 			       size) :
11794291ee30SJoe Perches 			extract(hid, data, offset + n * size, size);
1180dde5845aSJiri Kosina 
11814291ee30SJoe Perches 		/* Ignore report if ErrorRollOver */
11824291ee30SJoe Perches 		if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
11834291ee30SJoe Perches 		    value[n] >= min && value[n] <= max &&
11844291ee30SJoe Perches 		    field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1185dde5845aSJiri Kosina 			goto exit;
1186dde5845aSJiri Kosina 	}
1187dde5845aSJiri Kosina 
1188dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1189dde5845aSJiri Kosina 
1190dde5845aSJiri Kosina 		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1191dde5845aSJiri Kosina 			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1192dde5845aSJiri Kosina 			continue;
1193dde5845aSJiri Kosina 		}
1194dde5845aSJiri Kosina 
1195dde5845aSJiri Kosina 		if (field->value[n] >= min && field->value[n] <= max
1196dde5845aSJiri Kosina 			&& field->usage[field->value[n] - min].hid
1197dde5845aSJiri Kosina 			&& search(value, field->value[n], count))
1198dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1199dde5845aSJiri Kosina 
1200dde5845aSJiri Kosina 		if (value[n] >= min && value[n] <= max
1201dde5845aSJiri Kosina 			&& field->usage[value[n] - min].hid
1202dde5845aSJiri Kosina 			&& search(field->value, value[n], count))
1203dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1204dde5845aSJiri Kosina 	}
1205dde5845aSJiri Kosina 
1206dde5845aSJiri Kosina 	memcpy(field->value, value, count * sizeof(__s32));
1207dde5845aSJiri Kosina exit:
1208dde5845aSJiri Kosina 	kfree(value);
1209dde5845aSJiri Kosina }
1210dde5845aSJiri Kosina 
1211dde5845aSJiri Kosina /*
1212dde5845aSJiri Kosina  * Output the field into the report.
1213dde5845aSJiri Kosina  */
1214dde5845aSJiri Kosina 
12154291ee30SJoe Perches static void hid_output_field(const struct hid_device *hid,
12164291ee30SJoe Perches 			     struct hid_field *field, __u8 *data)
1217dde5845aSJiri Kosina {
1218dde5845aSJiri Kosina 	unsigned count = field->report_count;
1219dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
1220dde5845aSJiri Kosina 	unsigned size = field->report_size;
1221dde5845aSJiri Kosina 	unsigned n;
1222dde5845aSJiri Kosina 
1223dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1224dde5845aSJiri Kosina 		if (field->logical_minimum < 0)	/* signed values */
12254291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
12264291ee30SJoe Perches 				  s32ton(field->value[n], size));
1227dde5845aSJiri Kosina 		else				/* unsigned values */
12284291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
12294291ee30SJoe Perches 				  field->value[n]);
1230dde5845aSJiri Kosina 	}
1231dde5845aSJiri Kosina }
1232dde5845aSJiri Kosina 
1233dde5845aSJiri Kosina /*
123427ce4050SJiri Kosina  * Create a report. 'data' has to be allocated using
123527ce4050SJiri Kosina  * hid_alloc_report_buf() so that it has proper size.
1236dde5845aSJiri Kosina  */
1237dde5845aSJiri Kosina 
1238229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data)
1239dde5845aSJiri Kosina {
1240dde5845aSJiri Kosina 	unsigned n;
1241dde5845aSJiri Kosina 
1242dde5845aSJiri Kosina 	if (report->id > 0)
1243dde5845aSJiri Kosina 		*data++ = report->id;
1244dde5845aSJiri Kosina 
124575c28df8SPete Zaitcev 	memset(data, 0, ((report->size - 1) >> 3) + 1);
1246dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
12474291ee30SJoe Perches 		hid_output_field(report->device, report->field[n], data);
1248dde5845aSJiri Kosina }
1249229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report);
1250dde5845aSJiri Kosina 
1251dde5845aSJiri Kosina /*
125227ce4050SJiri Kosina  * Allocator for buffer that is going to be passed to hid_output_report()
125327ce4050SJiri Kosina  */
125427ce4050SJiri Kosina u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
125527ce4050SJiri Kosina {
125627ce4050SJiri Kosina 	/*
125727ce4050SJiri Kosina 	 * 7 extra bytes are necessary to achieve proper functionality
125827ce4050SJiri Kosina 	 * of implement() working on 8 byte chunks
125927ce4050SJiri Kosina 	 */
126027ce4050SJiri Kosina 
126127ce4050SJiri Kosina 	int len = ((report->size - 1) >> 3) + 1 + (report->id > 0) + 7;
126227ce4050SJiri Kosina 
126327ce4050SJiri Kosina 	return kmalloc(len, flags);
126427ce4050SJiri Kosina }
126527ce4050SJiri Kosina EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
126627ce4050SJiri Kosina 
126727ce4050SJiri Kosina /*
1268dde5845aSJiri Kosina  * Set a field value. The report this field belongs to has to be
1269dde5845aSJiri Kosina  * created and transferred to the device, to set this value in the
1270dde5845aSJiri Kosina  * device.
1271dde5845aSJiri Kosina  */
1272dde5845aSJiri Kosina 
1273dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1274dde5845aSJiri Kosina {
1275be67b68dSKees Cook 	unsigned size;
1276be67b68dSKees Cook 
1277be67b68dSKees Cook 	if (!field)
1278be67b68dSKees Cook 		return -1;
1279be67b68dSKees Cook 
1280be67b68dSKees Cook 	size = field->report_size;
1281dde5845aSJiri Kosina 
1282cd667ce2SJiri Kosina 	hid_dump_input(field->report->device, field->usage + offset, value);
1283dde5845aSJiri Kosina 
1284dde5845aSJiri Kosina 	if (offset >= field->report_count) {
12858c3d52fcSJiri Kosina 		hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
12868c3d52fcSJiri Kosina 				offset, field->report_count);
1287dde5845aSJiri Kosina 		return -1;
1288dde5845aSJiri Kosina 	}
1289dde5845aSJiri Kosina 	if (field->logical_minimum < 0) {
1290dde5845aSJiri Kosina 		if (value != snto32(s32ton(value, size), size)) {
12918c3d52fcSJiri Kosina 			hid_err(field->report->device, "value %d is out of range\n", value);
1292dde5845aSJiri Kosina 			return -1;
1293dde5845aSJiri Kosina 		}
1294dde5845aSJiri Kosina 	}
1295dde5845aSJiri Kosina 	field->value[offset] = value;
1296dde5845aSJiri Kosina 	return 0;
1297dde5845aSJiri Kosina }
1298229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field);
1299dde5845aSJiri Kosina 
130085cdaf52SJiri Slaby static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
130185cdaf52SJiri Slaby 		const u8 *data)
130285cdaf52SJiri Slaby {
130385cdaf52SJiri Slaby 	struct hid_report *report;
130485cdaf52SJiri Slaby 	unsigned int n = 0;	/* Normally report number is 0 */
130585cdaf52SJiri Slaby 
130685cdaf52SJiri Slaby 	/* Device uses numbered reports, data[0] is report number */
130785cdaf52SJiri Slaby 	if (report_enum->numbered)
130885cdaf52SJiri Slaby 		n = *data;
130985cdaf52SJiri Slaby 
131085cdaf52SJiri Slaby 	report = report_enum->report_id_hash[n];
131185cdaf52SJiri Slaby 	if (report == NULL)
131285cdaf52SJiri Slaby 		dbg_hid("undefined report_id %u received\n", n);
131385cdaf52SJiri Slaby 
131485cdaf52SJiri Slaby 	return report;
131585cdaf52SJiri Slaby }
131685cdaf52SJiri Slaby 
1317b6787242SJiri Kosina int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
131885cdaf52SJiri Slaby 		int interrupt)
1319aa8de2f0SJiri Kosina {
1320aa8de2f0SJiri Kosina 	struct hid_report_enum *report_enum = hid->report_enum + type;
1321aa8de2f0SJiri Kosina 	struct hid_report *report;
13226d85d037SBenjamin Tissoires 	struct hid_driver *hdrv;
132385cdaf52SJiri Slaby 	unsigned int a;
132485cdaf52SJiri Slaby 	int rsize, csize = size;
132585cdaf52SJiri Slaby 	u8 *cdata = data;
1326b6787242SJiri Kosina 	int ret = 0;
1327aa8de2f0SJiri Kosina 
132885cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
132985cdaf52SJiri Slaby 	if (!report)
1330b6787242SJiri Kosina 		goto out;
1331aa8de2f0SJiri Kosina 
133285cdaf52SJiri Slaby 	if (report_enum->numbered) {
133385cdaf52SJiri Slaby 		cdata++;
133485cdaf52SJiri Slaby 		csize--;
1335aa8de2f0SJiri Kosina 	}
1336aa8de2f0SJiri Kosina 
1337aa8de2f0SJiri Kosina 	rsize = ((report->size - 1) >> 3) + 1;
1338aa8de2f0SJiri Kosina 
1339966922f2SArmando Visconti 	if (rsize > HID_MAX_BUFFER_SIZE)
1340966922f2SArmando Visconti 		rsize = HID_MAX_BUFFER_SIZE;
1341966922f2SArmando Visconti 
134285cdaf52SJiri Slaby 	if (csize < rsize) {
134385cdaf52SJiri Slaby 		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
134485cdaf52SJiri Slaby 				csize, rsize);
134585cdaf52SJiri Slaby 		memset(cdata + csize, 0, rsize - csize);
1346aa8de2f0SJiri Kosina 	}
1347aa8de2f0SJiri Kosina 
1348aa8de2f0SJiri Kosina 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1349aa8de2f0SJiri Kosina 		hid->hiddev_report_event(hid, report);
1350b6787242SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1351b6787242SJiri Kosina 		ret = hidraw_report_event(hid, data, size);
1352b6787242SJiri Kosina 		if (ret)
1353b6787242SJiri Kosina 			goto out;
1354b6787242SJiri Kosina 	}
1355aa8de2f0SJiri Kosina 
1356cc6b54aaSBenjamin Tissoires 	if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
135785cdaf52SJiri Slaby 		for (a = 0; a < report->maxfield; a++)
135885cdaf52SJiri Slaby 			hid_input_field(hid, report->field[a], cdata, interrupt);
13596d85d037SBenjamin Tissoires 		hdrv = hid->driver;
13606d85d037SBenjamin Tissoires 		if (hdrv && hdrv->report)
13616d85d037SBenjamin Tissoires 			hdrv->report(hid, report);
1362b94e3c94SMatthieu CASTET 	}
1363aa8de2f0SJiri Kosina 
1364aa8de2f0SJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1365aa8de2f0SJiri Kosina 		hidinput_report_event(hid, report);
1366b6787242SJiri Kosina out:
1367b6787242SJiri Kosina 	return ret;
136885cdaf52SJiri Slaby }
136985cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_report_raw_event);
137085cdaf52SJiri Slaby 
137185cdaf52SJiri Slaby /**
137285cdaf52SJiri Slaby  * hid_input_report - report data from lower layer (usb, bt...)
137385cdaf52SJiri Slaby  *
137485cdaf52SJiri Slaby  * @hid: hid device
137585cdaf52SJiri Slaby  * @type: HID report type (HID_*_REPORT)
137685cdaf52SJiri Slaby  * @data: report contents
137785cdaf52SJiri Slaby  * @size: size of data parameter
1378ff9b00a2SJiri Kosina  * @interrupt: distinguish between interrupt and control transfers
137985cdaf52SJiri Slaby  *
138085cdaf52SJiri Slaby  * This is data entry for lower layers.
138185cdaf52SJiri Slaby  */
138285cdaf52SJiri Slaby int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
138385cdaf52SJiri Slaby {
138476c317d6SJulia Lawall 	struct hid_report_enum *report_enum;
138576c317d6SJulia Lawall 	struct hid_driver *hdrv;
138685cdaf52SJiri Slaby 	struct hid_report *report;
138745dc1ac7SJiri Kosina 	int ret = 0;
138885cdaf52SJiri Slaby 
13894ea54542SDavid Herrmann 	if (!hid)
139085cdaf52SJiri Slaby 		return -ENODEV;
13914ea54542SDavid Herrmann 
1392c849a614SAndrew de los Reyes 	if (down_trylock(&hid->driver_input_lock))
13934ea54542SDavid Herrmann 		return -EBUSY;
13944ea54542SDavid Herrmann 
13954ea54542SDavid Herrmann 	if (!hid->driver) {
13964ea54542SDavid Herrmann 		ret = -ENODEV;
13974ea54542SDavid Herrmann 		goto unlock;
13984ea54542SDavid Herrmann 	}
139976c317d6SJulia Lawall 	report_enum = hid->report_enum + type;
140076c317d6SJulia Lawall 	hdrv = hid->driver;
140185cdaf52SJiri Slaby 
140285cdaf52SJiri Slaby 	if (!size) {
140385cdaf52SJiri Slaby 		dbg_hid("empty report\n");
14044ea54542SDavid Herrmann 		ret = -1;
14054ea54542SDavid Herrmann 		goto unlock;
140685cdaf52SJiri Slaby 	}
140785cdaf52SJiri Slaby 
1408b94e3c94SMatthieu CASTET 	/* Avoid unnecessary overhead if debugfs is disabled */
1409a5f04b9dSBenjamin Tissoires 	if (!list_empty(&hid->debug_list))
1410a5f04b9dSBenjamin Tissoires 		hid_dump_report(hid, type, data, size);
1411b94e3c94SMatthieu CASTET 
1412f77e347bSJiri Kosina 	report = hid_get_report(report_enum, data);
1413f77e347bSJiri Kosina 
14144ea54542SDavid Herrmann 	if (!report) {
14154ea54542SDavid Herrmann 		ret = -1;
14164ea54542SDavid Herrmann 		goto unlock;
14174ea54542SDavid Herrmann 	}
1418f77e347bSJiri Kosina 
141985cdaf52SJiri Slaby 	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
142085cdaf52SJiri Slaby 		ret = hdrv->raw_event(hid, report, data, size);
1421556483e2SFelix Rueegg 		if (ret < 0)
14224ea54542SDavid Herrmann 			goto unlock;
14234ea54542SDavid Herrmann 	}
142485cdaf52SJiri Slaby 
1425b6787242SJiri Kosina 	ret = hid_report_raw_event(hid, type, data, size, interrupt);
1426aa8de2f0SJiri Kosina 
14274ea54542SDavid Herrmann unlock:
1428c849a614SAndrew de los Reyes 	up(&hid->driver_input_lock);
142945dc1ac7SJiri Kosina 	return ret;
1430aa8de2f0SJiri Kosina }
1431aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report);
1432aa8de2f0SJiri Kosina 
14330f37cd03SJiri Kosina static bool hid_match_one_id(struct hid_device *hdev,
14340f37cd03SJiri Kosina 		const struct hid_device_id *id)
14350f37cd03SJiri Kosina {
14367431fb76SHenrik Rydberg 	return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
14374d53b801SHenrik Rydberg 		(id->group == HID_GROUP_ANY || id->group == hdev->group) &&
14380f37cd03SJiri Kosina 		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
14390f37cd03SJiri Kosina 		(id->product == HID_ANY_ID || id->product == hdev->product);
14400f37cd03SJiri Kosina }
14410f37cd03SJiri Kosina 
1442bbc21cfdSJeremy Fitzhardinge const struct hid_device_id *hid_match_id(struct hid_device *hdev,
14430f37cd03SJiri Kosina 		const struct hid_device_id *id)
14440f37cd03SJiri Kosina {
14450f37cd03SJiri Kosina 	for (; id->bus; id++)
14460f37cd03SJiri Kosina 		if (hid_match_one_id(hdev, id))
14470f37cd03SJiri Kosina 			return id;
14480f37cd03SJiri Kosina 
14490f37cd03SJiri Kosina 	return NULL;
14500f37cd03SJiri Kosina }
14510f37cd03SJiri Kosina 
14520f37cd03SJiri Kosina static const struct hid_device_id hid_hiddev_list[] = {
1453c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1454c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
14550f37cd03SJiri Kosina 	{ }
14560f37cd03SJiri Kosina };
14570f37cd03SJiri Kosina 
14580f37cd03SJiri Kosina static bool hid_hiddev(struct hid_device *hdev)
14590f37cd03SJiri Kosina {
14600f37cd03SJiri Kosina 	return !!hid_match_id(hdev, hid_hiddev_list);
14610f37cd03SJiri Kosina }
14620f37cd03SJiri Kosina 
14636d3bfb74SAlan Ott 
14646d3bfb74SAlan Ott static ssize_t
14656d3bfb74SAlan Ott read_report_descriptor(struct file *filp, struct kobject *kobj,
14666d3bfb74SAlan Ott 		struct bin_attribute *attr,
14676d3bfb74SAlan Ott 		char *buf, loff_t off, size_t count)
14686d3bfb74SAlan Ott {
14696d3bfb74SAlan Ott 	struct device *dev = container_of(kobj, struct device, kobj);
14706d3bfb74SAlan Ott 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
14716d3bfb74SAlan Ott 
14726d3bfb74SAlan Ott 	if (off >= hdev->rsize)
14736d3bfb74SAlan Ott 		return 0;
14746d3bfb74SAlan Ott 
14756d3bfb74SAlan Ott 	if (off + count > hdev->rsize)
14766d3bfb74SAlan Ott 		count = hdev->rsize - off;
14776d3bfb74SAlan Ott 
14786d3bfb74SAlan Ott 	memcpy(buf, hdev->rdesc + off, count);
14796d3bfb74SAlan Ott 
14806d3bfb74SAlan Ott 	return count;
14816d3bfb74SAlan Ott }
14826d3bfb74SAlan Ott 
14836d3bfb74SAlan Ott static struct bin_attribute dev_bin_attr_report_desc = {
14846d3bfb74SAlan Ott 	.attr = { .name = "report_descriptor", .mode = 0444 },
14856d3bfb74SAlan Ott 	.read = read_report_descriptor,
14866d3bfb74SAlan Ott 	.size = HID_MAX_DESCRIPTOR_SIZE,
14876d3bfb74SAlan Ott };
14886d3bfb74SAlan Ott 
148993c10132SJiri Slaby int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
149093c10132SJiri Slaby {
149193c10132SJiri Slaby 	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
149293c10132SJiri Slaby 		"Joystick", "Gamepad", "Keyboard", "Keypad",
149393c10132SJiri Slaby 		"Multi-Axis Controller"
149493c10132SJiri Slaby 	};
149593c10132SJiri Slaby 	const char *type, *bus;
149693c10132SJiri Slaby 	char buf[64];
149793c10132SJiri Slaby 	unsigned int i;
149893c10132SJiri Slaby 	int len;
14996d3bfb74SAlan Ott 	int ret;
150093c10132SJiri Slaby 
1501b5e5a37eSBastien Nocera 	if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1502b5e5a37eSBastien Nocera 		connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
15033a343ee4SDaniel Mack 	if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
15043a343ee4SDaniel Mack 		connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
150593c10132SJiri Slaby 	if (hdev->bus != BUS_USB)
150693c10132SJiri Slaby 		connect_mask &= ~HID_CONNECT_HIDDEV;
15070f37cd03SJiri Kosina 	if (hid_hiddev(hdev))
15080f37cd03SJiri Kosina 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
150993c10132SJiri Slaby 
151093c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
151193c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
151293c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_INPUT;
1513b77c3920SBenjamin Tissoires 
151493c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
151593c10132SJiri Slaby 			!hdev->hiddev_connect(hdev,
151693c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDDEV_FORCE))
151793c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDDEV;
151893c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
151993c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDRAW;
152093c10132SJiri Slaby 
15214bc19f62SDavid Herrmann 	/* Drivers with the ->raw_event callback set are not required to connect
15224bc19f62SDavid Herrmann 	 * to any other listener. */
15234bc19f62SDavid Herrmann 	if (!hdev->claimed && !hdev->driver->raw_event) {
15244bc19f62SDavid Herrmann 		hid_err(hdev, "device has no listeners, quitting\n");
152593c10132SJiri Slaby 		return -ENODEV;
152693c10132SJiri Slaby 	}
152793c10132SJiri Slaby 
152893c10132SJiri Slaby 	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
152993c10132SJiri Slaby 			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
153093c10132SJiri Slaby 		hdev->ff_init(hdev);
153193c10132SJiri Slaby 
153293c10132SJiri Slaby 	len = 0;
153393c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_INPUT)
153493c10132SJiri Slaby 		len += sprintf(buf + len, "input");
153593c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
153693c10132SJiri Slaby 		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
153793c10132SJiri Slaby 				hdev->minor);
153893c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
153993c10132SJiri Slaby 		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
154093c10132SJiri Slaby 				((struct hidraw *)hdev->hidraw)->minor);
154193c10132SJiri Slaby 
154293c10132SJiri Slaby 	type = "Device";
154393c10132SJiri Slaby 	for (i = 0; i < hdev->maxcollection; i++) {
154493c10132SJiri Slaby 		struct hid_collection *col = &hdev->collection[i];
154593c10132SJiri Slaby 		if (col->type == HID_COLLECTION_APPLICATION &&
154693c10132SJiri Slaby 		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
154793c10132SJiri Slaby 		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
154893c10132SJiri Slaby 			type = types[col->usage & 0xffff];
154993c10132SJiri Slaby 			break;
155093c10132SJiri Slaby 		}
155193c10132SJiri Slaby 	}
155293c10132SJiri Slaby 
155393c10132SJiri Slaby 	switch (hdev->bus) {
155493c10132SJiri Slaby 	case BUS_USB:
155593c10132SJiri Slaby 		bus = "USB";
155693c10132SJiri Slaby 		break;
155793c10132SJiri Slaby 	case BUS_BLUETOOTH:
155893c10132SJiri Slaby 		bus = "BLUETOOTH";
155993c10132SJiri Slaby 		break;
156093c10132SJiri Slaby 	default:
156193c10132SJiri Slaby 		bus = "<UNKNOWN>";
156293c10132SJiri Slaby 	}
156393c10132SJiri Slaby 
15646d3bfb74SAlan Ott 	ret = device_create_bin_file(&hdev->dev, &dev_bin_attr_report_desc);
15656d3bfb74SAlan Ott 	if (ret)
15666d3bfb74SAlan Ott 		hid_warn(hdev,
15676d3bfb74SAlan Ott 			 "can't create sysfs report descriptor attribute err: %d\n", ret);
15686d3bfb74SAlan Ott 
15694291ee30SJoe Perches 	hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
157093c10132SJiri Slaby 		 buf, bus, hdev->version >> 8, hdev->version & 0xff,
157193c10132SJiri Slaby 		 type, hdev->name, hdev->phys);
157293c10132SJiri Slaby 
157393c10132SJiri Slaby 	return 0;
157493c10132SJiri Slaby }
157593c10132SJiri Slaby EXPORT_SYMBOL_GPL(hid_connect);
157693c10132SJiri Slaby 
1577c4c259bcSJiri Kosina void hid_disconnect(struct hid_device *hdev)
1578c4c259bcSJiri Kosina {
15796d3bfb74SAlan Ott 	device_remove_bin_file(&hdev->dev, &dev_bin_attr_report_desc);
1580c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_INPUT)
1581c4c259bcSJiri Kosina 		hidinput_disconnect(hdev);
1582c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
1583c4c259bcSJiri Kosina 		hdev->hiddev_disconnect(hdev);
1584c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
1585c4c259bcSJiri Kosina 		hidraw_disconnect(hdev);
1586c4c259bcSJiri Kosina }
1587c4c259bcSJiri Kosina EXPORT_SYMBOL_GPL(hid_disconnect);
1588c4c259bcSJiri Kosina 
158900315e4fSJiri Kosina /*
159000315e4fSJiri Kosina  * A list of devices for which there is a specialized driver on HID bus.
159100315e4fSJiri Kosina  *
159200315e4fSJiri Kosina  * Please note that for multitouch devices (driven by hid-multitouch driver),
159300315e4fSJiri Kosina  * there is a proper autodetection and autoloading in place (based on presence
159400315e4fSJiri Kosina  * of HID_DG_CONTACTID), so those devices don't need to be added to this list,
159500315e4fSJiri Kosina  * as we are doing the right thing in hid_scan_usage().
159683499b52SAlexander Holler  *
159783499b52SAlexander Holler  * Autodetection for (USB) HID sensor hubs exists too. If a collection of type
159883499b52SAlexander Holler  * physical is found inside a usage page of type sensor, hid-sensor-hub will be
159983499b52SAlexander Holler  * used as a driver. See hid_scan_report().
160000315e4fSJiri Kosina  */
1601ce06b9d6SDmitry Torokhov static const struct hid_device_id hid_have_special_driver[] = {
160214a21cd4SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
160314a21cd4SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1604b7e1b203SLech Perczak 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649) },
1605c0dbcc33SSergei Kolzun 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802) },
1606e17f5d76STristan Rice 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0xf705) },
16078c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1608128537ceSMichael Poole 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE) },
1609a462230eSChase Douglas 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICTRACKPAD) },
16108c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
16118c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
16128c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
16138c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
16148c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
16158c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
16168c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
16178c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
16188c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
16198c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
16208c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1621fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1622fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1623fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
16248c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
16258c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
16268c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
16278c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
16288c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
16298c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
16309a4a5574SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
16319a4a5574SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL2) },
16329a4a5574SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL3) },
16339a4a5574SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
16349a4a5574SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) },
1635ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1636ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1637ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
16388c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
16398c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
16408c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
16418c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
16428c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
16438c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1644a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1645a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1646a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
164799b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
164899b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
164999b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
165099b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
165199b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
165299b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
165347340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI) },
165447340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO) },
165547340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS) },
1656213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI) },
1657213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO) },
1658213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS) },
16594a4c8799SDan Bastone 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI) },
16604a4c8799SDan Bastone 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO) },
16614a4c8799SDan Bastone 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_JIS) },
1662f6f554f0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI) },
1663f6f554f0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO) },
1664f6f554f0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS) },
1665d762cc29SNobuhiro Iwamatsu 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI) },
1666d762cc29SNobuhiro Iwamatsu 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO) },
1667d762cc29SNobuhiro Iwamatsu 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS) },
1668b2e6ad7dSRyan Bourgeois 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI) },
1669b2e6ad7dSRyan Bourgeois 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO) },
1670b2e6ad7dSRyan Bourgeois 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS) },
16718d80da90SDirk Hohndel 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI) },
16728d80da90SDirk Hohndel 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO) },
16738d80da90SDirk Hohndel 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS) },
16749d9a04eeSDmitry Torokhov 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI) },
16759d9a04eeSDmitry Torokhov 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO) },
16769d9a04eeSDmitry Torokhov 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS) },
167723aeb61eSChristian Schuerer-Waldheim 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI) },
167823aeb61eSChristian Schuerer-Waldheim 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO) },
167923aeb61eSChristian Schuerer-Waldheim 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS) },
1680f9af7b9eSBen Hutchings 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI) },
1681ad734bc1SAndreas Krist 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO) },
16828c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
16838c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1684212da74dSJosenivaldo Benito Junior 	{ HID_USB_DEVICE(USB_VENDOR_ID_AUREAL, USB_DEVICE_ID_AUREAL_W01RN) },
1685b5635b12SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
1686bf280628SWayne Thomas 	{ HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE) },
1687c3dc66deSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE_2) },
16883b239cd7SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
16891ce31b25SRaphaël Doursenaud 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION_SOLAR) },
1690fcfacfd3SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1691b9e4b1e0SDennis Kügler 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS) },
16923596bb92SKeng-Yu Lin 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS2) },
16932d8767bbSCyrus Lien 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_AK1D) },
16943a370ca1SDon Prince 	{ HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_PRODIKEYS_PCMIDI) },
16950f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
16960f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1697e8d0eab4SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) },
169876c9d8feSLionel Vaux 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_4) },
16990f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
17003f866fbdSRichard Walmsley 	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
1701e05eefb9SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011) },
170264b386eaSRichard Nauber 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
1703d23efc19SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0009) },
1704d23efc19SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0030) },
170504561c5aSIgnaz Forster 	{ HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
17061f243e30SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
17075181e594SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1708578f3a35SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
17095181e594SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
171042859e0bSLukasz Lubojanski 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1711949f8fefSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
17121e093206SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1713c2fd1a4eSCory Maccarrone 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
1714d946e65eSAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
1715ff9bf5a2STom Harwood 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) },
171640d3597fSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A04A) },
171740d3597fSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A067) },
1718f1a4914bSAnders F. U. Kiær 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A072) },
17197da7cbbbSAnders F. U. Kiær 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A081) },
172068e353feSMartin Rusko 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION, USB_DEVICE_ID_HUION_580) },
1721aaca9cc0SMichael Karcher 	{ HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD) },
17224ddfe028SBastien Nocera 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ION, USB_DEVICE_ID_ICADE) },
1723fdf93aa3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
1724177900e8SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KEYTOUCH, USB_DEVICE_ID_KEYTOUCH_IEC) },
17253685c18eSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_GENIUS_GILA_GAMING_MOUSE) },
17260adb9c2cSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_GENIUS_GX_IMPERATOR) },
172779422741SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
172822ca20b2SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_EASYPEN_I405X) },
172922ca20b2SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_MOUSEPEN_I608X) },
173022ca20b2SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_EASYPEN_M610X) },
1731b5635b12SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
173275b07022SChris Schlund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LCPOWER, USB_DEVICE_ID_LCPOWER_LC1000 ) },
1733aad932e7SAndres Freund #if IS_ENABLED(CONFIG_HID_LENOVO_TPKBD)
1734c1dcad2dSBernhard Seibold 	{ HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1735aad932e7SAndres Freund #endif
17365f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
17375f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
17385f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
17395f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
17405844c1cdSDavid Dillow 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3) },
17415f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
17425f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
17435f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
17445f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
17455f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
17465f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
17475f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
17482c6118e4SHendrik Iben 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD_CORD) },
1749606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1750606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1751606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1752fd30ea8cSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
1753606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
175474f292caSGary Stein 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FLIGHT_SYSTEM_G940) },
1755606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1756606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1757bd04363dSElias Vanderstuyft 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_VIBRATION_WHEEL) },
17585623a24aSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL) },
1759e00ddc9bSMichal Malý 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFGT_WHEEL) },
1760243b706dSChristophe Borivant 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
1761fdc6807fSPeter Gundermann 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL) },
17620944e964SKonstantin Khlebnikov #if IS_ENABLED(CONFIG_HID_LOGITECH_DJ)
1763534a7b8eSNestor Lopez Casado 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
1764534a7b8eSNestor Lopez Casado 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
17650944e964SKonstantin Khlebnikov #endif
176632c88cbcSSimon Wood 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
1767606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
176824985cf6SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
176924985cf6SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) },
1770236db47cSBruno Prémont 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
1771236db47cSBruno Prémont 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
1772b580169aSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500) },
177378a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
177478a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
177589759e20SAdam Jiang 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP) },
177678a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
177778a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1778f3b83d71SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K) },
177978a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
17803b8006e5SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
178194011f93SRafi Rubin 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
17826e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1) },
17836e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2) },
17846e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3) },
17856e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4) },
17866e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5) },
17876e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6) },
17886e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7) },
17896e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8) },
17906e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9) },
17916e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10) },
17926e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11) },
17936e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12) },
17946e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13) },
17956e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14) },
17966e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15) },
17976e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16) },
17986e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17) },
17996e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18) },
1800270fdc07SHerton Ronaldo Krzesinski 	{ HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_PKB1700) },
1801cd9ec30dSJohnathon Harris 	{ HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_WKB2000) },
18021e762532SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1803f6a04605STerry Lambert 	{ HID_USB_DEVICE(USB_VENDOR_ID_PRIMAX, USB_DEVICE_ID_PRIMAX_KEYBOARD) },
1804ca9bbdccSStefan Achatz #if IS_ENABLED(CONFIG_HID_ROCCAT)
1805e68cc603SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
1806d41c2a70SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
1807e078809dSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
1808e078809dSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
180947dbdbffSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
18108936aa31SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
1811a4be0ed3SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE_OPTICAL) },
1812e078809dSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
18130e70f97fSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) },
18144424f616SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_LUA) },
1815cb7cf3daSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
18163fce2246SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
18176f3a1936SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK) },
18186f3a1936SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_GLOW) },
18196f3a1936SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_PRO) },
18206a2a6390SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
1821ca9bbdccSStefan Achatz #endif
18221e93674aSAndreas Hübner 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_PS1000) },
1823980a3da6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1824b355850bSDon Prince 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_WIRELESS_KBD_MOUSE) },
1825a6802e00SForest Bond 	{ HID_USB_DEVICE(USB_VENDOR_ID_SIS2_TOUCH, USB_DEVICE_ID_SIS9200_TOUCH) },
1826a6802e00SForest Bond 	{ HID_USB_DEVICE(USB_VENDOR_ID_SIS2_TOUCH, USB_DEVICE_ID_SIS817_TOUCH) },
1827d586dca0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_SKYCABLE, USB_DEVICE_ID_SKYCABLE_WIRELESS_PRESENTER) },
1828f04d5140SColin Leitner 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER) },
1829f04d5140SColin Leitner 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER) },
18305844c1cdSDavid Dillow 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE) },
1831bd28ce00SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
183235dca5b4SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER) },
1833f9ce7c28SBastien Nocera 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1834*0bd88dd3SFrank Praznik 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER) },
1835*0bd88dd3SFrank Praznik 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER) },
1836cc6e0bbbSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
1837c1e0ac19SFernando Luis Vázquez Cao 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE) },
183875dbb953SSimon Wood 	{ HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) },
183990231e7eSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
184030ba2fbdSVivien Didelot 	{ HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
1841daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1842daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
18437a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
18447a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
1845daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
18463ee8f0a2SMarkus Rathgeb 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653) },
1847daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1848d65c3768SSimon Wood 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65a) },
1849740363fbSJiri Kosina 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_TIVO, USB_DEVICE_ID_TIVO_SLIDE_BT) },
185044ea35c1SJarod Wilson 	{ HID_USB_DEVICE(USB_VENDOR_ID_TIVO, USB_DEVICE_ID_TIVO_SLIDE) },
1851f14f526dSLev Babiev 	{ HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
185254001081SKees Bakker 	{ HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED2, USB_DEVICE_ID_TOPSEED2_RF_COMBO) },
1853711a680eSBruno Premont 	{ HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
185441fa9230SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
1855f8a489ccSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
1856f8a489ccSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
1857f8a489ccSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
18586be914f1SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
1859d1257081SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
1860eb4e426aSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
1861fac733f0SJussi Kivilinna 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
186242fc04e5SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SUPER_JOY_BOX_3) },
1863ad395ccaSSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD) },
18641bcc2067SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_3_PRO) },
18651bcc2067SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_DUAL_BOX_PRO) },
18661bcc2067SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_5_PRO) },
1867ca2dcd40SBastien Nocera 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
186878761ff9SPrzemo Firszt 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
186972a46344SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_5_8_INCH) },
187000e7f964SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_12_1_INCH) },
18714fdc18d1SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_Q_PAD) },
1872a786e83cSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_PID_0038) },
187372a46344SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_10_6_INCH) },
18748f1acc32SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_14_1_INCH) },
1875d2ee4dd9SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SIRIUS_BATTERY_FREE_TABLET) },
187674bc6953SStefan Kriwanek 	{ HID_USB_DEVICE(USB_VENDOR_ID_X_TENSIONS, USB_DEVICE_ID_SPEEDLINK_VAD_CEZANNE) },
1877cb2c9e3fSOlivier Scherler 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIN_MO, USB_DEVICE_ID_XIN_MO_DUAL_ARCADE) },
1878daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1879daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1880a9885c8fSDon Prince 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZYDACRON, USB_DEVICE_ID_ZYDACRON_REMOTE_CONTROL) },
18818c19a515SJiri Slaby 
188278a849a6SJiri Slaby 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
188302fb72a0SDavid Herrmann 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1884a33042faSDavid Herrmann 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
188585cdaf52SJiri Slaby 	{ }
188685cdaf52SJiri Slaby };
188785cdaf52SJiri Slaby 
18883a6f82f7SJiri Slaby struct hid_dynid {
18893a6f82f7SJiri Slaby 	struct list_head list;
18903a6f82f7SJiri Slaby 	struct hid_device_id id;
18913a6f82f7SJiri Slaby };
18923a6f82f7SJiri Slaby 
18933a6f82f7SJiri Slaby /**
18943a6f82f7SJiri Slaby  * store_new_id - add a new HID device ID to this driver and re-probe devices
18953a6f82f7SJiri Slaby  * @driver: target device driver
18963a6f82f7SJiri Slaby  * @buf: buffer for scanning device ID data
18973a6f82f7SJiri Slaby  * @count: input size
18983a6f82f7SJiri Slaby  *
18993a6f82f7SJiri Slaby  * Adds a new dynamic hid device ID to this driver,
19003a6f82f7SJiri Slaby  * and causes the driver to probe for all devices again.
19013a6f82f7SJiri Slaby  */
19023a6f82f7SJiri Slaby static ssize_t store_new_id(struct device_driver *drv, const char *buf,
19033a6f82f7SJiri Slaby 		size_t count)
19043a6f82f7SJiri Slaby {
19053a6f82f7SJiri Slaby 	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
19063a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
19073a6f82f7SJiri Slaby 	__u32 bus, vendor, product;
19083a6f82f7SJiri Slaby 	unsigned long driver_data = 0;
19093a6f82f7SJiri Slaby 	int ret;
19103a6f82f7SJiri Slaby 
19113a6f82f7SJiri Slaby 	ret = sscanf(buf, "%x %x %x %lx",
19123a6f82f7SJiri Slaby 			&bus, &vendor, &product, &driver_data);
19133a6f82f7SJiri Slaby 	if (ret < 3)
19143a6f82f7SJiri Slaby 		return -EINVAL;
19153a6f82f7SJiri Slaby 
19163a6f82f7SJiri Slaby 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
19173a6f82f7SJiri Slaby 	if (!dynid)
19183a6f82f7SJiri Slaby 		return -ENOMEM;
19193a6f82f7SJiri Slaby 
19203a6f82f7SJiri Slaby 	dynid->id.bus = bus;
19214d53b801SHenrik Rydberg 	dynid->id.group = HID_GROUP_ANY;
19223a6f82f7SJiri Slaby 	dynid->id.vendor = vendor;
19233a6f82f7SJiri Slaby 	dynid->id.product = product;
19243a6f82f7SJiri Slaby 	dynid->id.driver_data = driver_data;
19253a6f82f7SJiri Slaby 
19263a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
19273a6f82f7SJiri Slaby 	list_add_tail(&dynid->list, &hdrv->dyn_list);
19283a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
19293a6f82f7SJiri Slaby 
19303a6f82f7SJiri Slaby 	ret = driver_attach(&hdrv->driver);
19313a6f82f7SJiri Slaby 
19323a6f82f7SJiri Slaby 	return ret ? : count;
19333a6f82f7SJiri Slaby }
19343a6f82f7SJiri Slaby static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
19353a6f82f7SJiri Slaby 
19363a6f82f7SJiri Slaby static void hid_free_dynids(struct hid_driver *hdrv)
19373a6f82f7SJiri Slaby {
19383a6f82f7SJiri Slaby 	struct hid_dynid *dynid, *n;
19393a6f82f7SJiri Slaby 
19403a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
19413a6f82f7SJiri Slaby 	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
19423a6f82f7SJiri Slaby 		list_del(&dynid->list);
19433a6f82f7SJiri Slaby 		kfree(dynid);
19443a6f82f7SJiri Slaby 	}
19453a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
19463a6f82f7SJiri Slaby }
19473a6f82f7SJiri Slaby 
19483a6f82f7SJiri Slaby static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
19493a6f82f7SJiri Slaby 		struct hid_driver *hdrv)
19503a6f82f7SJiri Slaby {
19513a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
19523a6f82f7SJiri Slaby 
19533a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
19543a6f82f7SJiri Slaby 	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
19553a6f82f7SJiri Slaby 		if (hid_match_one_id(hdev, &dynid->id)) {
19563a6f82f7SJiri Slaby 			spin_unlock(&hdrv->dyn_lock);
19573a6f82f7SJiri Slaby 			return &dynid->id;
19583a6f82f7SJiri Slaby 		}
19593a6f82f7SJiri Slaby 	}
19603a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
19613a6f82f7SJiri Slaby 
19623a6f82f7SJiri Slaby 	return hid_match_id(hdev, hdrv->id_table);
19633a6f82f7SJiri Slaby }
19643a6f82f7SJiri Slaby 
196585cdaf52SJiri Slaby static int hid_bus_match(struct device *dev, struct device_driver *drv)
196685cdaf52SJiri Slaby {
196785cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
196885cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
196985cdaf52SJiri Slaby 
1970070748edSHenrik Rydberg 	return hid_match_device(hdev, hdrv) != NULL;
197185cdaf52SJiri Slaby }
197285cdaf52SJiri Slaby 
197385cdaf52SJiri Slaby static int hid_device_probe(struct device *dev)
197485cdaf52SJiri Slaby {
197585cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(dev->driver,
197685cdaf52SJiri Slaby 			struct hid_driver, driver);
197785cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
197885cdaf52SJiri Slaby 	const struct hid_device_id *id;
197985cdaf52SJiri Slaby 	int ret = 0;
198085cdaf52SJiri Slaby 
19814ea54542SDavid Herrmann 	if (down_interruptible(&hdev->driver_lock))
19824ea54542SDavid Herrmann 		return -EINTR;
1983c849a614SAndrew de los Reyes 	if (down_interruptible(&hdev->driver_input_lock)) {
1984c849a614SAndrew de los Reyes 		ret = -EINTR;
1985c849a614SAndrew de los Reyes 		goto unlock_driver_lock;
1986c849a614SAndrew de los Reyes 	}
1987c849a614SAndrew de los Reyes 	hdev->io_started = false;
19884ea54542SDavid Herrmann 
198985cdaf52SJiri Slaby 	if (!hdev->driver) {
19903a6f82f7SJiri Slaby 		id = hid_match_device(hdev, hdrv);
1991ba623a77SDan Carpenter 		if (id == NULL) {
1992ba623a77SDan Carpenter 			ret = -ENODEV;
1993ba623a77SDan Carpenter 			goto unlock;
1994ba623a77SDan Carpenter 		}
1995c500c971SJiri Slaby 
199685cdaf52SJiri Slaby 		hdev->driver = hdrv;
1997c500c971SJiri Slaby 		if (hdrv->probe) {
1998c500c971SJiri Slaby 			ret = hdrv->probe(hdev, id);
1999c500c971SJiri Slaby 		} else { /* default probe */
2000a7197c2eSHenrik Rydberg 			ret = hid_open_report(hdev);
2001c500c971SJiri Slaby 			if (!ret)
200293c10132SJiri Slaby 				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2003c500c971SJiri Slaby 		}
2004a7197c2eSHenrik Rydberg 		if (ret) {
2005a7197c2eSHenrik Rydberg 			hid_close_report(hdev);
2006c500c971SJiri Slaby 			hdev->driver = NULL;
200785cdaf52SJiri Slaby 		}
2008a7197c2eSHenrik Rydberg 	}
2009ba623a77SDan Carpenter unlock:
2010c849a614SAndrew de los Reyes 	if (!hdev->io_started)
2011c849a614SAndrew de los Reyes 		up(&hdev->driver_input_lock);
2012c849a614SAndrew de los Reyes unlock_driver_lock:
20134ea54542SDavid Herrmann 	up(&hdev->driver_lock);
201485cdaf52SJiri Slaby 	return ret;
201585cdaf52SJiri Slaby }
201685cdaf52SJiri Slaby 
201785cdaf52SJiri Slaby static int hid_device_remove(struct device *dev)
201885cdaf52SJiri Slaby {
201985cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
20204ea54542SDavid Herrmann 	struct hid_driver *hdrv;
2021c849a614SAndrew de los Reyes 	int ret = 0;
202285cdaf52SJiri Slaby 
20234ea54542SDavid Herrmann 	if (down_interruptible(&hdev->driver_lock))
20244ea54542SDavid Herrmann 		return -EINTR;
2025c849a614SAndrew de los Reyes 	if (down_interruptible(&hdev->driver_input_lock)) {
2026c849a614SAndrew de los Reyes 		ret = -EINTR;
2027c849a614SAndrew de los Reyes 		goto unlock_driver_lock;
2028c849a614SAndrew de los Reyes 	}
2029c849a614SAndrew de los Reyes 	hdev->io_started = false;
20304ea54542SDavid Herrmann 
20314ea54542SDavid Herrmann 	hdrv = hdev->driver;
203285cdaf52SJiri Slaby 	if (hdrv) {
203385cdaf52SJiri Slaby 		if (hdrv->remove)
203485cdaf52SJiri Slaby 			hdrv->remove(hdev);
2035c500c971SJiri Slaby 		else /* default remove */
2036c500c971SJiri Slaby 			hid_hw_stop(hdev);
2037a7197c2eSHenrik Rydberg 		hid_close_report(hdev);
203885cdaf52SJiri Slaby 		hdev->driver = NULL;
203985cdaf52SJiri Slaby 	}
204085cdaf52SJiri Slaby 
2041c849a614SAndrew de los Reyes 	if (!hdev->io_started)
2042c849a614SAndrew de los Reyes 		up(&hdev->driver_input_lock);
2043c849a614SAndrew de los Reyes unlock_driver_lock:
20444ea54542SDavid Herrmann 	up(&hdev->driver_lock);
2045c849a614SAndrew de los Reyes 	return ret;
204685cdaf52SJiri Slaby }
204785cdaf52SJiri Slaby 
20484d53b801SHenrik Rydberg static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
20494d53b801SHenrik Rydberg 			     char *buf)
20504d53b801SHenrik Rydberg {
20514d53b801SHenrik Rydberg 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
20524d53b801SHenrik Rydberg 	int len;
20534d53b801SHenrik Rydberg 
20544d53b801SHenrik Rydberg 	len = snprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
20554d53b801SHenrik Rydberg 		       hdev->bus, hdev->group, hdev->vendor, hdev->product);
20564d53b801SHenrik Rydberg 
20574d53b801SHenrik Rydberg 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
20584d53b801SHenrik Rydberg }
20590d4260e0SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
20604d53b801SHenrik Rydberg 
20610d4260e0SGreg Kroah-Hartman static struct attribute *hid_dev_attrs[] = {
20620d4260e0SGreg Kroah-Hartman 	&dev_attr_modalias.attr,
20630d4260e0SGreg Kroah-Hartman 	NULL,
20644d53b801SHenrik Rydberg };
20650d4260e0SGreg Kroah-Hartman ATTRIBUTE_GROUPS(hid_dev);
20664d53b801SHenrik Rydberg 
206785cdaf52SJiri Slaby static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
206885cdaf52SJiri Slaby {
206985cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
207085cdaf52SJiri Slaby 
207185cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
207285cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
207385cdaf52SJiri Slaby 		return -ENOMEM;
207485cdaf52SJiri Slaby 
207585cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
207685cdaf52SJiri Slaby 		return -ENOMEM;
207785cdaf52SJiri Slaby 
207885cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
207985cdaf52SJiri Slaby 		return -ENOMEM;
208085cdaf52SJiri Slaby 
208185cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
208285cdaf52SJiri Slaby 		return -ENOMEM;
208385cdaf52SJiri Slaby 
20844d53b801SHenrik Rydberg 	if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
20854d53b801SHenrik Rydberg 			   hdev->bus, hdev->group, hdev->vendor, hdev->product))
208685cdaf52SJiri Slaby 		return -ENOMEM;
208785cdaf52SJiri Slaby 
208885cdaf52SJiri Slaby 	return 0;
208985cdaf52SJiri Slaby }
209085cdaf52SJiri Slaby 
209185cdaf52SJiri Slaby static struct bus_type hid_bus_type = {
209285cdaf52SJiri Slaby 	.name		= "hid",
20930d4260e0SGreg Kroah-Hartman 	.dev_groups	= hid_dev_groups,
209485cdaf52SJiri Slaby 	.match		= hid_bus_match,
209585cdaf52SJiri Slaby 	.probe		= hid_device_probe,
209685cdaf52SJiri Slaby 	.remove		= hid_device_remove,
209785cdaf52SJiri Slaby 	.uevent		= hid_uevent,
209885cdaf52SJiri Slaby };
209985cdaf52SJiri Slaby 
2100bae7eb33SJiri Kosina /* a list of devices that shouldn't be handled by HID core at all */
2101d458a9dfSJiri Slaby static const struct hid_device_id hid_ignore_list[] = {
2102d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
2103d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
2104d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
2105d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
2106d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
2107d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
2108d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
2109d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
2110d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
2111d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
2112d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
2113d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
211477f720b7SStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM)},
211577f720b7SStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM2)},
211662a56582SAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
211711030183SHans de Goede 	{ HID_USB_DEVICE(USB_VENDOR_ID_AXENTIA, USB_DEVICE_ID_AXENTIA_FM_RADIO) },
2118d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
2119d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
2120d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
2121d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
2122d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
2123d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
21245f6108cfSAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
2125d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
2126d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
212773bc7d31SMelchior FRANZ 	{ HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, 0x0004) },
2128789aaa2eSDan Delaney 	{ HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, 0x000a) },
2129d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
213070c66567SPetr Štetiar 	{ HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC5UH) },
2131df506f2cSPetr Štetiar 	{ HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC4UM) },
2132b1807719SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
2133d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
2134d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
2135d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
2136d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
2137d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
2138d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
2139d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
2140d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
2141d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
2142d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
2143d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
2144d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
2145d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
2146d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
2147d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
2148d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
21498e2ce73eSHans de Goede 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_RADIOSHARK) },
2150d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
2151d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
2152d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
2153d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
2154d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
2155d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
2156d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
2157d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
2158d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
2159d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
2160d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
2161d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
2162d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
2163d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
2164d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
2165d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
2166d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
2167d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
2168d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
2169d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
2170d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
2171d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
2172d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
2173d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
2174d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
2175d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
2176d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
2177d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
2178d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
2179d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
2180d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
2181d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
2182d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
2183d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
2184d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
2185d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
2186d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
2187d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
2188d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
2189d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
2190d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
2191d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
2192d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
2193d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
2194d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
2195d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
2196d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
219731b9779cSVincent Palatin 	{ HID_USB_DEVICE(USB_VENDOR_ID_JABRA, USB_DEVICE_ID_JABRA_SPEAK_410) },
219831b9779cSVincent Palatin 	{ HID_USB_DEVICE(USB_VENDOR_ID_JABRA, USB_DEVICE_ID_JABRA_SPEAK_510) },
2199d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
2200c91c21c5SAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
2201d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
2202eb8141ccSLamarque Vieira Souza 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_KYE, 0x0058) },
2203d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
2204ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) },
2205d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
2206ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) },
2207d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
2208ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) },
2209ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) },
2210ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) },
2211ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) },
2212ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) },
2213ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) },
2214d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
2215d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
2216d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
2217ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) },
2218ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) },
2219ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) },
2220d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
2221d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
2222ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) },
2223d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
2224d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
2225d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
2226d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
2227d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
2228ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) },
2229ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) },
2230ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) },
2231ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) },
2232ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) },
2233ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) },
2234ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) },
22353ffb62cbSYuri Khan 	{ HID_USB_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_BEATPAD) },
2236d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
2237d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
2238d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
2239d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
2240d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
2241d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
2242d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
2243d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
2244d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
2245d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
2246d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
2247d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
2248d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
2249d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
2250d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
2251d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
2252d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
2253d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
2254d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
22554cfae3e8SHenning Glawe 	{ HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
225635cfd1d9SMichael Tokarev 	{ HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
22578491ee10SJan Steinhoff #if defined(CONFIG_MOUSE_SYNAPTICS_USB) || defined(CONFIG_MOUSE_SYNAPTICS_USB_MODULE)
22588491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_TP) },
22598491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_INT_TP) },
22608491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_CPAD) },
22618491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_STICK) },
22628491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_WP) },
22638491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_COMP_TP) },
22648491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_WTP) },
22658491ee10SJan Steinhoff 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_DPAD) },
22668491ee10SJan Steinhoff #endif
2267d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
2268d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
2269d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
2270d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
2271d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
2272d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
2273d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
2274d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
2275d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
2276d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
2277d458a9dfSJiri Slaby 	{ }
2278d458a9dfSJiri Slaby };
2279d458a9dfSJiri Slaby 
2280b4d8e473SJiri Slaby /**
2281b4d8e473SJiri Slaby  * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
2282b4d8e473SJiri Slaby  *
2283b4d8e473SJiri Slaby  * There are composite devices for which we want to ignore only a certain
2284b4d8e473SJiri Slaby  * interface. This is a list of devices for which only the mouse interface will
2285b4d8e473SJiri Slaby  * be ignored. This allows a dedicated driver to take care of the interface.
2286b4d8e473SJiri Slaby  */
2287b4d8e473SJiri Slaby static const struct hid_device_id hid_mouse_ignore_list[] = {
2288b4d8e473SJiri Slaby 	/* appletouch driver */
2289b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
2290b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
2291b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
2292b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
2293b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
2294b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
2295b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
2296b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
2297b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
2298b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
2299b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
2300b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
2301b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
2302b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
2303b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
2304b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
2305b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
2306b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
2307b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
2308b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
2309ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
2310ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
2311ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
231299b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
231399b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
231499b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
231599b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
231699b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
231799b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
231847340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI) },
231947340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO) },
232047340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS) },
2321213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI) },
2322213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO) },
2323213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS) },
23244b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI) },
23254b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO) },
23264b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS) },
23274b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI) },
23284b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO) },
23294b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS) },
2330b2e6ad7dSRyan Bourgeois 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI) },
2331b2e6ad7dSRyan Bourgeois 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO) },
2332b2e6ad7dSRyan Bourgeois 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS) },
23338d80da90SDirk Hohndel 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI) },
23348d80da90SDirk Hohndel 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO) },
23358d80da90SDirk Hohndel 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS) },
23369d9a04eeSDmitry Torokhov 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI) },
23379d9a04eeSDmitry Torokhov 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO) },
23389d9a04eeSDmitry Torokhov 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS) },
2339b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
2340b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
2341b4d8e473SJiri Slaby 	{ }
2342b4d8e473SJiri Slaby };
2343b4d8e473SJiri Slaby 
23444529eefaSLamarque V. Souza bool hid_ignore(struct hid_device *hdev)
2345d458a9dfSJiri Slaby {
23464529eefaSLamarque V. Souza 	if (hdev->quirks & HID_QUIRK_NO_IGNORE)
23474529eefaSLamarque V. Souza 		return false;
23484529eefaSLamarque V. Souza 	if (hdev->quirks & HID_QUIRK_IGNORE)
23494529eefaSLamarque V. Souza 		return true;
23504529eefaSLamarque V. Souza 
2351d458a9dfSJiri Slaby 	switch (hdev->vendor) {
2352d458a9dfSJiri Slaby 	case USB_VENDOR_ID_CODEMERCS:
2353d458a9dfSJiri Slaby 		/* ignore all Code Mercenaries IOWarrior devices */
2354d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
2355d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
2356d458a9dfSJiri Slaby 			return true;
2357d458a9dfSJiri Slaby 		break;
2358d458a9dfSJiri Slaby 	case USB_VENDOR_ID_LOGITECH:
2359d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
2360d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
2361d458a9dfSJiri Slaby 			return true;
236265dd3b69SHans Verkuil 		/*
236365dd3b69SHans Verkuil 		 * The Keene FM transmitter USB device has the same USB ID as
236465dd3b69SHans Verkuil 		 * the Logitech AudioHub Speaker, but it should ignore the hid.
236565dd3b69SHans Verkuil 		 * Check if the name is that of the Keene device.
236665dd3b69SHans Verkuil 		 * For reference: the name of the AudioHub is
236765dd3b69SHans Verkuil 		 * "HOLTEK  AudioHub Speaker".
236865dd3b69SHans Verkuil 		 */
236965dd3b69SHans Verkuil 		if (hdev->product == USB_DEVICE_ID_LOGITECH_AUDIOHUB &&
237065dd3b69SHans Verkuil 			!strcmp(hdev->name, "HOLTEK  B-LINK USB Audio  "))
237165dd3b69SHans Verkuil 				return true;
2372d458a9dfSJiri Slaby 		break;
237331f7fd79SJarod Wilson 	case USB_VENDOR_ID_SOUNDGRAPH:
237431f7fd79SJarod Wilson 		if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
237531f7fd79SJarod Wilson 		    hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
237631f7fd79SJarod Wilson 			return true;
237731f7fd79SJarod Wilson 		break;
2378bba5394aSXing Wei 	case USB_VENDOR_ID_HANWANG:
2379bba5394aSXing Wei 		if (hdev->product >= USB_DEVICE_ID_HANWANG_TABLET_FIRST &&
2380bba5394aSXing Wei 		    hdev->product <= USB_DEVICE_ID_HANWANG_TABLET_LAST)
2381bba5394aSXing Wei 			return true;
2382bba5394aSXing Wei 		break;
23836dc1418eSTomoki Sekiyama 	case USB_VENDOR_ID_JESS:
23846dc1418eSTomoki Sekiyama 		if (hdev->product == USB_DEVICE_ID_JESS_YUREX &&
23856dc1418eSTomoki Sekiyama 				hdev->type == HID_TYPE_USBNONE)
23866dc1418eSTomoki Sekiyama 			return true;
23876dc1418eSTomoki Sekiyama 		break;
238830b6b7d8SIan Abbott 	case USB_VENDOR_ID_VELLEMAN:
238930b6b7d8SIan Abbott 		/* These are not HID devices.  They are handled by comedi. */
239030b6b7d8SIan Abbott 		if ((hdev->product >= USB_DEVICE_ID_VELLEMAN_K8055_FIRST &&
239130b6b7d8SIan Abbott 		     hdev->product <= USB_DEVICE_ID_VELLEMAN_K8055_LAST) ||
239230b6b7d8SIan Abbott 		    (hdev->product >= USB_DEVICE_ID_VELLEMAN_K8061_FIRST &&
239330b6b7d8SIan Abbott 		     hdev->product <= USB_DEVICE_ID_VELLEMAN_K8061_LAST))
239430b6b7d8SIan Abbott 			return true;
239530b6b7d8SIan Abbott 		break;
23965b4617d8SAlexey Klimov 	case USB_VENDOR_ID_ATMEL_V_USB:
23975b4617d8SAlexey Klimov 		/* Masterkit MA901 usb radio based on Atmel tiny85 chip and
23985b4617d8SAlexey Klimov 		 * it has the same USB ID as many Atmel V-USB devices. This
23995b4617d8SAlexey Klimov 		 * usb radio is handled by radio-ma901.c driver so we want
24005b4617d8SAlexey Klimov 		 * ignore the hid. Check the name, bus, product and ignore
24015b4617d8SAlexey Klimov 		 * if we have MA901 usb radio.
24025b4617d8SAlexey Klimov 		 */
24035b4617d8SAlexey Klimov 		if (hdev->product == USB_DEVICE_ID_ATMEL_V_USB &&
24045b4617d8SAlexey Klimov 			hdev->bus == BUS_USB &&
24055b4617d8SAlexey Klimov 			strncmp(hdev->name, "www.masterkit.ru MA901", 22) == 0)
24065b4617d8SAlexey Klimov 			return true;
24075b4617d8SAlexey Klimov 		break;
2408d458a9dfSJiri Slaby 	}
2409d458a9dfSJiri Slaby 
2410b4d8e473SJiri Slaby 	if (hdev->type == HID_TYPE_USBMOUSE &&
2411b4d8e473SJiri Slaby 			hid_match_id(hdev, hid_mouse_ignore_list))
2412b4d8e473SJiri Slaby 		return true;
2413b4d8e473SJiri Slaby 
2414d458a9dfSJiri Slaby 	return !!hid_match_id(hdev, hid_ignore_list);
2415d458a9dfSJiri Slaby }
24164529eefaSLamarque V. Souza EXPORT_SYMBOL_GPL(hid_ignore);
2417d458a9dfSJiri Slaby 
241885cdaf52SJiri Slaby int hid_add_device(struct hid_device *hdev)
241985cdaf52SJiri Slaby {
242085cdaf52SJiri Slaby 	static atomic_t id = ATOMIC_INIT(0);
242185cdaf52SJiri Slaby 	int ret;
242285cdaf52SJiri Slaby 
242385cdaf52SJiri Slaby 	if (WARN_ON(hdev->status & HID_STAT_ADDED))
242485cdaf52SJiri Slaby 		return -EBUSY;
242585cdaf52SJiri Slaby 
2426d458a9dfSJiri Slaby 	/* we need to kill them here, otherwise they will stay allocated to
2427d458a9dfSJiri Slaby 	 * wait for coming driver */
24284529eefaSLamarque V. Souza 	if (hid_ignore(hdev))
2429d458a9dfSJiri Slaby 		return -ENODEV;
2430d458a9dfSJiri Slaby 
2431a7197c2eSHenrik Rydberg 	/*
2432a7197c2eSHenrik Rydberg 	 * Read the device report descriptor once and use as template
2433a7197c2eSHenrik Rydberg 	 * for the driver-specific modifications.
2434a7197c2eSHenrik Rydberg 	 */
2435a7197c2eSHenrik Rydberg 	ret = hdev->ll_driver->parse(hdev);
2436a7197c2eSHenrik Rydberg 	if (ret)
2437a7197c2eSHenrik Rydberg 		return ret;
2438a7197c2eSHenrik Rydberg 	if (!hdev->dev_rdesc)
2439a7197c2eSHenrik Rydberg 		return -ENODEV;
2440a7197c2eSHenrik Rydberg 
2441734c6609SHenrik Rydberg 	/*
2442734c6609SHenrik Rydberg 	 * Scan generic devices for group information
2443734c6609SHenrik Rydberg 	 */
2444734c6609SHenrik Rydberg 	if (hid_ignore_special_drivers ||
2445734c6609SHenrik Rydberg 	    !hid_match_id(hdev, hid_have_special_driver)) {
2446734c6609SHenrik Rydberg 		ret = hid_scan_report(hdev);
2447734c6609SHenrik Rydberg 		if (ret)
2448734c6609SHenrik Rydberg 			hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2449734c6609SHenrik Rydberg 	}
2450734c6609SHenrik Rydberg 
24516bbe586fSKay Sievers 	/* XXX hack, any other cleaner solution after the driver core
24526bbe586fSKay Sievers 	 * is converted to allow more than 20 bytes as the device name? */
24536bbe586fSKay Sievers 	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
245485cdaf52SJiri Slaby 		     hdev->vendor, hdev->product, atomic_inc_return(&id));
245585cdaf52SJiri Slaby 
24564da361b6SBruno Prémont 	hid_debug_register(hdev, dev_name(&hdev->dev));
245785cdaf52SJiri Slaby 	ret = device_add(&hdev->dev);
245885cdaf52SJiri Slaby 	if (!ret)
245985cdaf52SJiri Slaby 		hdev->status |= HID_STAT_ADDED;
24604da361b6SBruno Prémont 	else
24614da361b6SBruno Prémont 		hid_debug_unregister(hdev);
2462a635f9ddSJiri Kosina 
246385cdaf52SJiri Slaby 	return ret;
246485cdaf52SJiri Slaby }
246585cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_add_device);
246685cdaf52SJiri Slaby 
246785cdaf52SJiri Slaby /**
246885cdaf52SJiri Slaby  * hid_allocate_device - allocate new hid device descriptor
246985cdaf52SJiri Slaby  *
247085cdaf52SJiri Slaby  * Allocate and initialize hid device, so that hid_destroy_device might be
247185cdaf52SJiri Slaby  * used to free it.
247285cdaf52SJiri Slaby  *
247385cdaf52SJiri Slaby  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
247485cdaf52SJiri Slaby  * error value.
247585cdaf52SJiri Slaby  */
247685cdaf52SJiri Slaby struct hid_device *hid_allocate_device(void)
247785cdaf52SJiri Slaby {
247885cdaf52SJiri Slaby 	struct hid_device *hdev;
247985cdaf52SJiri Slaby 	int ret = -ENOMEM;
248085cdaf52SJiri Slaby 
248185cdaf52SJiri Slaby 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
248285cdaf52SJiri Slaby 	if (hdev == NULL)
248385cdaf52SJiri Slaby 		return ERR_PTR(ret);
248485cdaf52SJiri Slaby 
248585cdaf52SJiri Slaby 	device_initialize(&hdev->dev);
248685cdaf52SJiri Slaby 	hdev->dev.release = hid_device_release;
248785cdaf52SJiri Slaby 	hdev->dev.bus = &hid_bus_type;
248885cdaf52SJiri Slaby 
2489a7197c2eSHenrik Rydberg 	hid_close_report(hdev);
249085cdaf52SJiri Slaby 
2491cd667ce2SJiri Kosina 	init_waitqueue_head(&hdev->debug_wait);
2492cd667ce2SJiri Kosina 	INIT_LIST_HEAD(&hdev->debug_list);
24931deb9d34SJiri Kosina 	spin_lock_init(&hdev->debug_list_lock);
24944ea54542SDavid Herrmann 	sema_init(&hdev->driver_lock, 1);
2495c849a614SAndrew de los Reyes 	sema_init(&hdev->driver_input_lock, 1);
2496cd667ce2SJiri Kosina 
249785cdaf52SJiri Slaby 	return hdev;
249885cdaf52SJiri Slaby }
249985cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_allocate_device);
250085cdaf52SJiri Slaby 
250185cdaf52SJiri Slaby static void hid_remove_device(struct hid_device *hdev)
250285cdaf52SJiri Slaby {
250385cdaf52SJiri Slaby 	if (hdev->status & HID_STAT_ADDED) {
250485cdaf52SJiri Slaby 		device_del(&hdev->dev);
2505a635f9ddSJiri Kosina 		hid_debug_unregister(hdev);
250685cdaf52SJiri Slaby 		hdev->status &= ~HID_STAT_ADDED;
250785cdaf52SJiri Slaby 	}
2508a7197c2eSHenrik Rydberg 	kfree(hdev->dev_rdesc);
2509a7197c2eSHenrik Rydberg 	hdev->dev_rdesc = NULL;
2510a7197c2eSHenrik Rydberg 	hdev->dev_rsize = 0;
251185cdaf52SJiri Slaby }
251285cdaf52SJiri Slaby 
251385cdaf52SJiri Slaby /**
251485cdaf52SJiri Slaby  * hid_destroy_device - free previously allocated device
251585cdaf52SJiri Slaby  *
251685cdaf52SJiri Slaby  * @hdev: hid device
251785cdaf52SJiri Slaby  *
251885cdaf52SJiri Slaby  * If you allocate hid_device through hid_allocate_device, you should ever
251985cdaf52SJiri Slaby  * free by this function.
252085cdaf52SJiri Slaby  */
252185cdaf52SJiri Slaby void hid_destroy_device(struct hid_device *hdev)
252285cdaf52SJiri Slaby {
252385cdaf52SJiri Slaby 	hid_remove_device(hdev);
252485cdaf52SJiri Slaby 	put_device(&hdev->dev);
252585cdaf52SJiri Slaby }
252685cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_destroy_device);
252785cdaf52SJiri Slaby 
252885cdaf52SJiri Slaby int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
252985cdaf52SJiri Slaby 		const char *mod_name)
253085cdaf52SJiri Slaby {
25313a6f82f7SJiri Slaby 	int ret;
25323a6f82f7SJiri Slaby 
253385cdaf52SJiri Slaby 	hdrv->driver.name = hdrv->name;
253485cdaf52SJiri Slaby 	hdrv->driver.bus = &hid_bus_type;
253585cdaf52SJiri Slaby 	hdrv->driver.owner = owner;
253685cdaf52SJiri Slaby 	hdrv->driver.mod_name = mod_name;
253785cdaf52SJiri Slaby 
25383a6f82f7SJiri Slaby 	INIT_LIST_HEAD(&hdrv->dyn_list);
25393a6f82f7SJiri Slaby 	spin_lock_init(&hdrv->dyn_lock);
25403a6f82f7SJiri Slaby 
25413a6f82f7SJiri Slaby 	ret = driver_register(&hdrv->driver);
25423a6f82f7SJiri Slaby 	if (ret)
25433a6f82f7SJiri Slaby 		return ret;
25443a6f82f7SJiri Slaby 
25453a6f82f7SJiri Slaby 	ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
25463a6f82f7SJiri Slaby 	if (ret)
25473a6f82f7SJiri Slaby 		driver_unregister(&hdrv->driver);
25483a6f82f7SJiri Slaby 
25493a6f82f7SJiri Slaby 	return ret;
255085cdaf52SJiri Slaby }
255185cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(__hid_register_driver);
255285cdaf52SJiri Slaby 
255385cdaf52SJiri Slaby void hid_unregister_driver(struct hid_driver *hdrv)
255485cdaf52SJiri Slaby {
25553a6f82f7SJiri Slaby 	driver_remove_file(&hdrv->driver, &driver_attr_new_id);
255685cdaf52SJiri Slaby 	driver_unregister(&hdrv->driver);
25573a6f82f7SJiri Slaby 	hid_free_dynids(hdrv);
255885cdaf52SJiri Slaby }
255985cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_unregister_driver);
256085cdaf52SJiri Slaby 
25610361a28dSOliver Neukum int hid_check_keys_pressed(struct hid_device *hid)
25620361a28dSOliver Neukum {
25630361a28dSOliver Neukum 	struct hid_input *hidinput;
25640361a28dSOliver Neukum 	int i;
25650361a28dSOliver Neukum 
2566e5288eb5SJiri Kosina 	if (!(hid->claimed & HID_CLAIMED_INPUT))
2567e5288eb5SJiri Kosina 		return 0;
2568e5288eb5SJiri Kosina 
25690361a28dSOliver Neukum 	list_for_each_entry(hidinput, &hid->inputs, list) {
25700361a28dSOliver Neukum 		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
25710361a28dSOliver Neukum 			if (hidinput->input->key[i])
25720361a28dSOliver Neukum 				return 1;
25730361a28dSOliver Neukum 	}
25740361a28dSOliver Neukum 
25750361a28dSOliver Neukum 	return 0;
25760361a28dSOliver Neukum }
25770361a28dSOliver Neukum 
25780361a28dSOliver Neukum EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
25790361a28dSOliver Neukum 
258086166b7bSJiri Kosina static int __init hid_init(void)
258186166b7bSJiri Kosina {
258285cdaf52SJiri Slaby 	int ret;
258385cdaf52SJiri Slaby 
2584a635f9ddSJiri Kosina 	if (hid_debug)
25854291ee30SJoe Perches 		pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
25864291ee30SJoe Perches 			"debugfs is now used for inspecting the device (report descriptor, reports)\n");
2587a635f9ddSJiri Kosina 
258885cdaf52SJiri Slaby 	ret = bus_register(&hid_bus_type);
258985cdaf52SJiri Slaby 	if (ret) {
25904291ee30SJoe Perches 		pr_err("can't register hid bus\n");
259185cdaf52SJiri Slaby 		goto err;
259285cdaf52SJiri Slaby 	}
259385cdaf52SJiri Slaby 
259485cdaf52SJiri Slaby 	ret = hidraw_init();
259585cdaf52SJiri Slaby 	if (ret)
259685cdaf52SJiri Slaby 		goto err_bus;
259785cdaf52SJiri Slaby 
2598a635f9ddSJiri Kosina 	hid_debug_init();
2599a635f9ddSJiri Kosina 
260085cdaf52SJiri Slaby 	return 0;
260185cdaf52SJiri Slaby err_bus:
260285cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
260385cdaf52SJiri Slaby err:
260485cdaf52SJiri Slaby 	return ret;
260586166b7bSJiri Kosina }
260686166b7bSJiri Kosina 
260786166b7bSJiri Kosina static void __exit hid_exit(void)
260886166b7bSJiri Kosina {
2609a635f9ddSJiri Kosina 	hid_debug_exit();
261086166b7bSJiri Kosina 	hidraw_exit();
261185cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
261286166b7bSJiri Kosina }
261386166b7bSJiri Kosina 
261486166b7bSJiri Kosina module_init(hid_init);
261586166b7bSJiri Kosina module_exit(hid_exit);
261686166b7bSJiri Kosina 
261788adb72bSJiri Kosina MODULE_AUTHOR("Andreas Gal");
261888adb72bSJiri Kosina MODULE_AUTHOR("Vojtech Pavlik");
261988adb72bSJiri Kosina MODULE_AUTHOR("Jiri Kosina");
2620aa938f79SJiri Kosina MODULE_LICENSE(DRIVER_LICENSE);
2621aa938f79SJiri Kosina 
2622