12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2dde5845aSJiri Kosina /*
3229695e5SJiri Kosina * HID support for Linux
4dde5845aSJiri Kosina *
5dde5845aSJiri Kosina * Copyright (c) 1999 Andreas Gal
6dde5845aSJiri Kosina * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7dde5845aSJiri Kosina * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
86b1968d5SJiri Kosina * Copyright (c) 2006-2012 Jiri Kosina
9dde5845aSJiri Kosina */
10dde5845aSJiri Kosina
11dde5845aSJiri Kosina /*
12dde5845aSJiri Kosina */
13dde5845aSJiri Kosina
144291ee30SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
154291ee30SJoe Perches
16dde5845aSJiri Kosina #include <linux/module.h>
17dde5845aSJiri Kosina #include <linux/slab.h>
18dde5845aSJiri Kosina #include <linux/init.h>
19dde5845aSJiri Kosina #include <linux/kernel.h>
20dde5845aSJiri Kosina #include <linux/list.h>
21dde5845aSJiri Kosina #include <linux/mm.h>
22dde5845aSJiri Kosina #include <linux/spinlock.h>
23dde5845aSJiri Kosina #include <asm/unaligned.h>
24dde5845aSJiri Kosina #include <asm/byteorder.h>
25dde5845aSJiri Kosina #include <linux/input.h>
26dde5845aSJiri Kosina #include <linux/wait.h>
2747a80edbSJiri Kosina #include <linux/vmalloc.h>
28c4124c9bSJiri Kosina #include <linux/sched.h>
294ea54542SDavid Herrmann #include <linux/semaphore.h>
30dde5845aSJiri Kosina
31dde5845aSJiri Kosina #include <linux/hid.h>
32dde5845aSJiri Kosina #include <linux/hiddev.h>
33c080d89aSJiri Kosina #include <linux/hid-debug.h>
3486166b7bSJiri Kosina #include <linux/hidraw.h>
35dde5845aSJiri Kosina
365f22a799SJiri Slaby #include "hid-ids.h"
375f22a799SJiri Slaby
38dde5845aSJiri Kosina /*
39dde5845aSJiri Kosina * Version Information
40dde5845aSJiri Kosina */
41dde5845aSJiri Kosina
4253149801SJiri Kosina #define DRIVER_DESC "HID core driver"
43dde5845aSJiri Kosina
446b1968d5SJiri Kosina static int hid_ignore_special_drivers = 0;
456b1968d5SJiri Kosina module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
46643727a9SHans Petter Selasky MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
476b1968d5SJiri Kosina
48dde5845aSJiri Kosina /*
49dde5845aSJiri Kosina * Register a new report for a device.
50dde5845aSJiri Kosina */
51dde5845aSJiri Kosina
hid_register_report(struct hid_device * device,enum hid_report_type type,unsigned int id,unsigned int application)52f07b3c1dSBenjamin Tissoires struct hid_report *hid_register_report(struct hid_device *device,
53ead77b65SBenjamin Tissoires enum hid_report_type type, unsigned int id,
54f07b3c1dSBenjamin Tissoires unsigned int application)
55dde5845aSJiri Kosina {
56dde5845aSJiri Kosina struct hid_report_enum *report_enum = device->report_enum + type;
57dde5845aSJiri Kosina struct hid_report *report;
58dde5845aSJiri Kosina
5943622021SKees Cook if (id >= HID_MAX_IDS)
6043622021SKees Cook return NULL;
61dde5845aSJiri Kosina if (report_enum->report_id_hash[id])
62dde5845aSJiri Kosina return report_enum->report_id_hash[id];
63dde5845aSJiri Kosina
64a3789a17SJoe Perches report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
65a3789a17SJoe Perches if (!report)
66dde5845aSJiri Kosina return NULL;
67dde5845aSJiri Kosina
68dde5845aSJiri Kosina if (id != 0)
69dde5845aSJiri Kosina report_enum->numbered = 1;
70dde5845aSJiri Kosina
71dde5845aSJiri Kosina report->id = id;
72dde5845aSJiri Kosina report->type = type;
73dde5845aSJiri Kosina report->size = 0;
74dde5845aSJiri Kosina report->device = device;
75f07b3c1dSBenjamin Tissoires report->application = application;
76dde5845aSJiri Kosina report_enum->report_id_hash[id] = report;
77dde5845aSJiri Kosina
78dde5845aSJiri Kosina list_add_tail(&report->list, &report_enum->report_list);
7922f4b026SBenjamin Tissoires INIT_LIST_HEAD(&report->field_entry_list);
80dde5845aSJiri Kosina
81dde5845aSJiri Kosina return report;
82dde5845aSJiri Kosina }
8390a006abSMichael Poole EXPORT_SYMBOL_GPL(hid_register_report);
84dde5845aSJiri Kosina
85dde5845aSJiri Kosina /*
86dde5845aSJiri Kosina * Register a new field for this report.
87dde5845aSJiri Kosina */
88dde5845aSJiri Kosina
hid_register_field(struct hid_report * report,unsigned usages)89ed9be64eSWill McVicker static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages)
90dde5845aSJiri Kosina {
91dde5845aSJiri Kosina struct hid_field *field;
92dde5845aSJiri Kosina
93dde5845aSJiri Kosina if (report->maxfield == HID_MAX_FIELDS) {
948c3d52fcSJiri Kosina hid_err(report->device, "too many fields in report\n");
95dde5845aSJiri Kosina return NULL;
96dde5845aSJiri Kosina }
97dde5845aSJiri Kosina
98a3789a17SJoe Perches field = kzalloc((sizeof(struct hid_field) +
99a3789a17SJoe Perches usages * sizeof(struct hid_usage) +
10022f4b026SBenjamin Tissoires 3 * usages * sizeof(unsigned int)), GFP_KERNEL);
101a3789a17SJoe Perches if (!field)
102a3789a17SJoe Perches return NULL;
103dde5845aSJiri Kosina
104dde5845aSJiri Kosina field->index = report->maxfield++;
105dde5845aSJiri Kosina report->field[field->index] = field;
106dde5845aSJiri Kosina field->usage = (struct hid_usage *)(field + 1);
107282bfd4cSJiri Slaby field->value = (s32 *)(field->usage + usages);
1081c1813a7SBenjamin Tissoires field->new_value = (s32 *)(field->value + usages);
10922f4b026SBenjamin Tissoires field->usages_priorities = (s32 *)(field->new_value + usages);
110dde5845aSJiri Kosina field->report = report;
111dde5845aSJiri Kosina
112dde5845aSJiri Kosina return field;
113dde5845aSJiri Kosina }
114dde5845aSJiri Kosina
115dde5845aSJiri Kosina /*
116dde5845aSJiri Kosina * Open a collection. The type/usage is pushed on the stack.
117dde5845aSJiri Kosina */
118dde5845aSJiri Kosina
open_collection(struct hid_parser * parser,unsigned type)119dde5845aSJiri Kosina static int open_collection(struct hid_parser *parser, unsigned type)
120dde5845aSJiri Kosina {
121dde5845aSJiri Kosina struct hid_collection *collection;
122dde5845aSJiri Kosina unsigned usage;
123ee46967fSPeter Hutterer int collection_index;
124dde5845aSJiri Kosina
125dde5845aSJiri Kosina usage = parser->local.usage[0];
126dde5845aSJiri Kosina
12708a8a7cfSBenjamin Tissoires if (parser->collection_stack_ptr == parser->collection_stack_size) {
12808a8a7cfSBenjamin Tissoires unsigned int *collection_stack;
12908a8a7cfSBenjamin Tissoires unsigned int new_size = parser->collection_stack_size +
13008a8a7cfSBenjamin Tissoires HID_COLLECTION_STACK_SIZE;
13108a8a7cfSBenjamin Tissoires
13208a8a7cfSBenjamin Tissoires collection_stack = krealloc(parser->collection_stack,
13308a8a7cfSBenjamin Tissoires new_size * sizeof(unsigned int),
13408a8a7cfSBenjamin Tissoires GFP_KERNEL);
13508a8a7cfSBenjamin Tissoires if (!collection_stack)
13608a8a7cfSBenjamin Tissoires return -ENOMEM;
13708a8a7cfSBenjamin Tissoires
13808a8a7cfSBenjamin Tissoires parser->collection_stack = collection_stack;
13908a8a7cfSBenjamin Tissoires parser->collection_stack_size = new_size;
140dde5845aSJiri Kosina }
141dde5845aSJiri Kosina
142dde5845aSJiri Kosina if (parser->device->maxcollection == parser->device->collection_size) {
1436da2ec56SKees Cook collection = kmalloc(
1446da2ec56SKees Cook array3_size(sizeof(struct hid_collection),
1456da2ec56SKees Cook parser->device->collection_size,
1466da2ec56SKees Cook 2),
1476da2ec56SKees Cook GFP_KERNEL);
148dde5845aSJiri Kosina if (collection == NULL) {
1498c3d52fcSJiri Kosina hid_err(parser->device, "failed to reallocate collection array\n");
150a6fbaacfSSachin Kamat return -ENOMEM;
151dde5845aSJiri Kosina }
152dde5845aSJiri Kosina memcpy(collection, parser->device->collection,
153dde5845aSJiri Kosina sizeof(struct hid_collection) *
154dde5845aSJiri Kosina parser->device->collection_size);
155dde5845aSJiri Kosina memset(collection + parser->device->collection_size, 0,
156dde5845aSJiri Kosina sizeof(struct hid_collection) *
157dde5845aSJiri Kosina parser->device->collection_size);
158dde5845aSJiri Kosina kfree(parser->device->collection);
159dde5845aSJiri Kosina parser->device->collection = collection;
160dde5845aSJiri Kosina parser->device->collection_size *= 2;
161dde5845aSJiri Kosina }
162dde5845aSJiri Kosina
163dde5845aSJiri Kosina parser->collection_stack[parser->collection_stack_ptr++] =
164dde5845aSJiri Kosina parser->device->maxcollection;
165dde5845aSJiri Kosina
166ee46967fSPeter Hutterer collection_index = parser->device->maxcollection++;
167ee46967fSPeter Hutterer collection = parser->device->collection + collection_index;
168dde5845aSJiri Kosina collection->type = type;
169dde5845aSJiri Kosina collection->usage = usage;
170dde5845aSJiri Kosina collection->level = parser->collection_stack_ptr - 1;
1711950f462SPhilipp Zabel collection->parent_idx = (collection->level == 0) ? -1 :
1721950f462SPhilipp Zabel parser->collection_stack[collection->level - 1];
173dde5845aSJiri Kosina
174dde5845aSJiri Kosina if (type == HID_COLLECTION_APPLICATION)
175dde5845aSJiri Kosina parser->device->maxapplication++;
176dde5845aSJiri Kosina
177dde5845aSJiri Kosina return 0;
178dde5845aSJiri Kosina }
179dde5845aSJiri Kosina
180dde5845aSJiri Kosina /*
181dde5845aSJiri Kosina * Close a collection.
182dde5845aSJiri Kosina */
183dde5845aSJiri Kosina
close_collection(struct hid_parser * parser)184dde5845aSJiri Kosina static int close_collection(struct hid_parser *parser)
185dde5845aSJiri Kosina {
186dde5845aSJiri Kosina if (!parser->collection_stack_ptr) {
1878c3d52fcSJiri Kosina hid_err(parser->device, "collection stack underflow\n");
188a6fbaacfSSachin Kamat return -EINVAL;
189dde5845aSJiri Kosina }
190dde5845aSJiri Kosina parser->collection_stack_ptr--;
191dde5845aSJiri Kosina return 0;
192dde5845aSJiri Kosina }
193dde5845aSJiri Kosina
194dde5845aSJiri Kosina /*
195dde5845aSJiri Kosina * Climb up the stack, search for the specified collection type
196dde5845aSJiri Kosina * and return the usage.
197dde5845aSJiri Kosina */
198dde5845aSJiri Kosina
hid_lookup_collection(struct hid_parser * parser,unsigned type)199dde5845aSJiri Kosina static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
200dde5845aSJiri Kosina {
201504499f2SJoe Perches struct hid_collection *collection = parser->device->collection;
202dde5845aSJiri Kosina int n;
203504499f2SJoe Perches
204504499f2SJoe Perches for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
205504499f2SJoe Perches unsigned index = parser->collection_stack[n];
206504499f2SJoe Perches if (collection[index].type == type)
207504499f2SJoe Perches return collection[index].usage;
208504499f2SJoe Perches }
209dde5845aSJiri Kosina return 0; /* we know nothing about this usage type */
210dde5845aSJiri Kosina }
211dde5845aSJiri Kosina
212dde5845aSJiri Kosina /*
2131cb0d2aeSCandle Sun * Concatenate usage which defines 16 bits or less with the
2141cb0d2aeSCandle Sun * currently defined usage page to form a 32 bit usage
2151cb0d2aeSCandle Sun */
2161cb0d2aeSCandle Sun
complete_usage(struct hid_parser * parser,unsigned int index)2171cb0d2aeSCandle Sun static void complete_usage(struct hid_parser *parser, unsigned int index)
2181cb0d2aeSCandle Sun {
2191cb0d2aeSCandle Sun parser->local.usage[index] &= 0xFFFF;
2201cb0d2aeSCandle Sun parser->local.usage[index] |=
2211cb0d2aeSCandle Sun (parser->global.usage_page & 0xFFFF) << 16;
2221cb0d2aeSCandle Sun }
2231cb0d2aeSCandle Sun
2241cb0d2aeSCandle Sun /*
225dde5845aSJiri Kosina * Add a usage to the temporary parser table.
226dde5845aSJiri Kosina */
227dde5845aSJiri Kosina
hid_add_usage(struct hid_parser * parser,unsigned usage,u8 size)22858e75155SNicolas Saenz Julienne static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
229dde5845aSJiri Kosina {
230dde5845aSJiri Kosina if (parser->local.usage_index >= HID_MAX_USAGES) {
2318c3d52fcSJiri Kosina hid_err(parser->device, "usage index exceeded\n");
232dde5845aSJiri Kosina return -1;
233dde5845aSJiri Kosina }
234dde5845aSJiri Kosina parser->local.usage[parser->local.usage_index] = usage;
2351cb0d2aeSCandle Sun
2361cb0d2aeSCandle Sun /*
2371cb0d2aeSCandle Sun * If Usage item only includes usage id, concatenate it with
2381cb0d2aeSCandle Sun * currently defined usage page
2391cb0d2aeSCandle Sun */
2401cb0d2aeSCandle Sun if (size <= 2)
2411cb0d2aeSCandle Sun complete_usage(parser, parser->local.usage_index);
2421cb0d2aeSCandle Sun
24358e75155SNicolas Saenz Julienne parser->local.usage_size[parser->local.usage_index] = size;
244dde5845aSJiri Kosina parser->local.collection_index[parser->local.usage_index] =
245dde5845aSJiri Kosina parser->collection_stack_ptr ?
246dde5845aSJiri Kosina parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
247dde5845aSJiri Kosina parser->local.usage_index++;
248dde5845aSJiri Kosina return 0;
249dde5845aSJiri Kosina }
250dde5845aSJiri Kosina
251dde5845aSJiri Kosina /*
252dde5845aSJiri Kosina * Register a new field for this report.
253dde5845aSJiri Kosina */
254dde5845aSJiri Kosina
hid_add_field(struct hid_parser * parser,unsigned report_type,unsigned flags)255dde5845aSJiri Kosina static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
256dde5845aSJiri Kosina {
257dde5845aSJiri Kosina struct hid_report *report;
258dde5845aSJiri Kosina struct hid_field *field;
259b1a37ed0SLee Jones unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
260f07b3c1dSBenjamin Tissoires unsigned int usages;
261f07b3c1dSBenjamin Tissoires unsigned int offset;
262f07b3c1dSBenjamin Tissoires unsigned int i;
263f07b3c1dSBenjamin Tissoires unsigned int application;
264dde5845aSJiri Kosina
265f07b3c1dSBenjamin Tissoires application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
266f07b3c1dSBenjamin Tissoires
267f07b3c1dSBenjamin Tissoires report = hid_register_report(parser->device, report_type,
268f07b3c1dSBenjamin Tissoires parser->global.report_id, application);
269a3789a17SJoe Perches if (!report) {
2708c3d52fcSJiri Kosina hid_err(parser->device, "hid_register_report failed\n");
271dde5845aSJiri Kosina return -1;
272dde5845aSJiri Kosina }
273dde5845aSJiri Kosina
274bb2e1976SJiri Kosina /* Handle both signed and unsigned cases properly */
2750cd516c2Ssrinivas pandruvada if ((parser->global.logical_minimum < 0 &&
2760cd516c2Ssrinivas pandruvada parser->global.logical_maximum <
2770cd516c2Ssrinivas pandruvada parser->global.logical_minimum) ||
2780cd516c2Ssrinivas pandruvada (parser->global.logical_minimum >= 0 &&
2790cd516c2Ssrinivas pandruvada (__u32)parser->global.logical_maximum <
2800cd516c2Ssrinivas pandruvada (__u32)parser->global.logical_minimum)) {
2810cd516c2Ssrinivas pandruvada dbg_hid("logical range invalid 0x%x 0x%x\n",
2820cd516c2Ssrinivas pandruvada parser->global.logical_minimum,
2830cd516c2Ssrinivas pandruvada parser->global.logical_maximum);
284dde5845aSJiri Kosina return -1;
285dde5845aSJiri Kosina }
286dde5845aSJiri Kosina
287dde5845aSJiri Kosina offset = report->size;
288dde5845aSJiri Kosina report->size += parser->global.report_size * parser->global.report_count;
289dde5845aSJiri Kosina
290b1a37ed0SLee Jones if (parser->device->ll_driver->max_buffer_size)
291b1a37ed0SLee Jones max_buffer_size = parser->device->ll_driver->max_buffer_size;
292b1a37ed0SLee Jones
2938ec321e9SAlan Stern /* Total size check: Allow for possible report index byte */
294b1a37ed0SLee Jones if (report->size > (max_buffer_size - 1) << 3) {
2958ec321e9SAlan Stern hid_err(parser->device, "report is too long\n");
2968ec321e9SAlan Stern return -1;
2978ec321e9SAlan Stern }
2988ec321e9SAlan Stern
299dde5845aSJiri Kosina if (!parser->local.usage_index) /* Ignore padding fields */
300dde5845aSJiri Kosina return 0;
301dde5845aSJiri Kosina
302cc6b54aaSBenjamin Tissoires usages = max_t(unsigned, parser->local.usage_index,
303cc6b54aaSBenjamin Tissoires parser->global.report_count);
304dde5845aSJiri Kosina
305ed9be64eSWill McVicker field = hid_register_field(report, usages);
306a3789a17SJoe Perches if (!field)
307dde5845aSJiri Kosina return 0;
308dde5845aSJiri Kosina
309dde5845aSJiri Kosina field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
310dde5845aSJiri Kosina field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
311f07b3c1dSBenjamin Tissoires field->application = application;
312dde5845aSJiri Kosina
313dde5845aSJiri Kosina for (i = 0; i < usages; i++) {
314cc6b54aaSBenjamin Tissoires unsigned j = i;
315dde5845aSJiri Kosina /* Duplicate the last usage we parsed if we have excess values */
316dde5845aSJiri Kosina if (i >= parser->local.usage_index)
317dde5845aSJiri Kosina j = parser->local.usage_index - 1;
318dde5845aSJiri Kosina field->usage[i].hid = parser->local.usage[j];
319dde5845aSJiri Kosina field->usage[i].collection_index =
320dde5845aSJiri Kosina parser->local.collection_index[j];
321cc6b54aaSBenjamin Tissoires field->usage[i].usage_index = i;
3225a4abb36SPeter Hutterer field->usage[i].resolution_multiplier = 1;
323dde5845aSJiri Kosina }
324dde5845aSJiri Kosina
325dde5845aSJiri Kosina field->maxusage = usages;
326dde5845aSJiri Kosina field->flags = flags;
327dde5845aSJiri Kosina field->report_offset = offset;
328dde5845aSJiri Kosina field->report_type = report_type;
329dde5845aSJiri Kosina field->report_size = parser->global.report_size;
330dde5845aSJiri Kosina field->report_count = parser->global.report_count;
331dde5845aSJiri Kosina field->logical_minimum = parser->global.logical_minimum;
332dde5845aSJiri Kosina field->logical_maximum = parser->global.logical_maximum;
333dde5845aSJiri Kosina field->physical_minimum = parser->global.physical_minimum;
334dde5845aSJiri Kosina field->physical_maximum = parser->global.physical_maximum;
335dde5845aSJiri Kosina field->unit_exponent = parser->global.unit_exponent;
336dde5845aSJiri Kosina field->unit = parser->global.unit;
337dde5845aSJiri Kosina
338dde5845aSJiri Kosina return 0;
339dde5845aSJiri Kosina }
340dde5845aSJiri Kosina
341dde5845aSJiri Kosina /*
342dde5845aSJiri Kosina * Read data value from item.
343dde5845aSJiri Kosina */
344dde5845aSJiri Kosina
item_udata(struct hid_item * item)345dde5845aSJiri Kosina static u32 item_udata(struct hid_item *item)
346dde5845aSJiri Kosina {
347dde5845aSJiri Kosina switch (item->size) {
348dde5845aSJiri Kosina case 1: return item->data.u8;
349dde5845aSJiri Kosina case 2: return item->data.u16;
350dde5845aSJiri Kosina case 4: return item->data.u32;
351dde5845aSJiri Kosina }
352dde5845aSJiri Kosina return 0;
353dde5845aSJiri Kosina }
354dde5845aSJiri Kosina
item_sdata(struct hid_item * item)355dde5845aSJiri Kosina static s32 item_sdata(struct hid_item *item)
356dde5845aSJiri Kosina {
357dde5845aSJiri Kosina switch (item->size) {
358dde5845aSJiri Kosina case 1: return item->data.s8;
359dde5845aSJiri Kosina case 2: return item->data.s16;
360dde5845aSJiri Kosina case 4: return item->data.s32;
361dde5845aSJiri Kosina }
362dde5845aSJiri Kosina return 0;
363dde5845aSJiri Kosina }
364dde5845aSJiri Kosina
365dde5845aSJiri Kosina /*
366dde5845aSJiri Kosina * Process a global item.
367dde5845aSJiri Kosina */
368dde5845aSJiri Kosina
hid_parser_global(struct hid_parser * parser,struct hid_item * item)369dde5845aSJiri Kosina static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
370dde5845aSJiri Kosina {
371ad0e669bSNikolai Kondrashov __s32 raw_value;
372dde5845aSJiri Kosina switch (item->tag) {
373dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_PUSH:
374dde5845aSJiri Kosina
375dde5845aSJiri Kosina if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
3768c3d52fcSJiri Kosina hid_err(parser->device, "global environment stack overflow\n");
377dde5845aSJiri Kosina return -1;
378dde5845aSJiri Kosina }
379dde5845aSJiri Kosina
380dde5845aSJiri Kosina memcpy(parser->global_stack + parser->global_stack_ptr++,
381dde5845aSJiri Kosina &parser->global, sizeof(struct hid_global));
382dde5845aSJiri Kosina return 0;
383dde5845aSJiri Kosina
384dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_POP:
385dde5845aSJiri Kosina
386dde5845aSJiri Kosina if (!parser->global_stack_ptr) {
3878c3d52fcSJiri Kosina hid_err(parser->device, "global environment stack underflow\n");
388dde5845aSJiri Kosina return -1;
389dde5845aSJiri Kosina }
390dde5845aSJiri Kosina
391880d29f1SJiri Slaby memcpy(&parser->global, parser->global_stack +
392880d29f1SJiri Slaby --parser->global_stack_ptr, sizeof(struct hid_global));
393dde5845aSJiri Kosina return 0;
394dde5845aSJiri Kosina
395dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
396dde5845aSJiri Kosina parser->global.usage_page = item_udata(item);
397dde5845aSJiri Kosina return 0;
398dde5845aSJiri Kosina
399dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
400dde5845aSJiri Kosina parser->global.logical_minimum = item_sdata(item);
401dde5845aSJiri Kosina return 0;
402dde5845aSJiri Kosina
403dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
404dde5845aSJiri Kosina if (parser->global.logical_minimum < 0)
405dde5845aSJiri Kosina parser->global.logical_maximum = item_sdata(item);
406dde5845aSJiri Kosina else
407dde5845aSJiri Kosina parser->global.logical_maximum = item_udata(item);
408dde5845aSJiri Kosina return 0;
409dde5845aSJiri Kosina
410dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
411dde5845aSJiri Kosina parser->global.physical_minimum = item_sdata(item);
412dde5845aSJiri Kosina return 0;
413dde5845aSJiri Kosina
414dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
415dde5845aSJiri Kosina if (parser->global.physical_minimum < 0)
416dde5845aSJiri Kosina parser->global.physical_maximum = item_sdata(item);
417dde5845aSJiri Kosina else
418dde5845aSJiri Kosina parser->global.physical_maximum = item_udata(item);
419dde5845aSJiri Kosina return 0;
420dde5845aSJiri Kosina
421dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
422ad0e669bSNikolai Kondrashov /* Many devices provide unit exponent as a two's complement
423ad0e669bSNikolai Kondrashov * nibble due to the common misunderstanding of HID
424ad0e669bSNikolai Kondrashov * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
425ad0e669bSNikolai Kondrashov * both this and the standard encoding. */
426ad0e669bSNikolai Kondrashov raw_value = item_sdata(item);
42777463838SBenjamin Tissoires if (!(raw_value & 0xfffffff0))
42877463838SBenjamin Tissoires parser->global.unit_exponent = hid_snto32(raw_value, 4);
42977463838SBenjamin Tissoires else
43077463838SBenjamin Tissoires parser->global.unit_exponent = raw_value;
431dde5845aSJiri Kosina return 0;
432dde5845aSJiri Kosina
433dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_UNIT:
434dde5845aSJiri Kosina parser->global.unit = item_udata(item);
435dde5845aSJiri Kosina return 0;
436dde5845aSJiri Kosina
437dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
438880d29f1SJiri Slaby parser->global.report_size = item_udata(item);
43971f6fa90SSong, Hongyan if (parser->global.report_size > 256) {
4408c3d52fcSJiri Kosina hid_err(parser->device, "invalid report_size %d\n",
441880d29f1SJiri Slaby parser->global.report_size);
442dde5845aSJiri Kosina return -1;
443dde5845aSJiri Kosina }
444dde5845aSJiri Kosina return 0;
445dde5845aSJiri Kosina
446dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
447880d29f1SJiri Slaby parser->global.report_count = item_udata(item);
448880d29f1SJiri Slaby if (parser->global.report_count > HID_MAX_USAGES) {
4498c3d52fcSJiri Kosina hid_err(parser->device, "invalid report_count %d\n",
450880d29f1SJiri Slaby parser->global.report_count);
451dde5845aSJiri Kosina return -1;
452dde5845aSJiri Kosina }
453dde5845aSJiri Kosina return 0;
454dde5845aSJiri Kosina
455dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_REPORT_ID:
456880d29f1SJiri Slaby parser->global.report_id = item_udata(item);
45743622021SKees Cook if (parser->global.report_id == 0 ||
45843622021SKees Cook parser->global.report_id >= HID_MAX_IDS) {
45943622021SKees Cook hid_err(parser->device, "report_id %u is invalid\n",
46043622021SKees Cook parser->global.report_id);
461dde5845aSJiri Kosina return -1;
462dde5845aSJiri Kosina }
463dde5845aSJiri Kosina return 0;
464dde5845aSJiri Kosina
465dde5845aSJiri Kosina default:
4668c3d52fcSJiri Kosina hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
467dde5845aSJiri Kosina return -1;
468dde5845aSJiri Kosina }
469dde5845aSJiri Kosina }
470dde5845aSJiri Kosina
471dde5845aSJiri Kosina /*
472dde5845aSJiri Kosina * Process a local item.
473dde5845aSJiri Kosina */
474dde5845aSJiri Kosina
hid_parser_local(struct hid_parser * parser,struct hid_item * item)475dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
476dde5845aSJiri Kosina {
477dde5845aSJiri Kosina __u32 data;
478dde5845aSJiri Kosina unsigned n;
479ba532198SBenjamin Tissoires __u32 count;
480dde5845aSJiri Kosina
481dde5845aSJiri Kosina data = item_udata(item);
482dde5845aSJiri Kosina
483dde5845aSJiri Kosina switch (item->tag) {
484dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_DELIMITER:
485dde5845aSJiri Kosina
486dde5845aSJiri Kosina if (data) {
487dde5845aSJiri Kosina /*
488dde5845aSJiri Kosina * We treat items before the first delimiter
489dde5845aSJiri Kosina * as global to all usage sets (branch 0).
490dde5845aSJiri Kosina * In the moment we process only these global
491dde5845aSJiri Kosina * items and the first delimiter set.
492dde5845aSJiri Kosina */
493dde5845aSJiri Kosina if (parser->local.delimiter_depth != 0) {
4948c3d52fcSJiri Kosina hid_err(parser->device, "nested delimiters\n");
495dde5845aSJiri Kosina return -1;
496dde5845aSJiri Kosina }
497dde5845aSJiri Kosina parser->local.delimiter_depth++;
498dde5845aSJiri Kosina parser->local.delimiter_branch++;
499dde5845aSJiri Kosina } else {
500dde5845aSJiri Kosina if (parser->local.delimiter_depth < 1) {
5018c3d52fcSJiri Kosina hid_err(parser->device, "bogus close delimiter\n");
502dde5845aSJiri Kosina return -1;
503dde5845aSJiri Kosina }
504dde5845aSJiri Kosina parser->local.delimiter_depth--;
505dde5845aSJiri Kosina }
50638ead6efSPaul Chavent return 0;
507dde5845aSJiri Kosina
508dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_USAGE:
509dde5845aSJiri Kosina
510dde5845aSJiri Kosina if (parser->local.delimiter_branch > 1) {
51158037eb9SJiri Kosina dbg_hid("alternative usage ignored\n");
512dde5845aSJiri Kosina return 0;
513dde5845aSJiri Kosina }
514dde5845aSJiri Kosina
51558e75155SNicolas Saenz Julienne return hid_add_usage(parser, data, item->size);
516dde5845aSJiri Kosina
517dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
518dde5845aSJiri Kosina
519dde5845aSJiri Kosina if (parser->local.delimiter_branch > 1) {
52058037eb9SJiri Kosina dbg_hid("alternative usage ignored\n");
521dde5845aSJiri Kosina return 0;
522dde5845aSJiri Kosina }
523dde5845aSJiri Kosina
524dde5845aSJiri Kosina parser->local.usage_minimum = data;
525dde5845aSJiri Kosina return 0;
526dde5845aSJiri Kosina
527dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
528dde5845aSJiri Kosina
529dde5845aSJiri Kosina if (parser->local.delimiter_branch > 1) {
53058037eb9SJiri Kosina dbg_hid("alternative usage ignored\n");
531dde5845aSJiri Kosina return 0;
532dde5845aSJiri Kosina }
533dde5845aSJiri Kosina
534ba532198SBenjamin Tissoires count = data - parser->local.usage_minimum;
535ba532198SBenjamin Tissoires if (count + parser->local.usage_index >= HID_MAX_USAGES) {
536ba532198SBenjamin Tissoires /*
537ba532198SBenjamin Tissoires * We do not warn if the name is not set, we are
538ba532198SBenjamin Tissoires * actually pre-scanning the device.
539ba532198SBenjamin Tissoires */
540ba532198SBenjamin Tissoires if (dev_name(&parser->device->dev))
541ba532198SBenjamin Tissoires hid_warn(parser->device,
542ba532198SBenjamin Tissoires "ignoring exceeding usage max\n");
543ba532198SBenjamin Tissoires data = HID_MAX_USAGES - parser->local.usage_index +
544ba532198SBenjamin Tissoires parser->local.usage_minimum - 1;
545ba532198SBenjamin Tissoires if (data <= 0) {
546ba532198SBenjamin Tissoires hid_err(parser->device,
547ba532198SBenjamin Tissoires "no more usage index available\n");
548ba532198SBenjamin Tissoires return -1;
549ba532198SBenjamin Tissoires }
550ba532198SBenjamin Tissoires }
551ba532198SBenjamin Tissoires
552dde5845aSJiri Kosina for (n = parser->local.usage_minimum; n <= data; n++)
55358e75155SNicolas Saenz Julienne if (hid_add_usage(parser, n, item->size)) {
55458037eb9SJiri Kosina dbg_hid("hid_add_usage failed\n");
555dde5845aSJiri Kosina return -1;
556dde5845aSJiri Kosina }
557dde5845aSJiri Kosina return 0;
558dde5845aSJiri Kosina
559dde5845aSJiri Kosina default:
560dde5845aSJiri Kosina
56158037eb9SJiri Kosina dbg_hid("unknown local item tag 0x%x\n", item->tag);
562dde5845aSJiri Kosina return 0;
563dde5845aSJiri Kosina }
564dde5845aSJiri Kosina return 0;
565dde5845aSJiri Kosina }
566dde5845aSJiri Kosina
567dde5845aSJiri Kosina /*
56858e75155SNicolas Saenz Julienne * Concatenate Usage Pages into Usages where relevant:
56958e75155SNicolas Saenz Julienne * As per specification, 6.2.2.8: "When the parser encounters a main item it
57058e75155SNicolas Saenz Julienne * concatenates the last declared Usage Page with a Usage to form a complete
57158e75155SNicolas Saenz Julienne * usage value."
57258e75155SNicolas Saenz Julienne */
57358e75155SNicolas Saenz Julienne
hid_concatenate_last_usage_page(struct hid_parser * parser)5741cb0d2aeSCandle Sun static void hid_concatenate_last_usage_page(struct hid_parser *parser)
57558e75155SNicolas Saenz Julienne {
57658e75155SNicolas Saenz Julienne int i;
5771cb0d2aeSCandle Sun unsigned int usage_page;
5781cb0d2aeSCandle Sun unsigned int current_page;
57958e75155SNicolas Saenz Julienne
5801cb0d2aeSCandle Sun if (!parser->local.usage_index)
5811cb0d2aeSCandle Sun return;
5821cb0d2aeSCandle Sun
5831cb0d2aeSCandle Sun usage_page = parser->global.usage_page;
5841cb0d2aeSCandle Sun
5851cb0d2aeSCandle Sun /*
5861cb0d2aeSCandle Sun * Concatenate usage page again only if last declared Usage Page
5871cb0d2aeSCandle Sun * has not been already used in previous usages concatenation
5881cb0d2aeSCandle Sun */
5891cb0d2aeSCandle Sun for (i = parser->local.usage_index - 1; i >= 0; i--) {
5901cb0d2aeSCandle Sun if (parser->local.usage_size[i] > 2)
5911cb0d2aeSCandle Sun /* Ignore extended usages */
5921cb0d2aeSCandle Sun continue;
5931cb0d2aeSCandle Sun
5941cb0d2aeSCandle Sun current_page = parser->local.usage[i] >> 16;
5951cb0d2aeSCandle Sun if (current_page == usage_page)
5961cb0d2aeSCandle Sun break;
5971cb0d2aeSCandle Sun
5981cb0d2aeSCandle Sun complete_usage(parser, i);
5991cb0d2aeSCandle Sun }
60058e75155SNicolas Saenz Julienne }
60158e75155SNicolas Saenz Julienne
60258e75155SNicolas Saenz Julienne /*
603dde5845aSJiri Kosina * Process a main item.
604dde5845aSJiri Kosina */
605dde5845aSJiri Kosina
hid_parser_main(struct hid_parser * parser,struct hid_item * item)606dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
607dde5845aSJiri Kosina {
608dde5845aSJiri Kosina __u32 data;
609dde5845aSJiri Kosina int ret;
610dde5845aSJiri Kosina
6111cb0d2aeSCandle Sun hid_concatenate_last_usage_page(parser);
61258e75155SNicolas Saenz Julienne
613dde5845aSJiri Kosina data = item_udata(item);
614dde5845aSJiri Kosina
615dde5845aSJiri Kosina switch (item->tag) {
616dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
617dde5845aSJiri Kosina ret = open_collection(parser, data & 0xff);
618dde5845aSJiri Kosina break;
619dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_END_COLLECTION:
620dde5845aSJiri Kosina ret = close_collection(parser);
621dde5845aSJiri Kosina break;
622dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_INPUT:
623dde5845aSJiri Kosina ret = hid_add_field(parser, HID_INPUT_REPORT, data);
624dde5845aSJiri Kosina break;
625dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_OUTPUT:
626dde5845aSJiri Kosina ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
627dde5845aSJiri Kosina break;
628dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_FEATURE:
629dde5845aSJiri Kosina ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
630dde5845aSJiri Kosina break;
631dde5845aSJiri Kosina default:
6327cb4774eSHans de Goede hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
633dde5845aSJiri Kosina ret = 0;
634dde5845aSJiri Kosina }
635dde5845aSJiri Kosina
636dde5845aSJiri Kosina memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
637dde5845aSJiri Kosina
638dde5845aSJiri Kosina return ret;
639dde5845aSJiri Kosina }
640dde5845aSJiri Kosina
641dde5845aSJiri Kosina /*
642dde5845aSJiri Kosina * Process a reserved item.
643dde5845aSJiri Kosina */
644dde5845aSJiri Kosina
hid_parser_reserved(struct hid_parser * parser,struct hid_item * item)645dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
646dde5845aSJiri Kosina {
64758037eb9SJiri Kosina dbg_hid("reserved item type, tag 0x%x\n", item->tag);
648dde5845aSJiri Kosina return 0;
649dde5845aSJiri Kosina }
650dde5845aSJiri Kosina
651dde5845aSJiri Kosina /*
652dde5845aSJiri Kosina * Free a report and all registered fields. The field->usage and
653dde5845aSJiri Kosina * field->value table's are allocated behind the field, so we need
654dde5845aSJiri Kosina * only to free(field) itself.
655dde5845aSJiri Kosina */
656dde5845aSJiri Kosina
hid_free_report(struct hid_report * report)657dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report)
658dde5845aSJiri Kosina {
659dde5845aSJiri Kosina unsigned n;
660dde5845aSJiri Kosina
66122f4b026SBenjamin Tissoires kfree(report->field_entries);
66222f4b026SBenjamin Tissoires
663dde5845aSJiri Kosina for (n = 0; n < report->maxfield; n++)
664dde5845aSJiri Kosina kfree(report->field[n]);
665dde5845aSJiri Kosina kfree(report);
666dde5845aSJiri Kosina }
667dde5845aSJiri Kosina
668dde5845aSJiri Kosina /*
669a7197c2eSHenrik Rydberg * Close report. This function returns the device
670a7197c2eSHenrik Rydberg * state to the point prior to hid_open_report().
671dde5845aSJiri Kosina */
hid_close_report(struct hid_device * device)672a7197c2eSHenrik Rydberg static void hid_close_report(struct hid_device *device)
673dde5845aSJiri Kosina {
674dde5845aSJiri Kosina unsigned i, j;
675dde5845aSJiri Kosina
676dde5845aSJiri Kosina for (i = 0; i < HID_REPORT_TYPES; i++) {
677dde5845aSJiri Kosina struct hid_report_enum *report_enum = device->report_enum + i;
678dde5845aSJiri Kosina
67943622021SKees Cook for (j = 0; j < HID_MAX_IDS; j++) {
680dde5845aSJiri Kosina struct hid_report *report = report_enum->report_id_hash[j];
681dde5845aSJiri Kosina if (report)
682dde5845aSJiri Kosina hid_free_report(report);
683dde5845aSJiri Kosina }
684a7197c2eSHenrik Rydberg memset(report_enum, 0, sizeof(*report_enum));
685a7197c2eSHenrik Rydberg INIT_LIST_HEAD(&report_enum->report_list);
686dde5845aSJiri Kosina }
687dde5845aSJiri Kosina
688dde5845aSJiri Kosina kfree(device->rdesc);
689a7197c2eSHenrik Rydberg device->rdesc = NULL;
690a7197c2eSHenrik Rydberg device->rsize = 0;
691a7197c2eSHenrik Rydberg
692767fe787SJiri Kosina kfree(device->collection);
693a7197c2eSHenrik Rydberg device->collection = NULL;
694a7197c2eSHenrik Rydberg device->collection_size = 0;
695a7197c2eSHenrik Rydberg device->maxcollection = 0;
696a7197c2eSHenrik Rydberg device->maxapplication = 0;
697a7197c2eSHenrik Rydberg
698a7197c2eSHenrik Rydberg device->status &= ~HID_STAT_PARSED;
699a7197c2eSHenrik Rydberg }
700a7197c2eSHenrik Rydberg
701a7197c2eSHenrik Rydberg /*
702a7197c2eSHenrik Rydberg * Free a device structure, all reports, and all fields.
703a7197c2eSHenrik Rydberg */
704a7197c2eSHenrik Rydberg
hiddev_free(struct kref * ref)7056642322aSCharles Yi void hiddev_free(struct kref *ref)
706a7197c2eSHenrik Rydberg {
7076642322aSCharles Yi struct hid_device *hid = container_of(ref, struct hid_device, ref);
708a7197c2eSHenrik Rydberg
709a7197c2eSHenrik Rydberg hid_close_report(hid);
710a7197c2eSHenrik Rydberg kfree(hid->dev_rdesc);
711a7197c2eSHenrik Rydberg kfree(hid);
712dde5845aSJiri Kosina }
713dde5845aSJiri Kosina
hid_device_release(struct device * dev)7146642322aSCharles Yi static void hid_device_release(struct device *dev)
7156642322aSCharles Yi {
7166642322aSCharles Yi struct hid_device *hid = to_hid_device(dev);
7176642322aSCharles Yi
7186642322aSCharles Yi kref_put(&hid->ref, hiddev_free);
7196642322aSCharles Yi }
7206642322aSCharles Yi
721dde5845aSJiri Kosina /*
722dde5845aSJiri Kosina * Fetch a report description item from the data stream. We support long
723dde5845aSJiri Kosina * items, though they are not used yet.
724dde5845aSJiri Kosina */
725dde5845aSJiri Kosina
fetch_item(__u8 * start,__u8 * end,struct hid_item * item)726dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
727dde5845aSJiri Kosina {
728dde5845aSJiri Kosina u8 b;
729dde5845aSJiri Kosina
730dde5845aSJiri Kosina if ((end - start) <= 0)
731dde5845aSJiri Kosina return NULL;
732dde5845aSJiri Kosina
733dde5845aSJiri Kosina b = *start++;
734dde5845aSJiri Kosina
735dde5845aSJiri Kosina item->type = (b >> 2) & 3;
736dde5845aSJiri Kosina item->tag = (b >> 4) & 15;
737dde5845aSJiri Kosina
738dde5845aSJiri Kosina if (item->tag == HID_ITEM_TAG_LONG) {
739dde5845aSJiri Kosina
740dde5845aSJiri Kosina item->format = HID_ITEM_FORMAT_LONG;
741dde5845aSJiri Kosina
742dde5845aSJiri Kosina if ((end - start) < 2)
743dde5845aSJiri Kosina return NULL;
744dde5845aSJiri Kosina
745dde5845aSJiri Kosina item->size = *start++;
746dde5845aSJiri Kosina item->tag = *start++;
747dde5845aSJiri Kosina
748dde5845aSJiri Kosina if ((end - start) < item->size)
749dde5845aSJiri Kosina return NULL;
750dde5845aSJiri Kosina
751dde5845aSJiri Kosina item->data.longdata = start;
752dde5845aSJiri Kosina start += item->size;
753dde5845aSJiri Kosina return start;
754dde5845aSJiri Kosina }
755dde5845aSJiri Kosina
756dde5845aSJiri Kosina item->format = HID_ITEM_FORMAT_SHORT;
757dde5845aSJiri Kosina item->size = b & 3;
758dde5845aSJiri Kosina
759dde5845aSJiri Kosina switch (item->size) {
760dde5845aSJiri Kosina case 0:
761dde5845aSJiri Kosina return start;
762dde5845aSJiri Kosina
763dde5845aSJiri Kosina case 1:
764dde5845aSJiri Kosina if ((end - start) < 1)
765dde5845aSJiri Kosina return NULL;
766dde5845aSJiri Kosina item->data.u8 = *start++;
767dde5845aSJiri Kosina return start;
768dde5845aSJiri Kosina
769dde5845aSJiri Kosina case 2:
770dde5845aSJiri Kosina if ((end - start) < 2)
771dde5845aSJiri Kosina return NULL;
772c105068fSHarvey Harrison item->data.u16 = get_unaligned_le16(start);
773dde5845aSJiri Kosina start = (__u8 *)((__le16 *)start + 1);
774dde5845aSJiri Kosina return start;
775dde5845aSJiri Kosina
776dde5845aSJiri Kosina case 3:
777dde5845aSJiri Kosina item->size++;
778dde5845aSJiri Kosina if ((end - start) < 4)
779dde5845aSJiri Kosina return NULL;
780c105068fSHarvey Harrison item->data.u32 = get_unaligned_le32(start);
781dde5845aSJiri Kosina start = (__u8 *)((__le32 *)start + 1);
782dde5845aSJiri Kosina return start;
783dde5845aSJiri Kosina }
784dde5845aSJiri Kosina
785dde5845aSJiri Kosina return NULL;
786dde5845aSJiri Kosina }
787dde5845aSJiri Kosina
hid_scan_input_usage(struct hid_parser * parser,u32 usage)7883dc8fc08SBenjamin Tissoires static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
789734c6609SHenrik Rydberg {
7903dc8fc08SBenjamin Tissoires struct hid_device *hid = parser->device;
7913dc8fc08SBenjamin Tissoires
7924fa3a583SHenrik Rydberg if (usage == HID_DG_CONTACTID)
7934fa3a583SHenrik Rydberg hid->group = HID_GROUP_MULTITOUCH;
794734c6609SHenrik Rydberg }
795734c6609SHenrik Rydberg
hid_scan_feature_usage(struct hid_parser * parser,u32 usage)796f961bd35SBenjamin Tissoires static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
797f961bd35SBenjamin Tissoires {
798f961bd35SBenjamin Tissoires if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
799f961bd35SBenjamin Tissoires parser->global.report_size == 8)
800f961bd35SBenjamin Tissoires parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
8012dbc6f11SBlaž Hrastnik
8022dbc6f11SBlaž Hrastnik if (usage == 0xff0000c6 && parser->global.report_count == 1 &&
8032dbc6f11SBlaž Hrastnik parser->global.report_size == 8)
8042dbc6f11SBlaž Hrastnik parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
805f961bd35SBenjamin Tissoires }
806f961bd35SBenjamin Tissoires
hid_scan_collection(struct hid_parser * parser,unsigned type)8073dc8fc08SBenjamin Tissoires static void hid_scan_collection(struct hid_parser *parser, unsigned type)
8083dc8fc08SBenjamin Tissoires {
8093dc8fc08SBenjamin Tissoires struct hid_device *hid = parser->device;
810e39f2d59SAndrew Duggan int i;
8113dc8fc08SBenjamin Tissoires
8123dc8fc08SBenjamin Tissoires if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
813e04955dbSRonald Tschalär (type == HID_COLLECTION_PHYSICAL ||
814e04955dbSRonald Tschalär type == HID_COLLECTION_APPLICATION))
8153dc8fc08SBenjamin Tissoires hid->group = HID_GROUP_SENSOR_HUB;
816be3b1634SAlan Wu
817be3b1634SAlan Wu if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
8180a76ac80SDennis Chen hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
819be3b1634SAlan Wu hid->group == HID_GROUP_MULTITOUCH)
820be3b1634SAlan Wu hid->group = HID_GROUP_GENERIC;
821e39f2d59SAndrew Duggan
822e39f2d59SAndrew Duggan if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
823e39f2d59SAndrew Duggan for (i = 0; i < parser->local.usage_index; i++)
824e39f2d59SAndrew Duggan if (parser->local.usage[i] == HID_GD_POINTER)
825e39f2d59SAndrew Duggan parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
826e39f2d59SAndrew Duggan
827e39f2d59SAndrew Duggan if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
828e39f2d59SAndrew Duggan parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
82914c9c014SSean O'Brien
83014c9c014SSean O'Brien if ((parser->global.usage_page << 16) == HID_UP_GOOGLEVENDOR)
83114c9c014SSean O'Brien for (i = 0; i < parser->local.usage_index; i++)
83214c9c014SSean O'Brien if (parser->local.usage[i] ==
83314c9c014SSean O'Brien (HID_UP_GOOGLEVENDOR | 0x0001))
83414c9c014SSean O'Brien parser->device->group =
83514c9c014SSean O'Brien HID_GROUP_VIVALDI;
8363dc8fc08SBenjamin Tissoires }
8373dc8fc08SBenjamin Tissoires
hid_scan_main(struct hid_parser * parser,struct hid_item * item)8383dc8fc08SBenjamin Tissoires static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
8393dc8fc08SBenjamin Tissoires {
8403dc8fc08SBenjamin Tissoires __u32 data;
8413dc8fc08SBenjamin Tissoires int i;
8423dc8fc08SBenjamin Tissoires
8431cb0d2aeSCandle Sun hid_concatenate_last_usage_page(parser);
84458e75155SNicolas Saenz Julienne
8453dc8fc08SBenjamin Tissoires data = item_udata(item);
8463dc8fc08SBenjamin Tissoires
8473dc8fc08SBenjamin Tissoires switch (item->tag) {
8483dc8fc08SBenjamin Tissoires case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
8493dc8fc08SBenjamin Tissoires hid_scan_collection(parser, data & 0xff);
8503dc8fc08SBenjamin Tissoires break;
8513dc8fc08SBenjamin Tissoires case HID_MAIN_ITEM_TAG_END_COLLECTION:
8523dc8fc08SBenjamin Tissoires break;
8533dc8fc08SBenjamin Tissoires case HID_MAIN_ITEM_TAG_INPUT:
854e24d0d39SBenjamin Tissoires /* ignore constant inputs, they will be ignored by hid-input */
855e24d0d39SBenjamin Tissoires if (data & HID_MAIN_ITEM_CONSTANT)
856e24d0d39SBenjamin Tissoires break;
8573dc8fc08SBenjamin Tissoires for (i = 0; i < parser->local.usage_index; i++)
8583dc8fc08SBenjamin Tissoires hid_scan_input_usage(parser, parser->local.usage[i]);
8593dc8fc08SBenjamin Tissoires break;
8603dc8fc08SBenjamin Tissoires case HID_MAIN_ITEM_TAG_OUTPUT:
8613dc8fc08SBenjamin Tissoires break;
8623dc8fc08SBenjamin Tissoires case HID_MAIN_ITEM_TAG_FEATURE:
863f961bd35SBenjamin Tissoires for (i = 0; i < parser->local.usage_index; i++)
864f961bd35SBenjamin Tissoires hid_scan_feature_usage(parser, parser->local.usage[i]);
8653dc8fc08SBenjamin Tissoires break;
8663dc8fc08SBenjamin Tissoires }
8673dc8fc08SBenjamin Tissoires
8683dc8fc08SBenjamin Tissoires /* Reset the local parser environment */
8693dc8fc08SBenjamin Tissoires memset(&parser->local, 0, sizeof(parser->local));
8703dc8fc08SBenjamin Tissoires
8713dc8fc08SBenjamin Tissoires return 0;
8723dc8fc08SBenjamin Tissoires }
8733dc8fc08SBenjamin Tissoires
874734c6609SHenrik Rydberg /*
875734c6609SHenrik Rydberg * Scan a report descriptor before the device is added to the bus.
876734c6609SHenrik Rydberg * Sets device groups and other properties that determine what driver
877734c6609SHenrik Rydberg * to load.
878734c6609SHenrik Rydberg */
hid_scan_report(struct hid_device * hid)879734c6609SHenrik Rydberg static int hid_scan_report(struct hid_device *hid)
880734c6609SHenrik Rydberg {
8813dc8fc08SBenjamin Tissoires struct hid_parser *parser;
8823dc8fc08SBenjamin Tissoires struct hid_item item;
883734c6609SHenrik Rydberg __u8 *start = hid->dev_rdesc;
884734c6609SHenrik Rydberg __u8 *end = start + hid->dev_rsize;
8853dc8fc08SBenjamin Tissoires static int (*dispatch_type[])(struct hid_parser *parser,
8863dc8fc08SBenjamin Tissoires struct hid_item *item) = {
8873dc8fc08SBenjamin Tissoires hid_scan_main,
8883dc8fc08SBenjamin Tissoires hid_parser_global,
8893dc8fc08SBenjamin Tissoires hid_parser_local,
8903dc8fc08SBenjamin Tissoires hid_parser_reserved
8913dc8fc08SBenjamin Tissoires };
892734c6609SHenrik Rydberg
8933dc8fc08SBenjamin Tissoires parser = vzalloc(sizeof(struct hid_parser));
8943dc8fc08SBenjamin Tissoires if (!parser)
8953dc8fc08SBenjamin Tissoires return -ENOMEM;
8963dc8fc08SBenjamin Tissoires
8973dc8fc08SBenjamin Tissoires parser->device = hid;
898734c6609SHenrik Rydberg hid->group = HID_GROUP_GENERIC;
899734c6609SHenrik Rydberg
9003dc8fc08SBenjamin Tissoires /*
9013dc8fc08SBenjamin Tissoires * The parsing is simpler than the one in hid_open_report() as we should
9023dc8fc08SBenjamin Tissoires * be robust against hid errors. Those errors will be raised by
9033dc8fc08SBenjamin Tissoires * hid_open_report() anyway.
9043dc8fc08SBenjamin Tissoires */
9053dc8fc08SBenjamin Tissoires while ((start = fetch_item(start, end, &item)) != NULL)
9063dc8fc08SBenjamin Tissoires dispatch_type[item.type](parser, &item);
9073dc8fc08SBenjamin Tissoires
908f961bd35SBenjamin Tissoires /*
909f961bd35SBenjamin Tissoires * Handle special flags set during scanning.
910f961bd35SBenjamin Tissoires */
911f961bd35SBenjamin Tissoires if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
912f961bd35SBenjamin Tissoires (hid->group == HID_GROUP_MULTITOUCH))
913f961bd35SBenjamin Tissoires hid->group = HID_GROUP_MULTITOUCH_WIN_8;
914f961bd35SBenjamin Tissoires
915ba391e5aSBenjamin Tissoires /*
916ba391e5aSBenjamin Tissoires * Vendor specific handlings
917ba391e5aSBenjamin Tissoires */
91829b47391SBenjamin Tissoires switch (hid->vendor) {
91929b47391SBenjamin Tissoires case USB_VENDOR_ID_WACOM:
92029b47391SBenjamin Tissoires hid->group = HID_GROUP_WACOM;
92129b47391SBenjamin Tissoires break;
922c241c5eeSBenjamin Tissoires case USB_VENDOR_ID_SYNAPTICS:
92384379d83SJiri Kosina if (hid->group == HID_GROUP_GENERIC)
924e39f2d59SAndrew Duggan if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
925e39f2d59SAndrew Duggan && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
926e39f2d59SAndrew Duggan /*
927e39f2d59SAndrew Duggan * hid-rmi should take care of them,
928e39f2d59SAndrew Duggan * not hid-generic
929e39f2d59SAndrew Duggan */
930c241c5eeSBenjamin Tissoires hid->group = HID_GROUP_RMI;
931c241c5eeSBenjamin Tissoires break;
93229b47391SBenjamin Tissoires }
93329b47391SBenjamin Tissoires
93408a8a7cfSBenjamin Tissoires kfree(parser->collection_stack);
9353dc8fc08SBenjamin Tissoires vfree(parser);
936734c6609SHenrik Rydberg return 0;
937734c6609SHenrik Rydberg }
938734c6609SHenrik Rydberg
93985cdaf52SJiri Slaby /**
94085cdaf52SJiri Slaby * hid_parse_report - parse device report
94185cdaf52SJiri Slaby *
9425f94e9c8SXiaofei Tan * @hid: hid device
94385cdaf52SJiri Slaby * @start: report start
94485cdaf52SJiri Slaby * @size: report size
94585cdaf52SJiri Slaby *
946a7197c2eSHenrik Rydberg * Allocate the device report as read by the bus driver. This function should
947a7197c2eSHenrik Rydberg * only be called from parse() in ll drivers.
948a7197c2eSHenrik Rydberg */
hid_parse_report(struct hid_device * hid,__u8 * start,unsigned size)949a7197c2eSHenrik Rydberg int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
950a7197c2eSHenrik Rydberg {
951a7197c2eSHenrik Rydberg hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
952a7197c2eSHenrik Rydberg if (!hid->dev_rdesc)
953a7197c2eSHenrik Rydberg return -ENOMEM;
954a7197c2eSHenrik Rydberg hid->dev_rsize = size;
955a7197c2eSHenrik Rydberg return 0;
956a7197c2eSHenrik Rydberg }
957a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_parse_report);
958a7197c2eSHenrik Rydberg
959331415ffSKees Cook static const char * const hid_report_names[] = {
960331415ffSKees Cook "HID_INPUT_REPORT",
961331415ffSKees Cook "HID_OUTPUT_REPORT",
962331415ffSKees Cook "HID_FEATURE_REPORT",
963331415ffSKees Cook };
964331415ffSKees Cook /**
965331415ffSKees Cook * hid_validate_values - validate existing device report's value indexes
966331415ffSKees Cook *
9675f94e9c8SXiaofei Tan * @hid: hid device
968331415ffSKees Cook * @type: which report type to examine
969331415ffSKees Cook * @id: which report ID to examine (0 for first)
970331415ffSKees Cook * @field_index: which report field to examine
971331415ffSKees Cook * @report_counts: expected number of values
972331415ffSKees Cook *
973331415ffSKees Cook * Validate the number of values in a given field of a given report, after
974331415ffSKees Cook * parsing.
975331415ffSKees Cook */
hid_validate_values(struct hid_device * hid,enum hid_report_type type,unsigned int id,unsigned int field_index,unsigned int report_counts)976331415ffSKees Cook struct hid_report *hid_validate_values(struct hid_device *hid,
977ead77b65SBenjamin Tissoires enum hid_report_type type, unsigned int id,
978331415ffSKees Cook unsigned int field_index,
979331415ffSKees Cook unsigned int report_counts)
980331415ffSKees Cook {
981331415ffSKees Cook struct hid_report *report;
982331415ffSKees Cook
983331415ffSKees Cook if (type > HID_FEATURE_REPORT) {
984331415ffSKees Cook hid_err(hid, "invalid HID report type %u\n", type);
985331415ffSKees Cook return NULL;
986331415ffSKees Cook }
987331415ffSKees Cook
988331415ffSKees Cook if (id >= HID_MAX_IDS) {
989331415ffSKees Cook hid_err(hid, "invalid HID report id %u\n", id);
990331415ffSKees Cook return NULL;
991331415ffSKees Cook }
992331415ffSKees Cook
993331415ffSKees Cook /*
994331415ffSKees Cook * Explicitly not using hid_get_report() here since it depends on
995331415ffSKees Cook * ->numbered being checked, which may not always be the case when
996331415ffSKees Cook * drivers go to access report values.
997331415ffSKees Cook */
9981b15d2e5SKees Cook if (id == 0) {
9991b15d2e5SKees Cook /*
10001b15d2e5SKees Cook * Validating on id 0 means we should examine the first
10011b15d2e5SKees Cook * report in the list.
10021b15d2e5SKees Cook */
1003b12fece4SPietro Borrello report = list_first_entry_or_null(
1004b12fece4SPietro Borrello &hid->report_enum[type].report_list,
10051b15d2e5SKees Cook struct hid_report, list);
10061b15d2e5SKees Cook } else {
1007331415ffSKees Cook report = hid->report_enum[type].report_id_hash[id];
10081b15d2e5SKees Cook }
1009331415ffSKees Cook if (!report) {
1010331415ffSKees Cook hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
1011331415ffSKees Cook return NULL;
1012331415ffSKees Cook }
1013331415ffSKees Cook if (report->maxfield <= field_index) {
1014331415ffSKees Cook hid_err(hid, "not enough fields in %s %u\n",
1015331415ffSKees Cook hid_report_names[type], id);
1016331415ffSKees Cook return NULL;
1017331415ffSKees Cook }
1018331415ffSKees Cook if (report->field[field_index]->report_count < report_counts) {
1019331415ffSKees Cook hid_err(hid, "not enough values in %s %u field %u\n",
1020331415ffSKees Cook hid_report_names[type], id, field_index);
1021331415ffSKees Cook return NULL;
1022331415ffSKees Cook }
1023331415ffSKees Cook return report;
1024331415ffSKees Cook }
1025331415ffSKees Cook EXPORT_SYMBOL_GPL(hid_validate_values);
1026331415ffSKees Cook
hid_calculate_multiplier(struct hid_device * hid,struct hid_field * multiplier)10275a4abb36SPeter Hutterer static int hid_calculate_multiplier(struct hid_device *hid,
10285a4abb36SPeter Hutterer struct hid_field *multiplier)
10295a4abb36SPeter Hutterer {
10305a4abb36SPeter Hutterer int m;
10315a4abb36SPeter Hutterer __s32 v = *multiplier->value;
10325a4abb36SPeter Hutterer __s32 lmin = multiplier->logical_minimum;
10335a4abb36SPeter Hutterer __s32 lmax = multiplier->logical_maximum;
10345a4abb36SPeter Hutterer __s32 pmin = multiplier->physical_minimum;
10355a4abb36SPeter Hutterer __s32 pmax = multiplier->physical_maximum;
10365a4abb36SPeter Hutterer
10375a4abb36SPeter Hutterer /*
10385a4abb36SPeter Hutterer * "Because OS implementations will generally divide the control's
10395a4abb36SPeter Hutterer * reported count by the Effective Resolution Multiplier, designers
10405a4abb36SPeter Hutterer * should take care not to establish a potential Effective
10415a4abb36SPeter Hutterer * Resolution Multiplier of zero."
10425a4abb36SPeter Hutterer * HID Usage Table, v1.12, Section 4.3.1, p31
10435a4abb36SPeter Hutterer */
10445a4abb36SPeter Hutterer if (lmax - lmin == 0)
10455a4abb36SPeter Hutterer return 1;
10465a4abb36SPeter Hutterer /*
10475a4abb36SPeter Hutterer * Handling the unit exponent is left as an exercise to whoever
10485a4abb36SPeter Hutterer * finds a device where that exponent is not 0.
10495a4abb36SPeter Hutterer */
10505a4abb36SPeter Hutterer m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin);
10515a4abb36SPeter Hutterer if (unlikely(multiplier->unit_exponent != 0)) {
10525a4abb36SPeter Hutterer hid_warn(hid,
10535a4abb36SPeter Hutterer "unsupported Resolution Multiplier unit exponent %d\n",
10545a4abb36SPeter Hutterer multiplier->unit_exponent);
10555a4abb36SPeter Hutterer }
10565a4abb36SPeter Hutterer
10575a4abb36SPeter Hutterer /* There are no devices with an effective multiplier > 255 */
10585a4abb36SPeter Hutterer if (unlikely(m == 0 || m > 255 || m < -255)) {
10595a4abb36SPeter Hutterer hid_warn(hid, "unsupported Resolution Multiplier %d\n", m);
10605a4abb36SPeter Hutterer m = 1;
10615a4abb36SPeter Hutterer }
10625a4abb36SPeter Hutterer
10635a4abb36SPeter Hutterer return m;
10645a4abb36SPeter Hutterer }
10655a4abb36SPeter Hutterer
hid_apply_multiplier_to_field(struct hid_device * hid,struct hid_field * field,struct hid_collection * multiplier_collection,int effective_multiplier)10665a4abb36SPeter Hutterer static void hid_apply_multiplier_to_field(struct hid_device *hid,
10675a4abb36SPeter Hutterer struct hid_field *field,
10685a4abb36SPeter Hutterer struct hid_collection *multiplier_collection,
10695a4abb36SPeter Hutterer int effective_multiplier)
10705a4abb36SPeter Hutterer {
10715a4abb36SPeter Hutterer struct hid_collection *collection;
10725a4abb36SPeter Hutterer struct hid_usage *usage;
10735a4abb36SPeter Hutterer int i;
10745a4abb36SPeter Hutterer
10755a4abb36SPeter Hutterer /*
10765a4abb36SPeter Hutterer * If multiplier_collection is NULL, the multiplier applies
10775a4abb36SPeter Hutterer * to all fields in the report.
10785a4abb36SPeter Hutterer * Otherwise, it is the Logical Collection the multiplier applies to
10795a4abb36SPeter Hutterer * but our field may be in a subcollection of that collection.
10805a4abb36SPeter Hutterer */
10815a4abb36SPeter Hutterer for (i = 0; i < field->maxusage; i++) {
10825a4abb36SPeter Hutterer usage = &field->usage[i];
10835a4abb36SPeter Hutterer
10845a4abb36SPeter Hutterer collection = &hid->collection[usage->collection_index];
1085ee46967fSPeter Hutterer while (collection->parent_idx != -1 &&
1086ee46967fSPeter Hutterer collection != multiplier_collection)
1087ee46967fSPeter Hutterer collection = &hid->collection[collection->parent_idx];
10885a4abb36SPeter Hutterer
1089ee46967fSPeter Hutterer if (collection->parent_idx != -1 ||
1090ee46967fSPeter Hutterer multiplier_collection == NULL)
10915a4abb36SPeter Hutterer usage->resolution_multiplier = effective_multiplier;
10925a4abb36SPeter Hutterer
10935a4abb36SPeter Hutterer }
10945a4abb36SPeter Hutterer }
10955a4abb36SPeter Hutterer
hid_apply_multiplier(struct hid_device * hid,struct hid_field * multiplier)10965a4abb36SPeter Hutterer static void hid_apply_multiplier(struct hid_device *hid,
10975a4abb36SPeter Hutterer struct hid_field *multiplier)
10985a4abb36SPeter Hutterer {
10995a4abb36SPeter Hutterer struct hid_report_enum *rep_enum;
11005a4abb36SPeter Hutterer struct hid_report *rep;
11015a4abb36SPeter Hutterer struct hid_field *field;
11025a4abb36SPeter Hutterer struct hid_collection *multiplier_collection;
11035a4abb36SPeter Hutterer int effective_multiplier;
11045a4abb36SPeter Hutterer int i;
11055a4abb36SPeter Hutterer
11065a4abb36SPeter Hutterer /*
11075a4abb36SPeter Hutterer * "The Resolution Multiplier control must be contained in the same
11085a4abb36SPeter Hutterer * Logical Collection as the control(s) to which it is to be applied.
11095a4abb36SPeter Hutterer * If no Resolution Multiplier is defined, then the Resolution
11105a4abb36SPeter Hutterer * Multiplier defaults to 1. If more than one control exists in a
11115a4abb36SPeter Hutterer * Logical Collection, the Resolution Multiplier is associated with
11125a4abb36SPeter Hutterer * all controls in the collection. If no Logical Collection is
11135a4abb36SPeter Hutterer * defined, the Resolution Multiplier is associated with all
11145a4abb36SPeter Hutterer * controls in the report."
11155a4abb36SPeter Hutterer * HID Usage Table, v1.12, Section 4.3.1, p30
11165a4abb36SPeter Hutterer *
11175a4abb36SPeter Hutterer * Thus, search from the current collection upwards until we find a
11185a4abb36SPeter Hutterer * logical collection. Then search all fields for that same parent
11195a4abb36SPeter Hutterer * collection. Those are the fields the multiplier applies to.
11205a4abb36SPeter Hutterer *
11215a4abb36SPeter Hutterer * If we have more than one multiplier, it will overwrite the
11225a4abb36SPeter Hutterer * applicable fields later.
11235a4abb36SPeter Hutterer */
11245a4abb36SPeter Hutterer multiplier_collection = &hid->collection[multiplier->usage->collection_index];
1125ee46967fSPeter Hutterer while (multiplier_collection->parent_idx != -1 &&
11265a4abb36SPeter Hutterer multiplier_collection->type != HID_COLLECTION_LOGICAL)
1127ee46967fSPeter Hutterer multiplier_collection = &hid->collection[multiplier_collection->parent_idx];
11285a4abb36SPeter Hutterer
11295a4abb36SPeter Hutterer effective_multiplier = hid_calculate_multiplier(hid, multiplier);
11305a4abb36SPeter Hutterer
11315a4abb36SPeter Hutterer rep_enum = &hid->report_enum[HID_INPUT_REPORT];
11325a4abb36SPeter Hutterer list_for_each_entry(rep, &rep_enum->report_list, list) {
11335a4abb36SPeter Hutterer for (i = 0; i < rep->maxfield; i++) {
11345a4abb36SPeter Hutterer field = rep->field[i];
11355a4abb36SPeter Hutterer hid_apply_multiplier_to_field(hid, field,
11365a4abb36SPeter Hutterer multiplier_collection,
11375a4abb36SPeter Hutterer effective_multiplier);
11385a4abb36SPeter Hutterer }
11395a4abb36SPeter Hutterer }
11405a4abb36SPeter Hutterer }
11415a4abb36SPeter Hutterer
11425a4abb36SPeter Hutterer /*
11435a4abb36SPeter Hutterer * hid_setup_resolution_multiplier - set up all resolution multipliers
11445a4abb36SPeter Hutterer *
11455a4abb36SPeter Hutterer * @device: hid device
11465a4abb36SPeter Hutterer *
11475a4abb36SPeter Hutterer * Search for all Resolution Multiplier Feature Reports and apply their
11485a4abb36SPeter Hutterer * value to all matching Input items. This only updates the internal struct
11495a4abb36SPeter Hutterer * fields.
11505a4abb36SPeter Hutterer *
11515a4abb36SPeter Hutterer * The Resolution Multiplier is applied by the hardware. If the multiplier
11525a4abb36SPeter Hutterer * is anything other than 1, the hardware will send pre-multiplied events
11535a4abb36SPeter Hutterer * so that the same physical interaction generates an accumulated
11545a4abb36SPeter Hutterer * accumulated_value = value * * multiplier
11555a4abb36SPeter Hutterer * This may be achieved by sending
11565a4abb36SPeter Hutterer * - "value * multiplier" for each event, or
11575a4abb36SPeter Hutterer * - "value" but "multiplier" times as frequently, or
11585a4abb36SPeter Hutterer * - a combination of the above
11595a4abb36SPeter Hutterer * The only guarantee is that the same physical interaction always generates
11605a4abb36SPeter Hutterer * an accumulated 'value * multiplier'.
11615a4abb36SPeter Hutterer *
11625a4abb36SPeter Hutterer * This function must be called before any event processing and after
11635a4abb36SPeter Hutterer * any SetRequest to the Resolution Multiplier.
11645a4abb36SPeter Hutterer */
hid_setup_resolution_multiplier(struct hid_device * hid)11655a4abb36SPeter Hutterer void hid_setup_resolution_multiplier(struct hid_device *hid)
11665a4abb36SPeter Hutterer {
11675a4abb36SPeter Hutterer struct hid_report_enum *rep_enum;
11685a4abb36SPeter Hutterer struct hid_report *rep;
11695a4abb36SPeter Hutterer struct hid_usage *usage;
11705a4abb36SPeter Hutterer int i, j;
11715a4abb36SPeter Hutterer
11725a4abb36SPeter Hutterer rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
11735a4abb36SPeter Hutterer list_for_each_entry(rep, &rep_enum->report_list, list) {
11745a4abb36SPeter Hutterer for (i = 0; i < rep->maxfield; i++) {
11755a4abb36SPeter Hutterer /* Ignore if report count is out of bounds. */
11765a4abb36SPeter Hutterer if (rep->field[i]->report_count < 1)
11775a4abb36SPeter Hutterer continue;
11785a4abb36SPeter Hutterer
11795a4abb36SPeter Hutterer for (j = 0; j < rep->field[i]->maxusage; j++) {
11805a4abb36SPeter Hutterer usage = &rep->field[i]->usage[j];
11815a4abb36SPeter Hutterer if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER)
11825a4abb36SPeter Hutterer hid_apply_multiplier(hid,
11835a4abb36SPeter Hutterer rep->field[i]);
11845a4abb36SPeter Hutterer }
11855a4abb36SPeter Hutterer }
11865a4abb36SPeter Hutterer }
11875a4abb36SPeter Hutterer }
11885a4abb36SPeter Hutterer EXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier);
11895a4abb36SPeter Hutterer
1190a7197c2eSHenrik Rydberg /**
1191a7197c2eSHenrik Rydberg * hid_open_report - open a driver-specific device report
1192a7197c2eSHenrik Rydberg *
1193a7197c2eSHenrik Rydberg * @device: hid device
1194a7197c2eSHenrik Rydberg *
1195dde5845aSJiri Kosina * Parse a report description into a hid_device structure. Reports are
1196dde5845aSJiri Kosina * enumerated, fields are attached to these reports.
119785cdaf52SJiri Slaby * 0 returned on success, otherwise nonzero error value.
1198a7197c2eSHenrik Rydberg *
1199a7197c2eSHenrik Rydberg * This function (or the equivalent hid_parse() macro) should only be
1200a7197c2eSHenrik Rydberg * called from probe() in drivers, before starting the device.
1201dde5845aSJiri Kosina */
hid_open_report(struct hid_device * device)1202a7197c2eSHenrik Rydberg int hid_open_report(struct hid_device *device)
1203dde5845aSJiri Kosina {
1204dde5845aSJiri Kosina struct hid_parser *parser;
1205dde5845aSJiri Kosina struct hid_item item;
1206a7197c2eSHenrik Rydberg unsigned int size;
1207a7197c2eSHenrik Rydberg __u8 *start;
120886e6b77eSKevin Daughtridge __u8 *buf;
1209dde5845aSJiri Kosina __u8 *end;
1210b3a81c77SMichał Mirosław __u8 *next;
121185cdaf52SJiri Slaby int ret;
1212ea427a22SXin Zhao int i;
1213dde5845aSJiri Kosina static int (*dispatch_type[])(struct hid_parser *parser,
1214dde5845aSJiri Kosina struct hid_item *item) = {
1215dde5845aSJiri Kosina hid_parser_main,
1216dde5845aSJiri Kosina hid_parser_global,
1217dde5845aSJiri Kosina hid_parser_local,
1218dde5845aSJiri Kosina hid_parser_reserved
1219dde5845aSJiri Kosina };
1220dde5845aSJiri Kosina
1221a7197c2eSHenrik Rydberg if (WARN_ON(device->status & HID_STAT_PARSED))
1222a7197c2eSHenrik Rydberg return -EBUSY;
1223a7197c2eSHenrik Rydberg
1224a7197c2eSHenrik Rydberg start = device->dev_rdesc;
1225a7197c2eSHenrik Rydberg if (WARN_ON(!start))
1226a7197c2eSHenrik Rydberg return -ENODEV;
1227a7197c2eSHenrik Rydberg size = device->dev_rsize;
1228a7197c2eSHenrik Rydberg
1229ad190df1SBenjamin Tissoires /* call_hid_bpf_rdesc_fixup() ensures we work on a copy of rdesc */
1230ad190df1SBenjamin Tissoires buf = call_hid_bpf_rdesc_fixup(device, start, &size);
123186e6b77eSKevin Daughtridge if (buf == NULL)
123285cdaf52SJiri Slaby return -ENOMEM;
123386e6b77eSKevin Daughtridge
123486e6b77eSKevin Daughtridge if (device->driver->report_fixup)
123586e6b77eSKevin Daughtridge start = device->driver->report_fixup(device, buf, &size);
123686e6b77eSKevin Daughtridge else
123786e6b77eSKevin Daughtridge start = buf;
123886e6b77eSKevin Daughtridge
123986e6b77eSKevin Daughtridge start = kmemdup(start, size, GFP_KERNEL);
124086e6b77eSKevin Daughtridge kfree(buf);
124186e6b77eSKevin Daughtridge if (start == NULL)
124286e6b77eSKevin Daughtridge return -ENOMEM;
124386e6b77eSKevin Daughtridge
124486e6b77eSKevin Daughtridge device->rdesc = start;
1245dde5845aSJiri Kosina device->rsize = size;
1246dde5845aSJiri Kosina
1247fe258020SJoe Perches parser = vzalloc(sizeof(struct hid_parser));
124885cdaf52SJiri Slaby if (!parser) {
124985cdaf52SJiri Slaby ret = -ENOMEM;
1250b034ed50SGustavo A. R. Silva goto alloc_err;
1251dde5845aSJiri Kosina }
125285cdaf52SJiri Slaby
1253dde5845aSJiri Kosina parser->device = device;
1254dde5845aSJiri Kosina
1255dde5845aSJiri Kosina end = start + size;
1256a7197c2eSHenrik Rydberg
1257a7197c2eSHenrik Rydberg device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1258a7197c2eSHenrik Rydberg sizeof(struct hid_collection), GFP_KERNEL);
1259a7197c2eSHenrik Rydberg if (!device->collection) {
1260a7197c2eSHenrik Rydberg ret = -ENOMEM;
1261a7197c2eSHenrik Rydberg goto err;
1262a7197c2eSHenrik Rydberg }
1263a7197c2eSHenrik Rydberg device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1264ea427a22SXin Zhao for (i = 0; i < HID_DEFAULT_NUM_COLLECTIONS; i++)
1265ea427a22SXin Zhao device->collection[i].parent_idx = -1;
1266a7197c2eSHenrik Rydberg
126785cdaf52SJiri Slaby ret = -EINVAL;
1268b3a81c77SMichał Mirosław while ((next = fetch_item(start, end, &item)) != NULL) {
1269b3a81c77SMichał Mirosław start = next;
1270dde5845aSJiri Kosina
1271dde5845aSJiri Kosina if (item.format != HID_ITEM_FORMAT_SHORT) {
12728c3d52fcSJiri Kosina hid_err(device, "unexpected long global item\n");
127385cdaf52SJiri Slaby goto err;
1274dde5845aSJiri Kosina }
1275dde5845aSJiri Kosina
1276dde5845aSJiri Kosina if (dispatch_type[item.type](parser, &item)) {
12778c3d52fcSJiri Kosina hid_err(device, "item %u %u %u %u parsing failed\n",
12784291ee30SJoe Perches item.format, (unsigned)item.size,
12794291ee30SJoe Perches (unsigned)item.type, (unsigned)item.tag);
128085cdaf52SJiri Slaby goto err;
1281dde5845aSJiri Kosina }
1282dde5845aSJiri Kosina
1283dde5845aSJiri Kosina if (start == end) {
1284dde5845aSJiri Kosina if (parser->collection_stack_ptr) {
12858c3d52fcSJiri Kosina hid_err(device, "unbalanced collection at end of report description\n");
128685cdaf52SJiri Slaby goto err;
1287dde5845aSJiri Kosina }
1288dde5845aSJiri Kosina if (parser->local.delimiter_depth) {
12898c3d52fcSJiri Kosina hid_err(device, "unbalanced delimiter at end of report description\n");
129085cdaf52SJiri Slaby goto err;
1291dde5845aSJiri Kosina }
12925a4abb36SPeter Hutterer
12935a4abb36SPeter Hutterer /*
12945a4abb36SPeter Hutterer * fetch initial values in case the device's
12955a4abb36SPeter Hutterer * default multiplier isn't the recommended 1
12965a4abb36SPeter Hutterer */
12975a4abb36SPeter Hutterer hid_setup_resolution_multiplier(device);
12985a4abb36SPeter Hutterer
1299b2dd9f2eSStefan Agner kfree(parser->collection_stack);
130047a80edbSJiri Kosina vfree(parser);
1301a7197c2eSHenrik Rydberg device->status |= HID_STAT_PARSED;
13025a4abb36SPeter Hutterer
130385cdaf52SJiri Slaby return 0;
1304dde5845aSJiri Kosina }
1305dde5845aSJiri Kosina }
1306dde5845aSJiri Kosina
1307b3a81c77SMichał Mirosław hid_err(device, "item fetching failed at offset %u/%u\n",
1308b3a81c77SMichał Mirosław size - (unsigned int)(end - start), size);
130985cdaf52SJiri Slaby err:
1310b2dd9f2eSStefan Agner kfree(parser->collection_stack);
1311b034ed50SGustavo A. R. Silva alloc_err:
131247a80edbSJiri Kosina vfree(parser);
1313a7197c2eSHenrik Rydberg hid_close_report(device);
131485cdaf52SJiri Slaby return ret;
1315dde5845aSJiri Kosina }
1316a7197c2eSHenrik Rydberg EXPORT_SYMBOL_GPL(hid_open_report);
1317dde5845aSJiri Kosina
1318dde5845aSJiri Kosina /*
1319dde5845aSJiri Kosina * Convert a signed n-bit integer to signed 32-bit integer. Common
1320dde5845aSJiri Kosina * cases are done through the compiler, the screwed things has to be
1321dde5845aSJiri Kosina * done by hand.
1322dde5845aSJiri Kosina */
1323dde5845aSJiri Kosina
snto32(__u32 value,unsigned n)1324dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n)
1325dde5845aSJiri Kosina {
1326a0312af1SRandy Dunlap if (!value || !n)
1327a0312af1SRandy Dunlap return 0;
1328a0312af1SRandy Dunlap
1329ec61b419SZhangPeng if (n > 32)
1330ec61b419SZhangPeng n = 32;
1331ec61b419SZhangPeng
1332dde5845aSJiri Kosina switch (n) {
1333dde5845aSJiri Kosina case 8: return ((__s8)value);
1334dde5845aSJiri Kosina case 16: return ((__s16)value);
1335dde5845aSJiri Kosina case 32: return ((__s32)value);
1336dde5845aSJiri Kosina }
133708585e43SAndy Shevchenko return value & (1 << (n - 1)) ? value | (~0U << n) : value;
1338dde5845aSJiri Kosina }
1339dde5845aSJiri Kosina
hid_snto32(__u32 value,unsigned n)134077463838SBenjamin Tissoires s32 hid_snto32(__u32 value, unsigned n)
134177463838SBenjamin Tissoires {
134277463838SBenjamin Tissoires return snto32(value, n);
134377463838SBenjamin Tissoires }
134477463838SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_snto32);
134577463838SBenjamin Tissoires
1346dde5845aSJiri Kosina /*
1347dde5845aSJiri Kosina * Convert a signed 32-bit integer to a signed n-bit integer.
1348dde5845aSJiri Kosina */
1349dde5845aSJiri Kosina
s32ton(__s32 value,unsigned n)1350dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n)
1351dde5845aSJiri Kosina {
1352dde5845aSJiri Kosina s32 a = value >> (n - 1);
1353dde5845aSJiri Kosina if (a && a != -1)
1354dde5845aSJiri Kosina return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1355dde5845aSJiri Kosina return value & ((1 << n) - 1);
1356dde5845aSJiri Kosina }
1357dde5845aSJiri Kosina
1358dde5845aSJiri Kosina /*
1359dde5845aSJiri Kosina * Extract/implement a data field from/to a little endian report (bit array).
1360dde5845aSJiri Kosina *
1361dde5845aSJiri Kosina * Code sort-of follows HID spec:
13625137b354SDmitry Torokhov * http://www.usb.org/developers/hidpage/HID1_11.pdf
1363dde5845aSJiri Kosina *
1364dde5845aSJiri Kosina * While the USB HID spec allows unlimited length bit fields in "report
1365dde5845aSJiri Kosina * descriptors", most devices never use more than 16 bits.
1366dde5845aSJiri Kosina * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1367dde5845aSJiri Kosina * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1368dde5845aSJiri Kosina */
1369dde5845aSJiri Kosina
__extract(u8 * report,unsigned offset,int n)13705137b354SDmitry Torokhov static u32 __extract(u8 *report, unsigned offset, int n)
13715137b354SDmitry Torokhov {
13725137b354SDmitry Torokhov unsigned int idx = offset / 8;
13735137b354SDmitry Torokhov unsigned int bit_nr = 0;
13745137b354SDmitry Torokhov unsigned int bit_shift = offset % 8;
13755137b354SDmitry Torokhov int bits_to_copy = 8 - bit_shift;
13765137b354SDmitry Torokhov u32 value = 0;
13775137b354SDmitry Torokhov u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
13785137b354SDmitry Torokhov
13795137b354SDmitry Torokhov while (n > 0) {
13805137b354SDmitry Torokhov value |= ((u32)report[idx] >> bit_shift) << bit_nr;
13815137b354SDmitry Torokhov n -= bits_to_copy;
13825137b354SDmitry Torokhov bit_nr += bits_to_copy;
13835137b354SDmitry Torokhov bits_to_copy = 8;
13845137b354SDmitry Torokhov bit_shift = 0;
13855137b354SDmitry Torokhov idx++;
13865137b354SDmitry Torokhov }
13875137b354SDmitry Torokhov
13885137b354SDmitry Torokhov return value & mask;
13895137b354SDmitry Torokhov }
13905137b354SDmitry Torokhov
hid_field_extract(const struct hid_device * hid,u8 * report,unsigned offset,unsigned n)13915137b354SDmitry Torokhov u32 hid_field_extract(const struct hid_device *hid, u8 *report,
13924291ee30SJoe Perches unsigned offset, unsigned n)
1393dde5845aSJiri Kosina {
139415fc1b5cSBenjamin Tissoires if (n > 32) {
13950af10eedSJoshua Clayton hid_warn_once(hid, "%s() called with n (%d) > 32! (%s)\n",
13960af10eedSJoshua Clayton __func__, n, current->comm);
139715fc1b5cSBenjamin Tissoires n = 32;
13985137b354SDmitry Torokhov }
1399dde5845aSJiri Kosina
14005137b354SDmitry Torokhov return __extract(report, offset, n);
1401dde5845aSJiri Kosina }
140204fba786SGoffredo Baroncelli EXPORT_SYMBOL_GPL(hid_field_extract);
1403dde5845aSJiri Kosina
1404dde5845aSJiri Kosina /*
1405dde5845aSJiri Kosina * "implement" : set bits in a little endian bit stream.
1406dde5845aSJiri Kosina * Same concepts as "extract" (see comments above).
1407dde5845aSJiri Kosina * The data mangled in the bit stream remains in little endian
1408dde5845aSJiri Kosina * order the whole time. It make more sense to talk about
1409dde5845aSJiri Kosina * endianness of register values by considering a register
14105137b354SDmitry Torokhov * a "cached" copy of the little endian bit stream.
1411dde5845aSJiri Kosina */
1412dde5845aSJiri Kosina
__implement(u8 * report,unsigned offset,int n,u32 value)14135137b354SDmitry Torokhov static void __implement(u8 *report, unsigned offset, int n, u32 value)
14145137b354SDmitry Torokhov {
14155137b354SDmitry Torokhov unsigned int idx = offset / 8;
14165137b354SDmitry Torokhov unsigned int bit_shift = offset % 8;
14175137b354SDmitry Torokhov int bits_to_set = 8 - bit_shift;
14185137b354SDmitry Torokhov
14195137b354SDmitry Torokhov while (n - bits_to_set >= 0) {
142095d1c895SDmitry Torokhov report[idx] &= ~(0xff << bit_shift);
14215137b354SDmitry Torokhov report[idx] |= value << bit_shift;
14225137b354SDmitry Torokhov value >>= bits_to_set;
14235137b354SDmitry Torokhov n -= bits_to_set;
14245137b354SDmitry Torokhov bits_to_set = 8;
14255137b354SDmitry Torokhov bit_shift = 0;
14265137b354SDmitry Torokhov idx++;
14275137b354SDmitry Torokhov }
14285137b354SDmitry Torokhov
14295137b354SDmitry Torokhov /* last nibble */
14305137b354SDmitry Torokhov if (n) {
143195d1c895SDmitry Torokhov u8 bit_mask = ((1U << n) - 1);
143295d1c895SDmitry Torokhov report[idx] &= ~(bit_mask << bit_shift);
143395d1c895SDmitry Torokhov report[idx] |= value << bit_shift;
14345137b354SDmitry Torokhov }
14355137b354SDmitry Torokhov }
14365137b354SDmitry Torokhov
implement(const struct hid_device * hid,u8 * report,unsigned offset,unsigned n,u32 value)14375137b354SDmitry Torokhov static void implement(const struct hid_device *hid, u8 *report,
14385137b354SDmitry Torokhov unsigned offset, unsigned n, u32 value)
14395137b354SDmitry Torokhov {
144095d1c895SDmitry Torokhov if (unlikely(n > 32)) {
14414291ee30SJoe Perches hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
14424291ee30SJoe Perches __func__, n, current->comm);
14435137b354SDmitry Torokhov n = 32;
144495d1c895SDmitry Torokhov } else if (n < 32) {
144595d1c895SDmitry Torokhov u32 m = (1U << n) - 1;
1446dde5845aSJiri Kosina
144795d1c895SDmitry Torokhov if (unlikely(value > m)) {
144895d1c895SDmitry Torokhov hid_warn(hid,
144995d1c895SDmitry Torokhov "%s() called with too large value %d (n: %d)! (%s)\n",
145095d1c895SDmitry Torokhov __func__, value, n, current->comm);
1451dde5845aSJiri Kosina value &= m;
145295d1c895SDmitry Torokhov }
145395d1c895SDmitry Torokhov }
1454dde5845aSJiri Kosina
14555137b354SDmitry Torokhov __implement(report, offset, n, value);
1456dde5845aSJiri Kosina }
1457dde5845aSJiri Kosina
1458dde5845aSJiri Kosina /*
1459dde5845aSJiri Kosina * Search an array for a value.
1460dde5845aSJiri Kosina */
1461dde5845aSJiri Kosina
search(__s32 * array,__s32 value,unsigned n)146216ee4cc8SJoe Perches static int search(__s32 *array, __s32 value, unsigned n)
1463dde5845aSJiri Kosina {
1464dde5845aSJiri Kosina while (n--) {
1465dde5845aSJiri Kosina if (*array++ == value)
1466dde5845aSJiri Kosina return 0;
1467dde5845aSJiri Kosina }
1468dde5845aSJiri Kosina return -1;
1469dde5845aSJiri Kosina }
1470dde5845aSJiri Kosina
147185cdaf52SJiri Slaby /**
147285cdaf52SJiri Slaby * hid_match_report - check if driver's raw_event should be called
147385cdaf52SJiri Slaby *
147485cdaf52SJiri Slaby * @hid: hid device
14755f94e9c8SXiaofei Tan * @report: hid report to match against
147685cdaf52SJiri Slaby *
147785cdaf52SJiri Slaby * compare hid->driver->report_table->report_type to report->type
147885cdaf52SJiri Slaby */
hid_match_report(struct hid_device * hid,struct hid_report * report)147985cdaf52SJiri Slaby static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1480dde5845aSJiri Kosina {
148185cdaf52SJiri Slaby const struct hid_report_id *id = hid->driver->report_table;
148285cdaf52SJiri Slaby
148385cdaf52SJiri Slaby if (!id) /* NULL means all */
148485cdaf52SJiri Slaby return 1;
148585cdaf52SJiri Slaby
148685cdaf52SJiri Slaby for (; id->report_type != HID_TERMINATOR; id++)
148785cdaf52SJiri Slaby if (id->report_type == HID_ANY_ID ||
148885cdaf52SJiri Slaby id->report_type == report->type)
148985cdaf52SJiri Slaby return 1;
149085cdaf52SJiri Slaby return 0;
149185cdaf52SJiri Slaby }
149285cdaf52SJiri Slaby
149385cdaf52SJiri Slaby /**
149485cdaf52SJiri Slaby * hid_match_usage - check if driver's event should be called
149585cdaf52SJiri Slaby *
149685cdaf52SJiri Slaby * @hid: hid device
149785cdaf52SJiri Slaby * @usage: usage to match against
149885cdaf52SJiri Slaby *
149985cdaf52SJiri Slaby * compare hid->driver->usage_table->usage_{type,code} to
150085cdaf52SJiri Slaby * usage->usage_{type,code}
150185cdaf52SJiri Slaby */
hid_match_usage(struct hid_device * hid,struct hid_usage * usage)150285cdaf52SJiri Slaby static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
150385cdaf52SJiri Slaby {
150485cdaf52SJiri Slaby const struct hid_usage_id *id = hid->driver->usage_table;
150585cdaf52SJiri Slaby
150685cdaf52SJiri Slaby if (!id) /* NULL means all */
150785cdaf52SJiri Slaby return 1;
150885cdaf52SJiri Slaby
150985cdaf52SJiri Slaby for (; id->usage_type != HID_ANY_ID - 1; id++)
151085cdaf52SJiri Slaby if ((id->usage_hid == HID_ANY_ID ||
151185cdaf52SJiri Slaby id->usage_hid == usage->hid) &&
151285cdaf52SJiri Slaby (id->usage_type == HID_ANY_ID ||
151385cdaf52SJiri Slaby id->usage_type == usage->type) &&
151485cdaf52SJiri Slaby (id->usage_code == HID_ANY_ID ||
151585cdaf52SJiri Slaby id->usage_code == usage->code))
151685cdaf52SJiri Slaby return 1;
151785cdaf52SJiri Slaby return 0;
151885cdaf52SJiri Slaby }
151985cdaf52SJiri Slaby
hid_process_event(struct hid_device * hid,struct hid_field * field,struct hid_usage * usage,__s32 value,int interrupt)152085cdaf52SJiri Slaby static void hid_process_event(struct hid_device *hid, struct hid_field *field,
152185cdaf52SJiri Slaby struct hid_usage *usage, __s32 value, int interrupt)
152285cdaf52SJiri Slaby {
152385cdaf52SJiri Slaby struct hid_driver *hdrv = hid->driver;
152485cdaf52SJiri Slaby int ret;
152585cdaf52SJiri Slaby
15269bfc8da0SHenrik Rydberg if (!list_empty(&hid->debug_list))
1527cd667ce2SJiri Kosina hid_dump_input(hid, usage, value);
152885cdaf52SJiri Slaby
152985cdaf52SJiri Slaby if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
153085cdaf52SJiri Slaby ret = hdrv->event(hid, field, usage, value);
153185cdaf52SJiri Slaby if (ret != 0) {
153285cdaf52SJiri Slaby if (ret < 0)
15338c3d52fcSJiri Kosina hid_err(hid, "%s's event failed with %d\n",
153485cdaf52SJiri Slaby hdrv->name, ret);
153585cdaf52SJiri Slaby return;
153685cdaf52SJiri Slaby }
153785cdaf52SJiri Slaby }
153885cdaf52SJiri Slaby
1539dde5845aSJiri Kosina if (hid->claimed & HID_CLAIMED_INPUT)
1540dde5845aSJiri Kosina hidinput_hid_event(hid, field, usage, value);
1541aa938f79SJiri Kosina if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1542aa938f79SJiri Kosina hid->hiddev_hid_event(hid, field, usage, value);
1543dde5845aSJiri Kosina }
1544dde5845aSJiri Kosina
1545dde5845aSJiri Kosina /*
154674acc277SBenjamin Tissoires * Checks if the given value is valid within this field
154774acc277SBenjamin Tissoires */
hid_array_value_is_valid(struct hid_field * field,__s32 value)154874acc277SBenjamin Tissoires static inline int hid_array_value_is_valid(struct hid_field *field,
154974acc277SBenjamin Tissoires __s32 value)
155074acc277SBenjamin Tissoires {
155174acc277SBenjamin Tissoires __s32 min = field->logical_minimum;
155274acc277SBenjamin Tissoires
155374acc277SBenjamin Tissoires /*
155474acc277SBenjamin Tissoires * Value needs to be between logical min and max, and
155574acc277SBenjamin Tissoires * (value - min) is used as an index in the usage array.
155674acc277SBenjamin Tissoires * This array is of size field->maxusage
155774acc277SBenjamin Tissoires */
155874acc277SBenjamin Tissoires return value >= min &&
155974acc277SBenjamin Tissoires value <= field->logical_maximum &&
156074acc277SBenjamin Tissoires value - min < field->maxusage;
156174acc277SBenjamin Tissoires }
156274acc277SBenjamin Tissoires
156374acc277SBenjamin Tissoires /*
1564b79c1abaSBenjamin Tissoires * Fetch the field from the data. The field content is stored for next
1565b79c1abaSBenjamin Tissoires * report processing (we do differential reporting to the layer).
1566dde5845aSJiri Kosina */
hid_input_fetch_field(struct hid_device * hid,struct hid_field * field,__u8 * data)1567b79c1abaSBenjamin Tissoires static void hid_input_fetch_field(struct hid_device *hid,
1568b79c1abaSBenjamin Tissoires struct hid_field *field,
1569b79c1abaSBenjamin Tissoires __u8 *data)
1570dde5845aSJiri Kosina {
1571dde5845aSJiri Kosina unsigned n;
1572dde5845aSJiri Kosina unsigned count = field->report_count;
1573dde5845aSJiri Kosina unsigned offset = field->report_offset;
1574dde5845aSJiri Kosina unsigned size = field->report_size;
1575dde5845aSJiri Kosina __s32 min = field->logical_minimum;
1576dde5845aSJiri Kosina __s32 *value;
1577dde5845aSJiri Kosina
15781c1813a7SBenjamin Tissoires value = field->new_value;
15791c1813a7SBenjamin Tissoires memset(value, 0, count * sizeof(__s32));
1580b79c1abaSBenjamin Tissoires field->ignored = false;
1581dde5845aSJiri Kosina
1582dde5845aSJiri Kosina for (n = 0; n < count; n++) {
1583dde5845aSJiri Kosina
15844291ee30SJoe Perches value[n] = min < 0 ?
158504fba786SGoffredo Baroncelli snto32(hid_field_extract(hid, data, offset + n * size,
158604fba786SGoffredo Baroncelli size), size) :
158704fba786SGoffredo Baroncelli hid_field_extract(hid, data, offset + n * size, size);
1588dde5845aSJiri Kosina
15894291ee30SJoe Perches /* Ignore report if ErrorRollOver */
15904291ee30SJoe Perches if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
159174acc277SBenjamin Tissoires hid_array_value_is_valid(field, value[n]) &&
1592b79c1abaSBenjamin Tissoires field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) {
1593b79c1abaSBenjamin Tissoires field->ignored = true;
15941c1813a7SBenjamin Tissoires return;
1595dde5845aSJiri Kosina }
1596b79c1abaSBenjamin Tissoires }
1597b79c1abaSBenjamin Tissoires }
1598dde5845aSJiri Kosina
1599b79c1abaSBenjamin Tissoires /*
1600b79c1abaSBenjamin Tissoires * Process a received variable field.
1601b79c1abaSBenjamin Tissoires */
1602dde5845aSJiri Kosina
hid_input_var_field(struct hid_device * hid,struct hid_field * field,int interrupt)1603b79c1abaSBenjamin Tissoires static void hid_input_var_field(struct hid_device *hid,
1604b79c1abaSBenjamin Tissoires struct hid_field *field,
1605b79c1abaSBenjamin Tissoires int interrupt)
1606b79c1abaSBenjamin Tissoires {
1607b79c1abaSBenjamin Tissoires unsigned int count = field->report_count;
1608b79c1abaSBenjamin Tissoires __s32 *value = field->new_value;
1609b79c1abaSBenjamin Tissoires unsigned int n;
1610b79c1abaSBenjamin Tissoires
1611b79c1abaSBenjamin Tissoires for (n = 0; n < count; n++)
161274acc277SBenjamin Tissoires hid_process_event(hid,
161374acc277SBenjamin Tissoires field,
161474acc277SBenjamin Tissoires &field->usage[n],
161574acc277SBenjamin Tissoires value[n],
161674acc277SBenjamin Tissoires interrupt);
1617b79c1abaSBenjamin Tissoires
1618b79c1abaSBenjamin Tissoires memcpy(field->value, value, count * sizeof(__s32));
1619dde5845aSJiri Kosina }
1620dde5845aSJiri Kosina
1621b79c1abaSBenjamin Tissoires /*
1622b79c1abaSBenjamin Tissoires * Process a received array field. The field content is stored for
1623b79c1abaSBenjamin Tissoires * next report processing (we do differential reporting to the layer).
1624b79c1abaSBenjamin Tissoires */
1625b79c1abaSBenjamin Tissoires
hid_input_array_field(struct hid_device * hid,struct hid_field * field,int interrupt)1626b79c1abaSBenjamin Tissoires static void hid_input_array_field(struct hid_device *hid,
1627b79c1abaSBenjamin Tissoires struct hid_field *field,
1628b79c1abaSBenjamin Tissoires int interrupt)
1629b79c1abaSBenjamin Tissoires {
1630b79c1abaSBenjamin Tissoires unsigned int n;
1631b79c1abaSBenjamin Tissoires unsigned int count = field->report_count;
1632b79c1abaSBenjamin Tissoires __s32 min = field->logical_minimum;
1633b79c1abaSBenjamin Tissoires __s32 *value;
1634b79c1abaSBenjamin Tissoires
1635b79c1abaSBenjamin Tissoires value = field->new_value;
1636b79c1abaSBenjamin Tissoires
1637b79c1abaSBenjamin Tissoires /* ErrorRollOver */
1638b79c1abaSBenjamin Tissoires if (field->ignored)
1639b79c1abaSBenjamin Tissoires return;
1640b79c1abaSBenjamin Tissoires
1641b79c1abaSBenjamin Tissoires for (n = 0; n < count; n++) {
164274acc277SBenjamin Tissoires if (hid_array_value_is_valid(field, field->value[n]) &&
164374acc277SBenjamin Tissoires search(value, field->value[n], count))
164474acc277SBenjamin Tissoires hid_process_event(hid,
164574acc277SBenjamin Tissoires field,
164674acc277SBenjamin Tissoires &field->usage[field->value[n] - min],
164774acc277SBenjamin Tissoires 0,
164874acc277SBenjamin Tissoires interrupt);
1649dde5845aSJiri Kosina
165074acc277SBenjamin Tissoires if (hid_array_value_is_valid(field, value[n]) &&
165174acc277SBenjamin Tissoires search(field->value, value[n], count))
165274acc277SBenjamin Tissoires hid_process_event(hid,
165374acc277SBenjamin Tissoires field,
165474acc277SBenjamin Tissoires &field->usage[value[n] - min],
165574acc277SBenjamin Tissoires 1,
165674acc277SBenjamin Tissoires interrupt);
1657dde5845aSJiri Kosina }
1658dde5845aSJiri Kosina
1659dde5845aSJiri Kosina memcpy(field->value, value, count * sizeof(__s32));
1660dde5845aSJiri Kosina }
1661dde5845aSJiri Kosina
1662dde5845aSJiri Kosina /*
1663b79c1abaSBenjamin Tissoires * Analyse a received report, and fetch the data from it. The field
1664b79c1abaSBenjamin Tissoires * content is stored for next report processing (we do differential
1665b79c1abaSBenjamin Tissoires * reporting to the layer).
1666b79c1abaSBenjamin Tissoires */
hid_process_report(struct hid_device * hid,struct hid_report * report,__u8 * data,int interrupt)1667b79c1abaSBenjamin Tissoires static void hid_process_report(struct hid_device *hid,
1668b79c1abaSBenjamin Tissoires struct hid_report *report,
1669b79c1abaSBenjamin Tissoires __u8 *data,
1670b79c1abaSBenjamin Tissoires int interrupt)
1671b79c1abaSBenjamin Tissoires {
1672b79c1abaSBenjamin Tissoires unsigned int a;
1673bebcc522SBenjamin Tissoires struct hid_field_entry *entry;
1674b79c1abaSBenjamin Tissoires struct hid_field *field;
1675b79c1abaSBenjamin Tissoires
1676bebcc522SBenjamin Tissoires /* first retrieve all incoming values in data */
1677bebcc522SBenjamin Tissoires for (a = 0; a < report->maxfield; a++)
16784df4b0feSLukas Bulwahn hid_input_fetch_field(hid, report->field[a], data);
1679bebcc522SBenjamin Tissoires
1680bebcc522SBenjamin Tissoires if (!list_empty(&report->field_entry_list)) {
1681bebcc522SBenjamin Tissoires /* INPUT_REPORT, we have a priority list of fields */
1682bebcc522SBenjamin Tissoires list_for_each_entry(entry,
1683bebcc522SBenjamin Tissoires &report->field_entry_list,
1684bebcc522SBenjamin Tissoires list) {
1685bebcc522SBenjamin Tissoires field = entry->field;
1686bebcc522SBenjamin Tissoires
1687bebcc522SBenjamin Tissoires if (field->flags & HID_MAIN_ITEM_VARIABLE)
1688bebcc522SBenjamin Tissoires hid_process_event(hid,
1689bebcc522SBenjamin Tissoires field,
1690bebcc522SBenjamin Tissoires &field->usage[entry->index],
1691bebcc522SBenjamin Tissoires field->new_value[entry->index],
1692bebcc522SBenjamin Tissoires interrupt);
1693bebcc522SBenjamin Tissoires else
1694bebcc522SBenjamin Tissoires hid_input_array_field(hid, field, interrupt);
1695bebcc522SBenjamin Tissoires }
1696bebcc522SBenjamin Tissoires
1697bebcc522SBenjamin Tissoires /* we need to do the memcpy at the end for var items */
1698b79c1abaSBenjamin Tissoires for (a = 0; a < report->maxfield; a++) {
1699b79c1abaSBenjamin Tissoires field = report->field[a];
1700b79c1abaSBenjamin Tissoires
1701bebcc522SBenjamin Tissoires if (field->flags & HID_MAIN_ITEM_VARIABLE)
1702bebcc522SBenjamin Tissoires memcpy(field->value, field->new_value,
1703bebcc522SBenjamin Tissoires field->report_count * sizeof(__s32));
1704bebcc522SBenjamin Tissoires }
1705bebcc522SBenjamin Tissoires } else {
1706bebcc522SBenjamin Tissoires /* FEATURE_REPORT, regular processing */
1707bebcc522SBenjamin Tissoires for (a = 0; a < report->maxfield; a++) {
1708bebcc522SBenjamin Tissoires field = report->field[a];
1709b79c1abaSBenjamin Tissoires
1710b79c1abaSBenjamin Tissoires if (field->flags & HID_MAIN_ITEM_VARIABLE)
1711b79c1abaSBenjamin Tissoires hid_input_var_field(hid, field, interrupt);
1712b79c1abaSBenjamin Tissoires else
1713b79c1abaSBenjamin Tissoires hid_input_array_field(hid, field, interrupt);
1714b79c1abaSBenjamin Tissoires }
1715b79c1abaSBenjamin Tissoires }
1716bebcc522SBenjamin Tissoires }
1717b79c1abaSBenjamin Tissoires
1718b79c1abaSBenjamin Tissoires /*
171922f4b026SBenjamin Tissoires * Insert a given usage_index in a field in the list
172022f4b026SBenjamin Tissoires * of processed usages in the report.
172122f4b026SBenjamin Tissoires *
172222f4b026SBenjamin Tissoires * The elements of lower priority score are processed
172322f4b026SBenjamin Tissoires * first.
172422f4b026SBenjamin Tissoires */
__hid_insert_field_entry(struct hid_device * hid,struct hid_report * report,struct hid_field_entry * entry,struct hid_field * field,unsigned int usage_index)172522f4b026SBenjamin Tissoires static void __hid_insert_field_entry(struct hid_device *hid,
172622f4b026SBenjamin Tissoires struct hid_report *report,
172722f4b026SBenjamin Tissoires struct hid_field_entry *entry,
172822f4b026SBenjamin Tissoires struct hid_field *field,
172922f4b026SBenjamin Tissoires unsigned int usage_index)
173022f4b026SBenjamin Tissoires {
173122f4b026SBenjamin Tissoires struct hid_field_entry *next;
173222f4b026SBenjamin Tissoires
173322f4b026SBenjamin Tissoires entry->field = field;
173422f4b026SBenjamin Tissoires entry->index = usage_index;
173522f4b026SBenjamin Tissoires entry->priority = field->usages_priorities[usage_index];
173622f4b026SBenjamin Tissoires
173722f4b026SBenjamin Tissoires /* insert the element at the correct position */
173822f4b026SBenjamin Tissoires list_for_each_entry(next,
173922f4b026SBenjamin Tissoires &report->field_entry_list,
174022f4b026SBenjamin Tissoires list) {
174122f4b026SBenjamin Tissoires /*
174222f4b026SBenjamin Tissoires * the priority of our element is strictly higher
174322f4b026SBenjamin Tissoires * than the next one, insert it before
174422f4b026SBenjamin Tissoires */
174522f4b026SBenjamin Tissoires if (entry->priority > next->priority) {
174622f4b026SBenjamin Tissoires list_add_tail(&entry->list, &next->list);
174722f4b026SBenjamin Tissoires return;
174822f4b026SBenjamin Tissoires }
174922f4b026SBenjamin Tissoires }
175022f4b026SBenjamin Tissoires
175122f4b026SBenjamin Tissoires /* lowest priority score: insert at the end */
175222f4b026SBenjamin Tissoires list_add_tail(&entry->list, &report->field_entry_list);
175322f4b026SBenjamin Tissoires }
175422f4b026SBenjamin Tissoires
hid_report_process_ordering(struct hid_device * hid,struct hid_report * report)175522f4b026SBenjamin Tissoires static void hid_report_process_ordering(struct hid_device *hid,
175622f4b026SBenjamin Tissoires struct hid_report *report)
175722f4b026SBenjamin Tissoires {
175822f4b026SBenjamin Tissoires struct hid_field *field;
175922f4b026SBenjamin Tissoires struct hid_field_entry *entries;
176022f4b026SBenjamin Tissoires unsigned int a, u, usages;
176122f4b026SBenjamin Tissoires unsigned int count = 0;
176222f4b026SBenjamin Tissoires
176322f4b026SBenjamin Tissoires /* count the number of individual fields in the report */
176422f4b026SBenjamin Tissoires for (a = 0; a < report->maxfield; a++) {
176522f4b026SBenjamin Tissoires field = report->field[a];
176622f4b026SBenjamin Tissoires
176722f4b026SBenjamin Tissoires if (field->flags & HID_MAIN_ITEM_VARIABLE)
176822f4b026SBenjamin Tissoires count += field->report_count;
176922f4b026SBenjamin Tissoires else
177022f4b026SBenjamin Tissoires count++;
177122f4b026SBenjamin Tissoires }
177222f4b026SBenjamin Tissoires
177322f4b026SBenjamin Tissoires /* allocate the memory to process the fields */
177422f4b026SBenjamin Tissoires entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
177522f4b026SBenjamin Tissoires if (!entries)
177622f4b026SBenjamin Tissoires return;
177722f4b026SBenjamin Tissoires
177822f4b026SBenjamin Tissoires report->field_entries = entries;
177922f4b026SBenjamin Tissoires
178022f4b026SBenjamin Tissoires /*
178122f4b026SBenjamin Tissoires * walk through all fields in the report and
178222f4b026SBenjamin Tissoires * store them by priority order in report->field_entry_list
178322f4b026SBenjamin Tissoires *
178422f4b026SBenjamin Tissoires * - Var elements are individualized (field + usage_index)
178522f4b026SBenjamin Tissoires * - Arrays are taken as one, we can not chose an order for them
178622f4b026SBenjamin Tissoires */
178722f4b026SBenjamin Tissoires usages = 0;
178822f4b026SBenjamin Tissoires for (a = 0; a < report->maxfield; a++) {
178922f4b026SBenjamin Tissoires field = report->field[a];
179022f4b026SBenjamin Tissoires
179122f4b026SBenjamin Tissoires if (field->flags & HID_MAIN_ITEM_VARIABLE) {
179222f4b026SBenjamin Tissoires for (u = 0; u < field->report_count; u++) {
179322f4b026SBenjamin Tissoires __hid_insert_field_entry(hid, report,
179422f4b026SBenjamin Tissoires &entries[usages],
179522f4b026SBenjamin Tissoires field, u);
179622f4b026SBenjamin Tissoires usages++;
179722f4b026SBenjamin Tissoires }
179822f4b026SBenjamin Tissoires } else {
179922f4b026SBenjamin Tissoires __hid_insert_field_entry(hid, report, &entries[usages],
180022f4b026SBenjamin Tissoires field, 0);
180122f4b026SBenjamin Tissoires usages++;
180222f4b026SBenjamin Tissoires }
180322f4b026SBenjamin Tissoires }
180422f4b026SBenjamin Tissoires }
180522f4b026SBenjamin Tissoires
hid_process_ordering(struct hid_device * hid)180622f4b026SBenjamin Tissoires static void hid_process_ordering(struct hid_device *hid)
180722f4b026SBenjamin Tissoires {
180822f4b026SBenjamin Tissoires struct hid_report *report;
180922f4b026SBenjamin Tissoires struct hid_report_enum *report_enum = &hid->report_enum[HID_INPUT_REPORT];
181022f4b026SBenjamin Tissoires
181122f4b026SBenjamin Tissoires list_for_each_entry(report, &report_enum->report_list, list)
181222f4b026SBenjamin Tissoires hid_report_process_ordering(hid, report);
181322f4b026SBenjamin Tissoires }
181422f4b026SBenjamin Tissoires
181522f4b026SBenjamin Tissoires /*
1816dde5845aSJiri Kosina * Output the field into the report.
1817dde5845aSJiri Kosina */
1818dde5845aSJiri Kosina
hid_output_field(const struct hid_device * hid,struct hid_field * field,__u8 * data)18194291ee30SJoe Perches static void hid_output_field(const struct hid_device *hid,
18204291ee30SJoe Perches struct hid_field *field, __u8 *data)
1821dde5845aSJiri Kosina {
1822dde5845aSJiri Kosina unsigned count = field->report_count;
1823dde5845aSJiri Kosina unsigned offset = field->report_offset;
1824dde5845aSJiri Kosina unsigned size = field->report_size;
1825dde5845aSJiri Kosina unsigned n;
1826dde5845aSJiri Kosina
1827dde5845aSJiri Kosina for (n = 0; n < count; n++) {
1828dde5845aSJiri Kosina if (field->logical_minimum < 0) /* signed values */
18294291ee30SJoe Perches implement(hid, data, offset + n * size, size,
18304291ee30SJoe Perches s32ton(field->value[n], size));
1831dde5845aSJiri Kosina else /* unsigned values */
18324291ee30SJoe Perches implement(hid, data, offset + n * size, size,
18334291ee30SJoe Perches field->value[n]);
1834dde5845aSJiri Kosina }
1835dde5845aSJiri Kosina }
1836dde5845aSJiri Kosina
1837dde5845aSJiri Kosina /*
1838bce1305cSMarc Zyngier * Compute the size of a report.
1839bce1305cSMarc Zyngier */
hid_compute_report_size(struct hid_report * report)1840bce1305cSMarc Zyngier static size_t hid_compute_report_size(struct hid_report *report)
1841bce1305cSMarc Zyngier {
1842bce1305cSMarc Zyngier if (report->size)
1843bce1305cSMarc Zyngier return ((report->size - 1) >> 3) + 1;
1844bce1305cSMarc Zyngier
1845bce1305cSMarc Zyngier return 0;
1846bce1305cSMarc Zyngier }
1847bce1305cSMarc Zyngier
1848bce1305cSMarc Zyngier /*
184927ce4050SJiri Kosina * Create a report. 'data' has to be allocated using
185027ce4050SJiri Kosina * hid_alloc_report_buf() so that it has proper size.
1851dde5845aSJiri Kosina */
1852dde5845aSJiri Kosina
hid_output_report(struct hid_report * report,__u8 * data)1853229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data)
1854dde5845aSJiri Kosina {
1855dde5845aSJiri Kosina unsigned n;
1856dde5845aSJiri Kosina
1857dde5845aSJiri Kosina if (report->id > 0)
1858dde5845aSJiri Kosina *data++ = report->id;
1859dde5845aSJiri Kosina
1860bce1305cSMarc Zyngier memset(data, 0, hid_compute_report_size(report));
1861dde5845aSJiri Kosina for (n = 0; n < report->maxfield; n++)
18624291ee30SJoe Perches hid_output_field(report->device, report->field[n], data);
1863dde5845aSJiri Kosina }
1864229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report);
1865dde5845aSJiri Kosina
1866dde5845aSJiri Kosina /*
186727ce4050SJiri Kosina * Allocator for buffer that is going to be passed to hid_output_report()
186827ce4050SJiri Kosina */
hid_alloc_report_buf(struct hid_report * report,gfp_t flags)186927ce4050SJiri Kosina u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
187027ce4050SJiri Kosina {
187127ce4050SJiri Kosina /*
187227ce4050SJiri Kosina * 7 extra bytes are necessary to achieve proper functionality
187327ce4050SJiri Kosina * of implement() working on 8 byte chunks
187427ce4050SJiri Kosina */
187527ce4050SJiri Kosina
18766de0b13cSAaron Ma u32 len = hid_report_len(report) + 7;
187727ce4050SJiri Kosina
18789d9f5c75SJiri Kosina return kzalloc(len, flags);
187927ce4050SJiri Kosina }
188027ce4050SJiri Kosina EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
188127ce4050SJiri Kosina
188227ce4050SJiri Kosina /*
1883dde5845aSJiri Kosina * Set a field value. The report this field belongs to has to be
1884dde5845aSJiri Kosina * created and transferred to the device, to set this value in the
1885dde5845aSJiri Kosina * device.
1886dde5845aSJiri Kosina */
1887dde5845aSJiri Kosina
hid_set_field(struct hid_field * field,unsigned offset,__s32 value)1888dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1889dde5845aSJiri Kosina {
1890be67b68dSKees Cook unsigned size;
1891be67b68dSKees Cook
1892be67b68dSKees Cook if (!field)
1893be67b68dSKees Cook return -1;
1894be67b68dSKees Cook
1895be67b68dSKees Cook size = field->report_size;
1896dde5845aSJiri Kosina
1897cd667ce2SJiri Kosina hid_dump_input(field->report->device, field->usage + offset, value);
1898dde5845aSJiri Kosina
1899dde5845aSJiri Kosina if (offset >= field->report_count) {
19008c3d52fcSJiri Kosina hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
19018c3d52fcSJiri Kosina offset, field->report_count);
1902dde5845aSJiri Kosina return -1;
1903dde5845aSJiri Kosina }
1904dde5845aSJiri Kosina if (field->logical_minimum < 0) {
1905dde5845aSJiri Kosina if (value != snto32(s32ton(value, size), size)) {
19068c3d52fcSJiri Kosina hid_err(field->report->device, "value %d is out of range\n", value);
1907dde5845aSJiri Kosina return -1;
1908dde5845aSJiri Kosina }
1909dde5845aSJiri Kosina }
1910dde5845aSJiri Kosina field->value[offset] = value;
1911dde5845aSJiri Kosina return 0;
1912dde5845aSJiri Kosina }
1913229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field);
1914dde5845aSJiri Kosina
hid_get_report(struct hid_report_enum * report_enum,const u8 * data)191585cdaf52SJiri Slaby static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
191685cdaf52SJiri Slaby const u8 *data)
191785cdaf52SJiri Slaby {
191885cdaf52SJiri Slaby struct hid_report *report;
191985cdaf52SJiri Slaby unsigned int n = 0; /* Normally report number is 0 */
192085cdaf52SJiri Slaby
192185cdaf52SJiri Slaby /* Device uses numbered reports, data[0] is report number */
192285cdaf52SJiri Slaby if (report_enum->numbered)
192385cdaf52SJiri Slaby n = *data;
192485cdaf52SJiri Slaby
192585cdaf52SJiri Slaby report = report_enum->report_id_hash[n];
192685cdaf52SJiri Slaby if (report == NULL)
192785cdaf52SJiri Slaby dbg_hid("undefined report_id %u received\n", n);
192885cdaf52SJiri Slaby
192985cdaf52SJiri Slaby return report;
193085cdaf52SJiri Slaby }
193185cdaf52SJiri Slaby
19324fa5a7f7SBenjamin Tissoires /*
19334fa5a7f7SBenjamin Tissoires * Implement a generic .request() callback, using .raw_request()
19344fa5a7f7SBenjamin Tissoires * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
19354fa5a7f7SBenjamin Tissoires */
__hid_request(struct hid_device * hid,struct hid_report * report,enum hid_class_request reqtype)1936d43c17eaSBenjamin Tissoires int __hid_request(struct hid_device *hid, struct hid_report *report,
1937735e1bb1SBenjamin Tissoires enum hid_class_request reqtype)
19384fa5a7f7SBenjamin Tissoires {
19394fa5a7f7SBenjamin Tissoires char *buf;
19404fa5a7f7SBenjamin Tissoires int ret;
19416de0b13cSAaron Ma u32 len;
19424fa5a7f7SBenjamin Tissoires
19434fa5a7f7SBenjamin Tissoires buf = hid_alloc_report_buf(report, GFP_KERNEL);
19444fa5a7f7SBenjamin Tissoires if (!buf)
1945d43c17eaSBenjamin Tissoires return -ENOMEM;
19464fa5a7f7SBenjamin Tissoires
19474fa5a7f7SBenjamin Tissoires len = hid_report_len(report);
19484fa5a7f7SBenjamin Tissoires
19494fa5a7f7SBenjamin Tissoires if (reqtype == HID_REQ_SET_REPORT)
19504fa5a7f7SBenjamin Tissoires hid_output_report(report, buf);
19514fa5a7f7SBenjamin Tissoires
19524fa5a7f7SBenjamin Tissoires ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
19534fa5a7f7SBenjamin Tissoires report->type, reqtype);
19544fa5a7f7SBenjamin Tissoires if (ret < 0) {
19554fa5a7f7SBenjamin Tissoires dbg_hid("unable to complete request: %d\n", ret);
19564fa5a7f7SBenjamin Tissoires goto out;
19574fa5a7f7SBenjamin Tissoires }
19584fa5a7f7SBenjamin Tissoires
19594fa5a7f7SBenjamin Tissoires if (reqtype == HID_REQ_GET_REPORT)
19604fa5a7f7SBenjamin Tissoires hid_input_report(hid, report->type, buf, ret, 0);
19614fa5a7f7SBenjamin Tissoires
1962d43c17eaSBenjamin Tissoires ret = 0;
1963d43c17eaSBenjamin Tissoires
19644fa5a7f7SBenjamin Tissoires out:
19654fa5a7f7SBenjamin Tissoires kfree(buf);
1966d43c17eaSBenjamin Tissoires return ret;
19674fa5a7f7SBenjamin Tissoires }
19684fa5a7f7SBenjamin Tissoires EXPORT_SYMBOL_GPL(__hid_request);
19694fa5a7f7SBenjamin Tissoires
hid_report_raw_event(struct hid_device * hid,enum hid_report_type type,u8 * data,u32 size,int interrupt)1970ead77b65SBenjamin Tissoires int hid_report_raw_event(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
197185cdaf52SJiri Slaby int interrupt)
1972aa8de2f0SJiri Kosina {
1973aa8de2f0SJiri Kosina struct hid_report_enum *report_enum = hid->report_enum + type;
1974aa8de2f0SJiri Kosina struct hid_report *report;
19756d85d037SBenjamin Tissoires struct hid_driver *hdrv;
1976b1a37ed0SLee Jones int max_buffer_size = HID_MAX_BUFFER_SIZE;
19776de0b13cSAaron Ma u32 rsize, csize = size;
197885cdaf52SJiri Slaby u8 *cdata = data;
1979b6787242SJiri Kosina int ret = 0;
1980aa8de2f0SJiri Kosina
198185cdaf52SJiri Slaby report = hid_get_report(report_enum, data);
198285cdaf52SJiri Slaby if (!report)
1983b6787242SJiri Kosina goto out;
1984aa8de2f0SJiri Kosina
198585cdaf52SJiri Slaby if (report_enum->numbered) {
198685cdaf52SJiri Slaby cdata++;
198785cdaf52SJiri Slaby csize--;
1988aa8de2f0SJiri Kosina }
1989aa8de2f0SJiri Kosina
1990bce1305cSMarc Zyngier rsize = hid_compute_report_size(report);
1991aa8de2f0SJiri Kosina
1992b1a37ed0SLee Jones if (hid->ll_driver->max_buffer_size)
1993b1a37ed0SLee Jones max_buffer_size = hid->ll_driver->max_buffer_size;
1994b1a37ed0SLee Jones
1995b1a37ed0SLee Jones if (report_enum->numbered && rsize >= max_buffer_size)
1996b1a37ed0SLee Jones rsize = max_buffer_size - 1;
1997b1a37ed0SLee Jones else if (rsize > max_buffer_size)
1998b1a37ed0SLee Jones rsize = max_buffer_size;
1999966922f2SArmando Visconti
200085cdaf52SJiri Slaby if (csize < rsize) {
200185cdaf52SJiri Slaby dbg_hid("report %d is too short, (%d < %d)\n", report->id,
200285cdaf52SJiri Slaby csize, rsize);
200385cdaf52SJiri Slaby memset(cdata + csize, 0, rsize - csize);
2004aa8de2f0SJiri Kosina }
2005aa8de2f0SJiri Kosina
2006aa8de2f0SJiri Kosina if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
2007aa8de2f0SJiri Kosina hid->hiddev_report_event(hid, report);
2008b6787242SJiri Kosina if (hid->claimed & HID_CLAIMED_HIDRAW) {
2009b6787242SJiri Kosina ret = hidraw_report_event(hid, data, size);
2010b6787242SJiri Kosina if (ret)
2011b6787242SJiri Kosina goto out;
2012b6787242SJiri Kosina }
2013aa8de2f0SJiri Kosina
2014cc6b54aaSBenjamin Tissoires if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
2015b79c1abaSBenjamin Tissoires hid_process_report(hid, report, cdata, interrupt);
20166d85d037SBenjamin Tissoires hdrv = hid->driver;
20176d85d037SBenjamin Tissoires if (hdrv && hdrv->report)
20186d85d037SBenjamin Tissoires hdrv->report(hid, report);
2019b94e3c94SMatthieu CASTET }
2020aa8de2f0SJiri Kosina
2021aa8de2f0SJiri Kosina if (hid->claimed & HID_CLAIMED_INPUT)
2022aa8de2f0SJiri Kosina hidinput_report_event(hid, report);
2023b6787242SJiri Kosina out:
2024b6787242SJiri Kosina return ret;
202585cdaf52SJiri Slaby }
202685cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_report_raw_event);
202785cdaf52SJiri Slaby
202885cdaf52SJiri Slaby /**
202985cdaf52SJiri Slaby * hid_input_report - report data from lower layer (usb, bt...)
203085cdaf52SJiri Slaby *
203185cdaf52SJiri Slaby * @hid: hid device
203285cdaf52SJiri Slaby * @type: HID report type (HID_*_REPORT)
203385cdaf52SJiri Slaby * @data: report contents
203485cdaf52SJiri Slaby * @size: size of data parameter
2035ff9b00a2SJiri Kosina * @interrupt: distinguish between interrupt and control transfers
203685cdaf52SJiri Slaby *
203785cdaf52SJiri Slaby * This is data entry for lower layers.
203885cdaf52SJiri Slaby */
hid_input_report(struct hid_device * hid,enum hid_report_type type,u8 * data,u32 size,int interrupt)2039ead77b65SBenjamin Tissoires int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
2040ead77b65SBenjamin Tissoires int interrupt)
204185cdaf52SJiri Slaby {
204276c317d6SJulia Lawall struct hid_report_enum *report_enum;
204376c317d6SJulia Lawall struct hid_driver *hdrv;
204485cdaf52SJiri Slaby struct hid_report *report;
204545dc1ac7SJiri Kosina int ret = 0;
204685cdaf52SJiri Slaby
20474ea54542SDavid Herrmann if (!hid)
204885cdaf52SJiri Slaby return -ENODEV;
20494ea54542SDavid Herrmann
2050c849a614SAndrew de los Reyes if (down_trylock(&hid->driver_input_lock))
20514ea54542SDavid Herrmann return -EBUSY;
20524ea54542SDavid Herrmann
20534ea54542SDavid Herrmann if (!hid->driver) {
20544ea54542SDavid Herrmann ret = -ENODEV;
20554ea54542SDavid Herrmann goto unlock;
20564ea54542SDavid Herrmann }
205776c317d6SJulia Lawall report_enum = hid->report_enum + type;
205876c317d6SJulia Lawall hdrv = hid->driver;
205985cdaf52SJiri Slaby
2060658ee5a6SBenjamin Tissoires data = dispatch_hid_bpf_device_event(hid, type, data, &size, interrupt);
2061658ee5a6SBenjamin Tissoires if (IS_ERR(data)) {
2062658ee5a6SBenjamin Tissoires ret = PTR_ERR(data);
2063f5c27da4SBenjamin Tissoires goto unlock;
2064658ee5a6SBenjamin Tissoires }
2065f5c27da4SBenjamin Tissoires
206685cdaf52SJiri Slaby if (!size) {
206785cdaf52SJiri Slaby dbg_hid("empty report\n");
20684ea54542SDavid Herrmann ret = -1;
20694ea54542SDavid Herrmann goto unlock;
207085cdaf52SJiri Slaby }
207185cdaf52SJiri Slaby
2072b94e3c94SMatthieu CASTET /* Avoid unnecessary overhead if debugfs is disabled */
2073a5f04b9dSBenjamin Tissoires if (!list_empty(&hid->debug_list))
2074a5f04b9dSBenjamin Tissoires hid_dump_report(hid, type, data, size);
2075b94e3c94SMatthieu CASTET
2076f77e347bSJiri Kosina report = hid_get_report(report_enum, data);
2077f77e347bSJiri Kosina
20784ea54542SDavid Herrmann if (!report) {
20794ea54542SDavid Herrmann ret = -1;
20804ea54542SDavid Herrmann goto unlock;
20814ea54542SDavid Herrmann }
2082f77e347bSJiri Kosina
208385cdaf52SJiri Slaby if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
208485cdaf52SJiri Slaby ret = hdrv->raw_event(hid, report, data, size);
2085556483e2SFelix Rueegg if (ret < 0)
20864ea54542SDavid Herrmann goto unlock;
20874ea54542SDavid Herrmann }
208885cdaf52SJiri Slaby
2089b6787242SJiri Kosina ret = hid_report_raw_event(hid, type, data, size, interrupt);
2090aa8de2f0SJiri Kosina
20914ea54542SDavid Herrmann unlock:
2092c849a614SAndrew de los Reyes up(&hid->driver_input_lock);
209345dc1ac7SJiri Kosina return ret;
2094aa8de2f0SJiri Kosina }
2095aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report);
2096aa8de2f0SJiri Kosina
hid_match_one_id(const struct hid_device * hdev,const struct hid_device_id * id)2097d5d3e202SBenjamin Tissoires bool hid_match_one_id(const struct hid_device *hdev,
20980f37cd03SJiri Kosina const struct hid_device_id *id)
20990f37cd03SJiri Kosina {
21007431fb76SHenrik Rydberg return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
21014d53b801SHenrik Rydberg (id->group == HID_GROUP_ANY || id->group == hdev->group) &&
21020f37cd03SJiri Kosina (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
21030f37cd03SJiri Kosina (id->product == HID_ANY_ID || id->product == hdev->product);
21040f37cd03SJiri Kosina }
21050f37cd03SJiri Kosina
hid_match_id(const struct hid_device * hdev,const struct hid_device_id * id)2106d5d3e202SBenjamin Tissoires const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
21070f37cd03SJiri Kosina const struct hid_device_id *id)
21080f37cd03SJiri Kosina {
21090f37cd03SJiri Kosina for (; id->bus; id++)
21100f37cd03SJiri Kosina if (hid_match_one_id(hdev, id))
21110f37cd03SJiri Kosina return id;
21120f37cd03SJiri Kosina
21130f37cd03SJiri Kosina return NULL;
21140f37cd03SJiri Kosina }
211580305f97SBastien Nocera EXPORT_SYMBOL_GPL(hid_match_id);
21160f37cd03SJiri Kosina
21170f37cd03SJiri Kosina static const struct hid_device_id hid_hiddev_list[] = {
2118c0bd6a42SRichard Hughes { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
2119c0bd6a42SRichard Hughes { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
21200f37cd03SJiri Kosina { }
21210f37cd03SJiri Kosina };
21220f37cd03SJiri Kosina
hid_hiddev(struct hid_device * hdev)21230f37cd03SJiri Kosina static bool hid_hiddev(struct hid_device *hdev)
21240f37cd03SJiri Kosina {
21250f37cd03SJiri Kosina return !!hid_match_id(hdev, hid_hiddev_list);
21260f37cd03SJiri Kosina }
21270f37cd03SJiri Kosina
21286d3bfb74SAlan Ott
21296d3bfb74SAlan Ott static ssize_t
read_report_descriptor(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)21306d3bfb74SAlan Ott read_report_descriptor(struct file *filp, struct kobject *kobj,
21316d3bfb74SAlan Ott struct bin_attribute *attr,
21326d3bfb74SAlan Ott char *buf, loff_t off, size_t count)
21336d3bfb74SAlan Ott {
21342cf83833SGeliang Tang struct device *dev = kobj_to_dev(kobj);
2135ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev);
21366d3bfb74SAlan Ott
21376d3bfb74SAlan Ott if (off >= hdev->rsize)
21386d3bfb74SAlan Ott return 0;
21396d3bfb74SAlan Ott
21406d3bfb74SAlan Ott if (off + count > hdev->rsize)
21416d3bfb74SAlan Ott count = hdev->rsize - off;
21426d3bfb74SAlan Ott
21436d3bfb74SAlan Ott memcpy(buf, hdev->rdesc + off, count);
21446d3bfb74SAlan Ott
21456d3bfb74SAlan Ott return count;
21466d3bfb74SAlan Ott }
21476d3bfb74SAlan Ott
2148a877417eSOlivier Gay static ssize_t
show_country(struct device * dev,struct device_attribute * attr,char * buf)2149a877417eSOlivier Gay show_country(struct device *dev, struct device_attribute *attr,
2150a877417eSOlivier Gay char *buf)
2151a877417eSOlivier Gay {
2152ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev);
2153a877417eSOlivier Gay
2154a877417eSOlivier Gay return sprintf(buf, "%02x\n", hdev->country & 0xff);
2155a877417eSOlivier Gay }
2156a877417eSOlivier Gay
21576d3bfb74SAlan Ott static struct bin_attribute dev_bin_attr_report_desc = {
21586d3bfb74SAlan Ott .attr = { .name = "report_descriptor", .mode = 0444 },
21596d3bfb74SAlan Ott .read = read_report_descriptor,
21606d3bfb74SAlan Ott .size = HID_MAX_DESCRIPTOR_SIZE,
21616d3bfb74SAlan Ott };
21626d3bfb74SAlan Ott
2163ad8378edSBhumika Goyal static const struct device_attribute dev_attr_country = {
2164a877417eSOlivier Gay .attr = { .name = "country", .mode = 0444 },
2165a877417eSOlivier Gay .show = show_country,
2166a877417eSOlivier Gay };
2167a877417eSOlivier Gay
hid_connect(struct hid_device * hdev,unsigned int connect_mask)216893c10132SJiri Slaby int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
216993c10132SJiri Slaby {
217093c10132SJiri Slaby static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
217193c10132SJiri Slaby "Joystick", "Gamepad", "Keyboard", "Keypad",
217293c10132SJiri Slaby "Multi-Axis Controller"
217393c10132SJiri Slaby };
217493c10132SJiri Slaby const char *type, *bus;
217579b568b9SRichard Purdie char buf[64] = "";
217693c10132SJiri Slaby unsigned int i;
217793c10132SJiri Slaby int len;
21786d3bfb74SAlan Ott int ret;
217993c10132SJiri Slaby
2180658ee5a6SBenjamin Tissoires ret = hid_bpf_connect_device(hdev);
2181658ee5a6SBenjamin Tissoires if (ret)
2182658ee5a6SBenjamin Tissoires return ret;
2183658ee5a6SBenjamin Tissoires
2184b5e5a37eSBastien Nocera if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
2185b5e5a37eSBastien Nocera connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
21863a343ee4SDaniel Mack if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
21873a343ee4SDaniel Mack connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
218893c10132SJiri Slaby if (hdev->bus != BUS_USB)
218993c10132SJiri Slaby connect_mask &= ~HID_CONNECT_HIDDEV;
21900f37cd03SJiri Kosina if (hid_hiddev(hdev))
21910f37cd03SJiri Kosina connect_mask |= HID_CONNECT_HIDDEV_FORCE;
219293c10132SJiri Slaby
219393c10132SJiri Slaby if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
219493c10132SJiri Slaby connect_mask & HID_CONNECT_HIDINPUT_FORCE))
219593c10132SJiri Slaby hdev->claimed |= HID_CLAIMED_INPUT;
2196b77c3920SBenjamin Tissoires
219793c10132SJiri Slaby if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
219893c10132SJiri Slaby !hdev->hiddev_connect(hdev,
219993c10132SJiri Slaby connect_mask & HID_CONNECT_HIDDEV_FORCE))
220093c10132SJiri Slaby hdev->claimed |= HID_CLAIMED_HIDDEV;
220193c10132SJiri Slaby if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
220293c10132SJiri Slaby hdev->claimed |= HID_CLAIMED_HIDRAW;
220393c10132SJiri Slaby
22047704ac93SBenjamin Tissoires if (connect_mask & HID_CONNECT_DRIVER)
22057704ac93SBenjamin Tissoires hdev->claimed |= HID_CLAIMED_DRIVER;
22067704ac93SBenjamin Tissoires
22074bc19f62SDavid Herrmann /* Drivers with the ->raw_event callback set are not required to connect
22084bc19f62SDavid Herrmann * to any other listener. */
22094bc19f62SDavid Herrmann if (!hdev->claimed && !hdev->driver->raw_event) {
22104bc19f62SDavid Herrmann hid_err(hdev, "device has no listeners, quitting\n");
221193c10132SJiri Slaby return -ENODEV;
221293c10132SJiri Slaby }
221393c10132SJiri Slaby
221422f4b026SBenjamin Tissoires hid_process_ordering(hdev);
221522f4b026SBenjamin Tissoires
221693c10132SJiri Slaby if ((hdev->claimed & HID_CLAIMED_INPUT) &&
221793c10132SJiri Slaby (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
221893c10132SJiri Slaby hdev->ff_init(hdev);
221993c10132SJiri Slaby
222093c10132SJiri Slaby len = 0;
222193c10132SJiri Slaby if (hdev->claimed & HID_CLAIMED_INPUT)
222293c10132SJiri Slaby len += sprintf(buf + len, "input");
222393c10132SJiri Slaby if (hdev->claimed & HID_CLAIMED_HIDDEV)
222493c10132SJiri Slaby len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
2225733aca90SJaejoong Kim ((struct hiddev *)hdev->hiddev)->minor);
222693c10132SJiri Slaby if (hdev->claimed & HID_CLAIMED_HIDRAW)
222793c10132SJiri Slaby len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
222893c10132SJiri Slaby ((struct hidraw *)hdev->hidraw)->minor);
222993c10132SJiri Slaby
223093c10132SJiri Slaby type = "Device";
223193c10132SJiri Slaby for (i = 0; i < hdev->maxcollection; i++) {
223293c10132SJiri Slaby struct hid_collection *col = &hdev->collection[i];
223393c10132SJiri Slaby if (col->type == HID_COLLECTION_APPLICATION &&
223493c10132SJiri Slaby (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
223593c10132SJiri Slaby (col->usage & 0xffff) < ARRAY_SIZE(types)) {
223693c10132SJiri Slaby type = types[col->usage & 0xffff];
223793c10132SJiri Slaby break;
223893c10132SJiri Slaby }
223993c10132SJiri Slaby }
224093c10132SJiri Slaby
224193c10132SJiri Slaby switch (hdev->bus) {
224293c10132SJiri Slaby case BUS_USB:
224393c10132SJiri Slaby bus = "USB";
224493c10132SJiri Slaby break;
224593c10132SJiri Slaby case BUS_BLUETOOTH:
224693c10132SJiri Slaby bus = "BLUETOOTH";
224793c10132SJiri Slaby break;
224806780727SDaniel Martin case BUS_I2C:
224906780727SDaniel Martin bus = "I2C";
225006780727SDaniel Martin break;
225148e33befSMark Bolhuis case BUS_VIRTUAL:
225248e33befSMark Bolhuis bus = "VIRTUAL";
225348e33befSMark Bolhuis break;
2254806fc359SBasavaraj Natikar case BUS_INTEL_ISHTP:
2255806fc359SBasavaraj Natikar case BUS_AMD_SFH:
2256806fc359SBasavaraj Natikar bus = "SENSOR HUB";
2257806fc359SBasavaraj Natikar break;
225893c10132SJiri Slaby default:
225993c10132SJiri Slaby bus = "<UNKNOWN>";
226093c10132SJiri Slaby }
226193c10132SJiri Slaby
2262a877417eSOlivier Gay ret = device_create_file(&hdev->dev, &dev_attr_country);
2263a877417eSOlivier Gay if (ret)
2264a877417eSOlivier Gay hid_warn(hdev,
2265a877417eSOlivier Gay "can't create sysfs country code attribute err: %d\n", ret);
2266a877417eSOlivier Gay
22674291ee30SJoe Perches hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
226893c10132SJiri Slaby buf, bus, hdev->version >> 8, hdev->version & 0xff,
226993c10132SJiri Slaby type, hdev->name, hdev->phys);
227093c10132SJiri Slaby
227193c10132SJiri Slaby return 0;
227293c10132SJiri Slaby }
227393c10132SJiri Slaby EXPORT_SYMBOL_GPL(hid_connect);
227493c10132SJiri Slaby
hid_disconnect(struct hid_device * hdev)2275c4c259bcSJiri Kosina void hid_disconnect(struct hid_device *hdev)
2276c4c259bcSJiri Kosina {
2277a877417eSOlivier Gay device_remove_file(&hdev->dev, &dev_attr_country);
2278c4c259bcSJiri Kosina if (hdev->claimed & HID_CLAIMED_INPUT)
2279c4c259bcSJiri Kosina hidinput_disconnect(hdev);
2280c4c259bcSJiri Kosina if (hdev->claimed & HID_CLAIMED_HIDDEV)
2281c4c259bcSJiri Kosina hdev->hiddev_disconnect(hdev);
2282c4c259bcSJiri Kosina if (hdev->claimed & HID_CLAIMED_HIDRAW)
2283c4c259bcSJiri Kosina hidraw_disconnect(hdev);
22849c5c6ed7SBenjamin Tissoires hdev->claimed = 0;
2285658ee5a6SBenjamin Tissoires
2286658ee5a6SBenjamin Tissoires hid_bpf_disconnect_device(hdev);
2287c4c259bcSJiri Kosina }
2288c4c259bcSJiri Kosina EXPORT_SYMBOL_GPL(hid_disconnect);
2289c4c259bcSJiri Kosina
2290aaac082dSDmitry Torokhov /**
2291aaac082dSDmitry Torokhov * hid_hw_start - start underlying HW
2292aaac082dSDmitry Torokhov * @hdev: hid device
2293aaac082dSDmitry Torokhov * @connect_mask: which outputs to connect, see HID_CONNECT_*
2294aaac082dSDmitry Torokhov *
2295aaac082dSDmitry Torokhov * Call this in probe function *after* hid_parse. This will setup HW
2296aaac082dSDmitry Torokhov * buffers and start the device (if not defeirred to device open).
2297aaac082dSDmitry Torokhov * hid_hw_stop must be called if this was successful.
2298aaac082dSDmitry Torokhov */
hid_hw_start(struct hid_device * hdev,unsigned int connect_mask)2299aaac082dSDmitry Torokhov int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
2300aaac082dSDmitry Torokhov {
2301aaac082dSDmitry Torokhov int error;
2302aaac082dSDmitry Torokhov
2303aaac082dSDmitry Torokhov error = hdev->ll_driver->start(hdev);
2304aaac082dSDmitry Torokhov if (error)
2305aaac082dSDmitry Torokhov return error;
2306aaac082dSDmitry Torokhov
2307aaac082dSDmitry Torokhov if (connect_mask) {
2308aaac082dSDmitry Torokhov error = hid_connect(hdev, connect_mask);
2309aaac082dSDmitry Torokhov if (error) {
2310aaac082dSDmitry Torokhov hdev->ll_driver->stop(hdev);
2311aaac082dSDmitry Torokhov return error;
2312aaac082dSDmitry Torokhov }
2313aaac082dSDmitry Torokhov }
2314aaac082dSDmitry Torokhov
2315aaac082dSDmitry Torokhov return 0;
2316aaac082dSDmitry Torokhov }
2317aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_start);
2318aaac082dSDmitry Torokhov
2319aaac082dSDmitry Torokhov /**
2320aaac082dSDmitry Torokhov * hid_hw_stop - stop underlying HW
2321aaac082dSDmitry Torokhov * @hdev: hid device
2322aaac082dSDmitry Torokhov *
2323aaac082dSDmitry Torokhov * This is usually called from remove function or from probe when something
2324aaac082dSDmitry Torokhov * failed and hid_hw_start was called already.
2325aaac082dSDmitry Torokhov */
hid_hw_stop(struct hid_device * hdev)2326aaac082dSDmitry Torokhov void hid_hw_stop(struct hid_device *hdev)
2327aaac082dSDmitry Torokhov {
2328aaac082dSDmitry Torokhov hid_disconnect(hdev);
2329aaac082dSDmitry Torokhov hdev->ll_driver->stop(hdev);
2330aaac082dSDmitry Torokhov }
2331aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_stop);
2332aaac082dSDmitry Torokhov
2333aaac082dSDmitry Torokhov /**
2334aaac082dSDmitry Torokhov * hid_hw_open - signal underlying HW to start delivering events
2335aaac082dSDmitry Torokhov * @hdev: hid device
2336aaac082dSDmitry Torokhov *
2337aaac082dSDmitry Torokhov * Tell underlying HW to start delivering events from the device.
2338aaac082dSDmitry Torokhov * This function should be called sometime after successful call
2339d6c70a86SHisao Tanabe * to hid_hw_start().
2340aaac082dSDmitry Torokhov */
hid_hw_open(struct hid_device * hdev)2341aaac082dSDmitry Torokhov int hid_hw_open(struct hid_device *hdev)
2342aaac082dSDmitry Torokhov {
2343aaac082dSDmitry Torokhov int ret;
2344aaac082dSDmitry Torokhov
2345aaac082dSDmitry Torokhov ret = mutex_lock_killable(&hdev->ll_open_lock);
2346aaac082dSDmitry Torokhov if (ret)
2347aaac082dSDmitry Torokhov return ret;
2348aaac082dSDmitry Torokhov
2349aaac082dSDmitry Torokhov if (!hdev->ll_open_count++) {
2350aaac082dSDmitry Torokhov ret = hdev->ll_driver->open(hdev);
2351aaac082dSDmitry Torokhov if (ret)
2352aaac082dSDmitry Torokhov hdev->ll_open_count--;
2353aaac082dSDmitry Torokhov }
2354aaac082dSDmitry Torokhov
2355aaac082dSDmitry Torokhov mutex_unlock(&hdev->ll_open_lock);
2356aaac082dSDmitry Torokhov return ret;
2357aaac082dSDmitry Torokhov }
2358aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_open);
2359aaac082dSDmitry Torokhov
2360aaac082dSDmitry Torokhov /**
2361aaac082dSDmitry Torokhov * hid_hw_close - signal underlaying HW to stop delivering events
2362aaac082dSDmitry Torokhov *
2363aaac082dSDmitry Torokhov * @hdev: hid device
2364aaac082dSDmitry Torokhov *
2365aaac082dSDmitry Torokhov * This function indicates that we are not interested in the events
2366aaac082dSDmitry Torokhov * from this device anymore. Delivery of events may or may not stop,
2367aaac082dSDmitry Torokhov * depending on the number of users still outstanding.
2368aaac082dSDmitry Torokhov */
hid_hw_close(struct hid_device * hdev)2369aaac082dSDmitry Torokhov void hid_hw_close(struct hid_device *hdev)
2370aaac082dSDmitry Torokhov {
2371aaac082dSDmitry Torokhov mutex_lock(&hdev->ll_open_lock);
2372aaac082dSDmitry Torokhov if (!--hdev->ll_open_count)
2373aaac082dSDmitry Torokhov hdev->ll_driver->close(hdev);
2374aaac082dSDmitry Torokhov mutex_unlock(&hdev->ll_open_lock);
2375aaac082dSDmitry Torokhov }
2376aaac082dSDmitry Torokhov EXPORT_SYMBOL_GPL(hid_hw_close);
2377aaac082dSDmitry Torokhov
2378f65a0b1fSBenjamin Tissoires /**
2379f65a0b1fSBenjamin Tissoires * hid_hw_request - send report request to device
2380f65a0b1fSBenjamin Tissoires *
2381f65a0b1fSBenjamin Tissoires * @hdev: hid device
2382f65a0b1fSBenjamin Tissoires * @report: report to send
2383f65a0b1fSBenjamin Tissoires * @reqtype: hid request type
2384f65a0b1fSBenjamin Tissoires */
hid_hw_request(struct hid_device * hdev,struct hid_report * report,enum hid_class_request reqtype)2385f65a0b1fSBenjamin Tissoires void hid_hw_request(struct hid_device *hdev,
2386735e1bb1SBenjamin Tissoires struct hid_report *report, enum hid_class_request reqtype)
2387f65a0b1fSBenjamin Tissoires {
2388f65a0b1fSBenjamin Tissoires if (hdev->ll_driver->request)
2389f65a0b1fSBenjamin Tissoires return hdev->ll_driver->request(hdev, report, reqtype);
2390f65a0b1fSBenjamin Tissoires
2391f65a0b1fSBenjamin Tissoires __hid_request(hdev, report, reqtype);
2392f65a0b1fSBenjamin Tissoires }
2393f65a0b1fSBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_hw_request);
2394f65a0b1fSBenjamin Tissoires
2395f65a0b1fSBenjamin Tissoires /**
2396f65a0b1fSBenjamin Tissoires * hid_hw_raw_request - send report request to device
2397f65a0b1fSBenjamin Tissoires *
2398f65a0b1fSBenjamin Tissoires * @hdev: hid device
2399f65a0b1fSBenjamin Tissoires * @reportnum: report ID
2400f65a0b1fSBenjamin Tissoires * @buf: in/out data to transfer
2401f65a0b1fSBenjamin Tissoires * @len: length of buf
2402f65a0b1fSBenjamin Tissoires * @rtype: HID report type
2403f65a0b1fSBenjamin Tissoires * @reqtype: HID_REQ_GET_REPORT or HID_REQ_SET_REPORT
2404f65a0b1fSBenjamin Tissoires *
2405f65a0b1fSBenjamin Tissoires * Return: count of data transferred, negative if error
2406f65a0b1fSBenjamin Tissoires *
2407f65a0b1fSBenjamin Tissoires * Same behavior as hid_hw_request, but with raw buffers instead.
2408f65a0b1fSBenjamin Tissoires */
hid_hw_raw_request(struct hid_device * hdev,unsigned char reportnum,__u8 * buf,size_t len,enum hid_report_type rtype,enum hid_class_request reqtype)2409f65a0b1fSBenjamin Tissoires int hid_hw_raw_request(struct hid_device *hdev,
2410f65a0b1fSBenjamin Tissoires unsigned char reportnum, __u8 *buf,
2411735e1bb1SBenjamin Tissoires size_t len, enum hid_report_type rtype, enum hid_class_request reqtype)
2412f65a0b1fSBenjamin Tissoires {
2413b1a37ed0SLee Jones unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
2414b1a37ed0SLee Jones
2415b1a37ed0SLee Jones if (hdev->ll_driver->max_buffer_size)
2416b1a37ed0SLee Jones max_buffer_size = hdev->ll_driver->max_buffer_size;
2417b1a37ed0SLee Jones
2418b1a37ed0SLee Jones if (len < 1 || len > max_buffer_size || !buf)
2419f65a0b1fSBenjamin Tissoires return -EINVAL;
2420f65a0b1fSBenjamin Tissoires
2421f65a0b1fSBenjamin Tissoires return hdev->ll_driver->raw_request(hdev, reportnum, buf, len,
2422f65a0b1fSBenjamin Tissoires rtype, reqtype);
2423f65a0b1fSBenjamin Tissoires }
2424f65a0b1fSBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_hw_raw_request);
2425f65a0b1fSBenjamin Tissoires
2426f65a0b1fSBenjamin Tissoires /**
2427f65a0b1fSBenjamin Tissoires * hid_hw_output_report - send output report to device
2428f65a0b1fSBenjamin Tissoires *
2429f65a0b1fSBenjamin Tissoires * @hdev: hid device
2430f65a0b1fSBenjamin Tissoires * @buf: raw data to transfer
2431f65a0b1fSBenjamin Tissoires * @len: length of buf
2432f65a0b1fSBenjamin Tissoires *
2433f65a0b1fSBenjamin Tissoires * Return: count of data transferred, negative if error
2434f65a0b1fSBenjamin Tissoires */
hid_hw_output_report(struct hid_device * hdev,__u8 * buf,size_t len)2435f65a0b1fSBenjamin Tissoires int hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len)
2436f65a0b1fSBenjamin Tissoires {
2437b1a37ed0SLee Jones unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
2438b1a37ed0SLee Jones
2439b1a37ed0SLee Jones if (hdev->ll_driver->max_buffer_size)
2440b1a37ed0SLee Jones max_buffer_size = hdev->ll_driver->max_buffer_size;
2441b1a37ed0SLee Jones
2442b1a37ed0SLee Jones if (len < 1 || len > max_buffer_size || !buf)
2443f65a0b1fSBenjamin Tissoires return -EINVAL;
2444f65a0b1fSBenjamin Tissoires
2445f65a0b1fSBenjamin Tissoires if (hdev->ll_driver->output_report)
2446f65a0b1fSBenjamin Tissoires return hdev->ll_driver->output_report(hdev, buf, len);
2447f65a0b1fSBenjamin Tissoires
2448f65a0b1fSBenjamin Tissoires return -ENOSYS;
2449f65a0b1fSBenjamin Tissoires }
2450f65a0b1fSBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_hw_output_report);
2451f65a0b1fSBenjamin Tissoires
24529e356208SBenjamin Tissoires #ifdef CONFIG_PM
hid_driver_suspend(struct hid_device * hdev,pm_message_t state)24539e356208SBenjamin Tissoires int hid_driver_suspend(struct hid_device *hdev, pm_message_t state)
24549e356208SBenjamin Tissoires {
24559e356208SBenjamin Tissoires if (hdev->driver && hdev->driver->suspend)
24569e356208SBenjamin Tissoires return hdev->driver->suspend(hdev, state);
24579e356208SBenjamin Tissoires
24589e356208SBenjamin Tissoires return 0;
24599e356208SBenjamin Tissoires }
24609e356208SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_driver_suspend);
24619e356208SBenjamin Tissoires
hid_driver_reset_resume(struct hid_device * hdev)24629e356208SBenjamin Tissoires int hid_driver_reset_resume(struct hid_device *hdev)
24639e356208SBenjamin Tissoires {
24649e356208SBenjamin Tissoires if (hdev->driver && hdev->driver->reset_resume)
24659e356208SBenjamin Tissoires return hdev->driver->reset_resume(hdev);
24669e356208SBenjamin Tissoires
24679e356208SBenjamin Tissoires return 0;
24689e356208SBenjamin Tissoires }
24699e356208SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_driver_reset_resume);
24709e356208SBenjamin Tissoires
hid_driver_resume(struct hid_device * hdev)24719e356208SBenjamin Tissoires int hid_driver_resume(struct hid_device *hdev)
24729e356208SBenjamin Tissoires {
24739e356208SBenjamin Tissoires if (hdev->driver && hdev->driver->resume)
24749e356208SBenjamin Tissoires return hdev->driver->resume(hdev);
24759e356208SBenjamin Tissoires
24769e356208SBenjamin Tissoires return 0;
24779e356208SBenjamin Tissoires }
24789e356208SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_driver_resume);
24799e356208SBenjamin Tissoires #endif /* CONFIG_PM */
24809e356208SBenjamin Tissoires
24813a6f82f7SJiri Slaby struct hid_dynid {
24823a6f82f7SJiri Slaby struct list_head list;
24833a6f82f7SJiri Slaby struct hid_device_id id;
24843a6f82f7SJiri Slaby };
24853a6f82f7SJiri Slaby
24863a6f82f7SJiri Slaby /**
2487cd2bb7b7SLee Jones * new_id_store - add a new HID device ID to this driver and re-probe devices
24885f94e9c8SXiaofei Tan * @drv: target device driver
24893a6f82f7SJiri Slaby * @buf: buffer for scanning device ID data
24903a6f82f7SJiri Slaby * @count: input size
24913a6f82f7SJiri Slaby *
24923a6f82f7SJiri Slaby * Adds a new dynamic hid device ID to this driver,
24933a6f82f7SJiri Slaby * and causes the driver to probe for all devices again.
24943a6f82f7SJiri Slaby */
new_id_store(struct device_driver * drv,const char * buf,size_t count)2495c2810325SGreg Kroah-Hartman static ssize_t new_id_store(struct device_driver *drv, const char *buf,
24963a6f82f7SJiri Slaby size_t count)
24973a6f82f7SJiri Slaby {
2498ba91a967SGeliang Tang struct hid_driver *hdrv = to_hid_driver(drv);
24993a6f82f7SJiri Slaby struct hid_dynid *dynid;
25003a6f82f7SJiri Slaby __u32 bus, vendor, product;
25013a6f82f7SJiri Slaby unsigned long driver_data = 0;
25023a6f82f7SJiri Slaby int ret;
25033a6f82f7SJiri Slaby
25043a6f82f7SJiri Slaby ret = sscanf(buf, "%x %x %x %lx",
25053a6f82f7SJiri Slaby &bus, &vendor, &product, &driver_data);
25063a6f82f7SJiri Slaby if (ret < 3)
25073a6f82f7SJiri Slaby return -EINVAL;
25083a6f82f7SJiri Slaby
25093a6f82f7SJiri Slaby dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
25103a6f82f7SJiri Slaby if (!dynid)
25113a6f82f7SJiri Slaby return -ENOMEM;
25123a6f82f7SJiri Slaby
25133a6f82f7SJiri Slaby dynid->id.bus = bus;
25144d53b801SHenrik Rydberg dynid->id.group = HID_GROUP_ANY;
25153a6f82f7SJiri Slaby dynid->id.vendor = vendor;
25163a6f82f7SJiri Slaby dynid->id.product = product;
25173a6f82f7SJiri Slaby dynid->id.driver_data = driver_data;
25183a6f82f7SJiri Slaby
25193a6f82f7SJiri Slaby spin_lock(&hdrv->dyn_lock);
25203a6f82f7SJiri Slaby list_add_tail(&dynid->list, &hdrv->dyn_list);
25213a6f82f7SJiri Slaby spin_unlock(&hdrv->dyn_lock);
25223a6f82f7SJiri Slaby
25233a6f82f7SJiri Slaby ret = driver_attach(&hdrv->driver);
25243a6f82f7SJiri Slaby
25253a6f82f7SJiri Slaby return ret ? : count;
25263a6f82f7SJiri Slaby }
2527c2810325SGreg Kroah-Hartman static DRIVER_ATTR_WO(new_id);
2528c2810325SGreg Kroah-Hartman
2529c2810325SGreg Kroah-Hartman static struct attribute *hid_drv_attrs[] = {
2530c2810325SGreg Kroah-Hartman &driver_attr_new_id.attr,
2531c2810325SGreg Kroah-Hartman NULL,
2532c2810325SGreg Kroah-Hartman };
2533c2810325SGreg Kroah-Hartman ATTRIBUTE_GROUPS(hid_drv);
25343a6f82f7SJiri Slaby
hid_free_dynids(struct hid_driver * hdrv)25353a6f82f7SJiri Slaby static void hid_free_dynids(struct hid_driver *hdrv)
25363a6f82f7SJiri Slaby {
25373a6f82f7SJiri Slaby struct hid_dynid *dynid, *n;
25383a6f82f7SJiri Slaby
25393a6f82f7SJiri Slaby spin_lock(&hdrv->dyn_lock);
25403a6f82f7SJiri Slaby list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
25413a6f82f7SJiri Slaby list_del(&dynid->list);
25423a6f82f7SJiri Slaby kfree(dynid);
25433a6f82f7SJiri Slaby }
25443a6f82f7SJiri Slaby spin_unlock(&hdrv->dyn_lock);
25453a6f82f7SJiri Slaby }
25463a6f82f7SJiri Slaby
hid_match_device(struct hid_device * hdev,struct hid_driver * hdrv)2547e04a0442SBenjamin Tissoires const struct hid_device_id *hid_match_device(struct hid_device *hdev,
25483a6f82f7SJiri Slaby struct hid_driver *hdrv)
25493a6f82f7SJiri Slaby {
25503a6f82f7SJiri Slaby struct hid_dynid *dynid;
25513a6f82f7SJiri Slaby
25523a6f82f7SJiri Slaby spin_lock(&hdrv->dyn_lock);
25533a6f82f7SJiri Slaby list_for_each_entry(dynid, &hdrv->dyn_list, list) {
25543a6f82f7SJiri Slaby if (hid_match_one_id(hdev, &dynid->id)) {
25553a6f82f7SJiri Slaby spin_unlock(&hdrv->dyn_lock);
25563a6f82f7SJiri Slaby return &dynid->id;
25573a6f82f7SJiri Slaby }
25583a6f82f7SJiri Slaby }
25593a6f82f7SJiri Slaby spin_unlock(&hdrv->dyn_lock);
25603a6f82f7SJiri Slaby
25613a6f82f7SJiri Slaby return hid_match_id(hdev, hdrv->id_table);
25623a6f82f7SJiri Slaby }
2563e04a0442SBenjamin Tissoires EXPORT_SYMBOL_GPL(hid_match_device);
25643a6f82f7SJiri Slaby
hid_bus_match(struct device * dev,struct device_driver * drv)256585cdaf52SJiri Slaby static int hid_bus_match(struct device *dev, struct device_driver *drv)
256685cdaf52SJiri Slaby {
2567ba91a967SGeliang Tang struct hid_driver *hdrv = to_hid_driver(drv);
2568ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev);
256985cdaf52SJiri Slaby
2570070748edSHenrik Rydberg return hid_match_device(hdev, hdrv) != NULL;
257185cdaf52SJiri Slaby }
257285cdaf52SJiri Slaby
25731a8861f1SDaniel M. Lambea /**
25741a8861f1SDaniel M. Lambea * hid_compare_device_paths - check if both devices share the same path
25751a8861f1SDaniel M. Lambea * @hdev_a: hid device
25761a8861f1SDaniel M. Lambea * @hdev_b: hid device
25771a8861f1SDaniel M. Lambea * @separator: char to use as separator
25781a8861f1SDaniel M. Lambea *
25791a8861f1SDaniel M. Lambea * Check if two devices share the same path up to the last occurrence of
25801a8861f1SDaniel M. Lambea * the separator char. Both paths must exist (i.e., zero-length paths
25811a8861f1SDaniel M. Lambea * don't match).
25821a8861f1SDaniel M. Lambea */
hid_compare_device_paths(struct hid_device * hdev_a,struct hid_device * hdev_b,char separator)25831a8861f1SDaniel M. Lambea bool hid_compare_device_paths(struct hid_device *hdev_a,
25841a8861f1SDaniel M. Lambea struct hid_device *hdev_b, char separator)
25851a8861f1SDaniel M. Lambea {
25861a8861f1SDaniel M. Lambea int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
25871a8861f1SDaniel M. Lambea int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
25881a8861f1SDaniel M. Lambea
25891a8861f1SDaniel M. Lambea if (n1 != n2 || n1 <= 0 || n2 <= 0)
25901a8861f1SDaniel M. Lambea return false;
25911a8861f1SDaniel M. Lambea
25921a8861f1SDaniel M. Lambea return !strncmp(hdev_a->phys, hdev_b->phys, n1);
25931a8861f1SDaniel M. Lambea }
25941a8861f1SDaniel M. Lambea EXPORT_SYMBOL_GPL(hid_compare_device_paths);
25951a8861f1SDaniel M. Lambea
hid_check_device_match(struct hid_device * hdev,struct hid_driver * hdrv,const struct hid_device_id ** id)2596207733f9SDmitry Torokhov static bool hid_check_device_match(struct hid_device *hdev,
2597207733f9SDmitry Torokhov struct hid_driver *hdrv,
2598207733f9SDmitry Torokhov const struct hid_device_id **id)
259985cdaf52SJiri Slaby {
2600207733f9SDmitry Torokhov *id = hid_match_device(hdev, hdrv);
2601207733f9SDmitry Torokhov if (!*id)
260289e756e3SDan Carpenter return false;
260385cdaf52SJiri Slaby
2604207733f9SDmitry Torokhov if (hdrv->match)
2605207733f9SDmitry Torokhov return hdrv->match(hdev, hid_ignore_special_drivers);
26064ea54542SDavid Herrmann
2607e04a0442SBenjamin Tissoires /*
2608207733f9SDmitry Torokhov * hid-generic implements .match(), so we must be dealing with a
2609207733f9SDmitry Torokhov * different HID driver here, and can simply check if
2610*115afb20SBenjamin Tissoires * hid_ignore_special_drivers or HID_QUIRK_IGNORE_SPECIAL_DRIVER
2611*115afb20SBenjamin Tissoires * are set or not.
2612e04a0442SBenjamin Tissoires */
2613*115afb20SBenjamin Tissoires return !hid_ignore_special_drivers && !(hdev->quirks & HID_QUIRK_IGNORE_SPECIAL_DRIVER);
2614e04a0442SBenjamin Tissoires }
2615207733f9SDmitry Torokhov
__hid_device_probe(struct hid_device * hdev,struct hid_driver * hdrv)2616207733f9SDmitry Torokhov static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv)
2617207733f9SDmitry Torokhov {
2618207733f9SDmitry Torokhov const struct hid_device_id *id;
2619207733f9SDmitry Torokhov int ret;
2620207733f9SDmitry Torokhov
2621207733f9SDmitry Torokhov if (!hid_check_device_match(hdev, hdrv, &id))
2622207733f9SDmitry Torokhov return -ENODEV;
2623e04a0442SBenjamin Tissoires
262462c68e7cSDmitry Torokhov hdev->devres_group_id = devres_open_group(&hdev->dev, NULL, GFP_KERNEL);
262562c68e7cSDmitry Torokhov if (!hdev->devres_group_id)
262662c68e7cSDmitry Torokhov return -ENOMEM;
262762c68e7cSDmitry Torokhov
26282904e68fSBenjamin Tissoires /* reset the quirks that has been previously set */
26292904e68fSBenjamin Tissoires hdev->quirks = hid_lookup_quirk(hdev);
263085cdaf52SJiri Slaby hdev->driver = hdrv;
2631207733f9SDmitry Torokhov
2632c500c971SJiri Slaby if (hdrv->probe) {
2633c500c971SJiri Slaby ret = hdrv->probe(hdev, id);
2634c500c971SJiri Slaby } else { /* default probe */
2635a7197c2eSHenrik Rydberg ret = hid_open_report(hdev);
2636c500c971SJiri Slaby if (!ret)
263793c10132SJiri Slaby ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2638c500c971SJiri Slaby }
2639207733f9SDmitry Torokhov
264062c68e7cSDmitry Torokhov /*
264162c68e7cSDmitry Torokhov * Note that we are not closing the devres group opened above so
264262c68e7cSDmitry Torokhov * even resources that were attached to the device after probe is
264362c68e7cSDmitry Torokhov * run are released when hid_device_remove() is executed. This is
264462c68e7cSDmitry Torokhov * needed as some drivers would allocate additional resources,
264562c68e7cSDmitry Torokhov * for example when updating firmware.
264662c68e7cSDmitry Torokhov */
264762c68e7cSDmitry Torokhov
2648a7197c2eSHenrik Rydberg if (ret) {
264962c68e7cSDmitry Torokhov devres_release_group(&hdev->dev, hdev->devres_group_id);
2650a7197c2eSHenrik Rydberg hid_close_report(hdev);
2651c500c971SJiri Slaby hdev->driver = NULL;
265285cdaf52SJiri Slaby }
2653207733f9SDmitry Torokhov
2654207733f9SDmitry Torokhov return ret;
2655a7197c2eSHenrik Rydberg }
2656207733f9SDmitry Torokhov
hid_device_probe(struct device * dev)2657207733f9SDmitry Torokhov static int hid_device_probe(struct device *dev)
2658207733f9SDmitry Torokhov {
2659207733f9SDmitry Torokhov struct hid_device *hdev = to_hid_device(dev);
2660207733f9SDmitry Torokhov struct hid_driver *hdrv = to_hid_driver(dev->driver);
2661207733f9SDmitry Torokhov int ret = 0;
2662207733f9SDmitry Torokhov
2663207733f9SDmitry Torokhov if (down_interruptible(&hdev->driver_input_lock))
2664207733f9SDmitry Torokhov return -EINTR;
2665207733f9SDmitry Torokhov
2666207733f9SDmitry Torokhov hdev->io_started = false;
2667207733f9SDmitry Torokhov clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
2668207733f9SDmitry Torokhov
2669207733f9SDmitry Torokhov if (!hdev->driver)
2670207733f9SDmitry Torokhov ret = __hid_device_probe(hdev, hdrv);
2671207733f9SDmitry Torokhov
2672c849a614SAndrew de los Reyes if (!hdev->io_started)
2673c849a614SAndrew de los Reyes up(&hdev->driver_input_lock);
2674207733f9SDmitry Torokhov
267585cdaf52SJiri Slaby return ret;
267685cdaf52SJiri Slaby }
267785cdaf52SJiri Slaby
hid_device_remove(struct device * dev)2678fc7a6209SUwe Kleine-König static void hid_device_remove(struct device *dev)
267985cdaf52SJiri Slaby {
2680ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev);
26814ea54542SDavid Herrmann struct hid_driver *hdrv;
268285cdaf52SJiri Slaby
2683f2145f8dSDmitry Torokhov down(&hdev->driver_input_lock);
2684c849a614SAndrew de los Reyes hdev->io_started = false;
26854ea54542SDavid Herrmann
26864ea54542SDavid Herrmann hdrv = hdev->driver;
268785cdaf52SJiri Slaby if (hdrv) {
268885cdaf52SJiri Slaby if (hdrv->remove)
268985cdaf52SJiri Slaby hdrv->remove(hdev);
2690c500c971SJiri Slaby else /* default remove */
2691c500c971SJiri Slaby hid_hw_stop(hdev);
269262c68e7cSDmitry Torokhov
269362c68e7cSDmitry Torokhov /* Release all devres resources allocated by the driver */
269462c68e7cSDmitry Torokhov devres_release_group(&hdev->dev, hdev->devres_group_id);
269562c68e7cSDmitry Torokhov
2696a7197c2eSHenrik Rydberg hid_close_report(hdev);
269785cdaf52SJiri Slaby hdev->driver = NULL;
269885cdaf52SJiri Slaby }
269985cdaf52SJiri Slaby
2700c849a614SAndrew de los Reyes if (!hdev->io_started)
2701c849a614SAndrew de los Reyes up(&hdev->driver_input_lock);
270285cdaf52SJiri Slaby }
270385cdaf52SJiri Slaby
modalias_show(struct device * dev,struct device_attribute * a,char * buf)27044d53b801SHenrik Rydberg static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
27054d53b801SHenrik Rydberg char *buf)
27064d53b801SHenrik Rydberg {
27074d53b801SHenrik Rydberg struct hid_device *hdev = container_of(dev, struct hid_device, dev);
27084d53b801SHenrik Rydberg
2709dfa0c5faSRasmus Villemoes return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
27104d53b801SHenrik Rydberg hdev->bus, hdev->group, hdev->vendor, hdev->product);
27114d53b801SHenrik Rydberg }
27120d4260e0SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
27134d53b801SHenrik Rydberg
27140d4260e0SGreg Kroah-Hartman static struct attribute *hid_dev_attrs[] = {
27150d4260e0SGreg Kroah-Hartman &dev_attr_modalias.attr,
27160d4260e0SGreg Kroah-Hartman NULL,
27174d53b801SHenrik Rydberg };
271854f32fd5SAndy Lutomirski static struct bin_attribute *hid_dev_bin_attrs[] = {
271954f32fd5SAndy Lutomirski &dev_bin_attr_report_desc,
272054f32fd5SAndy Lutomirski NULL
272154f32fd5SAndy Lutomirski };
272254f32fd5SAndy Lutomirski static const struct attribute_group hid_dev_group = {
272354f32fd5SAndy Lutomirski .attrs = hid_dev_attrs,
272454f32fd5SAndy Lutomirski .bin_attrs = hid_dev_bin_attrs,
272554f32fd5SAndy Lutomirski };
272654f32fd5SAndy Lutomirski __ATTRIBUTE_GROUPS(hid_dev);
27274d53b801SHenrik Rydberg
hid_uevent(const struct device * dev,struct kobj_uevent_env * env)27282a81ada3SGreg Kroah-Hartman static int hid_uevent(const struct device *dev, struct kobj_uevent_env *env)
272985cdaf52SJiri Slaby {
27302a81ada3SGreg Kroah-Hartman const struct hid_device *hdev = to_hid_device(dev);
273185cdaf52SJiri Slaby
273285cdaf52SJiri Slaby if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
273385cdaf52SJiri Slaby hdev->bus, hdev->vendor, hdev->product))
273485cdaf52SJiri Slaby return -ENOMEM;
273585cdaf52SJiri Slaby
273685cdaf52SJiri Slaby if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
273785cdaf52SJiri Slaby return -ENOMEM;
273885cdaf52SJiri Slaby
273985cdaf52SJiri Slaby if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
274085cdaf52SJiri Slaby return -ENOMEM;
274185cdaf52SJiri Slaby
274285cdaf52SJiri Slaby if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
274385cdaf52SJiri Slaby return -ENOMEM;
274485cdaf52SJiri Slaby
27454d53b801SHenrik Rydberg if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
27464d53b801SHenrik Rydberg hdev->bus, hdev->group, hdev->vendor, hdev->product))
274785cdaf52SJiri Slaby return -ENOMEM;
274885cdaf52SJiri Slaby
274985cdaf52SJiri Slaby return 0;
275085cdaf52SJiri Slaby }
275185cdaf52SJiri Slaby
2752e04a0442SBenjamin Tissoires struct bus_type hid_bus_type = {
275385cdaf52SJiri Slaby .name = "hid",
27540d4260e0SGreg Kroah-Hartman .dev_groups = hid_dev_groups,
2755c2810325SGreg Kroah-Hartman .drv_groups = hid_drv_groups,
275685cdaf52SJiri Slaby .match = hid_bus_match,
275785cdaf52SJiri Slaby .probe = hid_device_probe,
275885cdaf52SJiri Slaby .remove = hid_device_remove,
275985cdaf52SJiri Slaby .uevent = hid_uevent,
276085cdaf52SJiri Slaby };
2761d5d3e202SBenjamin Tissoires EXPORT_SYMBOL(hid_bus_type);
2762d458a9dfSJiri Slaby
hid_add_device(struct hid_device * hdev)276385cdaf52SJiri Slaby int hid_add_device(struct hid_device *hdev)
276485cdaf52SJiri Slaby {
276585cdaf52SJiri Slaby static atomic_t id = ATOMIC_INIT(0);
276685cdaf52SJiri Slaby int ret;
276785cdaf52SJiri Slaby
276885cdaf52SJiri Slaby if (WARN_ON(hdev->status & HID_STAT_ADDED))
276985cdaf52SJiri Slaby return -EBUSY;
277085cdaf52SJiri Slaby
2771d5d3e202SBenjamin Tissoires hdev->quirks = hid_lookup_quirk(hdev);
2772d5d3e202SBenjamin Tissoires
2773d458a9dfSJiri Slaby /* we need to kill them here, otherwise they will stay allocated to
2774d458a9dfSJiri Slaby * wait for coming driver */
27754529eefaSLamarque V. Souza if (hid_ignore(hdev))
2776d458a9dfSJiri Slaby return -ENODEV;
2777d458a9dfSJiri Slaby
2778a7197c2eSHenrik Rydberg /*
27793c86726cSBenjamin Tissoires * Check for the mandatory transport channel.
27803c86726cSBenjamin Tissoires */
27813c86726cSBenjamin Tissoires if (!hdev->ll_driver->raw_request) {
27823c86726cSBenjamin Tissoires hid_err(hdev, "transport driver missing .raw_request()\n");
27833c86726cSBenjamin Tissoires return -EINVAL;
27843c86726cSBenjamin Tissoires }
27853c86726cSBenjamin Tissoires
27863c86726cSBenjamin Tissoires /*
2787a7197c2eSHenrik Rydberg * Read the device report descriptor once and use as template
2788a7197c2eSHenrik Rydberg * for the driver-specific modifications.
2789a7197c2eSHenrik Rydberg */
2790a7197c2eSHenrik Rydberg ret = hdev->ll_driver->parse(hdev);
2791a7197c2eSHenrik Rydberg if (ret)
2792a7197c2eSHenrik Rydberg return ret;
2793a7197c2eSHenrik Rydberg if (!hdev->dev_rdesc)
2794a7197c2eSHenrik Rydberg return -ENODEV;
2795a7197c2eSHenrik Rydberg
2796734c6609SHenrik Rydberg /*
2797734c6609SHenrik Rydberg * Scan generic devices for group information
2798734c6609SHenrik Rydberg */
27994392bf33SBenjamin Tissoires if (hid_ignore_special_drivers) {
28004392bf33SBenjamin Tissoires hdev->group = HID_GROUP_GENERIC;
28014392bf33SBenjamin Tissoires } else if (!hdev->group &&
28026e65d9d5SBenjamin Tissoires !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
2803734c6609SHenrik Rydberg ret = hid_scan_report(hdev);
2804734c6609SHenrik Rydberg if (ret)
2805734c6609SHenrik Rydberg hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2806734c6609SHenrik Rydberg }
2807734c6609SHenrik Rydberg
28081e839143SBenjamin Tissoires hdev->id = atomic_inc_return(&id);
28091e839143SBenjamin Tissoires
28106bbe586fSKay Sievers /* XXX hack, any other cleaner solution after the driver core
28116bbe586fSKay Sievers * is converted to allow more than 20 bytes as the device name? */
28126bbe586fSKay Sievers dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
28131e839143SBenjamin Tissoires hdev->vendor, hdev->product, hdev->id);
281485cdaf52SJiri Slaby
28154da361b6SBruno Prémont hid_debug_register(hdev, dev_name(&hdev->dev));
281685cdaf52SJiri Slaby ret = device_add(&hdev->dev);
281785cdaf52SJiri Slaby if (!ret)
281885cdaf52SJiri Slaby hdev->status |= HID_STAT_ADDED;
28194da361b6SBruno Prémont else
28204da361b6SBruno Prémont hid_debug_unregister(hdev);
2821a635f9ddSJiri Kosina
282285cdaf52SJiri Slaby return ret;
282385cdaf52SJiri Slaby }
282485cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_add_device);
282585cdaf52SJiri Slaby
282685cdaf52SJiri Slaby /**
282785cdaf52SJiri Slaby * hid_allocate_device - allocate new hid device descriptor
282885cdaf52SJiri Slaby *
282985cdaf52SJiri Slaby * Allocate and initialize hid device, so that hid_destroy_device might be
283085cdaf52SJiri Slaby * used to free it.
283185cdaf52SJiri Slaby *
283285cdaf52SJiri Slaby * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
283385cdaf52SJiri Slaby * error value.
283485cdaf52SJiri Slaby */
hid_allocate_device(void)283585cdaf52SJiri Slaby struct hid_device *hid_allocate_device(void)
283685cdaf52SJiri Slaby {
283785cdaf52SJiri Slaby struct hid_device *hdev;
283885cdaf52SJiri Slaby int ret = -ENOMEM;
283985cdaf52SJiri Slaby
284085cdaf52SJiri Slaby hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
284185cdaf52SJiri Slaby if (hdev == NULL)
284285cdaf52SJiri Slaby return ERR_PTR(ret);
284385cdaf52SJiri Slaby
284485cdaf52SJiri Slaby device_initialize(&hdev->dev);
284585cdaf52SJiri Slaby hdev->dev.release = hid_device_release;
284685cdaf52SJiri Slaby hdev->dev.bus = &hid_bus_type;
284764bebefcSFu, Zhonghui device_enable_async_suspend(&hdev->dev);
284885cdaf52SJiri Slaby
2849a7197c2eSHenrik Rydberg hid_close_report(hdev);
285085cdaf52SJiri Slaby
2851cd667ce2SJiri Kosina init_waitqueue_head(&hdev->debug_wait);
2852cd667ce2SJiri Kosina INIT_LIST_HEAD(&hdev->debug_list);
28531deb9d34SJiri Kosina spin_lock_init(&hdev->debug_list_lock);
2854c849a614SAndrew de los Reyes sema_init(&hdev->driver_input_lock, 1);
2855aaac082dSDmitry Torokhov mutex_init(&hdev->ll_open_lock);
28566642322aSCharles Yi kref_init(&hdev->ref);
2857cd667ce2SJiri Kosina
2858f5c27da4SBenjamin Tissoires hid_bpf_device_init(hdev);
2859f5c27da4SBenjamin Tissoires
286085cdaf52SJiri Slaby return hdev;
286185cdaf52SJiri Slaby }
286285cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_allocate_device);
286385cdaf52SJiri Slaby
hid_remove_device(struct hid_device * hdev)286485cdaf52SJiri Slaby static void hid_remove_device(struct hid_device *hdev)
286585cdaf52SJiri Slaby {
286685cdaf52SJiri Slaby if (hdev->status & HID_STAT_ADDED) {
286785cdaf52SJiri Slaby device_del(&hdev->dev);
2868a635f9ddSJiri Kosina hid_debug_unregister(hdev);
286985cdaf52SJiri Slaby hdev->status &= ~HID_STAT_ADDED;
287085cdaf52SJiri Slaby }
2871a7197c2eSHenrik Rydberg kfree(hdev->dev_rdesc);
2872a7197c2eSHenrik Rydberg hdev->dev_rdesc = NULL;
2873a7197c2eSHenrik Rydberg hdev->dev_rsize = 0;
287485cdaf52SJiri Slaby }
287585cdaf52SJiri Slaby
287685cdaf52SJiri Slaby /**
287785cdaf52SJiri Slaby * hid_destroy_device - free previously allocated device
287885cdaf52SJiri Slaby *
287985cdaf52SJiri Slaby * @hdev: hid device
288085cdaf52SJiri Slaby *
288185cdaf52SJiri Slaby * If you allocate hid_device through hid_allocate_device, you should ever
288285cdaf52SJiri Slaby * free by this function.
288385cdaf52SJiri Slaby */
hid_destroy_device(struct hid_device * hdev)288485cdaf52SJiri Slaby void hid_destroy_device(struct hid_device *hdev)
288585cdaf52SJiri Slaby {
2886f5c27da4SBenjamin Tissoires hid_bpf_destroy_device(hdev);
288785cdaf52SJiri Slaby hid_remove_device(hdev);
288885cdaf52SJiri Slaby put_device(&hdev->dev);
288985cdaf52SJiri Slaby }
289085cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_destroy_device);
289185cdaf52SJiri Slaby
2892e04a0442SBenjamin Tissoires
__hid_bus_reprobe_drivers(struct device * dev,void * data)2893c17a7476SBenjamin Tissoires static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
2894e04a0442SBenjamin Tissoires {
2895c17a7476SBenjamin Tissoires struct hid_driver *hdrv = data;
2896c17a7476SBenjamin Tissoires struct hid_device *hdev = to_hid_device(dev);
2897c17a7476SBenjamin Tissoires
2898c17a7476SBenjamin Tissoires if (hdev->driver == hdrv &&
28998f732850SBenjamin Tissoires !hdrv->match(hdev, hid_ignore_special_drivers) &&
29008f732850SBenjamin Tissoires !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
2901c17a7476SBenjamin Tissoires return device_reprobe(dev);
2902c17a7476SBenjamin Tissoires
2903c17a7476SBenjamin Tissoires return 0;
2904c17a7476SBenjamin Tissoires }
2905c17a7476SBenjamin Tissoires
__hid_bus_driver_added(struct device_driver * drv,void * data)2906c17a7476SBenjamin Tissoires static int __hid_bus_driver_added(struct device_driver *drv, void *data)
2907c17a7476SBenjamin Tissoires {
2908e04a0442SBenjamin Tissoires struct hid_driver *hdrv = to_hid_driver(drv);
2909e04a0442SBenjamin Tissoires
2910c17a7476SBenjamin Tissoires if (hdrv->match) {
2911c17a7476SBenjamin Tissoires bus_for_each_dev(&hid_bus_type, NULL, hdrv,
2912c17a7476SBenjamin Tissoires __hid_bus_reprobe_drivers);
2913c17a7476SBenjamin Tissoires }
2914e04a0442SBenjamin Tissoires
2915e04a0442SBenjamin Tissoires return 0;
2916e04a0442SBenjamin Tissoires }
2917e04a0442SBenjamin Tissoires
__bus_removed_driver(struct device_driver * drv,void * data)2918e04a0442SBenjamin Tissoires static int __bus_removed_driver(struct device_driver *drv, void *data)
2919e04a0442SBenjamin Tissoires {
2920c17a7476SBenjamin Tissoires return bus_rescan_devices(&hid_bus_type);
2921e04a0442SBenjamin Tissoires }
2922e04a0442SBenjamin Tissoires
__hid_register_driver(struct hid_driver * hdrv,struct module * owner,const char * mod_name)292385cdaf52SJiri Slaby int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
292485cdaf52SJiri Slaby const char *mod_name)
292585cdaf52SJiri Slaby {
2926c17a7476SBenjamin Tissoires int ret;
2927c17a7476SBenjamin Tissoires
292885cdaf52SJiri Slaby hdrv->driver.name = hdrv->name;
292985cdaf52SJiri Slaby hdrv->driver.bus = &hid_bus_type;
293085cdaf52SJiri Slaby hdrv->driver.owner = owner;
293185cdaf52SJiri Slaby hdrv->driver.mod_name = mod_name;
293285cdaf52SJiri Slaby
29333a6f82f7SJiri Slaby INIT_LIST_HEAD(&hdrv->dyn_list);
29343a6f82f7SJiri Slaby spin_lock_init(&hdrv->dyn_lock);
29353a6f82f7SJiri Slaby
2936c17a7476SBenjamin Tissoires ret = driver_register(&hdrv->driver);
2937e04a0442SBenjamin Tissoires
2938c17a7476SBenjamin Tissoires if (ret == 0)
2939c17a7476SBenjamin Tissoires bus_for_each_drv(&hid_bus_type, NULL, NULL,
2940c17a7476SBenjamin Tissoires __hid_bus_driver_added);
2941c17a7476SBenjamin Tissoires
2942c17a7476SBenjamin Tissoires return ret;
294385cdaf52SJiri Slaby }
294485cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(__hid_register_driver);
294585cdaf52SJiri Slaby
hid_unregister_driver(struct hid_driver * hdrv)294685cdaf52SJiri Slaby void hid_unregister_driver(struct hid_driver *hdrv)
294785cdaf52SJiri Slaby {
294885cdaf52SJiri Slaby driver_unregister(&hdrv->driver);
29493a6f82f7SJiri Slaby hid_free_dynids(hdrv);
2950e04a0442SBenjamin Tissoires
2951e04a0442SBenjamin Tissoires bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
295285cdaf52SJiri Slaby }
295385cdaf52SJiri Slaby EXPORT_SYMBOL_GPL(hid_unregister_driver);
295485cdaf52SJiri Slaby
hid_check_keys_pressed(struct hid_device * hid)29550361a28dSOliver Neukum int hid_check_keys_pressed(struct hid_device *hid)
29560361a28dSOliver Neukum {
29570361a28dSOliver Neukum struct hid_input *hidinput;
29580361a28dSOliver Neukum int i;
29590361a28dSOliver Neukum
2960e5288eb5SJiri Kosina if (!(hid->claimed & HID_CLAIMED_INPUT))
2961e5288eb5SJiri Kosina return 0;
2962e5288eb5SJiri Kosina
29630361a28dSOliver Neukum list_for_each_entry(hidinput, &hid->inputs, list) {
29640361a28dSOliver Neukum for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
29650361a28dSOliver Neukum if (hidinput->input->key[i])
29660361a28dSOliver Neukum return 1;
29670361a28dSOliver Neukum }
29680361a28dSOliver Neukum
29690361a28dSOliver Neukum return 0;
29700361a28dSOliver Neukum }
29710361a28dSOliver Neukum EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
29720361a28dSOliver Neukum
2973f5c27da4SBenjamin Tissoires #ifdef CONFIG_HID_BPF
2974f5c27da4SBenjamin Tissoires static struct hid_bpf_ops hid_ops = {
297591a7f802SBenjamin Tissoires .hid_get_report = hid_get_report,
297691a7f802SBenjamin Tissoires .hid_hw_raw_request = hid_hw_raw_request,
2977f5c27da4SBenjamin Tissoires .owner = THIS_MODULE,
2978f5c27da4SBenjamin Tissoires .bus_type = &hid_bus_type,
2979f5c27da4SBenjamin Tissoires };
2980f5c27da4SBenjamin Tissoires #endif
2981f5c27da4SBenjamin Tissoires
hid_init(void)298286166b7bSJiri Kosina static int __init hid_init(void)
298386166b7bSJiri Kosina {
298485cdaf52SJiri Slaby int ret;
298585cdaf52SJiri Slaby
298685cdaf52SJiri Slaby ret = bus_register(&hid_bus_type);
298785cdaf52SJiri Slaby if (ret) {
29884291ee30SJoe Perches pr_err("can't register hid bus\n");
298985cdaf52SJiri Slaby goto err;
299085cdaf52SJiri Slaby }
299185cdaf52SJiri Slaby
2992f5c27da4SBenjamin Tissoires #ifdef CONFIG_HID_BPF
2993f5c27da4SBenjamin Tissoires hid_bpf_ops = &hid_ops;
2994f5c27da4SBenjamin Tissoires #endif
2995f5c27da4SBenjamin Tissoires
299685cdaf52SJiri Slaby ret = hidraw_init();
299785cdaf52SJiri Slaby if (ret)
299885cdaf52SJiri Slaby goto err_bus;
299985cdaf52SJiri Slaby
3000a635f9ddSJiri Kosina hid_debug_init();
3001a635f9ddSJiri Kosina
300285cdaf52SJiri Slaby return 0;
300385cdaf52SJiri Slaby err_bus:
300485cdaf52SJiri Slaby bus_unregister(&hid_bus_type);
300585cdaf52SJiri Slaby err:
300685cdaf52SJiri Slaby return ret;
300786166b7bSJiri Kosina }
300886166b7bSJiri Kosina
hid_exit(void)300986166b7bSJiri Kosina static void __exit hid_exit(void)
301086166b7bSJiri Kosina {
3011f5c27da4SBenjamin Tissoires #ifdef CONFIG_HID_BPF
3012f5c27da4SBenjamin Tissoires hid_bpf_ops = NULL;
3013f5c27da4SBenjamin Tissoires #endif
3014a635f9ddSJiri Kosina hid_debug_exit();
301586166b7bSJiri Kosina hidraw_exit();
301685cdaf52SJiri Slaby bus_unregister(&hid_bus_type);
3017d5d3e202SBenjamin Tissoires hid_quirks_exit(HID_BUS_ANY);
301886166b7bSJiri Kosina }
301986166b7bSJiri Kosina
302086166b7bSJiri Kosina module_init(hid_init);
302186166b7bSJiri Kosina module_exit(hid_exit);
302286166b7bSJiri Kosina
302388adb72bSJiri Kosina MODULE_AUTHOR("Andreas Gal");
302488adb72bSJiri Kosina MODULE_AUTHOR("Vojtech Pavlik");
302588adb72bSJiri Kosina MODULE_AUTHOR("Jiri Kosina");
30267021b600SGrant Grundler MODULE_LICENSE("GPL");
3027