xref: /openbmc/linux/drivers/hid/hid-core.c (revision 212da74da783ba9d4459799f4aaecd5de217a312)
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 
66dde5845aSJiri Kosina 	if (report_enum->report_id_hash[id])
67dde5845aSJiri Kosina 		return report_enum->report_id_hash[id];
68dde5845aSJiri Kosina 
69a3789a17SJoe Perches 	report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
70a3789a17SJoe Perches 	if (!report)
71dde5845aSJiri Kosina 		return NULL;
72dde5845aSJiri Kosina 
73dde5845aSJiri Kosina 	if (id != 0)
74dde5845aSJiri Kosina 		report_enum->numbered = 1;
75dde5845aSJiri Kosina 
76dde5845aSJiri Kosina 	report->id = id;
77dde5845aSJiri Kosina 	report->type = type;
78dde5845aSJiri Kosina 	report->size = 0;
79dde5845aSJiri Kosina 	report->device = device;
80dde5845aSJiri Kosina 	report_enum->report_id_hash[id] = report;
81dde5845aSJiri Kosina 
82dde5845aSJiri Kosina 	list_add_tail(&report->list, &report_enum->report_list);
83dde5845aSJiri Kosina 
84dde5845aSJiri Kosina 	return report;
85dde5845aSJiri Kosina }
8690a006abSMichael Poole EXPORT_SYMBOL_GPL(hid_register_report);
87dde5845aSJiri Kosina 
88dde5845aSJiri Kosina /*
89dde5845aSJiri Kosina  * Register a new field for this report.
90dde5845aSJiri Kosina  */
91dde5845aSJiri Kosina 
92dde5845aSJiri Kosina static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
93dde5845aSJiri Kosina {
94dde5845aSJiri Kosina 	struct hid_field *field;
95dde5845aSJiri Kosina 
96dde5845aSJiri Kosina 	if (report->maxfield == HID_MAX_FIELDS) {
978c3d52fcSJiri Kosina 		hid_err(report->device, "too many fields in report\n");
98dde5845aSJiri Kosina 		return NULL;
99dde5845aSJiri Kosina 	}
100dde5845aSJiri Kosina 
101a3789a17SJoe Perches 	field = kzalloc((sizeof(struct hid_field) +
102a3789a17SJoe Perches 			 usages * sizeof(struct hid_usage) +
103a3789a17SJoe Perches 			 values * sizeof(unsigned)), GFP_KERNEL);
104a3789a17SJoe Perches 	if (!field)
105a3789a17SJoe Perches 		return NULL;
106dde5845aSJiri Kosina 
107dde5845aSJiri Kosina 	field->index = report->maxfield++;
108dde5845aSJiri Kosina 	report->field[field->index] = field;
109dde5845aSJiri Kosina 	field->usage = (struct hid_usage *)(field + 1);
110282bfd4cSJiri Slaby 	field->value = (s32 *)(field->usage + usages);
111dde5845aSJiri Kosina 	field->report = report;
112dde5845aSJiri Kosina 
113dde5845aSJiri Kosina 	return field;
114dde5845aSJiri Kosina }
115dde5845aSJiri Kosina 
116dde5845aSJiri Kosina /*
117dde5845aSJiri Kosina  * Open a collection. The type/usage is pushed on the stack.
118dde5845aSJiri Kosina  */
119dde5845aSJiri Kosina 
120dde5845aSJiri Kosina static int open_collection(struct hid_parser *parser, unsigned type)
121dde5845aSJiri Kosina {
122dde5845aSJiri Kosina 	struct hid_collection *collection;
123dde5845aSJiri Kosina 	unsigned usage;
124dde5845aSJiri Kosina 
125dde5845aSJiri Kosina 	usage = parser->local.usage[0];
126dde5845aSJiri Kosina 
127dde5845aSJiri Kosina 	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
1288c3d52fcSJiri Kosina 		hid_err(parser->device, "collection stack overflow\n");
129dde5845aSJiri Kosina 		return -1;
130dde5845aSJiri Kosina 	}
131dde5845aSJiri Kosina 
132dde5845aSJiri Kosina 	if (parser->device->maxcollection == parser->device->collection_size) {
133dde5845aSJiri Kosina 		collection = kmalloc(sizeof(struct hid_collection) *
134dde5845aSJiri Kosina 				parser->device->collection_size * 2, GFP_KERNEL);
135dde5845aSJiri Kosina 		if (collection == NULL) {
1368c3d52fcSJiri Kosina 			hid_err(parser->device, "failed to reallocate collection array\n");
137dde5845aSJiri Kosina 			return -1;
138dde5845aSJiri Kosina 		}
139dde5845aSJiri Kosina 		memcpy(collection, parser->device->collection,
140dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
141dde5845aSJiri Kosina 			parser->device->collection_size);
142dde5845aSJiri Kosina 		memset(collection + parser->device->collection_size, 0,
143dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
144dde5845aSJiri Kosina 			parser->device->collection_size);
145dde5845aSJiri Kosina 		kfree(parser->device->collection);
146dde5845aSJiri Kosina 		parser->device->collection = collection;
147dde5845aSJiri Kosina 		parser->device->collection_size *= 2;
148dde5845aSJiri Kosina 	}
149dde5845aSJiri Kosina 
150dde5845aSJiri Kosina 	parser->collection_stack[parser->collection_stack_ptr++] =
151dde5845aSJiri Kosina 		parser->device->maxcollection;
152dde5845aSJiri Kosina 
153dde5845aSJiri Kosina 	collection = parser->device->collection +
154dde5845aSJiri Kosina 		parser->device->maxcollection++;
155dde5845aSJiri Kosina 	collection->type = type;
156dde5845aSJiri Kosina 	collection->usage = usage;
157dde5845aSJiri Kosina 	collection->level = parser->collection_stack_ptr - 1;
158dde5845aSJiri Kosina 
159dde5845aSJiri Kosina 	if (type == HID_COLLECTION_APPLICATION)
160dde5845aSJiri Kosina 		parser->device->maxapplication++;
161dde5845aSJiri Kosina 
162dde5845aSJiri Kosina 	return 0;
163dde5845aSJiri Kosina }
164dde5845aSJiri Kosina 
165dde5845aSJiri Kosina /*
166dde5845aSJiri Kosina  * Close a collection.
167dde5845aSJiri Kosina  */
168dde5845aSJiri Kosina 
169dde5845aSJiri Kosina static int close_collection(struct hid_parser *parser)
170dde5845aSJiri Kosina {
171dde5845aSJiri Kosina 	if (!parser->collection_stack_ptr) {
1728c3d52fcSJiri Kosina 		hid_err(parser->device, "collection stack underflow\n");
173dde5845aSJiri Kosina 		return -1;
174dde5845aSJiri Kosina 	}
175dde5845aSJiri Kosina 	parser->collection_stack_ptr--;
176dde5845aSJiri Kosina 	return 0;
177dde5845aSJiri Kosina }
178dde5845aSJiri Kosina 
179dde5845aSJiri Kosina /*
180dde5845aSJiri Kosina  * Climb up the stack, search for the specified collection type
181dde5845aSJiri Kosina  * and return the usage.
182dde5845aSJiri Kosina  */
183dde5845aSJiri Kosina 
184dde5845aSJiri Kosina static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
185dde5845aSJiri Kosina {
186504499f2SJoe Perches 	struct hid_collection *collection = parser->device->collection;
187dde5845aSJiri Kosina 	int n;
188504499f2SJoe Perches 
189504499f2SJoe Perches 	for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
190504499f2SJoe Perches 		unsigned index = parser->collection_stack[n];
191504499f2SJoe Perches 		if (collection[index].type == type)
192504499f2SJoe Perches 			return collection[index].usage;
193504499f2SJoe Perches 	}
194dde5845aSJiri Kosina 	return 0; /* we know nothing about this usage type */
195dde5845aSJiri Kosina }
196dde5845aSJiri Kosina 
197dde5845aSJiri Kosina /*
198dde5845aSJiri Kosina  * Add a usage to the temporary parser table.
199dde5845aSJiri Kosina  */
200dde5845aSJiri Kosina 
201dde5845aSJiri Kosina static int hid_add_usage(struct hid_parser *parser, unsigned usage)
202dde5845aSJiri Kosina {
203dde5845aSJiri Kosina 	if (parser->local.usage_index >= HID_MAX_USAGES) {
2048c3d52fcSJiri Kosina 		hid_err(parser->device, "usage index exceeded\n");
205dde5845aSJiri Kosina 		return -1;
206dde5845aSJiri Kosina 	}
207dde5845aSJiri Kosina 	parser->local.usage[parser->local.usage_index] = usage;
208dde5845aSJiri Kosina 	parser->local.collection_index[parser->local.usage_index] =
209dde5845aSJiri Kosina 		parser->collection_stack_ptr ?
210dde5845aSJiri Kosina 		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
211dde5845aSJiri Kosina 	parser->local.usage_index++;
212dde5845aSJiri Kosina 	return 0;
213dde5845aSJiri Kosina }
214dde5845aSJiri Kosina 
215dde5845aSJiri Kosina /*
216dde5845aSJiri Kosina  * Register a new field for this report.
217dde5845aSJiri Kosina  */
218dde5845aSJiri Kosina 
219dde5845aSJiri Kosina static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
220dde5845aSJiri Kosina {
221dde5845aSJiri Kosina 	struct hid_report *report;
222dde5845aSJiri Kosina 	struct hid_field *field;
223dde5845aSJiri Kosina 	int usages;
224dde5845aSJiri Kosina 	unsigned offset;
225dde5845aSJiri Kosina 	int i;
226dde5845aSJiri Kosina 
227a3789a17SJoe Perches 	report = hid_register_report(parser->device, report_type, parser->global.report_id);
228a3789a17SJoe Perches 	if (!report) {
2298c3d52fcSJiri Kosina 		hid_err(parser->device, "hid_register_report failed\n");
230dde5845aSJiri Kosina 		return -1;
231dde5845aSJiri Kosina 	}
232dde5845aSJiri Kosina 
233dde5845aSJiri Kosina 	if (parser->global.logical_maximum < parser->global.logical_minimum) {
2348c3d52fcSJiri Kosina 		hid_err(parser->device, "logical range invalid %d %d\n",
2358c3d52fcSJiri Kosina 				parser->global.logical_minimum, parser->global.logical_maximum);
236dde5845aSJiri Kosina 		return -1;
237dde5845aSJiri Kosina 	}
238dde5845aSJiri Kosina 
239dde5845aSJiri Kosina 	offset = report->size;
240dde5845aSJiri Kosina 	report->size += parser->global.report_size * parser->global.report_count;
241dde5845aSJiri Kosina 
242dde5845aSJiri Kosina 	if (!parser->local.usage_index) /* Ignore padding fields */
243dde5845aSJiri Kosina 		return 0;
244dde5845aSJiri Kosina 
245dde5845aSJiri Kosina 	usages = max_t(int, parser->local.usage_index, parser->global.report_count);
246dde5845aSJiri Kosina 
247a3789a17SJoe Perches 	field = hid_register_field(report, usages, parser->global.report_count);
248a3789a17SJoe Perches 	if (!field)
249dde5845aSJiri Kosina 		return 0;
250dde5845aSJiri Kosina 
251dde5845aSJiri Kosina 	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
252dde5845aSJiri Kosina 	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
253dde5845aSJiri Kosina 	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
254dde5845aSJiri Kosina 
255dde5845aSJiri Kosina 	for (i = 0; i < usages; i++) {
256dde5845aSJiri Kosina 		int j = i;
257dde5845aSJiri Kosina 		/* Duplicate the last usage we parsed if we have excess values */
258dde5845aSJiri Kosina 		if (i >= parser->local.usage_index)
259dde5845aSJiri Kosina 			j = parser->local.usage_index - 1;
260dde5845aSJiri Kosina 		field->usage[i].hid = parser->local.usage[j];
261dde5845aSJiri Kosina 		field->usage[i].collection_index =
262dde5845aSJiri Kosina 			parser->local.collection_index[j];
263dde5845aSJiri Kosina 	}
264dde5845aSJiri Kosina 
265dde5845aSJiri Kosina 	field->maxusage = usages;
266dde5845aSJiri Kosina 	field->flags = flags;
267dde5845aSJiri Kosina 	field->report_offset = offset;
268dde5845aSJiri Kosina 	field->report_type = report_type;
269dde5845aSJiri Kosina 	field->report_size = parser->global.report_size;
270dde5845aSJiri Kosina 	field->report_count = parser->global.report_count;
271dde5845aSJiri Kosina 	field->logical_minimum = parser->global.logical_minimum;
272dde5845aSJiri Kosina 	field->logical_maximum = parser->global.logical_maximum;
273dde5845aSJiri Kosina 	field->physical_minimum = parser->global.physical_minimum;
274dde5845aSJiri Kosina 	field->physical_maximum = parser->global.physical_maximum;
275dde5845aSJiri Kosina 	field->unit_exponent = parser->global.unit_exponent;
276dde5845aSJiri Kosina 	field->unit = parser->global.unit;
277dde5845aSJiri Kosina 
278dde5845aSJiri Kosina 	return 0;
279dde5845aSJiri Kosina }
280dde5845aSJiri Kosina 
281dde5845aSJiri Kosina /*
282dde5845aSJiri Kosina  * Read data value from item.
283dde5845aSJiri Kosina  */
284dde5845aSJiri Kosina 
285dde5845aSJiri Kosina static u32 item_udata(struct hid_item *item)
286dde5845aSJiri Kosina {
287dde5845aSJiri Kosina 	switch (item->size) {
288dde5845aSJiri Kosina 	case 1: return item->data.u8;
289dde5845aSJiri Kosina 	case 2: return item->data.u16;
290dde5845aSJiri Kosina 	case 4: return item->data.u32;
291dde5845aSJiri Kosina 	}
292dde5845aSJiri Kosina 	return 0;
293dde5845aSJiri Kosina }
294dde5845aSJiri Kosina 
295dde5845aSJiri Kosina static s32 item_sdata(struct hid_item *item)
296dde5845aSJiri Kosina {
297dde5845aSJiri Kosina 	switch (item->size) {
298dde5845aSJiri Kosina 	case 1: return item->data.s8;
299dde5845aSJiri Kosina 	case 2: return item->data.s16;
300dde5845aSJiri Kosina 	case 4: return item->data.s32;
301dde5845aSJiri Kosina 	}
302dde5845aSJiri Kosina 	return 0;
303dde5845aSJiri Kosina }
304dde5845aSJiri Kosina 
305dde5845aSJiri Kosina /*
306dde5845aSJiri Kosina  * Process a global item.
307dde5845aSJiri Kosina  */
308dde5845aSJiri Kosina 
309dde5845aSJiri Kosina static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
310dde5845aSJiri Kosina {
311dde5845aSJiri Kosina 	switch (item->tag) {
312dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PUSH:
313dde5845aSJiri Kosina 
314dde5845aSJiri Kosina 		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
3158c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack overflow\n");
316dde5845aSJiri Kosina 			return -1;
317dde5845aSJiri Kosina 		}
318dde5845aSJiri Kosina 
319dde5845aSJiri Kosina 		memcpy(parser->global_stack + parser->global_stack_ptr++,
320dde5845aSJiri Kosina 			&parser->global, sizeof(struct hid_global));
321dde5845aSJiri Kosina 		return 0;
322dde5845aSJiri Kosina 
323dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_POP:
324dde5845aSJiri Kosina 
325dde5845aSJiri Kosina 		if (!parser->global_stack_ptr) {
3268c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack underflow\n");
327dde5845aSJiri Kosina 			return -1;
328dde5845aSJiri Kosina 		}
329dde5845aSJiri Kosina 
330880d29f1SJiri Slaby 		memcpy(&parser->global, parser->global_stack +
331880d29f1SJiri Slaby 			--parser->global_stack_ptr, sizeof(struct hid_global));
332dde5845aSJiri Kosina 		return 0;
333dde5845aSJiri Kosina 
334dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
335dde5845aSJiri Kosina 		parser->global.usage_page = item_udata(item);
336dde5845aSJiri Kosina 		return 0;
337dde5845aSJiri Kosina 
338dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
339dde5845aSJiri Kosina 		parser->global.logical_minimum = item_sdata(item);
340dde5845aSJiri Kosina 		return 0;
341dde5845aSJiri Kosina 
342dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
343dde5845aSJiri Kosina 		if (parser->global.logical_minimum < 0)
344dde5845aSJiri Kosina 			parser->global.logical_maximum = item_sdata(item);
345dde5845aSJiri Kosina 		else
346dde5845aSJiri Kosina 			parser->global.logical_maximum = item_udata(item);
347dde5845aSJiri Kosina 		return 0;
348dde5845aSJiri Kosina 
349dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
350dde5845aSJiri Kosina 		parser->global.physical_minimum = item_sdata(item);
351dde5845aSJiri Kosina 		return 0;
352dde5845aSJiri Kosina 
353dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
354dde5845aSJiri Kosina 		if (parser->global.physical_minimum < 0)
355dde5845aSJiri Kosina 			parser->global.physical_maximum = item_sdata(item);
356dde5845aSJiri Kosina 		else
357dde5845aSJiri Kosina 			parser->global.physical_maximum = item_udata(item);
358dde5845aSJiri Kosina 		return 0;
359dde5845aSJiri Kosina 
360dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
361dde5845aSJiri Kosina 		parser->global.unit_exponent = item_sdata(item);
362dde5845aSJiri Kosina 		return 0;
363dde5845aSJiri Kosina 
364dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT:
365dde5845aSJiri Kosina 		parser->global.unit = item_udata(item);
366dde5845aSJiri Kosina 		return 0;
367dde5845aSJiri Kosina 
368dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
369880d29f1SJiri Slaby 		parser->global.report_size = item_udata(item);
370e46e927bSChase Douglas 		if (parser->global.report_size > 96) {
3718c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_size %d\n",
372880d29f1SJiri Slaby 					parser->global.report_size);
373dde5845aSJiri Kosina 			return -1;
374dde5845aSJiri Kosina 		}
375dde5845aSJiri Kosina 		return 0;
376dde5845aSJiri Kosina 
377dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
378880d29f1SJiri Slaby 		parser->global.report_count = item_udata(item);
379880d29f1SJiri Slaby 		if (parser->global.report_count > HID_MAX_USAGES) {
3808c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_count %d\n",
381880d29f1SJiri Slaby 					parser->global.report_count);
382dde5845aSJiri Kosina 			return -1;
383dde5845aSJiri Kosina 		}
384dde5845aSJiri Kosina 		return 0;
385dde5845aSJiri Kosina 
386dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
387880d29f1SJiri Slaby 		parser->global.report_id = item_udata(item);
388880d29f1SJiri Slaby 		if (parser->global.report_id == 0) {
3898c3d52fcSJiri Kosina 			hid_err(parser->device, "report_id 0 is invalid\n");
390dde5845aSJiri Kosina 			return -1;
391dde5845aSJiri Kosina 		}
392dde5845aSJiri Kosina 		return 0;
393dde5845aSJiri Kosina 
394dde5845aSJiri Kosina 	default:
3958c3d52fcSJiri Kosina 		hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
396dde5845aSJiri Kosina 		return -1;
397dde5845aSJiri Kosina 	}
398dde5845aSJiri Kosina }
399dde5845aSJiri Kosina 
400dde5845aSJiri Kosina /*
401dde5845aSJiri Kosina  * Process a local item.
402dde5845aSJiri Kosina  */
403dde5845aSJiri Kosina 
404dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
405dde5845aSJiri Kosina {
406dde5845aSJiri Kosina 	__u32 data;
407dde5845aSJiri Kosina 	unsigned n;
408dde5845aSJiri Kosina 
409dde5845aSJiri Kosina 	data = item_udata(item);
410dde5845aSJiri Kosina 
411dde5845aSJiri Kosina 	switch (item->tag) {
412dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_DELIMITER:
413dde5845aSJiri Kosina 
414dde5845aSJiri Kosina 		if (data) {
415dde5845aSJiri Kosina 			/*
416dde5845aSJiri Kosina 			 * We treat items before the first delimiter
417dde5845aSJiri Kosina 			 * as global to all usage sets (branch 0).
418dde5845aSJiri Kosina 			 * In the moment we process only these global
419dde5845aSJiri Kosina 			 * items and the first delimiter set.
420dde5845aSJiri Kosina 			 */
421dde5845aSJiri Kosina 			if (parser->local.delimiter_depth != 0) {
4228c3d52fcSJiri Kosina 				hid_err(parser->device, "nested delimiters\n");
423dde5845aSJiri Kosina 				return -1;
424dde5845aSJiri Kosina 			}
425dde5845aSJiri Kosina 			parser->local.delimiter_depth++;
426dde5845aSJiri Kosina 			parser->local.delimiter_branch++;
427dde5845aSJiri Kosina 		} else {
428dde5845aSJiri Kosina 			if (parser->local.delimiter_depth < 1) {
4298c3d52fcSJiri Kosina 				hid_err(parser->device, "bogus close delimiter\n");
430dde5845aSJiri Kosina 				return -1;
431dde5845aSJiri Kosina 			}
432dde5845aSJiri Kosina 			parser->local.delimiter_depth--;
433dde5845aSJiri Kosina 		}
434dde5845aSJiri Kosina 		return 1;
435dde5845aSJiri Kosina 
436dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE:
437dde5845aSJiri Kosina 
438dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
43958037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
440dde5845aSJiri Kosina 			return 0;
441dde5845aSJiri Kosina 		}
442dde5845aSJiri Kosina 
443dde5845aSJiri Kosina 		if (item->size <= 2)
444dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
445dde5845aSJiri Kosina 
446dde5845aSJiri Kosina 		return hid_add_usage(parser, data);
447dde5845aSJiri Kosina 
448dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
449dde5845aSJiri Kosina 
450dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
45158037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
452dde5845aSJiri Kosina 			return 0;
453dde5845aSJiri Kosina 		}
454dde5845aSJiri Kosina 
455dde5845aSJiri Kosina 		if (item->size <= 2)
456dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
457dde5845aSJiri Kosina 
458dde5845aSJiri Kosina 		parser->local.usage_minimum = data;
459dde5845aSJiri Kosina 		return 0;
460dde5845aSJiri Kosina 
461dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
462dde5845aSJiri Kosina 
463dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
46458037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
465dde5845aSJiri Kosina 			return 0;
466dde5845aSJiri Kosina 		}
467dde5845aSJiri Kosina 
468dde5845aSJiri Kosina 		if (item->size <= 2)
469dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
470dde5845aSJiri Kosina 
471dde5845aSJiri Kosina 		for (n = parser->local.usage_minimum; n <= data; n++)
472dde5845aSJiri Kosina 			if (hid_add_usage(parser, n)) {
47358037eb9SJiri Kosina 				dbg_hid("hid_add_usage failed\n");
474dde5845aSJiri Kosina 				return -1;
475dde5845aSJiri Kosina 			}
476dde5845aSJiri Kosina 		return 0;
477dde5845aSJiri Kosina 
478dde5845aSJiri Kosina 	default:
479dde5845aSJiri Kosina 
48058037eb9SJiri Kosina 		dbg_hid("unknown local item tag 0x%x\n", item->tag);
481dde5845aSJiri Kosina 		return 0;
482dde5845aSJiri Kosina 	}
483dde5845aSJiri Kosina 	return 0;
484dde5845aSJiri Kosina }
485dde5845aSJiri Kosina 
486dde5845aSJiri Kosina /*
487dde5845aSJiri Kosina  * Process a main item.
488dde5845aSJiri Kosina  */
489dde5845aSJiri Kosina 
490dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
491dde5845aSJiri Kosina {
492dde5845aSJiri Kosina 	__u32 data;
493dde5845aSJiri Kosina 	int ret;
494dde5845aSJiri Kosina 
495dde5845aSJiri Kosina 	data = item_udata(item);
496dde5845aSJiri Kosina 
497dde5845aSJiri Kosina 	switch (item->tag) {
498dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
499dde5845aSJiri Kosina 		ret = open_collection(parser, data & 0xff);
500dde5845aSJiri Kosina 		break;
501dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
502dde5845aSJiri Kosina 		ret = close_collection(parser);
503dde5845aSJiri Kosina 		break;
504dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_INPUT:
505dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
506dde5845aSJiri Kosina 		break;
507dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_OUTPUT:
508dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
509dde5845aSJiri Kosina 		break;
510dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_FEATURE:
511dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
512dde5845aSJiri Kosina 		break;
513dde5845aSJiri Kosina 	default:
5148c3d52fcSJiri Kosina 		hid_err(parser->device, "unknown main item tag 0x%x\n", item->tag);
515dde5845aSJiri Kosina 		ret = 0;
516dde5845aSJiri Kosina 	}
517dde5845aSJiri Kosina 
518dde5845aSJiri Kosina 	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
519dde5845aSJiri Kosina 
520dde5845aSJiri Kosina 	return ret;
521dde5845aSJiri Kosina }
522dde5845aSJiri Kosina 
523dde5845aSJiri Kosina /*
524dde5845aSJiri Kosina  * Process a reserved item.
525dde5845aSJiri Kosina  */
526dde5845aSJiri Kosina 
527dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
528dde5845aSJiri Kosina {
52958037eb9SJiri Kosina 	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
530dde5845aSJiri Kosina 	return 0;
531dde5845aSJiri Kosina }
532dde5845aSJiri Kosina 
533dde5845aSJiri Kosina /*
534dde5845aSJiri Kosina  * Free a report and all registered fields. The field->usage and
535dde5845aSJiri Kosina  * field->value table's are allocated behind the field, so we need
536dde5845aSJiri Kosina  * only to free(field) itself.
537dde5845aSJiri Kosina  */
538dde5845aSJiri Kosina 
539dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report)
540dde5845aSJiri Kosina {
541dde5845aSJiri Kosina 	unsigned n;
542dde5845aSJiri Kosina 
543dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
544dde5845aSJiri Kosina 		kfree(report->field[n]);
545dde5845aSJiri Kosina 	kfree(report);
546dde5845aSJiri Kosina }
547dde5845aSJiri Kosina 
548dde5845aSJiri Kosina /*
549dde5845aSJiri Kosina  * Free a device structure, all reports, and all fields.
550dde5845aSJiri Kosina  */
551dde5845aSJiri Kosina 
55285cdaf52SJiri Slaby static void hid_device_release(struct device *dev)
553dde5845aSJiri Kosina {
55485cdaf52SJiri Slaby 	struct hid_device *device = container_of(dev, struct hid_device, dev);
555dde5845aSJiri Kosina 	unsigned i, j;
556dde5845aSJiri Kosina 
557dde5845aSJiri Kosina 	for (i = 0; i < HID_REPORT_TYPES; i++) {
558dde5845aSJiri Kosina 		struct hid_report_enum *report_enum = device->report_enum + i;
559dde5845aSJiri Kosina 
560dde5845aSJiri Kosina 		for (j = 0; j < 256; j++) {
561dde5845aSJiri Kosina 			struct hid_report *report = report_enum->report_id_hash[j];
562dde5845aSJiri Kosina 			if (report)
563dde5845aSJiri Kosina 				hid_free_report(report);
564dde5845aSJiri Kosina 		}
565dde5845aSJiri Kosina 	}
566dde5845aSJiri Kosina 
567dde5845aSJiri Kosina 	kfree(device->rdesc);
568767fe787SJiri Kosina 	kfree(device->collection);
569dde5845aSJiri Kosina 	kfree(device);
570dde5845aSJiri Kosina }
571dde5845aSJiri Kosina 
572dde5845aSJiri Kosina /*
573dde5845aSJiri Kosina  * Fetch a report description item from the data stream. We support long
574dde5845aSJiri Kosina  * items, though they are not used yet.
575dde5845aSJiri Kosina  */
576dde5845aSJiri Kosina 
577dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
578dde5845aSJiri Kosina {
579dde5845aSJiri Kosina 	u8 b;
580dde5845aSJiri Kosina 
581dde5845aSJiri Kosina 	if ((end - start) <= 0)
582dde5845aSJiri Kosina 		return NULL;
583dde5845aSJiri Kosina 
584dde5845aSJiri Kosina 	b = *start++;
585dde5845aSJiri Kosina 
586dde5845aSJiri Kosina 	item->type = (b >> 2) & 3;
587dde5845aSJiri Kosina 	item->tag  = (b >> 4) & 15;
588dde5845aSJiri Kosina 
589dde5845aSJiri Kosina 	if (item->tag == HID_ITEM_TAG_LONG) {
590dde5845aSJiri Kosina 
591dde5845aSJiri Kosina 		item->format = HID_ITEM_FORMAT_LONG;
592dde5845aSJiri Kosina 
593dde5845aSJiri Kosina 		if ((end - start) < 2)
594dde5845aSJiri Kosina 			return NULL;
595dde5845aSJiri Kosina 
596dde5845aSJiri Kosina 		item->size = *start++;
597dde5845aSJiri Kosina 		item->tag  = *start++;
598dde5845aSJiri Kosina 
599dde5845aSJiri Kosina 		if ((end - start) < item->size)
600dde5845aSJiri Kosina 			return NULL;
601dde5845aSJiri Kosina 
602dde5845aSJiri Kosina 		item->data.longdata = start;
603dde5845aSJiri Kosina 		start += item->size;
604dde5845aSJiri Kosina 		return start;
605dde5845aSJiri Kosina 	}
606dde5845aSJiri Kosina 
607dde5845aSJiri Kosina 	item->format = HID_ITEM_FORMAT_SHORT;
608dde5845aSJiri Kosina 	item->size = b & 3;
609dde5845aSJiri Kosina 
610dde5845aSJiri Kosina 	switch (item->size) {
611dde5845aSJiri Kosina 	case 0:
612dde5845aSJiri Kosina 		return start;
613dde5845aSJiri Kosina 
614dde5845aSJiri Kosina 	case 1:
615dde5845aSJiri Kosina 		if ((end - start) < 1)
616dde5845aSJiri Kosina 			return NULL;
617dde5845aSJiri Kosina 		item->data.u8 = *start++;
618dde5845aSJiri Kosina 		return start;
619dde5845aSJiri Kosina 
620dde5845aSJiri Kosina 	case 2:
621dde5845aSJiri Kosina 		if ((end - start) < 2)
622dde5845aSJiri Kosina 			return NULL;
623c105068fSHarvey Harrison 		item->data.u16 = get_unaligned_le16(start);
624dde5845aSJiri Kosina 		start = (__u8 *)((__le16 *)start + 1);
625dde5845aSJiri Kosina 		return start;
626dde5845aSJiri Kosina 
627dde5845aSJiri Kosina 	case 3:
628dde5845aSJiri Kosina 		item->size++;
629dde5845aSJiri Kosina 		if ((end - start) < 4)
630dde5845aSJiri Kosina 			return NULL;
631c105068fSHarvey Harrison 		item->data.u32 = get_unaligned_le32(start);
632dde5845aSJiri Kosina 		start = (__u8 *)((__le32 *)start + 1);
633dde5845aSJiri Kosina 		return start;
634dde5845aSJiri Kosina 	}
635dde5845aSJiri Kosina 
636dde5845aSJiri Kosina 	return NULL;
637dde5845aSJiri Kosina }
638dde5845aSJiri Kosina 
63985cdaf52SJiri Slaby /**
64085cdaf52SJiri Slaby  * hid_parse_report - parse device report
64185cdaf52SJiri Slaby  *
64285cdaf52SJiri Slaby  * @device: hid device
64385cdaf52SJiri Slaby  * @start: report start
64485cdaf52SJiri Slaby  * @size: report size
64585cdaf52SJiri Slaby  *
646dde5845aSJiri Kosina  * Parse a report description into a hid_device structure. Reports are
647dde5845aSJiri Kosina  * enumerated, fields are attached to these reports.
64885cdaf52SJiri Slaby  * 0 returned on success, otherwise nonzero error value.
649dde5845aSJiri Kosina  */
65085cdaf52SJiri Slaby int hid_parse_report(struct hid_device *device, __u8 *start,
65185cdaf52SJiri Slaby 		unsigned size)
652dde5845aSJiri Kosina {
653dde5845aSJiri Kosina 	struct hid_parser *parser;
654dde5845aSJiri Kosina 	struct hid_item item;
655dde5845aSJiri Kosina 	__u8 *end;
65685cdaf52SJiri Slaby 	int ret;
657dde5845aSJiri Kosina 	static int (*dispatch_type[])(struct hid_parser *parser,
658dde5845aSJiri Kosina 				      struct hid_item *item) = {
659dde5845aSJiri Kosina 		hid_parser_main,
660dde5845aSJiri Kosina 		hid_parser_global,
661dde5845aSJiri Kosina 		hid_parser_local,
662dde5845aSJiri Kosina 		hid_parser_reserved
663dde5845aSJiri Kosina 	};
664dde5845aSJiri Kosina 
665c500c971SJiri Slaby 	if (device->driver->report_fixup)
66673e4008dSNikolai Kondrashov 		start = device->driver->report_fixup(device, start, &size);
667c500c971SJiri Slaby 
6681721a238SJulia Lawall 	device->rdesc = kmemdup(start, size, GFP_KERNEL);
66985cdaf52SJiri Slaby 	if (device->rdesc == NULL)
67085cdaf52SJiri Slaby 		return -ENOMEM;
671dde5845aSJiri Kosina 	device->rsize = size;
672dde5845aSJiri Kosina 
673fe258020SJoe Perches 	parser = vzalloc(sizeof(struct hid_parser));
67485cdaf52SJiri Slaby 	if (!parser) {
67585cdaf52SJiri Slaby 		ret = -ENOMEM;
67685cdaf52SJiri Slaby 		goto err;
677dde5845aSJiri Kosina 	}
67885cdaf52SJiri Slaby 
679dde5845aSJiri Kosina 	parser->device = device;
680dde5845aSJiri Kosina 
681dde5845aSJiri Kosina 	end = start + size;
68285cdaf52SJiri Slaby 	ret = -EINVAL;
683dde5845aSJiri Kosina 	while ((start = fetch_item(start, end, &item)) != NULL) {
684dde5845aSJiri Kosina 
685dde5845aSJiri Kosina 		if (item.format != HID_ITEM_FORMAT_SHORT) {
6868c3d52fcSJiri Kosina 			hid_err(device, "unexpected long global item\n");
68785cdaf52SJiri Slaby 			goto err;
688dde5845aSJiri Kosina 		}
689dde5845aSJiri Kosina 
690dde5845aSJiri Kosina 		if (dispatch_type[item.type](parser, &item)) {
6918c3d52fcSJiri Kosina 			hid_err(device, "item %u %u %u %u parsing failed\n",
6924291ee30SJoe Perches 				item.format, (unsigned)item.size,
6934291ee30SJoe Perches 				(unsigned)item.type, (unsigned)item.tag);
69485cdaf52SJiri Slaby 			goto err;
695dde5845aSJiri Kosina 		}
696dde5845aSJiri Kosina 
697dde5845aSJiri Kosina 		if (start == end) {
698dde5845aSJiri Kosina 			if (parser->collection_stack_ptr) {
6998c3d52fcSJiri Kosina 				hid_err(device, "unbalanced collection at end of report description\n");
70085cdaf52SJiri Slaby 				goto err;
701dde5845aSJiri Kosina 			}
702dde5845aSJiri Kosina 			if (parser->local.delimiter_depth) {
7038c3d52fcSJiri Kosina 				hid_err(device, "unbalanced delimiter at end of report description\n");
70485cdaf52SJiri Slaby 				goto err;
705dde5845aSJiri Kosina 			}
70647a80edbSJiri Kosina 			vfree(parser);
70785cdaf52SJiri Slaby 			return 0;
708dde5845aSJiri Kosina 		}
709dde5845aSJiri Kosina 	}
710dde5845aSJiri Kosina 
7118c3d52fcSJiri Kosina 	hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
71285cdaf52SJiri Slaby err:
71347a80edbSJiri Kosina 	vfree(parser);
71485cdaf52SJiri Slaby 	return ret;
715dde5845aSJiri Kosina }
716229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_parse_report);
717dde5845aSJiri Kosina 
718dde5845aSJiri Kosina /*
719dde5845aSJiri Kosina  * Convert a signed n-bit integer to signed 32-bit integer. Common
720dde5845aSJiri Kosina  * cases are done through the compiler, the screwed things has to be
721dde5845aSJiri Kosina  * done by hand.
722dde5845aSJiri Kosina  */
723dde5845aSJiri Kosina 
724dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n)
725dde5845aSJiri Kosina {
726dde5845aSJiri Kosina 	switch (n) {
727dde5845aSJiri Kosina 	case 8:  return ((__s8)value);
728dde5845aSJiri Kosina 	case 16: return ((__s16)value);
729dde5845aSJiri Kosina 	case 32: return ((__s32)value);
730dde5845aSJiri Kosina 	}
731dde5845aSJiri Kosina 	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
732dde5845aSJiri Kosina }
733dde5845aSJiri Kosina 
734dde5845aSJiri Kosina /*
735dde5845aSJiri Kosina  * Convert a signed 32-bit integer to a signed n-bit integer.
736dde5845aSJiri Kosina  */
737dde5845aSJiri Kosina 
738dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n)
739dde5845aSJiri Kosina {
740dde5845aSJiri Kosina 	s32 a = value >> (n - 1);
741dde5845aSJiri Kosina 	if (a && a != -1)
742dde5845aSJiri Kosina 		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
743dde5845aSJiri Kosina 	return value & ((1 << n) - 1);
744dde5845aSJiri Kosina }
745dde5845aSJiri Kosina 
746dde5845aSJiri Kosina /*
747dde5845aSJiri Kosina  * Extract/implement a data field from/to a little endian report (bit array).
748dde5845aSJiri Kosina  *
749dde5845aSJiri Kosina  * Code sort-of follows HID spec:
750dde5845aSJiri Kosina  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
751dde5845aSJiri Kosina  *
752dde5845aSJiri Kosina  * While the USB HID spec allows unlimited length bit fields in "report
753dde5845aSJiri Kosina  * descriptors", most devices never use more than 16 bits.
754dde5845aSJiri Kosina  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
755dde5845aSJiri Kosina  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
756dde5845aSJiri Kosina  */
757dde5845aSJiri Kosina 
75816ee4cc8SJoe Perches static __u32 extract(const struct hid_device *hid, __u8 *report,
7594291ee30SJoe Perches 		     unsigned offset, unsigned n)
760dde5845aSJiri Kosina {
761dde5845aSJiri Kosina 	u64 x;
762dde5845aSJiri Kosina 
763c4124c9bSJiri Kosina 	if (n > 32)
7644291ee30SJoe Perches 		hid_warn(hid, "extract() called with n (%d) > 32! (%s)\n",
765c4124c9bSJiri Kosina 			 n, current->comm);
766dde5845aSJiri Kosina 
767dde5845aSJiri Kosina 	report += offset >> 3;  /* adjust byte index */
768dde5845aSJiri Kosina 	offset &= 7;            /* now only need bit offset into one byte */
769c105068fSHarvey Harrison 	x = get_unaligned_le64(report);
770dde5845aSJiri Kosina 	x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
771dde5845aSJiri Kosina 	return (u32) x;
772dde5845aSJiri Kosina }
773dde5845aSJiri Kosina 
774dde5845aSJiri Kosina /*
775dde5845aSJiri Kosina  * "implement" : set bits in a little endian bit stream.
776dde5845aSJiri Kosina  * Same concepts as "extract" (see comments above).
777dde5845aSJiri Kosina  * The data mangled in the bit stream remains in little endian
778dde5845aSJiri Kosina  * order the whole time. It make more sense to talk about
779dde5845aSJiri Kosina  * endianness of register values by considering a register
780dde5845aSJiri Kosina  * a "cached" copy of the little endiad bit stream.
781dde5845aSJiri Kosina  */
78216ee4cc8SJoe Perches static void implement(const struct hid_device *hid, __u8 *report,
7834291ee30SJoe Perches 		      unsigned offset, unsigned n, __u32 value)
784dde5845aSJiri Kosina {
7856f0168d2SHarvey Harrison 	u64 x;
786dde5845aSJiri Kosina 	u64 m = (1ULL << n) - 1;
787dde5845aSJiri Kosina 
788c4124c9bSJiri Kosina 	if (n > 32)
7894291ee30SJoe Perches 		hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
7904291ee30SJoe Perches 			 __func__, n, current->comm);
791dde5845aSJiri Kosina 
792c4124c9bSJiri Kosina 	if (value > m)
7934291ee30SJoe Perches 		hid_warn(hid, "%s() called with too large value %d! (%s)\n",
7944291ee30SJoe Perches 			 __func__, value, current->comm);
795dde5845aSJiri Kosina 	WARN_ON(value > m);
796dde5845aSJiri Kosina 	value &= m;
797dde5845aSJiri Kosina 
798dde5845aSJiri Kosina 	report += offset >> 3;
799dde5845aSJiri Kosina 	offset &= 7;
800dde5845aSJiri Kosina 
8016f0168d2SHarvey Harrison 	x = get_unaligned_le64(report);
8026f0168d2SHarvey Harrison 	x &= ~(m << offset);
8036f0168d2SHarvey Harrison 	x |= ((u64)value) << offset;
8046f0168d2SHarvey Harrison 	put_unaligned_le64(x, report);
805dde5845aSJiri Kosina }
806dde5845aSJiri Kosina 
807dde5845aSJiri Kosina /*
808dde5845aSJiri Kosina  * Search an array for a value.
809dde5845aSJiri Kosina  */
810dde5845aSJiri Kosina 
81116ee4cc8SJoe Perches static int search(__s32 *array, __s32 value, unsigned n)
812dde5845aSJiri Kosina {
813dde5845aSJiri Kosina 	while (n--) {
814dde5845aSJiri Kosina 		if (*array++ == value)
815dde5845aSJiri Kosina 			return 0;
816dde5845aSJiri Kosina 	}
817dde5845aSJiri Kosina 	return -1;
818dde5845aSJiri Kosina }
819dde5845aSJiri Kosina 
82085cdaf52SJiri Slaby /**
82185cdaf52SJiri Slaby  * hid_match_report - check if driver's raw_event should be called
82285cdaf52SJiri Slaby  *
82385cdaf52SJiri Slaby  * @hid: hid device
82485cdaf52SJiri Slaby  * @report_type: type to match against
82585cdaf52SJiri Slaby  *
82685cdaf52SJiri Slaby  * compare hid->driver->report_table->report_type to report->type
82785cdaf52SJiri Slaby  */
82885cdaf52SJiri Slaby static int hid_match_report(struct hid_device *hid, struct hid_report *report)
829dde5845aSJiri Kosina {
83085cdaf52SJiri Slaby 	const struct hid_report_id *id = hid->driver->report_table;
83185cdaf52SJiri Slaby 
83285cdaf52SJiri Slaby 	if (!id) /* NULL means all */
83385cdaf52SJiri Slaby 		return 1;
83485cdaf52SJiri Slaby 
83585cdaf52SJiri Slaby 	for (; id->report_type != HID_TERMINATOR; id++)
83685cdaf52SJiri Slaby 		if (id->report_type == HID_ANY_ID ||
83785cdaf52SJiri Slaby 				id->report_type == report->type)
83885cdaf52SJiri Slaby 			return 1;
83985cdaf52SJiri Slaby 	return 0;
84085cdaf52SJiri Slaby }
84185cdaf52SJiri Slaby 
84285cdaf52SJiri Slaby /**
84385cdaf52SJiri Slaby  * hid_match_usage - check if driver's event should be called
84485cdaf52SJiri Slaby  *
84585cdaf52SJiri Slaby  * @hid: hid device
84685cdaf52SJiri Slaby  * @usage: usage to match against
84785cdaf52SJiri Slaby  *
84885cdaf52SJiri Slaby  * compare hid->driver->usage_table->usage_{type,code} to
84985cdaf52SJiri Slaby  * usage->usage_{type,code}
85085cdaf52SJiri Slaby  */
85185cdaf52SJiri Slaby static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
85285cdaf52SJiri Slaby {
85385cdaf52SJiri Slaby 	const struct hid_usage_id *id = hid->driver->usage_table;
85485cdaf52SJiri Slaby 
85585cdaf52SJiri Slaby 	if (!id) /* NULL means all */
85685cdaf52SJiri Slaby 		return 1;
85785cdaf52SJiri Slaby 
85885cdaf52SJiri Slaby 	for (; id->usage_type != HID_ANY_ID - 1; id++)
85985cdaf52SJiri Slaby 		if ((id->usage_hid == HID_ANY_ID ||
86085cdaf52SJiri Slaby 				id->usage_hid == usage->hid) &&
86185cdaf52SJiri Slaby 				(id->usage_type == HID_ANY_ID ||
86285cdaf52SJiri Slaby 				id->usage_type == usage->type) &&
86385cdaf52SJiri Slaby 				(id->usage_code == HID_ANY_ID ||
86485cdaf52SJiri Slaby 				 id->usage_code == usage->code))
86585cdaf52SJiri Slaby 			return 1;
86685cdaf52SJiri Slaby 	return 0;
86785cdaf52SJiri Slaby }
86885cdaf52SJiri Slaby 
86985cdaf52SJiri Slaby static void hid_process_event(struct hid_device *hid, struct hid_field *field,
87085cdaf52SJiri Slaby 		struct hid_usage *usage, __s32 value, int interrupt)
87185cdaf52SJiri Slaby {
87285cdaf52SJiri Slaby 	struct hid_driver *hdrv = hid->driver;
87385cdaf52SJiri Slaby 	int ret;
87485cdaf52SJiri Slaby 
875cd667ce2SJiri Kosina 	hid_dump_input(hid, usage, value);
87685cdaf52SJiri Slaby 
87785cdaf52SJiri Slaby 	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
87885cdaf52SJiri Slaby 		ret = hdrv->event(hid, field, usage, value);
87985cdaf52SJiri Slaby 		if (ret != 0) {
88085cdaf52SJiri Slaby 			if (ret < 0)
8818c3d52fcSJiri Kosina 				hid_err(hid, "%s's event failed with %d\n",
88285cdaf52SJiri Slaby 						hdrv->name, ret);
88385cdaf52SJiri Slaby 			return;
88485cdaf52SJiri Slaby 		}
88585cdaf52SJiri Slaby 	}
88685cdaf52SJiri Slaby 
887dde5845aSJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
888dde5845aSJiri Kosina 		hidinput_hid_event(hid, field, usage, value);
889aa938f79SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
890aa938f79SJiri Kosina 		hid->hiddev_hid_event(hid, field, usage, value);
891dde5845aSJiri Kosina }
892dde5845aSJiri Kosina 
893dde5845aSJiri Kosina /*
894dde5845aSJiri Kosina  * Analyse a received field, and fetch the data from it. The field
895dde5845aSJiri Kosina  * content is stored for next report processing (we do differential
896dde5845aSJiri Kosina  * reporting to the layer).
897dde5845aSJiri Kosina  */
898dde5845aSJiri Kosina 
899abdff0f7SAdrian Bunk static void hid_input_field(struct hid_device *hid, struct hid_field *field,
900abdff0f7SAdrian Bunk 			    __u8 *data, int interrupt)
901dde5845aSJiri Kosina {
902dde5845aSJiri Kosina 	unsigned n;
903dde5845aSJiri Kosina 	unsigned count = field->report_count;
904dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
905dde5845aSJiri Kosina 	unsigned size = field->report_size;
906dde5845aSJiri Kosina 	__s32 min = field->logical_minimum;
907dde5845aSJiri Kosina 	__s32 max = field->logical_maximum;
908dde5845aSJiri Kosina 	__s32 *value;
909dde5845aSJiri Kosina 
910a3789a17SJoe Perches 	value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC);
911a3789a17SJoe Perches 	if (!value)
912dde5845aSJiri Kosina 		return;
913dde5845aSJiri Kosina 
914dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
915dde5845aSJiri Kosina 
9164291ee30SJoe Perches 		value[n] = min < 0 ?
9174291ee30SJoe Perches 			snto32(extract(hid, data, offset + n * size, size),
9184291ee30SJoe Perches 			       size) :
9194291ee30SJoe Perches 			extract(hid, data, offset + n * size, size);
920dde5845aSJiri Kosina 
9214291ee30SJoe Perches 		/* Ignore report if ErrorRollOver */
9224291ee30SJoe Perches 		if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
9234291ee30SJoe Perches 		    value[n] >= min && value[n] <= max &&
9244291ee30SJoe Perches 		    field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
925dde5845aSJiri Kosina 			goto exit;
926dde5845aSJiri Kosina 	}
927dde5845aSJiri Kosina 
928dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
929dde5845aSJiri Kosina 
930dde5845aSJiri Kosina 		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
931dde5845aSJiri Kosina 			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
932dde5845aSJiri Kosina 			continue;
933dde5845aSJiri Kosina 		}
934dde5845aSJiri Kosina 
935dde5845aSJiri Kosina 		if (field->value[n] >= min && field->value[n] <= max
936dde5845aSJiri Kosina 			&& field->usage[field->value[n] - min].hid
937dde5845aSJiri Kosina 			&& search(value, field->value[n], count))
938dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
939dde5845aSJiri Kosina 
940dde5845aSJiri Kosina 		if (value[n] >= min && value[n] <= max
941dde5845aSJiri Kosina 			&& field->usage[value[n] - min].hid
942dde5845aSJiri Kosina 			&& search(field->value, value[n], count))
943dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
944dde5845aSJiri Kosina 	}
945dde5845aSJiri Kosina 
946dde5845aSJiri Kosina 	memcpy(field->value, value, count * sizeof(__s32));
947dde5845aSJiri Kosina exit:
948dde5845aSJiri Kosina 	kfree(value);
949dde5845aSJiri Kosina }
950dde5845aSJiri Kosina 
951dde5845aSJiri Kosina /*
952dde5845aSJiri Kosina  * Output the field into the report.
953dde5845aSJiri Kosina  */
954dde5845aSJiri Kosina 
9554291ee30SJoe Perches static void hid_output_field(const struct hid_device *hid,
9564291ee30SJoe Perches 			     struct hid_field *field, __u8 *data)
957dde5845aSJiri Kosina {
958dde5845aSJiri Kosina 	unsigned count = field->report_count;
959dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
960dde5845aSJiri Kosina 	unsigned size = field->report_size;
961dde5845aSJiri Kosina 	unsigned n;
962dde5845aSJiri Kosina 
963dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
964dde5845aSJiri Kosina 		if (field->logical_minimum < 0)	/* signed values */
9654291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
9664291ee30SJoe Perches 				  s32ton(field->value[n], size));
967dde5845aSJiri Kosina 		else				/* unsigned values */
9684291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
9694291ee30SJoe Perches 				  field->value[n]);
970dde5845aSJiri Kosina 	}
971dde5845aSJiri Kosina }
972dde5845aSJiri Kosina 
973dde5845aSJiri Kosina /*
974dde5845aSJiri Kosina  * Create a report.
975dde5845aSJiri Kosina  */
976dde5845aSJiri Kosina 
977229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data)
978dde5845aSJiri Kosina {
979dde5845aSJiri Kosina 	unsigned n;
980dde5845aSJiri Kosina 
981dde5845aSJiri Kosina 	if (report->id > 0)
982dde5845aSJiri Kosina 		*data++ = report->id;
983dde5845aSJiri Kosina 
98475c28df8SPete Zaitcev 	memset(data, 0, ((report->size - 1) >> 3) + 1);
985dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
9864291ee30SJoe Perches 		hid_output_field(report->device, report->field[n], data);
987dde5845aSJiri Kosina }
988229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report);
989dde5845aSJiri Kosina 
990dde5845aSJiri Kosina /*
991dde5845aSJiri Kosina  * Set a field value. The report this field belongs to has to be
992dde5845aSJiri Kosina  * created and transferred to the device, to set this value in the
993dde5845aSJiri Kosina  * device.
994dde5845aSJiri Kosina  */
995dde5845aSJiri Kosina 
996dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
997dde5845aSJiri Kosina {
998dde5845aSJiri Kosina 	unsigned size = field->report_size;
999dde5845aSJiri Kosina 
1000cd667ce2SJiri Kosina 	hid_dump_input(field->report->device, field->usage + offset, value);
1001dde5845aSJiri Kosina 
1002dde5845aSJiri Kosina 	if (offset >= field->report_count) {
10038c3d52fcSJiri Kosina 		hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
10048c3d52fcSJiri Kosina 				offset, field->report_count);
1005dde5845aSJiri Kosina 		return -1;
1006dde5845aSJiri Kosina 	}
1007dde5845aSJiri Kosina 	if (field->logical_minimum < 0) {
1008dde5845aSJiri Kosina 		if (value != snto32(s32ton(value, size), size)) {
10098c3d52fcSJiri Kosina 			hid_err(field->report->device, "value %d is out of range\n", value);
1010dde5845aSJiri Kosina 			return -1;
1011dde5845aSJiri Kosina 		}
1012dde5845aSJiri Kosina 	}
1013dde5845aSJiri Kosina 	field->value[offset] = value;
1014dde5845aSJiri Kosina 	return 0;
1015dde5845aSJiri Kosina }
1016229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field);
1017dde5845aSJiri Kosina 
101885cdaf52SJiri Slaby static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
101985cdaf52SJiri Slaby 		const u8 *data)
102085cdaf52SJiri Slaby {
102185cdaf52SJiri Slaby 	struct hid_report *report;
102285cdaf52SJiri Slaby 	unsigned int n = 0;	/* Normally report number is 0 */
102385cdaf52SJiri Slaby 
102485cdaf52SJiri Slaby 	/* Device uses numbered reports, data[0] is report number */
102585cdaf52SJiri Slaby 	if (report_enum->numbered)
102685cdaf52SJiri Slaby 		n = *data;
102785cdaf52SJiri Slaby 
102885cdaf52SJiri Slaby 	report = report_enum->report_id_hash[n];
102985cdaf52SJiri Slaby 	if (report == NULL)
103085cdaf52SJiri Slaby 		dbg_hid("undefined report_id %u received\n", n);
103185cdaf52SJiri Slaby 
103285cdaf52SJiri Slaby 	return report;
103385cdaf52SJiri Slaby }
103485cdaf52SJiri Slaby 
103585cdaf52SJiri Slaby void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
103685cdaf52SJiri Slaby 		int interrupt)
1037aa8de2f0SJiri Kosina {
1038aa8de2f0SJiri Kosina 	struct hid_report_enum *report_enum = hid->report_enum + type;
1039aa8de2f0SJiri Kosina 	struct hid_report *report;
104085cdaf52SJiri Slaby 	unsigned int a;
104185cdaf52SJiri Slaby 	int rsize, csize = size;
104285cdaf52SJiri Slaby 	u8 *cdata = data;
1043aa8de2f0SJiri Kosina 
104485cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
104585cdaf52SJiri Slaby 	if (!report)
104685cdaf52SJiri Slaby 		return;
1047aa8de2f0SJiri Kosina 
104885cdaf52SJiri Slaby 	if (report_enum->numbered) {
104985cdaf52SJiri Slaby 		cdata++;
105085cdaf52SJiri Slaby 		csize--;
1051aa8de2f0SJiri Kosina 	}
1052aa8de2f0SJiri Kosina 
1053aa8de2f0SJiri Kosina 	rsize = ((report->size - 1) >> 3) + 1;
1054aa8de2f0SJiri Kosina 
1055966922f2SArmando Visconti 	if (rsize > HID_MAX_BUFFER_SIZE)
1056966922f2SArmando Visconti 		rsize = HID_MAX_BUFFER_SIZE;
1057966922f2SArmando Visconti 
105885cdaf52SJiri Slaby 	if (csize < rsize) {
105985cdaf52SJiri Slaby 		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
106085cdaf52SJiri Slaby 				csize, rsize);
106185cdaf52SJiri Slaby 		memset(cdata + csize, 0, rsize - csize);
1062aa8de2f0SJiri Kosina 	}
1063aa8de2f0SJiri Kosina 
1064aa8de2f0SJiri Kosina 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1065aa8de2f0SJiri Kosina 		hid->hiddev_report_event(hid, report);
10665a38f2c7SAlan Ott 	if (hid->claimed & HID_CLAIMED_HIDRAW)
106786166b7bSJiri Kosina 		hidraw_report_event(hid, data, size);
1068aa8de2f0SJiri Kosina 
106985cdaf52SJiri Slaby 	for (a = 0; a < report->maxfield; a++)
107085cdaf52SJiri Slaby 		hid_input_field(hid, report->field[a], cdata, interrupt);
1071aa8de2f0SJiri Kosina 
1072aa8de2f0SJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1073aa8de2f0SJiri Kosina 		hidinput_report_event(hid, report);
107485cdaf52SJiri Slaby }
107585cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_report_raw_event);
107685cdaf52SJiri Slaby 
107785cdaf52SJiri Slaby /**
107885cdaf52SJiri Slaby  * hid_input_report - report data from lower layer (usb, bt...)
107985cdaf52SJiri Slaby  *
108085cdaf52SJiri Slaby  * @hid: hid device
108185cdaf52SJiri Slaby  * @type: HID report type (HID_*_REPORT)
108285cdaf52SJiri Slaby  * @data: report contents
108385cdaf52SJiri Slaby  * @size: size of data parameter
1084ff9b00a2SJiri Kosina  * @interrupt: distinguish between interrupt and control transfers
108585cdaf52SJiri Slaby  *
108685cdaf52SJiri Slaby  * This is data entry for lower layers.
108785cdaf52SJiri Slaby  */
108885cdaf52SJiri Slaby int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
108985cdaf52SJiri Slaby {
109076c317d6SJulia Lawall 	struct hid_report_enum *report_enum;
109176c317d6SJulia Lawall 	struct hid_driver *hdrv;
109285cdaf52SJiri Slaby 	struct hid_report *report;
1093cd667ce2SJiri Kosina 	char *buf;
109485cdaf52SJiri Slaby 	unsigned int i;
109545dc1ac7SJiri Kosina 	int ret = 0;
109685cdaf52SJiri Slaby 
10974ea54542SDavid Herrmann 	if (!hid)
109885cdaf52SJiri Slaby 		return -ENODEV;
10994ea54542SDavid Herrmann 
11004ea54542SDavid Herrmann 	if (down_trylock(&hid->driver_lock))
11014ea54542SDavid Herrmann 		return -EBUSY;
11024ea54542SDavid Herrmann 
11034ea54542SDavid Herrmann 	if (!hid->driver) {
11044ea54542SDavid Herrmann 		ret = -ENODEV;
11054ea54542SDavid Herrmann 		goto unlock;
11064ea54542SDavid Herrmann 	}
110776c317d6SJulia Lawall 	report_enum = hid->report_enum + type;
110876c317d6SJulia Lawall 	hdrv = hid->driver;
110985cdaf52SJiri Slaby 
111085cdaf52SJiri Slaby 	if (!size) {
111185cdaf52SJiri Slaby 		dbg_hid("empty report\n");
11124ea54542SDavid Herrmann 		ret = -1;
11134ea54542SDavid Herrmann 		goto unlock;
111485cdaf52SJiri Slaby 	}
111585cdaf52SJiri Slaby 
1116d1ff6522SJiri Kosina 	buf = kmalloc(sizeof(char) * HID_DEBUG_BUFSIZE, GFP_ATOMIC);
1117cd667ce2SJiri Kosina 
1118f77e347bSJiri Kosina 	if (!buf)
1119cd667ce2SJiri Kosina 		goto nomem;
112085cdaf52SJiri Slaby 
112185cdaf52SJiri Slaby 	/* dump the report */
1122cd667ce2SJiri Kosina 	snprintf(buf, HID_DEBUG_BUFSIZE - 1,
11231caea61eSJiri Kosina 			"\nreport (size %u) (%snumbered) = ", size, report_enum->numbered ? "" : "un");
1124cd667ce2SJiri Kosina 	hid_debug_event(hid, buf);
11251caea61eSJiri Kosina 
1126cd667ce2SJiri Kosina 	for (i = 0; i < size; i++) {
1127cd667ce2SJiri Kosina 		snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1128cd667ce2SJiri Kosina 				" %02x", data[i]);
1129cd667ce2SJiri Kosina 		hid_debug_event(hid, buf);
1130cd667ce2SJiri Kosina 	}
1131cd667ce2SJiri Kosina 	hid_debug_event(hid, "\n");
1132cd667ce2SJiri Kosina 	kfree(buf);
1133cd667ce2SJiri Kosina 
1134cd667ce2SJiri Kosina nomem:
1135f77e347bSJiri Kosina 	report = hid_get_report(report_enum, data);
1136f77e347bSJiri Kosina 
11374ea54542SDavid Herrmann 	if (!report) {
11384ea54542SDavid Herrmann 		ret = -1;
11394ea54542SDavid Herrmann 		goto unlock;
11404ea54542SDavid Herrmann 	}
1141f77e347bSJiri Kosina 
114285cdaf52SJiri Slaby 	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
114385cdaf52SJiri Slaby 		ret = hdrv->raw_event(hid, report, data, size);
11444ea54542SDavid Herrmann 		if (ret != 0) {
11454ea54542SDavid Herrmann 			ret = ret < 0 ? ret : 0;
11464ea54542SDavid Herrmann 			goto unlock;
11474ea54542SDavid Herrmann 		}
114885cdaf52SJiri Slaby 	}
114985cdaf52SJiri Slaby 
115085cdaf52SJiri Slaby 	hid_report_raw_event(hid, type, data, size, interrupt);
1151aa8de2f0SJiri Kosina 
11524ea54542SDavid Herrmann unlock:
11534ea54542SDavid Herrmann 	up(&hid->driver_lock);
115445dc1ac7SJiri Kosina 	return ret;
1155aa8de2f0SJiri Kosina }
1156aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report);
1157aa8de2f0SJiri Kosina 
11580f37cd03SJiri Kosina static bool hid_match_one_id(struct hid_device *hdev,
11590f37cd03SJiri Kosina 		const struct hid_device_id *id)
11600f37cd03SJiri Kosina {
11610f37cd03SJiri Kosina 	return id->bus == hdev->bus &&
11620f37cd03SJiri Kosina 		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
11630f37cd03SJiri Kosina 		(id->product == HID_ANY_ID || id->product == hdev->product);
11640f37cd03SJiri Kosina }
11650f37cd03SJiri Kosina 
1166bbc21cfdSJeremy Fitzhardinge const struct hid_device_id *hid_match_id(struct hid_device *hdev,
11670f37cd03SJiri Kosina 		const struct hid_device_id *id)
11680f37cd03SJiri Kosina {
11690f37cd03SJiri Kosina 	for (; id->bus; id++)
11700f37cd03SJiri Kosina 		if (hid_match_one_id(hdev, id))
11710f37cd03SJiri Kosina 			return id;
11720f37cd03SJiri Kosina 
11730f37cd03SJiri Kosina 	return NULL;
11740f37cd03SJiri Kosina }
11750f37cd03SJiri Kosina 
11760f37cd03SJiri Kosina static const struct hid_device_id hid_hiddev_list[] = {
1177c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1178c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
11790f37cd03SJiri Kosina 	{ }
11800f37cd03SJiri Kosina };
11810f37cd03SJiri Kosina 
11820f37cd03SJiri Kosina static bool hid_hiddev(struct hid_device *hdev)
11830f37cd03SJiri Kosina {
11840f37cd03SJiri Kosina 	return !!hid_match_id(hdev, hid_hiddev_list);
11850f37cd03SJiri Kosina }
11860f37cd03SJiri Kosina 
11876d3bfb74SAlan Ott 
11886d3bfb74SAlan Ott static ssize_t
11896d3bfb74SAlan Ott read_report_descriptor(struct file *filp, struct kobject *kobj,
11906d3bfb74SAlan Ott 		struct bin_attribute *attr,
11916d3bfb74SAlan Ott 		char *buf, loff_t off, size_t count)
11926d3bfb74SAlan Ott {
11936d3bfb74SAlan Ott 	struct device *dev = container_of(kobj, struct device, kobj);
11946d3bfb74SAlan Ott 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
11956d3bfb74SAlan Ott 
11966d3bfb74SAlan Ott 	if (off >= hdev->rsize)
11976d3bfb74SAlan Ott 		return 0;
11986d3bfb74SAlan Ott 
11996d3bfb74SAlan Ott 	if (off + count > hdev->rsize)
12006d3bfb74SAlan Ott 		count = hdev->rsize - off;
12016d3bfb74SAlan Ott 
12026d3bfb74SAlan Ott 	memcpy(buf, hdev->rdesc + off, count);
12036d3bfb74SAlan Ott 
12046d3bfb74SAlan Ott 	return count;
12056d3bfb74SAlan Ott }
12066d3bfb74SAlan Ott 
12076d3bfb74SAlan Ott static struct bin_attribute dev_bin_attr_report_desc = {
12086d3bfb74SAlan Ott 	.attr = { .name = "report_descriptor", .mode = 0444 },
12096d3bfb74SAlan Ott 	.read = read_report_descriptor,
12106d3bfb74SAlan Ott 	.size = HID_MAX_DESCRIPTOR_SIZE,
12116d3bfb74SAlan Ott };
12126d3bfb74SAlan Ott 
121393c10132SJiri Slaby int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
121493c10132SJiri Slaby {
121593c10132SJiri Slaby 	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
121693c10132SJiri Slaby 		"Joystick", "Gamepad", "Keyboard", "Keypad",
121793c10132SJiri Slaby 		"Multi-Axis Controller"
121893c10132SJiri Slaby 	};
121993c10132SJiri Slaby 	const char *type, *bus;
122093c10132SJiri Slaby 	char buf[64];
122193c10132SJiri Slaby 	unsigned int i;
122293c10132SJiri Slaby 	int len;
12236d3bfb74SAlan Ott 	int ret;
122493c10132SJiri Slaby 
1225b5e5a37eSBastien Nocera 	if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1226b5e5a37eSBastien Nocera 		connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
12273a343ee4SDaniel Mack 	if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
12283a343ee4SDaniel Mack 		connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
122993c10132SJiri Slaby 	if (hdev->bus != BUS_USB)
123093c10132SJiri Slaby 		connect_mask &= ~HID_CONNECT_HIDDEV;
12310f37cd03SJiri Kosina 	if (hid_hiddev(hdev))
12320f37cd03SJiri Kosina 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
123393c10132SJiri Slaby 
123493c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
123593c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
123693c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_INPUT;
1237b77c3920SBenjamin Tissoires 	if (hdev->quirks & HID_QUIRK_MULTITOUCH) {
1238b77c3920SBenjamin Tissoires 		/* this device should be handled by hid-multitouch, skip it */
1239b77c3920SBenjamin Tissoires 		return -ENODEV;
1240b77c3920SBenjamin Tissoires 	}
1241b77c3920SBenjamin Tissoires 
124293c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
124393c10132SJiri Slaby 			!hdev->hiddev_connect(hdev,
124493c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDDEV_FORCE))
124593c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDDEV;
124693c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
124793c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDRAW;
124893c10132SJiri Slaby 
124993c10132SJiri Slaby 	if (!hdev->claimed) {
12504291ee30SJoe Perches 		hid_err(hdev, "claimed by neither input, hiddev nor hidraw\n");
125193c10132SJiri Slaby 		return -ENODEV;
125293c10132SJiri Slaby 	}
125393c10132SJiri Slaby 
125493c10132SJiri Slaby 	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
125593c10132SJiri Slaby 			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
125693c10132SJiri Slaby 		hdev->ff_init(hdev);
125793c10132SJiri Slaby 
125893c10132SJiri Slaby 	len = 0;
125993c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_INPUT)
126093c10132SJiri Slaby 		len += sprintf(buf + len, "input");
126193c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
126293c10132SJiri Slaby 		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
126393c10132SJiri Slaby 				hdev->minor);
126493c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
126593c10132SJiri Slaby 		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
126693c10132SJiri Slaby 				((struct hidraw *)hdev->hidraw)->minor);
126793c10132SJiri Slaby 
126893c10132SJiri Slaby 	type = "Device";
126993c10132SJiri Slaby 	for (i = 0; i < hdev->maxcollection; i++) {
127093c10132SJiri Slaby 		struct hid_collection *col = &hdev->collection[i];
127193c10132SJiri Slaby 		if (col->type == HID_COLLECTION_APPLICATION &&
127293c10132SJiri Slaby 		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
127393c10132SJiri Slaby 		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
127493c10132SJiri Slaby 			type = types[col->usage & 0xffff];
127593c10132SJiri Slaby 			break;
127693c10132SJiri Slaby 		}
127793c10132SJiri Slaby 	}
127893c10132SJiri Slaby 
127993c10132SJiri Slaby 	switch (hdev->bus) {
128093c10132SJiri Slaby 	case BUS_USB:
128193c10132SJiri Slaby 		bus = "USB";
128293c10132SJiri Slaby 		break;
128393c10132SJiri Slaby 	case BUS_BLUETOOTH:
128493c10132SJiri Slaby 		bus = "BLUETOOTH";
128593c10132SJiri Slaby 		break;
128693c10132SJiri Slaby 	default:
128793c10132SJiri Slaby 		bus = "<UNKNOWN>";
128893c10132SJiri Slaby 	}
128993c10132SJiri Slaby 
12906d3bfb74SAlan Ott 	ret = device_create_bin_file(&hdev->dev, &dev_bin_attr_report_desc);
12916d3bfb74SAlan Ott 	if (ret)
12926d3bfb74SAlan Ott 		hid_warn(hdev,
12936d3bfb74SAlan Ott 			 "can't create sysfs report descriptor attribute err: %d\n", ret);
12946d3bfb74SAlan Ott 
12954291ee30SJoe Perches 	hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
129693c10132SJiri Slaby 		 buf, bus, hdev->version >> 8, hdev->version & 0xff,
129793c10132SJiri Slaby 		 type, hdev->name, hdev->phys);
129893c10132SJiri Slaby 
129993c10132SJiri Slaby 	return 0;
130093c10132SJiri Slaby }
130193c10132SJiri Slaby EXPORT_SYMBOL_GPL(hid_connect);
130293c10132SJiri Slaby 
1303c4c259bcSJiri Kosina void hid_disconnect(struct hid_device *hdev)
1304c4c259bcSJiri Kosina {
13056d3bfb74SAlan Ott 	device_remove_bin_file(&hdev->dev, &dev_bin_attr_report_desc);
1306c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_INPUT)
1307c4c259bcSJiri Kosina 		hidinput_disconnect(hdev);
1308c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
1309c4c259bcSJiri Kosina 		hdev->hiddev_disconnect(hdev);
1310c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
1311c4c259bcSJiri Kosina 		hidraw_disconnect(hdev);
1312c4c259bcSJiri Kosina }
1313c4c259bcSJiri Kosina EXPORT_SYMBOL_GPL(hid_disconnect);
1314c4c259bcSJiri Kosina 
1315bae7eb33SJiri Kosina /* a list of devices for which there is a specialized driver on HID bus */
1316ce06b9d6SDmitry Torokhov static const struct hid_device_id hid_have_special_driver[] = {
1317b6353f4fSStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
13186dec143aSStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M2256) },
131914a21cd4SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
132014a21cd4SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1321b7e1b203SLech Perczak 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649) },
1322c0dbcc33SSergei Kolzun 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802) },
1323e6aac342SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR, USB_DEVICE_ID_ACTIONSTAR_1011) },
1324df9bcaceSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
13258c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
13268c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1327128537ceSMichael Poole 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE) },
1328a462230eSChase Douglas 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICTRACKPAD) },
13298c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
13308c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
13318c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
13328c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
13338c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
13348c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
13358c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
13368c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
13378c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
13388c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
13398c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1340fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1341fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1342fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
13438c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
13448c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
13458c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
13468c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
13478c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
13488c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1349ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1350ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1351ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
13528c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
13538c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
13548c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
13558c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
13568c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
13578c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1358a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1359a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1360a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
136199b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
136299b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
136399b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
136499b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
136599b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
136699b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
136747340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI) },
136847340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO) },
136947340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS) },
1370213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI) },
1371213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO) },
1372213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS) },
13734a4c8799SDan Bastone 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI) },
13744a4c8799SDan Bastone 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO) },
13754a4c8799SDan Bastone 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_JIS) },
1376f6f554f0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI) },
1377f6f554f0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO) },
1378f6f554f0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS) },
1379d762cc29SNobuhiro Iwamatsu 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI) },
1380d762cc29SNobuhiro Iwamatsu 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO) },
1381d762cc29SNobuhiro Iwamatsu 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS) },
138223aeb61eSChristian Schuerer-Waldheim 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI) },
138323aeb61eSChristian Schuerer-Waldheim 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO) },
138423aeb61eSChristian Schuerer-Waldheim 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS) },
1385ad734bc1SAndreas Krist 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO) },
13868c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
13878c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1388eaca1386SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_T91MT) },
1389cec15a0eSRoland Baum 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
1390*212da74dSJosenivaldo Benito Junior 	{ HID_USB_DEVICE(USB_VENDOR_ID_AUREAL, USB_DEVICE_ID_AUREAL_W01RN) },
1391b5635b12SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
1392bf280628SWayne Thomas 	{ HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE) },
1393c3dc66deSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE_2) },
13945519cab4SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
139536213e1eSStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
1396bc5ab083SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
1397ba4d8abbSStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
1398272036edSFrançois Jaouen 	{ HID_USB_DEVICE(USB_VENDOR_ID_CANDO, USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
13993b239cd7SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
14001ce31b25SRaphaël Doursenaud 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION_SOLAR) },
1401fcfacfd3SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1402b9e4b1e0SDennis Kügler 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS) },
14033596bb92SKeng-Yu Lin 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS2) },
1404942fd422SAustin Zhang 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT, USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
14053a370ca1SDon Prince 	{ HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_PRODIKEYS_PCMIDI) },
140679603dc9SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH, USB_DEVICE_ID_CVTOUCH_SCREEN) },
14070f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
14080f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1409e8d0eab4SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) },
14100f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
1411a3b5e577SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
14123f866fbdSRichard Walmsley 	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
1413e05eefb9SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011) },
1414e36f690bSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
1415e36f690bSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
1416e36f690bSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
1417fd1d1525SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
1418fd1d1525SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
1419e36f690bSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
1420e36f690bSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
1421bb9ff210SMarek Vasut 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
1422e36f690bSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_DWAV, USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
142364b386eaSRichard Nauber 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
1424c04abeefSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_ELO, USB_DEVICE_ID_ELO_TS2515) },
142504561c5aSIgnaz Forster 	{ HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
14261f243e30SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
14274d5df5d1SAndreas Nielsen 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_FRUCTEL, USB_DEVICE_ID_GAMETEL_MT_MODE) },
14285181e594SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1429578f3a35SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
14305572da08SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
1431ee0fbd14SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH, USB_DEVICE_ID_GOODTOUCH_000f) },
14325181e594SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
143342859e0bSLukasz Lubojanski 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1434949f8fefSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
14351e093206SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1436c2fd1a4eSCory Maccarrone 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
14375519cab4SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_HANVON, USB_DEVICE_ID_HANVON_MULTITOUCH) },
143854580365SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT, USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
1439a062cc5aSStephane Chatty  	{ HID_USB_DEVICE(USB_VENDOR_ID_IDEACOM, USB_DEVICE_ID_IDEACOM_IDC6650) },
1440d946e65eSAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
14414e61f0d7SAustin Zhang 	{ HID_USB_DEVICE(USB_VENDOR_ID_ILITEK, USB_DEVICE_ID_ILITEK_MULTITOUCH) },
1442b2e7617eSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS, USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
1443fdf93aa3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
1444177900e8SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KEYTOUCH, USB_DEVICE_ID_KEYTOUCH_IEC) },
144579422741SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
144622ca20b2SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_EASYPEN_I405X) },
144722ca20b2SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_MOUSEPEN_I608X) },
144822ca20b2SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_EASYPEN_M610X) },
1449b5635b12SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
145075b07022SChris Schlund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LCPOWER, USB_DEVICE_ID_LCPOWER_LC1000 ) },
1451c50bb1a4SJeff Brown 	{ HID_USB_DEVICE(USB_VENDOR_ID_LG, USB_DEVICE_ID_LG_MULTITOUCH) },
14525f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
14535f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
14545f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
14555f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
14565f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
14575f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
14585f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
14595f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
14605f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
14615f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
14625f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
14632c6118e4SHendrik Iben 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD_CORD) },
1464606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1465606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1466606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1467fd30ea8cSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
1468606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
146974f292caSGary Stein 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FLIGHT_SYSTEM_G940) },
1470606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1471606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
14725623a24aSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL) },
1473e00ddc9bSMichal Malý 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFGT_WHEEL) },
1474243b706dSChristophe Borivant 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
1475fdc6807fSPeter Gundermann 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL) },
14760944e964SKonstantin Khlebnikov #if IS_ENABLED(CONFIG_HID_LOGITECH_DJ)
1477534a7b8eSNestor Lopez Casado 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
1478534a7b8eSNestor Lopez Casado 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
14790944e964SKonstantin Khlebnikov #endif
148032c88cbcSSimon Wood 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
1481606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
148224985cf6SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
148324985cf6SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) },
1484df167c4aSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_LUMIO, USB_DEVICE_ID_CRYSTALTOUCH) },
1485c3ead6deSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_LUMIO, USB_DEVICE_ID_CRYSTALTOUCH_DUAL) },
1486236db47cSBruno Prémont 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
1487236db47cSBruno Prémont 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
1488b580169aSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500) },
148978a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
149078a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
149178a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
149278a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1493f3b83d71SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K) },
149478a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
14953b8006e5SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
149694011f93SRafi Rubin 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
14976e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1) },
14986e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2) },
14996e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3) },
15006e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4) },
15016e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5) },
15026e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6) },
15036e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7) },
15046e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8) },
15056e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9) },
15066e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10) },
15076e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11) },
15086e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12) },
15096e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13) },
15106e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14) },
15116e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15) },
15126e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16) },
15136e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17) },
15146e32819eSmicki 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18) },
1515270fdc07SHerton Ronaldo Krzesinski 	{ HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_PKB1700) },
1516cd9ec30dSJohnathon Harris 	{ HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_WKB2000) },
15172258e863SDenis Kovalev 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANASONIC, USB_DEVICE_ID_PANABOARD_UBT780) },
15182258e863SDenis Kovalev 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANASONIC, USB_DEVICE_ID_PANABOARD_UBT880) },
15196ab3a9a6SJohn Sung 	{ HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT, USB_DEVICE_ID_PENMOUNT_PCI) },
15201e762532SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1521b7ea95ffSAaron Tian 	{ HID_USB_DEVICE(USB_VENDOR_ID_PIXART, USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
1522b7ea95ffSAaron Tian 	{ HID_USB_DEVICE(USB_VENDOR_ID_PIXART, USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
1523b7ea95ffSAaron Tian 	{ HID_USB_DEVICE(USB_VENDOR_ID_PIXART, USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
1524f6a04605STerry Lambert 	{ HID_USB_DEVICE(USB_VENDOR_ID_PRIMAX, USB_DEVICE_ID_PRIMAX_KEYBOARD) },
152549e4739aSStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) },
1526f54405dbSAlex Neblett 	{ HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, USB_DEVICE_ID_PIXART_IMAGING_INC_OPTICAL_TOUCH_SCREEN) },
152714bf62cdSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
1528e68cc603SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
1529d41c2a70SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
153047dbdbffSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
15310e70f97fSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) },
1532cb7cf3daSStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
15333fce2246SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
15341e93674aSAndreas Hübner 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_PS1000) },
1535980a3da6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1536b355850bSDon Prince 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_WIRELESS_KBD_MOUSE) },
1537d586dca0SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_SKYCABLE, USB_DEVICE_ID_SKYCABLE_WIRELESS_PRESENTER) },
1538bd28ce00SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
153935dca5b4SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER) },
1540f9ce7c28SBastien Nocera 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1541cc6e0bbbSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
1542d3fb5454SStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, USB_DEVICE_ID_MTP) },
1543b6dc7992SPascal Auriel - Stantum 	{ HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM, USB_DEVICE_ID_MTP_STM) },
1544b6dc7992SPascal Auriel - Stantum 	{ HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX, USB_DEVICE_ID_MTP_SITRONIX) },
154590231e7eSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1546daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1547daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
15487a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
15497a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
1550daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
15513ee8f0a2SMarkus Rathgeb 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653) },
1552daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1553d65c3768SSimon Wood 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65a) },
1554740363fbSJiri Kosina 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_TIVO, USB_DEVICE_ID_TIVO_SLIDE_BT) },
155544ea35c1SJarod Wilson 	{ HID_USB_DEVICE(USB_VENDOR_ID_TIVO, USB_DEVICE_ID_TIVO_SLIDE) },
1556f14f526dSLev Babiev 	{ HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
155754001081SKees Bakker 	{ HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED2, USB_DEVICE_ID_TOPSEED2_RF_COMBO) },
15585e74e56dSBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL, USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
1559711a680eSBruno Premont 	{ HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
1560c64f6f93SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
156141fa9230SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
1562f8a489ccSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
1563f8a489ccSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
1564f8a489ccSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
15656be914f1SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
1566617b64f9SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_UNITEC, USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
1567617b64f9SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_UNITEC, USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
1568fac733f0SJussi Kivilinna 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
156942fc04e5SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SUPER_JOY_BOX_3) },
1570ad395ccaSSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD) },
15711bcc2067SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_3_PRO) },
15721bcc2067SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_DUAL_BOX_PRO) },
15731bcc2067SSean Young 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_5_PRO) },
1574ca2dcd40SBastien Nocera 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
157578761ff9SPrzemo Firszt 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
157672a46344SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_5_8_INCH) },
157700e7f964SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_12_1_INCH) },
15784fdc18d1SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_Q_PAD) },
1579a786e83cSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_PID_0038) },
158072a46344SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_10_6_INCH) },
15818f1acc32SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_14_1_INCH) },
1582bc8a2a9bSice chien 	{ HID_USB_DEVICE(USB_VENDOR_ID_XAT, USB_DEVICE_ID_XAT_CSR) },
158311576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_SPX) },
158411576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_MPX) },
158511576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_CSR) },
158611576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_SPX1) },
158711576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_MPX1) },
158811576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_CSR1) },
158911576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_SPX2) },
159011576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_MPX2) },
159111576c61SMasatoshi Hoshikawa 	{ HID_USB_DEVICE(USB_VENDOR_ID_XIROKU, USB_DEVICE_ID_XIROKU_CSR2) },
159274bc6953SStefan Kriwanek 	{ HID_USB_DEVICE(USB_VENDOR_ID_X_TENSIONS, USB_DEVICE_ID_SPEEDLINK_VAD_CEZANNE) },
1593daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1594daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1595a9885c8fSDon Prince 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZYDACRON, USB_DEVICE_ID_ZYDACRON_REMOTE_CONTROL) },
15968c19a515SJiri Slaby 
159778a849a6SJiri Slaby 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
159802fb72a0SDavid Herrmann 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_WIIMOTE) },
159985cdaf52SJiri Slaby 	{ }
160085cdaf52SJiri Slaby };
160185cdaf52SJiri Slaby 
16023a6f82f7SJiri Slaby struct hid_dynid {
16033a6f82f7SJiri Slaby 	struct list_head list;
16043a6f82f7SJiri Slaby 	struct hid_device_id id;
16053a6f82f7SJiri Slaby };
16063a6f82f7SJiri Slaby 
16073a6f82f7SJiri Slaby /**
16083a6f82f7SJiri Slaby  * store_new_id - add a new HID device ID to this driver and re-probe devices
16093a6f82f7SJiri Slaby  * @driver: target device driver
16103a6f82f7SJiri Slaby  * @buf: buffer for scanning device ID data
16113a6f82f7SJiri Slaby  * @count: input size
16123a6f82f7SJiri Slaby  *
16133a6f82f7SJiri Slaby  * Adds a new dynamic hid device ID to this driver,
16143a6f82f7SJiri Slaby  * and causes the driver to probe for all devices again.
16153a6f82f7SJiri Slaby  */
16163a6f82f7SJiri Slaby static ssize_t store_new_id(struct device_driver *drv, const char *buf,
16173a6f82f7SJiri Slaby 		size_t count)
16183a6f82f7SJiri Slaby {
16193a6f82f7SJiri Slaby 	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
16203a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
16213a6f82f7SJiri Slaby 	__u32 bus, vendor, product;
16223a6f82f7SJiri Slaby 	unsigned long driver_data = 0;
16233a6f82f7SJiri Slaby 	int ret;
16243a6f82f7SJiri Slaby 
16253a6f82f7SJiri Slaby 	ret = sscanf(buf, "%x %x %x %lx",
16263a6f82f7SJiri Slaby 			&bus, &vendor, &product, &driver_data);
16273a6f82f7SJiri Slaby 	if (ret < 3)
16283a6f82f7SJiri Slaby 		return -EINVAL;
16293a6f82f7SJiri Slaby 
16303a6f82f7SJiri Slaby 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
16313a6f82f7SJiri Slaby 	if (!dynid)
16323a6f82f7SJiri Slaby 		return -ENOMEM;
16333a6f82f7SJiri Slaby 
16343a6f82f7SJiri Slaby 	dynid->id.bus = bus;
16353a6f82f7SJiri Slaby 	dynid->id.vendor = vendor;
16363a6f82f7SJiri Slaby 	dynid->id.product = product;
16373a6f82f7SJiri Slaby 	dynid->id.driver_data = driver_data;
16383a6f82f7SJiri Slaby 
16393a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
16403a6f82f7SJiri Slaby 	list_add_tail(&dynid->list, &hdrv->dyn_list);
16413a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
16423a6f82f7SJiri Slaby 
16433a6f82f7SJiri Slaby 	ret = driver_attach(&hdrv->driver);
16443a6f82f7SJiri Slaby 
16453a6f82f7SJiri Slaby 	return ret ? : count;
16463a6f82f7SJiri Slaby }
16473a6f82f7SJiri Slaby static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
16483a6f82f7SJiri Slaby 
16493a6f82f7SJiri Slaby static void hid_free_dynids(struct hid_driver *hdrv)
16503a6f82f7SJiri Slaby {
16513a6f82f7SJiri Slaby 	struct hid_dynid *dynid, *n;
16523a6f82f7SJiri Slaby 
16533a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
16543a6f82f7SJiri Slaby 	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
16553a6f82f7SJiri Slaby 		list_del(&dynid->list);
16563a6f82f7SJiri Slaby 		kfree(dynid);
16573a6f82f7SJiri Slaby 	}
16583a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
16593a6f82f7SJiri Slaby }
16603a6f82f7SJiri Slaby 
16613a6f82f7SJiri Slaby static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
16623a6f82f7SJiri Slaby 		struct hid_driver *hdrv)
16633a6f82f7SJiri Slaby {
16643a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
16653a6f82f7SJiri Slaby 
16663a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
16673a6f82f7SJiri Slaby 	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
16683a6f82f7SJiri Slaby 		if (hid_match_one_id(hdev, &dynid->id)) {
16693a6f82f7SJiri Slaby 			spin_unlock(&hdrv->dyn_lock);
16703a6f82f7SJiri Slaby 			return &dynid->id;
16713a6f82f7SJiri Slaby 		}
16723a6f82f7SJiri Slaby 	}
16733a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
16743a6f82f7SJiri Slaby 
16753a6f82f7SJiri Slaby 	return hid_match_id(hdev, hdrv->id_table);
16763a6f82f7SJiri Slaby }
16773a6f82f7SJiri Slaby 
167885cdaf52SJiri Slaby static int hid_bus_match(struct device *dev, struct device_driver *drv)
167985cdaf52SJiri Slaby {
168085cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
168185cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
168285cdaf52SJiri Slaby 
16838d179a9eSBenjamin Tissoires 	if ((hdev->quirks & HID_QUIRK_MULTITOUCH) &&
16848d179a9eSBenjamin Tissoires 		!strncmp(hdrv->name, "hid-multitouch", 14))
16858d179a9eSBenjamin Tissoires 		return 1;
16868d179a9eSBenjamin Tissoires 
16873a6f82f7SJiri Slaby 	if (!hid_match_device(hdev, hdrv))
168885cdaf52SJiri Slaby 		return 0;
168985cdaf52SJiri Slaby 
1690ce06b9d6SDmitry Torokhov 	/* generic wants all that don't have specialized driver */
16916b1968d5SJiri Kosina 	if (!strncmp(hdrv->name, "generic-", 8) && !hid_ignore_special_drivers)
1692ce06b9d6SDmitry Torokhov 		return !hid_match_id(hdev, hid_have_special_driver);
169385cdaf52SJiri Slaby 
169485cdaf52SJiri Slaby 	return 1;
169585cdaf52SJiri Slaby }
169685cdaf52SJiri Slaby 
169785cdaf52SJiri Slaby static int hid_device_probe(struct device *dev)
169885cdaf52SJiri Slaby {
169985cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(dev->driver,
170085cdaf52SJiri Slaby 			struct hid_driver, driver);
170185cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
170285cdaf52SJiri Slaby 	const struct hid_device_id *id;
170385cdaf52SJiri Slaby 	int ret = 0;
170485cdaf52SJiri Slaby 
17054ea54542SDavid Herrmann 	if (down_interruptible(&hdev->driver_lock))
17064ea54542SDavid Herrmann 		return -EINTR;
17074ea54542SDavid Herrmann 
170885cdaf52SJiri Slaby 	if (!hdev->driver) {
17093a6f82f7SJiri Slaby 		id = hid_match_device(hdev, hdrv);
1710ba623a77SDan Carpenter 		if (id == NULL) {
17118d179a9eSBenjamin Tissoires 			if (!((hdev->quirks & HID_QUIRK_MULTITOUCH) &&
17128d179a9eSBenjamin Tissoires 				!strncmp(hdrv->name, "hid-multitouch", 14))) {
1713ba623a77SDan Carpenter 				ret = -ENODEV;
1714ba623a77SDan Carpenter 				goto unlock;
1715ba623a77SDan Carpenter 			}
17168d179a9eSBenjamin Tissoires 		}
1717c500c971SJiri Slaby 
171885cdaf52SJiri Slaby 		hdev->driver = hdrv;
1719c500c971SJiri Slaby 		if (hdrv->probe) {
1720c500c971SJiri Slaby 			ret = hdrv->probe(hdev, id);
1721c500c971SJiri Slaby 		} else { /* default probe */
1722c500c971SJiri Slaby 			ret = hid_parse(hdev);
1723c500c971SJiri Slaby 			if (!ret)
172493c10132SJiri Slaby 				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1725c500c971SJiri Slaby 		}
1726c500c971SJiri Slaby 		if (ret)
1727c500c971SJiri Slaby 			hdev->driver = NULL;
172885cdaf52SJiri Slaby 	}
1729ba623a77SDan Carpenter unlock:
17304ea54542SDavid Herrmann 	up(&hdev->driver_lock);
173185cdaf52SJiri Slaby 	return ret;
173285cdaf52SJiri Slaby }
173385cdaf52SJiri Slaby 
173485cdaf52SJiri Slaby static int hid_device_remove(struct device *dev)
173585cdaf52SJiri Slaby {
173685cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
17374ea54542SDavid Herrmann 	struct hid_driver *hdrv;
173885cdaf52SJiri Slaby 
17394ea54542SDavid Herrmann 	if (down_interruptible(&hdev->driver_lock))
17404ea54542SDavid Herrmann 		return -EINTR;
17414ea54542SDavid Herrmann 
17424ea54542SDavid Herrmann 	hdrv = hdev->driver;
174385cdaf52SJiri Slaby 	if (hdrv) {
174485cdaf52SJiri Slaby 		if (hdrv->remove)
174585cdaf52SJiri Slaby 			hdrv->remove(hdev);
1746c500c971SJiri Slaby 		else /* default remove */
1747c500c971SJiri Slaby 			hid_hw_stop(hdev);
174885cdaf52SJiri Slaby 		hdev->driver = NULL;
174985cdaf52SJiri Slaby 	}
175085cdaf52SJiri Slaby 
17514ea54542SDavid Herrmann 	up(&hdev->driver_lock);
175285cdaf52SJiri Slaby 	return 0;
175385cdaf52SJiri Slaby }
175485cdaf52SJiri Slaby 
175585cdaf52SJiri Slaby static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
175685cdaf52SJiri Slaby {
175785cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
175885cdaf52SJiri Slaby 
175985cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
176085cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
176185cdaf52SJiri Slaby 		return -ENOMEM;
176285cdaf52SJiri Slaby 
176385cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
176485cdaf52SJiri Slaby 		return -ENOMEM;
176585cdaf52SJiri Slaby 
176685cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
176785cdaf52SJiri Slaby 		return -ENOMEM;
176885cdaf52SJiri Slaby 
176985cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
177085cdaf52SJiri Slaby 		return -ENOMEM;
177185cdaf52SJiri Slaby 
177285cdaf52SJiri Slaby 	if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
177385cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
177485cdaf52SJiri Slaby 		return -ENOMEM;
177585cdaf52SJiri Slaby 
177685cdaf52SJiri Slaby 	return 0;
177785cdaf52SJiri Slaby }
177885cdaf52SJiri Slaby 
177985cdaf52SJiri Slaby static struct bus_type hid_bus_type = {
178085cdaf52SJiri Slaby 	.name		= "hid",
178185cdaf52SJiri Slaby 	.match		= hid_bus_match,
178285cdaf52SJiri Slaby 	.probe		= hid_device_probe,
178385cdaf52SJiri Slaby 	.remove		= hid_device_remove,
178485cdaf52SJiri Slaby 	.uevent		= hid_uevent,
178585cdaf52SJiri Slaby };
178685cdaf52SJiri Slaby 
1787bae7eb33SJiri Kosina /* a list of devices that shouldn't be handled by HID core at all */
1788d458a9dfSJiri Slaby static const struct hid_device_id hid_ignore_list[] = {
1789d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1790d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1791d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1792d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1793d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1794d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1795d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1796d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1797d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1798d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1799d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1800d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
180177f720b7SStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM)},
180277f720b7SStephane Chatty 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM2)},
180362a56582SAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1804d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1805d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1806d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1807d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1808d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1809d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
18105f6108cfSAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
1811d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1812d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
181373bc7d31SMelchior FRANZ 	{ HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, 0x0004) },
1814789aaa2eSDan Delaney 	{ HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, 0x000a) },
1815d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
181670c66567SPetr Štetiar 	{ HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC5UH) },
1817df506f2cSPetr Štetiar 	{ HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC4UM) },
1818b1807719SBenjamin Tissoires 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1819d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1820d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1821d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1822d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1823d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1824d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1825d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1826d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1827d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1828d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1829d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1830d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1831d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1832d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1833d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1834d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1835d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1836d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1837d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1838d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1839d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1840d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1841d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1842d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1843d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1844d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1845d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1846d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1847d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1848d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1849d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1850d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1851d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1852d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1853d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1854d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1855d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1856d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1857d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1858d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1859d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1860d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1861d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1862d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1863d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1864d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1865d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1866d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1867d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1868d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1869d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1870d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1871d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1872d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1873d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1874d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1875d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1876d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1877d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1878d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1879d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1880d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1881d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1882d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1883c91c21c5SAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
1884d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1885eb8141ccSLamarque Vieira Souza 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_KYE, 0x0058) },
1886d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1887ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) },
1888d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1889ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) },
1890d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1891ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) },
1892ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) },
1893ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) },
1894ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) },
1895ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) },
1896ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) },
1897d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1898d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1899d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1900ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) },
1901ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) },
1902ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) },
1903d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1904d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1905ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) },
1906d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1907d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1908d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1909d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1910d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1911ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) },
1912ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) },
1913ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) },
1914ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) },
1915ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) },
1916ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) },
1917ce97cac8SMichael Hund 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) },
1918d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1919d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1920d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1921d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1922d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1923d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1924d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1925d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1926d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1927d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1928d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1929d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1930d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1931d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1932d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1933d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1934d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1935d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1936d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
19374cfae3e8SHenning Glawe 	{ HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
193835cfd1d9SMichael Tokarev 	{ HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
1939d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1940d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1941d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1942d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1943d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1944d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1945d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1946d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1947d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1948d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1949d458a9dfSJiri Slaby 	{ }
1950d458a9dfSJiri Slaby };
1951d458a9dfSJiri Slaby 
1952b4d8e473SJiri Slaby /**
1953b4d8e473SJiri Slaby  * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1954b4d8e473SJiri Slaby  *
1955b4d8e473SJiri Slaby  * There are composite devices for which we want to ignore only a certain
1956b4d8e473SJiri Slaby  * interface. This is a list of devices for which only the mouse interface will
1957b4d8e473SJiri Slaby  * be ignored. This allows a dedicated driver to take care of the interface.
1958b4d8e473SJiri Slaby  */
1959b4d8e473SJiri Slaby static const struct hid_device_id hid_mouse_ignore_list[] = {
1960b4d8e473SJiri Slaby 	/* appletouch driver */
1961b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1962b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1963b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1964b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1965b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1966b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1967b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1968b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1969b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1970b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1971b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1972b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1973b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1974b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1975b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1976b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1977b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1978b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1979b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1980b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1981ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1982ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1983ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
198499b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
198599b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
198699b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
198799b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
198899b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
198999b9f758SEdgar (gimli) Hucek 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
199047340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI) },
199147340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO) },
199247340bd9SAndy Botting 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS) },
1993213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI) },
1994213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO) },
1995213f9da8SGökçen Eraslan 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS) },
19964b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI) },
19974b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO) },
19984b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS) },
19994b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI) },
20004b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO) },
20014b086910SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS) },
2002b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
2003b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
2004b4d8e473SJiri Slaby 	{ }
2005b4d8e473SJiri Slaby };
2006b4d8e473SJiri Slaby 
2007d458a9dfSJiri Slaby static bool hid_ignore(struct hid_device *hdev)
2008d458a9dfSJiri Slaby {
2009d458a9dfSJiri Slaby 	switch (hdev->vendor) {
2010d458a9dfSJiri Slaby 	case USB_VENDOR_ID_CODEMERCS:
2011d458a9dfSJiri Slaby 		/* ignore all Code Mercenaries IOWarrior devices */
2012d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
2013d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
2014d458a9dfSJiri Slaby 			return true;
2015d458a9dfSJiri Slaby 		break;
2016d458a9dfSJiri Slaby 	case USB_VENDOR_ID_LOGITECH:
2017d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
2018d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
2019d458a9dfSJiri Slaby 			return true;
2020d458a9dfSJiri Slaby 		break;
202131f7fd79SJarod Wilson 	case USB_VENDOR_ID_SOUNDGRAPH:
202231f7fd79SJarod Wilson 		if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
202331f7fd79SJarod Wilson 		    hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
202431f7fd79SJarod Wilson 			return true;
202531f7fd79SJarod Wilson 		break;
2026bba5394aSXing Wei 	case USB_VENDOR_ID_HANWANG:
2027bba5394aSXing Wei 		if (hdev->product >= USB_DEVICE_ID_HANWANG_TABLET_FIRST &&
2028bba5394aSXing Wei 		    hdev->product <= USB_DEVICE_ID_HANWANG_TABLET_LAST)
2029bba5394aSXing Wei 			return true;
2030bba5394aSXing Wei 		break;
20316dc1418eSTomoki Sekiyama 	case USB_VENDOR_ID_JESS:
20326dc1418eSTomoki Sekiyama 		if (hdev->product == USB_DEVICE_ID_JESS_YUREX &&
20336dc1418eSTomoki Sekiyama 				hdev->type == HID_TYPE_USBNONE)
20346dc1418eSTomoki Sekiyama 			return true;
20356dc1418eSTomoki Sekiyama 	break;
2036d458a9dfSJiri Slaby 	}
2037d458a9dfSJiri Slaby 
2038b4d8e473SJiri Slaby 	if (hdev->type == HID_TYPE_USBMOUSE &&
2039b4d8e473SJiri Slaby 			hid_match_id(hdev, hid_mouse_ignore_list))
2040b4d8e473SJiri Slaby 		return true;
2041b4d8e473SJiri Slaby 
2042d458a9dfSJiri Slaby 	return !!hid_match_id(hdev, hid_ignore_list);
2043d458a9dfSJiri Slaby }
2044d458a9dfSJiri Slaby 
204585cdaf52SJiri Slaby int hid_add_device(struct hid_device *hdev)
204685cdaf52SJiri Slaby {
204785cdaf52SJiri Slaby 	static atomic_t id = ATOMIC_INIT(0);
204885cdaf52SJiri Slaby 	int ret;
204985cdaf52SJiri Slaby 
205085cdaf52SJiri Slaby 	if (WARN_ON(hdev->status & HID_STAT_ADDED))
205185cdaf52SJiri Slaby 		return -EBUSY;
205285cdaf52SJiri Slaby 
2053d458a9dfSJiri Slaby 	/* we need to kill them here, otherwise they will stay allocated to
2054d458a9dfSJiri Slaby 	 * wait for coming driver */
2055e5a2a04cSJindrich Makovicka 	if (!(hdev->quirks & HID_QUIRK_NO_IGNORE)
2056e5a2a04cSJindrich Makovicka             && (hid_ignore(hdev) || (hdev->quirks & HID_QUIRK_IGNORE)))
2057d458a9dfSJiri Slaby 		return -ENODEV;
2058d458a9dfSJiri Slaby 
20596bbe586fSKay Sievers 	/* XXX hack, any other cleaner solution after the driver core
20606bbe586fSKay Sievers 	 * is converted to allow more than 20 bytes as the device name? */
20616bbe586fSKay Sievers 	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
206285cdaf52SJiri Slaby 		     hdev->vendor, hdev->product, atomic_inc_return(&id));
206385cdaf52SJiri Slaby 
20644da361b6SBruno Prémont 	hid_debug_register(hdev, dev_name(&hdev->dev));
206585cdaf52SJiri Slaby 	ret = device_add(&hdev->dev);
206685cdaf52SJiri Slaby 	if (!ret)
206785cdaf52SJiri Slaby 		hdev->status |= HID_STAT_ADDED;
20684da361b6SBruno Prémont 	else
20694da361b6SBruno Prémont 		hid_debug_unregister(hdev);
2070a635f9ddSJiri Kosina 
207185cdaf52SJiri Slaby 	return ret;
207285cdaf52SJiri Slaby }
207385cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_add_device);
207485cdaf52SJiri Slaby 
207585cdaf52SJiri Slaby /**
207685cdaf52SJiri Slaby  * hid_allocate_device - allocate new hid device descriptor
207785cdaf52SJiri Slaby  *
207885cdaf52SJiri Slaby  * Allocate and initialize hid device, so that hid_destroy_device might be
207985cdaf52SJiri Slaby  * used to free it.
208085cdaf52SJiri Slaby  *
208185cdaf52SJiri Slaby  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
208285cdaf52SJiri Slaby  * error value.
208385cdaf52SJiri Slaby  */
208485cdaf52SJiri Slaby struct hid_device *hid_allocate_device(void)
208585cdaf52SJiri Slaby {
208685cdaf52SJiri Slaby 	struct hid_device *hdev;
208785cdaf52SJiri Slaby 	unsigned int i;
208885cdaf52SJiri Slaby 	int ret = -ENOMEM;
208985cdaf52SJiri Slaby 
209085cdaf52SJiri Slaby 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
209185cdaf52SJiri Slaby 	if (hdev == NULL)
209285cdaf52SJiri Slaby 		return ERR_PTR(ret);
209385cdaf52SJiri Slaby 
209485cdaf52SJiri Slaby 	device_initialize(&hdev->dev);
209585cdaf52SJiri Slaby 	hdev->dev.release = hid_device_release;
209685cdaf52SJiri Slaby 	hdev->dev.bus = &hid_bus_type;
209785cdaf52SJiri Slaby 
209885cdaf52SJiri Slaby 	hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
209985cdaf52SJiri Slaby 			sizeof(struct hid_collection), GFP_KERNEL);
210085cdaf52SJiri Slaby 	if (hdev->collection == NULL)
210185cdaf52SJiri Slaby 		goto err;
210285cdaf52SJiri Slaby 	hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
210385cdaf52SJiri Slaby 
210485cdaf52SJiri Slaby 	for (i = 0; i < HID_REPORT_TYPES; i++)
210585cdaf52SJiri Slaby 		INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
210685cdaf52SJiri Slaby 
2107cd667ce2SJiri Kosina 	init_waitqueue_head(&hdev->debug_wait);
2108cd667ce2SJiri Kosina 	INIT_LIST_HEAD(&hdev->debug_list);
21094ea54542SDavid Herrmann 	sema_init(&hdev->driver_lock, 1);
2110cd667ce2SJiri Kosina 
211185cdaf52SJiri Slaby 	return hdev;
211285cdaf52SJiri Slaby err:
211385cdaf52SJiri Slaby 	put_device(&hdev->dev);
211485cdaf52SJiri Slaby 	return ERR_PTR(ret);
211585cdaf52SJiri Slaby }
211685cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_allocate_device);
211785cdaf52SJiri Slaby 
211885cdaf52SJiri Slaby static void hid_remove_device(struct hid_device *hdev)
211985cdaf52SJiri Slaby {
212085cdaf52SJiri Slaby 	if (hdev->status & HID_STAT_ADDED) {
212185cdaf52SJiri Slaby 		device_del(&hdev->dev);
2122a635f9ddSJiri Kosina 		hid_debug_unregister(hdev);
212385cdaf52SJiri Slaby 		hdev->status &= ~HID_STAT_ADDED;
212485cdaf52SJiri Slaby 	}
212585cdaf52SJiri Slaby }
212685cdaf52SJiri Slaby 
212785cdaf52SJiri Slaby /**
212885cdaf52SJiri Slaby  * hid_destroy_device - free previously allocated device
212985cdaf52SJiri Slaby  *
213085cdaf52SJiri Slaby  * @hdev: hid device
213185cdaf52SJiri Slaby  *
213285cdaf52SJiri Slaby  * If you allocate hid_device through hid_allocate_device, you should ever
213385cdaf52SJiri Slaby  * free by this function.
213485cdaf52SJiri Slaby  */
213585cdaf52SJiri Slaby void hid_destroy_device(struct hid_device *hdev)
213685cdaf52SJiri Slaby {
213785cdaf52SJiri Slaby 	hid_remove_device(hdev);
213885cdaf52SJiri Slaby 	put_device(&hdev->dev);
213985cdaf52SJiri Slaby }
214085cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_destroy_device);
214185cdaf52SJiri Slaby 
214285cdaf52SJiri Slaby int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
214385cdaf52SJiri Slaby 		const char *mod_name)
214485cdaf52SJiri Slaby {
21453a6f82f7SJiri Slaby 	int ret;
21463a6f82f7SJiri Slaby 
214785cdaf52SJiri Slaby 	hdrv->driver.name = hdrv->name;
214885cdaf52SJiri Slaby 	hdrv->driver.bus = &hid_bus_type;
214985cdaf52SJiri Slaby 	hdrv->driver.owner = owner;
215085cdaf52SJiri Slaby 	hdrv->driver.mod_name = mod_name;
215185cdaf52SJiri Slaby 
21523a6f82f7SJiri Slaby 	INIT_LIST_HEAD(&hdrv->dyn_list);
21533a6f82f7SJiri Slaby 	spin_lock_init(&hdrv->dyn_lock);
21543a6f82f7SJiri Slaby 
21553a6f82f7SJiri Slaby 	ret = driver_register(&hdrv->driver);
21563a6f82f7SJiri Slaby 	if (ret)
21573a6f82f7SJiri Slaby 		return ret;
21583a6f82f7SJiri Slaby 
21593a6f82f7SJiri Slaby 	ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
21603a6f82f7SJiri Slaby 	if (ret)
21613a6f82f7SJiri Slaby 		driver_unregister(&hdrv->driver);
21623a6f82f7SJiri Slaby 
21633a6f82f7SJiri Slaby 	return ret;
216485cdaf52SJiri Slaby }
216585cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(__hid_register_driver);
216685cdaf52SJiri Slaby 
216785cdaf52SJiri Slaby void hid_unregister_driver(struct hid_driver *hdrv)
216885cdaf52SJiri Slaby {
21693a6f82f7SJiri Slaby 	driver_remove_file(&hdrv->driver, &driver_attr_new_id);
217085cdaf52SJiri Slaby 	driver_unregister(&hdrv->driver);
21713a6f82f7SJiri Slaby 	hid_free_dynids(hdrv);
217285cdaf52SJiri Slaby }
217385cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_unregister_driver);
217485cdaf52SJiri Slaby 
21750361a28dSOliver Neukum int hid_check_keys_pressed(struct hid_device *hid)
21760361a28dSOliver Neukum {
21770361a28dSOliver Neukum 	struct hid_input *hidinput;
21780361a28dSOliver Neukum 	int i;
21790361a28dSOliver Neukum 
2180e5288eb5SJiri Kosina 	if (!(hid->claimed & HID_CLAIMED_INPUT))
2181e5288eb5SJiri Kosina 		return 0;
2182e5288eb5SJiri Kosina 
21830361a28dSOliver Neukum 	list_for_each_entry(hidinput, &hid->inputs, list) {
21840361a28dSOliver Neukum 		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
21850361a28dSOliver Neukum 			if (hidinput->input->key[i])
21860361a28dSOliver Neukum 				return 1;
21870361a28dSOliver Neukum 	}
21880361a28dSOliver Neukum 
21890361a28dSOliver Neukum 	return 0;
21900361a28dSOliver Neukum }
21910361a28dSOliver Neukum 
21920361a28dSOliver Neukum EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
21930361a28dSOliver Neukum 
219486166b7bSJiri Kosina static int __init hid_init(void)
219586166b7bSJiri Kosina {
219685cdaf52SJiri Slaby 	int ret;
219785cdaf52SJiri Slaby 
2198a635f9ddSJiri Kosina 	if (hid_debug)
21994291ee30SJoe Perches 		pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
22004291ee30SJoe Perches 			"debugfs is now used for inspecting the device (report descriptor, reports)\n");
2201a635f9ddSJiri Kosina 
220285cdaf52SJiri Slaby 	ret = bus_register(&hid_bus_type);
220385cdaf52SJiri Slaby 	if (ret) {
22044291ee30SJoe Perches 		pr_err("can't register hid bus\n");
220585cdaf52SJiri Slaby 		goto err;
220685cdaf52SJiri Slaby 	}
220785cdaf52SJiri Slaby 
220885cdaf52SJiri Slaby 	ret = hidraw_init();
220985cdaf52SJiri Slaby 	if (ret)
221085cdaf52SJiri Slaby 		goto err_bus;
221185cdaf52SJiri Slaby 
2212a635f9ddSJiri Kosina 	hid_debug_init();
2213a635f9ddSJiri Kosina 
221485cdaf52SJiri Slaby 	return 0;
221585cdaf52SJiri Slaby err_bus:
221685cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
221785cdaf52SJiri Slaby err:
221885cdaf52SJiri Slaby 	return ret;
221986166b7bSJiri Kosina }
222086166b7bSJiri Kosina 
222186166b7bSJiri Kosina static void __exit hid_exit(void)
222286166b7bSJiri Kosina {
2223a635f9ddSJiri Kosina 	hid_debug_exit();
222486166b7bSJiri Kosina 	hidraw_exit();
222585cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
222686166b7bSJiri Kosina }
222786166b7bSJiri Kosina 
222886166b7bSJiri Kosina module_init(hid_init);
222986166b7bSJiri Kosina module_exit(hid_exit);
223086166b7bSJiri Kosina 
223188adb72bSJiri Kosina MODULE_AUTHOR("Andreas Gal");
223288adb72bSJiri Kosina MODULE_AUTHOR("Vojtech Pavlik");
223388adb72bSJiri Kosina MODULE_AUTHOR("Jiri Kosina");
2234aa938f79SJiri Kosina MODULE_LICENSE(DRIVER_LICENSE);
2235aa938f79SJiri Kosina 
2236