hid-core.c revision 8c19a51591d06f5226499972567f528cf6066bb7
1dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
2229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina *  HID support for Linux
3dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
4dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *  Copyright (c) 1999 Andreas Gal
5dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7f142b3a4f64eca235f5890d9cecb68482f62ed26Jiri Kosina *  Copyright (c) 2006-2007 Jiri Kosina
8dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
9dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
10dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
11dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * This program is free software; you can redistribute it and/or modify it
12dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * under the terms of the GNU General Public License as published by the Free
13dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Software Foundation; either version 2 of the License, or (at your option)
14dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * any later version.
15dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
16dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
17dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/module.h>
18dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/slab.h>
19dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/init.h>
20dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/kernel.h>
21dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/list.h>
22dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/mm.h>
23dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/spinlock.h>
24dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <asm/unaligned.h>
25dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <asm/byteorder.h>
26dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/input.h>
27dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/wait.h>
2847a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina#include <linux/vmalloc.h>
29c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina#include <linux/sched.h>
30dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
31dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/hid.h>
32dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#include <linux/hiddev.h>
33c080d89ad91e98fec0e8fc5f448a1ad899bd85c7Jiri Kosina#include <linux/hid-debug.h>
3486166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina#include <linux/hidraw.h>
35dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
365f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby#include "hid-ids.h"
375f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby
38dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
39dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Version Information
40dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
41dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
42dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#define DRIVER_VERSION "v2.6"
43f142b3a4f64eca235f5890d9cecb68482f62ed26Jiri Kosina#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
44531498013163c72cee8540e98e12c0c430c7fe87Jiri Kosina#define DRIVER_DESC "HID core driver"
45dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#define DRIVER_LICENSE "GPL"
46dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
4758037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina#ifdef CONFIG_HID_DEBUG
4858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosinaint hid_debug = 0;
49377e10fbb4ef219f540d06796e9fb0d3ec35a58fAnssi Hannulamodule_param_named(debug, hid_debug, int, 0600);
50377e10fbb4ef219f540d06796e9fb0d3ec35a58fAnssi HannulaMODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
5158037eb961f859607b161c50d9d4ecb374de1e8fJiri KosinaEXPORT_SYMBOL_GPL(hid_debug);
5258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina#endif
5358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina
54dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
55dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new report for a device.
56dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
57dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
58dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
59dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
60dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report_enum *report_enum = device->report_enum + type;
61dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
62dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
63dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report_enum->report_id_hash[id])
64dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return report_enum->report_id_hash[id];
65dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
66dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
67dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
68dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
69dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (id != 0)
70dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		report_enum->numbered = 1;
71dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
72dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->id = id;
73dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->type = type;
74dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size = 0;
75dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->device = device;
76dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report_enum->report_id_hash[id] = report;
77dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
78dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	list_add_tail(&report->list, &report_enum->report_list);
79dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
80dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return report;
81dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
82dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
83dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
84dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
85dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
86dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
87dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
88dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
89dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
90dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
91dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report->maxfield == HID_MAX_FIELDS) {
9258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("too many fields in report\n");
93dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
94dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
95dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
96dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
98dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
99dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->index = report->maxfield++;
100dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->field[field->index] = field;
101dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->usage = (struct hid_usage *)(field + 1);
102282bfd4cbcc2bbeb1a2cad1f42debd378e0b5ac9Jiri Slaby	field->value = (s32 *)(field->usage + usages);
103dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report = report;
104dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
105dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return field;
106dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
107dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
108dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
109dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Open a collection. The type/usage is pushed on the stack.
110dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
111dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
112dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int open_collection(struct hid_parser *parser, unsigned type)
113dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
114dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_collection *collection;
115dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned usage;
116dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
117dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usage = parser->local.usage[0];
118dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
119dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
12058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack overflow\n");
121dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
122dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
123dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
124dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->device->maxcollection == parser->device->collection_size) {
125dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		collection = kmalloc(sizeof(struct hid_collection) *
126dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->device->collection_size * 2, GFP_KERNEL);
127dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (collection == NULL) {
12858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("failed to reallocate collection array\n");
129dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
130dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
131dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memcpy(collection, parser->device->collection,
132dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
133dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
134dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memset(collection + parser->device->collection_size, 0,
135dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
136dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
137dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(parser->device->collection);
138dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection = collection;
139dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection_size *= 2;
140dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
141dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
142dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack[parser->collection_stack_ptr++] =
143dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection;
144dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
145dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection = parser->device->collection +
146dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection++;
147dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->type = type;
148dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->usage = usage;
149dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->level = parser->collection_stack_ptr - 1;
150dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
151dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (type == HID_COLLECTION_APPLICATION)
152dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxapplication++;
153dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
154dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
155dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
156dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
157dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
158dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Close a collection.
159dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
160dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
161dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int close_collection(struct hid_parser *parser)
162dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
163dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->collection_stack_ptr) {
16458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack underflow\n");
165dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
166dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
167dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack_ptr--;
168dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
169dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
170dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
171dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
172dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Climb up the stack, search for the specified collection type
173dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * and return the usage.
174dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
175dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
176dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
177dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
178dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int n;
179dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
180dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (parser->device->collection[parser->collection_stack[n]].type == type)
181dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return parser->device->collection[parser->collection_stack[n]].usage;
182dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0; /* we know nothing about this usage type */
183dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
184dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
185dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
186dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Add a usage to the temporary parser table.
187dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
188dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
189dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_usage(struct hid_parser *parser, unsigned usage)
190dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
191dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->local.usage_index >= HID_MAX_USAGES) {
19258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("usage index exceeded\n");
193dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
194dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
195dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage[parser->local.usage_index] = usage;
196dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.collection_index[parser->local.usage_index] =
197dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack_ptr ?
198dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
199dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage_index++;
200dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
201dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
202dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
203dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
204dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
205dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
206dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
207dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
208dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
209dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
210dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
211dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int usages;
212dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset;
213dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int i;
214dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
215dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
21658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("hid_register_report failed\n");
217dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
218dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
219dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
220dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->global.logical_maximum < parser->global.logical_minimum) {
22158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
222dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
223dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
224dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
225dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	offset = report->size;
226dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size += parser->global.report_size * parser->global.report_count;
227dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
228dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->local.usage_index) /* Ignore padding fields */
229dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
230dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
231dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usages = max_t(int, parser->local.usage_index, parser->global.report_count);
232dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
233dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
234dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
235dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
236dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
237dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
238dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
239dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
240dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < usages; i++) {
241dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		int j = i;
242dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		/* Duplicate the last usage we parsed if we have excess values */
243dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (i >= parser->local.usage_index)
244dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			j = parser->local.usage_index - 1;
245dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].hid = parser->local.usage[j];
246dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].collection_index =
247dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->local.collection_index[j];
248dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
249dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
250dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->maxusage = usages;
251dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->flags = flags;
252dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_offset = offset;
253dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_type = report_type;
254dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_size = parser->global.report_size;
255dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_count = parser->global.report_count;
256dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_minimum = parser->global.logical_minimum;
257dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_maximum = parser->global.logical_maximum;
258dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_minimum = parser->global.physical_minimum;
259dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_maximum = parser->global.physical_maximum;
260dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit_exponent = parser->global.unit_exponent;
261dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit = parser->global.unit;
262dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
263dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
264dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
265dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
266dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
267dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Read data value from item.
268dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
269dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
270dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u32 item_udata(struct hid_item *item)
271dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
272dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
273dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 1: return item->data.u8;
274dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 2: return item->data.u16;
275dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 4: return item->data.u32;
276dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
277dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
278dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
279dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
280dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic s32 item_sdata(struct hid_item *item)
281dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
282dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
283dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 1: return item->data.s8;
284dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 2: return item->data.s16;
285dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 4: return item->data.s32;
286dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
287dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
288dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
289dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
290dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
291dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a global item.
292dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
293dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
294dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
295dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
296dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
297dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
298dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_PUSH:
299dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
300dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
30158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("global enviroment stack overflow\n");
302dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
303dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
304dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
305dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			memcpy(parser->global_stack + parser->global_stack_ptr++,
306dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				&parser->global, sizeof(struct hid_global));
307dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
308dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
309dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_POP:
310dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
311dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (!parser->global_stack_ptr) {
31258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("global enviroment stack underflow\n");
313dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
314dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
315dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
316dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
317dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				sizeof(struct hid_global));
318dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
319dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
320dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
321dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.usage_page = item_udata(item);
322dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
323dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
324dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
325dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.logical_minimum = item_sdata(item);
326dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
327dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
328dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
329dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->global.logical_minimum < 0)
330dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.logical_maximum = item_sdata(item);
331dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			else
332dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.logical_maximum = item_udata(item);
333dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
334dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
335dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
336dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.physical_minimum = item_sdata(item);
337dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
338dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
339dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
340dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->global.physical_minimum < 0)
341dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.physical_maximum = item_sdata(item);
342dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			else
343dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.physical_maximum = item_udata(item);
344dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
345dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
346dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
347dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.unit_exponent = item_sdata(item);
348dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
349dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
350dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_UNIT:
351dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.unit = item_udata(item);
352dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
353dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
354dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
355dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((parser->global.report_size = item_udata(item)) > 32) {
35658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("invalid report_size %d\n", parser->global.report_size);
357dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
358dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
359dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
360dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
361dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
362dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
36358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("invalid report_count %d\n", parser->global.report_count);
364dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
365dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
366dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
367dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
368dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_REPORT_ID:
369dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((parser->global.report_id = item_udata(item)) == 0) {
37058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("report_id 0 is invalid\n");
371dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
372dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
373dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
374dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
375dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		default:
37658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unknown global tag 0x%x\n", item->tag);
377dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
378dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
379dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
380dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
381dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
382dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a local item.
383dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
384dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
385dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
386dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
387dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
388dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
389dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
390dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->size == 0) {
39158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("item data expected for local item\n");
392dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
393dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
394dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
395dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
396dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
397dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
398dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
399dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_DELIMITER:
400dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
401dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (data) {
402dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				/*
403dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * We treat items before the first delimiter
404dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * as global to all usage sets (branch 0).
405dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * In the moment we process only these global
406dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * items and the first delimiter set.
407dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 */
408dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				if (parser->local.delimiter_depth != 0) {
40958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina					dbg_hid("nested delimiters\n");
410dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina					return -1;
411dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				}
412dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->local.delimiter_depth++;
413dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->local.delimiter_branch++;
414dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			} else {
415dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				if (parser->local.delimiter_depth < 1) {
41658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina					dbg_hid("bogus close delimiter\n");
417dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina					return -1;
418dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				}
419dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->local.delimiter_depth--;
420dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
421dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 1;
422dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
423dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_USAGE:
424dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
425dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_branch > 1) {
42658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("alternative usage ignored\n");
427dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return 0;
428dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
429dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
430dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (item->size <= 2)
431dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				data = (parser->global.usage_page << 16) + data;
432dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
433dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return hid_add_usage(parser, data);
434dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
435dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
436dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
437dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_branch > 1) {
43858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("alternative usage ignored\n");
439dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return 0;
440dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
441dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
442dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (item->size <= 2)
443dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				data = (parser->global.usage_page << 16) + data;
444dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
445dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->local.usage_minimum = data;
446dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
447dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
448dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
449dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
450dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_branch > 1) {
45158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("alternative usage ignored\n");
452dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return 0;
453dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
454dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
455dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (item->size <= 2)
456dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				data = (parser->global.usage_page << 16) + data;
457dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
458dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			for (n = parser->local.usage_minimum; n <= data; n++)
459dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				if (hid_add_usage(parser, n)) {
46058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina					dbg_hid("hid_add_usage failed\n");
461dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina					return -1;
462dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				}
463dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
464dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
465dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		default:
466dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
46758037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unknown local item tag 0x%x\n", item->tag);
468dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
469dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
470dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
471dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
472dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
473dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
474dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a main item.
475dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
476dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
477dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
478dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
479dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
480dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int ret;
481dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
482dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
483dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
484dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
485dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
486dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = open_collection(parser, data & 0xff);
487dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
488dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_END_COLLECTION:
489dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = close_collection(parser);
490dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
491dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_INPUT:
492dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = hid_add_field(parser, HID_INPUT_REPORT, data);
493dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
494dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_OUTPUT:
495dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
496dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
497dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_FEATURE:
498dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
499dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
500dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		default:
50158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unknown main item tag 0x%x\n", item->tag);
502dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = 0;
503dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
504dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
505dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
506dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
507dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return ret;
508dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
509dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
510dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
511dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a reserved item.
512dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
513dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
514dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
515dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
51658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
517dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
518dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
519dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
520dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
521dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a report and all registered fields. The field->usage and
522dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * field->value table's are allocated behind the field, so we need
523dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * only to free(field) itself.
524dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
525dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
526dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_free_report(struct hid_report *report)
527dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
528dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
529dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
530dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < report->maxfield; n++)
531dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(report->field[n]);
532dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(report);
533dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
534dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
535dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
536dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a device structure, all reports, and all fields.
537dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
538dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
53985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_device_release(struct device *dev)
540dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
54185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *device = container_of(dev, struct hid_device, dev);
54285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned i, j;
543dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
544dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < HID_REPORT_TYPES; i++) {
545dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		struct hid_report_enum *report_enum = device->report_enum + i;
546dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
547dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		for (j = 0; j < 256; j++) {
548dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			struct hid_report *report = report_enum->report_id_hash[j];
549dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (report)
550dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_free_report(report);
551dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
552dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
553dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
554dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device->rdesc);
555767fe7877c2928b5633992ee60a49ad4516dc2afJiri Kosina	kfree(device->collection);
556dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device);
557dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
558dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
559dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
560dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Fetch a report description item from the data stream. We support long
561dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * items, though they are not used yet.
562dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
563dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
564dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
565dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
566dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u8 b;
567dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
568dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((end - start) <= 0)
569dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
570dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
571dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	b = *start++;
572dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
573dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->type = (b >> 2) & 3;
574dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->tag  = (b >> 4) & 15;
575dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
576dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->tag == HID_ITEM_TAG_LONG) {
577dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
578dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->format = HID_ITEM_FORMAT_LONG;
579dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
580dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < 2)
581dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
582dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
583dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->size = *start++;
584dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->tag  = *start++;
585dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
586dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < item->size)
587dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
588dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
589dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->data.longdata = start;
590dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		start += item->size;
591dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return start;
592dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
593dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
594dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->format = HID_ITEM_FORMAT_SHORT;
595dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->size = b & 3;
596dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
597dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
598dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
599dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 0:
600dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return start;
601dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
602dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 1:
603dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((end - start) < 1)
604dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
605dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			item->data.u8 = *start++;
606dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return start;
607dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
608dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 2:
609dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((end - start) < 2)
610dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
611c105068f2b35343eecf2bf16ee29a362b6121fa3Harvey Harrison			item->data.u16 = get_unaligned_le16(start);
612dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			start = (__u8 *)((__le16 *)start + 1);
613dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return start;
614dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
615dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 3:
616dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			item->size++;
617dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((end - start) < 4)
618dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
619c105068f2b35343eecf2bf16ee29a362b6121fa3Harvey Harrison			item->data.u32 = get_unaligned_le32(start);
620dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			start = (__u8 *)((__le32 *)start + 1);
621dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			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) {
716dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 8:  return ((__s8)value);
717dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 16: return ((__s16)value);
718dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		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
862dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	hid_dump_input(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
984dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	hid_dump_input(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		hid_dump_field(field, 8);
989dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
990dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
991dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (field->logical_minimum < 0) {
992dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (value != snto32(s32ton(value, size), size)) {
99358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("value %d is out of range\n", value);
994dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
995dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
996dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
997dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->value[offset] = value;
998dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
999dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
1000229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_set_field);
1001dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
100285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
100385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const u8 *data)
1004aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina{
1005aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	struct hid_report *report;
100685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int n = 0;	/* Normally report number is 0 */
1007aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
100885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* Device uses numbered reports, data[0] is report number */
100985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report_enum->numbered)
101085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		n = *data;
1011aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = report_enum->report_id_hash[n];
101385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report == NULL)
101485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("undefined report_id %u received\n", n);
1015aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return report;
101785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
1018aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
101985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
102085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		int interrupt)
102185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
102285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report_enum *report_enum = hid->report_enum + type;
102385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report *report;
102485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int a;
102585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int rsize, csize = size;
102685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	u8 *cdata = data;
1027aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
102885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = hid_get_report(report_enum, data);
102985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!report)
103085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return;
1031aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
103285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (report_enum->numbered) {
103385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		cdata++;
103485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		csize--;
1035aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
1036aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1037aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	rsize = ((report->size - 1) >> 3) + 1;
1038aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
103985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (csize < rsize) {
104085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
104185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby				csize, rsize);
104285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		memset(cdata + csize, 0, rsize - csize);
1043aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
1044aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1045aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1046aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hid->hiddev_report_event(hid, report);
1047b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina	if (hid->claimed & HID_CLAIMED_HIDRAW) {
1048b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		/* numbered reports need to be passed with the report num */
1049b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		if (report_enum->numbered)
1050b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina			hidraw_report_event(hid, data - 1, size + 1);
1051b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina		else
1052b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina			hidraw_report_event(hid, data, size);
1053b54ec3c13cf77d21bab25d8bb117f642561fdf4fJiri Kosina	}
1054aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
105585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (a = 0; a < report->maxfield; a++)
105685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hid_input_field(hid, report->field[a], cdata, interrupt);
1057aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1058aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (hid->claimed & HID_CLAIMED_INPUT)
1059aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hidinput_report_event(hid, report);
106085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
106185cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_report_raw_event);
106285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
106385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
106485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_input_report - report data from lower layer (usb, bt...)
106585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
106685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hid: hid device
106785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @type: HID report type (HID_*_REPORT)
106885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @data: report contents
106985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @size: size of data parameter
107085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @interrupt: called from atomic?
107185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
107285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * This is data entry for lower layers.
107385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
107485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
107585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
107685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report_enum *report_enum = hid->report_enum + type;
107785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = hid->driver;
107885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_report *report;
107985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int i;
108085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
108185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
108285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hid || !hid->driver)
108385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENODEV;
108485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
108585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!size) {
108685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid("empty report\n");
108785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -1;
108885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
108985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
109085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
109185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
109285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	report = hid_get_report(report_enum, data);
109385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!report)
109485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -1;
109585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
109685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* dump the report */
109785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	dbg_hid("report %d (size %u) = ", report->id, size);
109885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (i = 0; i < size; i++)
109985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		dbg_hid_line(" %02x", data[i]);
110085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	dbg_hid_line("\n");
110185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
110285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
110385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		ret = hdrv->raw_event(hid, report, data, size);
110485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (ret != 0)
110585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return ret < 0 ? ret : 0;
110685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
110785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
110885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hid_report_raw_event(hid, type, data, size, interrupt);
1109aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1110aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	return 0;
1111aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina}
1112aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri KosinaEXPORT_SYMBOL_GPL(hid_input_report);
1113aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
111485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic bool hid_match_one_id(struct hid_device *hdev,
111585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const struct hid_device_id *id)
111685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
111785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return id->bus == hdev->bus &&
111885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
111985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		(id->product == HID_ANY_ID || id->product == hdev->product);
112085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
112185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
112285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic const struct hid_device_id *hid_match_id(struct hid_device *hdev,
112385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const struct hid_device_id *id)
112485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
112585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (; id->bus; id++)
112685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (hid_match_one_id(hdev, id))
112785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			return id;
112885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
112985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return NULL;
113085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
113185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
113285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic const struct hid_device_id hid_blacklist[] = {
11338c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
11348c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
11358c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
11368c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
11378c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
11388c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
11398c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
11408c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
11418c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
11428c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
11438c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
11448c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
11458c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
11468c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
11478c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
11488c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
11498c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
11508c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
11518c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
11528c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
11538c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
11548c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
11558c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
11568c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
11578c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
11588c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
11598c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
11608c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
11618c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
11628c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
11635f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
11645f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
11655f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
11665f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
11675f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
11685f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
11695f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
11705f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_KBD) },
11715f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
11725f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
11735f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_LX3) },
11745f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_V150) },
11755f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
11765f22a7992349c5ca3842190be52d5e9a1dd7adf4Jiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
11778c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby
11788c19a51591d06f5226499972567f528cf6066bb7Jiri Slaby	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
117985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	{ }
118085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby};
118185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
118285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_bus_match(struct device *dev, struct device_driver *drv)
118385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
118485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
118585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
118685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
118785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hid_match_id(hdev, hdrv->id_table))
118885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return 0;
118985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
119085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* generic wants all non-blacklisted */
119185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!strncmp(hdrv->name, "generic-", 8))
119285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return !hid_match_id(hdev, hid_blacklist);
119385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
119485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 1;
119585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
119685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
119785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_device_probe(struct device *dev)
119885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
119985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = container_of(dev->driver,
120085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			struct hid_driver, driver);
120185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
120285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	const struct hid_device_id *id;
120385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret = 0;
120485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
120585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!hdev->driver) {
1206c500c9714011edab021591340042787722db9cf0Jiri Slaby		id = hid_match_id(hdev, hdrv->id_table);
1207c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (id == NULL)
1208c500c9714011edab021591340042787722db9cf0Jiri Slaby			return -ENODEV;
120985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1210c500c9714011edab021591340042787722db9cf0Jiri Slaby		hdev->driver = hdrv;
1211c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (hdrv->probe) {
1212c500c9714011edab021591340042787722db9cf0Jiri Slaby			ret = hdrv->probe(hdev, id);
1213c500c9714011edab021591340042787722db9cf0Jiri Slaby		} else { /* default probe */
1214c500c9714011edab021591340042787722db9cf0Jiri Slaby			ret = hid_parse(hdev);
1215c500c9714011edab021591340042787722db9cf0Jiri Slaby			if (!ret)
1216c500c9714011edab021591340042787722db9cf0Jiri Slaby				ret = hid_hw_start(hdev);
121785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		}
1218c500c9714011edab021591340042787722db9cf0Jiri Slaby		if (ret)
1219c500c9714011edab021591340042787722db9cf0Jiri Slaby			hdev->driver = NULL;
122085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
122185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
122285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
122385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
122485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_device_remove(struct device *dev)
122585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
122685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
122785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_driver *hdrv = hdev->driver;
122885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
122985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdrv) {
123085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		if (hdrv->remove)
123185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdrv->remove(hdev);
1232c500c9714011edab021591340042787722db9cf0Jiri Slaby		else /* default remove */
1233c500c9714011edab021591340042787722db9cf0Jiri Slaby			hid_hw_stop(hdev);
123485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->driver = NULL;
123585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
123685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
123785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
123885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
123985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
124085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
124185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
124285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
124385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
124485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
124585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdev->bus, hdev->vendor, hdev->product))
124685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
124785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
124885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
124985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
125085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
125185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
125285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
125385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
125485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
125585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
125685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
125785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
125885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdev->bus, hdev->vendor, hdev->product))
125985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -ENOMEM;
126085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
126185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
126285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
126385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
126485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic struct bus_type hid_bus_type = {
126585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.name		= "hid",
126685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.match		= hid_bus_match,
126785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.probe		= hid_device_probe,
126885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.remove		= hid_device_remove,
126985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	.uevent		= hid_uevent,
127085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby};
127185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1272d458a9dfc4de24870b8c747484b1988726534beeJiri Slabystatic const struct hid_device_id hid_ignore_list[] = {
1273d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1274d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1275d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1276d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1277d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1278d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1279d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1280d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1281d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1282d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1283d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1284d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1285d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1286d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1287d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1288d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1289d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1290d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1291d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1292d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1293d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1294d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1295d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1296d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1297d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1298d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1299d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1300d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1301d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1302d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1303d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1304d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1305d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1306d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1307d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1308d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1309d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1310d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1311d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1312d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1313d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1314d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1315d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1316d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1317d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1318d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1319d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1320d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1321d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1322d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1323d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1324d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1325d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1326d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1327d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1328d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1329d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1330d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1331d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1332d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1333d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1334d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1335d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1336d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1337d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1338d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1339d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1340d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1341d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1342d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1343d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1344d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1345d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1346d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1347d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1348d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1349d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1350d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1351d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1352d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1353d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1354d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1355d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1356d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1357d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1358d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1359d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1360d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1361d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1362d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1363d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1364d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1365d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1366d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1367d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1368d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1369d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1370d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1371d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1372d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1373d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1374d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1375d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1376d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1377d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1378d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1379d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1380d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1381d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1382d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1383d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1384d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1385d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1386d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1387d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1388d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1389d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1390d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1391d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1392d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1393d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1394d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1395d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1396d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1397d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD) },
1398d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1399d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1400d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1401d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1402d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1403d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1404d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1405d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1406d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1407d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1408d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	{ }
1409d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby};
1410d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
1411d458a9dfc4de24870b8c747484b1988726534beeJiri Slabystatic bool hid_ignore(struct hid_device *hdev)
1412d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby{
1413d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	switch (hdev->vendor) {
1414d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	case USB_VENDOR_ID_CODEMERCS:
1415d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		/* ignore all Code Mercenaries IOWarrior devices */
1416d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1417d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby				hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1418d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby			return true;
1419d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		break;
1420d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	case USB_VENDOR_ID_LOGITECH:
1421d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1422d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby				hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1423d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby			return true;
1424d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		break;
1425d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	}
1426d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
1427d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	return !!hid_match_id(hdev, hid_ignore_list);
1428d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby}
1429d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
143085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint hid_add_device(struct hid_device *hdev)
143185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
143285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	static atomic_t id = ATOMIC_INIT(0);
143385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
143485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
143585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (WARN_ON(hdev->status & HID_STAT_ADDED))
143685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return -EBUSY;
143785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
1438d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	/* we need to kill them here, otherwise they will stay allocated to
1439d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	 * wait for coming driver */
1440d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby	if (hid_ignore(hdev))
1441d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby		return -ENODEV;
1442d458a9dfc4de24870b8c747484b1988726534beeJiri Slaby
144385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	/* XXX hack, any other cleaner solution < 20 bus_id bytes? */
144485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	sprintf(hdev->dev.bus_id, "%04X:%04X:%04X.%04X", hdev->bus,
144585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			hdev->vendor, hdev->product, atomic_inc_return(&id));
144685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
144785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = device_add(&hdev->dev);
144885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (!ret)
144985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->status |= HID_STAT_ADDED;
145085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
145185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
145285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
145385cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_add_device);
145485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
145585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
145685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_allocate_device - allocate new hid device descriptor
145785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
145885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * Allocate and initialize hid device, so that hid_destroy_device might be
145985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * used to free it.
146085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
146185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
146285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * error value.
146385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
146485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystruct hid_device *hid_allocate_device(void)
146585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
146685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	struct hid_device *hdev;
146785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	unsigned int i;
146885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret = -ENOMEM;
146985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
147085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
147185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev == NULL)
147285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		return ERR_PTR(ret);
147385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
147485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	device_initialize(&hdev->dev);
147585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->dev.release = hid_device_release;
147685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->dev.bus = &hid_bus_type;
147785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
147885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
147985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby			sizeof(struct hid_collection), GFP_KERNEL);
148085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev->collection == NULL)
148185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
148285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
148385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
148485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	for (i = 0; i < HID_REPORT_TYPES; i++)
148585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
148685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
148785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return hdev;
148885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
148985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	put_device(&hdev->dev);
149085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ERR_PTR(ret);
149185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
149285cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_allocate_device);
149385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
149485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabystatic void hid_remove_device(struct hid_device *hdev)
149585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
149685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (hdev->status & HID_STAT_ADDED) {
149785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		device_del(&hdev->dev);
149885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		hdev->status &= ~HID_STAT_ADDED;
149985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
150085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
150185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
150285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby/**
150385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * hid_destroy_device - free previously allocated device
150485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
150585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * @hdev: hid device
150685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby *
150785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * If you allocate hid_device through hid_allocate_device, you should ever
150885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby * free by this function.
150985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby */
151085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_destroy_device(struct hid_device *hdev)
151185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
151285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hid_remove_device(hdev);
151385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	put_device(&hdev->dev);
151485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
151585cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_destroy_device);
151685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
151785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyint __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
151885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		const char *mod_name)
151985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
152085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.name = hdrv->name;
152185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.bus = &hid_bus_type;
152285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.owner = owner;
152385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	hdrv->driver.mod_name = mod_name;
152485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
152585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return driver_register(&hdrv->driver);
152685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
152785cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(__hid_register_driver);
152885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
152985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyvoid hid_unregister_driver(struct hid_driver *hdrv)
153085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby{
153185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	driver_unregister(&hdrv->driver);
153285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby}
153385cdaf524b7ddab627e7d15405693f2511ef7505Jiri SlabyEXPORT_SYMBOL_GPL(hid_unregister_driver);
153485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
153586166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic int __init hid_init(void)
153686166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
153785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	int ret;
153885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
153985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = bus_register(&hid_bus_type);
154085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (ret) {
154185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		printk(KERN_ERR "HID: can't register hid bus\n");
154285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err;
154385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	}
154485cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
154585cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	ret = hidraw_init();
154685cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	if (ret)
154785cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby		goto err_bus;
154885cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby
154985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return 0;
155085cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr_bus:
155185cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	bus_unregister(&hid_bus_type);
155285cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slabyerr:
155385cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	return ret;
155486166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
155586166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
155686166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic void __exit hid_exit(void)
155786166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
155886166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina	hidraw_exit();
155985cdaf524b7ddab627e7d15405693f2511ef7505Jiri Slaby	bus_unregister(&hid_bus_type);
156086166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
156186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
156286166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_init(hid_init);
156386166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_exit(hid_exit);
156486166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
1565aa938f7974b82cfd9ee955031987344f332b7c77Jiri KosinaMODULE_LICENSE(DRIVER_LICENSE);
1566aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina
1567