xref: /openbmc/linux/drivers/hid/hid-core.c (revision 02ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9)
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 
298dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_PUSH:
299dde5845aSJiri Kosina 
300dde5845aSJiri Kosina 			if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
30158037eb9SJiri Kosina 				dbg_hid("global enviroment stack overflow\n");
302dde5845aSJiri Kosina 				return -1;
303dde5845aSJiri Kosina 			}
304dde5845aSJiri Kosina 
305dde5845aSJiri Kosina 			memcpy(parser->global_stack + parser->global_stack_ptr++,
306dde5845aSJiri Kosina 				&parser->global, sizeof(struct hid_global));
307dde5845aSJiri Kosina 			return 0;
308dde5845aSJiri Kosina 
309dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_POP:
310dde5845aSJiri Kosina 
311dde5845aSJiri Kosina 			if (!parser->global_stack_ptr) {
31258037eb9SJiri Kosina 				dbg_hid("global enviroment stack underflow\n");
313dde5845aSJiri Kosina 				return -1;
314dde5845aSJiri Kosina 			}
315dde5845aSJiri Kosina 
316dde5845aSJiri Kosina 			memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
317dde5845aSJiri Kosina 				sizeof(struct hid_global));
318dde5845aSJiri Kosina 			return 0;
319dde5845aSJiri Kosina 
320dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
321dde5845aSJiri Kosina 			parser->global.usage_page = item_udata(item);
322dde5845aSJiri Kosina 			return 0;
323dde5845aSJiri Kosina 
324dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
325dde5845aSJiri Kosina 			parser->global.logical_minimum = item_sdata(item);
326dde5845aSJiri Kosina 			return 0;
327dde5845aSJiri Kosina 
328dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
329dde5845aSJiri Kosina 			if (parser->global.logical_minimum < 0)
330dde5845aSJiri Kosina 				parser->global.logical_maximum = item_sdata(item);
331dde5845aSJiri Kosina 			else
332dde5845aSJiri Kosina 				parser->global.logical_maximum = item_udata(item);
333dde5845aSJiri Kosina 			return 0;
334dde5845aSJiri Kosina 
335dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
336dde5845aSJiri Kosina 			parser->global.physical_minimum = item_sdata(item);
337dde5845aSJiri Kosina 			return 0;
338dde5845aSJiri Kosina 
339dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
340dde5845aSJiri Kosina 			if (parser->global.physical_minimum < 0)
341dde5845aSJiri Kosina 				parser->global.physical_maximum = item_sdata(item);
342dde5845aSJiri Kosina 			else
343dde5845aSJiri Kosina 				parser->global.physical_maximum = item_udata(item);
344dde5845aSJiri Kosina 			return 0;
345dde5845aSJiri Kosina 
346dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
347dde5845aSJiri Kosina 			parser->global.unit_exponent = item_sdata(item);
348dde5845aSJiri Kosina 			return 0;
349dde5845aSJiri Kosina 
350dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_UNIT:
351dde5845aSJiri Kosina 			parser->global.unit = item_udata(item);
352dde5845aSJiri Kosina 			return 0;
353dde5845aSJiri Kosina 
354dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
355dde5845aSJiri Kosina 			if ((parser->global.report_size = item_udata(item)) > 32) {
35658037eb9SJiri Kosina 				dbg_hid("invalid report_size %d\n", parser->global.report_size);
357dde5845aSJiri Kosina 				return -1;
358dde5845aSJiri Kosina 			}
359dde5845aSJiri Kosina 			return 0;
360dde5845aSJiri Kosina 
361dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
362dde5845aSJiri Kosina 			if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
36358037eb9SJiri Kosina 				dbg_hid("invalid report_count %d\n", parser->global.report_count);
364dde5845aSJiri Kosina 				return -1;
365dde5845aSJiri Kosina 			}
366dde5845aSJiri Kosina 			return 0;
367dde5845aSJiri Kosina 
368dde5845aSJiri Kosina 		case HID_GLOBAL_ITEM_TAG_REPORT_ID:
369dde5845aSJiri Kosina 			if ((parser->global.report_id = item_udata(item)) == 0) {
37058037eb9SJiri Kosina 				dbg_hid("report_id 0 is invalid\n");
371dde5845aSJiri Kosina 				return -1;
372dde5845aSJiri Kosina 			}
373dde5845aSJiri Kosina 			return 0;
374dde5845aSJiri Kosina 
375dde5845aSJiri Kosina 		default:
37658037eb9SJiri Kosina 			dbg_hid("unknown global tag 0x%x\n", item->tag);
377dde5845aSJiri Kosina 			return -1;
378dde5845aSJiri Kosina 	}
379dde5845aSJiri Kosina }
380dde5845aSJiri Kosina 
381dde5845aSJiri Kosina /*
382dde5845aSJiri Kosina  * Process a local item.
383dde5845aSJiri Kosina  */
384dde5845aSJiri Kosina 
385dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
386dde5845aSJiri Kosina {
387dde5845aSJiri Kosina 	__u32 data;
388dde5845aSJiri Kosina 	unsigned n;
389dde5845aSJiri Kosina 
390dde5845aSJiri Kosina 	if (item->size == 0) {
39158037eb9SJiri Kosina 		dbg_hid("item data expected for local item\n");
392dde5845aSJiri Kosina 		return -1;
393dde5845aSJiri Kosina 	}
394dde5845aSJiri Kosina 
395dde5845aSJiri Kosina 	data = item_udata(item);
396dde5845aSJiri Kosina 
397dde5845aSJiri Kosina 	switch (item->tag) {
398dde5845aSJiri Kosina 
399dde5845aSJiri Kosina 		case HID_LOCAL_ITEM_TAG_DELIMITER:
400dde5845aSJiri Kosina 
401dde5845aSJiri Kosina 			if (data) {
402dde5845aSJiri Kosina 				/*
403dde5845aSJiri Kosina 				 * We treat items before the first delimiter
404dde5845aSJiri Kosina 				 * as global to all usage sets (branch 0).
405dde5845aSJiri Kosina 				 * In the moment we process only these global
406dde5845aSJiri Kosina 				 * items and the first delimiter set.
407dde5845aSJiri Kosina 				 */
408dde5845aSJiri Kosina 				if (parser->local.delimiter_depth != 0) {
40958037eb9SJiri Kosina 					dbg_hid("nested delimiters\n");
410dde5845aSJiri Kosina 					return -1;
411dde5845aSJiri Kosina 				}
412dde5845aSJiri Kosina 				parser->local.delimiter_depth++;
413dde5845aSJiri Kosina 				parser->local.delimiter_branch++;
414dde5845aSJiri Kosina 			} else {
415dde5845aSJiri Kosina 				if (parser->local.delimiter_depth < 1) {
41658037eb9SJiri Kosina 					dbg_hid("bogus close delimiter\n");
417dde5845aSJiri Kosina 					return -1;
418dde5845aSJiri Kosina 				}
419dde5845aSJiri Kosina 				parser->local.delimiter_depth--;
420dde5845aSJiri Kosina 			}
421dde5845aSJiri Kosina 			return 1;
422dde5845aSJiri Kosina 
423dde5845aSJiri Kosina 		case HID_LOCAL_ITEM_TAG_USAGE:
424dde5845aSJiri Kosina 
425dde5845aSJiri Kosina 			if (parser->local.delimiter_branch > 1) {
42658037eb9SJiri Kosina 				dbg_hid("alternative usage ignored\n");
427dde5845aSJiri Kosina 				return 0;
428dde5845aSJiri Kosina 			}
429dde5845aSJiri Kosina 
430dde5845aSJiri Kosina 			if (item->size <= 2)
431dde5845aSJiri Kosina 				data = (parser->global.usage_page << 16) + data;
432dde5845aSJiri Kosina 
433dde5845aSJiri Kosina 			return hid_add_usage(parser, data);
434dde5845aSJiri Kosina 
435dde5845aSJiri Kosina 		case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
436dde5845aSJiri Kosina 
437dde5845aSJiri Kosina 			if (parser->local.delimiter_branch > 1) {
43858037eb9SJiri Kosina 				dbg_hid("alternative usage ignored\n");
439dde5845aSJiri Kosina 				return 0;
440dde5845aSJiri Kosina 			}
441dde5845aSJiri Kosina 
442dde5845aSJiri Kosina 			if (item->size <= 2)
443dde5845aSJiri Kosina 				data = (parser->global.usage_page << 16) + data;
444dde5845aSJiri Kosina 
445dde5845aSJiri Kosina 			parser->local.usage_minimum = data;
446dde5845aSJiri Kosina 			return 0;
447dde5845aSJiri Kosina 
448dde5845aSJiri Kosina 		case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
449dde5845aSJiri Kosina 
450dde5845aSJiri Kosina 			if (parser->local.delimiter_branch > 1) {
45158037eb9SJiri Kosina 				dbg_hid("alternative usage ignored\n");
452dde5845aSJiri Kosina 				return 0;
453dde5845aSJiri Kosina 			}
454dde5845aSJiri Kosina 
455dde5845aSJiri Kosina 			if (item->size <= 2)
456dde5845aSJiri Kosina 				data = (parser->global.usage_page << 16) + data;
457dde5845aSJiri Kosina 
458dde5845aSJiri Kosina 			for (n = parser->local.usage_minimum; n <= data; n++)
459dde5845aSJiri Kosina 				if (hid_add_usage(parser, n)) {
46058037eb9SJiri Kosina 					dbg_hid("hid_add_usage failed\n");
461dde5845aSJiri Kosina 					return -1;
462dde5845aSJiri Kosina 				}
463dde5845aSJiri Kosina 			return 0;
464dde5845aSJiri Kosina 
465dde5845aSJiri Kosina 		default:
466dde5845aSJiri Kosina 
46758037eb9SJiri Kosina 			dbg_hid("unknown local item tag 0x%x\n", item->tag);
468dde5845aSJiri Kosina 			return 0;
469dde5845aSJiri Kosina 	}
470dde5845aSJiri Kosina 	return 0;
471dde5845aSJiri Kosina }
472dde5845aSJiri Kosina 
473dde5845aSJiri Kosina /*
474dde5845aSJiri Kosina  * Process a main item.
475dde5845aSJiri Kosina  */
476dde5845aSJiri Kosina 
477dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
478dde5845aSJiri Kosina {
479dde5845aSJiri Kosina 	__u32 data;
480dde5845aSJiri Kosina 	int ret;
481dde5845aSJiri Kosina 
482dde5845aSJiri Kosina 	data = item_udata(item);
483dde5845aSJiri Kosina 
484dde5845aSJiri Kosina 	switch (item->tag) {
485dde5845aSJiri Kosina 		case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
486dde5845aSJiri Kosina 			ret = open_collection(parser, data & 0xff);
487dde5845aSJiri Kosina 			break;
488dde5845aSJiri Kosina 		case HID_MAIN_ITEM_TAG_END_COLLECTION:
489dde5845aSJiri Kosina 			ret = close_collection(parser);
490dde5845aSJiri Kosina 			break;
491dde5845aSJiri Kosina 		case HID_MAIN_ITEM_TAG_INPUT:
492dde5845aSJiri Kosina 			ret = hid_add_field(parser, HID_INPUT_REPORT, data);
493dde5845aSJiri Kosina 			break;
494dde5845aSJiri Kosina 		case HID_MAIN_ITEM_TAG_OUTPUT:
495dde5845aSJiri Kosina 			ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
496dde5845aSJiri Kosina 			break;
497dde5845aSJiri Kosina 		case HID_MAIN_ITEM_TAG_FEATURE:
498dde5845aSJiri Kosina 			ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
499dde5845aSJiri Kosina 			break;
500dde5845aSJiri Kosina 		default:
50158037eb9SJiri Kosina 			dbg_hid("unknown main item tag 0x%x\n", item->tag);
502dde5845aSJiri Kosina 			ret = 0;
503dde5845aSJiri Kosina 	}
504dde5845aSJiri Kosina 
505dde5845aSJiri Kosina 	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
506dde5845aSJiri Kosina 
507dde5845aSJiri Kosina 	return ret;
508dde5845aSJiri Kosina }
509dde5845aSJiri Kosina 
510dde5845aSJiri Kosina /*
511dde5845aSJiri Kosina  * Process a reserved item.
512dde5845aSJiri Kosina  */
513dde5845aSJiri Kosina 
514dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
515dde5845aSJiri Kosina {
51658037eb9SJiri Kosina 	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
517dde5845aSJiri Kosina 	return 0;
518dde5845aSJiri Kosina }
519dde5845aSJiri Kosina 
520dde5845aSJiri Kosina /*
521dde5845aSJiri Kosina  * Free a report and all registered fields. The field->usage and
522dde5845aSJiri Kosina  * field->value table's are allocated behind the field, so we need
523dde5845aSJiri Kosina  * only to free(field) itself.
524dde5845aSJiri Kosina  */
525dde5845aSJiri Kosina 
526dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report)
527dde5845aSJiri Kosina {
528dde5845aSJiri Kosina 	unsigned n;
529dde5845aSJiri Kosina 
530dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
531dde5845aSJiri Kosina 		kfree(report->field[n]);
532dde5845aSJiri Kosina 	kfree(report);
533dde5845aSJiri Kosina }
534dde5845aSJiri Kosina 
535dde5845aSJiri Kosina /*
536dde5845aSJiri Kosina  * Free a device structure, all reports, and all fields.
537dde5845aSJiri Kosina  */
538dde5845aSJiri Kosina 
53985cdaf52SJiri Slaby static void hid_device_release(struct device *dev)
540dde5845aSJiri Kosina {
54185cdaf52SJiri Slaby 	struct hid_device *device = container_of(dev, struct hid_device, dev);
542dde5845aSJiri Kosina 	unsigned i, j;
543dde5845aSJiri Kosina 
544dde5845aSJiri Kosina 	for (i = 0; i < HID_REPORT_TYPES; i++) {
545dde5845aSJiri Kosina 		struct hid_report_enum *report_enum = device->report_enum + i;
546dde5845aSJiri Kosina 
547dde5845aSJiri Kosina 		for (j = 0; j < 256; j++) {
548dde5845aSJiri Kosina 			struct hid_report *report = report_enum->report_id_hash[j];
549dde5845aSJiri Kosina 			if (report)
550dde5845aSJiri Kosina 				hid_free_report(report);
551dde5845aSJiri Kosina 		}
552dde5845aSJiri Kosina 	}
553dde5845aSJiri Kosina 
554dde5845aSJiri Kosina 	kfree(device->rdesc);
555767fe787SJiri Kosina 	kfree(device->collection);
556dde5845aSJiri Kosina 	kfree(device);
557dde5845aSJiri Kosina }
558dde5845aSJiri Kosina 
559dde5845aSJiri Kosina /*
560dde5845aSJiri Kosina  * Fetch a report description item from the data stream. We support long
561dde5845aSJiri Kosina  * items, though they are not used yet.
562dde5845aSJiri Kosina  */
563dde5845aSJiri Kosina 
564dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
565dde5845aSJiri Kosina {
566dde5845aSJiri Kosina 	u8 b;
567dde5845aSJiri Kosina 
568dde5845aSJiri Kosina 	if ((end - start) <= 0)
569dde5845aSJiri Kosina 		return NULL;
570dde5845aSJiri Kosina 
571dde5845aSJiri Kosina 	b = *start++;
572dde5845aSJiri Kosina 
573dde5845aSJiri Kosina 	item->type = (b >> 2) & 3;
574dde5845aSJiri Kosina 	item->tag  = (b >> 4) & 15;
575dde5845aSJiri Kosina 
576dde5845aSJiri Kosina 	if (item->tag == HID_ITEM_TAG_LONG) {
577dde5845aSJiri Kosina 
578dde5845aSJiri Kosina 		item->format = HID_ITEM_FORMAT_LONG;
579dde5845aSJiri Kosina 
580dde5845aSJiri Kosina 		if ((end - start) < 2)
581dde5845aSJiri Kosina 			return NULL;
582dde5845aSJiri Kosina 
583dde5845aSJiri Kosina 		item->size = *start++;
584dde5845aSJiri Kosina 		item->tag  = *start++;
585dde5845aSJiri Kosina 
586dde5845aSJiri Kosina 		if ((end - start) < item->size)
587dde5845aSJiri Kosina 			return NULL;
588dde5845aSJiri Kosina 
589dde5845aSJiri Kosina 		item->data.longdata = start;
590dde5845aSJiri Kosina 		start += item->size;
591dde5845aSJiri Kosina 		return start;
592dde5845aSJiri Kosina 	}
593dde5845aSJiri Kosina 
594dde5845aSJiri Kosina 	item->format = HID_ITEM_FORMAT_SHORT;
595dde5845aSJiri Kosina 	item->size = b & 3;
596dde5845aSJiri Kosina 
597dde5845aSJiri Kosina 	switch (item->size) {
598dde5845aSJiri Kosina 
599dde5845aSJiri Kosina 		case 0:
600dde5845aSJiri Kosina 			return start;
601dde5845aSJiri Kosina 
602dde5845aSJiri Kosina 		case 1:
603dde5845aSJiri Kosina 			if ((end - start) < 1)
604dde5845aSJiri Kosina 				return NULL;
605dde5845aSJiri Kosina 			item->data.u8 = *start++;
606dde5845aSJiri Kosina 			return start;
607dde5845aSJiri Kosina 
608dde5845aSJiri Kosina 		case 2:
609dde5845aSJiri Kosina 			if ((end - start) < 2)
610dde5845aSJiri Kosina 				return NULL;
611c105068fSHarvey Harrison 			item->data.u16 = get_unaligned_le16(start);
612dde5845aSJiri Kosina 			start = (__u8 *)((__le16 *)start + 1);
613dde5845aSJiri Kosina 			return start;
614dde5845aSJiri Kosina 
615dde5845aSJiri Kosina 		case 3:
616dde5845aSJiri Kosina 			item->size++;
617dde5845aSJiri Kosina 			if ((end - start) < 4)
618dde5845aSJiri Kosina 				return NULL;
619c105068fSHarvey Harrison 			item->data.u32 = get_unaligned_le32(start);
620dde5845aSJiri Kosina 			start = (__u8 *)((__le32 *)start + 1);
621dde5845aSJiri Kosina 			return start;
622dde5845aSJiri Kosina 	}
623dde5845aSJiri Kosina 
624dde5845aSJiri Kosina 	return NULL;
625dde5845aSJiri Kosina }
626dde5845aSJiri Kosina 
62785cdaf52SJiri Slaby /**
62885cdaf52SJiri Slaby  * hid_parse_report - parse device report
62985cdaf52SJiri Slaby  *
63085cdaf52SJiri Slaby  * @device: hid device
63185cdaf52SJiri Slaby  * @start: report start
63285cdaf52SJiri Slaby  * @size: report size
63385cdaf52SJiri Slaby  *
634dde5845aSJiri Kosina  * Parse a report description into a hid_device structure. Reports are
635dde5845aSJiri Kosina  * enumerated, fields are attached to these reports.
63685cdaf52SJiri Slaby  * 0 returned on success, otherwise nonzero error value.
637dde5845aSJiri Kosina  */
63885cdaf52SJiri Slaby int hid_parse_report(struct hid_device *device, __u8 *start,
63985cdaf52SJiri Slaby 		unsigned size)
640dde5845aSJiri Kosina {
641dde5845aSJiri Kosina 	struct hid_parser *parser;
642dde5845aSJiri Kosina 	struct hid_item item;
643dde5845aSJiri Kosina 	__u8 *end;
64485cdaf52SJiri Slaby 	int ret;
645dde5845aSJiri Kosina 	static int (*dispatch_type[])(struct hid_parser *parser,
646dde5845aSJiri Kosina 				      struct hid_item *item) = {
647dde5845aSJiri Kosina 		hid_parser_main,
648dde5845aSJiri Kosina 		hid_parser_global,
649dde5845aSJiri Kosina 		hid_parser_local,
650dde5845aSJiri Kosina 		hid_parser_reserved
651dde5845aSJiri Kosina 	};
652dde5845aSJiri Kosina 
653c500c971SJiri Slaby 	if (device->driver->report_fixup)
654c500c971SJiri Slaby 		device->driver->report_fixup(device, start, size);
655c500c971SJiri Slaby 
65685cdaf52SJiri Slaby 	device->rdesc = kmalloc(size, GFP_KERNEL);
65785cdaf52SJiri Slaby 	if (device->rdesc == NULL)
65885cdaf52SJiri Slaby 		return -ENOMEM;
659dde5845aSJiri Kosina 	memcpy(device->rdesc, start, size);
660dde5845aSJiri Kosina 	device->rsize = size;
661dde5845aSJiri Kosina 
66285cdaf52SJiri Slaby 	parser = vmalloc(sizeof(struct hid_parser));
66385cdaf52SJiri Slaby 	if (!parser) {
66485cdaf52SJiri Slaby 		ret = -ENOMEM;
66585cdaf52SJiri Slaby 		goto err;
666dde5845aSJiri Kosina 	}
66785cdaf52SJiri Slaby 
66847a80edbSJiri Kosina 	memset(parser, 0, sizeof(struct hid_parser));
669dde5845aSJiri Kosina 	parser->device = device;
670dde5845aSJiri Kosina 
671dde5845aSJiri Kosina 	end = start + size;
67285cdaf52SJiri Slaby 	ret = -EINVAL;
673dde5845aSJiri Kosina 	while ((start = fetch_item(start, end, &item)) != NULL) {
674dde5845aSJiri Kosina 
675dde5845aSJiri Kosina 		if (item.format != HID_ITEM_FORMAT_SHORT) {
67658037eb9SJiri Kosina 			dbg_hid("unexpected long global item\n");
67785cdaf52SJiri Slaby 			goto err;
678dde5845aSJiri Kosina 		}
679dde5845aSJiri Kosina 
680dde5845aSJiri Kosina 		if (dispatch_type[item.type](parser, &item)) {
68158037eb9SJiri Kosina 			dbg_hid("item %u %u %u %u parsing failed\n",
682dde5845aSJiri Kosina 				item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
68385cdaf52SJiri Slaby 			goto err;
684dde5845aSJiri Kosina 		}
685dde5845aSJiri Kosina 
686dde5845aSJiri Kosina 		if (start == end) {
687dde5845aSJiri Kosina 			if (parser->collection_stack_ptr) {
68858037eb9SJiri Kosina 				dbg_hid("unbalanced collection at end of report description\n");
68985cdaf52SJiri Slaby 				goto err;
690dde5845aSJiri Kosina 			}
691dde5845aSJiri Kosina 			if (parser->local.delimiter_depth) {
69258037eb9SJiri Kosina 				dbg_hid("unbalanced delimiter at end of report description\n");
69385cdaf52SJiri Slaby 				goto err;
694dde5845aSJiri Kosina 			}
69547a80edbSJiri Kosina 			vfree(parser);
69685cdaf52SJiri Slaby 			return 0;
697dde5845aSJiri Kosina 		}
698dde5845aSJiri Kosina 	}
699dde5845aSJiri Kosina 
70058037eb9SJiri Kosina 	dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
70185cdaf52SJiri Slaby err:
70247a80edbSJiri Kosina 	vfree(parser);
70385cdaf52SJiri Slaby 	return ret;
704dde5845aSJiri Kosina }
705229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_parse_report);
706dde5845aSJiri Kosina 
707dde5845aSJiri Kosina /*
708dde5845aSJiri Kosina  * Convert a signed n-bit integer to signed 32-bit integer. Common
709dde5845aSJiri Kosina  * cases are done through the compiler, the screwed things has to be
710dde5845aSJiri Kosina  * done by hand.
711dde5845aSJiri Kosina  */
712dde5845aSJiri Kosina 
713dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n)
714dde5845aSJiri Kosina {
715dde5845aSJiri Kosina 	switch (n) {
716dde5845aSJiri Kosina 		case 8:  return ((__s8)value);
717dde5845aSJiri Kosina 		case 16: return ((__s16)value);
718dde5845aSJiri Kosina 		case 32: return ((__s32)value);
719dde5845aSJiri Kosina 	}
720dde5845aSJiri Kosina 	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
721dde5845aSJiri Kosina }
722dde5845aSJiri Kosina 
723dde5845aSJiri Kosina /*
724dde5845aSJiri Kosina  * Convert a signed 32-bit integer to a signed n-bit integer.
725dde5845aSJiri Kosina  */
726dde5845aSJiri Kosina 
727dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n)
728dde5845aSJiri Kosina {
729dde5845aSJiri Kosina 	s32 a = value >> (n - 1);
730dde5845aSJiri Kosina 	if (a && a != -1)
731dde5845aSJiri Kosina 		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
732dde5845aSJiri Kosina 	return value & ((1 << n) - 1);
733dde5845aSJiri Kosina }
734dde5845aSJiri Kosina 
735dde5845aSJiri Kosina /*
736dde5845aSJiri Kosina  * Extract/implement a data field from/to a little endian report (bit array).
737dde5845aSJiri Kosina  *
738dde5845aSJiri Kosina  * Code sort-of follows HID spec:
739dde5845aSJiri Kosina  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
740dde5845aSJiri Kosina  *
741dde5845aSJiri Kosina  * While the USB HID spec allows unlimited length bit fields in "report
742dde5845aSJiri Kosina  * descriptors", most devices never use more than 16 bits.
743dde5845aSJiri Kosina  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
744dde5845aSJiri Kosina  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
745dde5845aSJiri Kosina  */
746dde5845aSJiri Kosina 
747dde5845aSJiri Kosina static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
748dde5845aSJiri Kosina {
749dde5845aSJiri Kosina 	u64 x;
750dde5845aSJiri Kosina 
751c4124c9bSJiri Kosina 	if (n > 32)
752c4124c9bSJiri Kosina 		printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
753c4124c9bSJiri Kosina 				n, current->comm);
754dde5845aSJiri Kosina 
755dde5845aSJiri Kosina 	report += offset >> 3;  /* adjust byte index */
756dde5845aSJiri Kosina 	offset &= 7;            /* now only need bit offset into one byte */
757c105068fSHarvey Harrison 	x = get_unaligned_le64(report);
758dde5845aSJiri Kosina 	x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
759dde5845aSJiri Kosina 	return (u32) x;
760dde5845aSJiri Kosina }
761dde5845aSJiri Kosina 
762dde5845aSJiri Kosina /*
763dde5845aSJiri Kosina  * "implement" : set bits in a little endian bit stream.
764dde5845aSJiri Kosina  * Same concepts as "extract" (see comments above).
765dde5845aSJiri Kosina  * The data mangled in the bit stream remains in little endian
766dde5845aSJiri Kosina  * order the whole time. It make more sense to talk about
767dde5845aSJiri Kosina  * endianness of register values by considering a register
768dde5845aSJiri Kosina  * a "cached" copy of the little endiad bit stream.
769dde5845aSJiri Kosina  */
770dde5845aSJiri Kosina static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
771dde5845aSJiri Kosina {
7726f0168d2SHarvey Harrison 	u64 x;
773dde5845aSJiri Kosina 	u64 m = (1ULL << n) - 1;
774dde5845aSJiri Kosina 
775c4124c9bSJiri Kosina 	if (n > 32)
776c4124c9bSJiri Kosina 		printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
777c4124c9bSJiri Kosina 				n, current->comm);
778dde5845aSJiri Kosina 
779c4124c9bSJiri Kosina 	if (value > m)
780c4124c9bSJiri Kosina 		printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
781c4124c9bSJiri Kosina 				value, current->comm);
782dde5845aSJiri Kosina 	WARN_ON(value > m);
783dde5845aSJiri Kosina 	value &= m;
784dde5845aSJiri Kosina 
785dde5845aSJiri Kosina 	report += offset >> 3;
786dde5845aSJiri Kosina 	offset &= 7;
787dde5845aSJiri Kosina 
7886f0168d2SHarvey Harrison 	x = get_unaligned_le64(report);
7896f0168d2SHarvey Harrison 	x &= ~(m << offset);
7906f0168d2SHarvey Harrison 	x |= ((u64)value) << offset;
7916f0168d2SHarvey Harrison 	put_unaligned_le64(x, report);
792dde5845aSJiri Kosina }
793dde5845aSJiri Kosina 
794dde5845aSJiri Kosina /*
795dde5845aSJiri Kosina  * Search an array for a value.
796dde5845aSJiri Kosina  */
797dde5845aSJiri Kosina 
798dde5845aSJiri Kosina static __inline__ int search(__s32 *array, __s32 value, unsigned n)
799dde5845aSJiri Kosina {
800dde5845aSJiri Kosina 	while (n--) {
801dde5845aSJiri Kosina 		if (*array++ == value)
802dde5845aSJiri Kosina 			return 0;
803dde5845aSJiri Kosina 	}
804dde5845aSJiri Kosina 	return -1;
805dde5845aSJiri Kosina }
806dde5845aSJiri Kosina 
80785cdaf52SJiri Slaby /**
80885cdaf52SJiri Slaby  * hid_match_report - check if driver's raw_event should be called
80985cdaf52SJiri Slaby  *
81085cdaf52SJiri Slaby  * @hid: hid device
81185cdaf52SJiri Slaby  * @report_type: type to match against
81285cdaf52SJiri Slaby  *
81385cdaf52SJiri Slaby  * compare hid->driver->report_table->report_type to report->type
81485cdaf52SJiri Slaby  */
81585cdaf52SJiri Slaby static int hid_match_report(struct hid_device *hid, struct hid_report *report)
816dde5845aSJiri Kosina {
81785cdaf52SJiri Slaby 	const struct hid_report_id *id = hid->driver->report_table;
81885cdaf52SJiri Slaby 
81985cdaf52SJiri Slaby 	if (!id) /* NULL means all */
82085cdaf52SJiri Slaby 		return 1;
82185cdaf52SJiri Slaby 
82285cdaf52SJiri Slaby 	for (; id->report_type != HID_TERMINATOR; id++)
82385cdaf52SJiri Slaby 		if (id->report_type == HID_ANY_ID ||
82485cdaf52SJiri Slaby 				id->report_type == report->type)
82585cdaf52SJiri Slaby 			return 1;
82685cdaf52SJiri Slaby 	return 0;
82785cdaf52SJiri Slaby }
82885cdaf52SJiri Slaby 
82985cdaf52SJiri Slaby /**
83085cdaf52SJiri Slaby  * hid_match_usage - check if driver's event should be called
83185cdaf52SJiri Slaby  *
83285cdaf52SJiri Slaby  * @hid: hid device
83385cdaf52SJiri Slaby  * @usage: usage to match against
83485cdaf52SJiri Slaby  *
83585cdaf52SJiri Slaby  * compare hid->driver->usage_table->usage_{type,code} to
83685cdaf52SJiri Slaby  * usage->usage_{type,code}
83785cdaf52SJiri Slaby  */
83885cdaf52SJiri Slaby static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
83985cdaf52SJiri Slaby {
84085cdaf52SJiri Slaby 	const struct hid_usage_id *id = hid->driver->usage_table;
84185cdaf52SJiri Slaby 
84285cdaf52SJiri Slaby 	if (!id) /* NULL means all */
84385cdaf52SJiri Slaby 		return 1;
84485cdaf52SJiri Slaby 
84585cdaf52SJiri Slaby 	for (; id->usage_type != HID_ANY_ID - 1; id++)
84685cdaf52SJiri Slaby 		if ((id->usage_hid == HID_ANY_ID ||
84785cdaf52SJiri Slaby 				id->usage_hid == usage->hid) &&
84885cdaf52SJiri Slaby 				(id->usage_type == HID_ANY_ID ||
84985cdaf52SJiri Slaby 				id->usage_type == usage->type) &&
85085cdaf52SJiri Slaby 				(id->usage_code == HID_ANY_ID ||
85185cdaf52SJiri Slaby 				 id->usage_code == usage->code))
85285cdaf52SJiri Slaby 			return 1;
85385cdaf52SJiri Slaby 	return 0;
85485cdaf52SJiri Slaby }
85585cdaf52SJiri Slaby 
85685cdaf52SJiri Slaby static void hid_process_event(struct hid_device *hid, struct hid_field *field,
85785cdaf52SJiri Slaby 		struct hid_usage *usage, __s32 value, int interrupt)
85885cdaf52SJiri Slaby {
85985cdaf52SJiri Slaby 	struct hid_driver *hdrv = hid->driver;
86085cdaf52SJiri Slaby 	int ret;
86185cdaf52SJiri Slaby 
862dde5845aSJiri Kosina 	hid_dump_input(usage, value);
86385cdaf52SJiri Slaby 
86485cdaf52SJiri Slaby 	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
86585cdaf52SJiri Slaby 		ret = hdrv->event(hid, field, usage, value);
86685cdaf52SJiri Slaby 		if (ret != 0) {
86785cdaf52SJiri Slaby 			if (ret < 0)
86885cdaf52SJiri Slaby 				dbg_hid("%s's event failed with %d\n",
86985cdaf52SJiri Slaby 						hdrv->name, ret);
87085cdaf52SJiri Slaby 			return;
87185cdaf52SJiri Slaby 		}
87285cdaf52SJiri Slaby 	}
87385cdaf52SJiri Slaby 
874dde5845aSJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
875dde5845aSJiri Kosina 		hidinput_hid_event(hid, field, usage, value);
876aa938f79SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
877aa938f79SJiri Kosina 		hid->hiddev_hid_event(hid, field, usage, value);
878dde5845aSJiri Kosina }
879dde5845aSJiri Kosina 
880dde5845aSJiri Kosina /*
881dde5845aSJiri Kosina  * Analyse a received field, and fetch the data from it. The field
882dde5845aSJiri Kosina  * content is stored for next report processing (we do differential
883dde5845aSJiri Kosina  * reporting to the layer).
884dde5845aSJiri Kosina  */
885dde5845aSJiri Kosina 
886abdff0f7SAdrian Bunk static void hid_input_field(struct hid_device *hid, struct hid_field *field,
887abdff0f7SAdrian Bunk 			    __u8 *data, int interrupt)
888dde5845aSJiri Kosina {
889dde5845aSJiri Kosina 	unsigned n;
890dde5845aSJiri Kosina 	unsigned count = field->report_count;
891dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
892dde5845aSJiri Kosina 	unsigned size = field->report_size;
893dde5845aSJiri Kosina 	__s32 min = field->logical_minimum;
894dde5845aSJiri Kosina 	__s32 max = field->logical_maximum;
895dde5845aSJiri Kosina 	__s32 *value;
896dde5845aSJiri Kosina 
897dde5845aSJiri Kosina 	if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
898dde5845aSJiri Kosina 		return;
899dde5845aSJiri Kosina 
900dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
901dde5845aSJiri Kosina 
902dde5845aSJiri Kosina 			value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
903dde5845aSJiri Kosina 						    extract(data, offset + n * size, size);
904dde5845aSJiri Kosina 
905dde5845aSJiri Kosina 			if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
906dde5845aSJiri Kosina 			    && value[n] >= min && value[n] <= max
907dde5845aSJiri Kosina 			    && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
908dde5845aSJiri Kosina 				goto exit;
909dde5845aSJiri Kosina 	}
910dde5845aSJiri Kosina 
911dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
912dde5845aSJiri Kosina 
913dde5845aSJiri Kosina 		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
914dde5845aSJiri Kosina 			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
915dde5845aSJiri Kosina 			continue;
916dde5845aSJiri Kosina 		}
917dde5845aSJiri Kosina 
918dde5845aSJiri Kosina 		if (field->value[n] >= min && field->value[n] <= max
919dde5845aSJiri Kosina 			&& field->usage[field->value[n] - min].hid
920dde5845aSJiri Kosina 			&& search(value, field->value[n], count))
921dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
922dde5845aSJiri Kosina 
923dde5845aSJiri Kosina 		if (value[n] >= min && value[n] <= max
924dde5845aSJiri Kosina 			&& field->usage[value[n] - min].hid
925dde5845aSJiri Kosina 			&& search(field->value, value[n], count))
926dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
927dde5845aSJiri Kosina 	}
928dde5845aSJiri Kosina 
929dde5845aSJiri Kosina 	memcpy(field->value, value, count * sizeof(__s32));
930dde5845aSJiri Kosina exit:
931dde5845aSJiri Kosina 	kfree(value);
932dde5845aSJiri Kosina }
933dde5845aSJiri Kosina 
934dde5845aSJiri Kosina /*
935dde5845aSJiri Kosina  * Output the field into the report.
936dde5845aSJiri Kosina  */
937dde5845aSJiri Kosina 
938dde5845aSJiri Kosina static void hid_output_field(struct hid_field *field, __u8 *data)
939dde5845aSJiri Kosina {
940dde5845aSJiri Kosina 	unsigned count = field->report_count;
941dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
942dde5845aSJiri Kosina 	unsigned size = field->report_size;
94346386b58SSimon Budig 	unsigned bitsused = offset + count * size;
944dde5845aSJiri Kosina 	unsigned n;
945dde5845aSJiri Kosina 
94646386b58SSimon Budig 	/* make sure the unused bits in the last byte are zeros */
94746386b58SSimon Budig 	if (count > 0 && size > 0 && (bitsused % 8) != 0)
94846386b58SSimon Budig 		data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
94946386b58SSimon Budig 
950dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
951dde5845aSJiri Kosina 		if (field->logical_minimum < 0)	/* signed values */
952dde5845aSJiri Kosina 			implement(data, offset + n * size, size, s32ton(field->value[n], size));
953dde5845aSJiri Kosina 		else				/* unsigned values */
954dde5845aSJiri Kosina 			implement(data, offset + n * size, size, field->value[n]);
955dde5845aSJiri Kosina 	}
956dde5845aSJiri Kosina }
957dde5845aSJiri Kosina 
958dde5845aSJiri Kosina /*
959dde5845aSJiri Kosina  * Create a report.
960dde5845aSJiri Kosina  */
961dde5845aSJiri Kosina 
962229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data)
963dde5845aSJiri Kosina {
964dde5845aSJiri Kosina 	unsigned n;
965dde5845aSJiri Kosina 
966dde5845aSJiri Kosina 	if (report->id > 0)
967dde5845aSJiri Kosina 		*data++ = report->id;
968dde5845aSJiri Kosina 
969dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
970dde5845aSJiri Kosina 		hid_output_field(report->field[n], data);
971dde5845aSJiri Kosina }
972229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report);
973dde5845aSJiri Kosina 
974dde5845aSJiri Kosina /*
975dde5845aSJiri Kosina  * Set a field value. The report this field belongs to has to be
976dde5845aSJiri Kosina  * created and transferred to the device, to set this value in the
977dde5845aSJiri Kosina  * device.
978dde5845aSJiri Kosina  */
979dde5845aSJiri Kosina 
980dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
981dde5845aSJiri Kosina {
982dde5845aSJiri Kosina 	unsigned size = field->report_size;
983dde5845aSJiri Kosina 
984dde5845aSJiri Kosina 	hid_dump_input(field->usage + offset, value);
985dde5845aSJiri Kosina 
986dde5845aSJiri Kosina 	if (offset >= field->report_count) {
98758037eb9SJiri Kosina 		dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
988dde5845aSJiri Kosina 		hid_dump_field(field, 8);
989dde5845aSJiri Kosina 		return -1;
990dde5845aSJiri Kosina 	}
991dde5845aSJiri Kosina 	if (field->logical_minimum < 0) {
992dde5845aSJiri Kosina 		if (value != snto32(s32ton(value, size), size)) {
99358037eb9SJiri Kosina 			dbg_hid("value %d is out of range\n", value);
994dde5845aSJiri Kosina 			return -1;
995dde5845aSJiri Kosina 		}
996dde5845aSJiri Kosina 	}
997dde5845aSJiri Kosina 	field->value[offset] = value;
998dde5845aSJiri Kosina 	return 0;
999dde5845aSJiri Kosina }
1000229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field);
1001dde5845aSJiri Kosina 
100285cdaf52SJiri Slaby static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
100385cdaf52SJiri Slaby 		const u8 *data)
100485cdaf52SJiri Slaby {
100585cdaf52SJiri Slaby 	struct hid_report *report;
100685cdaf52SJiri Slaby 	unsigned int n = 0;	/* Normally report number is 0 */
100785cdaf52SJiri Slaby 
100885cdaf52SJiri Slaby 	/* Device uses numbered reports, data[0] is report number */
100985cdaf52SJiri Slaby 	if (report_enum->numbered)
101085cdaf52SJiri Slaby 		n = *data;
101185cdaf52SJiri Slaby 
101285cdaf52SJiri Slaby 	report = report_enum->report_id_hash[n];
101385cdaf52SJiri Slaby 	if (report == NULL)
101485cdaf52SJiri Slaby 		dbg_hid("undefined report_id %u received\n", n);
101585cdaf52SJiri Slaby 
101685cdaf52SJiri Slaby 	return report;
101785cdaf52SJiri Slaby }
101885cdaf52SJiri Slaby 
101985cdaf52SJiri Slaby void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
102085cdaf52SJiri Slaby 		int interrupt)
1021aa8de2f0SJiri Kosina {
1022aa8de2f0SJiri Kosina 	struct hid_report_enum *report_enum = hid->report_enum + type;
1023aa8de2f0SJiri Kosina 	struct hid_report *report;
102485cdaf52SJiri Slaby 	unsigned int a;
102585cdaf52SJiri Slaby 	int rsize, csize = size;
102685cdaf52SJiri Slaby 	u8 *cdata = data;
1027aa8de2f0SJiri Kosina 
102885cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
102985cdaf52SJiri Slaby 	if (!report)
103085cdaf52SJiri Slaby 		return;
1031aa8de2f0SJiri Kosina 
103285cdaf52SJiri Slaby 	if (report_enum->numbered) {
103385cdaf52SJiri Slaby 		cdata++;
103485cdaf52SJiri Slaby 		csize--;
1035aa8de2f0SJiri Kosina 	}
1036aa8de2f0SJiri Kosina 
1037aa8de2f0SJiri Kosina 	rsize = ((report->size - 1) >> 3) + 1;
1038aa8de2f0SJiri Kosina 
103985cdaf52SJiri Slaby 	if (csize < rsize) {
104085cdaf52SJiri Slaby 		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
104185cdaf52SJiri Slaby 				csize, rsize);
104285cdaf52SJiri Slaby 		memset(cdata + csize, 0, rsize - csize);
1043aa8de2f0SJiri Kosina 	}
1044aa8de2f0SJiri Kosina 
1045aa8de2f0SJiri Kosina 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1046aa8de2f0SJiri Kosina 		hid->hiddev_report_event(hid, report);
1047b54ec3c1SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1048b54ec3c1SJiri Kosina 		/* numbered reports need to be passed with the report num */
1049b54ec3c1SJiri Kosina 		if (report_enum->numbered)
1050b54ec3c1SJiri Kosina 			hidraw_report_event(hid, data - 1, size + 1);
1051b54ec3c1SJiri Kosina 		else
105286166b7bSJiri Kosina 			hidraw_report_event(hid, data, size);
1053b54ec3c1SJiri Kosina 	}
1054aa8de2f0SJiri Kosina 
105585cdaf52SJiri Slaby 	for (a = 0; a < report->maxfield; a++)
105685cdaf52SJiri Slaby 		hid_input_field(hid, report->field[a], cdata, interrupt);
1057aa8de2f0SJiri Kosina 
1058aa8de2f0SJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1059aa8de2f0SJiri Kosina 		hidinput_report_event(hid, report);
106085cdaf52SJiri Slaby }
106185cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_report_raw_event);
106285cdaf52SJiri Slaby 
106385cdaf52SJiri Slaby /**
106485cdaf52SJiri Slaby  * hid_input_report - report data from lower layer (usb, bt...)
106585cdaf52SJiri Slaby  *
106685cdaf52SJiri Slaby  * @hid: hid device
106785cdaf52SJiri Slaby  * @type: HID report type (HID_*_REPORT)
106885cdaf52SJiri Slaby  * @data: report contents
106985cdaf52SJiri Slaby  * @size: size of data parameter
107085cdaf52SJiri Slaby  * @interrupt: called from atomic?
107185cdaf52SJiri Slaby  *
107285cdaf52SJiri Slaby  * This is data entry for lower layers.
107385cdaf52SJiri Slaby  */
107485cdaf52SJiri Slaby int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
107585cdaf52SJiri Slaby {
107685cdaf52SJiri Slaby 	struct hid_report_enum *report_enum = hid->report_enum + type;
107785cdaf52SJiri Slaby 	struct hid_driver *hdrv = hid->driver;
107885cdaf52SJiri Slaby 	struct hid_report *report;
107985cdaf52SJiri Slaby 	unsigned int i;
108085cdaf52SJiri Slaby 	int ret;
108185cdaf52SJiri Slaby 
108285cdaf52SJiri Slaby 	if (!hid || !hid->driver)
108385cdaf52SJiri Slaby 		return -ENODEV;
108485cdaf52SJiri Slaby 
108585cdaf52SJiri Slaby 	if (!size) {
108685cdaf52SJiri Slaby 		dbg_hid("empty report\n");
108785cdaf52SJiri Slaby 		return -1;
108885cdaf52SJiri Slaby 	}
108985cdaf52SJiri Slaby 
109085cdaf52SJiri Slaby 	dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
109185cdaf52SJiri Slaby 
109285cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
109385cdaf52SJiri Slaby 	if (!report)
109485cdaf52SJiri Slaby 		return -1;
109585cdaf52SJiri Slaby 
109685cdaf52SJiri Slaby 	/* dump the report */
109785cdaf52SJiri Slaby 	dbg_hid("report %d (size %u) = ", report->id, size);
109885cdaf52SJiri Slaby 	for (i = 0; i < size; i++)
109985cdaf52SJiri Slaby 		dbg_hid_line(" %02x", data[i]);
110085cdaf52SJiri Slaby 	dbg_hid_line("\n");
110185cdaf52SJiri Slaby 
110285cdaf52SJiri Slaby 	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
110385cdaf52SJiri Slaby 		ret = hdrv->raw_event(hid, report, data, size);
110485cdaf52SJiri Slaby 		if (ret != 0)
110585cdaf52SJiri Slaby 			return ret < 0 ? ret : 0;
110685cdaf52SJiri Slaby 	}
110785cdaf52SJiri Slaby 
110885cdaf52SJiri Slaby 	hid_report_raw_event(hid, type, data, size, interrupt);
1109aa8de2f0SJiri Kosina 
1110aa8de2f0SJiri Kosina 	return 0;
1111aa8de2f0SJiri Kosina }
1112aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report);
1113aa8de2f0SJiri Kosina 
111485cdaf52SJiri Slaby static bool hid_match_one_id(struct hid_device *hdev,
111585cdaf52SJiri Slaby 		const struct hid_device_id *id)
111685cdaf52SJiri Slaby {
111785cdaf52SJiri Slaby 	return id->bus == hdev->bus &&
111885cdaf52SJiri Slaby 		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
111985cdaf52SJiri Slaby 		(id->product == HID_ANY_ID || id->product == hdev->product);
112085cdaf52SJiri Slaby }
112185cdaf52SJiri Slaby 
112285cdaf52SJiri Slaby static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
112385cdaf52SJiri Slaby 		const struct hid_device_id *id)
112485cdaf52SJiri Slaby {
112585cdaf52SJiri Slaby 	for (; id->bus; id++)
112685cdaf52SJiri Slaby 		if (hid_match_one_id(hdev, id))
112785cdaf52SJiri Slaby 			return id;
112885cdaf52SJiri Slaby 
112985cdaf52SJiri Slaby 	return NULL;
113085cdaf52SJiri Slaby }
113185cdaf52SJiri Slaby 
113285cdaf52SJiri Slaby static const struct hid_device_id hid_blacklist[] = {
11338c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
11348c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
11358c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
11368c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
11378c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
11388c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
11398c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
11408c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
11418c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
11428c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
11438c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
11448c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
11458c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
11468c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
11478c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
11488c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
11498c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
11508c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
11518c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
11528c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
11538c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
11548c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
11558c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
11568c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
11578c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
11588c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
11598c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
11608c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
11618c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
11628c19a515SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
11635f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
11645f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
11655f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
11665f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
11675f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
11685f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
11695f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
11705f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_KBD) },
11715f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
11725f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
11735f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_LX3) },
11745f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_V150) },
11755f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
11765f22a799SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
11778c19a515SJiri Slaby 
11788c19a515SJiri Slaby 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
117985cdaf52SJiri Slaby 	{ }
118085cdaf52SJiri Slaby };
118185cdaf52SJiri Slaby 
118285cdaf52SJiri Slaby static int hid_bus_match(struct device *dev, struct device_driver *drv)
118385cdaf52SJiri Slaby {
118485cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
118585cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
118685cdaf52SJiri Slaby 
118785cdaf52SJiri Slaby 	if (!hid_match_id(hdev, hdrv->id_table))
118885cdaf52SJiri Slaby 		return 0;
118985cdaf52SJiri Slaby 
119085cdaf52SJiri Slaby 	/* generic wants all non-blacklisted */
119185cdaf52SJiri Slaby 	if (!strncmp(hdrv->name, "generic-", 8))
119285cdaf52SJiri Slaby 		return !hid_match_id(hdev, hid_blacklist);
119385cdaf52SJiri Slaby 
119485cdaf52SJiri Slaby 	return 1;
119585cdaf52SJiri Slaby }
119685cdaf52SJiri Slaby 
119785cdaf52SJiri Slaby static int hid_device_probe(struct device *dev)
119885cdaf52SJiri Slaby {
119985cdaf52SJiri Slaby 	struct hid_driver *hdrv = container_of(dev->driver,
120085cdaf52SJiri Slaby 			struct hid_driver, driver);
120185cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
120285cdaf52SJiri Slaby 	const struct hid_device_id *id;
120385cdaf52SJiri Slaby 	int ret = 0;
120485cdaf52SJiri Slaby 
120585cdaf52SJiri Slaby 	if (!hdev->driver) {
120685cdaf52SJiri Slaby 		id = hid_match_id(hdev, hdrv->id_table);
1207c500c971SJiri Slaby 		if (id == NULL)
1208c500c971SJiri Slaby 			return -ENODEV;
1209c500c971SJiri Slaby 
121085cdaf52SJiri Slaby 		hdev->driver = hdrv;
1211c500c971SJiri Slaby 		if (hdrv->probe) {
1212c500c971SJiri Slaby 			ret = hdrv->probe(hdev, id);
1213c500c971SJiri Slaby 		} else { /* default probe */
1214c500c971SJiri Slaby 			ret = hid_parse(hdev);
1215c500c971SJiri Slaby 			if (!ret)
1216c500c971SJiri Slaby 				ret = hid_hw_start(hdev);
1217c500c971SJiri Slaby 		}
1218c500c971SJiri Slaby 		if (ret)
1219c500c971SJiri Slaby 			hdev->driver = NULL;
122085cdaf52SJiri Slaby 	}
122185cdaf52SJiri Slaby 	return ret;
122285cdaf52SJiri Slaby }
122385cdaf52SJiri Slaby 
122485cdaf52SJiri Slaby static int hid_device_remove(struct device *dev)
122585cdaf52SJiri Slaby {
122685cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
122785cdaf52SJiri Slaby 	struct hid_driver *hdrv = hdev->driver;
122885cdaf52SJiri Slaby 
122985cdaf52SJiri Slaby 	if (hdrv) {
123085cdaf52SJiri Slaby 		if (hdrv->remove)
123185cdaf52SJiri Slaby 			hdrv->remove(hdev);
1232c500c971SJiri Slaby 		else /* default remove */
1233c500c971SJiri Slaby 			hid_hw_stop(hdev);
123485cdaf52SJiri Slaby 		hdev->driver = NULL;
123585cdaf52SJiri Slaby 	}
123685cdaf52SJiri Slaby 
123785cdaf52SJiri Slaby 	return 0;
123885cdaf52SJiri Slaby }
123985cdaf52SJiri Slaby 
124085cdaf52SJiri Slaby static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
124185cdaf52SJiri Slaby {
124285cdaf52SJiri Slaby 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
124385cdaf52SJiri Slaby 
124485cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
124585cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
124685cdaf52SJiri Slaby 		return -ENOMEM;
124785cdaf52SJiri Slaby 
124885cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
124985cdaf52SJiri Slaby 		return -ENOMEM;
125085cdaf52SJiri Slaby 
125185cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
125285cdaf52SJiri Slaby 		return -ENOMEM;
125385cdaf52SJiri Slaby 
125485cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
125585cdaf52SJiri Slaby 		return -ENOMEM;
125685cdaf52SJiri Slaby 
125785cdaf52SJiri Slaby 	if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
125885cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
125985cdaf52SJiri Slaby 		return -ENOMEM;
126085cdaf52SJiri Slaby 
126185cdaf52SJiri Slaby 	return 0;
126285cdaf52SJiri Slaby }
126385cdaf52SJiri Slaby 
126485cdaf52SJiri Slaby static struct bus_type hid_bus_type = {
126585cdaf52SJiri Slaby 	.name		= "hid",
126685cdaf52SJiri Slaby 	.match		= hid_bus_match,
126785cdaf52SJiri Slaby 	.probe		= hid_device_probe,
126885cdaf52SJiri Slaby 	.remove		= hid_device_remove,
126985cdaf52SJiri Slaby 	.uevent		= hid_uevent,
127085cdaf52SJiri Slaby };
127185cdaf52SJiri Slaby 
1272d458a9dfSJiri Slaby static const struct hid_device_id hid_ignore_list[] = {
1273d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1274d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1275d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1276d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1277d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1278d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1279d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1280d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1281d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1282d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1283d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1284d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1285d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1286d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1287d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1288d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1289d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1290d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1291d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1292d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1293d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1294d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1295d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1296d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1297d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1298d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1299d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1300d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1301d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1302d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1303d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1304d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1305d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1306d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1307d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1308d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1309d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1310d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1311d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1312d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1313d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1314d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1315d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1316d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1317d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1318d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1319d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1320d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1321d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1322d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1323d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1324d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1325d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1326d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1327d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1328d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1329d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1330d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1331d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1332d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1333d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1334d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1335d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1336d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1337d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1338d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1339d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1340d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1341d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1342d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1343d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1344d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1345d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1346d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1347d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1348d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1349d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1350d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1351d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1352d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1353d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1354d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1355d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1356d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1357d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1358d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1359d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1360d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1361d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1362d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1363d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1364d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1365d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1366d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1367d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1368d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1369d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1370d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1371d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1372d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1373d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1374d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1375d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1376d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1377d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1378d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1379d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1380d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1381d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1382d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1383d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1384d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1385d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1386d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1387d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1388d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1389d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1390d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1391d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1392d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1393d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1394d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1395d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1396d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1397d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD) },
1398d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1399d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1400d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1401d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1402d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1403d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1404d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1405d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1406d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1407d458a9dfSJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1408d458a9dfSJiri Slaby 	{ }
1409d458a9dfSJiri Slaby };
1410d458a9dfSJiri Slaby 
1411d458a9dfSJiri Slaby static bool hid_ignore(struct hid_device *hdev)
1412d458a9dfSJiri Slaby {
1413d458a9dfSJiri Slaby 	switch (hdev->vendor) {
1414d458a9dfSJiri Slaby 	case USB_VENDOR_ID_CODEMERCS:
1415d458a9dfSJiri Slaby 		/* ignore all Code Mercenaries IOWarrior devices */
1416d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1417d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1418d458a9dfSJiri Slaby 			return true;
1419d458a9dfSJiri Slaby 		break;
1420d458a9dfSJiri Slaby 	case USB_VENDOR_ID_LOGITECH:
1421d458a9dfSJiri Slaby 		if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1422d458a9dfSJiri Slaby 				hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1423d458a9dfSJiri Slaby 			return true;
1424d458a9dfSJiri Slaby 		break;
1425d458a9dfSJiri Slaby 	}
1426d458a9dfSJiri Slaby 
1427d458a9dfSJiri Slaby 	return !!hid_match_id(hdev, hid_ignore_list);
1428d458a9dfSJiri Slaby }
1429d458a9dfSJiri Slaby 
143085cdaf52SJiri Slaby int hid_add_device(struct hid_device *hdev)
143185cdaf52SJiri Slaby {
143285cdaf52SJiri Slaby 	static atomic_t id = ATOMIC_INIT(0);
143385cdaf52SJiri Slaby 	int ret;
143485cdaf52SJiri Slaby 
143585cdaf52SJiri Slaby 	if (WARN_ON(hdev->status & HID_STAT_ADDED))
143685cdaf52SJiri Slaby 		return -EBUSY;
143785cdaf52SJiri Slaby 
1438d458a9dfSJiri Slaby 	/* we need to kill them here, otherwise they will stay allocated to
1439d458a9dfSJiri Slaby 	 * wait for coming driver */
1440d458a9dfSJiri Slaby 	if (hid_ignore(hdev))
1441d458a9dfSJiri Slaby 		return -ENODEV;
1442d458a9dfSJiri Slaby 
144385cdaf52SJiri Slaby 	/* XXX hack, any other cleaner solution < 20 bus_id bytes? */
144485cdaf52SJiri Slaby 	sprintf(hdev->dev.bus_id, "%04X:%04X:%04X.%04X", hdev->bus,
144585cdaf52SJiri Slaby 			hdev->vendor, hdev->product, atomic_inc_return(&id));
144685cdaf52SJiri Slaby 
144785cdaf52SJiri Slaby 	ret = device_add(&hdev->dev);
144885cdaf52SJiri Slaby 	if (!ret)
144985cdaf52SJiri Slaby 		hdev->status |= HID_STAT_ADDED;
145085cdaf52SJiri Slaby 
145185cdaf52SJiri Slaby 	return ret;
145285cdaf52SJiri Slaby }
145385cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_add_device);
145485cdaf52SJiri Slaby 
145585cdaf52SJiri Slaby /**
145685cdaf52SJiri Slaby  * hid_allocate_device - allocate new hid device descriptor
145785cdaf52SJiri Slaby  *
145885cdaf52SJiri Slaby  * Allocate and initialize hid device, so that hid_destroy_device might be
145985cdaf52SJiri Slaby  * used to free it.
146085cdaf52SJiri Slaby  *
146185cdaf52SJiri Slaby  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
146285cdaf52SJiri Slaby  * error value.
146385cdaf52SJiri Slaby  */
146485cdaf52SJiri Slaby struct hid_device *hid_allocate_device(void)
146585cdaf52SJiri Slaby {
146685cdaf52SJiri Slaby 	struct hid_device *hdev;
146785cdaf52SJiri Slaby 	unsigned int i;
146885cdaf52SJiri Slaby 	int ret = -ENOMEM;
146985cdaf52SJiri Slaby 
147085cdaf52SJiri Slaby 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
147185cdaf52SJiri Slaby 	if (hdev == NULL)
147285cdaf52SJiri Slaby 		return ERR_PTR(ret);
147385cdaf52SJiri Slaby 
147485cdaf52SJiri Slaby 	device_initialize(&hdev->dev);
147585cdaf52SJiri Slaby 	hdev->dev.release = hid_device_release;
147685cdaf52SJiri Slaby 	hdev->dev.bus = &hid_bus_type;
147785cdaf52SJiri Slaby 
147885cdaf52SJiri Slaby 	hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
147985cdaf52SJiri Slaby 			sizeof(struct hid_collection), GFP_KERNEL);
148085cdaf52SJiri Slaby 	if (hdev->collection == NULL)
148185cdaf52SJiri Slaby 		goto err;
148285cdaf52SJiri Slaby 	hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
148385cdaf52SJiri Slaby 
148485cdaf52SJiri Slaby 	for (i = 0; i < HID_REPORT_TYPES; i++)
148585cdaf52SJiri Slaby 		INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
148685cdaf52SJiri Slaby 
148785cdaf52SJiri Slaby 	return hdev;
148885cdaf52SJiri Slaby err:
148985cdaf52SJiri Slaby 	put_device(&hdev->dev);
149085cdaf52SJiri Slaby 	return ERR_PTR(ret);
149185cdaf52SJiri Slaby }
149285cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_allocate_device);
149385cdaf52SJiri Slaby 
149485cdaf52SJiri Slaby static void hid_remove_device(struct hid_device *hdev)
149585cdaf52SJiri Slaby {
149685cdaf52SJiri Slaby 	if (hdev->status & HID_STAT_ADDED) {
149785cdaf52SJiri Slaby 		device_del(&hdev->dev);
149885cdaf52SJiri Slaby 		hdev->status &= ~HID_STAT_ADDED;
149985cdaf52SJiri Slaby 	}
150085cdaf52SJiri Slaby }
150185cdaf52SJiri Slaby 
150285cdaf52SJiri Slaby /**
150385cdaf52SJiri Slaby  * hid_destroy_device - free previously allocated device
150485cdaf52SJiri Slaby  *
150585cdaf52SJiri Slaby  * @hdev: hid device
150685cdaf52SJiri Slaby  *
150785cdaf52SJiri Slaby  * If you allocate hid_device through hid_allocate_device, you should ever
150885cdaf52SJiri Slaby  * free by this function.
150985cdaf52SJiri Slaby  */
151085cdaf52SJiri Slaby void hid_destroy_device(struct hid_device *hdev)
151185cdaf52SJiri Slaby {
151285cdaf52SJiri Slaby 	hid_remove_device(hdev);
151385cdaf52SJiri Slaby 	put_device(&hdev->dev);
151485cdaf52SJiri Slaby }
151585cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_destroy_device);
151685cdaf52SJiri Slaby 
151785cdaf52SJiri Slaby int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
151885cdaf52SJiri Slaby 		const char *mod_name)
151985cdaf52SJiri Slaby {
152085cdaf52SJiri Slaby 	hdrv->driver.name = hdrv->name;
152185cdaf52SJiri Slaby 	hdrv->driver.bus = &hid_bus_type;
152285cdaf52SJiri Slaby 	hdrv->driver.owner = owner;
152385cdaf52SJiri Slaby 	hdrv->driver.mod_name = mod_name;
152485cdaf52SJiri Slaby 
152585cdaf52SJiri Slaby 	return driver_register(&hdrv->driver);
152685cdaf52SJiri Slaby }
152785cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(__hid_register_driver);
152885cdaf52SJiri Slaby 
152985cdaf52SJiri Slaby void hid_unregister_driver(struct hid_driver *hdrv)
153085cdaf52SJiri Slaby {
153185cdaf52SJiri Slaby 	driver_unregister(&hdrv->driver);
153285cdaf52SJiri Slaby }
153385cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_unregister_driver);
153485cdaf52SJiri Slaby 
1535*02ae9a1aSJiri Slaby #ifdef CONFIG_HID_COMPAT
1536*02ae9a1aSJiri Slaby static void hid_compat_load(struct work_struct *ws)
1537*02ae9a1aSJiri Slaby {
1538*02ae9a1aSJiri Slaby 	request_module("hid-dummy");
1539*02ae9a1aSJiri Slaby }
1540*02ae9a1aSJiri Slaby static DECLARE_WORK(hid_compat_work, hid_compat_load);
1541*02ae9a1aSJiri Slaby #endif
1542*02ae9a1aSJiri Slaby 
154386166b7bSJiri Kosina static int __init hid_init(void)
154486166b7bSJiri Kosina {
154585cdaf52SJiri Slaby 	int ret;
154685cdaf52SJiri Slaby 
154785cdaf52SJiri Slaby 	ret = bus_register(&hid_bus_type);
154885cdaf52SJiri Slaby 	if (ret) {
154985cdaf52SJiri Slaby 		printk(KERN_ERR "HID: can't register hid bus\n");
155085cdaf52SJiri Slaby 		goto err;
155185cdaf52SJiri Slaby 	}
155285cdaf52SJiri Slaby 
155385cdaf52SJiri Slaby 	ret = hidraw_init();
155485cdaf52SJiri Slaby 	if (ret)
155585cdaf52SJiri Slaby 		goto err_bus;
155685cdaf52SJiri Slaby 
1557*02ae9a1aSJiri Slaby #ifdef CONFIG_HID_COMPAT
1558*02ae9a1aSJiri Slaby 	schedule_work(&hid_compat_work);
1559*02ae9a1aSJiri Slaby #endif
1560*02ae9a1aSJiri Slaby 
156185cdaf52SJiri Slaby 	return 0;
156285cdaf52SJiri Slaby err_bus:
156385cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
156485cdaf52SJiri Slaby err:
156585cdaf52SJiri Slaby 	return ret;
156686166b7bSJiri Kosina }
156786166b7bSJiri Kosina 
156886166b7bSJiri Kosina static void __exit hid_exit(void)
156986166b7bSJiri Kosina {
157086166b7bSJiri Kosina 	hidraw_exit();
157185cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
157286166b7bSJiri Kosina }
157386166b7bSJiri Kosina 
157486166b7bSJiri Kosina module_init(hid_init);
157586166b7bSJiri Kosina module_exit(hid_exit);
157686166b7bSJiri Kosina 
1577aa938f79SJiri Kosina MODULE_LICENSE(DRIVER_LICENSE);
1578aa938f79SJiri Kosina 
1579