xref: /openbmc/linux/drivers/hid/hid-core.c (revision c17a7476e4c41884d82e3675c25ceae982c07a63)
1dde5845aSJiri Kosina /*
2229695e5SJiri Kosina  *  HID support for Linux
3dde5845aSJiri Kosina  *
4dde5845aSJiri Kosina  *  Copyright (c) 1999 Andreas Gal
5dde5845aSJiri Kosina  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6dde5845aSJiri Kosina  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
76b1968d5SJiri Kosina  *  Copyright (c) 2006-2012 Jiri Kosina
8dde5845aSJiri Kosina  */
9dde5845aSJiri Kosina 
10dde5845aSJiri Kosina /*
11dde5845aSJiri Kosina  * This program is free software; you can redistribute it and/or modify it
12dde5845aSJiri Kosina  * under the terms of the GNU General Public License as published by the Free
13dde5845aSJiri Kosina  * Software Foundation; either version 2 of the License, or (at your option)
14dde5845aSJiri Kosina  * any later version.
15dde5845aSJiri Kosina  */
16dde5845aSJiri Kosina 
174291ee30SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
184291ee30SJoe Perches 
19dde5845aSJiri Kosina #include <linux/module.h>
20dde5845aSJiri Kosina #include <linux/slab.h>
21dde5845aSJiri Kosina #include <linux/init.h>
22dde5845aSJiri Kosina #include <linux/kernel.h>
23dde5845aSJiri Kosina #include <linux/list.h>
24dde5845aSJiri Kosina #include <linux/mm.h>
25dde5845aSJiri Kosina #include <linux/spinlock.h>
26dde5845aSJiri Kosina #include <asm/unaligned.h>
27dde5845aSJiri Kosina #include <asm/byteorder.h>
28dde5845aSJiri Kosina #include <linux/input.h>
29dde5845aSJiri Kosina #include <linux/wait.h>
3047a80edbSJiri Kosina #include <linux/vmalloc.h>
31c4124c9bSJiri Kosina #include <linux/sched.h>
324ea54542SDavid Herrmann #include <linux/semaphore.h>
33dde5845aSJiri Kosina 
34dde5845aSJiri Kosina #include <linux/hid.h>
35dde5845aSJiri Kosina #include <linux/hiddev.h>
36c080d89aSJiri Kosina #include <linux/hid-debug.h>
3786166b7bSJiri Kosina #include <linux/hidraw.h>
38dde5845aSJiri Kosina 
395f22a799SJiri Slaby #include "hid-ids.h"
405f22a799SJiri Slaby 
41dde5845aSJiri Kosina /*
42dde5845aSJiri Kosina  * Version Information
43dde5845aSJiri Kosina  */
44dde5845aSJiri Kosina 
4553149801SJiri Kosina #define DRIVER_DESC "HID core driver"
46dde5845aSJiri Kosina 
4758037eb9SJiri Kosina int hid_debug = 0;
48377e10fbSAnssi Hannula module_param_named(debug, hid_debug, int, 0600);
49cd667ce2SJiri Kosina MODULE_PARM_DESC(debug, "toggle HID debugging messages");
5058037eb9SJiri Kosina EXPORT_SYMBOL_GPL(hid_debug);
5158037eb9SJiri Kosina 
526b1968d5SJiri Kosina static int hid_ignore_special_drivers = 0;
536b1968d5SJiri Kosina module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
54643727a9SHans Petter Selasky MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
556b1968d5SJiri Kosina 
56dde5845aSJiri Kosina /*
57dde5845aSJiri Kosina  * Register a new report for a device.
58dde5845aSJiri Kosina  */
59dde5845aSJiri Kosina 
6090a006abSMichael Poole struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
61dde5845aSJiri Kosina {
62dde5845aSJiri Kosina 	struct hid_report_enum *report_enum = device->report_enum + type;
63dde5845aSJiri Kosina 	struct hid_report *report;
64dde5845aSJiri Kosina 
6543622021SKees Cook 	if (id >= HID_MAX_IDS)
6643622021SKees Cook 		return NULL;
67dde5845aSJiri Kosina 	if (report_enum->report_id_hash[id])
68dde5845aSJiri Kosina 		return report_enum->report_id_hash[id];
69dde5845aSJiri Kosina 
70a3789a17SJoe Perches 	report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
71a3789a17SJoe Perches 	if (!report)
72dde5845aSJiri Kosina 		return NULL;
73dde5845aSJiri Kosina 
74dde5845aSJiri Kosina 	if (id != 0)
75dde5845aSJiri Kosina 		report_enum->numbered = 1;
76dde5845aSJiri Kosina 
77dde5845aSJiri Kosina 	report->id = id;
78dde5845aSJiri Kosina 	report->type = type;
79dde5845aSJiri Kosina 	report->size = 0;
80dde5845aSJiri Kosina 	report->device = device;
81dde5845aSJiri Kosina 	report_enum->report_id_hash[id] = report;
82dde5845aSJiri Kosina 
83dde5845aSJiri Kosina 	list_add_tail(&report->list, &report_enum->report_list);
84dde5845aSJiri Kosina 
85dde5845aSJiri Kosina 	return report;
86dde5845aSJiri Kosina }
8790a006abSMichael Poole EXPORT_SYMBOL_GPL(hid_register_report);
88dde5845aSJiri Kosina 
89dde5845aSJiri Kosina /*
90dde5845aSJiri Kosina  * Register a new field for this report.
91dde5845aSJiri Kosina  */
92dde5845aSJiri Kosina 
93dde5845aSJiri Kosina static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
94dde5845aSJiri Kosina {
95dde5845aSJiri Kosina 	struct hid_field *field;
96dde5845aSJiri Kosina 
97dde5845aSJiri Kosina 	if (report->maxfield == HID_MAX_FIELDS) {
988c3d52fcSJiri Kosina 		hid_err(report->device, "too many fields in report\n");
99dde5845aSJiri Kosina 		return NULL;
100dde5845aSJiri Kosina 	}
101dde5845aSJiri Kosina 
102a3789a17SJoe Perches 	field = kzalloc((sizeof(struct hid_field) +
103a3789a17SJoe Perches 			 usages * sizeof(struct hid_usage) +
104a3789a17SJoe Perches 			 values * sizeof(unsigned)), GFP_KERNEL);
105a3789a17SJoe Perches 	if (!field)
106a3789a17SJoe Perches 		return NULL;
107dde5845aSJiri Kosina 
108dde5845aSJiri Kosina 	field->index = report->maxfield++;
109dde5845aSJiri Kosina 	report->field[field->index] = field;
110dde5845aSJiri Kosina 	field->usage = (struct hid_usage *)(field + 1);
111282bfd4cSJiri Slaby 	field->value = (s32 *)(field->usage + usages);
112dde5845aSJiri Kosina 	field->report = report;
113dde5845aSJiri Kosina 
114dde5845aSJiri Kosina 	return field;
115dde5845aSJiri Kosina }
116dde5845aSJiri Kosina 
117dde5845aSJiri Kosina /*
118dde5845aSJiri Kosina  * Open a collection. The type/usage is pushed on the stack.
119dde5845aSJiri Kosina  */
120dde5845aSJiri Kosina 
121dde5845aSJiri Kosina static int open_collection(struct hid_parser *parser, unsigned type)
122dde5845aSJiri Kosina {
123dde5845aSJiri Kosina 	struct hid_collection *collection;
124dde5845aSJiri Kosina 	unsigned usage;
125dde5845aSJiri Kosina 
126dde5845aSJiri Kosina 	usage = parser->local.usage[0];
127dde5845aSJiri Kosina 
128dde5845aSJiri Kosina 	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
1298c3d52fcSJiri Kosina 		hid_err(parser->device, "collection stack overflow\n");
130a6fbaacfSSachin Kamat 		return -EINVAL;
131dde5845aSJiri Kosina 	}
132dde5845aSJiri Kosina 
133dde5845aSJiri Kosina 	if (parser->device->maxcollection == parser->device->collection_size) {
134dde5845aSJiri Kosina 		collection = kmalloc(sizeof(struct hid_collection) *
135dde5845aSJiri Kosina 				parser->device->collection_size * 2, GFP_KERNEL);
136dde5845aSJiri Kosina 		if (collection == NULL) {
1378c3d52fcSJiri Kosina 			hid_err(parser->device, "failed to reallocate collection array\n");
138a6fbaacfSSachin Kamat 			return -ENOMEM;
139dde5845aSJiri Kosina 		}
140dde5845aSJiri Kosina 		memcpy(collection, parser->device->collection,
141dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
142dde5845aSJiri Kosina 			parser->device->collection_size);
143dde5845aSJiri Kosina 		memset(collection + parser->device->collection_size, 0,
144dde5845aSJiri Kosina 			sizeof(struct hid_collection) *
145dde5845aSJiri Kosina 			parser->device->collection_size);
146dde5845aSJiri Kosina 		kfree(parser->device->collection);
147dde5845aSJiri Kosina 		parser->device->collection = collection;
148dde5845aSJiri Kosina 		parser->device->collection_size *= 2;
149dde5845aSJiri Kosina 	}
150dde5845aSJiri Kosina 
151dde5845aSJiri Kosina 	parser->collection_stack[parser->collection_stack_ptr++] =
152dde5845aSJiri Kosina 		parser->device->maxcollection;
153dde5845aSJiri Kosina 
154dde5845aSJiri Kosina 	collection = parser->device->collection +
155dde5845aSJiri Kosina 		parser->device->maxcollection++;
156dde5845aSJiri Kosina 	collection->type = type;
157dde5845aSJiri Kosina 	collection->usage = usage;
158dde5845aSJiri Kosina 	collection->level = parser->collection_stack_ptr - 1;
159dde5845aSJiri Kosina 
160dde5845aSJiri Kosina 	if (type == HID_COLLECTION_APPLICATION)
161dde5845aSJiri Kosina 		parser->device->maxapplication++;
162dde5845aSJiri Kosina 
163dde5845aSJiri Kosina 	return 0;
164dde5845aSJiri Kosina }
165dde5845aSJiri Kosina 
166dde5845aSJiri Kosina /*
167dde5845aSJiri Kosina  * Close a collection.
168dde5845aSJiri Kosina  */
169dde5845aSJiri Kosina 
170dde5845aSJiri Kosina static int close_collection(struct hid_parser *parser)
171dde5845aSJiri Kosina {
172dde5845aSJiri Kosina 	if (!parser->collection_stack_ptr) {
1738c3d52fcSJiri Kosina 		hid_err(parser->device, "collection stack underflow\n");
174a6fbaacfSSachin Kamat 		return -EINVAL;
175dde5845aSJiri Kosina 	}
176dde5845aSJiri Kosina 	parser->collection_stack_ptr--;
177dde5845aSJiri Kosina 	return 0;
178dde5845aSJiri Kosina }
179dde5845aSJiri Kosina 
180dde5845aSJiri Kosina /*
181dde5845aSJiri Kosina  * Climb up the stack, search for the specified collection type
182dde5845aSJiri Kosina  * and return the usage.
183dde5845aSJiri Kosina  */
184dde5845aSJiri Kosina 
185dde5845aSJiri Kosina static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
186dde5845aSJiri Kosina {
187504499f2SJoe Perches 	struct hid_collection *collection = parser->device->collection;
188dde5845aSJiri Kosina 	int n;
189504499f2SJoe Perches 
190504499f2SJoe Perches 	for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
191504499f2SJoe Perches 		unsigned index = parser->collection_stack[n];
192504499f2SJoe Perches 		if (collection[index].type == type)
193504499f2SJoe Perches 			return collection[index].usage;
194504499f2SJoe Perches 	}
195dde5845aSJiri Kosina 	return 0; /* we know nothing about this usage type */
196dde5845aSJiri Kosina }
197dde5845aSJiri Kosina 
198dde5845aSJiri Kosina /*
199dde5845aSJiri Kosina  * Add a usage to the temporary parser table.
200dde5845aSJiri Kosina  */
201dde5845aSJiri Kosina 
202dde5845aSJiri Kosina static int hid_add_usage(struct hid_parser *parser, unsigned usage)
203dde5845aSJiri Kosina {
204dde5845aSJiri Kosina 	if (parser->local.usage_index >= HID_MAX_USAGES) {
2058c3d52fcSJiri Kosina 		hid_err(parser->device, "usage index exceeded\n");
206dde5845aSJiri Kosina 		return -1;
207dde5845aSJiri Kosina 	}
208dde5845aSJiri Kosina 	parser->local.usage[parser->local.usage_index] = usage;
209dde5845aSJiri Kosina 	parser->local.collection_index[parser->local.usage_index] =
210dde5845aSJiri Kosina 		parser->collection_stack_ptr ?
211dde5845aSJiri Kosina 		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
212dde5845aSJiri Kosina 	parser->local.usage_index++;
213dde5845aSJiri Kosina 	return 0;
214dde5845aSJiri Kosina }
215dde5845aSJiri Kosina 
216dde5845aSJiri Kosina /*
217dde5845aSJiri Kosina  * Register a new field for this report.
218dde5845aSJiri Kosina  */
219dde5845aSJiri Kosina 
220dde5845aSJiri Kosina static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
221dde5845aSJiri Kosina {
222dde5845aSJiri Kosina 	struct hid_report *report;
223dde5845aSJiri Kosina 	struct hid_field *field;
224cc6b54aaSBenjamin Tissoires 	unsigned usages;
225dde5845aSJiri Kosina 	unsigned offset;
226cc6b54aaSBenjamin Tissoires 	unsigned i;
227dde5845aSJiri Kosina 
228a3789a17SJoe Perches 	report = hid_register_report(parser->device, report_type, parser->global.report_id);
229a3789a17SJoe Perches 	if (!report) {
2308c3d52fcSJiri Kosina 		hid_err(parser->device, "hid_register_report failed\n");
231dde5845aSJiri Kosina 		return -1;
232dde5845aSJiri Kosina 	}
233dde5845aSJiri Kosina 
234bb2e1976SJiri Kosina 	/* Handle both signed and unsigned cases properly */
2350cd516c2Ssrinivas pandruvada 	if ((parser->global.logical_minimum < 0 &&
2360cd516c2Ssrinivas pandruvada 		parser->global.logical_maximum <
2370cd516c2Ssrinivas pandruvada 		parser->global.logical_minimum) ||
2380cd516c2Ssrinivas pandruvada 		(parser->global.logical_minimum >= 0 &&
2390cd516c2Ssrinivas pandruvada 		(__u32)parser->global.logical_maximum <
2400cd516c2Ssrinivas pandruvada 		(__u32)parser->global.logical_minimum)) {
2410cd516c2Ssrinivas pandruvada 		dbg_hid("logical range invalid 0x%x 0x%x\n",
2420cd516c2Ssrinivas pandruvada 			parser->global.logical_minimum,
2430cd516c2Ssrinivas pandruvada 			parser->global.logical_maximum);
244dde5845aSJiri Kosina 		return -1;
245dde5845aSJiri Kosina 	}
246dde5845aSJiri Kosina 
247dde5845aSJiri Kosina 	offset = report->size;
248dde5845aSJiri Kosina 	report->size += parser->global.report_size * parser->global.report_count;
249dde5845aSJiri Kosina 
250dde5845aSJiri Kosina 	if (!parser->local.usage_index) /* Ignore padding fields */
251dde5845aSJiri Kosina 		return 0;
252dde5845aSJiri Kosina 
253cc6b54aaSBenjamin Tissoires 	usages = max_t(unsigned, parser->local.usage_index,
254cc6b54aaSBenjamin Tissoires 				 parser->global.report_count);
255dde5845aSJiri Kosina 
256a3789a17SJoe Perches 	field = hid_register_field(report, usages, parser->global.report_count);
257a3789a17SJoe Perches 	if (!field)
258dde5845aSJiri Kosina 		return 0;
259dde5845aSJiri Kosina 
260dde5845aSJiri Kosina 	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
261dde5845aSJiri Kosina 	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
262dde5845aSJiri Kosina 	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
263dde5845aSJiri Kosina 
264dde5845aSJiri Kosina 	for (i = 0; i < usages; i++) {
265cc6b54aaSBenjamin Tissoires 		unsigned j = i;
266dde5845aSJiri Kosina 		/* Duplicate the last usage we parsed if we have excess values */
267dde5845aSJiri Kosina 		if (i >= parser->local.usage_index)
268dde5845aSJiri Kosina 			j = parser->local.usage_index - 1;
269dde5845aSJiri Kosina 		field->usage[i].hid = parser->local.usage[j];
270dde5845aSJiri Kosina 		field->usage[i].collection_index =
271dde5845aSJiri Kosina 			parser->local.collection_index[j];
272cc6b54aaSBenjamin Tissoires 		field->usage[i].usage_index = i;
273dde5845aSJiri Kosina 	}
274dde5845aSJiri Kosina 
275dde5845aSJiri Kosina 	field->maxusage = usages;
276dde5845aSJiri Kosina 	field->flags = flags;
277dde5845aSJiri Kosina 	field->report_offset = offset;
278dde5845aSJiri Kosina 	field->report_type = report_type;
279dde5845aSJiri Kosina 	field->report_size = parser->global.report_size;
280dde5845aSJiri Kosina 	field->report_count = parser->global.report_count;
281dde5845aSJiri Kosina 	field->logical_minimum = parser->global.logical_minimum;
282dde5845aSJiri Kosina 	field->logical_maximum = parser->global.logical_maximum;
283dde5845aSJiri Kosina 	field->physical_minimum = parser->global.physical_minimum;
284dde5845aSJiri Kosina 	field->physical_maximum = parser->global.physical_maximum;
285dde5845aSJiri Kosina 	field->unit_exponent = parser->global.unit_exponent;
286dde5845aSJiri Kosina 	field->unit = parser->global.unit;
287dde5845aSJiri Kosina 
288dde5845aSJiri Kosina 	return 0;
289dde5845aSJiri Kosina }
290dde5845aSJiri Kosina 
291dde5845aSJiri Kosina /*
292dde5845aSJiri Kosina  * Read data value from item.
293dde5845aSJiri Kosina  */
294dde5845aSJiri Kosina 
295dde5845aSJiri Kosina static u32 item_udata(struct hid_item *item)
296dde5845aSJiri Kosina {
297dde5845aSJiri Kosina 	switch (item->size) {
298dde5845aSJiri Kosina 	case 1: return item->data.u8;
299dde5845aSJiri Kosina 	case 2: return item->data.u16;
300dde5845aSJiri Kosina 	case 4: return item->data.u32;
301dde5845aSJiri Kosina 	}
302dde5845aSJiri Kosina 	return 0;
303dde5845aSJiri Kosina }
304dde5845aSJiri Kosina 
305dde5845aSJiri Kosina static s32 item_sdata(struct hid_item *item)
306dde5845aSJiri Kosina {
307dde5845aSJiri Kosina 	switch (item->size) {
308dde5845aSJiri Kosina 	case 1: return item->data.s8;
309dde5845aSJiri Kosina 	case 2: return item->data.s16;
310dde5845aSJiri Kosina 	case 4: return item->data.s32;
311dde5845aSJiri Kosina 	}
312dde5845aSJiri Kosina 	return 0;
313dde5845aSJiri Kosina }
314dde5845aSJiri Kosina 
315dde5845aSJiri Kosina /*
316dde5845aSJiri Kosina  * Process a global item.
317dde5845aSJiri Kosina  */
318dde5845aSJiri Kosina 
319dde5845aSJiri Kosina static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
320dde5845aSJiri Kosina {
321ad0e669bSNikolai Kondrashov 	__s32 raw_value;
322dde5845aSJiri Kosina 	switch (item->tag) {
323dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PUSH:
324dde5845aSJiri Kosina 
325dde5845aSJiri Kosina 		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
3268c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack overflow\n");
327dde5845aSJiri Kosina 			return -1;
328dde5845aSJiri Kosina 		}
329dde5845aSJiri Kosina 
330dde5845aSJiri Kosina 		memcpy(parser->global_stack + parser->global_stack_ptr++,
331dde5845aSJiri Kosina 			&parser->global, sizeof(struct hid_global));
332dde5845aSJiri Kosina 		return 0;
333dde5845aSJiri Kosina 
334dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_POP:
335dde5845aSJiri Kosina 
336dde5845aSJiri Kosina 		if (!parser->global_stack_ptr) {
3378c3d52fcSJiri Kosina 			hid_err(parser->device, "global environment stack underflow\n");
338dde5845aSJiri Kosina 			return -1;
339dde5845aSJiri Kosina 		}
340dde5845aSJiri Kosina 
341880d29f1SJiri Slaby 		memcpy(&parser->global, parser->global_stack +
342880d29f1SJiri Slaby 			--parser->global_stack_ptr, sizeof(struct hid_global));
343dde5845aSJiri Kosina 		return 0;
344dde5845aSJiri Kosina 
345dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
346dde5845aSJiri Kosina 		parser->global.usage_page = item_udata(item);
347dde5845aSJiri Kosina 		return 0;
348dde5845aSJiri Kosina 
349dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
350dde5845aSJiri Kosina 		parser->global.logical_minimum = item_sdata(item);
351dde5845aSJiri Kosina 		return 0;
352dde5845aSJiri Kosina 
353dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
354dde5845aSJiri Kosina 		if (parser->global.logical_minimum < 0)
355dde5845aSJiri Kosina 			parser->global.logical_maximum = item_sdata(item);
356dde5845aSJiri Kosina 		else
357dde5845aSJiri Kosina 			parser->global.logical_maximum = item_udata(item);
358dde5845aSJiri Kosina 		return 0;
359dde5845aSJiri Kosina 
360dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
361dde5845aSJiri Kosina 		parser->global.physical_minimum = item_sdata(item);
362dde5845aSJiri Kosina 		return 0;
363dde5845aSJiri Kosina 
364dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
365dde5845aSJiri Kosina 		if (parser->global.physical_minimum < 0)
366dde5845aSJiri Kosina 			parser->global.physical_maximum = item_sdata(item);
367dde5845aSJiri Kosina 		else
368dde5845aSJiri Kosina 			parser->global.physical_maximum = item_udata(item);
369dde5845aSJiri Kosina 		return 0;
370dde5845aSJiri Kosina 
371dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
372ad0e669bSNikolai Kondrashov 		/* Many devices provide unit exponent as a two's complement
373ad0e669bSNikolai Kondrashov 		 * nibble due to the common misunderstanding of HID
374ad0e669bSNikolai Kondrashov 		 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
375ad0e669bSNikolai Kondrashov 		 * both this and the standard encoding. */
376ad0e669bSNikolai Kondrashov 		raw_value = item_sdata(item);
37777463838SBenjamin Tissoires 		if (!(raw_value & 0xfffffff0))
37877463838SBenjamin Tissoires 			parser->global.unit_exponent = hid_snto32(raw_value, 4);
37977463838SBenjamin Tissoires 		else
38077463838SBenjamin Tissoires 			parser->global.unit_exponent = raw_value;
381dde5845aSJiri Kosina 		return 0;
382dde5845aSJiri Kosina 
383dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_UNIT:
384dde5845aSJiri Kosina 		parser->global.unit = item_udata(item);
385dde5845aSJiri Kosina 		return 0;
386dde5845aSJiri Kosina 
387dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
388880d29f1SJiri Slaby 		parser->global.report_size = item_udata(item);
38923408f95SMarek Vasut 		if (parser->global.report_size > 128) {
3908c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_size %d\n",
391880d29f1SJiri Slaby 					parser->global.report_size);
392dde5845aSJiri Kosina 			return -1;
393dde5845aSJiri Kosina 		}
394dde5845aSJiri Kosina 		return 0;
395dde5845aSJiri Kosina 
396dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
397880d29f1SJiri Slaby 		parser->global.report_count = item_udata(item);
398880d29f1SJiri Slaby 		if (parser->global.report_count > HID_MAX_USAGES) {
3998c3d52fcSJiri Kosina 			hid_err(parser->device, "invalid report_count %d\n",
400880d29f1SJiri Slaby 					parser->global.report_count);
401dde5845aSJiri Kosina 			return -1;
402dde5845aSJiri Kosina 		}
403dde5845aSJiri Kosina 		return 0;
404dde5845aSJiri Kosina 
405dde5845aSJiri Kosina 	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
406880d29f1SJiri Slaby 		parser->global.report_id = item_udata(item);
40743622021SKees Cook 		if (parser->global.report_id == 0 ||
40843622021SKees Cook 		    parser->global.report_id >= HID_MAX_IDS) {
40943622021SKees Cook 			hid_err(parser->device, "report_id %u is invalid\n",
41043622021SKees Cook 				parser->global.report_id);
411dde5845aSJiri Kosina 			return -1;
412dde5845aSJiri Kosina 		}
413dde5845aSJiri Kosina 		return 0;
414dde5845aSJiri Kosina 
415dde5845aSJiri Kosina 	default:
4168c3d52fcSJiri Kosina 		hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
417dde5845aSJiri Kosina 		return -1;
418dde5845aSJiri Kosina 	}
419dde5845aSJiri Kosina }
420dde5845aSJiri Kosina 
421dde5845aSJiri Kosina /*
422dde5845aSJiri Kosina  * Process a local item.
423dde5845aSJiri Kosina  */
424dde5845aSJiri Kosina 
425dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
426dde5845aSJiri Kosina {
427dde5845aSJiri Kosina 	__u32 data;
428dde5845aSJiri Kosina 	unsigned n;
429ba532198SBenjamin Tissoires 	__u32 count;
430dde5845aSJiri Kosina 
431dde5845aSJiri Kosina 	data = item_udata(item);
432dde5845aSJiri Kosina 
433dde5845aSJiri Kosina 	switch (item->tag) {
434dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_DELIMITER:
435dde5845aSJiri Kosina 
436dde5845aSJiri Kosina 		if (data) {
437dde5845aSJiri Kosina 			/*
438dde5845aSJiri Kosina 			 * We treat items before the first delimiter
439dde5845aSJiri Kosina 			 * as global to all usage sets (branch 0).
440dde5845aSJiri Kosina 			 * In the moment we process only these global
441dde5845aSJiri Kosina 			 * items and the first delimiter set.
442dde5845aSJiri Kosina 			 */
443dde5845aSJiri Kosina 			if (parser->local.delimiter_depth != 0) {
4448c3d52fcSJiri Kosina 				hid_err(parser->device, "nested delimiters\n");
445dde5845aSJiri Kosina 				return -1;
446dde5845aSJiri Kosina 			}
447dde5845aSJiri Kosina 			parser->local.delimiter_depth++;
448dde5845aSJiri Kosina 			parser->local.delimiter_branch++;
449dde5845aSJiri Kosina 		} else {
450dde5845aSJiri Kosina 			if (parser->local.delimiter_depth < 1) {
4518c3d52fcSJiri Kosina 				hid_err(parser->device, "bogus close delimiter\n");
452dde5845aSJiri Kosina 				return -1;
453dde5845aSJiri Kosina 			}
454dde5845aSJiri Kosina 			parser->local.delimiter_depth--;
455dde5845aSJiri Kosina 		}
45638ead6efSPaul Chavent 		return 0;
457dde5845aSJiri Kosina 
458dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE:
459dde5845aSJiri Kosina 
460dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
46158037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
462dde5845aSJiri Kosina 			return 0;
463dde5845aSJiri Kosina 		}
464dde5845aSJiri Kosina 
465dde5845aSJiri Kosina 		if (item->size <= 2)
466dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
467dde5845aSJiri Kosina 
468dde5845aSJiri Kosina 		return hid_add_usage(parser, data);
469dde5845aSJiri Kosina 
470dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
471dde5845aSJiri Kosina 
472dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
47358037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
474dde5845aSJiri Kosina 			return 0;
475dde5845aSJiri Kosina 		}
476dde5845aSJiri Kosina 
477dde5845aSJiri Kosina 		if (item->size <= 2)
478dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
479dde5845aSJiri Kosina 
480dde5845aSJiri Kosina 		parser->local.usage_minimum = data;
481dde5845aSJiri Kosina 		return 0;
482dde5845aSJiri Kosina 
483dde5845aSJiri Kosina 	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
484dde5845aSJiri Kosina 
485dde5845aSJiri Kosina 		if (parser->local.delimiter_branch > 1) {
48658037eb9SJiri Kosina 			dbg_hid("alternative usage ignored\n");
487dde5845aSJiri Kosina 			return 0;
488dde5845aSJiri Kosina 		}
489dde5845aSJiri Kosina 
490dde5845aSJiri Kosina 		if (item->size <= 2)
491dde5845aSJiri Kosina 			data = (parser->global.usage_page << 16) + data;
492dde5845aSJiri Kosina 
493ba532198SBenjamin Tissoires 		count = data - parser->local.usage_minimum;
494ba532198SBenjamin Tissoires 		if (count + parser->local.usage_index >= HID_MAX_USAGES) {
495ba532198SBenjamin Tissoires 			/*
496ba532198SBenjamin Tissoires 			 * We do not warn if the name is not set, we are
497ba532198SBenjamin Tissoires 			 * actually pre-scanning the device.
498ba532198SBenjamin Tissoires 			 */
499ba532198SBenjamin Tissoires 			if (dev_name(&parser->device->dev))
500ba532198SBenjamin Tissoires 				hid_warn(parser->device,
501ba532198SBenjamin Tissoires 					 "ignoring exceeding usage max\n");
502ba532198SBenjamin Tissoires 			data = HID_MAX_USAGES - parser->local.usage_index +
503ba532198SBenjamin Tissoires 				parser->local.usage_minimum - 1;
504ba532198SBenjamin Tissoires 			if (data <= 0) {
505ba532198SBenjamin Tissoires 				hid_err(parser->device,
506ba532198SBenjamin Tissoires 					"no more usage index available\n");
507ba532198SBenjamin Tissoires 				return -1;
508ba532198SBenjamin Tissoires 			}
509ba532198SBenjamin Tissoires 		}
510ba532198SBenjamin Tissoires 
511dde5845aSJiri Kosina 		for (n = parser->local.usage_minimum; n <= data; n++)
512dde5845aSJiri Kosina 			if (hid_add_usage(parser, n)) {
51358037eb9SJiri Kosina 				dbg_hid("hid_add_usage failed\n");
514dde5845aSJiri Kosina 				return -1;
515dde5845aSJiri Kosina 			}
516dde5845aSJiri Kosina 		return 0;
517dde5845aSJiri Kosina 
518dde5845aSJiri Kosina 	default:
519dde5845aSJiri Kosina 
52058037eb9SJiri Kosina 		dbg_hid("unknown local item tag 0x%x\n", item->tag);
521dde5845aSJiri Kosina 		return 0;
522dde5845aSJiri Kosina 	}
523dde5845aSJiri Kosina 	return 0;
524dde5845aSJiri Kosina }
525dde5845aSJiri Kosina 
526dde5845aSJiri Kosina /*
527dde5845aSJiri Kosina  * Process a main item.
528dde5845aSJiri Kosina  */
529dde5845aSJiri Kosina 
530dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
531dde5845aSJiri Kosina {
532dde5845aSJiri Kosina 	__u32 data;
533dde5845aSJiri Kosina 	int ret;
534dde5845aSJiri Kosina 
535dde5845aSJiri Kosina 	data = item_udata(item);
536dde5845aSJiri Kosina 
537dde5845aSJiri Kosina 	switch (item->tag) {
538dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
539dde5845aSJiri Kosina 		ret = open_collection(parser, data & 0xff);
540dde5845aSJiri Kosina 		break;
541dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
542dde5845aSJiri Kosina 		ret = close_collection(parser);
543dde5845aSJiri Kosina 		break;
544dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_INPUT:
545dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
546dde5845aSJiri Kosina 		break;
547dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_OUTPUT:
548dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
549dde5845aSJiri Kosina 		break;
550dde5845aSJiri Kosina 	case HID_MAIN_ITEM_TAG_FEATURE:
551dde5845aSJiri Kosina 		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
552dde5845aSJiri Kosina 		break;
553dde5845aSJiri Kosina 	default:
5547cb4774eSHans de Goede 		hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
555dde5845aSJiri Kosina 		ret = 0;
556dde5845aSJiri Kosina 	}
557dde5845aSJiri Kosina 
558dde5845aSJiri Kosina 	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
559dde5845aSJiri Kosina 
560dde5845aSJiri Kosina 	return ret;
561dde5845aSJiri Kosina }
562dde5845aSJiri Kosina 
563dde5845aSJiri Kosina /*
564dde5845aSJiri Kosina  * Process a reserved item.
565dde5845aSJiri Kosina  */
566dde5845aSJiri Kosina 
567dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
568dde5845aSJiri Kosina {
56958037eb9SJiri Kosina 	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
570dde5845aSJiri Kosina 	return 0;
571dde5845aSJiri Kosina }
572dde5845aSJiri Kosina 
573dde5845aSJiri Kosina /*
574dde5845aSJiri Kosina  * Free a report and all registered fields. The field->usage and
575dde5845aSJiri Kosina  * field->value table's are allocated behind the field, so we need
576dde5845aSJiri Kosina  * only to free(field) itself.
577dde5845aSJiri Kosina  */
578dde5845aSJiri Kosina 
579dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report)
580dde5845aSJiri Kosina {
581dde5845aSJiri Kosina 	unsigned n;
582dde5845aSJiri Kosina 
583dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
584dde5845aSJiri Kosina 		kfree(report->field[n]);
585dde5845aSJiri Kosina 	kfree(report);
586dde5845aSJiri Kosina }
587dde5845aSJiri Kosina 
588dde5845aSJiri Kosina /*
589a7197c2eSHenrik Rydberg  * Close report. This function returns the device
590a7197c2eSHenrik Rydberg  * state to the point prior to hid_open_report().
591dde5845aSJiri Kosina  */
592a7197c2eSHenrik Rydberg static void hid_close_report(struct hid_device *device)
593dde5845aSJiri Kosina {
594dde5845aSJiri Kosina 	unsigned i, j;
595dde5845aSJiri Kosina 
596dde5845aSJiri Kosina 	for (i = 0; i < HID_REPORT_TYPES; i++) {
597dde5845aSJiri Kosina 		struct hid_report_enum *report_enum = device->report_enum + i;
598dde5845aSJiri Kosina 
59943622021SKees Cook 		for (j = 0; j < HID_MAX_IDS; j++) {
600dde5845aSJiri Kosina 			struct hid_report *report = report_enum->report_id_hash[j];
601dde5845aSJiri Kosina 			if (report)
602dde5845aSJiri Kosina 				hid_free_report(report);
603dde5845aSJiri Kosina 		}
604a7197c2eSHenrik Rydberg 		memset(report_enum, 0, sizeof(*report_enum));
605a7197c2eSHenrik Rydberg 		INIT_LIST_HEAD(&report_enum->report_list);
606dde5845aSJiri Kosina 	}
607dde5845aSJiri Kosina 
608dde5845aSJiri Kosina 	kfree(device->rdesc);
609a7197c2eSHenrik Rydberg 	device->rdesc = NULL;
610a7197c2eSHenrik Rydberg 	device->rsize = 0;
611a7197c2eSHenrik Rydberg 
612767fe787SJiri Kosina 	kfree(device->collection);
613a7197c2eSHenrik Rydberg 	device->collection = NULL;
614a7197c2eSHenrik Rydberg 	device->collection_size = 0;
615a7197c2eSHenrik Rydberg 	device->maxcollection = 0;
616a7197c2eSHenrik Rydberg 	device->maxapplication = 0;
617a7197c2eSHenrik Rydberg 
618a7197c2eSHenrik Rydberg 	device->status &= ~HID_STAT_PARSED;
619a7197c2eSHenrik Rydberg }
620a7197c2eSHenrik Rydberg 
621a7197c2eSHenrik Rydberg /*
622a7197c2eSHenrik Rydberg  * Free a device structure, all reports, and all fields.
623a7197c2eSHenrik Rydberg  */
624a7197c2eSHenrik Rydberg 
625a7197c2eSHenrik Rydberg static void hid_device_release(struct device *dev)
626a7197c2eSHenrik Rydberg {
627ee79a8f8SGeliang Tang 	struct hid_device *hid = to_hid_device(dev);
628a7197c2eSHenrik Rydberg 
629a7197c2eSHenrik Rydberg 	hid_close_report(hid);
630a7197c2eSHenrik Rydberg 	kfree(hid->dev_rdesc);
631a7197c2eSHenrik Rydberg 	kfree(hid);
632dde5845aSJiri Kosina }
633dde5845aSJiri Kosina 
634dde5845aSJiri Kosina /*
635dde5845aSJiri Kosina  * Fetch a report description item from the data stream. We support long
636dde5845aSJiri Kosina  * items, though they are not used yet.
637dde5845aSJiri Kosina  */
638dde5845aSJiri Kosina 
639dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
640dde5845aSJiri Kosina {
641dde5845aSJiri Kosina 	u8 b;
642dde5845aSJiri Kosina 
643dde5845aSJiri Kosina 	if ((end - start) <= 0)
644dde5845aSJiri Kosina 		return NULL;
645dde5845aSJiri Kosina 
646dde5845aSJiri Kosina 	b = *start++;
647dde5845aSJiri Kosina 
648dde5845aSJiri Kosina 	item->type = (b >> 2) & 3;
649dde5845aSJiri Kosina 	item->tag  = (b >> 4) & 15;
650dde5845aSJiri Kosina 
651dde5845aSJiri Kosina 	if (item->tag == HID_ITEM_TAG_LONG) {
652dde5845aSJiri Kosina 
653dde5845aSJiri Kosina 		item->format = HID_ITEM_FORMAT_LONG;
654dde5845aSJiri Kosina 
655dde5845aSJiri Kosina 		if ((end - start) < 2)
656dde5845aSJiri Kosina 			return NULL;
657dde5845aSJiri Kosina 
658dde5845aSJiri Kosina 		item->size = *start++;
659dde5845aSJiri Kosina 		item->tag  = *start++;
660dde5845aSJiri Kosina 
661dde5845aSJiri Kosina 		if ((end - start) < item->size)
662dde5845aSJiri Kosina 			return NULL;
663dde5845aSJiri Kosina 
664dde5845aSJiri Kosina 		item->data.longdata = start;
665dde5845aSJiri Kosina 		start += item->size;
666dde5845aSJiri Kosina 		return start;
667dde5845aSJiri Kosina 	}
668dde5845aSJiri Kosina 
669dde5845aSJiri Kosina 	item->format = HID_ITEM_FORMAT_SHORT;
670dde5845aSJiri Kosina 	item->size = b & 3;
671dde5845aSJiri Kosina 
672dde5845aSJiri Kosina 	switch (item->size) {
673dde5845aSJiri Kosina 	case 0:
674dde5845aSJiri Kosina 		return start;
675dde5845aSJiri Kosina 
676dde5845aSJiri Kosina 	case 1:
677dde5845aSJiri Kosina 		if ((end - start) < 1)
678dde5845aSJiri Kosina 			return NULL;
679dde5845aSJiri Kosina 		item->data.u8 = *start++;
680dde5845aSJiri Kosina 		return start;
681dde5845aSJiri Kosina 
682dde5845aSJiri Kosina 	case 2:
683dde5845aSJiri Kosina 		if ((end - start) < 2)
684dde5845aSJiri Kosina 			return NULL;
685c105068fSHarvey Harrison 		item->data.u16 = get_unaligned_le16(start);
686dde5845aSJiri Kosina 		start = (__u8 *)((__le16 *)start + 1);
687dde5845aSJiri Kosina 		return start;
688dde5845aSJiri Kosina 
689dde5845aSJiri Kosina 	case 3:
690dde5845aSJiri Kosina 		item->size++;
691dde5845aSJiri Kosina 		if ((end - start) < 4)
692dde5845aSJiri Kosina 			return NULL;
693c105068fSHarvey Harrison 		item->data.u32 = get_unaligned_le32(start);
694dde5845aSJiri Kosina 		start = (__u8 *)((__le32 *)start + 1);
695dde5845aSJiri Kosina 		return start;
696dde5845aSJiri Kosina 	}
697dde5845aSJiri Kosina 
698dde5845aSJiri Kosina 	return NULL;
699dde5845aSJiri Kosina }
700dde5845aSJiri Kosina 
7013dc8fc08SBenjamin Tissoires static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
702734c6609SHenrik Rydberg {
7033dc8fc08SBenjamin Tissoires 	struct hid_device *hid = parser->device;
7043dc8fc08SBenjamin Tissoires 
7054fa3a583SHenrik Rydberg 	if (usage == HID_DG_CONTACTID)
7064fa3a583SHenrik Rydberg 		hid->group = HID_GROUP_MULTITOUCH;
707734c6609SHenrik Rydberg }
708734c6609SHenrik Rydberg 
709f961bd35SBenjamin Tissoires static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
710f961bd35SBenjamin Tissoires {
711f961bd35SBenjamin Tissoires 	if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
712f961bd35SBenjamin Tissoires 	    parser->global.report_size == 8)
713f961bd35SBenjamin Tissoires 		parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
714f961bd35SBenjamin Tissoires }
715f961bd35SBenjamin Tissoires 
7163dc8fc08SBenjamin Tissoires static void hid_scan_collection(struct hid_parser *parser, unsigned type)
7173dc8fc08SBenjamin Tissoires {
7183dc8fc08SBenjamin Tissoires 	struct hid_device *hid = parser->device;
719e39f2d59SAndrew Duggan 	int i;
7203dc8fc08SBenjamin Tissoires 
7213dc8fc08SBenjamin Tissoires 	if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
7223dc8fc08SBenjamin Tissoires 	    type == HID_COLLECTION_PHYSICAL)
7233dc8fc08SBenjamin Tissoires 		hid->group = HID_GROUP_SENSOR_HUB;
724be3b1634SAlan Wu 
725be3b1634SAlan Wu 	if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
7260a76ac80SDennis Chen 	    hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
727be3b1634SAlan Wu 	    hid->group == HID_GROUP_MULTITOUCH)
728be3b1634SAlan Wu 		hid->group = HID_GROUP_GENERIC;
729e39f2d59SAndrew Duggan 
730e39f2d59SAndrew Duggan 	if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
731e39f2d59SAndrew Duggan 		for (i = 0; i < parser->local.usage_index; i++)
732e39f2d59SAndrew Duggan 			if (parser->local.usage[i] == HID_GD_POINTER)
733e39f2d59SAndrew Duggan 				parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
734e39f2d59SAndrew Duggan 
735e39f2d59SAndrew Duggan 	if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
736e39f2d59SAndrew Duggan 		parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
7373dc8fc08SBenjamin Tissoires }
7383dc8fc08SBenjamin Tissoires 
7393dc8fc08SBenjamin Tissoires static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
7403dc8fc08SBenjamin Tissoires {
7413dc8fc08SBenjamin Tissoires 	__u32 data;
7423dc8fc08SBenjamin Tissoires 	int i;
7433dc8fc08SBenjamin Tissoires 
7443dc8fc08SBenjamin Tissoires 	data = item_udata(item);
7453dc8fc08SBenjamin Tissoires 
7463dc8fc08SBenjamin Tissoires 	switch (item->tag) {
7473dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
7483dc8fc08SBenjamin Tissoires 		hid_scan_collection(parser, data & 0xff);
7493dc8fc08SBenjamin Tissoires 		break;
7503dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_END_COLLECTION:
7513dc8fc08SBenjamin Tissoires 		break;
7523dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_INPUT:
753e24d0d39SBenjamin Tissoires 		/* ignore constant inputs, they will be ignored by hid-input */
754e24d0d39SBenjamin Tissoires 		if (data & HID_MAIN_ITEM_CONSTANT)
755e24d0d39SBenjamin Tissoires 			break;
7563dc8fc08SBenjamin Tissoires 		for (i = 0; i < parser->local.usage_index; i++)
7573dc8fc08SBenjamin Tissoires 			hid_scan_input_usage(parser, parser->local.usage[i]);
7583dc8fc08SBenjamin Tissoires 		break;
7593dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_OUTPUT:
7603dc8fc08SBenjamin Tissoires 		break;
7613dc8fc08SBenjamin Tissoires 	case HID_MAIN_ITEM_TAG_FEATURE:
762f961bd35SBenjamin Tissoires 		for (i = 0; i < parser->local.usage_index; i++)
763f961bd35SBenjamin Tissoires 			hid_scan_feature_usage(parser, parser->local.usage[i]);
7643dc8fc08SBenjamin Tissoires 		break;
7653dc8fc08SBenjamin Tissoires 	}
7663dc8fc08SBenjamin Tissoires 
7673dc8fc08SBenjamin Tissoires 	/* Reset the local parser environment */
7683dc8fc08SBenjamin Tissoires 	memset(&parser->local, 0, sizeof(parser->local));
7693dc8fc08SBenjamin Tissoires 
7703dc8fc08SBenjamin Tissoires 	return 0;
7713dc8fc08SBenjamin Tissoires }
7723dc8fc08SBenjamin Tissoires 
773734c6609SHenrik Rydberg /*
774734c6609SHenrik Rydberg  * Scan a report descriptor before the device is added to the bus.
775734c6609SHenrik Rydberg  * Sets device groups and other properties that determine what driver
776734c6609SHenrik Rydberg  * to load.
777734c6609SHenrik Rydberg  */
778734c6609SHenrik Rydberg static int hid_scan_report(struct hid_device *hid)
779734c6609SHenrik Rydberg {
7803dc8fc08SBenjamin Tissoires 	struct hid_parser *parser;
7813dc8fc08SBenjamin Tissoires 	struct hid_item item;
782734c6609SHenrik Rydberg 	__u8 *start = hid->dev_rdesc;
783734c6609SHenrik Rydberg 	__u8 *end = start + hid->dev_rsize;
7843dc8fc08SBenjamin Tissoires 	static int (*dispatch_type[])(struct hid_parser *parser,
7853dc8fc08SBenjamin Tissoires 				      struct hid_item *item) = {
7863dc8fc08SBenjamin Tissoires 		hid_scan_main,
7873dc8fc08SBenjamin Tissoires 		hid_parser_global,
7883dc8fc08SBenjamin Tissoires 		hid_parser_local,
7893dc8fc08SBenjamin Tissoires 		hid_parser_reserved
7903dc8fc08SBenjamin Tissoires 	};
791734c6609SHenrik Rydberg 
7923dc8fc08SBenjamin Tissoires 	parser = vzalloc(sizeof(struct hid_parser));
7933dc8fc08SBenjamin Tissoires 	if (!parser)
7943dc8fc08SBenjamin Tissoires 		return -ENOMEM;
7953dc8fc08SBenjamin Tissoires 
7963dc8fc08SBenjamin Tissoires 	parser->device = hid;
797734c6609SHenrik Rydberg 	hid->group = HID_GROUP_GENERIC;
798734c6609SHenrik Rydberg 
7993dc8fc08SBenjamin Tissoires 	/*
8003dc8fc08SBenjamin Tissoires 	 * The parsing is simpler than the one in hid_open_report() as we should
8013dc8fc08SBenjamin Tissoires 	 * be robust against hid errors. Those errors will be raised by
8023dc8fc08SBenjamin Tissoires 	 * hid_open_report() anyway.
8033dc8fc08SBenjamin Tissoires 	 */
8043dc8fc08SBenjamin Tissoires 	while ((start = fetch_item(start, end, &item)) != NULL)
8053dc8fc08SBenjamin Tissoires 		dispatch_type[item.type](parser, &item);
8063dc8fc08SBenjamin Tissoires 
807f961bd35SBenjamin Tissoires 	/*
808f961bd35SBenjamin Tissoires 	 * Handle special flags set during scanning.
809f961bd35SBenjamin Tissoires 	 */
810f961bd35SBenjamin Tissoires 	if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
811f961bd35SBenjamin Tissoires 	    (hid->group == HID_GROUP_MULTITOUCH))
812f961bd35SBenjamin Tissoires 		hid->group = HID_GROUP_MULTITOUCH_WIN_8;
813f961bd35SBenjamin Tissoires 
814ba391e5aSBenjamin Tissoires 	/*
815ba391e5aSBenjamin Tissoires 	 * Vendor specific handlings
816ba391e5aSBenjamin Tissoires 	 */
81729b47391SBenjamin Tissoires 	switch (hid->vendor) {
81829b47391SBenjamin Tissoires 	case USB_VENDOR_ID_WACOM:
81929b47391SBenjamin Tissoires 		hid->group = HID_GROUP_WACOM;
82029b47391SBenjamin Tissoires 		break;
821c241c5eeSBenjamin Tissoires 	case USB_VENDOR_ID_SYNAPTICS:
82284379d83SJiri Kosina 		if (hid->group == HID_GROUP_GENERIC)
823e39f2d59SAndrew Duggan 			if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
824e39f2d59SAndrew Duggan 			    && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
825e39f2d59SAndrew Duggan 				/*
826e39f2d59SAndrew Duggan 				 * hid-rmi should take care of them,
827e39f2d59SAndrew Duggan 				 * not hid-generic
828e39f2d59SAndrew Duggan 				 */
829c241c5eeSBenjamin Tissoires 				hid->group = HID_GROUP_RMI;
830c241c5eeSBenjamin Tissoires 		break;
83129b47391SBenjamin Tissoires 	}
83229b47391SBenjamin Tissoires 
8333dc8fc08SBenjamin Tissoires 	vfree(parser);
834734c6609SHenrik Rydberg 	return 0;
835734c6609SHenrik Rydberg }
836734c6609SHenrik Rydberg 
83785cdaf52SJiri Slaby /**
83885cdaf52SJiri Slaby  * hid_parse_report - parse device report
83985cdaf52SJiri Slaby  *
84085cdaf52SJiri Slaby  * @device: hid device
84185cdaf52SJiri Slaby  * @start: report start
84285cdaf52SJiri Slaby  * @size: report size
84385cdaf52SJiri Slaby  *
844a7197c2eSHenrik Rydberg  * Allocate the device report as read by the bus driver. This function should
845a7197c2eSHenrik Rydberg  * only be called from parse() in ll drivers.
846a7197c2eSHenrik Rydberg  */
847a7197c2eSHenrik Rydberg int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
848a7197c2eSHenrik Rydberg {
849a7197c2eSHenrik Rydberg 	hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
850a7197c2eSHenrik Rydberg 	if (!hid->dev_rdesc)
851a7197c2eSHenrik Rydberg 		return -ENOMEM;
852a7197c2eSHenrik Rydberg 	hid->dev_rsize = size;
853a7197c2eSHenrik Rydberg 	return 0;
854a7197c2eSHenrik Rydberg }
855a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_parse_report);
856a7197c2eSHenrik Rydberg 
857331415ffSKees Cook static const char * const hid_report_names[] = {
858331415ffSKees Cook 	"HID_INPUT_REPORT",
859331415ffSKees Cook 	"HID_OUTPUT_REPORT",
860331415ffSKees Cook 	"HID_FEATURE_REPORT",
861331415ffSKees Cook };
862331415ffSKees Cook /**
863331415ffSKees Cook  * hid_validate_values - validate existing device report's value indexes
864331415ffSKees Cook  *
865331415ffSKees Cook  * @device: hid device
866331415ffSKees Cook  * @type: which report type to examine
867331415ffSKees Cook  * @id: which report ID to examine (0 for first)
868331415ffSKees Cook  * @field_index: which report field to examine
869331415ffSKees Cook  * @report_counts: expected number of values
870331415ffSKees Cook  *
871331415ffSKees Cook  * Validate the number of values in a given field of a given report, after
872331415ffSKees Cook  * parsing.
873331415ffSKees Cook  */
874331415ffSKees Cook struct hid_report *hid_validate_values(struct hid_device *hid,
875331415ffSKees Cook 				       unsigned int type, unsigned int id,
876331415ffSKees Cook 				       unsigned int field_index,
877331415ffSKees Cook 				       unsigned int report_counts)
878331415ffSKees Cook {
879331415ffSKees Cook 	struct hid_report *report;
880331415ffSKees Cook 
881331415ffSKees Cook 	if (type > HID_FEATURE_REPORT) {
882331415ffSKees Cook 		hid_err(hid, "invalid HID report type %u\n", type);
883331415ffSKees Cook 		return NULL;
884331415ffSKees Cook 	}
885331415ffSKees Cook 
886331415ffSKees Cook 	if (id >= HID_MAX_IDS) {
887331415ffSKees Cook 		hid_err(hid, "invalid HID report id %u\n", id);
888331415ffSKees Cook 		return NULL;
889331415ffSKees Cook 	}
890331415ffSKees Cook 
891331415ffSKees Cook 	/*
892331415ffSKees Cook 	 * Explicitly not using hid_get_report() here since it depends on
893331415ffSKees Cook 	 * ->numbered being checked, which may not always be the case when
894331415ffSKees Cook 	 * drivers go to access report values.
895331415ffSKees Cook 	 */
8961b15d2e5SKees Cook 	if (id == 0) {
8971b15d2e5SKees Cook 		/*
8981b15d2e5SKees Cook 		 * Validating on id 0 means we should examine the first
8991b15d2e5SKees Cook 		 * report in the list.
9001b15d2e5SKees Cook 		 */
9011b15d2e5SKees Cook 		report = list_entry(
9021b15d2e5SKees Cook 				hid->report_enum[type].report_list.next,
9031b15d2e5SKees Cook 				struct hid_report, list);
9041b15d2e5SKees Cook 	} else {
905331415ffSKees Cook 		report = hid->report_enum[type].report_id_hash[id];
9061b15d2e5SKees Cook 	}
907331415ffSKees Cook 	if (!report) {
908331415ffSKees Cook 		hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
909331415ffSKees Cook 		return NULL;
910331415ffSKees Cook 	}
911331415ffSKees Cook 	if (report->maxfield <= field_index) {
912331415ffSKees Cook 		hid_err(hid, "not enough fields in %s %u\n",
913331415ffSKees Cook 			hid_report_names[type], id);
914331415ffSKees Cook 		return NULL;
915331415ffSKees Cook 	}
916331415ffSKees Cook 	if (report->field[field_index]->report_count < report_counts) {
917331415ffSKees Cook 		hid_err(hid, "not enough values in %s %u field %u\n",
918331415ffSKees Cook 			hid_report_names[type], id, field_index);
919331415ffSKees Cook 		return NULL;
920331415ffSKees Cook 	}
921331415ffSKees Cook 	return report;
922331415ffSKees Cook }
923331415ffSKees Cook EXPORT_SYMBOL_GPL(hid_validate_values);
924331415ffSKees Cook 
925a7197c2eSHenrik Rydberg /**
926a7197c2eSHenrik Rydberg  * hid_open_report - open a driver-specific device report
927a7197c2eSHenrik Rydberg  *
928a7197c2eSHenrik Rydberg  * @device: hid device
929a7197c2eSHenrik Rydberg  *
930dde5845aSJiri Kosina  * Parse a report description into a hid_device structure. Reports are
931dde5845aSJiri Kosina  * enumerated, fields are attached to these reports.
93285cdaf52SJiri Slaby  * 0 returned on success, otherwise nonzero error value.
933a7197c2eSHenrik Rydberg  *
934a7197c2eSHenrik Rydberg  * This function (or the equivalent hid_parse() macro) should only be
935a7197c2eSHenrik Rydberg  * called from probe() in drivers, before starting the device.
936dde5845aSJiri Kosina  */
937a7197c2eSHenrik Rydberg int hid_open_report(struct hid_device *device)
938dde5845aSJiri Kosina {
939dde5845aSJiri Kosina 	struct hid_parser *parser;
940dde5845aSJiri Kosina 	struct hid_item item;
941a7197c2eSHenrik Rydberg 	unsigned int size;
942a7197c2eSHenrik Rydberg 	__u8 *start;
94386e6b77eSKevin Daughtridge 	__u8 *buf;
944dde5845aSJiri Kosina 	__u8 *end;
94585cdaf52SJiri Slaby 	int ret;
946dde5845aSJiri Kosina 	static int (*dispatch_type[])(struct hid_parser *parser,
947dde5845aSJiri Kosina 				      struct hid_item *item) = {
948dde5845aSJiri Kosina 		hid_parser_main,
949dde5845aSJiri Kosina 		hid_parser_global,
950dde5845aSJiri Kosina 		hid_parser_local,
951dde5845aSJiri Kosina 		hid_parser_reserved
952dde5845aSJiri Kosina 	};
953dde5845aSJiri Kosina 
954a7197c2eSHenrik Rydberg 	if (WARN_ON(device->status & HID_STAT_PARSED))
955a7197c2eSHenrik Rydberg 		return -EBUSY;
956a7197c2eSHenrik Rydberg 
957a7197c2eSHenrik Rydberg 	start = device->dev_rdesc;
958a7197c2eSHenrik Rydberg 	if (WARN_ON(!start))
959a7197c2eSHenrik Rydberg 		return -ENODEV;
960a7197c2eSHenrik Rydberg 	size = device->dev_rsize;
961a7197c2eSHenrik Rydberg 
96286e6b77eSKevin Daughtridge 	buf = kmemdup(start, size, GFP_KERNEL);
96386e6b77eSKevin Daughtridge 	if (buf == NULL)
96485cdaf52SJiri Slaby 		return -ENOMEM;
96586e6b77eSKevin Daughtridge 
96686e6b77eSKevin Daughtridge 	if (device->driver->report_fixup)
96786e6b77eSKevin Daughtridge 		start = device->driver->report_fixup(device, buf, &size);
96886e6b77eSKevin Daughtridge 	else
96986e6b77eSKevin Daughtridge 		start = buf;
97086e6b77eSKevin Daughtridge 
97186e6b77eSKevin Daughtridge 	start = kmemdup(start, size, GFP_KERNEL);
97286e6b77eSKevin Daughtridge 	kfree(buf);
97386e6b77eSKevin Daughtridge 	if (start == NULL)
97486e6b77eSKevin Daughtridge 		return -ENOMEM;
97586e6b77eSKevin Daughtridge 
97686e6b77eSKevin Daughtridge 	device->rdesc = start;
977dde5845aSJiri Kosina 	device->rsize = size;
978dde5845aSJiri Kosina 
979fe258020SJoe Perches 	parser = vzalloc(sizeof(struct hid_parser));
98085cdaf52SJiri Slaby 	if (!parser) {
98185cdaf52SJiri Slaby 		ret = -ENOMEM;
98285cdaf52SJiri Slaby 		goto err;
983dde5845aSJiri Kosina 	}
98485cdaf52SJiri Slaby 
985dde5845aSJiri Kosina 	parser->device = device;
986dde5845aSJiri Kosina 
987dde5845aSJiri Kosina 	end = start + size;
988a7197c2eSHenrik Rydberg 
989a7197c2eSHenrik Rydberg 	device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
990a7197c2eSHenrik Rydberg 				     sizeof(struct hid_collection), GFP_KERNEL);
991a7197c2eSHenrik Rydberg 	if (!device->collection) {
992a7197c2eSHenrik Rydberg 		ret = -ENOMEM;
993a7197c2eSHenrik Rydberg 		goto err;
994a7197c2eSHenrik Rydberg 	}
995a7197c2eSHenrik Rydberg 	device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
996a7197c2eSHenrik Rydberg 
99785cdaf52SJiri Slaby 	ret = -EINVAL;
998dde5845aSJiri Kosina 	while ((start = fetch_item(start, end, &item)) != NULL) {
999dde5845aSJiri Kosina 
1000dde5845aSJiri Kosina 		if (item.format != HID_ITEM_FORMAT_SHORT) {
10018c3d52fcSJiri Kosina 			hid_err(device, "unexpected long global item\n");
100285cdaf52SJiri Slaby 			goto err;
1003dde5845aSJiri Kosina 		}
1004dde5845aSJiri Kosina 
1005dde5845aSJiri Kosina 		if (dispatch_type[item.type](parser, &item)) {
10068c3d52fcSJiri Kosina 			hid_err(device, "item %u %u %u %u parsing failed\n",
10074291ee30SJoe Perches 				item.format, (unsigned)item.size,
10084291ee30SJoe Perches 				(unsigned)item.type, (unsigned)item.tag);
100985cdaf52SJiri Slaby 			goto err;
1010dde5845aSJiri Kosina 		}
1011dde5845aSJiri Kosina 
1012dde5845aSJiri Kosina 		if (start == end) {
1013dde5845aSJiri Kosina 			if (parser->collection_stack_ptr) {
10148c3d52fcSJiri Kosina 				hid_err(device, "unbalanced collection at end of report description\n");
101585cdaf52SJiri Slaby 				goto err;
1016dde5845aSJiri Kosina 			}
1017dde5845aSJiri Kosina 			if (parser->local.delimiter_depth) {
10188c3d52fcSJiri Kosina 				hid_err(device, "unbalanced delimiter at end of report description\n");
101985cdaf52SJiri Slaby 				goto err;
1020dde5845aSJiri Kosina 			}
102147a80edbSJiri Kosina 			vfree(parser);
1022a7197c2eSHenrik Rydberg 			device->status |= HID_STAT_PARSED;
102385cdaf52SJiri Slaby 			return 0;
1024dde5845aSJiri Kosina 		}
1025dde5845aSJiri Kosina 	}
1026dde5845aSJiri Kosina 
10278c3d52fcSJiri Kosina 	hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
102885cdaf52SJiri Slaby err:
102947a80edbSJiri Kosina 	vfree(parser);
1030a7197c2eSHenrik Rydberg 	hid_close_report(device);
103185cdaf52SJiri Slaby 	return ret;
1032dde5845aSJiri Kosina }
1033a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_open_report);
1034dde5845aSJiri Kosina 
1035dde5845aSJiri Kosina /*
1036dde5845aSJiri Kosina  * Convert a signed n-bit integer to signed 32-bit integer. Common
1037dde5845aSJiri Kosina  * cases are done through the compiler, the screwed things has to be
1038dde5845aSJiri Kosina  * done by hand.
1039dde5845aSJiri Kosina  */
1040dde5845aSJiri Kosina 
1041dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n)
1042dde5845aSJiri Kosina {
1043dde5845aSJiri Kosina 	switch (n) {
1044dde5845aSJiri Kosina 	case 8:  return ((__s8)value);
1045dde5845aSJiri Kosina 	case 16: return ((__s16)value);
1046dde5845aSJiri Kosina 	case 32: return ((__s32)value);
1047dde5845aSJiri Kosina 	}
104808585e43SAndy Shevchenko 	return value & (1 << (n - 1)) ? value | (~0U << n) : value;
1049dde5845aSJiri Kosina }
1050dde5845aSJiri Kosina 
105177463838SBenjamin Tissoires s32 hid_snto32(__u32 value, unsigned n)
105277463838SBenjamin Tissoires {
105377463838SBenjamin Tissoires 	return snto32(value, n);
105477463838SBenjamin Tissoires }
105577463838SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_snto32);
105677463838SBenjamin Tissoires 
1057dde5845aSJiri Kosina /*
1058dde5845aSJiri Kosina  * Convert a signed 32-bit integer to a signed n-bit integer.
1059dde5845aSJiri Kosina  */
1060dde5845aSJiri Kosina 
1061dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n)
1062dde5845aSJiri Kosina {
1063dde5845aSJiri Kosina 	s32 a = value >> (n - 1);
1064dde5845aSJiri Kosina 	if (a && a != -1)
1065dde5845aSJiri Kosina 		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1066dde5845aSJiri Kosina 	return value & ((1 << n) - 1);
1067dde5845aSJiri Kosina }
1068dde5845aSJiri Kosina 
1069dde5845aSJiri Kosina /*
1070dde5845aSJiri Kosina  * Extract/implement a data field from/to a little endian report (bit array).
1071dde5845aSJiri Kosina  *
1072dde5845aSJiri Kosina  * Code sort-of follows HID spec:
10735137b354SDmitry Torokhov  *     http://www.usb.org/developers/hidpage/HID1_11.pdf
1074dde5845aSJiri Kosina  *
1075dde5845aSJiri Kosina  * While the USB HID spec allows unlimited length bit fields in "report
1076dde5845aSJiri Kosina  * descriptors", most devices never use more than 16 bits.
1077dde5845aSJiri Kosina  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1078dde5845aSJiri Kosina  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1079dde5845aSJiri Kosina  */
1080dde5845aSJiri Kosina 
10815137b354SDmitry Torokhov static u32 __extract(u8 *report, unsigned offset, int n)
10825137b354SDmitry Torokhov {
10835137b354SDmitry Torokhov 	unsigned int idx = offset / 8;
10845137b354SDmitry Torokhov 	unsigned int bit_nr = 0;
10855137b354SDmitry Torokhov 	unsigned int bit_shift = offset % 8;
10865137b354SDmitry Torokhov 	int bits_to_copy = 8 - bit_shift;
10875137b354SDmitry Torokhov 	u32 value = 0;
10885137b354SDmitry Torokhov 	u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
10895137b354SDmitry Torokhov 
10905137b354SDmitry Torokhov 	while (n > 0) {
10915137b354SDmitry Torokhov 		value |= ((u32)report[idx] >> bit_shift) << bit_nr;
10925137b354SDmitry Torokhov 		n -= bits_to_copy;
10935137b354SDmitry Torokhov 		bit_nr += bits_to_copy;
10945137b354SDmitry Torokhov 		bits_to_copy = 8;
10955137b354SDmitry Torokhov 		bit_shift = 0;
10965137b354SDmitry Torokhov 		idx++;
10975137b354SDmitry Torokhov 	}
10985137b354SDmitry Torokhov 
10995137b354SDmitry Torokhov 	return value & mask;
11005137b354SDmitry Torokhov }
11015137b354SDmitry Torokhov 
11025137b354SDmitry Torokhov u32 hid_field_extract(const struct hid_device *hid, u8 *report,
11034291ee30SJoe Perches 			unsigned offset, unsigned n)
1104dde5845aSJiri Kosina {
11055137b354SDmitry Torokhov 	if (n > 32) {
110604fba786SGoffredo Baroncelli 		hid_warn(hid, "hid_field_extract() called with n (%d) > 32! (%s)\n",
1107c4124c9bSJiri Kosina 			 n, current->comm);
11085137b354SDmitry Torokhov 		n = 32;
11095137b354SDmitry Torokhov 	}
1110dde5845aSJiri Kosina 
11115137b354SDmitry Torokhov 	return __extract(report, offset, n);
1112dde5845aSJiri Kosina }
111304fba786SGoffredo Baroncelli EXPORT_SYMBOL_GPL(hid_field_extract);
1114dde5845aSJiri Kosina 
1115dde5845aSJiri Kosina /*
1116dde5845aSJiri Kosina  * "implement" : set bits in a little endian bit stream.
1117dde5845aSJiri Kosina  * Same concepts as "extract" (see comments above).
1118dde5845aSJiri Kosina  * The data mangled in the bit stream remains in little endian
1119dde5845aSJiri Kosina  * order the whole time. It make more sense to talk about
1120dde5845aSJiri Kosina  * endianness of register values by considering a register
11215137b354SDmitry Torokhov  * a "cached" copy of the little endian bit stream.
1122dde5845aSJiri Kosina  */
1123dde5845aSJiri Kosina 
11245137b354SDmitry Torokhov static void __implement(u8 *report, unsigned offset, int n, u32 value)
11255137b354SDmitry Torokhov {
11265137b354SDmitry Torokhov 	unsigned int idx = offset / 8;
11275137b354SDmitry Torokhov 	unsigned int bit_shift = offset % 8;
11285137b354SDmitry Torokhov 	int bits_to_set = 8 - bit_shift;
11295137b354SDmitry Torokhov 
11305137b354SDmitry Torokhov 	while (n - bits_to_set >= 0) {
113195d1c895SDmitry Torokhov 		report[idx] &= ~(0xff << bit_shift);
11325137b354SDmitry Torokhov 		report[idx] |= value << bit_shift;
11335137b354SDmitry Torokhov 		value >>= bits_to_set;
11345137b354SDmitry Torokhov 		n -= bits_to_set;
11355137b354SDmitry Torokhov 		bits_to_set = 8;
11365137b354SDmitry Torokhov 		bit_shift = 0;
11375137b354SDmitry Torokhov 		idx++;
11385137b354SDmitry Torokhov 	}
11395137b354SDmitry Torokhov 
11405137b354SDmitry Torokhov 	/* last nibble */
11415137b354SDmitry Torokhov 	if (n) {
114295d1c895SDmitry Torokhov 		u8 bit_mask = ((1U << n) - 1);
114395d1c895SDmitry Torokhov 		report[idx] &= ~(bit_mask << bit_shift);
114495d1c895SDmitry Torokhov 		report[idx] |= value << bit_shift;
11455137b354SDmitry Torokhov 	}
11465137b354SDmitry Torokhov }
11475137b354SDmitry Torokhov 
11485137b354SDmitry Torokhov static void implement(const struct hid_device *hid, u8 *report,
11495137b354SDmitry Torokhov 		      unsigned offset, unsigned n, u32 value)
11505137b354SDmitry Torokhov {
115195d1c895SDmitry Torokhov 	if (unlikely(n > 32)) {
11524291ee30SJoe Perches 		hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
11534291ee30SJoe Perches 			 __func__, n, current->comm);
11545137b354SDmitry Torokhov 		n = 32;
115595d1c895SDmitry Torokhov 	} else if (n < 32) {
115695d1c895SDmitry Torokhov 		u32 m = (1U << n) - 1;
1157dde5845aSJiri Kosina 
115895d1c895SDmitry Torokhov 		if (unlikely(value > m)) {
115995d1c895SDmitry Torokhov 			hid_warn(hid,
116095d1c895SDmitry Torokhov 				 "%s() called with too large value %d (n: %d)! (%s)\n",
116195d1c895SDmitry Torokhov 				 __func__, value, n, current->comm);
116295d1c895SDmitry Torokhov 			WARN_ON(1);
1163dde5845aSJiri Kosina 			value &= m;
116495d1c895SDmitry Torokhov 		}
116595d1c895SDmitry Torokhov 	}
1166dde5845aSJiri Kosina 
11675137b354SDmitry Torokhov 	__implement(report, offset, n, value);
1168dde5845aSJiri Kosina }
1169dde5845aSJiri Kosina 
1170dde5845aSJiri Kosina /*
1171dde5845aSJiri Kosina  * Search an array for a value.
1172dde5845aSJiri Kosina  */
1173dde5845aSJiri Kosina 
117416ee4cc8SJoe Perches static int search(__s32 *array, __s32 value, unsigned n)
1175dde5845aSJiri Kosina {
1176dde5845aSJiri Kosina 	while (n--) {
1177dde5845aSJiri Kosina 		if (*array++ == value)
1178dde5845aSJiri Kosina 			return 0;
1179dde5845aSJiri Kosina 	}
1180dde5845aSJiri Kosina 	return -1;
1181dde5845aSJiri Kosina }
1182dde5845aSJiri Kosina 
118385cdaf52SJiri Slaby /**
118485cdaf52SJiri Slaby  * hid_match_report - check if driver's raw_event should be called
118585cdaf52SJiri Slaby  *
118685cdaf52SJiri Slaby  * @hid: hid device
118785cdaf52SJiri Slaby  * @report_type: type to match against
118885cdaf52SJiri Slaby  *
118985cdaf52SJiri Slaby  * compare hid->driver->report_table->report_type to report->type
119085cdaf52SJiri Slaby  */
119185cdaf52SJiri Slaby static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1192dde5845aSJiri Kosina {
119385cdaf52SJiri Slaby 	const struct hid_report_id *id = hid->driver->report_table;
119485cdaf52SJiri Slaby 
119585cdaf52SJiri Slaby 	if (!id) /* NULL means all */
119685cdaf52SJiri Slaby 		return 1;
119785cdaf52SJiri Slaby 
119885cdaf52SJiri Slaby 	for (; id->report_type != HID_TERMINATOR; id++)
119985cdaf52SJiri Slaby 		if (id->report_type == HID_ANY_ID ||
120085cdaf52SJiri Slaby 				id->report_type == report->type)
120185cdaf52SJiri Slaby 			return 1;
120285cdaf52SJiri Slaby 	return 0;
120385cdaf52SJiri Slaby }
120485cdaf52SJiri Slaby 
120585cdaf52SJiri Slaby /**
120685cdaf52SJiri Slaby  * hid_match_usage - check if driver's event should be called
120785cdaf52SJiri Slaby  *
120885cdaf52SJiri Slaby  * @hid: hid device
120985cdaf52SJiri Slaby  * @usage: usage to match against
121085cdaf52SJiri Slaby  *
121185cdaf52SJiri Slaby  * compare hid->driver->usage_table->usage_{type,code} to
121285cdaf52SJiri Slaby  * usage->usage_{type,code}
121385cdaf52SJiri Slaby  */
121485cdaf52SJiri Slaby static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
121585cdaf52SJiri Slaby {
121685cdaf52SJiri Slaby 	const struct hid_usage_id *id = hid->driver->usage_table;
121785cdaf52SJiri Slaby 
121885cdaf52SJiri Slaby 	if (!id) /* NULL means all */
121985cdaf52SJiri Slaby 		return 1;
122085cdaf52SJiri Slaby 
122185cdaf52SJiri Slaby 	for (; id->usage_type != HID_ANY_ID - 1; id++)
122285cdaf52SJiri Slaby 		if ((id->usage_hid == HID_ANY_ID ||
122385cdaf52SJiri Slaby 				id->usage_hid == usage->hid) &&
122485cdaf52SJiri Slaby 				(id->usage_type == HID_ANY_ID ||
122585cdaf52SJiri Slaby 				id->usage_type == usage->type) &&
122685cdaf52SJiri Slaby 				(id->usage_code == HID_ANY_ID ||
122785cdaf52SJiri Slaby 				 id->usage_code == usage->code))
122885cdaf52SJiri Slaby 			return 1;
122985cdaf52SJiri Slaby 	return 0;
123085cdaf52SJiri Slaby }
123185cdaf52SJiri Slaby 
123285cdaf52SJiri Slaby static void hid_process_event(struct hid_device *hid, struct hid_field *field,
123385cdaf52SJiri Slaby 		struct hid_usage *usage, __s32 value, int interrupt)
123485cdaf52SJiri Slaby {
123585cdaf52SJiri Slaby 	struct hid_driver *hdrv = hid->driver;
123685cdaf52SJiri Slaby 	int ret;
123785cdaf52SJiri Slaby 
12389bfc8da0SHenrik Rydberg 	if (!list_empty(&hid->debug_list))
1239cd667ce2SJiri Kosina 		hid_dump_input(hid, usage, value);
124085cdaf52SJiri Slaby 
124185cdaf52SJiri Slaby 	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
124285cdaf52SJiri Slaby 		ret = hdrv->event(hid, field, usage, value);
124385cdaf52SJiri Slaby 		if (ret != 0) {
124485cdaf52SJiri Slaby 			if (ret < 0)
12458c3d52fcSJiri Kosina 				hid_err(hid, "%s's event failed with %d\n",
124685cdaf52SJiri Slaby 						hdrv->name, ret);
124785cdaf52SJiri Slaby 			return;
124885cdaf52SJiri Slaby 		}
124985cdaf52SJiri Slaby 	}
125085cdaf52SJiri Slaby 
1251dde5845aSJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1252dde5845aSJiri Kosina 		hidinput_hid_event(hid, field, usage, value);
1253aa938f79SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1254aa938f79SJiri Kosina 		hid->hiddev_hid_event(hid, field, usage, value);
1255dde5845aSJiri Kosina }
1256dde5845aSJiri Kosina 
1257dde5845aSJiri Kosina /*
1258dde5845aSJiri Kosina  * Analyse a received field, and fetch the data from it. The field
1259dde5845aSJiri Kosina  * content is stored for next report processing (we do differential
1260dde5845aSJiri Kosina  * reporting to the layer).
1261dde5845aSJiri Kosina  */
1262dde5845aSJiri Kosina 
1263abdff0f7SAdrian Bunk static void hid_input_field(struct hid_device *hid, struct hid_field *field,
1264abdff0f7SAdrian Bunk 			    __u8 *data, int interrupt)
1265dde5845aSJiri Kosina {
1266dde5845aSJiri Kosina 	unsigned n;
1267dde5845aSJiri Kosina 	unsigned count = field->report_count;
1268dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
1269dde5845aSJiri Kosina 	unsigned size = field->report_size;
1270dde5845aSJiri Kosina 	__s32 min = field->logical_minimum;
1271dde5845aSJiri Kosina 	__s32 max = field->logical_maximum;
1272dde5845aSJiri Kosina 	__s32 *value;
1273dde5845aSJiri Kosina 
1274a3789a17SJoe Perches 	value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC);
1275a3789a17SJoe Perches 	if (!value)
1276dde5845aSJiri Kosina 		return;
1277dde5845aSJiri Kosina 
1278dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1279dde5845aSJiri Kosina 
12804291ee30SJoe Perches 		value[n] = min < 0 ?
128104fba786SGoffredo Baroncelli 			snto32(hid_field_extract(hid, data, offset + n * size,
128204fba786SGoffredo Baroncelli 			       size), size) :
128304fba786SGoffredo Baroncelli 			hid_field_extract(hid, data, offset + n * size, size);
1284dde5845aSJiri Kosina 
12854291ee30SJoe Perches 		/* Ignore report if ErrorRollOver */
12864291ee30SJoe Perches 		if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
12874291ee30SJoe Perches 		    value[n] >= min && value[n] <= max &&
128850220deaSBenjamin Tissoires 		    value[n] - min < field->maxusage &&
12894291ee30SJoe Perches 		    field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1290dde5845aSJiri Kosina 			goto exit;
1291dde5845aSJiri Kosina 	}
1292dde5845aSJiri Kosina 
1293dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1294dde5845aSJiri Kosina 
1295dde5845aSJiri Kosina 		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1296dde5845aSJiri Kosina 			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1297dde5845aSJiri Kosina 			continue;
1298dde5845aSJiri Kosina 		}
1299dde5845aSJiri Kosina 
1300dde5845aSJiri Kosina 		if (field->value[n] >= min && field->value[n] <= max
130150220deaSBenjamin Tissoires 			&& field->value[n] - min < field->maxusage
1302dde5845aSJiri Kosina 			&& field->usage[field->value[n] - min].hid
1303dde5845aSJiri Kosina 			&& search(value, field->value[n], count))
1304dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1305dde5845aSJiri Kosina 
1306dde5845aSJiri Kosina 		if (value[n] >= min && value[n] <= max
130750220deaSBenjamin Tissoires 			&& value[n] - min < field->maxusage
1308dde5845aSJiri Kosina 			&& field->usage[value[n] - min].hid
1309dde5845aSJiri Kosina 			&& search(field->value, value[n], count))
1310dde5845aSJiri Kosina 				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1311dde5845aSJiri Kosina 	}
1312dde5845aSJiri Kosina 
1313dde5845aSJiri Kosina 	memcpy(field->value, value, count * sizeof(__s32));
1314dde5845aSJiri Kosina exit:
1315dde5845aSJiri Kosina 	kfree(value);
1316dde5845aSJiri Kosina }
1317dde5845aSJiri Kosina 
1318dde5845aSJiri Kosina /*
1319dde5845aSJiri Kosina  * Output the field into the report.
1320dde5845aSJiri Kosina  */
1321dde5845aSJiri Kosina 
13224291ee30SJoe Perches static void hid_output_field(const struct hid_device *hid,
13234291ee30SJoe Perches 			     struct hid_field *field, __u8 *data)
1324dde5845aSJiri Kosina {
1325dde5845aSJiri Kosina 	unsigned count = field->report_count;
1326dde5845aSJiri Kosina 	unsigned offset = field->report_offset;
1327dde5845aSJiri Kosina 	unsigned size = field->report_size;
1328dde5845aSJiri Kosina 	unsigned n;
1329dde5845aSJiri Kosina 
1330dde5845aSJiri Kosina 	for (n = 0; n < count; n++) {
1331dde5845aSJiri Kosina 		if (field->logical_minimum < 0)	/* signed values */
13324291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
13334291ee30SJoe Perches 				  s32ton(field->value[n], size));
1334dde5845aSJiri Kosina 		else				/* unsigned values */
13354291ee30SJoe Perches 			implement(hid, data, offset + n * size, size,
13364291ee30SJoe Perches 				  field->value[n]);
1337dde5845aSJiri Kosina 	}
1338dde5845aSJiri Kosina }
1339dde5845aSJiri Kosina 
1340dde5845aSJiri Kosina /*
134127ce4050SJiri Kosina  * Create a report. 'data' has to be allocated using
134227ce4050SJiri Kosina  * hid_alloc_report_buf() so that it has proper size.
1343dde5845aSJiri Kosina  */
1344dde5845aSJiri Kosina 
1345229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data)
1346dde5845aSJiri Kosina {
1347dde5845aSJiri Kosina 	unsigned n;
1348dde5845aSJiri Kosina 
1349dde5845aSJiri Kosina 	if (report->id > 0)
1350dde5845aSJiri Kosina 		*data++ = report->id;
1351dde5845aSJiri Kosina 
135275c28df8SPete Zaitcev 	memset(data, 0, ((report->size - 1) >> 3) + 1);
1353dde5845aSJiri Kosina 	for (n = 0; n < report->maxfield; n++)
13544291ee30SJoe Perches 		hid_output_field(report->device, report->field[n], data);
1355dde5845aSJiri Kosina }
1356229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report);
1357dde5845aSJiri Kosina 
1358dde5845aSJiri Kosina /*
135927ce4050SJiri Kosina  * Allocator for buffer that is going to be passed to hid_output_report()
136027ce4050SJiri Kosina  */
136127ce4050SJiri Kosina u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
136227ce4050SJiri Kosina {
136327ce4050SJiri Kosina 	/*
136427ce4050SJiri Kosina 	 * 7 extra bytes are necessary to achieve proper functionality
136527ce4050SJiri Kosina 	 * of implement() working on 8 byte chunks
136627ce4050SJiri Kosina 	 */
136727ce4050SJiri Kosina 
136819e4ec52SBenjamin Tissoires 	int len = hid_report_len(report) + 7;
136927ce4050SJiri Kosina 
137027ce4050SJiri Kosina 	return kmalloc(len, flags);
137127ce4050SJiri Kosina }
137227ce4050SJiri Kosina EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
137327ce4050SJiri Kosina 
137427ce4050SJiri Kosina /*
1375dde5845aSJiri Kosina  * Set a field value. The report this field belongs to has to be
1376dde5845aSJiri Kosina  * created and transferred to the device, to set this value in the
1377dde5845aSJiri Kosina  * device.
1378dde5845aSJiri Kosina  */
1379dde5845aSJiri Kosina 
1380dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1381dde5845aSJiri Kosina {
1382be67b68dSKees Cook 	unsigned size;
1383be67b68dSKees Cook 
1384be67b68dSKees Cook 	if (!field)
1385be67b68dSKees Cook 		return -1;
1386be67b68dSKees Cook 
1387be67b68dSKees Cook 	size = field->report_size;
1388dde5845aSJiri Kosina 
1389cd667ce2SJiri Kosina 	hid_dump_input(field->report->device, field->usage + offset, value);
1390dde5845aSJiri Kosina 
1391dde5845aSJiri Kosina 	if (offset >= field->report_count) {
13928c3d52fcSJiri Kosina 		hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
13938c3d52fcSJiri Kosina 				offset, field->report_count);
1394dde5845aSJiri Kosina 		return -1;
1395dde5845aSJiri Kosina 	}
1396dde5845aSJiri Kosina 	if (field->logical_minimum < 0) {
1397dde5845aSJiri Kosina 		if (value != snto32(s32ton(value, size), size)) {
13988c3d52fcSJiri Kosina 			hid_err(field->report->device, "value %d is out of range\n", value);
1399dde5845aSJiri Kosina 			return -1;
1400dde5845aSJiri Kosina 		}
1401dde5845aSJiri Kosina 	}
1402dde5845aSJiri Kosina 	field->value[offset] = value;
1403dde5845aSJiri Kosina 	return 0;
1404dde5845aSJiri Kosina }
1405229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field);
1406dde5845aSJiri Kosina 
140785cdaf52SJiri Slaby static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
140885cdaf52SJiri Slaby 		const u8 *data)
140985cdaf52SJiri Slaby {
141085cdaf52SJiri Slaby 	struct hid_report *report;
141185cdaf52SJiri Slaby 	unsigned int n = 0;	/* Normally report number is 0 */
141285cdaf52SJiri Slaby 
141385cdaf52SJiri Slaby 	/* Device uses numbered reports, data[0] is report number */
141485cdaf52SJiri Slaby 	if (report_enum->numbered)
141585cdaf52SJiri Slaby 		n = *data;
141685cdaf52SJiri Slaby 
141785cdaf52SJiri Slaby 	report = report_enum->report_id_hash[n];
141885cdaf52SJiri Slaby 	if (report == NULL)
141985cdaf52SJiri Slaby 		dbg_hid("undefined report_id %u received\n", n);
142085cdaf52SJiri Slaby 
142185cdaf52SJiri Slaby 	return report;
142285cdaf52SJiri Slaby }
142385cdaf52SJiri Slaby 
14244fa5a7f7SBenjamin Tissoires /*
14254fa5a7f7SBenjamin Tissoires  * Implement a generic .request() callback, using .raw_request()
14264fa5a7f7SBenjamin Tissoires  * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
14274fa5a7f7SBenjamin Tissoires  */
14284fa5a7f7SBenjamin Tissoires void __hid_request(struct hid_device *hid, struct hid_report *report,
14294fa5a7f7SBenjamin Tissoires 		int reqtype)
14304fa5a7f7SBenjamin Tissoires {
14314fa5a7f7SBenjamin Tissoires 	char *buf;
14324fa5a7f7SBenjamin Tissoires 	int ret;
14334fa5a7f7SBenjamin Tissoires 	int len;
14344fa5a7f7SBenjamin Tissoires 
14354fa5a7f7SBenjamin Tissoires 	buf = hid_alloc_report_buf(report, GFP_KERNEL);
14364fa5a7f7SBenjamin Tissoires 	if (!buf)
14374fa5a7f7SBenjamin Tissoires 		return;
14384fa5a7f7SBenjamin Tissoires 
14394fa5a7f7SBenjamin Tissoires 	len = hid_report_len(report);
14404fa5a7f7SBenjamin Tissoires 
14414fa5a7f7SBenjamin Tissoires 	if (reqtype == HID_REQ_SET_REPORT)
14424fa5a7f7SBenjamin Tissoires 		hid_output_report(report, buf);
14434fa5a7f7SBenjamin Tissoires 
14444fa5a7f7SBenjamin Tissoires 	ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
14454fa5a7f7SBenjamin Tissoires 					  report->type, reqtype);
14464fa5a7f7SBenjamin Tissoires 	if (ret < 0) {
14474fa5a7f7SBenjamin Tissoires 		dbg_hid("unable to complete request: %d\n", ret);
14484fa5a7f7SBenjamin Tissoires 		goto out;
14494fa5a7f7SBenjamin Tissoires 	}
14504fa5a7f7SBenjamin Tissoires 
14514fa5a7f7SBenjamin Tissoires 	if (reqtype == HID_REQ_GET_REPORT)
14524fa5a7f7SBenjamin Tissoires 		hid_input_report(hid, report->type, buf, ret, 0);
14534fa5a7f7SBenjamin Tissoires 
14544fa5a7f7SBenjamin Tissoires out:
14554fa5a7f7SBenjamin Tissoires 	kfree(buf);
14564fa5a7f7SBenjamin Tissoires }
14574fa5a7f7SBenjamin Tissoires EXPORT_SYMBOL_GPL(__hid_request);
14584fa5a7f7SBenjamin Tissoires 
1459b6787242SJiri Kosina int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
146085cdaf52SJiri Slaby 		int interrupt)
1461aa8de2f0SJiri Kosina {
1462aa8de2f0SJiri Kosina 	struct hid_report_enum *report_enum = hid->report_enum + type;
1463aa8de2f0SJiri Kosina 	struct hid_report *report;
14646d85d037SBenjamin Tissoires 	struct hid_driver *hdrv;
146585cdaf52SJiri Slaby 	unsigned int a;
146685cdaf52SJiri Slaby 	int rsize, csize = size;
146785cdaf52SJiri Slaby 	u8 *cdata = data;
1468b6787242SJiri Kosina 	int ret = 0;
1469aa8de2f0SJiri Kosina 
147085cdaf52SJiri Slaby 	report = hid_get_report(report_enum, data);
147185cdaf52SJiri Slaby 	if (!report)
1472b6787242SJiri Kosina 		goto out;
1473aa8de2f0SJiri Kosina 
147485cdaf52SJiri Slaby 	if (report_enum->numbered) {
147585cdaf52SJiri Slaby 		cdata++;
147685cdaf52SJiri Slaby 		csize--;
1477aa8de2f0SJiri Kosina 	}
1478aa8de2f0SJiri Kosina 
1479aa8de2f0SJiri Kosina 	rsize = ((report->size - 1) >> 3) + 1;
1480aa8de2f0SJiri Kosina 
1481966922f2SArmando Visconti 	if (rsize > HID_MAX_BUFFER_SIZE)
1482966922f2SArmando Visconti 		rsize = HID_MAX_BUFFER_SIZE;
1483966922f2SArmando Visconti 
148485cdaf52SJiri Slaby 	if (csize < rsize) {
148585cdaf52SJiri Slaby 		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
148685cdaf52SJiri Slaby 				csize, rsize);
148785cdaf52SJiri Slaby 		memset(cdata + csize, 0, rsize - csize);
1488aa8de2f0SJiri Kosina 	}
1489aa8de2f0SJiri Kosina 
1490aa8de2f0SJiri Kosina 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1491aa8de2f0SJiri Kosina 		hid->hiddev_report_event(hid, report);
1492b6787242SJiri Kosina 	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1493b6787242SJiri Kosina 		ret = hidraw_report_event(hid, data, size);
1494b6787242SJiri Kosina 		if (ret)
1495b6787242SJiri Kosina 			goto out;
1496b6787242SJiri Kosina 	}
1497aa8de2f0SJiri Kosina 
1498cc6b54aaSBenjamin Tissoires 	if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
149985cdaf52SJiri Slaby 		for (a = 0; a < report->maxfield; a++)
150085cdaf52SJiri Slaby 			hid_input_field(hid, report->field[a], cdata, interrupt);
15016d85d037SBenjamin Tissoires 		hdrv = hid->driver;
15026d85d037SBenjamin Tissoires 		if (hdrv && hdrv->report)
15036d85d037SBenjamin Tissoires 			hdrv->report(hid, report);
1504b94e3c94SMatthieu CASTET 	}
1505aa8de2f0SJiri Kosina 
1506aa8de2f0SJiri Kosina 	if (hid->claimed & HID_CLAIMED_INPUT)
1507aa8de2f0SJiri Kosina 		hidinput_report_event(hid, report);
1508b6787242SJiri Kosina out:
1509b6787242SJiri Kosina 	return ret;
151085cdaf52SJiri Slaby }
151185cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_report_raw_event);
151285cdaf52SJiri Slaby 
151385cdaf52SJiri Slaby /**
151485cdaf52SJiri Slaby  * hid_input_report - report data from lower layer (usb, bt...)
151585cdaf52SJiri Slaby  *
151685cdaf52SJiri Slaby  * @hid: hid device
151785cdaf52SJiri Slaby  * @type: HID report type (HID_*_REPORT)
151885cdaf52SJiri Slaby  * @data: report contents
151985cdaf52SJiri Slaby  * @size: size of data parameter
1520ff9b00a2SJiri Kosina  * @interrupt: distinguish between interrupt and control transfers
152185cdaf52SJiri Slaby  *
152285cdaf52SJiri Slaby  * This is data entry for lower layers.
152385cdaf52SJiri Slaby  */
152485cdaf52SJiri Slaby int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
152585cdaf52SJiri Slaby {
152676c317d6SJulia Lawall 	struct hid_report_enum *report_enum;
152776c317d6SJulia Lawall 	struct hid_driver *hdrv;
152885cdaf52SJiri Slaby 	struct hid_report *report;
152945dc1ac7SJiri Kosina 	int ret = 0;
153085cdaf52SJiri Slaby 
15314ea54542SDavid Herrmann 	if (!hid)
153285cdaf52SJiri Slaby 		return -ENODEV;
15334ea54542SDavid Herrmann 
1534c849a614SAndrew de los Reyes 	if (down_trylock(&hid->driver_input_lock))
15354ea54542SDavid Herrmann 		return -EBUSY;
15364ea54542SDavid Herrmann 
15374ea54542SDavid Herrmann 	if (!hid->driver) {
15384ea54542SDavid Herrmann 		ret = -ENODEV;
15394ea54542SDavid Herrmann 		goto unlock;
15404ea54542SDavid Herrmann 	}
154176c317d6SJulia Lawall 	report_enum = hid->report_enum + type;
154276c317d6SJulia Lawall 	hdrv = hid->driver;
154385cdaf52SJiri Slaby 
154485cdaf52SJiri Slaby 	if (!size) {
154585cdaf52SJiri Slaby 		dbg_hid("empty report\n");
15464ea54542SDavid Herrmann 		ret = -1;
15474ea54542SDavid Herrmann 		goto unlock;
154885cdaf52SJiri Slaby 	}
154985cdaf52SJiri Slaby 
1550b94e3c94SMatthieu CASTET 	/* Avoid unnecessary overhead if debugfs is disabled */
1551a5f04b9dSBenjamin Tissoires 	if (!list_empty(&hid->debug_list))
1552a5f04b9dSBenjamin Tissoires 		hid_dump_report(hid, type, data, size);
1553b94e3c94SMatthieu CASTET 
1554f77e347bSJiri Kosina 	report = hid_get_report(report_enum, data);
1555f77e347bSJiri Kosina 
15564ea54542SDavid Herrmann 	if (!report) {
15574ea54542SDavid Herrmann 		ret = -1;
15584ea54542SDavid Herrmann 		goto unlock;
15594ea54542SDavid Herrmann 	}
1560f77e347bSJiri Kosina 
156185cdaf52SJiri Slaby 	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
156285cdaf52SJiri Slaby 		ret = hdrv->raw_event(hid, report, data, size);
1563556483e2SFelix Rueegg 		if (ret < 0)
15644ea54542SDavid Herrmann 			goto unlock;
15654ea54542SDavid Herrmann 	}
156685cdaf52SJiri Slaby 
1567b6787242SJiri Kosina 	ret = hid_report_raw_event(hid, type, data, size, interrupt);
1568aa8de2f0SJiri Kosina 
15694ea54542SDavid Herrmann unlock:
1570c849a614SAndrew de los Reyes 	up(&hid->driver_input_lock);
157145dc1ac7SJiri Kosina 	return ret;
1572aa8de2f0SJiri Kosina }
1573aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report);
1574aa8de2f0SJiri Kosina 
1575d5d3e202SBenjamin Tissoires bool hid_match_one_id(const struct hid_device *hdev,
15760f37cd03SJiri Kosina 		      const struct hid_device_id *id)
15770f37cd03SJiri Kosina {
15787431fb76SHenrik Rydberg 	return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
15794d53b801SHenrik Rydberg 		(id->group == HID_GROUP_ANY || id->group == hdev->group) &&
15800f37cd03SJiri Kosina 		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
15810f37cd03SJiri Kosina 		(id->product == HID_ANY_ID || id->product == hdev->product);
15820f37cd03SJiri Kosina }
15830f37cd03SJiri Kosina 
1584d5d3e202SBenjamin Tissoires const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
15850f37cd03SJiri Kosina 		const struct hid_device_id *id)
15860f37cd03SJiri Kosina {
15870f37cd03SJiri Kosina 	for (; id->bus; id++)
15880f37cd03SJiri Kosina 		if (hid_match_one_id(hdev, id))
15890f37cd03SJiri Kosina 			return id;
15900f37cd03SJiri Kosina 
15910f37cd03SJiri Kosina 	return NULL;
15920f37cd03SJiri Kosina }
15930f37cd03SJiri Kosina 
15940f37cd03SJiri Kosina static const struct hid_device_id hid_hiddev_list[] = {
1595c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1596c0bd6a42SRichard Hughes 	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
15970f37cd03SJiri Kosina 	{ }
15980f37cd03SJiri Kosina };
15990f37cd03SJiri Kosina 
16000f37cd03SJiri Kosina static bool hid_hiddev(struct hid_device *hdev)
16010f37cd03SJiri Kosina {
16020f37cd03SJiri Kosina 	return !!hid_match_id(hdev, hid_hiddev_list);
16030f37cd03SJiri Kosina }
16040f37cd03SJiri Kosina 
16056d3bfb74SAlan Ott 
16066d3bfb74SAlan Ott static ssize_t
16076d3bfb74SAlan Ott read_report_descriptor(struct file *filp, struct kobject *kobj,
16086d3bfb74SAlan Ott 		struct bin_attribute *attr,
16096d3bfb74SAlan Ott 		char *buf, loff_t off, size_t count)
16106d3bfb74SAlan Ott {
16112cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj);
1612ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
16136d3bfb74SAlan Ott 
16146d3bfb74SAlan Ott 	if (off >= hdev->rsize)
16156d3bfb74SAlan Ott 		return 0;
16166d3bfb74SAlan Ott 
16176d3bfb74SAlan Ott 	if (off + count > hdev->rsize)
16186d3bfb74SAlan Ott 		count = hdev->rsize - off;
16196d3bfb74SAlan Ott 
16206d3bfb74SAlan Ott 	memcpy(buf, hdev->rdesc + off, count);
16216d3bfb74SAlan Ott 
16226d3bfb74SAlan Ott 	return count;
16236d3bfb74SAlan Ott }
16246d3bfb74SAlan Ott 
1625a877417eSOlivier Gay static ssize_t
1626a877417eSOlivier Gay show_country(struct device *dev, struct device_attribute *attr,
1627a877417eSOlivier Gay 		char *buf)
1628a877417eSOlivier Gay {
1629ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1630a877417eSOlivier Gay 
1631a877417eSOlivier Gay 	return sprintf(buf, "%02x\n", hdev->country & 0xff);
1632a877417eSOlivier Gay }
1633a877417eSOlivier Gay 
16346d3bfb74SAlan Ott static struct bin_attribute dev_bin_attr_report_desc = {
16356d3bfb74SAlan Ott 	.attr = { .name = "report_descriptor", .mode = 0444 },
16366d3bfb74SAlan Ott 	.read = read_report_descriptor,
16376d3bfb74SAlan Ott 	.size = HID_MAX_DESCRIPTOR_SIZE,
16386d3bfb74SAlan Ott };
16396d3bfb74SAlan Ott 
1640ad8378edSBhumika Goyal static const struct device_attribute dev_attr_country = {
1641a877417eSOlivier Gay 	.attr = { .name = "country", .mode = 0444 },
1642a877417eSOlivier Gay 	.show = show_country,
1643a877417eSOlivier Gay };
1644a877417eSOlivier Gay 
164593c10132SJiri Slaby int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
164693c10132SJiri Slaby {
164793c10132SJiri Slaby 	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
164893c10132SJiri Slaby 		"Joystick", "Gamepad", "Keyboard", "Keypad",
164993c10132SJiri Slaby 		"Multi-Axis Controller"
165093c10132SJiri Slaby 	};
165193c10132SJiri Slaby 	const char *type, *bus;
165279b568b9SRichard Purdie 	char buf[64] = "";
165393c10132SJiri Slaby 	unsigned int i;
165493c10132SJiri Slaby 	int len;
16556d3bfb74SAlan Ott 	int ret;
165693c10132SJiri Slaby 
1657b5e5a37eSBastien Nocera 	if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1658b5e5a37eSBastien Nocera 		connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
16593a343ee4SDaniel Mack 	if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
16603a343ee4SDaniel Mack 		connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
166193c10132SJiri Slaby 	if (hdev->bus != BUS_USB)
166293c10132SJiri Slaby 		connect_mask &= ~HID_CONNECT_HIDDEV;
16630f37cd03SJiri Kosina 	if (hid_hiddev(hdev))
16640f37cd03SJiri Kosina 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
166593c10132SJiri Slaby 
166693c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
166793c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
166893c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_INPUT;
1669b77c3920SBenjamin Tissoires 
167093c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
167193c10132SJiri Slaby 			!hdev->hiddev_connect(hdev,
167293c10132SJiri Slaby 				connect_mask & HID_CONNECT_HIDDEV_FORCE))
167393c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDDEV;
167493c10132SJiri Slaby 	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
167593c10132SJiri Slaby 		hdev->claimed |= HID_CLAIMED_HIDRAW;
167693c10132SJiri Slaby 
16777704ac93SBenjamin Tissoires 	if (connect_mask & HID_CONNECT_DRIVER)
16787704ac93SBenjamin Tissoires 		hdev->claimed |= HID_CLAIMED_DRIVER;
16797704ac93SBenjamin Tissoires 
16804bc19f62SDavid Herrmann 	/* Drivers with the ->raw_event callback set are not required to connect
16814bc19f62SDavid Herrmann 	 * to any other listener. */
16824bc19f62SDavid Herrmann 	if (!hdev->claimed && !hdev->driver->raw_event) {
16834bc19f62SDavid Herrmann 		hid_err(hdev, "device has no listeners, quitting\n");
168493c10132SJiri Slaby 		return -ENODEV;
168593c10132SJiri Slaby 	}
168693c10132SJiri Slaby 
168793c10132SJiri Slaby 	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
168893c10132SJiri Slaby 			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
168993c10132SJiri Slaby 		hdev->ff_init(hdev);
169093c10132SJiri Slaby 
169193c10132SJiri Slaby 	len = 0;
169293c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_INPUT)
169393c10132SJiri Slaby 		len += sprintf(buf + len, "input");
169493c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
169593c10132SJiri Slaby 		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1696733aca90SJaejoong Kim 				((struct hiddev *)hdev->hiddev)->minor);
169793c10132SJiri Slaby 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
169893c10132SJiri Slaby 		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
169993c10132SJiri Slaby 				((struct hidraw *)hdev->hidraw)->minor);
170093c10132SJiri Slaby 
170193c10132SJiri Slaby 	type = "Device";
170293c10132SJiri Slaby 	for (i = 0; i < hdev->maxcollection; i++) {
170393c10132SJiri Slaby 		struct hid_collection *col = &hdev->collection[i];
170493c10132SJiri Slaby 		if (col->type == HID_COLLECTION_APPLICATION &&
170593c10132SJiri Slaby 		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
170693c10132SJiri Slaby 		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
170793c10132SJiri Slaby 			type = types[col->usage & 0xffff];
170893c10132SJiri Slaby 			break;
170993c10132SJiri Slaby 		}
171093c10132SJiri Slaby 	}
171193c10132SJiri Slaby 
171293c10132SJiri Slaby 	switch (hdev->bus) {
171393c10132SJiri Slaby 	case BUS_USB:
171493c10132SJiri Slaby 		bus = "USB";
171593c10132SJiri Slaby 		break;
171693c10132SJiri Slaby 	case BUS_BLUETOOTH:
171793c10132SJiri Slaby 		bus = "BLUETOOTH";
171893c10132SJiri Slaby 		break;
171906780727SDaniel Martin 	case BUS_I2C:
172006780727SDaniel Martin 		bus = "I2C";
172106780727SDaniel Martin 		break;
172293c10132SJiri Slaby 	default:
172393c10132SJiri Slaby 		bus = "<UNKNOWN>";
172493c10132SJiri Slaby 	}
172593c10132SJiri Slaby 
1726a877417eSOlivier Gay 	ret = device_create_file(&hdev->dev, &dev_attr_country);
1727a877417eSOlivier Gay 	if (ret)
1728a877417eSOlivier Gay 		hid_warn(hdev,
1729a877417eSOlivier Gay 			 "can't create sysfs country code attribute err: %d\n", ret);
1730a877417eSOlivier Gay 
17314291ee30SJoe Perches 	hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
173293c10132SJiri Slaby 		 buf, bus, hdev->version >> 8, hdev->version & 0xff,
173393c10132SJiri Slaby 		 type, hdev->name, hdev->phys);
173493c10132SJiri Slaby 
173593c10132SJiri Slaby 	return 0;
173693c10132SJiri Slaby }
173793c10132SJiri Slaby EXPORT_SYMBOL_GPL(hid_connect);
173893c10132SJiri Slaby 
1739c4c259bcSJiri Kosina void hid_disconnect(struct hid_device *hdev)
1740c4c259bcSJiri Kosina {
1741a877417eSOlivier Gay 	device_remove_file(&hdev->dev, &dev_attr_country);
1742c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_INPUT)
1743c4c259bcSJiri Kosina 		hidinput_disconnect(hdev);
1744c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDDEV)
1745c4c259bcSJiri Kosina 		hdev->hiddev_disconnect(hdev);
1746c4c259bcSJiri Kosina 	if (hdev->claimed & HID_CLAIMED_HIDRAW)
1747c4c259bcSJiri Kosina 		hidraw_disconnect(hdev);
17489c5c6ed7SBenjamin Tissoires 	hdev->claimed = 0;
1749c4c259bcSJiri Kosina }
1750c4c259bcSJiri Kosina EXPORT_SYMBOL_GPL(hid_disconnect);
1751c4c259bcSJiri Kosina 
1752aaac082dSDmitry Torokhov /**
1753aaac082dSDmitry Torokhov  * hid_hw_start - start underlying HW
1754aaac082dSDmitry Torokhov  * @hdev: hid device
1755aaac082dSDmitry Torokhov  * @connect_mask: which outputs to connect, see HID_CONNECT_*
1756aaac082dSDmitry Torokhov  *
1757aaac082dSDmitry Torokhov  * Call this in probe function *after* hid_parse. This will setup HW
1758aaac082dSDmitry Torokhov  * buffers and start the device (if not defeirred to device open).
1759aaac082dSDmitry Torokhov  * hid_hw_stop must be called if this was successful.
1760aaac082dSDmitry Torokhov  */
1761aaac082dSDmitry Torokhov int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1762aaac082dSDmitry Torokhov {
1763aaac082dSDmitry Torokhov 	int error;
1764aaac082dSDmitry Torokhov 
1765aaac082dSDmitry Torokhov 	error = hdev->ll_driver->start(hdev);
1766aaac082dSDmitry Torokhov 	if (error)
1767aaac082dSDmitry Torokhov 		return error;
1768aaac082dSDmitry Torokhov 
1769aaac082dSDmitry Torokhov 	if (connect_mask) {
1770aaac082dSDmitry Torokhov 		error = hid_connect(hdev, connect_mask);
1771aaac082dSDmitry Torokhov 		if (error) {
1772aaac082dSDmitry Torokhov 			hdev->ll_driver->stop(hdev);
1773aaac082dSDmitry Torokhov 			return error;
1774aaac082dSDmitry Torokhov 		}
1775aaac082dSDmitry Torokhov 	}
1776aaac082dSDmitry Torokhov 
1777aaac082dSDmitry Torokhov 	return 0;
1778aaac082dSDmitry Torokhov }
1779aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_start);
1780aaac082dSDmitry Torokhov 
1781aaac082dSDmitry Torokhov /**
1782aaac082dSDmitry Torokhov  * hid_hw_stop - stop underlying HW
1783aaac082dSDmitry Torokhov  * @hdev: hid device
1784aaac082dSDmitry Torokhov  *
1785aaac082dSDmitry Torokhov  * This is usually called from remove function or from probe when something
1786aaac082dSDmitry Torokhov  * failed and hid_hw_start was called already.
1787aaac082dSDmitry Torokhov  */
1788aaac082dSDmitry Torokhov void hid_hw_stop(struct hid_device *hdev)
1789aaac082dSDmitry Torokhov {
1790aaac082dSDmitry Torokhov 	hid_disconnect(hdev);
1791aaac082dSDmitry Torokhov 	hdev->ll_driver->stop(hdev);
1792aaac082dSDmitry Torokhov }
1793aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_stop);
1794aaac082dSDmitry Torokhov 
1795aaac082dSDmitry Torokhov /**
1796aaac082dSDmitry Torokhov  * hid_hw_open - signal underlying HW to start delivering events
1797aaac082dSDmitry Torokhov  * @hdev: hid device
1798aaac082dSDmitry Torokhov  *
1799aaac082dSDmitry Torokhov  * Tell underlying HW to start delivering events from the device.
1800aaac082dSDmitry Torokhov  * This function should be called sometime after successful call
1801aaac082dSDmitry Torokhov  * to hid_hiw_start().
1802aaac082dSDmitry Torokhov  */
1803aaac082dSDmitry Torokhov int hid_hw_open(struct hid_device *hdev)
1804aaac082dSDmitry Torokhov {
1805aaac082dSDmitry Torokhov 	int ret;
1806aaac082dSDmitry Torokhov 
1807aaac082dSDmitry Torokhov 	ret = mutex_lock_killable(&hdev->ll_open_lock);
1808aaac082dSDmitry Torokhov 	if (ret)
1809aaac082dSDmitry Torokhov 		return ret;
1810aaac082dSDmitry Torokhov 
1811aaac082dSDmitry Torokhov 	if (!hdev->ll_open_count++) {
1812aaac082dSDmitry Torokhov 		ret = hdev->ll_driver->open(hdev);
1813aaac082dSDmitry Torokhov 		if (ret)
1814aaac082dSDmitry Torokhov 			hdev->ll_open_count--;
1815aaac082dSDmitry Torokhov 	}
1816aaac082dSDmitry Torokhov 
1817aaac082dSDmitry Torokhov 	mutex_unlock(&hdev->ll_open_lock);
1818aaac082dSDmitry Torokhov 	return ret;
1819aaac082dSDmitry Torokhov }
1820aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_open);
1821aaac082dSDmitry Torokhov 
1822aaac082dSDmitry Torokhov /**
1823aaac082dSDmitry Torokhov  * hid_hw_close - signal underlaying HW to stop delivering events
1824aaac082dSDmitry Torokhov  *
1825aaac082dSDmitry Torokhov  * @hdev: hid device
1826aaac082dSDmitry Torokhov  *
1827aaac082dSDmitry Torokhov  * This function indicates that we are not interested in the events
1828aaac082dSDmitry Torokhov  * from this device anymore. Delivery of events may or may not stop,
1829aaac082dSDmitry Torokhov  * depending on the number of users still outstanding.
1830aaac082dSDmitry Torokhov  */
1831aaac082dSDmitry Torokhov void hid_hw_close(struct hid_device *hdev)
1832aaac082dSDmitry Torokhov {
1833aaac082dSDmitry Torokhov 	mutex_lock(&hdev->ll_open_lock);
1834aaac082dSDmitry Torokhov 	if (!--hdev->ll_open_count)
1835aaac082dSDmitry Torokhov 		hdev->ll_driver->close(hdev);
1836aaac082dSDmitry Torokhov 	mutex_unlock(&hdev->ll_open_lock);
1837aaac082dSDmitry Torokhov }
1838aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_close);
1839aaac082dSDmitry Torokhov 
18403a6f82f7SJiri Slaby struct hid_dynid {
18413a6f82f7SJiri Slaby 	struct list_head list;
18423a6f82f7SJiri Slaby 	struct hid_device_id id;
18433a6f82f7SJiri Slaby };
18443a6f82f7SJiri Slaby 
18453a6f82f7SJiri Slaby /**
18463a6f82f7SJiri Slaby  * store_new_id - add a new HID device ID to this driver and re-probe devices
18473a6f82f7SJiri Slaby  * @driver: target device driver
18483a6f82f7SJiri Slaby  * @buf: buffer for scanning device ID data
18493a6f82f7SJiri Slaby  * @count: input size
18503a6f82f7SJiri Slaby  *
18513a6f82f7SJiri Slaby  * Adds a new dynamic hid device ID to this driver,
18523a6f82f7SJiri Slaby  * and causes the driver to probe for all devices again.
18533a6f82f7SJiri Slaby  */
1854c2810325SGreg Kroah-Hartman static ssize_t new_id_store(struct device_driver *drv, const char *buf,
18553a6f82f7SJiri Slaby 		size_t count)
18563a6f82f7SJiri Slaby {
1857ba91a967SGeliang Tang 	struct hid_driver *hdrv = to_hid_driver(drv);
18583a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
18593a6f82f7SJiri Slaby 	__u32 bus, vendor, product;
18603a6f82f7SJiri Slaby 	unsigned long driver_data = 0;
18613a6f82f7SJiri Slaby 	int ret;
18623a6f82f7SJiri Slaby 
18633a6f82f7SJiri Slaby 	ret = sscanf(buf, "%x %x %x %lx",
18643a6f82f7SJiri Slaby 			&bus, &vendor, &product, &driver_data);
18653a6f82f7SJiri Slaby 	if (ret < 3)
18663a6f82f7SJiri Slaby 		return -EINVAL;
18673a6f82f7SJiri Slaby 
18683a6f82f7SJiri Slaby 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
18693a6f82f7SJiri Slaby 	if (!dynid)
18703a6f82f7SJiri Slaby 		return -ENOMEM;
18713a6f82f7SJiri Slaby 
18723a6f82f7SJiri Slaby 	dynid->id.bus = bus;
18734d53b801SHenrik Rydberg 	dynid->id.group = HID_GROUP_ANY;
18743a6f82f7SJiri Slaby 	dynid->id.vendor = vendor;
18753a6f82f7SJiri Slaby 	dynid->id.product = product;
18763a6f82f7SJiri Slaby 	dynid->id.driver_data = driver_data;
18773a6f82f7SJiri Slaby 
18783a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
18793a6f82f7SJiri Slaby 	list_add_tail(&dynid->list, &hdrv->dyn_list);
18803a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
18813a6f82f7SJiri Slaby 
18823a6f82f7SJiri Slaby 	ret = driver_attach(&hdrv->driver);
18833a6f82f7SJiri Slaby 
18843a6f82f7SJiri Slaby 	return ret ? : count;
18853a6f82f7SJiri Slaby }
1886c2810325SGreg Kroah-Hartman static DRIVER_ATTR_WO(new_id);
1887c2810325SGreg Kroah-Hartman 
1888c2810325SGreg Kroah-Hartman static struct attribute *hid_drv_attrs[] = {
1889c2810325SGreg Kroah-Hartman 	&driver_attr_new_id.attr,
1890c2810325SGreg Kroah-Hartman 	NULL,
1891c2810325SGreg Kroah-Hartman };
1892c2810325SGreg Kroah-Hartman ATTRIBUTE_GROUPS(hid_drv);
18933a6f82f7SJiri Slaby 
18943a6f82f7SJiri Slaby static void hid_free_dynids(struct hid_driver *hdrv)
18953a6f82f7SJiri Slaby {
18963a6f82f7SJiri Slaby 	struct hid_dynid *dynid, *n;
18973a6f82f7SJiri Slaby 
18983a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
18993a6f82f7SJiri Slaby 	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
19003a6f82f7SJiri Slaby 		list_del(&dynid->list);
19013a6f82f7SJiri Slaby 		kfree(dynid);
19023a6f82f7SJiri Slaby 	}
19033a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
19043a6f82f7SJiri Slaby }
19053a6f82f7SJiri Slaby 
1906e04a0442SBenjamin Tissoires const struct hid_device_id *hid_match_device(struct hid_device *hdev,
19073a6f82f7SJiri Slaby 					     struct hid_driver *hdrv)
19083a6f82f7SJiri Slaby {
19093a6f82f7SJiri Slaby 	struct hid_dynid *dynid;
19103a6f82f7SJiri Slaby 
19113a6f82f7SJiri Slaby 	spin_lock(&hdrv->dyn_lock);
19123a6f82f7SJiri Slaby 	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
19133a6f82f7SJiri Slaby 		if (hid_match_one_id(hdev, &dynid->id)) {
19143a6f82f7SJiri Slaby 			spin_unlock(&hdrv->dyn_lock);
19153a6f82f7SJiri Slaby 			return &dynid->id;
19163a6f82f7SJiri Slaby 		}
19173a6f82f7SJiri Slaby 	}
19183a6f82f7SJiri Slaby 	spin_unlock(&hdrv->dyn_lock);
19193a6f82f7SJiri Slaby 
19203a6f82f7SJiri Slaby 	return hid_match_id(hdev, hdrv->id_table);
19213a6f82f7SJiri Slaby }
1922e04a0442SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_match_device);
19233a6f82f7SJiri Slaby 
192485cdaf52SJiri Slaby static int hid_bus_match(struct device *dev, struct device_driver *drv)
192585cdaf52SJiri Slaby {
1926ba91a967SGeliang Tang 	struct hid_driver *hdrv = to_hid_driver(drv);
1927ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
192885cdaf52SJiri Slaby 
1929070748edSHenrik Rydberg 	return hid_match_device(hdev, hdrv) != NULL;
193085cdaf52SJiri Slaby }
193185cdaf52SJiri Slaby 
193285cdaf52SJiri Slaby static int hid_device_probe(struct device *dev)
193385cdaf52SJiri Slaby {
1934ba91a967SGeliang Tang 	struct hid_driver *hdrv = to_hid_driver(dev->driver);
1935ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
193685cdaf52SJiri Slaby 	const struct hid_device_id *id;
193785cdaf52SJiri Slaby 	int ret = 0;
193885cdaf52SJiri Slaby 
1939c849a614SAndrew de los Reyes 	if (down_interruptible(&hdev->driver_input_lock)) {
1940c849a614SAndrew de los Reyes 		ret = -EINTR;
19416f68f0acSBinoy Jayan 		goto end;
1942c849a614SAndrew de los Reyes 	}
1943c849a614SAndrew de los Reyes 	hdev->io_started = false;
19444ea54542SDavid Herrmann 
194585cdaf52SJiri Slaby 	if (!hdev->driver) {
19463a6f82f7SJiri Slaby 		id = hid_match_device(hdev, hdrv);
1947ba623a77SDan Carpenter 		if (id == NULL) {
1948ba623a77SDan Carpenter 			ret = -ENODEV;
1949ba623a77SDan Carpenter 			goto unlock;
1950ba623a77SDan Carpenter 		}
1951c500c971SJiri Slaby 
1952e04a0442SBenjamin Tissoires 		if (hdrv->match) {
1953e04a0442SBenjamin Tissoires 			if (!hdrv->match(hdev, hid_ignore_special_drivers)) {
1954e04a0442SBenjamin Tissoires 				ret = -ENODEV;
1955e04a0442SBenjamin Tissoires 				goto unlock;
1956e04a0442SBenjamin Tissoires 			}
1957e04a0442SBenjamin Tissoires 		} else {
1958e04a0442SBenjamin Tissoires 			/*
1959e04a0442SBenjamin Tissoires 			 * hid-generic implements .match(), so if
1960e04a0442SBenjamin Tissoires 			 * hid_ignore_special_drivers is set, we can safely
1961e04a0442SBenjamin Tissoires 			 * return.
1962e04a0442SBenjamin Tissoires 			 */
1963e04a0442SBenjamin Tissoires 			if (hid_ignore_special_drivers) {
1964e04a0442SBenjamin Tissoires 				ret = -ENODEV;
1965e04a0442SBenjamin Tissoires 				goto unlock;
1966e04a0442SBenjamin Tissoires 			}
1967e04a0442SBenjamin Tissoires 		}
1968e04a0442SBenjamin Tissoires 
196985cdaf52SJiri Slaby 		hdev->driver = hdrv;
1970c500c971SJiri Slaby 		if (hdrv->probe) {
1971c500c971SJiri Slaby 			ret = hdrv->probe(hdev, id);
1972c500c971SJiri Slaby 		} else { /* default probe */
1973a7197c2eSHenrik Rydberg 			ret = hid_open_report(hdev);
1974c500c971SJiri Slaby 			if (!ret)
197593c10132SJiri Slaby 				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1976c500c971SJiri Slaby 		}
1977a7197c2eSHenrik Rydberg 		if (ret) {
1978a7197c2eSHenrik Rydberg 			hid_close_report(hdev);
1979c500c971SJiri Slaby 			hdev->driver = NULL;
198085cdaf52SJiri Slaby 		}
1981a7197c2eSHenrik Rydberg 	}
1982ba623a77SDan Carpenter unlock:
1983c849a614SAndrew de los Reyes 	if (!hdev->io_started)
1984c849a614SAndrew de los Reyes 		up(&hdev->driver_input_lock);
19856f68f0acSBinoy Jayan end:
198685cdaf52SJiri Slaby 	return ret;
198785cdaf52SJiri Slaby }
198885cdaf52SJiri Slaby 
198985cdaf52SJiri Slaby static int hid_device_remove(struct device *dev)
199085cdaf52SJiri Slaby {
1991ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
19924ea54542SDavid Herrmann 	struct hid_driver *hdrv;
1993c849a614SAndrew de los Reyes 	int ret = 0;
199485cdaf52SJiri Slaby 
1995c849a614SAndrew de los Reyes 	if (down_interruptible(&hdev->driver_input_lock)) {
1996c849a614SAndrew de los Reyes 		ret = -EINTR;
19976f68f0acSBinoy Jayan 		goto end;
1998c849a614SAndrew de los Reyes 	}
1999c849a614SAndrew de los Reyes 	hdev->io_started = false;
20004ea54542SDavid Herrmann 
20014ea54542SDavid Herrmann 	hdrv = hdev->driver;
200285cdaf52SJiri Slaby 	if (hdrv) {
200385cdaf52SJiri Slaby 		if (hdrv->remove)
200485cdaf52SJiri Slaby 			hdrv->remove(hdev);
2005c500c971SJiri Slaby 		else /* default remove */
2006c500c971SJiri Slaby 			hid_hw_stop(hdev);
2007a7197c2eSHenrik Rydberg 		hid_close_report(hdev);
200885cdaf52SJiri Slaby 		hdev->driver = NULL;
200985cdaf52SJiri Slaby 	}
201085cdaf52SJiri Slaby 
2011c849a614SAndrew de los Reyes 	if (!hdev->io_started)
2012c849a614SAndrew de los Reyes 		up(&hdev->driver_input_lock);
20136f68f0acSBinoy Jayan end:
2014c849a614SAndrew de los Reyes 	return ret;
201585cdaf52SJiri Slaby }
201685cdaf52SJiri Slaby 
20174d53b801SHenrik Rydberg static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
20184d53b801SHenrik Rydberg 			     char *buf)
20194d53b801SHenrik Rydberg {
20204d53b801SHenrik Rydberg 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
20214d53b801SHenrik Rydberg 
2022dfa0c5faSRasmus Villemoes 	return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
20234d53b801SHenrik Rydberg 			 hdev->bus, hdev->group, hdev->vendor, hdev->product);
20244d53b801SHenrik Rydberg }
20250d4260e0SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
20264d53b801SHenrik Rydberg 
20270d4260e0SGreg Kroah-Hartman static struct attribute *hid_dev_attrs[] = {
20280d4260e0SGreg Kroah-Hartman 	&dev_attr_modalias.attr,
20290d4260e0SGreg Kroah-Hartman 	NULL,
20304d53b801SHenrik Rydberg };
203154f32fd5SAndy Lutomirski static struct bin_attribute *hid_dev_bin_attrs[] = {
203254f32fd5SAndy Lutomirski 	&dev_bin_attr_report_desc,
203354f32fd5SAndy Lutomirski 	NULL
203454f32fd5SAndy Lutomirski };
203554f32fd5SAndy Lutomirski static const struct attribute_group hid_dev_group = {
203654f32fd5SAndy Lutomirski 	.attrs = hid_dev_attrs,
203754f32fd5SAndy Lutomirski 	.bin_attrs = hid_dev_bin_attrs,
203854f32fd5SAndy Lutomirski };
203954f32fd5SAndy Lutomirski __ATTRIBUTE_GROUPS(hid_dev);
20404d53b801SHenrik Rydberg 
204185cdaf52SJiri Slaby static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
204285cdaf52SJiri Slaby {
2043ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
204485cdaf52SJiri Slaby 
204585cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
204685cdaf52SJiri Slaby 			hdev->bus, hdev->vendor, hdev->product))
204785cdaf52SJiri Slaby 		return -ENOMEM;
204885cdaf52SJiri Slaby 
204985cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
205085cdaf52SJiri Slaby 		return -ENOMEM;
205185cdaf52SJiri Slaby 
205285cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
205385cdaf52SJiri Slaby 		return -ENOMEM;
205485cdaf52SJiri Slaby 
205585cdaf52SJiri Slaby 	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
205685cdaf52SJiri Slaby 		return -ENOMEM;
205785cdaf52SJiri Slaby 
20584d53b801SHenrik Rydberg 	if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
20594d53b801SHenrik Rydberg 			   hdev->bus, hdev->group, hdev->vendor, hdev->product))
206085cdaf52SJiri Slaby 		return -ENOMEM;
206185cdaf52SJiri Slaby 
206285cdaf52SJiri Slaby 	return 0;
206385cdaf52SJiri Slaby }
206485cdaf52SJiri Slaby 
2065e04a0442SBenjamin Tissoires struct bus_type hid_bus_type = {
206685cdaf52SJiri Slaby 	.name		= "hid",
20670d4260e0SGreg Kroah-Hartman 	.dev_groups	= hid_dev_groups,
2068c2810325SGreg Kroah-Hartman 	.drv_groups	= hid_drv_groups,
206985cdaf52SJiri Slaby 	.match		= hid_bus_match,
207085cdaf52SJiri Slaby 	.probe		= hid_device_probe,
207185cdaf52SJiri Slaby 	.remove		= hid_device_remove,
207285cdaf52SJiri Slaby 	.uevent		= hid_uevent,
207385cdaf52SJiri Slaby };
2074d5d3e202SBenjamin Tissoires EXPORT_SYMBOL(hid_bus_type);
2075d458a9dfSJiri Slaby 
207685cdaf52SJiri Slaby int hid_add_device(struct hid_device *hdev)
207785cdaf52SJiri Slaby {
207885cdaf52SJiri Slaby 	static atomic_t id = ATOMIC_INIT(0);
207985cdaf52SJiri Slaby 	int ret;
208085cdaf52SJiri Slaby 
208185cdaf52SJiri Slaby 	if (WARN_ON(hdev->status & HID_STAT_ADDED))
208285cdaf52SJiri Slaby 		return -EBUSY;
208385cdaf52SJiri Slaby 
2084d5d3e202SBenjamin Tissoires 	hdev->quirks = hid_lookup_quirk(hdev);
2085d5d3e202SBenjamin Tissoires 
2086d458a9dfSJiri Slaby 	/* we need to kill them here, otherwise they will stay allocated to
2087d458a9dfSJiri Slaby 	 * wait for coming driver */
20884529eefaSLamarque V. Souza 	if (hid_ignore(hdev))
2089d458a9dfSJiri Slaby 		return -ENODEV;
2090d458a9dfSJiri Slaby 
2091a7197c2eSHenrik Rydberg 	/*
20923c86726cSBenjamin Tissoires 	 * Check for the mandatory transport channel.
20933c86726cSBenjamin Tissoires 	 */
20943c86726cSBenjamin Tissoires 	 if (!hdev->ll_driver->raw_request) {
20953c86726cSBenjamin Tissoires 		hid_err(hdev, "transport driver missing .raw_request()\n");
20963c86726cSBenjamin Tissoires 		return -EINVAL;
20973c86726cSBenjamin Tissoires 	 }
20983c86726cSBenjamin Tissoires 
20993c86726cSBenjamin Tissoires 	/*
2100a7197c2eSHenrik Rydberg 	 * Read the device report descriptor once and use as template
2101a7197c2eSHenrik Rydberg 	 * for the driver-specific modifications.
2102a7197c2eSHenrik Rydberg 	 */
2103a7197c2eSHenrik Rydberg 	ret = hdev->ll_driver->parse(hdev);
2104a7197c2eSHenrik Rydberg 	if (ret)
2105a7197c2eSHenrik Rydberg 		return ret;
2106a7197c2eSHenrik Rydberg 	if (!hdev->dev_rdesc)
2107a7197c2eSHenrik Rydberg 		return -ENODEV;
2108a7197c2eSHenrik Rydberg 
2109734c6609SHenrik Rydberg 	/*
2110734c6609SHenrik Rydberg 	 * Scan generic devices for group information
2111734c6609SHenrik Rydberg 	 */
21124392bf33SBenjamin Tissoires 	if (hid_ignore_special_drivers) {
21134392bf33SBenjamin Tissoires 		hdev->group = HID_GROUP_GENERIC;
21144392bf33SBenjamin Tissoires 	} else if (!hdev->group &&
21156e65d9d5SBenjamin Tissoires 		   !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
2116734c6609SHenrik Rydberg 		ret = hid_scan_report(hdev);
2117734c6609SHenrik Rydberg 		if (ret)
2118734c6609SHenrik Rydberg 			hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2119734c6609SHenrik Rydberg 	}
2120734c6609SHenrik Rydberg 
21216bbe586fSKay Sievers 	/* XXX hack, any other cleaner solution after the driver core
21226bbe586fSKay Sievers 	 * is converted to allow more than 20 bytes as the device name? */
21236bbe586fSKay Sievers 	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
212485cdaf52SJiri Slaby 		     hdev->vendor, hdev->product, atomic_inc_return(&id));
212585cdaf52SJiri Slaby 
21264da361b6SBruno Prémont 	hid_debug_register(hdev, dev_name(&hdev->dev));
212785cdaf52SJiri Slaby 	ret = device_add(&hdev->dev);
212885cdaf52SJiri Slaby 	if (!ret)
212985cdaf52SJiri Slaby 		hdev->status |= HID_STAT_ADDED;
21304da361b6SBruno Prémont 	else
21314da361b6SBruno Prémont 		hid_debug_unregister(hdev);
2132a635f9ddSJiri Kosina 
213385cdaf52SJiri Slaby 	return ret;
213485cdaf52SJiri Slaby }
213585cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_add_device);
213685cdaf52SJiri Slaby 
213785cdaf52SJiri Slaby /**
213885cdaf52SJiri Slaby  * hid_allocate_device - allocate new hid device descriptor
213985cdaf52SJiri Slaby  *
214085cdaf52SJiri Slaby  * Allocate and initialize hid device, so that hid_destroy_device might be
214185cdaf52SJiri Slaby  * used to free it.
214285cdaf52SJiri Slaby  *
214385cdaf52SJiri Slaby  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
214485cdaf52SJiri Slaby  * error value.
214585cdaf52SJiri Slaby  */
214685cdaf52SJiri Slaby struct hid_device *hid_allocate_device(void)
214785cdaf52SJiri Slaby {
214885cdaf52SJiri Slaby 	struct hid_device *hdev;
214985cdaf52SJiri Slaby 	int ret = -ENOMEM;
215085cdaf52SJiri Slaby 
215185cdaf52SJiri Slaby 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
215285cdaf52SJiri Slaby 	if (hdev == NULL)
215385cdaf52SJiri Slaby 		return ERR_PTR(ret);
215485cdaf52SJiri Slaby 
215585cdaf52SJiri Slaby 	device_initialize(&hdev->dev);
215685cdaf52SJiri Slaby 	hdev->dev.release = hid_device_release;
215785cdaf52SJiri Slaby 	hdev->dev.bus = &hid_bus_type;
215864bebefcSFu, Zhonghui 	device_enable_async_suspend(&hdev->dev);
215985cdaf52SJiri Slaby 
2160a7197c2eSHenrik Rydberg 	hid_close_report(hdev);
216185cdaf52SJiri Slaby 
2162cd667ce2SJiri Kosina 	init_waitqueue_head(&hdev->debug_wait);
2163cd667ce2SJiri Kosina 	INIT_LIST_HEAD(&hdev->debug_list);
21641deb9d34SJiri Kosina 	spin_lock_init(&hdev->debug_list_lock);
2165c849a614SAndrew de los Reyes 	sema_init(&hdev->driver_input_lock, 1);
2166aaac082dSDmitry Torokhov 	mutex_init(&hdev->ll_open_lock);
2167cd667ce2SJiri Kosina 
216885cdaf52SJiri Slaby 	return hdev;
216985cdaf52SJiri Slaby }
217085cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_allocate_device);
217185cdaf52SJiri Slaby 
217285cdaf52SJiri Slaby static void hid_remove_device(struct hid_device *hdev)
217385cdaf52SJiri Slaby {
217485cdaf52SJiri Slaby 	if (hdev->status & HID_STAT_ADDED) {
217585cdaf52SJiri Slaby 		device_del(&hdev->dev);
2176a635f9ddSJiri Kosina 		hid_debug_unregister(hdev);
217785cdaf52SJiri Slaby 		hdev->status &= ~HID_STAT_ADDED;
217885cdaf52SJiri Slaby 	}
2179a7197c2eSHenrik Rydberg 	kfree(hdev->dev_rdesc);
2180a7197c2eSHenrik Rydberg 	hdev->dev_rdesc = NULL;
2181a7197c2eSHenrik Rydberg 	hdev->dev_rsize = 0;
218285cdaf52SJiri Slaby }
218385cdaf52SJiri Slaby 
218485cdaf52SJiri Slaby /**
218585cdaf52SJiri Slaby  * hid_destroy_device - free previously allocated device
218685cdaf52SJiri Slaby  *
218785cdaf52SJiri Slaby  * @hdev: hid device
218885cdaf52SJiri Slaby  *
218985cdaf52SJiri Slaby  * If you allocate hid_device through hid_allocate_device, you should ever
219085cdaf52SJiri Slaby  * free by this function.
219185cdaf52SJiri Slaby  */
219285cdaf52SJiri Slaby void hid_destroy_device(struct hid_device *hdev)
219385cdaf52SJiri Slaby {
219485cdaf52SJiri Slaby 	hid_remove_device(hdev);
219585cdaf52SJiri Slaby 	put_device(&hdev->dev);
219685cdaf52SJiri Slaby }
219785cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_destroy_device);
219885cdaf52SJiri Slaby 
2199e04a0442SBenjamin Tissoires 
2200*c17a7476SBenjamin Tissoires static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
2201e04a0442SBenjamin Tissoires {
2202*c17a7476SBenjamin Tissoires 	struct hid_driver *hdrv = data;
2203*c17a7476SBenjamin Tissoires 	struct hid_device *hdev = to_hid_device(dev);
2204*c17a7476SBenjamin Tissoires 
2205*c17a7476SBenjamin Tissoires 	if (hdev->driver == hdrv &&
2206*c17a7476SBenjamin Tissoires 	    !hdrv->match(hdev, hid_ignore_special_drivers))
2207*c17a7476SBenjamin Tissoires 		return device_reprobe(dev);
2208*c17a7476SBenjamin Tissoires 
2209*c17a7476SBenjamin Tissoires 	return 0;
2210*c17a7476SBenjamin Tissoires }
2211*c17a7476SBenjamin Tissoires 
2212*c17a7476SBenjamin Tissoires static int __hid_bus_driver_added(struct device_driver *drv, void *data)
2213*c17a7476SBenjamin Tissoires {
2214e04a0442SBenjamin Tissoires 	struct hid_driver *hdrv = to_hid_driver(drv);
2215e04a0442SBenjamin Tissoires 
2216*c17a7476SBenjamin Tissoires 	if (hdrv->match) {
2217*c17a7476SBenjamin Tissoires 		bus_for_each_dev(&hid_bus_type, NULL, hdrv,
2218*c17a7476SBenjamin Tissoires 				 __hid_bus_reprobe_drivers);
2219*c17a7476SBenjamin Tissoires 	}
2220e04a0442SBenjamin Tissoires 
2221e04a0442SBenjamin Tissoires 	return 0;
2222e04a0442SBenjamin Tissoires }
2223e04a0442SBenjamin Tissoires 
2224e04a0442SBenjamin Tissoires static int __bus_removed_driver(struct device_driver *drv, void *data)
2225e04a0442SBenjamin Tissoires {
2226*c17a7476SBenjamin Tissoires 	return bus_rescan_devices(&hid_bus_type);
2227e04a0442SBenjamin Tissoires }
2228e04a0442SBenjamin Tissoires 
222985cdaf52SJiri Slaby int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
223085cdaf52SJiri Slaby 		const char *mod_name)
223185cdaf52SJiri Slaby {
2232*c17a7476SBenjamin Tissoires 	int ret;
2233*c17a7476SBenjamin Tissoires 
223485cdaf52SJiri Slaby 	hdrv->driver.name = hdrv->name;
223585cdaf52SJiri Slaby 	hdrv->driver.bus = &hid_bus_type;
223685cdaf52SJiri Slaby 	hdrv->driver.owner = owner;
223785cdaf52SJiri Slaby 	hdrv->driver.mod_name = mod_name;
223885cdaf52SJiri Slaby 
22393a6f82f7SJiri Slaby 	INIT_LIST_HEAD(&hdrv->dyn_list);
22403a6f82f7SJiri Slaby 	spin_lock_init(&hdrv->dyn_lock);
22413a6f82f7SJiri Slaby 
2242*c17a7476SBenjamin Tissoires 	ret = driver_register(&hdrv->driver);
2243e04a0442SBenjamin Tissoires 
2244*c17a7476SBenjamin Tissoires 	if (ret == 0)
2245*c17a7476SBenjamin Tissoires 		bus_for_each_drv(&hid_bus_type, NULL, NULL,
2246*c17a7476SBenjamin Tissoires 				 __hid_bus_driver_added);
2247*c17a7476SBenjamin Tissoires 
2248*c17a7476SBenjamin Tissoires 	return ret;
224985cdaf52SJiri Slaby }
225085cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(__hid_register_driver);
225185cdaf52SJiri Slaby 
225285cdaf52SJiri Slaby void hid_unregister_driver(struct hid_driver *hdrv)
225385cdaf52SJiri Slaby {
225485cdaf52SJiri Slaby 	driver_unregister(&hdrv->driver);
22553a6f82f7SJiri Slaby 	hid_free_dynids(hdrv);
2256e04a0442SBenjamin Tissoires 
2257e04a0442SBenjamin Tissoires 	bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
225885cdaf52SJiri Slaby }
225985cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_unregister_driver);
226085cdaf52SJiri Slaby 
22610361a28dSOliver Neukum int hid_check_keys_pressed(struct hid_device *hid)
22620361a28dSOliver Neukum {
22630361a28dSOliver Neukum 	struct hid_input *hidinput;
22640361a28dSOliver Neukum 	int i;
22650361a28dSOliver Neukum 
2266e5288eb5SJiri Kosina 	if (!(hid->claimed & HID_CLAIMED_INPUT))
2267e5288eb5SJiri Kosina 		return 0;
2268e5288eb5SJiri Kosina 
22690361a28dSOliver Neukum 	list_for_each_entry(hidinput, &hid->inputs, list) {
22700361a28dSOliver Neukum 		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
22710361a28dSOliver Neukum 			if (hidinput->input->key[i])
22720361a28dSOliver Neukum 				return 1;
22730361a28dSOliver Neukum 	}
22740361a28dSOliver Neukum 
22750361a28dSOliver Neukum 	return 0;
22760361a28dSOliver Neukum }
22770361a28dSOliver Neukum 
22780361a28dSOliver Neukum EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
22790361a28dSOliver Neukum 
228086166b7bSJiri Kosina static int __init hid_init(void)
228186166b7bSJiri Kosina {
228285cdaf52SJiri Slaby 	int ret;
228385cdaf52SJiri Slaby 
2284a635f9ddSJiri Kosina 	if (hid_debug)
22854291ee30SJoe Perches 		pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
22864291ee30SJoe Perches 			"debugfs is now used for inspecting the device (report descriptor, reports)\n");
2287a635f9ddSJiri Kosina 
228885cdaf52SJiri Slaby 	ret = bus_register(&hid_bus_type);
228985cdaf52SJiri Slaby 	if (ret) {
22904291ee30SJoe Perches 		pr_err("can't register hid bus\n");
229185cdaf52SJiri Slaby 		goto err;
229285cdaf52SJiri Slaby 	}
229385cdaf52SJiri Slaby 
229485cdaf52SJiri Slaby 	ret = hidraw_init();
229585cdaf52SJiri Slaby 	if (ret)
229685cdaf52SJiri Slaby 		goto err_bus;
229785cdaf52SJiri Slaby 
2298a635f9ddSJiri Kosina 	hid_debug_init();
2299a635f9ddSJiri Kosina 
230085cdaf52SJiri Slaby 	return 0;
230185cdaf52SJiri Slaby err_bus:
230285cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
230385cdaf52SJiri Slaby err:
230485cdaf52SJiri Slaby 	return ret;
230586166b7bSJiri Kosina }
230686166b7bSJiri Kosina 
230786166b7bSJiri Kosina static void __exit hid_exit(void)
230886166b7bSJiri Kosina {
2309a635f9ddSJiri Kosina 	hid_debug_exit();
231086166b7bSJiri Kosina 	hidraw_exit();
231185cdaf52SJiri Slaby 	bus_unregister(&hid_bus_type);
2312d5d3e202SBenjamin Tissoires 	hid_quirks_exit(HID_BUS_ANY);
231386166b7bSJiri Kosina }
231486166b7bSJiri Kosina 
231586166b7bSJiri Kosina module_init(hid_init);
231686166b7bSJiri Kosina module_exit(hid_exit);
231786166b7bSJiri Kosina 
231888adb72bSJiri Kosina MODULE_AUTHOR("Andreas Gal");
231988adb72bSJiri Kosina MODULE_AUTHOR("Vojtech Pavlik");
232088adb72bSJiri Kosina MODULE_AUTHOR("Jiri Kosina");
23217021b600SGrant Grundler MODULE_LICENSE("GPL");
2322