hid-core.c revision e8d0eab4d9eda9f5e97852f780f020bfb134f9f0
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 Kosinaint hid_debug = 0;
48377e10fbb4ef219f540d06796e9fb0d3ec35a58fAnssi Hannulamodule_param_named(debug, hid_debug, int, 0600);
49cd667ce24796700e1a0e6e7528efc61c96ff832eJiri KosinaMODULE_PARM_DESC(debug, "toggle HID debugging messages");
5058037eb961f859607b161c50d9d4ecb374de1e8fJiri KosinaEXPORT_SYMBOL_GPL(hid_debug);
5158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina
52dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
53dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new report for a device.
54dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
55dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
56dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
57dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
58dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report_enum *report_enum = device->report_enum + type;
59dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
60dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
61dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report_enum->report_id_hash[id])
62dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return report_enum->report_id_hash[id];
63dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
64dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
65dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
66dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
67dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (id != 0)
68dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		report_enum->numbered = 1;
69dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
70dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->id = id;
71dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->type = type;
72dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size = 0;
73dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->device = device;
74dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report_enum->report_id_hash[id] = report;
75dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
76dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	list_add_tail(&report->list, &report_enum->report_list);
77dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
78dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return report;
79dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
80dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
81dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
82dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
83dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
84dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
85dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
86dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
87dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
88dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
89dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report->maxfield == HID_MAX_FIELDS) {
9058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("too many fields in report\n");
91dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
92dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
93dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
94dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
95dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
96dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
97dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->index = report->maxfield++;
98dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->field[field->index] = field;
99dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->usage = (struct hid_usage *)(field + 1);
100282bfd4cbcc2bbeb1a2cad1f42debd378e0b5ac9Jiri Slaby	field->value = (s32 *)(field->usage + usages);
101dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report = report;
102dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
103dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return field;
104dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
105dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
106dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
107dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Open a collection. The type/usage is pushed on the stack.
108dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
109dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
110dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int open_collection(struct hid_parser *parser, unsigned type)
111dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
112dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_collection *collection;
113dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned usage;
114dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
115dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usage = parser->local.usage[0];
116dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
117dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
11858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack overflow\n");
119dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
120dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
121dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
122dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->device->maxcollection == parser->device->collection_size) {
123dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		collection = kmalloc(sizeof(struct hid_collection) *
124dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->device->collection_size * 2, GFP_KERNEL);
125dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (collection == NULL) {
12658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("failed to reallocate collection array\n");
127dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
128dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
129dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memcpy(collection, parser->device->collection,
130dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
131dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
132dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memset(collection + parser->device->collection_size, 0,
133dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
134dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
135dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(parser->device->collection);
136dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection = collection;
137dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection_size *= 2;
138dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
139dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
140dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack[parser->collection_stack_ptr++] =
141dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection;
142dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
143dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection = parser->device->collection +
144dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection++;
145dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->type = type;
146dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->usage = usage;
147dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->level = parser->collection_stack_ptr - 1;
148dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
149dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (type == HID_COLLECTION_APPLICATION)
150dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxapplication++;
151dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
152dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
153dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
154dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
155dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
156dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Close a collection.
157dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
158dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
159dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int close_collection(struct hid_parser *parser)
160dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
161dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->collection_stack_ptr) {
16258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack underflow\n");
163dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
164dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
165dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack_ptr--;
166dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
167dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
168dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
169dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
170dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Climb up the stack, search for the specified collection type
171dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * and return the usage.
172dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
173dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
174dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
175dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
176dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int n;
177dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
178dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (parser->device->collection[parser->collection_stack[n]].type == type)
179dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return parser->device->collection[parser->collection_stack[n]].usage;
180dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0; /* we know nothing about this usage type */
181dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
182dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
183dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
184dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Add a usage to the temporary parser table.
185dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
186dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
187dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_usage(struct hid_parser *parser, unsigned usage)
188dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
189dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->local.usage_index >= HID_MAX_USAGES) {
19058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("usage index exceeded\n");
191dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
192dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
193dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage[parser->local.usage_index] = usage;
194dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.collection_index[parser->local.usage_index] =
195dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack_ptr ?
196dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
197dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage_index++;
198dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
199dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
200dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
201dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
202dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
203dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
204dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
205dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
206dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
207dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
208dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
209dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int usages;
210dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset;
211dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int i;
212dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
213dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
21458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("hid_register_report failed\n");
215dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
216dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
217dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
218dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->global.logical_maximum < parser->global.logical_minimum) {
21958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
220dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
221dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
222dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
223dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	offset = report->size;
224dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size += parser->global.report_size * parser->global.report_count;
225dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
226dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->local.usage_index) /* Ignore padding fields */
227dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
228dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
229dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usages = max_t(int, parser->local.usage_index, parser->global.report_count);
230dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
231dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
232dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
233dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
234dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
235dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
236dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
237dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
238dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < usages; i++) {
239dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		int j = i;
240dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		/* Duplicate the last usage we parsed if we have excess values */
241dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (i >= parser->local.usage_index)
242dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			j = parser->local.usage_index - 1;
243dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].hid = parser->local.usage[j];
244dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].collection_index =
245dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->local.collection_index[j];
246dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
247dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
248dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->maxusage = usages;
249dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->flags = flags;
250dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_offset = offset;
251dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_type = report_type;
252dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_size = parser->global.report_size;
253dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_count = parser->global.report_count;
254dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_minimum = parser->global.logical_minimum;
255dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_maximum = parser->global.logical_maximum;
256dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_minimum = parser->global.physical_minimum;
257dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_maximum = parser->global.physical_maximum;
258dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit_exponent = parser->global.unit_exponent;
259dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit = parser->global.unit;
260dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
261dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
262dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
263dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
264dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
265dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Read data value from item.
266dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
267dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
268dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u32 item_udata(struct hid_item *item)
269dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
270dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
271880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 1: return item->data.u8;
272880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 2: return item->data.u16;
273880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 4: return item->data.u32;
274dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
275dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
276dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
277dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
278dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic s32 item_sdata(struct hid_item *item)
279dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
280dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
281880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 1: return item->data.s8;
282880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 2: return item->data.s16;
283880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 4: return item->data.s32;
284dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
285dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
286dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
287dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
288dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
289dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a global item.
290dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
291dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
292dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
293dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
294dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
295880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_PUSH:
296dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
297880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
298880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("global enviroment stack overflow\n");
299880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
300880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
301dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
302880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		memcpy(parser->global_stack + parser->global_stack_ptr++,
303880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			&parser->global, sizeof(struct hid_global));
304880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
305dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
306880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_POP:
307dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
308880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (!parser->global_stack_ptr) {
309880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("global enviroment stack underflow\n");
310880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
311880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
312dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
313880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		memcpy(&parser->global, parser->global_stack +
314880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			--parser->global_stack_ptr, sizeof(struct hid_global));
315880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
316dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
317880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
318880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.usage_page = item_udata(item);
319880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
320dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
321880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
322880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.logical_minimum = item_sdata(item);
323880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
324dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
325880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
326880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.logical_minimum < 0)
327880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.logical_maximum = item_sdata(item);
328880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		else
329880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.logical_maximum = item_udata(item);
330880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
331dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
332880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
333880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.physical_minimum = item_sdata(item);
334880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
335dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
336880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
337880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.physical_minimum < 0)
338880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.physical_maximum = item_sdata(item);
339880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		else
340880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->global.physical_maximum = item_udata(item);
341880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
342dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
343880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
344880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.unit_exponent = item_sdata(item);
345880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
346dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
347880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_UNIT:
348880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.unit = item_udata(item);
349880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
350dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
351880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
352880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.report_size = item_udata(item);
353880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.report_size > 32) {
354880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("invalid report_size %d\n",
355880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby					parser->global.report_size);
356880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
357880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
358880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
359dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
360880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
361880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.report_count = item_udata(item);
362880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.report_count > HID_MAX_USAGES) {
363880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("invalid report_count %d\n",
364880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby					parser->global.report_count);
365880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return -1;
366880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
367880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
368dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
369880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
370880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->global.report_id = item_udata(item);
371880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->global.report_id == 0) {
372880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("report_id 0 is invalid\n");
373dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
374880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
375880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
376880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
377880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	default:
378880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		dbg_hid("unknown global tag 0x%x\n", item->tag);
379880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return -1;
380dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
381dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
382dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
383dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
384dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a local item.
385dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
386dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
387dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
388dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
389dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
390dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
391dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
392dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->size == 0) {
39358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("item data expected for local item\n");
394dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
395dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
396dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
397dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
398dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
399dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
400880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_DELIMITER:
401880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
402880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (data) {
403880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			/*
404880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * We treat items before the first delimiter
405880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * as global to all usage sets (branch 0).
406880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * In the moment we process only these global
407880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 * items and the first delimiter set.
408880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			 */
409880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			if (parser->local.delimiter_depth != 0) {
410880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				dbg_hid("nested delimiters\n");
411880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				return -1;
412dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
413880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->local.delimiter_depth++;
414880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->local.delimiter_branch++;
415880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		} else {
416880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			if (parser->local.delimiter_depth < 1) {
417880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				dbg_hid("bogus close delimiter\n");
418880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				return -1;
419dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
420880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			parser->local.delimiter_depth--;
421880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
422880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 1;
423dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
424880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_USAGE:
425dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
426880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->local.delimiter_branch > 1) {
427880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("alternative usage ignored\n");
428880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return 0;
429880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
430dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
431880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (item->size <= 2)
432880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			data = (parser->global.usage_page << 16) + data;
433dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
434880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return hid_add_usage(parser, data);
435dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
436880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
437dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
438880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->local.delimiter_branch > 1) {
439880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("alternative usage ignored\n");
440dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
441880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
442dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
443880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (item->size <= 2)
444880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			data = (parser->global.usage_page << 16) + data;
445dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
446880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		parser->local.usage_minimum = data;
447880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
448dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
449880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
450dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
451880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (parser->local.delimiter_branch > 1) {
452880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			dbg_hid("alternative usage ignored\n");
453dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
454880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		}
455dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
456880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if (item->size <= 2)
457880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			data = (parser->global.usage_page << 16) + data;
458dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
459880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		for (n = parser->local.usage_minimum; n <= data; n++)
460880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			if (hid_add_usage(parser, n)) {
461880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				dbg_hid("hid_add_usage failed\n");
462880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby				return -1;
463880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			}
464880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
465880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
466880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	default:
467880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
468880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		dbg_hid("unknown local item tag 0x%x\n", item->tag);
469880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return 0;
470dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
471dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
472dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
473dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
474dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
475dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a main item.
476dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
477dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
478dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
479dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
480dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
481dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int ret;
482dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
483dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
484dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
485dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
486880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
487880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = open_collection(parser, data & 0xff);
488880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
489880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_END_COLLECTION:
490880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = close_collection(parser);
491880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
492880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_INPUT:
493880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
494880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
495880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_OUTPUT:
496880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
497880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
498880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case HID_MAIN_ITEM_TAG_FEATURE:
499880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
500880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		break;
501880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	default:
502880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		dbg_hid("unknown main item tag 0x%x\n", item->tag);
503880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		ret = 0;
504dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
505dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
506dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
507dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
508dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return ret;
509dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
510dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
511dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
512dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a reserved item.
513dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
514dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
515dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
516dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
51758037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
518dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
519dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
520dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
521dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
522dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a report and all registered fields. The field->usage and
523dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * field->value table's are allocated behind the field, so we need
524dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * only to free(field) itself.
525dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
526dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
527dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_free_report(struct hid_report *report)
528dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
529dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
530dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
531dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < report->maxfield; n++)
532dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(report->field[n]);
533dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(report);
534dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
535dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
536dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
537dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a device structure, all reports, and all fields.
538dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
539dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
54085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_device_release(struct device *dev)
541dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
54285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *device = container_of(dev, struct hid_device, dev);
54385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned i, j;
544dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
545dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < HID_REPORT_TYPES; i++) {
546dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		struct hid_report_enum *report_enum = device->report_enum + i;
547dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
548dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		for (j = 0; j < 256; j++) {
549dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			struct hid_report *report = report_enum->report_id_hash[j];
550dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (report)
551dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_free_report(report);
552dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
553dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
554dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
555dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device->rdesc);
556767fe7877c2928b5633992ee60a49ad4516dc2afJiri Kosina	kfree(device->collection);
557dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device);
558dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
559dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
560dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
561dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Fetch a report description item from the data stream. We support long
562dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * items, though they are not used yet.
563dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
564dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
565dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
566dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
567dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u8 b;
568dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
569dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((end - start) <= 0)
570dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
571dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
572dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	b = *start++;
573dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
574dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->type = (b >> 2) & 3;
575dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->tag  = (b >> 4) & 15;
576dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
577dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->tag == HID_ITEM_TAG_LONG) {
578dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
579dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->format = HID_ITEM_FORMAT_LONG;
580dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
581dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < 2)
582dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
583dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
584dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->size = *start++;
585dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->tag  = *start++;
586dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
587dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < item->size)
588dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
589dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
590dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->data.longdata = start;
591dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		start += item->size;
592dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return start;
593dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
594dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
595dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->format = HID_ITEM_FORMAT_SHORT;
596dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->size = b & 3;
597dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
598dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
599880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 0:
600880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
601dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
602880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 1:
603880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if ((end - start) < 1)
604880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return NULL;
605880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->data.u8 = *start++;
606880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
607880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
608880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 2:
609880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if ((end - start) < 2)
610880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return NULL;
611880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->data.u16 = get_unaligned_le16(start);
612880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		start = (__u8 *)((__le16 *)start + 1);
613880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
614880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby
615880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 3:
616880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->size++;
617880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		if ((end - start) < 4)
618880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby			return NULL;
619880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		item->data.u32 = get_unaligned_le32(start);
620880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		start = (__u8 *)((__le32 *)start + 1);
621880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby		return start;
622dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
623dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
624dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return NULL;
625dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
626dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
62785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
62885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_parse_report - parse device report
62985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
63085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @device: hid device
63185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @start: report start
63285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @size: report size
63385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
634dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Parse a report description into a hid_device structure. Reports are
635dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * enumerated, fields are attached to these reports.
63685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * 0 returned on success, otherwise nonzero error value.
637dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
63885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_parse_report(struct hid_device *device, __u8 *start,
63985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		unsigned size)
640dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
641dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_parser *parser;
642dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_item item;
643dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u8 *end;
64485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
645dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	static int (*dispatch_type[])(struct hid_parser *parser,
646dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				      struct hid_item *item) = {
647dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_main,
648dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_global,
649dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_local,
650dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_reserved
651dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	};
652dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
653c500c9714011edab021591340042787722db9cf0Jiri Slaby	if (device->driver->report_fixup)
654c500c9714011edab021591340042787722db9cf0Jiri Slaby		device->driver->report_fixup(device, start, size);
655c500c9714011edab021591340042787722db9cf0Jiri Slaby
65685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	device->rdesc = kmalloc(size, GFP_KERNEL);
65785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (device->rdesc == NULL)
65885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
659dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memcpy(device->rdesc, start, size);
660dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	device->rsize = size;
661dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
66285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	parser = vmalloc(sizeof(struct hid_parser));
66385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!parser) {
66485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		ret = -ENOMEM;
66585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
666dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
66785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
66847a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina	memset(parser, 0, sizeof(struct hid_parser));
669dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->device = device;
670dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
671dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	end = start + size;
67285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = -EINVAL;
673dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	while ((start = fetch_item(start, end, &item)) != NULL) {
674dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
675dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (item.format != HID_ITEM_FORMAT_SHORT) {
67658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unexpected long global item\n");
67785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			goto err;
678dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
679dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
680dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (dispatch_type[item.type](parser, &item)) {
68158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("item %u %u %u %u parsing failed\n",
682dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
68385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			goto err;
684dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
685dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
686dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (start == end) {
687dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->collection_stack_ptr) {
68858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("unbalanced collection at end of report description\n");
68985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				goto err;
690dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
691dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_depth) {
69258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("unbalanced delimiter at end of report description\n");
69385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				goto err;
694dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
69547a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina			vfree(parser);
69685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return 0;
697dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
698dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
699dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
70058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
70185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
70247a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina	vfree(parser);
70385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
704dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
705229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_parse_report);
706dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
707dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
708dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Convert a signed n-bit integer to signed 32-bit integer. Common
709dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * cases are done through the compiler, the screwed things has to be
710dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * done by hand.
711dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
712dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
713dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic s32 snto32(__u32 value, unsigned n)
714dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
715dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (n) {
716880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 8:  return ((__s8)value);
717880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 16: return ((__s16)value);
718880d29f109428be1d027adf919a7457d8fe41fd3Jiri Slaby	case 32: return ((__s32)value);
719dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
720dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
721dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
722dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
723dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
724dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Convert a signed 32-bit integer to a signed n-bit integer.
725dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
726dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
727dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u32 s32ton(__s32 value, unsigned n)
728dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
729dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	s32 a = value >> (n - 1);
730dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (a && a != -1)
731dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
732dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return value & ((1 << n) - 1);
733dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
734dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
735dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
736dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Extract/implement a data field from/to a little endian report (bit array).
737dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
738dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Code sort-of follows HID spec:
739dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
740dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
741dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * While the USB HID spec allows unlimited length bit fields in "report
742dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * descriptors", most devices never use more than 16 bits.
743dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * One model of UPS is claimed to report "LINEV" as a 32-bit field.
744dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
745dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
746dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
747dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
748dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
749dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u64 x;
750dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
751c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (n > 32)
752c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
753c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				n, current->comm);
754dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
755dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report += offset >> 3;  /* adjust byte index */
756229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina	offset &= 7;            /* now only need bit offset into one byte */
757c105068f2b35343eecf2bf16ee29a362b6121fa3Harvey Harrison	x = get_unaligned_le64(report);
758229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina	x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
759dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return (u32) x;
760dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
761dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
762dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
763dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * "implement" : set bits in a little endian bit stream.
764dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Same concepts as "extract" (see comments above).
765dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * The data mangled in the bit stream remains in little endian
766dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * order the whole time. It make more sense to talk about
767dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * endianness of register values by considering a register
768dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * a "cached" copy of the little endiad bit stream.
769dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
770dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
771dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
7726f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	u64 x;
773dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u64 m = (1ULL << n) - 1;
774dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
775c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (n > 32)
776c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
777c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				n, current->comm);
778dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
779c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (value > m)
780c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
781c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				value, current->comm);
782dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	WARN_ON(value > m);
783dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	value &= m;
784dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
785dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report += offset >> 3;
786dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	offset &= 7;
787dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
7886f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	x = get_unaligned_le64(report);
7896f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	x &= ~(m << offset);
7906f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	x |= ((u64)value) << offset;
7916f0168d2dacd7972d887e1ca27943ef8af7512a5Harvey Harrison	put_unaligned_le64(x, report);
792dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
793dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
794dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
795dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Search an array for a value.
796dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
797dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
798dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ int search(__s32 *array, __s32 value, unsigned n)
799dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
800dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	while (n--) {
801dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (*array++ == value)
802dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
803dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
804dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return -1;
805dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
806dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
80785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
80885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_match_report - check if driver's raw_event should be called
80985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
81085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hid: hid device
81185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @report_type: type to match against
81285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
81385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * compare hid->driver->report_table->report_type to report->type
81485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
81585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_match_report(struct hid_device *hid, struct hid_report *report)
816dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
81785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	const struct hid_report_id *id = hid->driver->report_table;
81885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
81985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!id) /* NULL means all */
82085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return 1;
82185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
82285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (; id->report_type != HID_TERMINATOR; id++)
82385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (id->report_type == HID_ANY_ID ||
82485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				id->report_type == report->type)
82585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return 1;
82685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
82785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
82885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
82985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
83085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_match_usage - check if driver's event should be called
83185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
83285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hid: hid device
83385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @usage: usage to match against
83485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
83585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * compare hid->driver->usage_table->usage_{type,code} to
83685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * usage->usage_{type,code}
83785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
83885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
83985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
84085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	const struct hid_usage_id *id = hid->driver->usage_table;
84185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
84285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!id) /* NULL means all */
84385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return 1;
84485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
84585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (; id->usage_type != HID_ANY_ID - 1; id++)
84685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if ((id->usage_hid == HID_ANY_ID ||
84785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				id->usage_hid == usage->hid) &&
84885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				(id->usage_type == HID_ANY_ID ||
84985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				id->usage_type == usage->type) &&
85085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				(id->usage_code == HID_ANY_ID ||
85185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				 id->usage_code == usage->code))
85285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return 1;
85385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
85485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
85585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
85685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_process_event(struct hid_device *hid, struct hid_field *field,
85785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		struct hid_usage *usage, __s32 value, int interrupt)
85885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
85985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = hid->driver;
86085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
86185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
862cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	hid_dump_input(hid, usage, value);
86385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
86485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
86585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		ret = hdrv->event(hid, field, usage, value);
86685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (ret != 0) {
86785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			if (ret < 0)
86885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				dbg_hid("%s's event failed with %d\n",
86985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby						hdrv->name, ret);
87085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return;
87185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		}
87285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
87385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
874dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (hid->claimed & HID_CLAIMED_INPUT)
875dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hidinput_hid_event(hid, field, usage, value);
876aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
877aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina		hid->hiddev_hid_event(hid, field, usage, value);
878dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
879dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
880dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
881dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Analyse a received field, and fetch the data from it. The field
882dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * content is stored for next report processing (we do differential
883dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * reporting to the layer).
884dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
885dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
886abdff0f7749a6696ba2a4238b675cbc55abcdb7aAdrian Bunkstatic void hid_input_field(struct hid_device *hid, struct hid_field *field,
887abdff0f7749a6696ba2a4238b675cbc55abcdb7aAdrian Bunk			    __u8 *data, int interrupt)
888dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
889dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
890dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned count = field->report_count;
891dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset = field->report_offset;
892dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
893dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 min = field->logical_minimum;
894dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 max = field->logical_maximum;
895dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 *value;
896dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
897dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
898dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return;
899dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
900dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
901dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
902dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
903dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina						    extract(data, offset + n * size, size);
904dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
905dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
906dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			    && value[n] >= min && value[n] <= max
907dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			    && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
908dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				goto exit;
909dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
910dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
911dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
912dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
913dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
914dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
915dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			continue;
916dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
917dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
918dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (field->value[n] >= min && field->value[n] <= max
919dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& field->usage[field->value[n] - min].hid
920dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& search(value, field->value[n], count))
921dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
922dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
923dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (value[n] >= min && value[n] <= max
924dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& field->usage[value[n] - min].hid
925dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& search(field->value, value[n], count))
926dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
927dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
928dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
929dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memcpy(field->value, value, count * sizeof(__s32));
930dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinaexit:
931dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(value);
932dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
933dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
934dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
935dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Output the field into the report.
936dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
937dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
938dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_output_field(struct hid_field *field, __u8 *data)
939dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
940dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned count = field->report_count;
941dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset = field->report_offset;
942dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
94346386b587086c8d2698222a031bf749688464032Simon Budig	unsigned bitsused = offset + count * size;
944dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
945dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
94646386b587086c8d2698222a031bf749688464032Simon Budig	/* make sure the unused bits in the last byte are zeros */
94746386b587086c8d2698222a031bf749688464032Simon Budig	if (count > 0 && size > 0 && (bitsused % 8) != 0)
94846386b587086c8d2698222a031bf749688464032Simon Budig		data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
94946386b587086c8d2698222a031bf749688464032Simon Budig
950dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
951dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (field->logical_minimum < 0)	/* signed values */
952dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			implement(data, offset + n * size, size, s32ton(field->value[n], size));
953dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		else				/* unsigned values */
954dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			implement(data, offset + n * size, size, field->value[n]);
955dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
956dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
957dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
958dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
959dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Create a report.
960dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
961dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
962229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosinavoid hid_output_report(struct hid_report *report, __u8 *data)
963dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
964dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
965dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
966dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report->id > 0)
967dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		*data++ = report->id;
968dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
969dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < report->maxfield; n++)
970dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_output_field(report->field[n], data);
971dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
972229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_output_report);
973dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
974dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
975dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Set a field value. The report this field belongs to has to be
976dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * created and transferred to the device, to set this value in the
977dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * device.
978dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
979dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
980dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinaint hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
981dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
982dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
983dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
984cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	hid_dump_input(field->report->device, field->usage + offset, value);
985dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
986dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (offset >= field->report_count) {
98758037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
988dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
989dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
990dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (field->logical_minimum < 0) {
991dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (value != snto32(s32ton(value, size), size)) {
99258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("value %d is out of range\n", value);
993dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
994dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
995dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
996dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->value[offset] = value;
997dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
998dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
999229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_set_field);
1000dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
100185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
100285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const u8 *data)
1003aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina{
1004aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	struct hid_report *report;
100585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int n = 0;	/* Normally report number is 0 */
1006aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
100785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* Device uses numbered reports, data[0] is report number */
100885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report_enum->numbered)
100985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		n = *data;
1010aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = report_enum->report_id_hash[n];
101285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report == NULL)
101385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("undefined report_id %u received\n", n);
1014aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return report;
101685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
1017aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
101985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		int interrupt)
102085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
102185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report_enum *report_enum = hid->report_enum + type;
102285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report *report;
102385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int a;
102485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int rsize, csize = size;
102585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	u8 *cdata = data;
1026aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
102785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = hid_get_report(report_enum, data);
102885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!report)
102985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return;
1030aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
103185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report_enum->numbered) {
103285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		cdata++;
103385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		csize--;
1034aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
1035aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1036aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	rsize = ((report->size - 1) >> 3) + 1;
1037aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
103885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (csize < rsize) {
103985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
104085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				csize, rsize);
104185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		memset(cdata + csize, 0, rsize - csize);
1042aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
1043aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1044aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1045aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hid->hiddev_report_event(hid, report);
1046b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1047b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		/* numbered reports need to be passed with the report num */
1048b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		if (report_enum->numbered)
1049b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina			hidraw_report_event(hid, data - 1, size + 1);
1050b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		else
1051b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina			hidraw_report_event(hid, data, size);
1052b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina	}
1053aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
105485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (a = 0; a < report->maxfield; a++)
105585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hid_input_field(hid, report->field[a], cdata, interrupt);
1056aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1057aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (hid->claimed & HID_CLAIMED_INPUT)
1058aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hidinput_report_event(hid, report);
105985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
106085cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_report_raw_event);
106185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
106285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
106385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_input_report - report data from lower layer (usb, bt...)
106485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
106585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hid: hid device
106685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @type: HID report type (HID_*_REPORT)
106785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @data: report contents
106885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @size: size of data parameter
1069ff9b00a226ccea66e6ce70e9083c42f5b6001f73Jiri Kosina * @interrupt: distinguish between interrupt and control transfers
107085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
107185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * This is data entry for lower layers.
107285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
107385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
107485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
107576c317d6e5cb7f58541879006d39774596962715Julia Lawall	struct hid_report_enum *report_enum;
107676c317d6e5cb7f58541879006d39774596962715Julia Lawall	struct hid_driver *hdrv;
107785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report *report;
1078cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	char *buf;
107985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int i;
108085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
108185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
108285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hid || !hid->driver)
108385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENODEV;
108476c317d6e5cb7f58541879006d39774596962715Julia Lawall	report_enum = hid->report_enum + type;
108576c317d6e5cb7f58541879006d39774596962715Julia Lawall	hdrv = hid->driver;
108685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
108785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!size) {
108885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("empty report\n");
108985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -1;
109085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
109185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1092d1ff65226c5afe55f9af38a439058f41b71e114fJiri Kosina	buf = kmalloc(sizeof(char) * HID_DEBUG_BUFSIZE, GFP_ATOMIC);
1093cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina
1094cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	if (!buf) {
1095cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina		report = hid_get_report(report_enum, data);
1096cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina		goto nomem;
1097cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	}
1098cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina
1099cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1100cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina			"\nreport (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1101cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	hid_debug_event(hid, buf);
110285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
110385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = hid_get_report(report_enum, data);
110455dba52458a11126ff4445b5b94ebde03afcf47aJiri Kosina	if (!report) {
110555dba52458a11126ff4445b5b94ebde03afcf47aJiri Kosina		kfree(buf);
110685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -1;
110755dba52458a11126ff4445b5b94ebde03afcf47aJiri Kosina	}
110885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
110985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* dump the report */
1110cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1111cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina			"report %d (size %u) = ", report->id, size);
1112cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	hid_debug_event(hid, buf);
1113cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	for (i = 0; i < size; i++) {
1114cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina		snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1115cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina				" %02x", data[i]);
1116cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina		hid_debug_event(hid, buf);
1117cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	}
1118cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	hid_debug_event(hid, "\n");
111985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1120cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	kfree(buf);
112185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1122cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosinanomem:
112385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
112485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		ret = hdrv->raw_event(hid, report, data, size);
112585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (ret != 0)
112685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return ret < 0 ? ret : 0;
112785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
112885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
112985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hid_report_raw_event(hid, type, data, size, interrupt);
1130aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1131aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	return 0;
1132aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina}
1133aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri KosinaEXPORT_SYMBOL_GPL(hid_input_report);
1134aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
11350f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic bool hid_match_one_id(struct hid_device *hdev,
11360f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		const struct hid_device_id *id)
11370f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina{
11380f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	return id->bus == hdev->bus &&
11390f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
11400f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		(id->product == HID_ANY_ID || id->product == hdev->product);
11410f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina}
11420f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11430f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic const struct hid_device_id *hid_match_id(struct hid_device *hdev,
11440f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		const struct hid_device_id *id)
11450f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina{
11460f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	for (; id->bus; id++)
11470f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		if (hid_match_one_id(hdev, id))
11480f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina			return id;
11490f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11500f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	return NULL;
11510f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina}
11520f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11530f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic const struct hid_device_id hid_hiddev_list[] = {
1154c0bd6a429819f794f626f9e73be4564e18e132dbRichard Hughes	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1155c0bd6a429819f794f626f9e73be4564e18e132dbRichard Hughes	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
11560f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	{ }
11570f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina};
11580f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
11590f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosinastatic bool hid_hiddev(struct hid_device *hdev)
11600f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina{
11610f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	return !!hid_match_id(hdev, hid_hiddev_list);
11620f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina}
11630f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina
116493c10132a7ac160df3175b53f7ee857625412165Jiri Slabyint hid_connect(struct hid_device *hdev, unsigned int connect_mask)
116593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby{
116693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
116793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		"Joystick", "Gamepad", "Keyboard", "Keypad",
116893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		"Multi-Axis Controller"
116993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	};
117093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	const char *type, *bus;
117193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	char buf[64];
117293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	unsigned int i;
117393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	int len;
117493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
117593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->bus != BUS_USB)
117693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		connect_mask &= ~HID_CONNECT_HIDDEV;
11770f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina	if (hid_hiddev(hdev))
11780f37cd0306463ad35f958d8f74a2b00e5b190b4bJiri Kosina		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
117993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
118093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
118193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
118293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->claimed |= HID_CLAIMED_INPUT;
118393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
118493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			!hdev->hiddev_connect(hdev,
118593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				connect_mask & HID_CONNECT_HIDDEV_FORCE))
118693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->claimed |= HID_CLAIMED_HIDDEV;
118793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
118893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->claimed |= HID_CLAIMED_HIDRAW;
118993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
119093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (!hdev->claimed) {
119193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
119293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				"hidraw\n");
119393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		return -ENODEV;
119493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	}
119593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
119693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
119793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
119893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		hdev->ff_init(hdev);
119993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
120093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	len = 0;
120193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->claimed & HID_CLAIMED_INPUT)
120293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		len += sprintf(buf + len, "input");
120393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->claimed & HID_CLAIMED_HIDDEV)
120493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
120593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				hdev->minor);
120693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	if (hdev->claimed & HID_CLAIMED_HIDRAW)
120793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
120893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				((struct hidraw *)hdev->hidraw)->minor);
120993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
121093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	type = "Device";
121193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	for (i = 0; i < hdev->maxcollection; i++) {
121293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		struct hid_collection *col = &hdev->collection[i];
121393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		if (col->type == HID_COLLECTION_APPLICATION &&
121493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
121593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
121693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			type = types[col->usage & 0xffff];
121793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			break;
121893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		}
121993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	}
122093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
122193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	switch (hdev->bus) {
122293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	case BUS_USB:
122393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		bus = "USB";
122493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		break;
122593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	case BUS_BLUETOOTH:
122693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		bus = "BLUETOOTH";
122793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		break;
122893c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	default:
122993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby		bus = "<UNKNOWN>";
123093c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	}
123193c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
123293c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
123393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			buf, bus, hdev->version >> 8, hdev->version & 0xff,
123493c10132a7ac160df3175b53f7ee857625412165Jiri Slaby			type, hdev->name, hdev->phys);
123593c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
123693c10132a7ac160df3175b53f7ee857625412165Jiri Slaby	return 0;
123793c10132a7ac160df3175b53f7ee857625412165Jiri Slaby}
123893c10132a7ac160df3175b53f7ee857625412165Jiri SlabyEXPORT_SYMBOL_GPL(hid_connect);
123993c10132a7ac160df3175b53f7ee857625412165Jiri Slaby
1240c4c259bcc27c4242b012106afdba183622b1735fJiri Kosinavoid hid_disconnect(struct hid_device *hdev)
1241c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina{
1242c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina	if (hdev->claimed & HID_CLAIMED_INPUT)
1243c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina		hidinput_disconnect(hdev);
1244c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina	if (hdev->claimed & HID_CLAIMED_HIDDEV)
1245c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina		hdev->hiddev_disconnect(hdev);
1246c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina	if (hdev->claimed & HID_CLAIMED_HIDRAW)
1247c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina		hidraw_disconnect(hdev);
1248c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina}
1249c4c259bcc27c4242b012106afdba183622b1735fJiri KosinaEXPORT_SYMBOL_GPL(hid_disconnect);
1250c4c259bcc27c4242b012106afdba183622b1735fJiri Kosina
1251bae7eb33b25387fdc7ccae08768bef1f9484a5b0Jiri Kosina/* a list of devices for which there is a specialized driver on HID bus */
125285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic const struct hid_device_id hid_blacklist[] = {
125314a21cd459f97e3b3cc4fcde48fc5bcdb81d097eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
125414a21cd459f97e3b3cc4fcde48fc5bcdb81d097eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1255df9bcace7b1c29e9be1c13b034ff04f4f4c90edeJiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
12568c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
12578c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
12588c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
12598c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
12608c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
12618c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
12628c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
12638c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
12648c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
12658c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
12668c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
12678c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
12688c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1269fef3f571ecc2a82395c531d97b3f71a59e04e946Ryan Finnie	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1270fef3f571ecc2a82395c531d97b3f71a59e04e946Ryan Finnie	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1271fef3f571ecc2a82395c531d97b3f71a59e04e946Ryan Finnie	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
12728c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
12738c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
12748c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
12758c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
12768c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
12778c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1278ee8a1a0a1a5817accd03ced7e7ffde3a4430f485Jan Scholz	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1279ee8a1a0a1a5817accd03ced7e7ffde3a4430f485Jan Scholz	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1280ee8a1a0a1a5817accd03ced7e7ffde3a4430f485Jan Scholz	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
12818c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
12828c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
12838c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
12848c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
12858c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
12868c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1287a96d6ef34751093797c3a6c6080733dd7af23d35Henrik Rydberg	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1288a96d6ef34751093797c3a6c6080733dd7af23d35Henrik Rydberg	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1289a96d6ef34751093797c3a6c6080733dd7af23d35Henrik Rydberg	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
12908c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
12918c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1292b5635b129b3ca3a9c879a36f58f5b8c4903d267aJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
12933b239cd739a9499da08326356add3d9d992c7911Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1294fcfacfd3594d5d2fa99fb5e7d33dee3904b1a156Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
12950f2213208f8da51bcb665309e3468f000489c04fJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
12960f2213208f8da51bcb665309e3468f000489c04fJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1297e8d0eab4d9eda9f5e97852f780f020bfb134f9f0Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) },
12980f2213208f8da51bcb665309e3468f000489c04fJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
12993f866fbd52d1863db5c07700e560aef22c4fdc01Richard Walmsley	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
13001f243e302cea1561ac881eb5d27041c5342beba4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
13015181e594512faeac7d7fd9620ce91105f45bf643Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1302578f3a35fecabff49bad808c5301313f785b5462Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
13035181e594512faeac7d7fd9620ce91105f45bf643Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
130442859e0bd21daba9974757fcfe4a4dde265fe28dLukasz Lubojanski	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1305949f8fef77186c7361d22e1ea6f42c76ceda42b0Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
13061e093206734568e780c2717da1c89b97e9471954Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1307fdf93aa33268889e126aa677f2072238bd76adb0Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
1308794227415f8f5f395a9ed4269a4ee7a3c34f66a5Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
1309b5635b129b3ca3a9c879a36f58f5b8c4903d267aJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
13105f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
13115f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
13125f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
13135f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
13145f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
13155f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
13165f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
13175f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
13185f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
13195f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
13205f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1321606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1322606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1323606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1324fd30ea8c871552ddd6a5e1c0886de8fef4df53bcJiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
1325606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1326606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1327606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1328243b706d8a71364ad6080328d45b73516c8af5f3Christophe Borivant	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
1329606bd0a8616a0e59021cb2997e942513f24f641dJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
133078a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
133178a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
133278a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
133378a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
133478a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
13353b8006e51038ef263a0404756d9e190c9a9f74d5Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
133694011f93f2cd7410401e22390cf7a14fe5495a22Rafi Rubin	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
13371e76253220dbe66e048e55680266dd1f4af0be85Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1338980a3da6acdd577ee3ae192e868dc52fe4b7f2e5Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1339bd28ce008bdc68ef5902f68d2d62cbb7fa78c415Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1340cc6e0bbb47f02fd36cd55b3189c0c79079096ab8Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
134190231e7eaf752856a2c13f786f36ec7f641bad28Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1342daedb3d6a91f9626ab4c921378ac52e44de833d5Anssi Hannula	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1343daedb3d6a91f9626ab4c921378ac52e44de833d5Anssi Hannula	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
13447a84b1336a145d683fb8cdfd6c2c67545a58b126Ruben Aos Garralda	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
13457a84b1336a145d683fb8cdfd6c2c67545a58b126Ruben Aos Garralda	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
1346daedb3d6a91f9626ab4c921378ac52e44de833d5Anssi Hannula	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1347daedb3d6a91f9626ab4c921378ac52e44de833d5Anssi Hannula	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1348f14f526d02b14fd0b8c1ac4ec413e4577ad5f62eLev Babiev	{ HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
1349711a680e35059bc5c7c28d3c4bd0bebd3b7bb6eeBruno Premont	{ HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
1350fac733f029251a393c42a8313432f2d9fe43bb83Jussi Kivilinna	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
1351ca2dcd40f54c8928b3994712a6cadd2078a087faBastien Nocera	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
1352daedb3d6a91f9626ab4c921378ac52e44de833d5Anssi Hannula	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1353daedb3d6a91f9626ab4c921378ac52e44de833d5Anssi Hannula	{ HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
13548c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby
135578a849a682a1d5ee7b7187b08abdc48656326a4eJiri Slaby	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
135685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	{ }
135785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby};
135885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
13593a6f82f7a22cf19687f556997c6978b31c109360Jiri Slabystruct hid_dynid {
13603a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	struct list_head list;
13613a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	struct hid_device_id id;
13623a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby};
13633a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
13643a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby/**
13653a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby * store_new_id - add a new HID device ID to this driver and re-probe devices
13663a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby * @driver: target device driver
13673a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby * @buf: buffer for scanning device ID data
13683a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby * @count: input size
13693a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby *
13703a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby * Adds a new dynamic hid device ID to this driver,
13713a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby * and causes the driver to probe for all devices again.
13723a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby */
13733a6f82f7a22cf19687f556997c6978b31c109360Jiri Slabystatic ssize_t store_new_id(struct device_driver *drv, const char *buf,
13743a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		size_t count)
13753a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby{
13763a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
13773a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	struct hid_dynid *dynid;
13783a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	__u32 bus, vendor, product;
13793a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	unsigned long driver_data = 0;
13803a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	int ret;
13813a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
13823a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	ret = sscanf(buf, "%x %x %x %lx",
13833a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby			&bus, &vendor, &product, &driver_data);
13843a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	if (ret < 3)
13853a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		return -EINVAL;
13863a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
13873a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
13883a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	if (!dynid)
13893a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		return -ENOMEM;
13903a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
13913a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	dynid->id.bus = bus;
13923a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	dynid->id.vendor = vendor;
13933a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	dynid->id.product = product;
13943a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	dynid->id.driver_data = driver_data;
13953a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
13963a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	spin_lock(&hdrv->dyn_lock);
13973a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	list_add_tail(&dynid->list, &hdrv->dyn_list);
13983a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	spin_unlock(&hdrv->dyn_lock);
13993a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
14003a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	ret = 0;
14013a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	if (get_driver(&hdrv->driver)) {
14023a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		ret = driver_attach(&hdrv->driver);
14033a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		put_driver(&hdrv->driver);
14043a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	}
14053a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
14063a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	return ret ? : count;
14073a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby}
14083a6f82f7a22cf19687f556997c6978b31c109360Jiri Slabystatic DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
14093a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
14103a6f82f7a22cf19687f556997c6978b31c109360Jiri Slabystatic void hid_free_dynids(struct hid_driver *hdrv)
14113a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby{
14123a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	struct hid_dynid *dynid, *n;
14133a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
14143a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	spin_lock(&hdrv->dyn_lock);
14153a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
14163a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		list_del(&dynid->list);
14173a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		kfree(dynid);
14183a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	}
14193a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	spin_unlock(&hdrv->dyn_lock);
14203a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby}
14213a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
14223a6f82f7a22cf19687f556997c6978b31c109360Jiri Slabystatic const struct hid_device_id *hid_match_device(struct hid_device *hdev,
14233a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		struct hid_driver *hdrv)
14243a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby{
14253a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	struct hid_dynid *dynid;
14263a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
14273a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	spin_lock(&hdrv->dyn_lock);
14283a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
14293a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		if (hid_match_one_id(hdev, &dynid->id)) {
14303a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby			spin_unlock(&hdrv->dyn_lock);
14313a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby			return &dynid->id;
14323a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		}
14333a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	}
14343a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	spin_unlock(&hdrv->dyn_lock);
14353a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
14363a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	return hid_match_id(hdev, hdrv->id_table);
14373a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby}
14383a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
143985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_bus_match(struct device *dev, struct device_driver *drv)
144085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
144185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
144285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
144385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
14443a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	if (!hid_match_device(hdev, hdrv))
144585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return 0;
144685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
144785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* generic wants all non-blacklisted */
144885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!strncmp(hdrv->name, "generic-", 8))
144985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return !hid_match_id(hdev, hid_blacklist);
145085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
145185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 1;
145285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
145385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
145485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_device_probe(struct device *dev)
145585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
145685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = container_of(dev->driver,
145785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			struct hid_driver, driver);
145885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
145985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	const struct hid_device_id *id;
146085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret = 0;
146185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
146285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hdev->driver) {
14633a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		id = hid_match_device(hdev, hdrv);
1464c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (id == NULL)
1465c500c9714011edab021591340042787722db9cf0Jiri Slaby			return -ENODEV;
146685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1467c500c9714011edab021591340042787722db9cf0Jiri Slaby		hdev->driver = hdrv;
1468c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (hdrv->probe) {
1469c500c9714011edab021591340042787722db9cf0Jiri Slaby			ret = hdrv->probe(hdev, id);
1470c500c9714011edab021591340042787722db9cf0Jiri Slaby		} else { /* default probe */
1471c500c9714011edab021591340042787722db9cf0Jiri Slaby			ret = hid_parse(hdev);
1472c500c9714011edab021591340042787722db9cf0Jiri Slaby			if (!ret)
147393c10132a7ac160df3175b53f7ee857625412165Jiri Slaby				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
147485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		}
1475c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (ret)
1476c500c9714011edab021591340042787722db9cf0Jiri Slaby			hdev->driver = NULL;
147785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
147885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
147985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
148085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
148185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_device_remove(struct device *dev)
148285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
148385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
148485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = hdev->driver;
148585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
148685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv) {
148785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (hdrv->remove)
148885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdrv->remove(hdev);
1489c500c9714011edab021591340042787722db9cf0Jiri Slaby		else /* default remove */
1490c500c9714011edab021591340042787722db9cf0Jiri Slaby			hid_hw_stop(hdev);
149185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->driver = NULL;
149285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
149385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
149485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
149585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
149685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
149785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
149885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
149985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
150085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
150185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
150285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdev->bus, hdev->vendor, hdev->product))
150385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
150485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
150585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
150685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
150785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
150885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
150985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
151085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
151185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
151285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
151385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
151485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
151585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdev->bus, hdev->vendor, hdev->product))
151685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
151785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
151885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
151985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
152085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
152185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic struct bus_type hid_bus_type = {
152285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.name		= "hid",
152385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.match		= hid_bus_match,
152485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.probe		= hid_device_probe,
152585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.remove		= hid_device_remove,
152685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.uevent		= hid_uevent,
152785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby};
152885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1529bae7eb33b25387fdc7ccae08768bef1f9484a5b0Jiri Kosina/* a list of devices that shouldn't be handled by HID core at all */
1530d458a9dfc4de24870b8c747484b1988726534beeJiri Slabystatic const struct hid_device_id hid_ignore_list[] = {
1531d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1532d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1533d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1534d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1535d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1536d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1537d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1538d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1539d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1540d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1541d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1542d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1543d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1544ac2d98998c32fcdc8d8919b7a4536a7945fd0422Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2)},
154562a56582e01b1c5139b235004548e233201df9aaAlexey Klimov	{ HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1546d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1547d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1548d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1549d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1550d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1551d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
15525f6108cf9be4a77d6bee96750aa4fe18b6b97deeAlexey Klimov	{ HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
1553d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1554d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1555d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1556d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1557d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1558d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1559d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1560d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1561d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1562d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1563d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1564d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1565d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1566d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1567d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1568d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1569d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1570d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1571d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1572d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1573d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1574d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1575d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1576d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1577d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1578d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1579d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1580d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1581d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1582d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1583d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1584d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1585d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1586d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1587d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1588d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1589d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1590d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1591d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1592d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1593d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1594d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1595d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1596d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1597d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1598d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1599d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1600d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1601d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1602d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1603d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1604d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1605d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1606d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1607d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1608d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1609d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1610d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1611d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1612d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1613d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1614d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1615d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1616d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1617d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1618d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1619d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1620d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1621d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1622c91c21c5a6facddce936d82e5bc0c655d04288aaAlexey Klimov	{ HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
1623d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1624d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1625d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1626d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1627d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1628d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1629d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1630d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1631d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1632d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1633d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1634d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1635d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1636d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1637d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1638d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1639d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1640d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1641d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1642d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1643d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1644d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1645d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1646d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1647d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1648d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1649d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1650d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1651d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1652d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1653d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1654d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1655d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1656d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
16574cfae3e80431fd113f0f4a8e1d4fff84aafe045cHenning Glawe	{ HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
165835cfd1d964f3c2420862f2167a0eb85ff1208999Michael Tokarev	{ HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
1659d1d3a5f6eaee337d793ab9ac28e696f0262c3c8aRemi Cattiau	{ HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY1) },
1660d1d3a5f6eaee337d793ab9ac28e696f0262c3c8aRemi Cattiau	{ HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY2) },
1661d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1662d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1663d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1664d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1665d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1666d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1667d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1668d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1669d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1670d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1671d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ }
1672d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby};
1673d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
1674b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby/**
1675b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1676b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby *
1677b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * There are composite devices for which we want to ignore only a certain
1678b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * interface. This is a list of devices for which only the mouse interface will
1679b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby * be ignored. This allows a dedicated driver to take care of the interface.
1680b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby */
1681b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slabystatic const struct hid_device_id hid_mouse_ignore_list[] = {
1682b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	/* appletouch driver */
1683b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1684b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1685b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1686b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1687b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1688b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1689b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1690b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1691b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1692b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1693b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1694b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1695b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1696b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1697b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1698b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1699b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1700b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1701b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1702b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1703ac26fca3e14c8882e382daa7e96ab73e0186cf03Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1704ac26fca3e14c8882e382daa7e96ab73e0186cf03Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1705ac26fca3e14c8882e382daa7e96ab73e0186cf03Jiri Kosina	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1706b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1707b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1708b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	{ }
1709b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby};
1710b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby
1711d458a9dfc4de24870b8c747484b1988726534beeJiri Slabystatic bool hid_ignore(struct hid_device *hdev)
1712d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby{
1713d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	switch (hdev->vendor) {
1714d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	case USB_VENDOR_ID_CODEMERCS:
1715d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		/* ignore all Code Mercenaries IOWarrior devices */
1716d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1717d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby				hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1718d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby			return true;
1719d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		break;
1720d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	case USB_VENDOR_ID_LOGITECH:
1721d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1722d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby				hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1723d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby			return true;
1724d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		break;
172531f7fd795d17b264c3c05e4a976f963012c17c68Jarod Wilson	case USB_VENDOR_ID_SOUNDGRAPH:
172631f7fd795d17b264c3c05e4a976f963012c17c68Jarod Wilson		if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
172731f7fd795d17b264c3c05e4a976f963012c17c68Jarod Wilson		    hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
172831f7fd795d17b264c3c05e4a976f963012c17c68Jarod Wilson			return true;
172931f7fd795d17b264c3c05e4a976f963012c17c68Jarod Wilson		break;
1730d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	}
1731d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
1732b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby	if (hdev->type == HID_TYPE_USBMOUSE &&
1733b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby			hid_match_id(hdev, hid_mouse_ignore_list))
1734b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby		return true;
1735b4d8e4736c94e78fd846dca0c95a3a0710582584Jiri Slaby
1736d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	return !!hid_match_id(hdev, hid_ignore_list);
1737d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby}
1738d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
173985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_add_device(struct hid_device *hdev)
174085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
174185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	static atomic_t id = ATOMIC_INIT(0);
174285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
174385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
174485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (WARN_ON(hdev->status & HID_STAT_ADDED))
174585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -EBUSY;
174685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1747d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	/* we need to kill them here, otherwise they will stay allocated to
1748d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	 * wait for coming driver */
1749d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	if (hid_ignore(hdev))
1750d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		return -ENODEV;
1751d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
17526bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers	/* XXX hack, any other cleaner solution after the driver core
17536bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers	 * is converted to allow more than 20 bytes as the device name? */
17546bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
17556bbe586fd4d94439f3960e200056ff057f7db5c6Kay Sievers		     hdev->vendor, hdev->product, atomic_inc_return(&id));
175685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
175785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = device_add(&hdev->dev);
175885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!ret)
175985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->status |= HID_STAT_ADDED;
176085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1761a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina	hid_debug_register(hdev, dev_name(&hdev->dev));
1762a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina
176385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
176485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
176585cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_add_device);
176685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
176785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
176885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_allocate_device - allocate new hid device descriptor
176985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
177085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * Allocate and initialize hid device, so that hid_destroy_device might be
177185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * used to free it.
177285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
177385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
177485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * error value.
177585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
177685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystruct hid_device *hid_allocate_device(void)
177785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
177885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev;
177985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int i;
178085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret = -ENOMEM;
178185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
178285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
178385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev == NULL)
178485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return ERR_PTR(ret);
178585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
178685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	device_initialize(&hdev->dev);
178785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->dev.release = hid_device_release;
178885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->dev.bus = &hid_bus_type;
178985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
179085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
179185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			sizeof(struct hid_collection), GFP_KERNEL);
179285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev->collection == NULL)
179385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
179485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
179585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
179685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (i = 0; i < HID_REPORT_TYPES; i++)
179785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
179885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1799cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	init_waitqueue_head(&hdev->debug_wait);
1800cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina	INIT_LIST_HEAD(&hdev->debug_list);
1801cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina
180285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return hdev;
180385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
180485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	put_device(&hdev->dev);
180585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ERR_PTR(ret);
180685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
180785cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_allocate_device);
180885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
180985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_remove_device(struct hid_device *hdev)
181085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
181185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev->status & HID_STAT_ADDED) {
181285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		device_del(&hdev->dev);
1813a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina		hid_debug_unregister(hdev);
181485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->status &= ~HID_STAT_ADDED;
181585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
181685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
181785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
181885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
181985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_destroy_device - free previously allocated device
182085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
182185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hdev: hid device
182285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
182385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * If you allocate hid_device through hid_allocate_device, you should ever
182485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * free by this function.
182585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
182685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_destroy_device(struct hid_device *hdev)
182785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
182885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hid_remove_device(hdev);
182985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	put_device(&hdev->dev);
183085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
183185cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_destroy_device);
183285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
183385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
183485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const char *mod_name)
183585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
18363a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	int ret;
18373a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
183885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.name = hdrv->name;
183985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.bus = &hid_bus_type;
184085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.owner = owner;
184185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.mod_name = mod_name;
184285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
18433a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	INIT_LIST_HEAD(&hdrv->dyn_list);
18443a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	spin_lock_init(&hdrv->dyn_lock);
18453a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
18463a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	ret = driver_register(&hdrv->driver);
18473a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	if (ret)
18483a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		return ret;
18493a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
18503a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
18513a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	if (ret)
18523a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby		driver_unregister(&hdrv->driver);
18533a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby
18543a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	return ret;
185585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
185685cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(__hid_register_driver);
185785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
185885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_unregister_driver(struct hid_driver *hdrv)
185985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
18603a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	driver_remove_file(&hdrv->driver, &driver_attr_new_id);
186185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	driver_unregister(&hdrv->driver);
18623a6f82f7a22cf19687f556997c6978b31c109360Jiri Slaby	hid_free_dynids(hdrv);
186385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
186485cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_unregister_driver);
186585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
18660361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukumint hid_check_keys_pressed(struct hid_device *hid)
18670361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum{
18680361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum	struct hid_input *hidinput;
18690361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum	int i;
18700361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum
1871e5288eb5ea89bd82d897cb687348bc608278f920Jiri Kosina	if (!(hid->claimed & HID_CLAIMED_INPUT))
1872e5288eb5ea89bd82d897cb687348bc608278f920Jiri Kosina		return 0;
1873e5288eb5ea89bd82d897cb687348bc608278f920Jiri Kosina
18740361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum	list_for_each_entry(hidinput, &hid->inputs, list) {
18750361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
18760361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum			if (hidinput->input->key[i])
18770361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum				return 1;
18780361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum	}
18790361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum
18800361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum	return 0;
18810361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum}
18820361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum
18830361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver NeukumEXPORT_SYMBOL_GPL(hid_check_keys_pressed);
18840361a28d3f9a4315a100c7b37ba0b55cfe15fe07Oliver Neukum
188586166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic int __init hid_init(void)
188686166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
188785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
188885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1889a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina	if (hid_debug)
1890cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina		printk(KERN_WARNING "HID: hid_debug is now used solely for parser and driver debugging.\n"
1891cd667ce24796700e1a0e6e7528efc61c96ff832eJiri Kosina				"HID: debugfs is now used for inspecting the device (report descriptor, reports)\n");
1892a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina
189385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = bus_register(&hid_bus_type);
189485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (ret) {
189585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		printk(KERN_ERR "HID: can't register hid bus\n");
189685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
189785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
189885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
189985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = hidraw_init();
190085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (ret)
190185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err_bus;
190285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1903a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina	hid_debug_init();
1904a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina
190585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
190685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr_bus:
190785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	bus_unregister(&hid_bus_type);
190885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
190985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
191086166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
191186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
191286166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic void __exit hid_exit(void)
191386166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
1914a635f9dd83f3382577f4544a96df12356e951a40Jiri Kosina	hid_debug_exit();
191586166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina	hidraw_exit();
191685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	bus_unregister(&hid_bus_type);
191786166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
191886166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
191986166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_init(hid_init);
192086166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_exit(hid_exit);
192186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
1922aa938f7974b82cfd9ee955031987344f332b7c77Jiri KosinaMODULE_LICENSE(DRIVER_LICENSE);
1923aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina
1924