hid-core.c revision 6bbe586fd4d94439f3960e200056ff057f7db5c6
1dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
2229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina *  HID support for Linux
3dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
4dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *  Copyright (c) 1999 Andreas Gal
5dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7f142b3a4f64eca235f5890d9cecb68482f62ed26Jiri Kosina *  Copyright (c) 2006-2007 Jiri Kosina
8dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
9dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
10dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
11dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * This program is free software; you can redistribute it and/or modify it
12dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * under the terms of the GNU General Public License as published by the Free
13dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Software Foundation; either version 2 of the License, or (at your option)
14dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * any later version.
15dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
16dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
17dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/module.h>
18dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/slab.h>
19dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/init.h>
20dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/kernel.h>
21dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/list.h>
22dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/mm.h>
23dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/spinlock.h>
24dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <asm/unaligned.h>
25dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <asm/byteorder.h>
26dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/input.h>
27dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/wait.h>
2847a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina#include <linux/vmalloc.h>
29c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina#include <linux/sched.h>
30dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
31dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/hid.h>
32dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/hiddev.h>
33c080d89ad91e98fec0e8fc5f448a1ad899bd85c7Jiri Kosina#include <linux/hid-debug.h>
3486166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina#include <linux/hidraw.h>
35dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
365f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby#include "hid-ids.h"
375f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby
38dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
39dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Version Information
40dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
41dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
42dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#define DRIVER_VERSION "v2.6"
43f142b3a4f64eca235f5890d9cecb68482f62ed26Jiri Kosina#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
44531498013163c72cee8540e98e12c0c430c7fe87Jiri Kosina#define DRIVER_DESC "HID core driver"
45dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#define DRIVER_LICENSE "GPL"
46dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
4758037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina#ifdef CONFIG_HID_DEBUG
4858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosinaint hid_debug = 0;
49377e10fbb4ef219f540d06796e9fb0d3ec35a58fAnssi Hannulamodule_param_named(debug, hid_debug, int, 0600);
50377e10fbb4ef219f540d06796e9fb0d3ec35a58fAnssi HannulaMODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
5158037eb961f859607b161c50d9d4ecb374de1e8fJiri KosinaEXPORT_SYMBOL_GPL(hid_debug);
5258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina#endif
5358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina
54dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
55dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new report for a device.
56dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
57dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
58dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
59dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
60dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report_enum *report_enum = device->report_enum + type;
61dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
62dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
63dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report_enum->report_id_hash[id])
64dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return report_enum->report_id_hash[id];
65dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
66dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
67dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
68dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
69dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (id != 0)
70dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		report_enum->numbered = 1;
71dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
72dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->id = id;
73dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->type = type;
74dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size = 0;
75dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->device = device;
76dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report_enum->report_id_hash[id] = report;
77dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
78dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	list_add_tail(&report->list, &report_enum->report_list);
79dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
80dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return report;
81dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
82dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
83dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
84dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
85dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
86dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
87dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
88dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
89dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
90dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
91dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report->maxfield == HID_MAX_FIELDS) {
9258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("too many fields in report\n");
93dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
94dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
95dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
96dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
98dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
99dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->index = report->maxfield++;
100dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->field[field->index] = field;
101dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->usage = (struct hid_usage *)(field + 1);
102282bfd4cbcc2bbeb1a2cad1f42debd378e0b5ac9Jiri Slaby	field->value = (s32 *)(field->usage + usages);
103dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report = report;
104dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
105dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return field;
106dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
107dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
108dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
109dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Open a collection. The type/usage is pushed on the stack.
110dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
111dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
112dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int open_collection(struct hid_parser *parser, unsigned type)
113dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
114dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_collection *collection;
115dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned usage;
116dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
117dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usage = parser->local.usage[0];
118dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
119dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
12058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack overflow\n");
121dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
122dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
123dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
124dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->device->maxcollection == parser->device->collection_size) {
125dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		collection = kmalloc(sizeof(struct hid_collection) *
126dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->device->collection_size * 2, GFP_KERNEL);
127dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (collection == NULL) {
12858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("failed to reallocate collection array\n");
129dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
130dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
131dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memcpy(collection, parser->device->collection,
132dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
133dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
134dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memset(collection + parser->device->collection_size, 0,
135dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
136dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
137dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(parser->device->collection);
138dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection = collection;
139dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection_size *= 2;
140dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
141dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
142dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack[parser->collection_stack_ptr++] =
143dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection;
144dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
145dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection = parser->device->collection +
146dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection++;
147dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->type = type;
148dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->usage = usage;
149dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->level = parser->collection_stack_ptr - 1;
150dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
151dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (type == HID_COLLECTION_APPLICATION)
152dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxapplication++;
153dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
154dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
155dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
156dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
157dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
158dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Close a collection.
159dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
160dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
161dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int close_collection(struct hid_parser *parser)
162dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
163dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->collection_stack_ptr) {
16458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack underflow\n");
165dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
166dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
167dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack_ptr--;
168dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
169dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
170dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
171dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
172dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Climb up the stack, search for the specified collection type
173dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * and return the usage.
174dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
175dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
176dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
177dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
178dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int n;
179dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
180dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (parser->device->collection[parser->collection_stack[n]].type == type)
181dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return parser->device->collection[parser->collection_stack[n]].usage;
182dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0; /* we know nothing about this usage type */
183dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
184dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
185dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
186dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Add a usage to the temporary parser table.
187dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
188dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
189dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_usage(struct hid_parser *parser, unsigned usage)
190dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
191dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->local.usage_index >= HID_MAX_USAGES) {
19258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("usage index exceeded\n");
193dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
194dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
195dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage[parser->local.usage_index] = usage;
196dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.collection_index[parser->local.usage_index] =
197dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack_ptr ?
198dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
199dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage_index++;
200dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
201dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
202dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
203dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
204dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
205dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
206dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
207dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
208dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
209dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
210dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
211dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int usages;
212dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset;
213dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int i;
214dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
215dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
21658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("hid_register_report failed\n");
217dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
218dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
219dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
220dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->global.logical_maximum < parser->global.logical_minimum) {
22158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
222dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
223dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
224dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
225dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	offset = report->size;
226dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size += parser->global.report_size * parser->global.report_count;
227dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
228dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->local.usage_index) /* Ignore padding fields */
229dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
230dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
231dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usages = max_t(int, parser->local.usage_index, parser->global.report_count);
232dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
233dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
234dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
235dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
236dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
237dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
238dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
239dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
240dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < usages; i++) {
241dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		int j = i;
242dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		/* Duplicate the last usage we parsed if we have excess values */
243dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (i >= parser->local.usage_index)
244dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			j = parser->local.usage_index - 1;
245dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].hid = parser->local.usage[j];
246dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].collection_index =
247dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->local.collection_index[j];
248dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
249dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
250dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->maxusage = usages;
251dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->flags = flags;
252dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_offset = offset;
253dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_type = report_type;
254dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_size = parser->global.report_size;
255dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_count = parser->global.report_count;
256dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_minimum = parser->global.logical_minimum;
257dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_maximum = parser->global.logical_maximum;
258dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_minimum = parser->global.physical_minimum;
259dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_maximum = parser->global.physical_maximum;
260dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit_exponent = parser->global.unit_exponent;
261dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit = parser->global.unit;
262dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
263dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
264dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
265dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
266dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
267dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Read data value from item.
268dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
269dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
270dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u32 item_udata(struct hid_item *item)
271dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
272dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
273880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 1: return item->data.u8;
274880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 2: return item->data.u16;
275880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 4: return item->data.u32;
276dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
277dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
278dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
279dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
280dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic s32 item_sdata(struct hid_item *item)
281dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
282dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
283880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 1: return item->data.s8;
284880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 2: return item->data.s16;
285880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 4: return item->data.s32;
286dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
287dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
288dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
289dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
290dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
291dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a global item.
292dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
293dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
294dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
295dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
296dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
297880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_PUSH:
298dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
299880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
300880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("global enviroment stack overflow\n");
301880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
302880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
303dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
304880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		memcpy(parser->global_stack + parser->global_stack_ptr++,
305880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			&parser->global, sizeof(struct hid_global));
306880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
307dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
308880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_POP:
309dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
310880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (!parser->global_stack_ptr) {
311880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("global enviroment stack underflow\n");
312880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
313880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
314dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
315880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		memcpy(&parser->global, parser->global_stack +
316880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			--parser->global_stack_ptr, sizeof(struct hid_global));
317880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
318dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
319880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
320880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.usage_page = item_udata(item);
321880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
322dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
323880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
324880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.logical_minimum = item_sdata(item);
325880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
326dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
327880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
328880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.logical_minimum < 0)
329880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.logical_maximum = item_sdata(item);
330880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		else
331880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.logical_maximum = item_udata(item);
332880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
333dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
334880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
335880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.physical_minimum = item_sdata(item);
336880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
337dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
338880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
339880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.physical_minimum < 0)
340880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.physical_maximum = item_sdata(item);
341880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		else
342880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.physical_maximum = item_udata(item);
343880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
344dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
345880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
346880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.unit_exponent = item_sdata(item);
347880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
348dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
349880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_UNIT:
350880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.unit = item_udata(item);
351880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
352dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
353880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
354880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.report_size = item_udata(item);
355880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.report_size > 32) {
356880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("invalid report_size %d\n",
357880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby					parser->global.report_size);
358880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
359880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
360880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
361dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
362880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
363880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.report_count = item_udata(item);
364880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.report_count > HID_MAX_USAGES) {
365880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("invalid report_count %d\n",
366880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby					parser->global.report_count);
367880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
368880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
369880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
370dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
371880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
372880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.report_id = item_udata(item);
373880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.report_id == 0) {
374880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("report_id 0 is invalid\n");
375dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
376880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
377880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
378880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
379880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	default:
380880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		dbg_hid("unknown global tag 0x%x\n", item->tag);
381880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return -1;
382dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
383dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
384dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
385dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
386dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a local item.
387dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
388dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
389dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
390dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
391dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
392dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
393dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
394dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->size == 0) {
39558037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("item data expected for local item\n");
396dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
397dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
398dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
399dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
400dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
401dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
402880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_DELIMITER:
403880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
404880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (data) {
405880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			/*
406880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * We treat items before the first delimiter
407880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * as global to all usage sets (branch 0).
408880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * In the moment we process only these global
409880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * items and the first delimiter set.
410880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 */
411880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			if (parser->local.delimiter_depth != 0) {
412880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				dbg_hid("nested delimiters\n");
413880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				return -1;
414dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
415880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->local.delimiter_depth++;
416880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->local.delimiter_branch++;
417880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		} else {
418880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			if (parser->local.delimiter_depth < 1) {
419880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				dbg_hid("bogus close delimiter\n");
420880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				return -1;
421dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
422880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->local.delimiter_depth--;
423880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
424880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 1;
425dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
426880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_USAGE:
427dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
428880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->local.delimiter_branch > 1) {
429880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("alternative usage ignored\n");
430880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return 0;
431880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
432dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
433880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (item->size <= 2)
434880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			data = (parser->global.usage_page << 16) + data;
435dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
436880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return hid_add_usage(parser, data);
437dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
438880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
439dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
440880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->local.delimiter_branch > 1) {
441880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("alternative usage ignored\n");
442dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
443880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
444dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
445880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (item->size <= 2)
446880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			data = (parser->global.usage_page << 16) + data;
447dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
448880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->local.usage_minimum = data;
449880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
450dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
451880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
452dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
453880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->local.delimiter_branch > 1) {
454880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("alternative usage ignored\n");
455dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
456880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
457dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
458880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (item->size <= 2)
459880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			data = (parser->global.usage_page << 16) + data;
460dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
461880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		for (n = parser->local.usage_minimum; n <= data; n++)
462880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			if (hid_add_usage(parser, n)) {
463880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				dbg_hid("hid_add_usage failed\n");
464880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				return -1;
465880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			}
466880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
467880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
468880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	default:
469880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
470880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		dbg_hid("unknown local item tag 0x%x\n", item->tag);
471880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
472dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
473dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
474dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
475dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
476dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
477dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a main item.
478dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
479dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
480dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
481dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
482dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
483dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int ret;
484dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
485dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
486dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
487dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
488880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
489880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = open_collection(parser, data & 0xff);
490880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
491880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_END_COLLECTION:
492880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = close_collection(parser);
493880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
494880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_INPUT:
495880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
496880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
497880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_OUTPUT:
498880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
499880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
500880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_FEATURE:
501880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
502880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
503880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	default:
504880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		dbg_hid("unknown main item tag 0x%x\n", item->tag);
505880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = 0;
506dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
507dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
508dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
509dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
510dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return ret;
511dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
512dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
513dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
514dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a reserved item.
515dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
516dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
517dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
518dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
51958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
520dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
521dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
522dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
523dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
524dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a report and all registered fields. The field->usage and
525dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * field->value table's are allocated behind the field, so we need
526dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * only to free(field) itself.
527dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
528dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
529dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_free_report(struct hid_report *report)
530dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
531dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
532dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
533dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < report->maxfield; n++)
534dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(report->field[n]);
535dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(report);
536dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
537dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
538dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
539dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a device structure, all reports, and all fields.
540dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
541dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
54285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_device_release(struct device *dev)
543dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
54485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *device = container_of(dev, struct hid_device, dev);
54585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned i, j;
546dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
547dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < HID_REPORT_TYPES; i++) {
548dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		struct hid_report_enum *report_enum = device->report_enum + i;
549dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
550dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		for (j = 0; j < 256; j++) {
551dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			struct hid_report *report = report_enum->report_id_hash[j];
552dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (report)
553dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_free_report(report);
554dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
555dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
556dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
557dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device->rdesc);
558767fe7877c2928b5633992ee60a49ad4516dc2afJiri Kosina	kfree(device->collection);
559dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device);
560dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
561dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
562dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
563dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Fetch a report description item from the data stream. We support long
564dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * items, though they are not used yet.
565dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
566dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
567dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
568dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
569dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u8 b;
570dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
571dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((end - start) <= 0)
572dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
573dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
574dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	b = *start++;
575dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
576dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->type = (b >> 2) & 3;
577dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->tag  = (b >> 4) & 15;
578dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
579dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->tag == HID_ITEM_TAG_LONG) {
580dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
581dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->format = HID_ITEM_FORMAT_LONG;
582dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
583dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < 2)
584dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
585dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
586dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->size = *start++;
587dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->tag  = *start++;
588dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
589dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < item->size)
590dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
591dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
592dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->data.longdata = start;
593dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		start += item->size;
594dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return start;
595dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
596dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
597dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->format = HID_ITEM_FORMAT_SHORT;
598dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->size = b & 3;
599dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
600dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
601880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 0:
602880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
603dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
604880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 1:
605880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if ((end - start) < 1)
606880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return NULL;
607880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->data.u8 = *start++;
608880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
609880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
610880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 2:
611880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if ((end - start) < 2)
612880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return NULL;
613880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->data.u16 = get_unaligned_le16(start);
614880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		start = (__u8 *)((__le16 *)start + 1);
615880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
616880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
617880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 3:
618880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->size++;
619880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if ((end - start) < 4)
620880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return NULL;
621880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->data.u32 = get_unaligned_le32(start);
622880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		start = (__u8 *)((__le32 *)start + 1);
623880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
624dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
625dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
626dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return NULL;
627dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
628dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
62985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
63085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_parse_report - parse device report
63185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
63285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @device: hid device
63385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @start: report start
63485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @size: report size
63585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
636dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Parse a report description into a hid_device structure. Reports are
637dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * enumerated, fields are attached to these reports.
63885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * 0 returned on success, otherwise nonzero error value.
639dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
64085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_parse_report(struct hid_device *device, __u8 *start,
64185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		unsigned size)
642dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
643dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_parser *parser;
644dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_item item;
645dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u8 *end;
64685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
647dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	static int (*dispatch_type[])(struct hid_parser *parser,
648dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				      struct hid_item *item) = {
649dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_main,
650dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_global,
651dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_local,
652dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_reserved
653dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	};
654dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
655c500c9714011edab021591340042787722db9cf0Jiri Slaby	if (device->driver->report_fixup)
656c500c9714011edab021591340042787722db9cf0Jiri Slaby		device->driver->report_fixup(device, start, size);
657c500c9714011edab021591340042787722db9cf0Jiri Slaby
65885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	device->rdesc = kmalloc(size, GFP_KERNEL);
65985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (device->rdesc == NULL)
66085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
661dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memcpy(device->rdesc, start, size);
662dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	device->rsize = size;
663dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
66485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	parser = vmalloc(sizeof(struct hid_parser));
66585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!parser) {
66685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		ret = -ENOMEM;
66785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
668dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
66985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
67047a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina	memset(parser, 0, sizeof(struct hid_parser));
671dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->device = device;
672dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
673dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	end = start + size;
67485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = -EINVAL;
675dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	while ((start = fetch_item(start, end, &item)) != NULL) {
676dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
677dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (item.format != HID_ITEM_FORMAT_SHORT) {
67858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unexpected long global item\n");
67985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			goto err;
680dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
681dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
682dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (dispatch_type[item.type](parser, &item)) {
68358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("item %u %u %u %u parsing failed\n",
684dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
68585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			goto err;
686dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
687dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
688dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (start == end) {
689dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->collection_stack_ptr) {
69058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("unbalanced collection at end of report description\n");
69185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				goto err;
692dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
693dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_depth) {
69458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("unbalanced delimiter at end of report description\n");
69585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				goto err;
696dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
69747a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina			vfree(parser);
69885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return 0;
699dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
700dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
701dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
70258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
70385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
70447a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina	vfree(parser);
70585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
706dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
707229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_parse_report);
708dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
709dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
710dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Convert a signed n-bit integer to signed 32-bit integer. Common
711dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * cases are done through the compiler, the screwed things has to be
712dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * done by hand.
713dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
714dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
715dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic s32 snto32(__u32 value, unsigned n)
716dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
717dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (n) {
718880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 8:  return ((__s8)value);
719880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 16: return ((__s16)value);
720880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 32: return ((__s32)value);
721dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
722dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
723dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
724dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
725dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
726dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Convert a signed 32-bit integer to a signed n-bit integer.
727dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
728dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
729dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u32 s32ton(__s32 value, unsigned n)
730dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
731dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	s32 a = value >> (n - 1);
732dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (a && a != -1)
733dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
734dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return value & ((1 << n) - 1);
735dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
736dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
737dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
738dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Extract/implement a data field from/to a little endian report (bit array).
739dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
740dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Code sort-of follows HID spec:
741dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
742dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
743dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * While the USB HID spec allows unlimited length bit fields in "report
744dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * descriptors", most devices never use more than 16 bits.
745dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * One model of UPS is claimed to report "LINEV" as a 32-bit field.
746dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
747dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
748dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
749dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
750dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
751dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u64 x;
752dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
753c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (n > 32)
754c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
755c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				n, current->comm);
756dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
757dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report += offset >> 3;  /* adjust byte index */
758229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina	offset &= 7;            /* now only need bit offset into one byte */
759c105068f2b35343eecf2bf16ee29a362b6121fa3Harvey Harrison	x = get_unaligned_le64(report);
760229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina	x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
761dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return (u32) x;
762dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
763dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
764dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
765dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * "implement" : set bits in a little endian bit stream.
766dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Same concepts as "extract" (see comments above).
767dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * The data mangled in the bit stream remains in little endian
768dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * order the whole time. It make more sense to talk about
769dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * endianness of register values by considering a register
770dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * a "cached" copy of the little endiad bit stream.
771dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
772dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
773dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
7746f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	u64 x;
775dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u64 m = (1ULL << n) - 1;
776dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
777c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (n > 32)
778c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
779c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				n, current->comm);
780dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
781c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (value > m)
782c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
783c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				value, current->comm);
784dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	WARN_ON(value > m);
785dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	value &= m;
786dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
787dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report += offset >> 3;
788dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	offset &= 7;
789dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
7906f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	x = get_unaligned_le64(report);
7916f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	x &= ~(m << offset);
7926f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	x |= ((u64)value) << offset;
7936f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	put_unaligned_le64(x, report);
794dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
795dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
796dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
797dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Search an array for a value.
798dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
799dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
800dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ int search(__s32 *array, __s32 value, unsigned n)
801dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
802dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	while (n--) {
803dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (*array++ == value)
804dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
805dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
806dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return -1;
807dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
808dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
80985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
81085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_match_report - check if driver's raw_event should be called
81185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
81285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hid: hid device
81385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @report_type: type to match against
81485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
81585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * compare hid->driver->report_table->report_type to report->type
81685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
81785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_match_report(struct hid_device *hid, struct hid_report *report)
818dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
81985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	const struct hid_report_id *id = hid->driver->report_table;
82085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
82185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!id) /* NULL means all */
82285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return 1;
82385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
82485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (; id->report_type != HID_TERMINATOR; id++)
82585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (id->report_type == HID_ANY_ID ||
82685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				id->report_type == report->type)
82785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return 1;
82885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
82985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
83085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
83185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
83285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_match_usage - check if driver's event should be called
83385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
83485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hid: hid device
83585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @usage: usage to match against
83685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
83785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * compare hid->driver->usage_table->usage_{type,code} to
83885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * usage->usage_{type,code}
83985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
84085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
84185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
84285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	const struct hid_usage_id *id = hid->driver->usage_table;
84385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
84485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!id) /* NULL means all */
84585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return 1;
84685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
84785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (; id->usage_type != HID_ANY_ID - 1; id++)
84885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if ((id->usage_hid == HID_ANY_ID ||
84985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				id->usage_hid == usage->hid) &&
85085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				(id->usage_type == HID_ANY_ID ||
85185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				id->usage_type == usage->type) &&
85285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				(id->usage_code == HID_ANY_ID ||
85385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				 id->usage_code == usage->code))
85485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return 1;
85585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
85685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
85785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
85885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_process_event(struct hid_device *hid, struct hid_field *field,
85985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		struct hid_usage *usage, __s32 value, int interrupt)
86085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
86185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = hid->driver;
86285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
86385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
864dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	hid_dump_input(usage, value);
86585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
86685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
86785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		ret = hdrv->event(hid, field, usage, value);
86885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (ret != 0) {
86985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			if (ret < 0)
87085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				dbg_hid("%s's event failed with %d\n",
87185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby						hdrv->name, ret);
87285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return;
87385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		}
87485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
87585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
876dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (hid->claimed & HID_CLAIMED_INPUT)
877dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hidinput_hid_event(hid, field, usage, value);
878aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
879aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina		hid->hiddev_hid_event(hid, field, usage, value);
880dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
881dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
882dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
883dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Analyse a received field, and fetch the data from it. The field
884dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * content is stored for next report processing (we do differential
885dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * reporting to the layer).
886dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
887dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
888abdff0f7749a6696ba2a4238b675cbc55abcdb7aAdrian Bunkstatic void hid_input_field(struct hid_device *hid, struct hid_field *field,
889abdff0f7749a6696ba2a4238b675cbc55abcdb7aAdrian Bunk			    __u8 *data, int interrupt)
890dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
891dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
892dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned count = field->report_count;
893dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset = field->report_offset;
894dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
895dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 min = field->logical_minimum;
896dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 max = field->logical_maximum;
897dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 *value;
898dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
899dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
900dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return;
901dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
902dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
903dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
904dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
905dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina						    extract(data, offset + n * size, size);
906dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
907dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
908dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			    && value[n] >= min && value[n] <= max
909dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			    && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
910dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				goto exit;
911dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
912dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
913dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
914dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
915dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
916dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
917dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			continue;
918dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
919dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
920dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (field->value[n] >= min && field->value[n] <= max
921dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& field->usage[field->value[n] - min].hid
922dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& search(value, field->value[n], count))
923dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
924dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
925dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (value[n] >= min && value[n] <= max
926dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& field->usage[value[n] - min].hid
927dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& search(field->value, value[n], count))
928dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
929dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
930dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
931dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memcpy(field->value, value, count * sizeof(__s32));
932dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinaexit:
933dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(value);
934dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
935dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
936dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
937dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Output the field into the report.
938dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
939dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
940dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_output_field(struct hid_field *field, __u8 *data)
941dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
942dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned count = field->report_count;
943dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset = field->report_offset;
944dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
94546386b587086c8d2698222a031bf749688464032Simon Budig	unsigned bitsused = offset + count * size;
946dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
947dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
94846386b587086c8d2698222a031bf749688464032Simon Budig	/* make sure the unused bits in the last byte are zeros */
94946386b587086c8d2698222a031bf749688464032Simon Budig	if (count > 0 && size > 0 && (bitsused % 8) != 0)
95046386b587086c8d2698222a031bf749688464032Simon Budig		data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
95146386b587086c8d2698222a031bf749688464032Simon Budig
952dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
953dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (field->logical_minimum < 0)	/* signed values */
954dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			implement(data, offset + n * size, size, s32ton(field->value[n], size));
955dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		else				/* unsigned values */
956dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			implement(data, offset + n * size, size, field->value[n]);
957dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
958dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
959dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
960dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
961dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Create a report.
962dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
963dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
964229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosinavoid hid_output_report(struct hid_report *report, __u8 *data)
965dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
966dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
967dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
968dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report->id > 0)
969dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		*data++ = report->id;
970dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
971dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < report->maxfield; n++)
972dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_output_field(report->field[n], data);
973dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
974229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_output_report);
975dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
976dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
977dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Set a field value. The report this field belongs to has to be
978dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * created and transferred to the device, to set this value in the
979dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * device.
980dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
981dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
982dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinaint hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
983dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
984dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
985dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
986dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	hid_dump_input(field->usage + offset, value);
987dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
988dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (offset >= field->report_count) {
98958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
990dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_dump_field(field, 8);
991dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
992dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
993dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (field->logical_minimum < 0) {
994dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (value != snto32(s32ton(value, size), size)) {
99558037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("value %d is out of range\n", value);
996dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
997dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
998dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
999dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->value[offset] = value;
1000dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
1001dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
1002229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_set_field);
1003dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
100485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
100585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const u8 *data)
1006aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina{
1007aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	struct hid_report *report;
100885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int n = 0;	/* Normally report number is 0 */
1009aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* Device uses numbered reports, data[0] is report number */
101185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report_enum->numbered)
101285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		n = *data;
1013aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = report_enum->report_id_hash[n];
101585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report == NULL)
101685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("undefined report_id %u received\n", n);
1017aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return report;
101985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
1020aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
102185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
102285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		int interrupt)
102385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
102485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report_enum *report_enum = hid->report_enum + type;
102585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report *report;
102685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int a;
102785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int rsize, csize = size;
102885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	u8 *cdata = data;
1029aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
103085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = hid_get_report(report_enum, data);
103185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!report)
103285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return;
1033aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
103485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report_enum->numbered) {
103585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		cdata++;
103685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		csize--;
1037aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
1038aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1039aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	rsize = ((report->size - 1) >> 3) + 1;
1040aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
104185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (csize < rsize) {
104285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
104385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				csize, rsize);
104485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		memset(cdata + csize, 0, rsize - csize);
1045aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
1046aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1047aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1048aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hid->hiddev_report_event(hid, report);
1049b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1050b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		/* numbered reports need to be passed with the report num */
1051b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		if (report_enum->numbered)
1052b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina			hidraw_report_event(hid, data - 1, size + 1);
1053b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		else
1054b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina			hidraw_report_event(hid, data, size);
1055b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina	}
1056aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
105785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (a = 0; a < report->maxfield; a++)
105885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hid_input_field(hid, report->field[a], cdata, interrupt);
1059aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1060aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (hid->claimed & HID_CLAIMED_INPUT)
1061aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hidinput_report_event(hid, report);
106285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
106385cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_report_raw_event);
106485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
106585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
106685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_input_report - report data from lower layer (usb, bt...)
106785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
106885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hid: hid device
106985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @type: HID report type (HID_*_REPORT)
107085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @data: report contents
107185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @size: size of data parameter
107285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @interrupt: called from atomic?
107385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
107485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * This is data entry for lower layers.
107585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
107685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
107785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
107885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report_enum *report_enum = hid->report_enum + type;
107985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = hid->driver;
108085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report *report;
108185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int i;
108285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
108385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
108485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hid || !hid->driver)
108585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENODEV;
108685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
108785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!size) {
108885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("empty report\n");
108985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -1;
109085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
109185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
109285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
109385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
109485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = hid_get_report(report_enum, data);
109585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!report)
109685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -1;
109785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
109885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* dump the report */
109985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	dbg_hid("report %d (size %u) = ", report->id, size);
110085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (i = 0; i < size; i++)
110185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid_line(" %02x", data[i]);
110285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	dbg_hid_line("\n");
110385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
110485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
110585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		ret = hdrv->raw_event(hid, report, data, size);
110685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (ret != 0)
110785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return ret < 0 ? ret : 0;
110885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
110985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
111085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hid_report_raw_event(hid, type, data, size, interrupt);
1111aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1112aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	return 0;
1113aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina}
1114aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri KosinaEXPORT_SYMBOL_GPL(hid_input_report);
1115aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
11160f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic bool hid_match_one_id(struct hid_device *hdev,
11170f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		const struct hid_device_id *id)
11180f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina{
11190f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	return id->bus == hdev->bus &&
11200f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
11210f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		(id->product == HID_ANY_ID || id->product == hdev->product);
11220f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina}
11230f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11240f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic const struct hid_device_id *hid_match_id(struct hid_device *hdev,
11250f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		const struct hid_device_id *id)
11260f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina{
11270f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	for (; id->bus; id++)
11280f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		if (hid_match_one_id(hdev, id))
11290f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina			return id;
11300f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11310f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	return NULL;
11320f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina}
11330f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11340f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic const struct hid_device_id hid_hiddev_list[] = {
1135c0bd6a429819f794f626f9e73be4564e18e132dbRichard Hughes	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1136c0bd6a429819f794f626f9e73be4564e18e132dbRichard Hughes	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
11370f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	{ }
11380f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina};
11390f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11400f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic bool hid_hiddev(struct hid_device *hdev)
11410f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina{
11420f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	return !!hid_match_id(hdev, hid_hiddev_list);
11430f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina}
11440f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
114593c10132a7ac160df3175b53f7ee857625412165Jiri Slabyint hid_connect(struct hid_device *hdev, unsigned int connect_mask)
114693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby{
114793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
114893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		"Joystick", "Gamepad", "Keyboard", "Keypad",
114993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		"Multi-Axis Controller"
115093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	};
115193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	const char *type, *bus;
115293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	char buf[64];
115393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	unsigned int i;
115493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	int len;
115593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
115693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->bus != BUS_USB)
115793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		connect_mask &= ~HID_CONNECT_HIDDEV;
11580f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	if (hid_hiddev(hdev))
11590f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
116093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
116193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
116293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
116393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->claimed |= HID_CLAIMED_INPUT;
116493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
116593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			!hdev->hiddev_connect(hdev,
116693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				connect_mask & HID_CONNECT_HIDDEV_FORCE))
116793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->claimed |= HID_CLAIMED_HIDDEV;
116893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
116993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->claimed |= HID_CLAIMED_HIDRAW;
117093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
117193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (!hdev->claimed) {
117293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
117393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				"hidraw\n");
117493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		return -ENODEV;
117593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	}
117693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
117793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
117893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
117993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->ff_init(hdev);
118093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
118193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	len = 0;
118293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->claimed & HID_CLAIMED_INPUT)
118393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		len += sprintf(buf + len, "input");
118493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->claimed & HID_CLAIMED_HIDDEV)
118593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
118693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				hdev->minor);
118793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->claimed & HID_CLAIMED_HIDRAW)
118893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
118993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				((struct hidraw *)hdev->hidraw)->minor);
119093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
119193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	type = "Device";
119293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	for (i = 0; i < hdev->maxcollection; i++) {
119393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		struct hid_collection *col = &hdev->collection[i];
119493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		if (col->type == HID_COLLECTION_APPLICATION &&
119593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
119693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
119793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			type = types[col->usage & 0xffff];
119893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			break;
119993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		}
120093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	}
120193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
120293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	switch (hdev->bus) {
120393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	case BUS_USB:
120493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		bus = "USB";
120593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		break;
120693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	case BUS_BLUETOOTH:
120793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		bus = "BLUETOOTH";
120893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		break;
120993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	default:
121093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		bus = "<UNKNOWN>";
121193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	}
121293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
121393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
121493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			buf, bus, hdev->version >> 8, hdev->version & 0xff,
121593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			type, hdev->name, hdev->phys);
121693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
121793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	return 0;
121893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby}
121993c10132a7ac160df3175b53f7ee857625412165Jiri SlabyEXPORT_SYMBOL_GPL(hid_connect);
122093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
122185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic const struct hid_device_id hid_blacklist[] = {
122214a21cd459f97e3b3cc4fcde48fc5bcdb81d097eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
122314a21cd459f97e3b3cc4fcde48fc5bcdb81d097eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1224df9bcace7b1c29e9be1c13b034ff04f4f4c90edeJiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
12258c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
12268c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
12278c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
12288c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
12298c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
12308c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
12318c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
12328c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
12338c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
12348c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
12358c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
12368c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
12378c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
12388c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
12398c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
12408c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
12418c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
12428c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
12438c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1244ee8a1a0a1a5817accd03ced7e7ffde3a4430f485Jan Scholz	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1245ee8a1a0a1a5817accd03ced7e7ffde3a4430f485Jan Scholz	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1246ee8a1a0a1a5817accd03ced7e7ffde3a4430f485Jan Scholz	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
12478c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
12488c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
12498c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
12508c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
12518c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
12528c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1253a96d6ef34751093797c3a6c6080733dd7af23d35Henrik Rydberg	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1254a96d6ef34751093797c3a6c6080733dd7af23d35Henrik Rydberg	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1255a96d6ef34751093797c3a6c6080733dd7af23d35Henrik Rydberg	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
12568c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
12578c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1258b5635b129b3ca3a9c879a36f58f5b8c4903d267aJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
12593b239cd739a9499da08326356add3d9d992c7911Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1260fcfacfd3594d5d2fa99fb5e7d33dee3904b1a156Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
12610f2213208f8da51bcb665309e3468f000489c04fJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
12620f2213208f8da51bcb665309e3468f000489c04fJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
12630f2213208f8da51bcb665309e3468f000489c04fJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
12641f243e302cea1561ac881eb5d27041c5342beba4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
12655181e594512faeac7d7fd9620ce91105f45bf643Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1266578f3a35fecabff49bad808c5301313f785b5462Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
12675181e594512faeac7d7fd9620ce91105f45bf643Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
1268949f8fef77186c7361d22e1ea6f42c76ceda42b0Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
12691e093206734568e780c2717da1c89b97e9471954Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1270b5635b129b3ca3a9c879a36f58f5b8c4903d267aJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
12715f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
12725f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
12735f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
12745f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
12755f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
12765f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
12775f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
12785f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
12795f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
12805f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
12815f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1282606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1283606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1284606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1285606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1286606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1287606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1288606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
128978a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
129078a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
129178a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
129278a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
129378a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
12943b8006e51038ef263a0404756d9e190c9a9f74d5Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
12951e76253220dbe66e048e55680266dd1f4af0be85Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1296980a3da6acdd577ee3ae192e868dc52fe4b7f2e5Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1297bd28ce008bdc68ef5902f68d2d62cbb7fa78c415Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1298cc6e0bbb47f02fd36cd55b3189c0c79079096ab8Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
129990231e7eaf752856a2c13f786f36ec7f641bad28Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
13008c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby
13018c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
130278a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
130385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	{ }
130485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby};
130585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
130685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_bus_match(struct device *dev, struct device_driver *drv)
130785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
130885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
130985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
131085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
131185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hid_match_id(hdev, hdrv->id_table))
131285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return 0;
131385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
131485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* generic wants all non-blacklisted */
131585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!strncmp(hdrv->name, "generic-", 8))
131685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return !hid_match_id(hdev, hid_blacklist);
131785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
131885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 1;
131985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
132085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
132185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_device_probe(struct device *dev)
132285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
132385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = container_of(dev->driver,
132485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			struct hid_driver, driver);
132585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
132685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	const struct hid_device_id *id;
132785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret = 0;
132885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
132985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hdev->driver) {
1330c500c9714011edab021591340042787722db9cf0Jiri Slaby		id = hid_match_id(hdev, hdrv->id_table);
1331c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (id == NULL)
1332c500c9714011edab021591340042787722db9cf0Jiri Slaby			return -ENODEV;
133385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1334c500c9714011edab021591340042787722db9cf0Jiri Slaby		hdev->driver = hdrv;
1335c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (hdrv->probe) {
1336c500c9714011edab021591340042787722db9cf0Jiri Slaby			ret = hdrv->probe(hdev, id);
1337c500c9714011edab021591340042787722db9cf0Jiri Slaby		} else { /* default probe */
1338c500c9714011edab021591340042787722db9cf0Jiri Slaby			ret = hid_parse(hdev);
1339c500c9714011edab021591340042787722db9cf0Jiri Slaby			if (!ret)
134093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
134185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		}
1342c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (ret)
1343c500c9714011edab021591340042787722db9cf0Jiri Slaby			hdev->driver = NULL;
134485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
134585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
134685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
134785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
134885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_device_remove(struct device *dev)
134985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
135085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
135185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = hdev->driver;
135285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
135385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv) {
135485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (hdrv->remove)
135585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdrv->remove(hdev);
1356c500c9714011edab021591340042787722db9cf0Jiri Slaby		else /* default remove */
1357c500c9714011edab021591340042787722db9cf0Jiri Slaby			hid_hw_stop(hdev);
135885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->driver = NULL;
135985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
136085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
136185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
136285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
136385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
136485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
136585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
136685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
136785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
136885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
136985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdev->bus, hdev->vendor, hdev->product))
137085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
137185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
137285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
137385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
137485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
137585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
137685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
137785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
137885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
137985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
138085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
138185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
138285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdev->bus, hdev->vendor, hdev->product))
138385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
138485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
138585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
138685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
138785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
138885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic struct bus_type hid_bus_type = {
138985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.name		= "hid",
139085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.match		= hid_bus_match,
139185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.probe		= hid_device_probe,
139285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.remove		= hid_device_remove,
139385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.uevent		= hid_uevent,
139485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby};
139585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1396d458a9dfc4de24870b8c747484b1988726534beeJiri Slabystatic const struct hid_device_id hid_ignore_list[] = {
1397d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1398d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1399d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1400d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1401d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1402d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1403d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1404d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1405d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1406d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1407d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1408d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1409d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1410ac2d98998c32fcdc8d8919b7a4536a7945fd0422Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2)},
141162a56582e01b1c5139b235004548e233201df9aaAlexey Klimov	{ HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1412d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1413d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1414d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1415d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1416d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1417d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1418d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1419d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1420d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1421d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1422d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1423d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1424d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1425d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1426d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1427d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1428d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1429d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1430d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1431d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1432d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1433d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1434d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1435d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1436d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1437d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1438d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1439d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1440d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1441d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1442d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1443d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1444d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1445d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1446d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1447d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1448d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1449d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1450d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1451d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1452d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1453d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1454d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1455d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1456d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1457d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1458d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1459d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1460d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1461d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1462d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1463d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1464d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1465d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1466d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1467d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1468d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1469d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1470d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1471d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1472d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1473d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1474d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1475d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1476d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1477d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1478d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1479d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1480d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1481d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1482d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1483d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1484d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1485d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1486d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1487c91c21c5a6facddce936d82e5bc0c655d04288aaAlexey Klimov	{ HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
1488d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1489d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1490d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1491d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1492d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1493d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1494d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1495d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1496d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1497d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1498d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1499d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1500d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1501d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1502d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1503d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1504d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1505d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1506d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1507d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1508d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1509d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1510d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1511d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1512d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1513d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1514d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1515d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1516d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1517d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1518d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1519d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1520d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1521d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1522d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD) },
152324c88eb671b437d700fbfa7aa79adeb9690db1ebJarod Wilson	{ HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD2) },
152424c88eb671b437d700fbfa7aa79adeb9690db1ebJarod Wilson	{ HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD3) },
1525d1d3a5f6eaee337d793ab9ac28e696f0262c3c8aRemi Cattiau	{ HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY1) },
1526d1d3a5f6eaee337d793ab9ac28e696f0262c3c8aRemi Cattiau	{ HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY2) },
152710e41a711e55f485709b4ca157e587cf36ef5a69Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
152810e41a711e55f485709b4ca157e587cf36ef5a69Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
152910e41a711e55f485709b4ca157e587cf36ef5a69Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
153010e41a711e55f485709b4ca157e587cf36ef5a69Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1531d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1532d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1533d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1534d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1535d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1536d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1537d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1538d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1539d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1540d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1541987fbc1f7d446f4bf7063d3b756ae29db80be75eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1542987fbc1f7d446f4bf7063d3b756ae29db80be75eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1543d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ }
1544d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby};
1545d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
1546b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby/**
1547b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1548b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby *
1549b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * There are composite devices for which we want to ignore only a certain
1550b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * interface. This is a list of devices for which only the mouse interface will
1551b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * be ignored. This allows a dedicated driver to take care of the interface.
1552b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby */
1553b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slabystatic const struct hid_device_id hid_mouse_ignore_list[] = {
1554b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	/* appletouch driver */
1555b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1556b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1557b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1558b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1559b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1560b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1561b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1562b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1563b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1564b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1565b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1566b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1567b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1568b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1569b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1570b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1571b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1572b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1573b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1574b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1575ac26fca3e14c8882e382daa7e96ab73e0186cf03Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1576ac26fca3e14c8882e382daa7e96ab73e0186cf03Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1577ac26fca3e14c8882e382daa7e96ab73e0186cf03Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1578b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1579b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1580b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ }
1581b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby};
1582b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby
1583d458a9dfc4de24870b8c747484b1988726534beeJiri Slabystatic bool hid_ignore(struct hid_device *hdev)
1584d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby{
1585d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	switch (hdev->vendor) {
1586d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	case USB_VENDOR_ID_CODEMERCS:
1587d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		/* ignore all Code Mercenaries IOWarrior devices */
1588d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1589d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby				hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1590d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby			return true;
1591d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		break;
1592d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	case USB_VENDOR_ID_LOGITECH:
1593d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1594d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby				hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1595d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby			return true;
1596d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		break;
1597d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	}
1598d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
1599b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	if (hdev->type == HID_TYPE_USBMOUSE &&
1600b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby			hid_match_id(hdev, hid_mouse_ignore_list))
1601b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby		return true;
1602b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby
1603d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	return !!hid_match_id(hdev, hid_ignore_list);
1604d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby}
1605d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
160685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_add_device(struct hid_device *hdev)
160785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
160885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	static atomic_t id = ATOMIC_INIT(0);
160985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
161085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
161185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (WARN_ON(hdev->status & HID_STAT_ADDED))
161285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -EBUSY;
161385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1614d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	/* we need to kill them here, otherwise they will stay allocated to
1615d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	 * wait for coming driver */
1616d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	if (hid_ignore(hdev))
1617d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		return -ENODEV;
1618d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
16196bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers	/* XXX hack, any other cleaner solution after the driver core
16206bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers	 * is converted to allow more than 20 bytes as the device name? */
16216bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
16226bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers		     hdev->vendor, hdev->product, atomic_inc_return(&id));
162385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
162485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = device_add(&hdev->dev);
162585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!ret)
162685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->status |= HID_STAT_ADDED;
162785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
162885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
162985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
163085cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_add_device);
163185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
163285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
163385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_allocate_device - allocate new hid device descriptor
163485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
163585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * Allocate and initialize hid device, so that hid_destroy_device might be
163685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * used to free it.
163785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
163885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
163985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * error value.
164085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
164185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystruct hid_device *hid_allocate_device(void)
164285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
164385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev;
164485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int i;
164585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret = -ENOMEM;
164685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
164785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
164885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev == NULL)
164985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return ERR_PTR(ret);
165085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
165185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	device_initialize(&hdev->dev);
165285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->dev.release = hid_device_release;
165385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->dev.bus = &hid_bus_type;
165485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
165585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
165685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			sizeof(struct hid_collection), GFP_KERNEL);
165785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev->collection == NULL)
165885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
165985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
166085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
166185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (i = 0; i < HID_REPORT_TYPES; i++)
166285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
166385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
166485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return hdev;
166585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
166685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	put_device(&hdev->dev);
166785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ERR_PTR(ret);
166885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
166985cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_allocate_device);
167085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
167185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_remove_device(struct hid_device *hdev)
167285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
167385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev->status & HID_STAT_ADDED) {
167485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		device_del(&hdev->dev);
167585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->status &= ~HID_STAT_ADDED;
167685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
167785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
167885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
167985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
168085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_destroy_device - free previously allocated device
168185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
168285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hdev: hid device
168385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
168485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * If you allocate hid_device through hid_allocate_device, you should ever
168585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * free by this function.
168685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
168785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_destroy_device(struct hid_device *hdev)
168885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
168985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hid_remove_device(hdev);
169085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	put_device(&hdev->dev);
169185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
169285cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_destroy_device);
169385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
169485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
169585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const char *mod_name)
169685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
169785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.name = hdrv->name;
169885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.bus = &hid_bus_type;
169985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.owner = owner;
170085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.mod_name = mod_name;
170185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
170285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return driver_register(&hdrv->driver);
170385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
170485cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(__hid_register_driver);
170585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
170685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_unregister_driver(struct hid_driver *hdrv)
170785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
170885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	driver_unregister(&hdrv->driver);
170985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
171085cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_unregister_driver);
171185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
171202ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby#ifdef CONFIG_HID_COMPAT
171302ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slabystatic void hid_compat_load(struct work_struct *ws)
171402ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby{
171502ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby	request_module("hid-dummy");
171602ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby}
171702ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slabystatic DECLARE_WORK(hid_compat_work, hid_compat_load);
1718d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slabystatic struct workqueue_struct *hid_compat_wq;
171902ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby#endif
172002ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby
172186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic int __init hid_init(void)
172286166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
172385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
172485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
172585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = bus_register(&hid_bus_type);
172685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (ret) {
172785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		printk(KERN_ERR "HID: can't register hid bus\n");
172885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
172985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
173085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
173185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = hidraw_init();
173285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (ret)
173385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err_bus;
173485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
173502ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby#ifdef CONFIG_HID_COMPAT
173643ff3a48c13f3ddc085271c2eea2985d28c8aa08Andi Kleen	hid_compat_wq = create_singlethread_workqueue("hid_compat");
1737d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby	if (!hid_compat_wq) {
1738d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby		hidraw_exit();
1739d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby		goto err;
1740d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby	}
1741d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby	queue_work(hid_compat_wq, &hid_compat_work);
174202ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby#endif
174302ae9a1a8bc1d08a8fd5f6a0b8bde400b0f891b9Jiri Slaby
174485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
174585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr_bus:
174685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	bus_unregister(&hid_bus_type);
174785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
174885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
174986166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
175086166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
175186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic void __exit hid_exit(void)
175286166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
1753d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby#ifdef CONFIG_HID_COMPAT
1754d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby	destroy_workqueue(hid_compat_wq);
1755d92870ddd248e8c2562a8c4047885d3ad221ece7Jiri Slaby#endif
175686166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina	hidraw_exit();
175785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	bus_unregister(&hid_bus_type);
175886166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
175986166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
176086166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_init(hid_init);
176186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_exit(hid_exit);
176286166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
1763aa938f7974b82cfd9ee955031987344f332b7c77Jiri KosinaMODULE_LICENSE(DRIVER_LICENSE);
1764aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina
1765