hid-core.c revision c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3
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
36dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
37dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Version Information
38dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
39dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
40dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#define DRIVER_VERSION "v2.6"
41f142b3a4f64eca235f5890d9cecb68482f62ed26Jiri Kosina#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
42531498013163c72cee8540e98e12c0c430c7fe87Jiri Kosina#define DRIVER_DESC "HID core driver"
43dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina#define DRIVER_LICENSE "GPL"
44dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
4558037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina#ifdef CONFIG_HID_DEBUG
4658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosinaint hid_debug = 0;
4758037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosinamodule_param_named(debug, hid_debug, bool, 0600);
4858037eb961f859607b161c50d9d4ecb374de1e8fJiri KosinaMODULE_PARM_DESC(debug, "Turn HID debugging mode on and off");
4958037eb961f859607b161c50d9d4ecb374de1e8fJiri KosinaEXPORT_SYMBOL_GPL(hid_debug);
5058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina#endif
5158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina
52dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
53dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new report for a device.
54dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
55dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
56dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
57dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
58dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report_enum *report_enum = device->report_enum + type;
59dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
60dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
61dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report_enum->report_id_hash[id])
62dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return report_enum->report_id_hash[id];
63dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
64dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
65dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
66dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
67dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (id != 0)
68dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		report_enum->numbered = 1;
69dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
70dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->id = id;
71dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->type = type;
72dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size = 0;
73dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->device = device;
74dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report_enum->report_id_hash[id] = report;
75dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
76dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	list_add_tail(&report->list, &report_enum->report_list);
77dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
78dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return report;
79dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
80dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
81dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
82dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
83dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
84dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
85dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
86dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
87dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
88dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
89dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report->maxfield == HID_MAX_FIELDS) {
9058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("too many fields in report\n");
91dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
92dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
93dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
94dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
95dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
96dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
97dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->index = report->maxfield++;
98dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->field[field->index] = field;
99dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->usage = (struct hid_usage *)(field + 1);
100dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->value = (unsigned *)(field->usage + usages);
101dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report = report;
102dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
103dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return field;
104dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
105dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
106dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
107dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Open a collection. The type/usage is pushed on the stack.
108dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
109dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
110dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int open_collection(struct hid_parser *parser, unsigned type)
111dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
112dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_collection *collection;
113dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned usage;
114dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
115dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usage = parser->local.usage[0];
116dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
117dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
11858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack overflow\n");
119dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
120dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
121dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
122dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->device->maxcollection == parser->device->collection_size) {
123dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		collection = kmalloc(sizeof(struct hid_collection) *
124dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->device->collection_size * 2, GFP_KERNEL);
125dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (collection == NULL) {
12658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("failed to reallocate collection array\n");
127dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
128dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
129dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memcpy(collection, parser->device->collection,
130dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
131dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
132dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		memset(collection + parser->device->collection_size, 0,
133dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			sizeof(struct hid_collection) *
134dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->device->collection_size);
135dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(parser->device->collection);
136dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection = collection;
137dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->collection_size *= 2;
138dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
139dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
140dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack[parser->collection_stack_ptr++] =
141dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection;
142dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
143dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection = parser->device->collection +
144dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxcollection++;
145dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->type = type;
146dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->usage = usage;
147dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	collection->level = parser->collection_stack_ptr - 1;
148dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
149dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (type == HID_COLLECTION_APPLICATION)
150dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->device->maxapplication++;
151dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
152dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
153dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
154dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
155dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
156dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Close a collection.
157dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
158dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
159dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int close_collection(struct hid_parser *parser)
160dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
161dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->collection_stack_ptr) {
16258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("collection stack underflow\n");
163dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
164dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
165dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->collection_stack_ptr--;
166dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
167dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
168dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
169dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
170dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Climb up the stack, search for the specified collection type
171dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * and return the usage.
172dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
173dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
174dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
175dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
176dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int n;
177dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
178dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (parser->device->collection[parser->collection_stack[n]].type == type)
179dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return parser->device->collection[parser->collection_stack[n]].usage;
180dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0; /* we know nothing about this usage type */
181dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
182dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
183dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
184dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Add a usage to the temporary parser table.
185dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
186dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
187dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_usage(struct hid_parser *parser, unsigned usage)
188dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
189dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->local.usage_index >= HID_MAX_USAGES) {
19058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("usage index exceeded\n");
191dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
192dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
193dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage[parser->local.usage_index] = usage;
194dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.collection_index[parser->local.usage_index] =
195dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack_ptr ?
196dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
197dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->local.usage_index++;
198dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
199dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
200dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
201dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
202dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Register a new field for this report.
203dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
204dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
205dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
206dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
207dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_report *report;
208dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_field *field;
209dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int usages;
210dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset;
211dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int i;
212dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
213dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
21458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("hid_register_report failed\n");
215dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
216dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
217dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
218dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (parser->global.logical_maximum < parser->global.logical_minimum) {
21958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
220dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
221dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
222dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
223dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	offset = report->size;
224dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report->size += parser->global.report_size * parser->global.report_count;
225dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
226dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!parser->local.usage_index) /* Ignore padding fields */
227dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
228dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
229dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	usages = max_t(int, parser->local.usage_index, parser->global.report_count);
230dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
231dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
232dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return 0;
233dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
234dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
235dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
236dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
237dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
238dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < usages; i++) {
239dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		int j = i;
240dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		/* Duplicate the last usage we parsed if we have excess values */
241dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (i >= parser->local.usage_index)
242dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			j = parser->local.usage_index - 1;
243dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].hid = parser->local.usage[j];
244dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		field->usage[i].collection_index =
245dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->local.collection_index[j];
246dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
247dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
248dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->maxusage = usages;
249dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->flags = flags;
250dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_offset = offset;
251dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_type = report_type;
252dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_size = parser->global.report_size;
253dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->report_count = parser->global.report_count;
254dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_minimum = parser->global.logical_minimum;
255dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->logical_maximum = parser->global.logical_maximum;
256dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_minimum = parser->global.physical_minimum;
257dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->physical_maximum = parser->global.physical_maximum;
258dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit_exponent = parser->global.unit_exponent;
259dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->unit = parser->global.unit;
260dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
261dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
262dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
263dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
264dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
265dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Read data value from item.
266dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
267dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
268dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u32 item_udata(struct hid_item *item)
269dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
270dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
271dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 1: return item->data.u8;
272dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 2: return item->data.u16;
273dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 4: return item->data.u32;
274dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
275dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
276dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
277dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
278dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic s32 item_sdata(struct hid_item *item)
279dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
280dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
281dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 1: return item->data.s8;
282dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 2: return item->data.s16;
283dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 4: return item->data.s32;
284dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
285dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
286dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
287dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
288dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
289dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a global item.
290dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
291dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
292dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
293dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
294dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
295dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
296dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_PUSH:
297dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
298dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
29958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("global enviroment stack overflow\n");
300dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
301dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
302dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
303dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			memcpy(parser->global_stack + parser->global_stack_ptr++,
304dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				&parser->global, sizeof(struct hid_global));
305dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
306dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
307dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_POP:
308dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
309dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (!parser->global_stack_ptr) {
31058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("global enviroment stack underflow\n");
311dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
312dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
313dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
314dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
315dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				sizeof(struct hid_global));
316dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
317dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
318dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
319dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.usage_page = item_udata(item);
320dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
321dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
322dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
323dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.logical_minimum = item_sdata(item);
324dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
325dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
326dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
327dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->global.logical_minimum < 0)
328dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.logical_maximum = item_sdata(item);
329dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			else
330dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.logical_maximum = item_udata(item);
331dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
332dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
333dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
334dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.physical_minimum = item_sdata(item);
335dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
336dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
337dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
338dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->global.physical_minimum < 0)
339dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.physical_maximum = item_sdata(item);
340dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			else
341dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->global.physical_maximum = item_udata(item);
342dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
343dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
344dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
345dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.unit_exponent = item_sdata(item);
346dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
347dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
348dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_UNIT:
349dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->global.unit = item_udata(item);
350dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
351dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
352dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
353dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((parser->global.report_size = item_udata(item)) > 32) {
35458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("invalid report_size %d\n", parser->global.report_size);
355dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
356dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
357dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
358dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
359dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
360dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
36158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("invalid report_count %d\n", parser->global.report_count);
362dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
363dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
364dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
365dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
366dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_GLOBAL_ITEM_TAG_REPORT_ID:
367dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((parser->global.report_id = item_udata(item)) == 0) {
36858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("report_id 0 is invalid\n");
369dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return -1;
370dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
371dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
372dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
373dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		default:
37458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unknown global tag 0x%x\n", item->tag);
375dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
376dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
377dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
378dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
379dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
380dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a local item.
381dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
382dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
383dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
384dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
385dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
386dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
387dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
388dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->size == 0) {
38958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("item data expected for local item\n");
390dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
391dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
392dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
393dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
394dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
395dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
396dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
397dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_DELIMITER:
398dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
399dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (data) {
400dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				/*
401dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * We treat items before the first delimiter
402dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * as global to all usage sets (branch 0).
403dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * In the moment we process only these global
404dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 * items and the first delimiter set.
405dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				 */
406dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				if (parser->local.delimiter_depth != 0) {
40758037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina					dbg_hid("nested delimiters\n");
408dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina					return -1;
409dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				}
410dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->local.delimiter_depth++;
411dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->local.delimiter_branch++;
412dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			} else {
413dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				if (parser->local.delimiter_depth < 1) {
41458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina					dbg_hid("bogus close delimiter\n");
415dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina					return -1;
416dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				}
417dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				parser->local.delimiter_depth--;
418dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
419dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 1;
420dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
421dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_USAGE:
422dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
423dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_branch > 1) {
42458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("alternative usage ignored\n");
425dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return 0;
426dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
427dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
428dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (item->size <= 2)
429dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				data = (parser->global.usage_page << 16) + data;
430dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
431dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return hid_add_usage(parser, data);
432dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
433dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
434dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
435dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_branch > 1) {
43658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("alternative usage ignored\n");
437dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return 0;
438dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
439dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
440dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (item->size <= 2)
441dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				data = (parser->global.usage_page << 16) + data;
442dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
443dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			parser->local.usage_minimum = data;
444dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
445dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
446dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
447dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
448dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_branch > 1) {
44958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("alternative usage ignored\n");
450dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return 0;
451dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
452dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
453dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (item->size <= 2)
454dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				data = (parser->global.usage_page << 16) + data;
455dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
456dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			for (n = parser->local.usage_minimum; n <= data; n++)
457dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				if (hid_add_usage(parser, n)) {
45858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina					dbg_hid("hid_add_usage failed\n");
459dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina					return -1;
460dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				}
461dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
462dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
463dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		default:
464dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
46558037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unknown local item tag 0x%x\n", item->tag);
466dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
467dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
468dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
469dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
470dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
471dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
472dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a main item.
473dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
474dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
475dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
476dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
477dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u32 data;
478dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	int ret;
479dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
480dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	data = item_udata(item);
481dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
482dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->tag) {
483dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
484dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = open_collection(parser, data & 0xff);
485dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
486dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_END_COLLECTION:
487dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = close_collection(parser);
488dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
489dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_INPUT:
490dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = hid_add_field(parser, HID_INPUT_REPORT, data);
491dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
492dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_OUTPUT:
493dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
494dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
495dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case HID_MAIN_ITEM_TAG_FEATURE:
496dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
497dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			break;
498dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		default:
49958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unknown main item tag 0x%x\n", item->tag);
500dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			ret = 0;
501dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
502dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
503dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
504dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
505dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return ret;
506dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
507dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
508dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
509dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Process a reserved item.
510dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
511dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
512dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
513dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
51458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
515dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
516dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
517dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
518dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
519dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a report and all registered fields. The field->usage and
520dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * field->value table's are allocated behind the field, so we need
521dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * only to free(field) itself.
522dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
523dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
524dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_free_report(struct hid_report *report)
525dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
526dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
527dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
528dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < report->maxfield; n++)
529dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(report->field[n]);
530dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(report);
531dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
532dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
533dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
534dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Free a device structure, all reports, and all fields.
535dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
536dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
537229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosinavoid hid_free_device(struct hid_device *device)
538dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
539dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned i,j;
540dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
541dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < HID_REPORT_TYPES; i++) {
542dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		struct hid_report_enum *report_enum = device->report_enum + i;
543dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
544dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		for (j = 0; j < 256; j++) {
545dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			struct hid_report *report = report_enum->report_id_hash[j];
546dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (report)
547dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_free_report(report);
548dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
549dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
550dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
551dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device->rdesc);
552767fe7877c2928b5633992ee60a49ad4516dc2afJiri Kosina	kfree(device->collection);
553dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(device);
554dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
555229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_free_device);
556dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
557dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
558dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Fetch a report description item from the data stream. We support long
559dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * items, though they are not used yet.
560dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
561dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
562dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
563dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
564dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u8 b;
565dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
566dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if ((end - start) <= 0)
567dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
568dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
569dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	b = *start++;
570dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
571dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->type = (b >> 2) & 3;
572dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->tag  = (b >> 4) & 15;
573dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
574dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (item->tag == HID_ITEM_TAG_LONG) {
575dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
576dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->format = HID_ITEM_FORMAT_LONG;
577dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
578dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < 2)
579dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
580dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
581dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->size = *start++;
582dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->tag  = *start++;
583dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
584dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if ((end - start) < item->size)
585dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
586dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
587dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		item->data.longdata = start;
588dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		start += item->size;
589dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return start;
590dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
591dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
592dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->format = HID_ITEM_FORMAT_SHORT;
593dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	item->size = b & 3;
594dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
595dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (item->size) {
596dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
597dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 0:
598dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return start;
599dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
600dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 1:
601dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((end - start) < 1)
602dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
603dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			item->data.u8 = *start++;
604dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return start;
605dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
606dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 2:
607dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((end - start) < 2)
608dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
609dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
610dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			start = (__u8 *)((__le16 *)start + 1);
611dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return start;
612dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
613dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 3:
614dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			item->size++;
615dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if ((end - start) < 4)
616dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
617dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
618dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			start = (__u8 *)((__le32 *)start + 1);
619dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return start;
620dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
621dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
622dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return NULL;
623dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
624dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
625dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
626dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Parse a report description into a hid_device structure. Reports are
627dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * enumerated, fields are attached to these reports.
628dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
629dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
630229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosinastruct hid_device *hid_parse_report(__u8 *start, unsigned size)
631dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
632dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_device *device;
633dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_parser *parser;
634dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	struct hid_item item;
635dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__u8 *end;
636dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned i;
637dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	static int (*dispatch_type[])(struct hid_parser *parser,
638dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				      struct hid_item *item) = {
639dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_main,
640dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_global,
641dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_local,
642dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_parser_reserved
643dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	};
644dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
645dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
646dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
647dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
648dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
649dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				   HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
650dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(device);
651dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
652dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
653dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
654dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
655dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (i = 0; i < HID_REPORT_TYPES; i++)
656dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		INIT_LIST_HEAD(&device->report_enum[i].report_list);
657dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
658d6509c36ba333282362e5f9769869f8a8d790aafAhmed S. Darwish	if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
659dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(device->collection);
660dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(device);
661dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
662dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
663dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memcpy(device->rdesc, start, size);
664dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	device->rsize = size;
665dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
66647a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina	if (!(parser = vmalloc(sizeof(struct hid_parser)))) {
667dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(device->rdesc);
668dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(device->collection);
669dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		kfree(device);
670dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return NULL;
671dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
67247a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina	memset(parser, 0, sizeof(struct hid_parser));
673dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	parser->device = device;
674dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
675dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	end = start + size;
676dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	while ((start = fetch_item(start, end, &item)) != NULL) {
677dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
678dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (item.format != HID_ITEM_FORMAT_SHORT) {
67958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("unexpected long global item\n");
680dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			hid_free_device(device);
68147a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina			vfree(parser);
682dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
683dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
684dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
685dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (dispatch_type[item.type](parser, &item)) {
68658037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("item %u %u %u %u parsing failed\n",
687dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
688dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			hid_free_device(device);
68947a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina			vfree(parser);
690dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return NULL;
691dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
692dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
693dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (start == end) {
694dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->collection_stack_ptr) {
69558037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("unbalanced collection at end of report description\n");
696dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_free_device(device);
69747a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina				vfree(parser);
698dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
699dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
700dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (parser->local.delimiter_depth) {
70158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina				dbg_hid("unbalanced delimiter at end of report description\n");
702dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_free_device(device);
70347a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina				vfree(parser);
704dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				return NULL;
705dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			}
70647a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina			vfree(parser);
707dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return device;
708dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
709dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
710dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
71158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
712dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	hid_free_device(device);
71347a80edb1d80ae644ea374a016c8e68d43d2671fJiri Kosina	vfree(parser);
714dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return NULL;
715dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
716229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_parse_report);
717dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
718dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
719dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Convert a signed n-bit integer to signed 32-bit integer. Common
720dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * cases are done through the compiler, the screwed things has to be
721dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * done by hand.
722dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
723dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
724dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic s32 snto32(__u32 value, unsigned n)
725dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
726dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	switch (n) {
727dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 8:  return ((__s8)value);
728dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 16: return ((__s16)value);
729dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		case 32: return ((__s32)value);
730dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
731dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
732dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
733dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
734dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
735dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Convert a signed 32-bit integer to a signed n-bit integer.
736dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
737dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
738dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic u32 s32ton(__s32 value, unsigned n)
739dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
740dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	s32 a = value >> (n - 1);
741dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (a && a != -1)
742dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
743dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return value & ((1 << n) - 1);
744dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
745dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
746dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
747dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Extract/implement a data field from/to a little endian report (bit array).
748dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
749dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Code sort-of follows HID spec:
750dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
751dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina *
752dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * While the USB HID spec allows unlimited length bit fields in "report
753dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * descriptors", most devices never use more than 16 bits.
754dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * One model of UPS is claimed to report "LINEV" as a 32-bit field.
755dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
756dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
757dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
758dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
759dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
760dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u64 x;
761dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
762c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (n > 32)
763c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
764c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				n, current->comm);
765dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
766dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report += offset >> 3;  /* adjust byte index */
767229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina	offset &= 7;            /* now only need bit offset into one byte */
768b87496aa493a540b9c084b16b73f915705ebdbccAl Viro	x = le64_to_cpu(get_unaligned((__le64 *) report));
769229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosina	x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
770dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return (u32) x;
771dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
772dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
773dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
774dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * "implement" : set bits in a little endian bit stream.
775dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Same concepts as "extract" (see comments above).
776dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * The data mangled in the bit stream remains in little endian
777dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * order the whole time. It make more sense to talk about
778dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * endianness of register values by considering a register
779dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * a "cached" copy of the little endiad bit stream.
780dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
781dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
782dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
783b87496aa493a540b9c084b16b73f915705ebdbccAl Viro	__le64 x;
784dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	u64 m = (1ULL << n) - 1;
785dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
786c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (n > 32)
787c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
788c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				n, current->comm);
789dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
790c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina	if (value > m)
791c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina		printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
792c4124c9b68bf8ecf1e8afedad3a22e18666cb6f3Jiri Kosina				value, current->comm);
793dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	WARN_ON(value > m);
794dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	value &= m;
795dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
796dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	report += offset >> 3;
797dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	offset &= 7;
798dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
799b87496aa493a540b9c084b16b73f915705ebdbccAl Viro	x = get_unaligned((__le64 *)report);
800dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	x &= cpu_to_le64(~(m << offset));
801dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	x |= cpu_to_le64(((u64) value) << offset);
802b87496aa493a540b9c084b16b73f915705ebdbccAl Viro	put_unaligned(x, (__le64 *) report);
803dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
804dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
805dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
806dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Search an array for a value.
807dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
808dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
809dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic __inline__ int search(__s32 *array, __s32 value, unsigned n)
810dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
811dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	while (n--) {
812dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (*array++ == value)
813dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return 0;
814dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
815dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return -1;
816dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
817dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
818dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
819dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
820dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	hid_dump_input(usage, value);
821dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (hid->claimed & HID_CLAIMED_INPUT)
822dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hidinput_hid_event(hid, field, usage, value);
823aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
824aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina		hid->hiddev_hid_event(hid, field, usage, value);
825dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
826dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
827dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
828dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Analyse a received field, and fetch the data from it. The field
829dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * content is stored for next report processing (we do differential
830dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * reporting to the layer).
831dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
832dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
833229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosinavoid hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
834dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
835dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
836dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned count = field->report_count;
837dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset = field->report_offset;
838dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
839dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 min = field->logical_minimum;
840dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 max = field->logical_maximum;
841dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	__s32 *value;
842dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
843dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
844dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return;
845dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
846dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
847dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
848dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
849dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina						    extract(data, offset + n * size, size);
850dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
851dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
852dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			    && value[n] >= min && value[n] <= max
853dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			    && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
854dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				goto exit;
855dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
856dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
857dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
858dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
859dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
860dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
861dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			continue;
862dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
863dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
864dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (field->value[n] >= min && field->value[n] <= max
865dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& field->usage[field->value[n] - min].hid
866dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& search(value, field->value[n], count))
867dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
868dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
869dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (value[n] >= min && value[n] <= max
870dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& field->usage[value[n] - min].hid
871dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			&& search(field->value, value[n], count))
872dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
873dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
874dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
875dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	memcpy(field->value, value, count * sizeof(__s32));
876dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinaexit:
877dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	kfree(value);
878dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
879229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_input_field);
880dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
881dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
882dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Output the field into the report.
883dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
884dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
885dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinastatic void hid_output_field(struct hid_field *field, __u8 *data)
886dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
887dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned count = field->report_count;
888dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned offset = field->report_offset;
889dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
89046386b587086c8d2698222a031bf749688464032Simon Budig	unsigned bitsused = offset + count * size;
891dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
892dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
89346386b587086c8d2698222a031bf749688464032Simon Budig	/* make sure the unused bits in the last byte are zeros */
89446386b587086c8d2698222a031bf749688464032Simon Budig	if (count > 0 && size > 0 && (bitsused % 8) != 0)
89546386b587086c8d2698222a031bf749688464032Simon Budig		data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
89646386b587086c8d2698222a031bf749688464032Simon Budig
897dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < count; n++) {
898dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (field->logical_minimum < 0)	/* signed values */
899dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			implement(data, offset + n * size, size, s32ton(field->value[n], size));
900dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		else				/* unsigned values */
901dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			implement(data, offset + n * size, size, field->value[n]);
902dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
903dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
904dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
905dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
906dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Create a report.
907dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
908dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
909229695e51efc4ed5e04ab471c82591d0f432909dJiri Kosinavoid hid_output_report(struct hid_report *report, __u8 *data)
910dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
911dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned n;
912dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
913dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (report->id > 0)
914dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		*data++ = report->id;
915dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
916dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	for (n = 0; n < report->maxfield; n++)
917dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_output_field(report->field[n], data);
918dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
919229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_output_report);
920dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
921dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina/*
922dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * Set a field value. The report this field belongs to has to be
923dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * created and transferred to the device, to set this value in the
924dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina * device.
925dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina */
926dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
927dde5845a529ff753364a6d1aea61180946270bfaJiri Kosinaint hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
928dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina{
929dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	unsigned size = field->report_size;
930dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
931dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	hid_dump_input(field->usage + offset, value);
932dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
933dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (offset >= field->report_count) {
93458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
935dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		hid_dump_field(field, 8);
936dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		return -1;
937dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
938dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	if (field->logical_minimum < 0) {
939dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		if (value != snto32(s32ton(value, size), size)) {
94058037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina			dbg_hid("value %d is out of range\n", value);
941dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina			return -1;
942dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina		}
943dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	}
944dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	field->value[offset] = value;
945dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina	return 0;
946dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina}
947229695e51efc4ed5e04ab471c82591d0f432909dJiri KosinaEXPORT_SYMBOL_GPL(hid_set_field);
948dde5845a529ff753364a6d1aea61180946270bfaJiri Kosina
949aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosinaint hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
950aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina{
951aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	struct hid_report_enum *report_enum = hid->report_enum + type;
952aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	struct hid_report *report;
95358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	int n, rsize, i;
954aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
955aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (!hid)
956aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		return -ENODEV;
957aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
958aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (!size) {
95958037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("empty report\n");
960aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		return -1;
961aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
962aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
96358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
964aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
965aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	n = 0;                          /* Normally report number is 0 */
966aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
967aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		n = *data++;
968aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		size--;
969aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
970aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
97158037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	/* dump the report descriptor */
97258037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid("report %d (size %u) = ", n, size);
97358037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	for (i = 0; i < size; i++)
97458037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid_line(" %02x", data[i]);
97558037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina	dbg_hid_line("\n");
976aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
977aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (!(report = report_enum->report_id_hash[n])) {
97858037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("undefined report_id %d received\n", n);
979aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		return -1;
980aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
981aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
982aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	rsize = ((report->size - 1) >> 3) + 1;
983aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
984aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (size < rsize) {
98558037eb961f859607b161c50d9d4ecb374de1e8fJiri Kosina		dbg_hid("report %d is too short, (%d < %d)\n", report->id, size, rsize);
9868da7d1bae512aee155ef02f7ab1266358842e1fdAdam Kropelin		memset(data + size, 0, rsize - size);
987aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	}
988aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
989aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
990aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hid->hiddev_report_event(hid, report);
99186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina	if (hid->claimed & HID_CLAIMED_HIDRAW)
99286166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina		hidraw_report_event(hid, data, size);
993aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
994aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	for (n = 0; n < report->maxfield; n++)
995aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hid_input_field(hid, report->field[n], data, interrupt);
996aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
997aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	if (hid->claimed & HID_CLAIMED_INPUT)
998aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina		hidinput_report_event(hid, report);
999aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
1000aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina	return 0;
1001aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina}
1002aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri KosinaEXPORT_SYMBOL_GPL(hid_input_report);
1003aa8de2f038baec993f07ef66fb3e94481d1ec22bJiri Kosina
100486166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic int __init hid_init(void)
100586166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
100686166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina	return hidraw_init();
100786166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
100886166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
100986166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinastatic void __exit hid_exit(void)
101086166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina{
101186166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina	hidraw_exit();
101286166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina}
101386166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
101486166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_init(hid_init);
101586166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosinamodule_exit(hid_exit);
101686166b7bcda0bcb53525114fa1c87ac432be478eJiri Kosina
1017aa938f7974b82cfd9ee955031987344f332b7c77Jiri KosinaMODULE_LICENSE(DRIVER_LICENSE);
1018aa938f7974b82cfd9ee955031987344f332b7c77Jiri Kosina
1019