xref: /openbmc/linux/drivers/hid/hid-core.c (revision 4cfae3e80431fd113f0f4a8e1d4fff84aafe045c)
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
7f142b3a4SJiri Kosina  *  Copyright (c) 2006-2007 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 
17dde5845aSJiri Kosina #include <linux/module.h>
18dde5845aSJiri Kosina #include <linux/slab.h>
19dde5845aSJiri Kosina #include <linux/init.h>
20dde5845aSJiri Kosina #include <linux/kernel.h>
21dde5845aSJiri Kosina #include <linux/list.h>
22dde5845aSJiri Kosina #include <linux/mm.h>
23dde5845aSJiri Kosina #include <linux/spinlock.h>
24dde5845aSJiri Kosina #include <asm/unaligned.h>
25dde5845aSJiri Kosina #include <asm/byteorder.h>
26dde5845aSJiri Kosina #include <linux/input.h>
27dde5845aSJiri Kosina #include <linux/wait.h>
2847a80edbSJiri Kosina #include <linux/vmalloc.h>
29c4124c9bSJiri Kosina #include <linux/sched.h>
30dde5845aSJiri Kosina 
31dde5845aSJiri Kosina #include <linux/hid.h>
32dde5845aSJiri Kosina #include <linux/hiddev.h>
33c080d89aSJiri Kosina #include <linux/hid-debug.h>
3486166b7bSJiri Kosina #include <linux/hidraw.h>
35dde5845aSJiri Kosina 
365f22a799SJiri Slaby #include "hid-ids.h"
375f22a799SJiri Slaby 
38dde5845aSJiri Kosina /*
39dde5845aSJiri Kosina  * Version Information
40dde5845aSJiri Kosina  */
41dde5845aSJiri Kosina 
42dde5845aSJiri Kosina #define DRIVER_VERSION "v2.6"
43f142b3a4SJiri Kosina #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
4453149801SJiri Kosina #define DRIVER_DESC "HID core driver"
45dde5845aSJiri Kosina #define DRIVER_LICENSE "GPL"
46dde5845aSJiri Kosina 
4758037eb9SJiri Kosina #ifdef CONFIG_HID_DEBUG
4858037eb9SJiri Kosina int hid_debug = 0;
49377e10fbSAnssi Hannula module_param_named(debug, hid_debug, int, 0600);
50377e10fbSAnssi Hannula MODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
5158037eb9SJiri Kosina EXPORT_SYMBOL_GPL(hid_debug);
5258037eb9SJiri Kosina #endif
5358037eb9SJiri Kosina 
54dde5845aSJiri Kosina /*
55dde5845aSJiri Kosina  * Register a new report for a device.
56dde5845aSJiri Kosina  */
57dde5845aSJiri Kosina 
58dde5845aSJiri Kosina static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
59dde5845aSJiri Kosina {
60dde5845aSJiri Kosina 	struct hid_report_enum *report_enum = device->report_enum + type;
61dde5845aSJiri Kosina 	struct hid_report *report;
62dde5845aSJiri Kosina 
63dde5845aSJiri Kosina 	if (report_enum->report_id_hash[id])
64dde5845aSJiri Kosina 		return report_enum->report_id_hash[id];
65dde5845aSJiri Kosina 
66dde5845aSJiri Kosina 	if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
67dde5845aSJiri Kosina 		return NULL;
68dde5845aSJiri Kosina 
69dde5845aSJiri Kosina 	if (id != 0)
70dde5845aSJiri Kosina 		report_enum->numbered = 1;
71dde5845aSJiri Kosina 
72dde5845aSJiri Kosina 	report->id = id;
73dde5845aSJiri Kosina 	report->type = type;
74dde5845aSJiri Kosina 	report->size = 0;
75dde5845aSJiri Kosina 	report->device = device;
76dde5845aSJiri Kosina 	report_enum->report_id_hash[id] = report;
77dde5845aSJiri Kosina 
78dde5845aSJiri Kosina 	list_add_tail(&report->list, &report_enum->report_list);
79dde5845aSJiri Kosina 
80dde5845aSJiri Kosina 	return report;
81dde5845aSJiri Kosina }
82dde5845aSJiri Kosina 
83dde5845aSJiri Kosina /*
84dde5845aSJiri Kosina  * Register a new field for this report.
85dde5845aSJiri Kosina  */
86dde5845aSJiri Kosina 
87dde5845aSJiri Kosina static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
88dde5845aSJiri Kosina {
89dde5845aSJiri Kosina 	struct hid_field *field;
90dde5845aSJiri Kosina 
91dde5845aSJiri Kosina 	if (report->maxfield == HID_MAX_FIELDS) {
9258037eb9SJiri Kosina 		dbg_hid("too many fields in report\n");
93dde5845aSJiri Kosina 		return NULL;
94dde5845aSJiri Kosina 	}
95dde5845aSJiri Kosina 
96dde5845aSJiri Kosina 	if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97dde5845aSJiri Kosina 		+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
98dde5845aSJiri Kosina 
99dde5845aSJiri Kosina 	field->index = report->maxfield++;
100dde5845aSJiri Kosina 	report->field[field->index] = field;
101dde5845aSJiri Kosina 	field->usage = (struct hid_usage *)(field + 1);
102282bfd4cSJiri Slaby 	field->value = (s32 *)(field->usage + usages);
103dde5845aSJiri Kosina 	field->report = report;
104dde5845aSJiri Kosina 
105dde5845aSJiri Kosina 	return field;
106dde5845aSJiri Kosina }
107dde5845aSJiri Kosina 
108dde5845aSJiri Kosina /*
109dde5845aSJiri Kosina  * Open a collection. The type/usage is pushed on the stack.
110dde5845aSJiri Kosina  */
111dde5845aSJiri Kosina 
112dde5845aSJiri Kosina static int open_collection(struct hid_parser *parser, unsigned type)
113dde5845aSJiri Kosina {
114dde5845aSJiri Kosina 	struct hid_collection *collection;
115dde5845aSJiri Kosina 	unsigned usage;
116dde5845aSJiri Kosina 
117dde5845aSJiri Kosina 	usage = parser->local.usage[0];
118dde5845aSJiri Kosina 
119dde5845aSJiri Kosina 	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
12058037eb9SJiri Kosina 		dbg_hid("collection stack overflow\n");
121dde5845aSJiri Kosina 		return -1;
122dde5845aSJiri Kosina 	}
123dde5845aSJiri Kosina 
124dde5845aSJiri Kosina 	if (parser->device->maxcollection == parser->device->collection_size) {
125dde5845aSJiri Kosina 		collection = kmalloc(sizeof(struct hid_collection) *
126dde5845aSJiri Kosina 				parser->device->collection_size * 2, GFP_KERNEL);
127dde5845aSJiri Kosina 		if (collection == NULL) {
12858037eb9SJiri Kosina 			dbg_hid("failed to reallocate collection array\n");
129dde5845aSJiri Kosina 			return -1;
130dde5845aSJiri Kosina 		}
131dde5845aSJiri Kosina 		memcpy(collection, parser->device->collection,
132dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
133dde5845aSJiri Kosina 			parser->device->collection_size);
134dde5845aSJiri Kosina 		memset(collection + parser->device->collection_size, 0,
135dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
136dde5845aSJiri Kosina 			parser->device->collection_size);
137dde5845aSJiri Kosina 		kfree(parser->device->collection);
138dde5845aSJiri Kosina 		parser->device->collection = collection;
139dde5845aSJiri Kosina 		parser->device->collection_size *= 2;
140dde5845aSJiri Kosina 	}
141dde5845aSJiri Kosina 
142dde5845aSJiri Kosina 	parser->collection_stack[parser->collection_stack_ptr++] =
143dde5845aSJiri Kosina 		parser->device->maxcollection;
144dde5845aSJiri Kosina 
145dde5845aSJiri Kosina 	collection = parser->device->collection +
146dde5845aSJiri Kosina 		parser->device->maxcollection++;
147dde5845aSJiri Kosina 	collection->type = type;
148dde5845aSJiri Kosina 	collection->usage = usage;
149dde5845aSJiri Kosina 	collection->level = parser->collection_stack_ptr - 1;
150dde5845aSJiri Kosina 
151dde5845aSJiri Kosina 	if (type == HID_COLLECTION_APPLICATION)
152dde5845aSJiri Kosina 		parser->device->maxapplication++;
153dde5845aSJiri Kosina 
154dde5845aSJiri Kosina 	return 0;
155dde5845aSJiri Kosina }
156dde5845aSJiri Kosina 
157dde5845aSJiri Kosina /*
158dde5845aSJiri Kosina  * Close a collection.
159dde5845aSJiri Kosina  */
160dde5845aSJiri Kosina 
161dde5845aSJiri Kosina static int close_collection(struct hid_parser *parser)
162dde5845aSJiri Kosina {
163dde5845aSJiri Kosina 	if (!parser->collection_stack_ptr) {
16458037eb9SJiri Kosina 		dbg_hid("collection stack underflow\n");
165dde5845aSJiri Kosina 		return -1;
166dde5845aSJiri Kosina 	}
167dde5845aSJiri Kosina 	parser->collection_stack_ptr--;
168dde5845aSJiri Kosina 	return 0;
169dde5845aSJiri Kosina }
170dde5845aSJiri Kosina 
171dde5845aSJiri Kosina /*
172dde5845aSJiri Kosina  * Climb up the stack, search for the specified collection type
173dde5845aSJiri Kosina  * and return the usage.
174dde5845aSJiri Kosina  */
175dde5845aSJiri Kosina 
176dde5845aSJiri Kosina static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
177dde5845aSJiri Kosina {
178dde5845aSJiri Kosina 	int n;
179dde5845aSJiri Kosina 	for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
180dde5845aSJiri Kosina 		if (parser->device->collection[parser->collection_stack[n]].type == type)
181dde5845aSJiri Kosina 			return parser->device->collection[parser->collection_stack[n]].usage;
182dde5845aSJiri Kosina 	return 0; /* we know nothing about this usage type */
183dde5845aSJiri Kosina }
184dde5845aSJiri Kosina 
185dde5845aSJiri Kosina /*
186dde5845aSJiri Kosina  * Add a usage to the temporary parser table.
187dde5845aSJiri Kosina  */
188dde5845aSJiri Kosina 
189dde5845aSJiri Kosina static int hid_add_usage(struct hid_parser *parser, unsigned usage)
190dde5845aSJiri Kosina {
191dde5845aSJiri Kosina 	if (parser->local.usage_index >= HID_MAX_USAGES) {
19258037eb9SJiri Kosina 		dbg_hid("usage index exceeded\n");
193dde5845aSJiri Kosina 		return -1;
194dde5845aSJiri Kosina 	}
195dde5845aSJiri Kosina 	parser->local.usage[parser->local.usage_index] = usage;
196dde5845aSJiri Kosina 	parser->local.collection_index[parser->local.usage_index] =
197dde5845aSJiri Kosina 		parser->collection_stack_ptr ?
198dde5845aSJiri Kosina 		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
199dde5845aSJiri Kosina 	parser->local.usage_index++;
200dde5845aSJiri Kosina 	return 0;
201dde5845aSJiri Kosina }
202dde5845aSJiri Kosina 
203dde5845aSJiri Kosina /*
204dde5845aSJiri Kosina  * Register a new field for this report.
205dde5845aSJiri Kosina  */
206dde5845aSJiri Kosina 
207dde5845aSJiri Kosina static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
208dde5845aSJiri Kosina {
209dde5845aSJiri Kosina 	struct hid_report *report;
210dde5845aSJiri Kosina 	struct hid_field *field;
211dde5845aSJiri Kosina 	int usages;
212dde5845aSJiri Kosina 	unsigned offset;
213dde5845aSJiri Kosina 	int i;
214dde5845aSJiri Kosina 
215dde5845aSJiri Kosina 	if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
21658037eb9SJiri Kosina 		dbg_hid("hid_register_report failed\n");
217dde5845aSJiri Kosina 		return -1;
218dde5845aSJiri Kosina 	}
219dde5845aSJiri Kosina 
220dde5845aSJiri Kosina 	if (parser->global.logical_maximum < parser->global.logical_minimum) {
22158037eb9SJiri Kosina 		dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
222dde5845aSJiri Kosina 		return -1;
223dde5845aSJiri Kosina 	}
224dde5845aSJiri Kosina 
225dde5845aSJiri Kosina 	offset = report->size;
226dde5845aSJiri Kosina 	report->size += parser->global.report_size * parser->global.report_count;
227dde5845aSJiri Kosina 
228dde5845aSJiri Kosina 	if (!parser->local.usage_index) /* Ignore padding fields */
229dde5845aSJiri Kosina 		return 0;
230dde5845aSJiri Kosina 
231dde5845aSJiri Kosina 	usages = max_t(int, parser->local.usage_index, parser->global.report_count);
232dde5845aSJiri Kosina 
233dde5845aSJiri Kosina 	if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
234dde5845aSJiri Kosina 		return 0;
235dde5845aSJiri Kosina 
236dde5845aSJiri Kosina 	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
237dde5845aSJiri Kosina 	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
238dde5845aSJiri Kosina 	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
239dde5845aSJiri Kosina 
240dde5845aSJiri Kosina 	for (i = 0; i < usages; i++) {
241dde5845aSJiri Kosina 		int j = i;
242dde5845aSJiri Kosina 		/* Duplicate the last usage we parsed if we have excess values */
243dde5845aSJiri Kosina 		if (i >= parser->local.usage_index)
244dde5845aSJiri Kosina 			j = parser->local.usage_index - 1;
245dde5845aSJiri Kosina 		field->usage[i].hid = parser->local.usage[j];
246dde5845aSJiri Kosina 		field->usage[i].collection_index =
247dde5845aSJiri Kosina 			parser->local.collection_index[j];
248dde5845aSJiri Kosina 	}
249dde5845aSJiri Kosina 
250dde5845aSJiri Kosina 	field->maxusage = usages;
251dde5845aSJiri Kosina 	field->flags = flags;
252dde5845aSJiri Kosina 	field->report_offset = offset;
253dde5845aSJiri Kosina 	field->report_type = report_type;
254dde5845aSJiri Kosina 	field->report_size = parser->global.report_size;
255dde5845aSJiri Kosina 	field->report_count = parser->global.report_count;
256dde5845aSJiri Kosina 	field->logical_minimum = parser->global.logical_minimum;
257dde5845aSJiri Kosina 	field->logical_maximum = parser->global.logical_maximum;
258dde5845aSJiri Kosina 	field->physical_minimum = parser->global.physical_minimum;
259dde5845aSJiri Kosina 	field->physical_maximum = parser->global.physical_maximum;
260dde5845aSJiri Kosina 	field->unit_exponent = parser->global.unit_exponent;
261dde5845aSJiri Kosina 	field->unit = parser->global.unit;
262dde5845aSJiri Kosina 
263dde5845aSJiri Kosina 	return 0;
264dde5845aSJiri Kosina }
265dde5845aSJiri Kosina 
266dde5845aSJiri Kosina /*
267dde5845aSJiri Kosina  * Read data value from item.
268dde5845aSJiri Kosina  */
269dde5845aSJiri Kosina 
270dde5845aSJiri Kosina static u32 item_udata(struct hid_item *item)
271dde5845aSJiri Kosina {
272dde5845aSJiri Kosina 	switch (item->size) {
273dde5845aSJiri Kosina 	case 1: return item->data.u8;
274dde5845aSJiri Kosina 	case 2: return item->data.u16;
275dde5845aSJiri Kosina 	case 4: return item->data.u32;
276dde5845aSJiri Kosina 	}
277dde5845aSJiri Kosina 	return 0;
278dde5845aSJiri Kosina }
279dde5845aSJiri Kosina 
280dde5845aSJiri Kosina static s32 item_sdata(struct hid_item *item)
281dde5845aSJiri Kosina {
282dde5845aSJiri Kosina 	switch (item->size) {
283dde5845aSJiri Kosina 	case 1: return item->data.s8;
284dde5845aSJiri Kosina 	case 2: return item->data.s16;
285dde5845aSJiri Kosina 	case 4: return item->data.s32;
286dde5845aSJiri Kosina 	}
287dde5845aSJiri Kosina 	return 0;
288dde5845aSJiri Kosina }
289dde5845aSJiri Kosina 
290dde5845aSJiri Kosina /*
291dde5845aSJiri Kosina  * Process a global item.
292dde5845aSJiri Kosina  */
293dde5845aSJiri Kosina 
294dde5845aSJiri Kosina static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
295dde5845aSJiri Kosina {
296dde5845aSJiri Kosina 	switch (item->tag) {
297dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PUSH:
298dde5845aSJiri Kosina 
299dde5845aSJiri Kosina 		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
30058037eb9SJiri Kosina 			dbg_hid("global enviroment stack overflow\n");
301dde5845aSJiri Kosina 			return -1;
302dde5845aSJiri Kosina 		}
303dde5845aSJiri Kosina 
304dde5845aSJiri Kosina 		memcpy(parser->global_stack + parser->global_stack_ptr++,
305dde5845aSJiri Kosina 			&parser->global, sizeof(struct hid_global));
306dde5845aSJiri Kosina 		return 0;
307dde5845aSJiri Kosina 
308dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_POP:
309dde5845aSJiri Kosina 
310dde5845aSJiri Kosina 		if (!parser->global_stack_ptr) {
31158037eb9SJiri Kosina 			dbg_hid("global enviroment stack underflow\n");
312dde5845aSJiri Kosina 			return -1;
313dde5845aSJiri Kosina 		}
314dde5845aSJiri Kosina 
315880d29f1SJiri Slaby 		memcpy(&parser->global, parser->global_stack +
316880d29f1SJiri Slaby 			--parser->global_stack_ptr, sizeof(struct hid_global));
317dde5845aSJiri Kosina 		return 0;
318dde5845aSJiri Kosina 
319dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
320dde5845aSJiri Kosina 		parser->global.usage_page = item_udata(item);
321dde5845aSJiri Kosina 		return 0;
322dde5845aSJiri Kosina 
323dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
324dde5845aSJiri Kosina 		parser->global.logical_minimum = item_sdata(item);
325dde5845aSJiri Kosina 		return 0;
326dde5845aSJiri Kosina 
327dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
328dde5845aSJiri Kosina 		if (parser->global.logical_minimum < 0)
329dde5845aSJiri Kosina 			parser->global.logical_maximum = item_sdata(item);
330dde5845aSJiri Kosina 		else
331dde5845aSJiri Kosina 			parser->global.logical_maximum = item_udata(item);
332dde5845aSJiri Kosina 		return 0;
333dde5845aSJiri Kosina 
334dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
335dde5845aSJiri Kosina 		parser->global.physical_minimum = item_sdata(item);
336dde5845aSJiri Kosina 		return 0;
337dde5845aSJiri Kosina 
338dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
339dde5845aSJiri Kosina 		if (parser->global.physical_minimum < 0)
340dde5845aSJiri Kosina 			parser->global.physical_maximum = item_sdata(item);
341dde5845aSJiri Kosina 		else
342dde5845aSJiri Kosina 			parser->global.physical_maximum = item_udata(item);
343dde5845aSJiri Kosina 		return 0;
344dde5845aSJiri Kosina 
345dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
346dde5845aSJiri Kosina 		parser->global.unit_exponent = item_sdata(item);
347dde5845aSJiri Kosina 		return 0;
348dde5845aSJiri Kosina 
349dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT:
350dde5845aSJiri Kosina 		parser->global.unit = item_udata(item);
351dde5845aSJiri Kosina 		return 0;
352dde5845aSJiri Kosina 
353dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
354880d29f1SJiri Slaby 		parser->global.report_size = item_udata(item);
355880d29f1SJiri Slaby 		if (parser->global.report_size > 32) {
356880d29f1SJiri Slaby 			dbg_hid("invalid report_size %d\n",
357880d29f1SJiri Slaby 					parser->global.report_size);
358dde5845aSJiri Kosina 			return -1;
359dde5845aSJiri Kosina 		}
360dde5845aSJiri Kosina 		return 0;
361dde5845aSJiri Kosina 
362dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
363880d29f1SJiri Slaby 		parser->global.report_count = item_udata(item);
364880d29f1SJiri Slaby 		if (parser->global.report_count > HID_MAX_USAGES) {
365880d29f1SJiri Slaby 			dbg_hid("invalid report_count %d\n",
366880d29f1SJiri Slaby 					parser->global.report_count);
367dde5845aSJiri Kosina 			return -1;
368dde5845aSJiri Kosina 		}
369dde5845aSJiri Kosina 		return 0;
370dde5845aSJiri Kosina 
371dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
372880d29f1SJiri Slaby 		parser->global.report_id = item_udata(item);
373880d29f1SJiri Slaby 		if (parser->global.report_id == 0) {
37458037eb9SJiri Kosina 			dbg_hid("report_id 0 is invalid\n");
375dde5845aSJiri Kosina 			return -1;
376dde5845aSJiri Kosina 		}
377dde5845aSJiri Kosina 		return 0;
378dde5845aSJiri Kosina 
379dde5845aSJiri Kosina 	default:
38058037eb9SJiri Kosina 		dbg_hid("unknown global tag 0x%x\n", item->tag);
381dde5845aSJiri Kosina 		return -1;
382dde5845aSJiri Kosina 	}
383dde5845aSJiri Kosina }
384dde5845aSJiri Kosina 
385dde5845aSJiri Kosina /*
386dde5845aSJiri Kosina  * Process a local item.
387dde5845aSJiri Kosina  */
388dde5845aSJiri Kosina 
389dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
390dde5845aSJiri Kosina {
391dde5845aSJiri Kosina 	__u32 data;
392dde5845aSJiri Kosina 	unsigned n;
393dde5845aSJiri Kosina 
394dde5845aSJiri Kosina 	if (item->size == 0) {
39558037eb9SJiri Kosina 		dbg_hid("item data expected for local item\n");
396dde5845aSJiri Kosina 		return -1;
397dde5845aSJiri Kosina 	}
398dde5845aSJiri Kosina 
399dde5845aSJiri Kosina 	data = item_udata(item);
400dde5845aSJiri Kosina 
401dde5845aSJiri Kosina 	switch (item->tag) {
402dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_DELIMITER:
403dde5845aSJiri Kosina 
404dde5845aSJiri Kosina 		if (data) {
405dde5845aSJiri Kosina 			/*
406dde5845aSJiri Kosina 			 * We treat items before the first delimiter
407dde5845aSJiri Kosina 			 * as global to all usage sets (branch 0).
408dde5845aSJiri Kosina 			 * In the moment we process only these global
409dde5845aSJiri Kosina 			 * items and the first delimiter set.
410dde5845aSJiri Kosina 			 */
411dde5845aSJiri Kosina 			if (parser->local.delimiter_depth != 0) {
41258037eb9SJiri Kosina 				dbg_hid("nested delimiters\n");
413dde5845aSJiri Kosina 				return -1;
414dde5845aSJiri Kosina 			}
415dde5845aSJiri Kosina 			parser->local.delimiter_depth++;
416dde5845aSJiri Kosina 			parser->local.delimiter_branch++;
417dde5845aSJiri Kosina 		} else {
418dde5845aSJiri Kosina 			if (parser->local.delimiter_depth < 1) {
41958037eb9SJiri Kosina 				dbg_hid("bogus close delimiter\n");
420dde5845aSJiri Kosina 				return -1;
421dde5845aSJiri Kosina 			}
422dde5845aSJiri Kosina 			parser->local.delimiter_depth--;
423dde5845aSJiri Kosina 		}
424dde5845aSJiri Kosina 		return 1;
425dde5845aSJiri Kosina 
426dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE:
427dde5845aSJiri Kosina 
428dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
42958037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
430dde5845aSJiri Kosina 			return 0;
431dde5845aSJiri Kosina 		}
432dde5845aSJiri Kosina 
433dde5845aSJiri Kosina 		if (item->size <= 2)
434dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
435dde5845aSJiri Kosina 
436dde5845aSJiri Kosina 		return hid_add_usage(parser, data);
437dde5845aSJiri Kosina 
438dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
439dde5845aSJiri Kosina 
440dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
44158037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
442dde5845aSJiri Kosina 			return 0;
443dde5845aSJiri Kosina 		}
444dde5845aSJiri Kosina 
445dde5845aSJiri Kosina 		if (item->size <= 2)
446dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
447dde5845aSJiri Kosina 
448dde5845aSJiri Kosina 		parser->local.usage_minimum = data;
449dde5845aSJiri Kosina 		return 0;
450dde5845aSJiri Kosina 
451dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
452dde5845aSJiri Kosina 
453dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
45458037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
455dde5845aSJiri Kosina 			return 0;
456dde5845aSJiri Kosina 		}
457dde5845aSJiri Kosina 
458dde5845aSJiri Kosina 		if (item->size <= 2)
459dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
460dde5845aSJiri Kosina 
461dde5845aSJiri Kosina 		for (n = parser->local.usage_minimum; n <= data; n++)
462dde5845aSJiri Kosina 			if (hid_add_usage(parser, n)) {
46358037eb9SJiri Kosina 				dbg_hid("hid_add_usage failed\n");
464dde5845aSJiri Kosina 				return -1;
465dde5845aSJiri Kosina 			}
466dde5845aSJiri Kosina 		return 0;
467dde5845aSJiri Kosina 
468dde5845aSJiri Kosina 	default:
469dde5845aSJiri Kosina 
47058037eb9SJiri Kosina 		dbg_hid("unknown local item tag 0x%x\n", item->tag);
471dde5845aSJiri Kosina 		return 0;
472dde5845aSJiri Kosina 	}
473dde5845aSJiri Kosina 	return 0;
474dde5845aSJiri Kosina }
475dde5845aSJiri Kosina 
476dde5845aSJiri Kosina /*
477dde5845aSJiri Kosina  * Process a main item.
478dde5845aSJiri Kosina  */
479dde5845aSJiri Kosina 
480dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
481dde5845aSJiri Kosina {
482dde5845aSJiri Kosina 	__u32 data;
483dde5845aSJiri Kosina 	int ret;
484dde5845aSJiri Kosina 
485dde5845aSJiri Kosina 	data = item_udata(item);
486dde5845aSJiri Kosina 
487dde5845aSJiri Kosina 	switch (item->tag) {
488dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
489dde5845aSJiri Kosina 		ret = open_collection(parser, data & 0xff);
490dde5845aSJiri Kosina 		break;
491dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
492dde5845aSJiri Kosina 		ret = close_collection(parser);
493dde5845aSJiri Kosina 		break;
494dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_INPUT:
495dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
496dde5845aSJiri Kosina 		break;
497dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_OUTPUT:
498dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
499dde5845aSJiri Kosina 		break;
500dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_FEATURE:
501dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
502dde5845aSJiri Kosina 		break;
503dde5845aSJiri Kosina 	default:
50458037eb9SJiri Kosina 		dbg_hid("unknown main item tag 0x%x\n", item->tag);
505dde5845aSJiri Kosina 		ret = 0;
506dde5845aSJiri Kosina 	}
507dde5845aSJiri Kosina 
508dde5845aSJiri Kosina 	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
509dde5845aSJiri Kosina 
510dde5845aSJiri Kosina 	return ret;
511dde5845aSJiri Kosina }
512dde5845aSJiri Kosina 
513dde5845aSJiri Kosina /*
514dde5845aSJiri Kosina  * Process a reserved item.
515dde5845aSJiri Kosina  */
516dde5845aSJiri Kosina 
517dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
518dde5845aSJiri Kosina {
51958037eb9SJiri Kosina 	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
520dde5845aSJiri Kosina 	return 0;
521dde5845aSJiri Kosina }
522dde5845aSJiri Kosina 
523dde5845aSJiri Kosina /*
524dde5845aSJiri Kosina  * Free a report and all registered fields. The field->usage and
525dde5845aSJiri Kosina  * field->value table's are allocated behind the field, so we need
526dde5845aSJiri Kosina  * only to free(field) itself.
527dde5845aSJiri Kosina  */
528dde5845aSJiri Kosina 
529dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report)
530dde5845aSJiri Kosina {
531dde5845aSJiri Kosina 	unsigned n;
532dde5845aSJiri Kosina 
533dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
534dde5845aSJiri Kosina 		kfree(report->field[n]);
535dde5845aSJiri Kosina 	kfree(report);
536dde5845aSJiri Kosina }
537dde5845aSJiri Kosina 
538dde5845aSJiri Kosina /*
539dde5845aSJiri Kosina  * Free a device structure, all reports, and all fields.
540dde5845aSJiri Kosina  */
541dde5845aSJiri Kosina 
54285cdaf52SJiri Slaby static void hid_device_release(struct device *dev)
543dde5845aSJiri Kosina {
54485cdaf52SJiri Slaby 	struct hid_device *device = container_of(dev, struct hid_device, dev);
545dde5845aSJiri Kosina 	unsigned i, j;
546dde5845aSJiri Kosina 
547dde5845aSJiri Kosina 	for (i = 0; i < HID_REPORT_TYPES; i++) {
548dde5845aSJiri Kosina 		struct hid_report_enum *report_enum = device->report_enum + i;
549dde5845aSJiri Kosina 
550dde5845aSJiri Kosina 		for (j = 0; j < 256; j++) {
551dde5845aSJiri Kosina 			struct hid_report *report = report_enum->report_id_hash[j];
552dde5845aSJiri Kosina 			if (report)
553dde5845aSJiri Kosina 				hid_free_report(report);
554dde5845aSJiri Kosina 		}
555dde5845aSJiri Kosina 	}
556dde5845aSJiri Kosina 
557dde5845aSJiri Kosina 	kfree(device->rdesc);
558767fe787SJiri Kosina 	kfree(device->collection);
559dde5845aSJiri Kosina 	kfree(device);
560dde5845aSJiri Kosina }
561dde5845aSJiri Kosina 
562dde5845aSJiri Kosina /*
563dde5845aSJiri Kosina  * Fetch a report description item from the data stream. We support long
564dde5845aSJiri Kosina  * items, though they are not used yet.
565dde5845aSJiri Kosina  */
566dde5845aSJiri Kosina 
567dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
568dde5845aSJiri Kosina {
569dde5845aSJiri Kosina 	u8 b;
570dde5845aSJiri Kosina 
571dde5845aSJiri Kosina 	if ((end - start) <= 0)
572dde5845aSJiri Kosina 		return NULL;
573dde5845aSJiri Kosina 
574dde5845aSJiri Kosina 	b = *start++;
575dde5845aSJiri Kosina 
576dde5845aSJiri Kosina 	item->type = (b >> 2) & 3;
577dde5845aSJiri Kosina 	item->tag  = (b >> 4) & 15;
578dde5845aSJiri Kosina 
579dde5845aSJiri Kosina 	if (item->tag == HID_ITEM_TAG_LONG) {
580dde5845aSJiri Kosina 
581dde5845aSJiri Kosina 		item->format = HID_ITEM_FORMAT_LONG;
582dde5845aSJiri Kosina 
583dde5845aSJiri Kosina 		if ((end - start) < 2)
584dde5845aSJiri Kosina 			return NULL;
585dde5845aSJiri Kosina 
586dde5845aSJiri Kosina 		item->size = *start++;
587dde5845aSJiri Kosina 		item->tag  = *start++;
588dde5845aSJiri Kosina 
589dde5845aSJiri Kosina 		if ((end - start) < item->size)
590dde5845aSJiri Kosina 			return NULL;
591dde5845aSJiri Kosina 
592dde5845aSJiri Kosina 		item->data.longdata = start;
593dde5845aSJiri Kosina 		start += item->size;
594dde5845aSJiri Kosina 		return start;
595dde5845aSJiri Kosina 	}
596dde5845aSJiri Kosina 
597dde5845aSJiri Kosina 	item->format = HID_ITEM_FORMAT_SHORT;
598dde5845aSJiri Kosina 	item->size = b & 3;
599dde5845aSJiri Kosina 
600dde5845aSJiri Kosina 	switch (item->size) {
601dde5845aSJiri Kosina 	case 0:
602dde5845aSJiri Kosina 		return start;
603dde5845aSJiri Kosina 
604dde5845aSJiri Kosina 	case 1:
605dde5845aSJiri Kosina 		if ((end - start) < 1)
606dde5845aSJiri Kosina 			return NULL;
607dde5845aSJiri Kosina 		item->data.u8 = *start++;
608dde5845aSJiri Kosina 		return start;
609dde5845aSJiri Kosina 
610dde5845aSJiri Kosina 	case 2:
611dde5845aSJiri Kosina 		if ((end - start) < 2)
612dde5845aSJiri Kosina 			return NULL;
613c105068fSHarvey Harrison 		item->data.u16 = get_unaligned_le16(start);
614dde5845aSJiri Kosina 		start = (__u8 *)((__le16 *)start + 1);
615dde5845aSJiri Kosina 		return start;
616dde5845aSJiri Kosina 
617dde5845aSJiri Kosina 	case 3:
618dde5845aSJiri Kosina 		item->size++;
619dde5845aSJiri Kosina 		if ((end - start) < 4)
620dde5845aSJiri Kosina 			return NULL;
621c105068fSHarvey Harrison 		item->data.u32 = get_unaligned_le32(start);
622dde5845aSJiri Kosina 		start = (__u8 *)((__le32 *)start + 1);
623dde5845aSJiri Kosina 		return start;
624dde5845aSJiri Kosina 	}
625dde5845aSJiri Kosina 
626dde5845aSJiri Kosina 	return NULL;
627dde5845aSJiri Kosina }
628dde5845aSJiri Kosina 
62985cdaf52SJiri Slaby /**
63085cdaf52SJiri Slaby  * hid_parse_report - parse device report
63185cdaf52SJiri Slaby  *
63285cdaf52SJiri Slaby  * @device: hid device
63385cdaf52SJiri Slaby  * @start: report start
63485cdaf52SJiri Slaby  * @size: report size
63585cdaf52SJiri Slaby  *
636dde5845aSJiri Kosina  * Parse a report description into a hid_device structure. Reports are
637dde5845aSJiri Kosina  * enumerated, fields are attached to these reports.
63885cdaf52SJiri Slaby  * 0 returned on success, otherwise nonzero error value.
639dde5845aSJiri Kosina  */
64085cdaf52SJiri Slaby int hid_parse_report(struct hid_device *device, __u8 *start,
64185cdaf52SJiri Slaby 		unsigned size)
642dde5845aSJiri Kosina {
643dde5845aSJiri Kosina 	struct hid_parser *parser;
644dde5845aSJiri Kosina 	struct hid_item item;
645dde5845aSJiri Kosina 	__u8 *end;
64685cdaf52SJiri Slaby 	int ret;
647dde5845aSJiri Kosina 	static int (*dispatch_type[])(struct hid_parser *parser,
648dde5845aSJiri Kosina 				      struct hid_item *item) = {
649dde5845aSJiri Kosina 		hid_parser_main,
650dde5845aSJiri Kosina 		hid_parser_global,
651dde5845aSJiri Kosina 		hid_parser_local,
652dde5845aSJiri Kosina 		hid_parser_reserved
653dde5845aSJiri Kosina 	};
654dde5845aSJiri Kosina 
655c500c971SJiri Slaby 	if (device->driver->report_fixup)
656c500c971SJiri Slaby 		device->driver->report_fixup(device, start, size);
657c500c971SJiri Slaby 
65885cdaf52SJiri Slaby 	device->rdesc = kmalloc(size, GFP_KERNEL);
65985cdaf52SJiri Slaby 	if (device->rdesc == NULL)
66085cdaf52SJiri Slaby 		return -ENOMEM;
661dde5845aSJiri Kosina 	memcpy(device->rdesc, start, size);
662dde5845aSJiri Kosina 	device->rsize = size;
663dde5845aSJiri Kosina 
66485cdaf52SJiri Slaby 	parser = vmalloc(sizeof(struct hid_parser));
66585cdaf52SJiri Slaby 	if (!parser) {
66685cdaf52SJiri Slaby 		ret = -ENOMEM;
66785cdaf52SJiri Slaby 		goto err;
668dde5845aSJiri Kosina 	}
66985cdaf52SJiri Slaby 
67047a80edbSJiri Kosina 	memset(parser, 0, sizeof(struct hid_parser));
671dde5845aSJiri Kosina 	parser->device = device;
672dde5845aSJiri Kosina 
673dde5845aSJiri Kosina 	end = start + size;
67485cdaf52SJiri Slaby 	ret = -EINVAL;
675dde5845aSJiri Kosina 	while ((start = fetch_item(start, end, &item)) != NULL) {
676dde5845aSJiri Kosina 
677dde5845aSJiri Kosina 		if (item.format != HID_ITEM_FORMAT_SHORT) {
67858037eb9SJiri Kosina 			dbg_hid("unexpected long global item\n");
67985cdaf52SJiri Slaby 			goto err;
680dde5845aSJiri Kosina 		}
681dde5845aSJiri Kosina 
682dde5845aSJiri Kosina 		if (dispatch_type[item.type](parser, &item)) {
68358037eb9SJiri Kosina 			dbg_hid("item %u %u %u %u parsing failed\n",
684dde5845aSJiri Kosina 				item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
68585cdaf52SJiri Slaby 			goto err;
686dde5845aSJiri Kosina 		}
687dde5845aSJiri Kosina 
688dde5845aSJiri Kosina 		if (start == end) {
689dde5845aSJiri Kosina 			if (parser->collection_stack_ptr) {
69058037eb9SJiri Kosina 				dbg_hid("unbalanced collection at end of report description\n");
69185cdaf52SJiri Slaby 				goto err;
692dde5845aSJiri Kosina 			}
693dde5845aSJiri Kosina 			if (parser->local.delimiter_depth) {
69458037eb9SJiri Kosina 				dbg_hid("unbalanced delimiter at end of report description\n");
69585cdaf52SJiri Slaby 				goto err;
696dde5845aSJiri Kosina 			}
69747a80edbSJiri Kosina 			vfree(parser);
69885cdaf52SJiri Slaby 			return 0;
699dde5845aSJiri Kosina 		}
700dde5845aSJiri Kosina 	}
701dde5845aSJiri Kosina 
70258037eb9SJiri Kosina 	dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
70385cdaf52SJiri Slaby err:
70447a80edbSJiri Kosina 	vfree(parser);
70585cdaf52SJiri Slaby 	return ret;
706dde5845aSJiri Kosina }
707229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_parse_report);
708dde5845aSJiri Kosina 
709dde5845aSJiri Kosina /*
710dde5845aSJiri Kosina  * Convert a signed n-bit integer to signed 32-bit integer. Common
711dde5845aSJiri Kosina  * cases are done through the compiler, the screwed things has to be
712dde5845aSJiri Kosina  * done by hand.
713dde5845aSJiri Kosina  */
714dde5845aSJiri Kosina 
715dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n)
716dde5845aSJiri Kosina {
717dde5845aSJiri Kosina 	switch (n) {
718dde5845aSJiri Kosina 	case 8:  return ((__s8)value);
719dde5845aSJiri Kosina 	case 16: return ((__s16)value);
720dde5845aSJiri Kosina 	case 32: return ((__s32)value);
721dde5845aSJiri Kosina 	}
722dde5845aSJiri Kosina 	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
723dde5845aSJiri Kosina }
724dde5845aSJiri Kosina 
725dde5845aSJiri Kosina /*
726dde5845aSJiri Kosina  * Convert a signed 32-bit integer to a signed n-bit integer.
727dde5845aSJiri Kosina  */
728dde5845aSJiri Kosina 
729dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n)
730dde5845aSJiri Kosina {
731dde5845aSJiri Kosina 	s32 a = value >> (n - 1);
732dde5845aSJiri Kosina 	if (a && a != -1)
733dde5845aSJiri Kosina 		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
734dde5845aSJiri Kosina 	return value & ((1 << n) - 1);
735dde5845aSJiri Kosina }
736dde5845aSJiri Kosina 
737dde5845aSJiri Kosina /*
738dde5845aSJiri Kosina  * Extract/implement a data field from/to a little endian report (bit array).
739dde5845aSJiri Kosina  *
740dde5845aSJiri Kosina  * Code sort-of follows HID spec:
741dde5845aSJiri Kosina  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
742dde5845aSJiri Kosina  *
743dde5845aSJiri Kosina  * While the USB HID spec allows unlimited length bit fields in "report
744dde5845aSJiri Kosina  * descriptors", most devices never use more than 16 bits.
745dde5845aSJiri Kosina  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
746dde5845aSJiri Kosina  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
747dde5845aSJiri Kosina  */
748dde5845aSJiri Kosina 
749dde5845aSJiri Kosina static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
750dde5845aSJiri Kosina {
751dde5845aSJiri Kosina 	u64 x;
752dde5845aSJiri Kosina 
753c4124c9bSJiri Kosina 	if (n > 32)
754c4124c9bSJiri Kosina 		printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
755c4124c9bSJiri Kosina 				n, current->comm);
756dde5845aSJiri Kosina 
757dde5845aSJiri Kosina 	report += offset >> 3;  /* adjust byte index */
758dde5845aSJiri Kosina 	offset &= 7;            /* now only need bit offset into one byte */
759c105068fSHarvey Harrison 	x = get_unaligned_le64(report);
760dde5845aSJiri Kosina 	x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
761dde5845aSJiri Kosina 	return (u32) x;
762dde5845aSJiri Kosina }
763dde5845aSJiri Kosina 
764dde5845aSJiri Kosina /*
765dde5845aSJiri Kosina  * "implement" : set bits in a little endian bit stream.
766dde5845aSJiri Kosina  * Same concepts as "extract" (see comments above).
767dde5845aSJiri Kosina  * The data mangled in the bit stream remains in little endian
768dde5845aSJiri Kosina  * order the whole time. It make more sense to talk about
769dde5845aSJiri Kosina  * endianness of register values by considering a register
770dde5845aSJiri Kosina  * a "cached" copy of the little endiad bit stream.
771dde5845aSJiri Kosina  */
772dde5845aSJiri Kosina static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
773dde5845aSJiri Kosina {
7746f0168d2SHarvey Harrison 	u64 x;
775dde5845aSJiri Kosina 	u64 m = (1ULL << n) - 1;
776dde5845aSJiri Kosina 
777c4124c9bSJiri Kosina 	if (n > 32)
778c4124c9bSJiri Kosina 		printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
779c4124c9bSJiri Kosina 				n, current->comm);
780dde5845aSJiri Kosina 
781c4124c9bSJiri Kosina 	if (value > m)
782c4124c9bSJiri Kosina 		printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
783c4124c9bSJiri Kosina 				value, current->comm);
784dde5845aSJiri Kosina 	WARN_ON(value > m);
785dde5845aSJiri Kosina 	value &= m;
786dde5845aSJiri Kosina 
787dde5845aSJiri Kosina 	report += offset >> 3;
788dde5845aSJiri Kosina 	offset &= 7;
789dde5845aSJiri Kosina 
7906f0168d2SHarvey Harrison 	x = get_unaligned_le64(report);
7916f0168d2SHarvey Harrison 	x &= ~(m << offset);
7926f0168d2SHarvey Harrison 	x |= ((u64)value) << offset;
7936f0168d2SHarvey Harrison 	put_unaligned_le64(x, report);
794dde5845aSJiri Kosina }
795dde5845aSJiri Kosina 
796dde5845aSJiri Kosina /*
797dde5845aSJiri Kosina  * Search an array for a value.
798dde5845aSJiri Kosina  */
799dde5845aSJiri Kosina 
800dde5845aSJiri Kosina static __inline__ int search(__s32 *array, __s32 value, unsigned n)
801dde5845aSJiri Kosina {
802dde5845aSJiri Kosina 	while (n--) {
803dde5845aSJiri Kosina 		if (*array++ == value)
804dde5845aSJiri Kosina 			return 0;
805dde5845aSJiri Kosina 	}
806dde5845aSJiri Kosina 	return -1;
807dde5845aSJiri Kosina }
808dde5845aSJiri Kosina 
80985cdaf52SJiri Slaby /**
81085cdaf52SJiri Slaby  * hid_match_report - check if driver's raw_event should be called
81185cdaf52SJiri Slaby  *
81285cdaf52SJiri Slaby  * @hid: hid device
81385cdaf52SJiri Slaby  * @report_type: type to match against
81485cdaf52SJiri Slaby  *
81585cdaf52SJiri Slaby  * compare hid->driver->report_table->report_type to report->type
81685cdaf52SJiri Slaby  */
81785cdaf52SJiri Slaby static int hid_match_report(struct hid_device *hid, struct hid_report *report)
818dde5845aSJiri Kosina {
81985cdaf52SJiri Slaby 	const struct hid_report_id *id = hid->driver->report_table;
82085cdaf52SJiri Slaby 
82185cdaf52SJiri Slaby 	if (!id) /* NULL means all */
82285cdaf52SJiri Slaby 		return 1;
82385cdaf52SJiri Slaby 
82485cdaf52SJiri Slaby 	for (; id->report_type != HID_TERMINATOR; id++)
82585cdaf52SJiri Slaby 		if (id->report_type == HID_ANY_ID ||
82685cdaf52SJiri Slaby 				id->report_type == report->type)
82785cdaf52SJiri Slaby 			return 1;
82885cdaf52SJiri Slaby 	return 0;
82985cdaf52SJiri Slaby }
83085cdaf52SJiri Slaby 
83185cdaf52SJiri Slaby /**
83285cdaf52SJiri Slaby  * hid_match_usage - check if driver's event should be called
83385cdaf52SJiri Slaby  *
83485cdaf52SJiri Slaby  * @hid: hid device
83585cdaf52SJiri Slaby  * @usage: usage to match against
83685cdaf52SJiri Slaby  *
83785cdaf52SJiri Slaby  * compare hid->driver->usage_table->usage_{type,code} to
83885cdaf52SJiri Slaby  * usage->usage_{type,code}
83985cdaf52SJiri Slaby  */
84085cdaf52SJiri Slaby static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
84185cdaf52SJiri Slaby {
84285cdaf52SJiri Slaby 	const struct hid_usage_id *id = hid->driver->usage_table;
84385cdaf52SJiri Slaby 
84485cdaf52SJiri Slaby 	if (!id) /* NULL means all */
84585cdaf52SJiri Slaby 		return 1;
84685cdaf52SJiri Slaby 
84785cdaf52SJiri Slaby 	for (; id->usage_type != HID_ANY_ID - 1; id++)
84885cdaf52SJiri Slaby 		if ((id->usage_hid == HID_ANY_ID ||
84985cdaf52SJiri Slaby 				id->usage_hid == usage->hid) &&
85085cdaf52SJiri Slaby 				(id->usage_type == HID_ANY_ID ||
85185cdaf52SJiri Slaby 				id->usage_type == usage->type) &&
85285cdaf52SJiri Slaby 				(id->usage_code == HID_ANY_ID ||
85385cdaf52SJiri Slaby 				 id->usage_code == usage->code))
85485cdaf52SJiri Slaby 			return 1;
85585cdaf52SJiri Slaby 	return 0;
85685cdaf52SJiri Slaby }
85785cdaf52SJiri Slaby 
85885cdaf52SJiri Slaby static void hid_process_event(struct hid_device *hid, struct hid_field *field,
85985cdaf52SJiri Slaby 		struct hid_usage *usage, __s32 value, int interrupt)
86085cdaf52SJiri Slaby {
86185cdaf52SJiri Slaby 	struct hid_driver *hdrv = hid->driver;
86285cdaf52SJiri Slaby 	int ret;
86385cdaf52SJiri Slaby 
864dde5845aSJiri Kosina 	hid_dump_input(usage, value);
86585cdaf52SJiri Slaby 
86685cdaf52SJiri Slaby 	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
86785cdaf52SJiri Slaby 		ret = hdrv->event(hid, field, usage, value);
86885cdaf52SJiri Slaby 		if (ret != 0) {
86985cdaf52SJiri Slaby 			if (ret < 0)
87085cdaf52SJiri Slaby 				dbg_hid("%s's event failed with %d\n",
87185cdaf52SJiri Slaby 						hdrv->name, ret);
87285cdaf52SJiri Slaby 			return;
87385cdaf52SJiri Slaby 		}
87485cdaf52SJiri Slaby 	}
87585cdaf52SJiri Slaby 
876dde5845aSJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
877dde5845aSJiri Kosina 		hidinput_hid_event(hid, field, usage, value);
878aa938f79SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
879aa938f79SJiri Kosina 		hid->hiddev_hid_event(hid, field, usage, value);
880dde5845aSJiri Kosina }
881dde5845aSJiri Kosina 
882dde5845aSJiri Kosina /*
883dde5845aSJiri Kosina  * Analyse a received field, and fetch the data from it. The field
884dde5845aSJiri Kosina  * content is stored for next report processing (we do differential
885dde5845aSJiri Kosina  * reporting to the layer).
886dde5845aSJiri Kosina  */
887dde5845aSJiri Kosina 
888abdff0f7SAdrian Bunk static void hid_input_field(struct hid_device *hid, struct hid_field *field,
889abdff0f7SAdrian Bunk 			    __u8 *data, int interrupt)
890dde5845aSJiri Kosina {
891dde5845aSJiri Kosina 	unsigned n;
892dde5845aSJiri Kosina 	unsigned count = field->report_count;
893dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
894dde5845aSJiri Kosina 	unsigned size = field->report_size;
895dde5845aSJiri Kosina 	__s32 min = field->logical_minimum;
896dde5845aSJiri Kosina 	__s32 max = field->logical_maximum;
897dde5845aSJiri Kosina 	__s32 *value;
898dde5845aSJiri Kosina 
899dde5845aSJiri Kosina 	if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
900dde5845aSJiri Kosina 		return;
901dde5845aSJiri Kosina 
902dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
903dde5845aSJiri Kosina 
904dde5845aSJiri Kosina 			value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
905dde5845aSJiri Kosina 						    extract(data, offset + n * size, size);
906dde5845aSJiri Kosina 
907dde5845aSJiri Kosina 			if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
908dde5845aSJiri Kosina 			    && value[n] >= min && value[n] <= max
909dde5845aSJiri Kosina 			    && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
910dde5845aSJiri Kosina 				goto exit;
911dde5845aSJiri Kosina 	}
912dde5845aSJiri Kosina 
913dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
914dde5845aSJiri Kosina 
915dde5845aSJiri Kosina 		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
916dde5845aSJiri Kosina 			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
917dde5845aSJiri Kosina 			continue;
918dde5845aSJiri Kosina 		}
919dde5845aSJiri Kosina 
920dde5845aSJiri Kosina 		if (field->value[n] >= min && field->value[n] <= max
921dde5845aSJiri Kosina 			&& field->usage[field->value[n] - min].hid
922dde5845aSJiri Kosina 			&& search(value, field->value[n], count))
923dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
924dde5845aSJiri Kosina 
925dde5845aSJiri Kosina 		if (value[n] >= min && value[n] <= max
926dde5845aSJiri Kosina 			&& field->usage[value[n] - min].hid
927dde5845aSJiri Kosina 			&& search(field->value, value[n], count))
928dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
929dde5845aSJiri Kosina 	}
930dde5845aSJiri Kosina 
931dde5845aSJiri Kosina 	memcpy(field->value, value, count * sizeof(__s32));
932dde5845aSJiri Kosina exit:
933dde5845aSJiri Kosina 	kfree(value);
934dde5845aSJiri Kosina }
935dde5845aSJiri Kosina 
936dde5845aSJiri Kosina /*
937dde5845aSJiri Kosina  * Output the field into the report.
938dde5845aSJiri Kosina  */
939dde5845aSJiri Kosina 
940dde5845aSJiri Kosina static void hid_output_field(struct hid_field *field, __u8 *data)
941dde5845aSJiri Kosina {
942dde5845aSJiri Kosina 	unsigned count = field->report_count;
943dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
944dde5845aSJiri Kosina 	unsigned size = field->report_size;
94546386b58SSimon Budig 	unsigned bitsused = offset + count * size;
946dde5845aSJiri Kosina 	unsigned n;
947dde5845aSJiri Kosina 
94846386b58SSimon Budig 	/* make sure the unused bits in the last byte are zeros */
94946386b58SSimon Budig 	if (count > 0 && size > 0 && (bitsused % 8) != 0)
95046386b58SSimon Budig 		data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
95146386b58SSimon Budig 
952dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
953dde5845aSJiri Kosina 		if (field->logical_minimum < 0)	/* signed values */
954dde5845aSJiri Kosina 			implement(data, offset + n * size, size, s32ton(field->value[n], size));
955dde5845aSJiri Kosina 		else				/* unsigned values */
956dde5845aSJiri Kosina 			implement(data, offset + n * size, size, field->value[n]);
957dde5845aSJiri Kosina 	}
958dde5845aSJiri Kosina }
959dde5845aSJiri Kosina 
960dde5845aSJiri Kosina /*
961dde5845aSJiri Kosina  * Create a report.
962dde5845aSJiri Kosina  */
963dde5845aSJiri Kosina 
964229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data)
965dde5845aSJiri Kosina {
966dde5845aSJiri Kosina 	unsigned n;
967dde5845aSJiri Kosina 
968dde5845aSJiri Kosina 	if (report->id > 0)
969dde5845aSJiri Kosina 		*data++ = report->id;
970dde5845aSJiri Kosina 
971dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
972dde5845aSJiri Kosina 		hid_output_field(report->field[n], data);
973dde5845aSJiri Kosina }
974229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report);
975dde5845aSJiri Kosina 
976dde5845aSJiri Kosina /*
977dde5845aSJiri Kosina  * Set a field value. The report this field belongs to has to be
978dde5845aSJiri Kosina  * created and transferred to the device, to set this value in the
979dde5845aSJiri Kosina  * device.
980dde5845aSJiri Kosina  */
981dde5845aSJiri Kosina 
982dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
983dde5845aSJiri Kosina {
984dde5845aSJiri Kosina 	unsigned size = field->report_size;
985dde5845aSJiri Kosina 
986dde5845aSJiri Kosina 	hid_dump_input(field->usage + offset, value);
987dde5845aSJiri Kosina 
988dde5845aSJiri Kosina 	if (offset >= field->report_count) {
98958037eb9SJiri Kosina 		dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
990dde5845aSJiri Kosina 		hid_dump_field(field, 8);
991dde5845aSJiri Kosina 		return -1;
992dde5845aSJiri Kosina 	}
993dde5845aSJiri Kosina 	if (field->logical_minimum < 0) {
994dde5845aSJiri Kosina 		if (value != snto32(s32ton(value, size), size)) {
99558037eb9SJiri Kosina 			dbg_hid("value %d is out of range\n", value);
996dde5845aSJiri Kosina 			return -1;
997dde5845aSJiri Kosina 		}
998dde5845aSJiri Kosina 	}
999dde5845aSJiri Kosina 	field->value[offset] = value;
1000dde5845aSJiri Kosina 	return 0;
1001dde5845aSJiri Kosina }
1002229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field);
1003dde5845aSJiri Kosina 
100485cdaf52SJiri Slaby static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
100585cdaf52SJiri Slaby 		const u8 *data)
100685cdaf52SJiri Slaby {
100785cdaf52SJiri Slaby 	struct hid_report *report;
100885cdaf52SJiri Slaby 	unsigned int n = 0;	/* Normally report number is 0 */
100985cdaf52SJiri Slaby 
101085cdaf52SJiri Slaby 	/* Device uses numbered reports, data[0] is report number */
101185cdaf52SJiri Slaby 	if (report_enum->numbered)
101285cdaf52SJiri Slaby 		n = *data;
101385cdaf52SJiri Slaby 
101485cdaf52SJiri Slaby 	report = report_enum->report_id_hash[n];
101585cdaf52SJiri Slaby 	if (report == NULL)
101685cdaf52SJiri Slaby 		dbg_hid("undefined report_id %u received\n", n);
101785cdaf52SJiri Slaby 
101885cdaf52SJiri Slaby 	return report;
101985cdaf52SJiri Slaby }
102085cdaf52SJiri Slaby 
102185cdaf52SJiri Slaby void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
102285cdaf52SJiri Slaby 		int interrupt)
1023aa8de2f0SJiri Kosina {
1024aa8de2f0SJiri Kosina 	struct hid_report_enum *report_enum = hid->report_enum + type;
1025aa8de2f0SJiri Kosina 	struct hid_report *report;
102685cdaf52SJiri Slaby 	unsigned int a;
102785cdaf52SJiri Slaby 	int rsize, csize = size;
102885cdaf52SJiri Slaby 	u8 *cdata = data;
1029aa8de2f0SJiri Kosina 
103085cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
103185cdaf52SJiri Slaby 	if (!report)
103285cdaf52SJiri Slaby 		return;
1033aa8de2f0SJiri Kosina 
103485cdaf52SJiri Slaby 	if (report_enum->numbered) {
103585cdaf52SJiri Slaby 		cdata++;
103685cdaf52SJiri Slaby 		csize--;
1037aa8de2f0SJiri Kosina 	}
1038aa8de2f0SJiri Kosina 
1039aa8de2f0SJiri Kosina 	rsize = ((report->size - 1) >> 3) + 1;
1040aa8de2f0SJiri Kosina 
104185cdaf52SJiri Slaby 	if (csize < rsize) {
104285cdaf52SJiri Slaby 		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
104385cdaf52SJiri Slaby 				csize, rsize);
104485cdaf52SJiri Slaby 		memset(cdata + csize, 0, rsize - csize);
1045aa8de2f0SJiri Kosina 	}
1046aa8de2f0SJiri Kosina 
1047aa8de2f0SJiri Kosina 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1048aa8de2f0SJiri Kosina 		hid->hiddev_report_event(hid, report);
1049b54ec3c1SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1050b54ec3c1SJiri Kosina 		/* numbered reports need to be passed with the report num */
1051b54ec3c1SJiri Kosina 		if (report_enum->numbered)
1052b54ec3c1SJiri Kosina 			hidraw_report_event(hid, data - 1, size + 1);
1053b54ec3c1SJiri Kosina 		else
105486166b7bSJiri Kosina 			hidraw_report_event(hid, data, size);
1055b54ec3c1SJiri Kosina 	}
1056aa8de2f0SJiri Kosina 
105785cdaf52SJiri Slaby 	for (a = 0; a < report->maxfield; a++)
105885cdaf52SJiri Slaby 		hid_input_field(hid, report->field[a], cdata, interrupt);
1059aa8de2f0SJiri Kosina 
1060aa8de2f0SJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1061aa8de2f0SJiri Kosina 		hidinput_report_event(hid, report);
106285cdaf52SJiri Slaby }
106385cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_report_raw_event);
106485cdaf52SJiri Slaby 
106585cdaf52SJiri Slaby /**
106685cdaf52SJiri Slaby  * hid_input_report - report data from lower layer (usb, bt...)
106785cdaf52SJiri Slaby  *
106885cdaf52SJiri Slaby  * @hid: hid device
106985cdaf52SJiri Slaby  * @type: HID report type (HID_*_REPORT)
107085cdaf52SJiri Slaby  * @data: report contents
107185cdaf52SJiri Slaby  * @size: size of data parameter
107285cdaf52SJiri Slaby  * @interrupt: called from atomic?
107385cdaf52SJiri Slaby  *
107485cdaf52SJiri Slaby  * This is data entry for lower layers.
107585cdaf52SJiri Slaby  */
107685cdaf52SJiri Slaby int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
107785cdaf52SJiri Slaby {
107876c317d6SJulia Lawall 	struct hid_report_enum *report_enum;
107976c317d6SJulia Lawall 	struct hid_driver *hdrv;
108085cdaf52SJiri Slaby 	struct hid_report *report;
108185cdaf52SJiri Slaby 	unsigned int i;
108285cdaf52SJiri Slaby 	int ret;
108385cdaf52SJiri Slaby 
108485cdaf52SJiri Slaby 	if (!hid || !hid->driver)
108585cdaf52SJiri Slaby 		return -ENODEV;
108676c317d6SJulia Lawall 	report_enum = hid->report_enum + type;
108776c317d6SJulia Lawall 	hdrv = hid->driver;
108885cdaf52SJiri Slaby 
108985cdaf52SJiri Slaby 	if (!size) {
109085cdaf52SJiri Slaby 		dbg_hid("empty report\n");
109185cdaf52SJiri Slaby 		return -1;
109285cdaf52SJiri Slaby 	}
109385cdaf52SJiri Slaby 
109485cdaf52SJiri Slaby 	dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
109585cdaf52SJiri Slaby 
109685cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
109785cdaf52SJiri Slaby 	if (!report)
109885cdaf52SJiri Slaby 		return -1;
109985cdaf52SJiri Slaby 
110085cdaf52SJiri Slaby 	/* dump the report */
110185cdaf52SJiri Slaby 	dbg_hid("report %d (size %u) = ", report->id, size);
110285cdaf52SJiri Slaby 	for (i = 0; i < size; i++)
110385cdaf52SJiri Slaby 		dbg_hid_line(" %02x", data[i]);
110485cdaf52SJiri Slaby 	dbg_hid_line("\n");
110585cdaf52SJiri Slaby 
110685cdaf52SJiri Slaby 	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
110785cdaf52SJiri Slaby 		ret = hdrv->raw_event(hid, report, data, size);
110885cdaf52SJiri Slaby 		if (ret != 0)
110985cdaf52SJiri Slaby 			return ret < 0 ? ret : 0;
111085cdaf52SJiri Slaby 	}
111185cdaf52SJiri Slaby 
111285cdaf52SJiri Slaby 	hid_report_raw_event(hid, type, data, size, interrupt);
1113aa8de2f0SJiri Kosina 
1114aa8de2f0SJiri Kosina 	return 0;
1115aa8de2f0SJiri Kosina }
1116aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report);
1117aa8de2f0SJiri Kosina 
11180f37cd03SJiri Kosina static bool hid_match_one_id(struct hid_device *hdev,
11190f37cd03SJiri Kosina 		const struct hid_device_id *id)
11200f37cd03SJiri Kosina {
11210f37cd03SJiri Kosina 	return id->bus == hdev->bus &&
11220f37cd03SJiri Kosina 		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
11230f37cd03SJiri Kosina 		(id->product == HID_ANY_ID || id->product == hdev->product);
11240f37cd03SJiri Kosina }
11250f37cd03SJiri Kosina 
11260f37cd03SJiri Kosina static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
11270f37cd03SJiri Kosina 		const struct hid_device_id *id)
11280f37cd03SJiri Kosina {
11290f37cd03SJiri Kosina 	for (; id->bus; id++)
11300f37cd03SJiri Kosina 		if (hid_match_one_id(hdev, id))
11310f37cd03SJiri Kosina 			return id;
11320f37cd03SJiri Kosina 
11330f37cd03SJiri Kosina 	return NULL;
11340f37cd03SJiri Kosina }
11350f37cd03SJiri Kosina 
11360f37cd03SJiri Kosina static const struct hid_device_id hid_hiddev_list[] = {
1137c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1138c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
11390f37cd03SJiri Kosina 	{ }
11400f37cd03SJiri Kosina };
11410f37cd03SJiri Kosina 
11420f37cd03SJiri Kosina static bool hid_hiddev(struct hid_device *hdev)
11430f37cd03SJiri Kosina {
11440f37cd03SJiri Kosina 	return !!hid_match_id(hdev, hid_hiddev_list);
11450f37cd03SJiri Kosina }
11460f37cd03SJiri Kosina 
114793c10132SJiri Slaby int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
114893c10132SJiri Slaby {
114993c10132SJiri Slaby 	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
115093c10132SJiri Slaby 		"Joystick", "Gamepad", "Keyboard", "Keypad",
115193c10132SJiri Slaby 		"Multi-Axis Controller"
115293c10132SJiri Slaby 	};
115393c10132SJiri Slaby 	const char *type, *bus;
115493c10132SJiri Slaby 	char buf[64];
115593c10132SJiri Slaby 	unsigned int i;
115693c10132SJiri Slaby 	int len;
115793c10132SJiri Slaby 
115893c10132SJiri Slaby 	if (hdev->bus != BUS_USB)
115993c10132SJiri Slaby 		connect_mask &= ~HID_CONNECT_HIDDEV;
11600f37cd03SJiri Kosina 	if (hid_hiddev(hdev))
11610f37cd03SJiri Kosina 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
116293c10132SJiri Slaby 
116393c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
116493c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
116593c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_INPUT;
116693c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
116793c10132SJiri Slaby 			!hdev->hiddev_connect(hdev,
116893c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDDEV_FORCE))
116993c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDDEV;
117093c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
117193c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDRAW;
117293c10132SJiri Slaby 
117393c10132SJiri Slaby 	if (!hdev->claimed) {
117493c10132SJiri Slaby 		dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
117593c10132SJiri Slaby 				"hidraw\n");
117693c10132SJiri Slaby 		return -ENODEV;
117793c10132SJiri Slaby 	}
117893c10132SJiri Slaby 
117993c10132SJiri Slaby 	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
118093c10132SJiri Slaby 			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
118193c10132SJiri Slaby 		hdev->ff_init(hdev);
118293c10132SJiri Slaby 
118393c10132SJiri Slaby 	len = 0;
118493c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_INPUT)
118593c10132SJiri Slaby 		len += sprintf(buf + len, "input");
118693c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
118793c10132SJiri Slaby 		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
118893c10132SJiri Slaby 				hdev->minor);
118993c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
119093c10132SJiri Slaby 		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
119193c10132SJiri Slaby 				((struct hidraw *)hdev->hidraw)->minor);
119293c10132SJiri Slaby 
119393c10132SJiri Slaby 	type = "Device";
119493c10132SJiri Slaby 	for (i = 0; i < hdev->maxcollection; i++) {
119593c10132SJiri Slaby 		struct hid_collection *col = &hdev->collection[i];
119693c10132SJiri Slaby 		if (col->type == HID_COLLECTION_APPLICATION &&
119793c10132SJiri Slaby 		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
119893c10132SJiri Slaby 		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
119993c10132SJiri Slaby 			type = types[col->usage & 0xffff];
120093c10132SJiri Slaby 			break;
120193c10132SJiri Slaby 		}
120293c10132SJiri Slaby 	}
120393c10132SJiri Slaby 
120493c10132SJiri Slaby 	switch (hdev->bus) {
120593c10132SJiri Slaby 	case BUS_USB:
120693c10132SJiri Slaby 		bus = "USB";
120793c10132SJiri Slaby 		break;
120893c10132SJiri Slaby 	case BUS_BLUETOOTH:
120993c10132SJiri Slaby 		bus = "BLUETOOTH";
121093c10132SJiri Slaby 		break;
121193c10132SJiri Slaby 	default:
121293c10132SJiri Slaby 		bus = "<UNKNOWN>";
121393c10132SJiri Slaby 	}
121493c10132SJiri Slaby 
121593c10132SJiri Slaby 	dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
121693c10132SJiri Slaby 			buf, bus, hdev->version >> 8, hdev->version & 0xff,
121793c10132SJiri Slaby 			type, hdev->name, hdev->phys);
121893c10132SJiri Slaby 
121993c10132SJiri Slaby 	return 0;
122093c10132SJiri Slaby }
122193c10132SJiri Slaby EXPORT_SYMBOL_GPL(hid_connect);
122293c10132SJiri Slaby 
1223bae7eb33SJiri Kosina /* a list of devices for which there is a specialized driver on HID bus */
122485cdaf52SJiri Slaby static const struct hid_device_id hid_blacklist[] = {
122514a21cd4SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
122614a21cd4SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1227df9bcaceSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
12288c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
12298c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
12308c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
12318c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
12328c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
12338c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
12348c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
12358c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
12368c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
12378c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
12388c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
12398c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
12408c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1241fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1242fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1243fef3f571SRyan Finnie 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
12448c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
12458c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
12468c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
12478c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
12488c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
12498c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1250ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1251ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1252ee8a1a0aSJan Scholz 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
12538c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
12548c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
12558c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
12568c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
12578c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
12588c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1259a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1260a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1261a96d6ef3SHenrik Rydberg 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
12628c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
12638c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1264b5635b12SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
12653b239cd7SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1266fcfacfd3SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
12670f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
12680f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
12690f221320SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
12703f866fbdSRichard Walmsley 	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
12711f243e30SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
12725181e594SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1273578f3a35SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
12745181e594SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
127542859e0bSLukasz Lubojanski 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1276949f8fefSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
12771e093206SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1278fdf93aa3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
127979422741SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
1280b5635b12SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
12815f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
12825f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
12835f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
12845f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
12855f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
12865f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
12875f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
12885f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
12895f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
12905f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
12915f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1292606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1293606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1294606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1295fd30ea8cSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
1296606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1297606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1298606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1299243b706dSChristophe Borivant 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
1300606bd0a8SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
130178a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
130278a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
130378a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
130478a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
130578a849a6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
13063b8006e5SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
130794011f93SRafi Rubin 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
13081e762532SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1309980a3da6SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1310bd28ce00SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1311cc6e0bbbSJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
131290231e7eSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1313daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1314daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
13157a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
13167a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
1317daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1318daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1319f14f526dSLev Babiev 	{ HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
1320711a680eSBruno Premont 	{ HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
1321fac733f0SJussi Kivilinna 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
1322ca2dcd40SBastien Nocera 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
1323daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1324daedb3d6SAnssi Hannula 	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
13258c19a515SJiri Slaby 
13268c19a515SJiri Slaby 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
132778a849a6SJiri Slaby 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
132885cdaf52SJiri Slaby 	{ }
132985cdaf52SJiri Slaby };
133085cdaf52SJiri Slaby 
13313a6f82f7SJiri Slaby struct hid_dynid {
13323a6f82f7SJiri Slaby 	struct list_head list;
13333a6f82f7SJiri Slaby 	struct hid_device_id id;
13343a6f82f7SJiri Slaby };
13353a6f82f7SJiri Slaby 
13363a6f82f7SJiri Slaby /**
13373a6f82f7SJiri Slaby  * store_new_id - add a new HID device ID to this driver and re-probe devices
13383a6f82f7SJiri Slaby  * @driver: target device driver
13393a6f82f7SJiri Slaby  * @buf: buffer for scanning device ID data
13403a6f82f7SJiri Slaby  * @count: input size
13413a6f82f7SJiri Slaby  *
13423a6f82f7SJiri Slaby  * Adds a new dynamic hid device ID to this driver,
13433a6f82f7SJiri Slaby  * and causes the driver to probe for all devices again.
13443a6f82f7SJiri Slaby  */
13453a6f82f7SJiri Slaby static ssize_t store_new_id(struct device_driver *drv, const char *buf,
13463a6f82f7SJiri Slaby 		size_t count)
13473a6f82f7SJiri Slaby {
13483a6f82f7SJiri Slaby 	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
13493a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
13503a6f82f7SJiri Slaby 	__u32 bus, vendor, product;
13513a6f82f7SJiri Slaby 	unsigned long driver_data = 0;
13523a6f82f7SJiri Slaby 	int ret;
13533a6f82f7SJiri Slaby 
13543a6f82f7SJiri Slaby 	ret = sscanf(buf, "%x %x %x %lx",
13553a6f82f7SJiri Slaby 			&bus, &vendor, &product, &driver_data);
13563a6f82f7SJiri Slaby 	if (ret < 3)
13573a6f82f7SJiri Slaby 		return -EINVAL;
13583a6f82f7SJiri Slaby 
13593a6f82f7SJiri Slaby 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
13603a6f82f7SJiri Slaby 	if (!dynid)
13613a6f82f7SJiri Slaby 		return -ENOMEM;
13623a6f82f7SJiri Slaby 
13633a6f82f7SJiri Slaby 	dynid->id.bus = bus;
13643a6f82f7SJiri Slaby 	dynid->id.vendor = vendor;
13653a6f82f7SJiri Slaby 	dynid->id.product = product;
13663a6f82f7SJiri Slaby 	dynid->id.driver_data = driver_data;
13673a6f82f7SJiri Slaby 
13683a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
13693a6f82f7SJiri Slaby 	list_add_tail(&dynid->list, &hdrv->dyn_list);
13703a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
13713a6f82f7SJiri Slaby 
13723a6f82f7SJiri Slaby 	ret = 0;
13733a6f82f7SJiri Slaby 	if (get_driver(&hdrv->driver)) {
13743a6f82f7SJiri Slaby 		ret = driver_attach(&hdrv->driver);
13753a6f82f7SJiri Slaby 		put_driver(&hdrv->driver);
13763a6f82f7SJiri Slaby 	}
13773a6f82f7SJiri Slaby 
13783a6f82f7SJiri Slaby 	return ret ? : count;
13793a6f82f7SJiri Slaby }
13803a6f82f7SJiri Slaby static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
13813a6f82f7SJiri Slaby 
13823a6f82f7SJiri Slaby static void hid_free_dynids(struct hid_driver *hdrv)
13833a6f82f7SJiri Slaby {
13843a6f82f7SJiri Slaby 	struct hid_dynid *dynid, *n;
13853a6f82f7SJiri Slaby 
13863a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
13873a6f82f7SJiri Slaby 	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
13883a6f82f7SJiri Slaby 		list_del(&dynid->list);
13893a6f82f7SJiri Slaby 		kfree(dynid);
13903a6f82f7SJiri Slaby 	}
13913a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
13923a6f82f7SJiri Slaby }
13933a6f82f7SJiri Slaby 
13943a6f82f7SJiri Slaby static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
13953a6f82f7SJiri Slaby 		struct hid_driver *hdrv)
13963a6f82f7SJiri Slaby {
13973a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
13983a6f82f7SJiri Slaby 
13993a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
14003a6f82f7SJiri Slaby 	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
14013a6f82f7SJiri Slaby 		if (hid_match_one_id(hdev, &dynid->id)) {
14023a6f82f7SJiri Slaby 			spin_unlock(&hdrv->dyn_lock);
14033a6f82f7SJiri Slaby 			return &dynid->id;
14043a6f82f7SJiri Slaby 		}
14053a6f82f7SJiri Slaby 	}
14063a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
14073a6f82f7SJiri Slaby 
14083a6f82f7SJiri Slaby 	return hid_match_id(hdev, hdrv->id_table);
14093a6f82f7SJiri Slaby }
14103a6f82f7SJiri Slaby 
141185cdaf52SJiri Slaby static int hid_bus_match(struct device *dev, struct device_driver *drv)
141285cdaf52SJiri Slaby {
141385cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
141485cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
141585cdaf52SJiri Slaby 
14163a6f82f7SJiri Slaby 	if (!hid_match_device(hdev, hdrv))
141785cdaf52SJiri Slaby 		return 0;
141885cdaf52SJiri Slaby 
141985cdaf52SJiri Slaby 	/* generic wants all non-blacklisted */
142085cdaf52SJiri Slaby 	if (!strncmp(hdrv->name, "generic-", 8))
142185cdaf52SJiri Slaby 		return !hid_match_id(hdev, hid_blacklist);
142285cdaf52SJiri Slaby 
142385cdaf52SJiri Slaby 	return 1;
142485cdaf52SJiri Slaby }
142585cdaf52SJiri Slaby 
142685cdaf52SJiri Slaby static int hid_device_probe(struct device *dev)
142785cdaf52SJiri Slaby {
142885cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(dev->driver,
142985cdaf52SJiri Slaby 			struct hid_driver, driver);
143085cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
143185cdaf52SJiri Slaby 	const struct hid_device_id *id;
143285cdaf52SJiri Slaby 	int ret = 0;
143385cdaf52SJiri Slaby 
143485cdaf52SJiri Slaby 	if (!hdev->driver) {
14353a6f82f7SJiri Slaby 		id = hid_match_device(hdev, hdrv);
1436c500c971SJiri Slaby 		if (id == NULL)
1437c500c971SJiri Slaby 			return -ENODEV;
1438c500c971SJiri Slaby 
143985cdaf52SJiri Slaby 		hdev->driver = hdrv;
1440c500c971SJiri Slaby 		if (hdrv->probe) {
1441c500c971SJiri Slaby 			ret = hdrv->probe(hdev, id);
1442c500c971SJiri Slaby 		} else { /* default probe */
1443c500c971SJiri Slaby 			ret = hid_parse(hdev);
1444c500c971SJiri Slaby 			if (!ret)
144593c10132SJiri Slaby 				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1446c500c971SJiri Slaby 		}
1447c500c971SJiri Slaby 		if (ret)
1448c500c971SJiri Slaby 			hdev->driver = NULL;
144985cdaf52SJiri Slaby 	}
145085cdaf52SJiri Slaby 	return ret;
145185cdaf52SJiri Slaby }
145285cdaf52SJiri Slaby 
145385cdaf52SJiri Slaby static int hid_device_remove(struct device *dev)
145485cdaf52SJiri Slaby {
145585cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
145685cdaf52SJiri Slaby 	struct hid_driver *hdrv = hdev->driver;
145785cdaf52SJiri Slaby 
145885cdaf52SJiri Slaby 	if (hdrv) {
145985cdaf52SJiri Slaby 		if (hdrv->remove)
146085cdaf52SJiri Slaby 			hdrv->remove(hdev);
1461c500c971SJiri Slaby 		else /* default remove */
1462c500c971SJiri Slaby 			hid_hw_stop(hdev);
146385cdaf52SJiri Slaby 		hdev->driver = NULL;
146485cdaf52SJiri Slaby 	}
146585cdaf52SJiri Slaby 
146685cdaf52SJiri Slaby 	return 0;
146785cdaf52SJiri Slaby }
146885cdaf52SJiri Slaby 
146985cdaf52SJiri Slaby static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
147085cdaf52SJiri Slaby {
147185cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
147285cdaf52SJiri Slaby 
147385cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
147485cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
147585cdaf52SJiri Slaby 		return -ENOMEM;
147685cdaf52SJiri Slaby 
147785cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
147885cdaf52SJiri Slaby 		return -ENOMEM;
147985cdaf52SJiri Slaby 
148085cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
148185cdaf52SJiri Slaby 		return -ENOMEM;
148285cdaf52SJiri Slaby 
148385cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
148485cdaf52SJiri Slaby 		return -ENOMEM;
148585cdaf52SJiri Slaby 
148685cdaf52SJiri Slaby 	if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
148785cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
148885cdaf52SJiri Slaby 		return -ENOMEM;
148985cdaf52SJiri Slaby 
149085cdaf52SJiri Slaby 	return 0;
149185cdaf52SJiri Slaby }
149285cdaf52SJiri Slaby 
149385cdaf52SJiri Slaby static struct bus_type hid_bus_type = {
149485cdaf52SJiri Slaby 	.name		= "hid",
149585cdaf52SJiri Slaby 	.match		= hid_bus_match,
149685cdaf52SJiri Slaby 	.probe		= hid_device_probe,
149785cdaf52SJiri Slaby 	.remove		= hid_device_remove,
149885cdaf52SJiri Slaby 	.uevent		= hid_uevent,
149985cdaf52SJiri Slaby };
150085cdaf52SJiri Slaby 
1501bae7eb33SJiri Kosina /* a list of devices that shouldn't be handled by HID core at all */
1502d458a9dfSJiri Slaby static const struct hid_device_id hid_ignore_list[] = {
1503d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1504d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1505d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1506d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1507d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1508d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1509d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1510d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1511d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1512d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1513d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1514d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1515d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1516ac2d9899SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2)},
151762a56582SAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1518d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1519d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1520d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1521d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1522d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1523d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
15245f6108cfSAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
1525d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1526d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1527d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1528d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1529d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1530d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1531d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1532d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1533d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1534d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1535d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1536d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1537d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1538d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1539d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1540d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1541d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1542d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1543d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1544d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1545d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1546d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1547d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1548d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1549d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1550d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1551d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1552d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1553d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1554d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1555d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1556d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1557d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1558d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1559d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1560d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1561d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1562d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1563d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1564d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1565d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1566d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1567d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1568d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1569d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1570d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1571d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1572d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1573d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1574d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1575d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1576d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1577d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1578d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1579d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1580d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1581d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1582d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1583d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1584d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1585d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1586d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1587d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1588d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1589d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1590d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1591d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1592d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1593d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1594c91c21c5SAlexey Klimov 	{ HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
1595d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1596d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1597d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1598d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1599d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1600d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1601d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1602d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1603d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1604d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1605d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1606d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1607d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1608d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1609d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1610d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1611d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1612d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1613d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1614d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1615d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1616d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1617d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1618d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1619d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1620d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1621d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1622d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1623d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1624d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1625d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1626d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1627d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1628d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1629*4cfae3e8SHenning Glawe 	{ HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
163035cfd1d9SMichael Tokarev 	{ HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
1631d1d3a5f6SRemi Cattiau 	{ HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY1) },
1632d1d3a5f6SRemi Cattiau 	{ HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY2) },
1633d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1634d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1635d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1636d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1637d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1638d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1639d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1640d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1641d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1642d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1643d458a9dfSJiri Slaby 	{ }
1644d458a9dfSJiri Slaby };
1645d458a9dfSJiri Slaby 
1646b4d8e473SJiri Slaby /**
1647b4d8e473SJiri Slaby  * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1648b4d8e473SJiri Slaby  *
1649b4d8e473SJiri Slaby  * There are composite devices for which we want to ignore only a certain
1650b4d8e473SJiri Slaby  * interface. This is a list of devices for which only the mouse interface will
1651b4d8e473SJiri Slaby  * be ignored. This allows a dedicated driver to take care of the interface.
1652b4d8e473SJiri Slaby  */
1653b4d8e473SJiri Slaby static const struct hid_device_id hid_mouse_ignore_list[] = {
1654b4d8e473SJiri Slaby 	/* appletouch driver */
1655b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1656b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1657b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1658b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1659b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1660b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1661b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1662b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1663b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1664b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1665b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1666b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1667b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1668b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1669b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1670b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1671b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1672b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1673b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1674b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1675ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1676ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1677ac26fca3SJiri Kosina 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1678b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1679b4d8e473SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1680b4d8e473SJiri Slaby 	{ }
1681b4d8e473SJiri Slaby };
1682b4d8e473SJiri Slaby 
1683d458a9dfSJiri Slaby static bool hid_ignore(struct hid_device *hdev)
1684d458a9dfSJiri Slaby {
1685d458a9dfSJiri Slaby 	switch (hdev->vendor) {
1686d458a9dfSJiri Slaby 	case USB_VENDOR_ID_CODEMERCS:
1687d458a9dfSJiri Slaby 		/* ignore all Code Mercenaries IOWarrior devices */
1688d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1689d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1690d458a9dfSJiri Slaby 			return true;
1691d458a9dfSJiri Slaby 		break;
1692d458a9dfSJiri Slaby 	case USB_VENDOR_ID_LOGITECH:
1693d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1694d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1695d458a9dfSJiri Slaby 			return true;
1696d458a9dfSJiri Slaby 		break;
169731f7fd79SJarod Wilson 	case USB_VENDOR_ID_SOUNDGRAPH:
169831f7fd79SJarod Wilson 		if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
169931f7fd79SJarod Wilson 		    hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
170031f7fd79SJarod Wilson 			return true;
170131f7fd79SJarod Wilson 		break;
1702d458a9dfSJiri Slaby 	}
1703d458a9dfSJiri Slaby 
1704b4d8e473SJiri Slaby 	if (hdev->type == HID_TYPE_USBMOUSE &&
1705b4d8e473SJiri Slaby 			hid_match_id(hdev, hid_mouse_ignore_list))
1706b4d8e473SJiri Slaby 		return true;
1707b4d8e473SJiri Slaby 
1708d458a9dfSJiri Slaby 	return !!hid_match_id(hdev, hid_ignore_list);
1709d458a9dfSJiri Slaby }
1710d458a9dfSJiri Slaby 
171185cdaf52SJiri Slaby int hid_add_device(struct hid_device *hdev)
171285cdaf52SJiri Slaby {
171385cdaf52SJiri Slaby 	static atomic_t id = ATOMIC_INIT(0);
171485cdaf52SJiri Slaby 	int ret;
171585cdaf52SJiri Slaby 
171685cdaf52SJiri Slaby 	if (WARN_ON(hdev->status & HID_STAT_ADDED))
171785cdaf52SJiri Slaby 		return -EBUSY;
171885cdaf52SJiri Slaby 
1719d458a9dfSJiri Slaby 	/* we need to kill them here, otherwise they will stay allocated to
1720d458a9dfSJiri Slaby 	 * wait for coming driver */
1721d458a9dfSJiri Slaby 	if (hid_ignore(hdev))
1722d458a9dfSJiri Slaby 		return -ENODEV;
1723d458a9dfSJiri Slaby 
17246bbe586fSKay Sievers 	/* XXX hack, any other cleaner solution after the driver core
17256bbe586fSKay Sievers 	 * is converted to allow more than 20 bytes as the device name? */
17266bbe586fSKay Sievers 	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
172785cdaf52SJiri Slaby 		     hdev->vendor, hdev->product, atomic_inc_return(&id));
172885cdaf52SJiri Slaby 
172985cdaf52SJiri Slaby 	ret = device_add(&hdev->dev);
173085cdaf52SJiri Slaby 	if (!ret)
173185cdaf52SJiri Slaby 		hdev->status |= HID_STAT_ADDED;
173285cdaf52SJiri Slaby 
173385cdaf52SJiri Slaby 	return ret;
173485cdaf52SJiri Slaby }
173585cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_add_device);
173685cdaf52SJiri Slaby 
173785cdaf52SJiri Slaby /**
173885cdaf52SJiri Slaby  * hid_allocate_device - allocate new hid device descriptor
173985cdaf52SJiri Slaby  *
174085cdaf52SJiri Slaby  * Allocate and initialize hid device, so that hid_destroy_device might be
174185cdaf52SJiri Slaby  * used to free it.
174285cdaf52SJiri Slaby  *
174385cdaf52SJiri Slaby  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
174485cdaf52SJiri Slaby  * error value.
174585cdaf52SJiri Slaby  */
174685cdaf52SJiri Slaby struct hid_device *hid_allocate_device(void)
174785cdaf52SJiri Slaby {
174885cdaf52SJiri Slaby 	struct hid_device *hdev;
174985cdaf52SJiri Slaby 	unsigned int i;
175085cdaf52SJiri Slaby 	int ret = -ENOMEM;
175185cdaf52SJiri Slaby 
175285cdaf52SJiri Slaby 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
175385cdaf52SJiri Slaby 	if (hdev == NULL)
175485cdaf52SJiri Slaby 		return ERR_PTR(ret);
175585cdaf52SJiri Slaby 
175685cdaf52SJiri Slaby 	device_initialize(&hdev->dev);
175785cdaf52SJiri Slaby 	hdev->dev.release = hid_device_release;
175885cdaf52SJiri Slaby 	hdev->dev.bus = &hid_bus_type;
175985cdaf52SJiri Slaby 
176085cdaf52SJiri Slaby 	hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
176185cdaf52SJiri Slaby 			sizeof(struct hid_collection), GFP_KERNEL);
176285cdaf52SJiri Slaby 	if (hdev->collection == NULL)
176385cdaf52SJiri Slaby 		goto err;
176485cdaf52SJiri Slaby 	hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
176585cdaf52SJiri Slaby 
176685cdaf52SJiri Slaby 	for (i = 0; i < HID_REPORT_TYPES; i++)
176785cdaf52SJiri Slaby 		INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
176885cdaf52SJiri Slaby 
176985cdaf52SJiri Slaby 	return hdev;
177085cdaf52SJiri Slaby err:
177185cdaf52SJiri Slaby 	put_device(&hdev->dev);
177285cdaf52SJiri Slaby 	return ERR_PTR(ret);
177385cdaf52SJiri Slaby }
177485cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_allocate_device);
177585cdaf52SJiri Slaby 
177685cdaf52SJiri Slaby static void hid_remove_device(struct hid_device *hdev)
177785cdaf52SJiri Slaby {
177885cdaf52SJiri Slaby 	if (hdev->status & HID_STAT_ADDED) {
177985cdaf52SJiri Slaby 		device_del(&hdev->dev);
178085cdaf52SJiri Slaby 		hdev->status &= ~HID_STAT_ADDED;
178185cdaf52SJiri Slaby 	}
178285cdaf52SJiri Slaby }
178385cdaf52SJiri Slaby 
178485cdaf52SJiri Slaby /**
178585cdaf52SJiri Slaby  * hid_destroy_device - free previously allocated device
178685cdaf52SJiri Slaby  *
178785cdaf52SJiri Slaby  * @hdev: hid device
178885cdaf52SJiri Slaby  *
178985cdaf52SJiri Slaby  * If you allocate hid_device through hid_allocate_device, you should ever
179085cdaf52SJiri Slaby  * free by this function.
179185cdaf52SJiri Slaby  */
179285cdaf52SJiri Slaby void hid_destroy_device(struct hid_device *hdev)
179385cdaf52SJiri Slaby {
179485cdaf52SJiri Slaby 	hid_remove_device(hdev);
179585cdaf52SJiri Slaby 	put_device(&hdev->dev);
179685cdaf52SJiri Slaby }
179785cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_destroy_device);
179885cdaf52SJiri Slaby 
179985cdaf52SJiri Slaby int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
180085cdaf52SJiri Slaby 		const char *mod_name)
180185cdaf52SJiri Slaby {
18023a6f82f7SJiri Slaby 	int ret;
18033a6f82f7SJiri Slaby 
180485cdaf52SJiri Slaby 	hdrv->driver.name = hdrv->name;
180585cdaf52SJiri Slaby 	hdrv->driver.bus = &hid_bus_type;
180685cdaf52SJiri Slaby 	hdrv->driver.owner = owner;
180785cdaf52SJiri Slaby 	hdrv->driver.mod_name = mod_name;
180885cdaf52SJiri Slaby 
18093a6f82f7SJiri Slaby 	INIT_LIST_HEAD(&hdrv->dyn_list);
18103a6f82f7SJiri Slaby 	spin_lock_init(&hdrv->dyn_lock);
18113a6f82f7SJiri Slaby 
18123a6f82f7SJiri Slaby 	ret = driver_register(&hdrv->driver);
18133a6f82f7SJiri Slaby 	if (ret)
18143a6f82f7SJiri Slaby 		return ret;
18153a6f82f7SJiri Slaby 
18163a6f82f7SJiri Slaby 	ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
18173a6f82f7SJiri Slaby 	if (ret)
18183a6f82f7SJiri Slaby 		driver_unregister(&hdrv->driver);
18193a6f82f7SJiri Slaby 
18203a6f82f7SJiri Slaby 	return ret;
182185cdaf52SJiri Slaby }
182285cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(__hid_register_driver);
182385cdaf52SJiri Slaby 
182485cdaf52SJiri Slaby void hid_unregister_driver(struct hid_driver *hdrv)
182585cdaf52SJiri Slaby {
18263a6f82f7SJiri Slaby 	driver_remove_file(&hdrv->driver, &driver_attr_new_id);
182785cdaf52SJiri Slaby 	driver_unregister(&hdrv->driver);
18283a6f82f7SJiri Slaby 	hid_free_dynids(hdrv);
182985cdaf52SJiri Slaby }
183085cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_unregister_driver);
183185cdaf52SJiri Slaby 
18320361a28dSOliver Neukum int hid_check_keys_pressed(struct hid_device *hid)
18330361a28dSOliver Neukum {
18340361a28dSOliver Neukum 	struct hid_input *hidinput;
18350361a28dSOliver Neukum 	int i;
18360361a28dSOliver Neukum 
1837e5288eb5SJiri Kosina 	if (!(hid->claimed & HID_CLAIMED_INPUT))
1838e5288eb5SJiri Kosina 		return 0;
1839e5288eb5SJiri Kosina 
18400361a28dSOliver Neukum 	list_for_each_entry(hidinput, &hid->inputs, list) {
18410361a28dSOliver Neukum 		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
18420361a28dSOliver Neukum 			if (hidinput->input->key[i])
18430361a28dSOliver Neukum 				return 1;
18440361a28dSOliver Neukum 	}
18450361a28dSOliver Neukum 
18460361a28dSOliver Neukum 	return 0;
18470361a28dSOliver Neukum }
18480361a28dSOliver Neukum 
18490361a28dSOliver Neukum EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
18500361a28dSOliver Neukum 
185186166b7bSJiri Kosina static int __init hid_init(void)
185286166b7bSJiri Kosina {
185385cdaf52SJiri Slaby 	int ret;
185485cdaf52SJiri Slaby 
185585cdaf52SJiri Slaby 	ret = bus_register(&hid_bus_type);
185685cdaf52SJiri Slaby 	if (ret) {
185785cdaf52SJiri Slaby 		printk(KERN_ERR "HID: can't register hid bus\n");
185885cdaf52SJiri Slaby 		goto err;
185985cdaf52SJiri Slaby 	}
186085cdaf52SJiri Slaby 
186185cdaf52SJiri Slaby 	ret = hidraw_init();
186285cdaf52SJiri Slaby 	if (ret)
186385cdaf52SJiri Slaby 		goto err_bus;
186485cdaf52SJiri Slaby 
186585cdaf52SJiri Slaby 	return 0;
186685cdaf52SJiri Slaby err_bus:
186785cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
186885cdaf52SJiri Slaby err:
186985cdaf52SJiri Slaby 	return ret;
187086166b7bSJiri Kosina }
187186166b7bSJiri Kosina 
187286166b7bSJiri Kosina static void __exit hid_exit(void)
187386166b7bSJiri Kosina {
187486166b7bSJiri Kosina 	hidraw_exit();
187585cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
187686166b7bSJiri Kosina }
187786166b7bSJiri Kosina 
187886166b7bSJiri Kosina module_init(hid_init);
187986166b7bSJiri Kosina module_exit(hid_exit);
188086166b7bSJiri Kosina 
1881aa938f79SJiri Kosina MODULE_LICENSE(DRIVER_LICENSE);
1882aa938f79SJiri Kosina 
1883