blob: 402ad974b31c478bfe83a59f855cce5e29c65f92 [file] [log] [blame]
Jiri Kosinadde58452006-12-08 18:40:44 +01001/*
Jiri Kosina229695e2006-12-08 18:40:53 +01002 * HID support for Linux
Jiri Kosinadde58452006-12-08 18:40:44 +01003 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Jiri Kosina6b1968d2012-03-09 13:55:43 +01007 * Copyright (c) 2006-2012 Jiri Kosina
Jiri Kosinadde58452006-12-08 18:40:44 +01008 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 */
16
Joe Perches4291ee32010-12-09 19:29:03 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Jiri Kosinadde58452006-12-08 18:40:44 +010019#include <linux/module.h>
20#include <linux/slab.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010023#include <linux/list.h>
24#include <linux/mm.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010025#include <linux/spinlock.h>
26#include <asm/unaligned.h>
27#include <asm/byteorder.h>
28#include <linux/input.h>
29#include <linux/wait.h>
Jiri Kosina47a80ed2007-03-12 14:55:12 +010030#include <linux/vmalloc.h>
Jiri Kosinac4124c92007-11-30 11:12:58 +010031#include <linux/sched.h>
David Herrmann4ea54542011-08-10 14:02:07 +020032#include <linux/semaphore.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010033
Jiri Kosinadde58452006-12-08 18:40:44 +010034#include <linux/hid.h>
35#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010036#include <linux/hid-debug.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020037#include <linux/hidraw.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010038
Jiri Slaby5f22a792008-05-16 11:49:19 +020039#include "hid-ids.h"
40
Jiri Kosinadde58452006-12-08 18:40:44 +010041/*
42 * Version Information
43 */
44
Jiri Kosina53149802007-01-09 13:24:25 +010045#define DRIVER_DESC "HID core driver"
Jiri Kosinadde58452006-12-08 18:40:44 +010046
Jiri Kosina58037eb2007-05-30 15:07:13 +020047int hid_debug = 0;
Anssi Hannula377e10f2008-03-22 23:50:13 +010048module_param_named(debug, hid_debug, int, 0600);
Jiri Kosinacd667ce2009-06-12 15:20:57 +020049MODULE_PARM_DESC(debug, "toggle HID debugging messages");
Jiri Kosina58037eb2007-05-30 15:07:13 +020050EXPORT_SYMBOL_GPL(hid_debug);
Jiri Kosina58037eb2007-05-30 15:07:13 +020051
Jiri Kosina6b1968d2012-03-09 13:55:43 +010052static int hid_ignore_special_drivers = 0;
53module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
Hans Petter Selasky643727a2014-09-08 09:35:35 +020054MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
Jiri Kosina6b1968d2012-03-09 13:55:43 +010055
Jiri Kosinadde58452006-12-08 18:40:44 +010056/*
Jiri Kosinadde58452006-12-08 18:40:44 +010057 * Register a new report for a device.
58 */
59
Benjamin Tissoiresf07b3c12018-04-24 10:04:33 +020060struct hid_report *hid_register_report(struct hid_device *device,
61 unsigned int type, unsigned int id,
62 unsigned int application)
Jiri Kosinadde58452006-12-08 18:40:44 +010063{
64 struct hid_report_enum *report_enum = device->report_enum + type;
65 struct hid_report *report;
66
Kees Cook43622022013-08-28 22:29:55 +020067 if (id >= HID_MAX_IDS)
68 return NULL;
Jiri Kosinadde58452006-12-08 18:40:44 +010069 if (report_enum->report_id_hash[id])
70 return report_enum->report_id_hash[id];
71
Joe Perchesa3789a12010-12-09 19:29:07 -080072 report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
73 if (!report)
Jiri Kosinadde58452006-12-08 18:40:44 +010074 return NULL;
75
76 if (id != 0)
77 report_enum->numbered = 1;
78
79 report->id = id;
80 report->type = type;
81 report->size = 0;
82 report->device = device;
Benjamin Tissoiresf07b3c12018-04-24 10:04:33 +020083 report->application = application;
Jiri Kosinadde58452006-12-08 18:40:44 +010084 report_enum->report_id_hash[id] = report;
85
86 list_add_tail(&report->list, &report_enum->report_list);
87
88 return report;
89}
Michael Poole90a006a2010-01-24 22:32:29 -050090EXPORT_SYMBOL_GPL(hid_register_report);
Jiri Kosinadde58452006-12-08 18:40:44 +010091
92/*
93 * Register a new field for this report.
94 */
95
96static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
97{
98 struct hid_field *field;
99
100 if (report->maxfield == HID_MAX_FIELDS) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100101 hid_err(report->device, "too many fields in report\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100102 return NULL;
103 }
104
Joe Perchesa3789a12010-12-09 19:29:07 -0800105 field = kzalloc((sizeof(struct hid_field) +
106 usages * sizeof(struct hid_usage) +
107 values * sizeof(unsigned)), GFP_KERNEL);
108 if (!field)
109 return NULL;
Jiri Kosinadde58452006-12-08 18:40:44 +0100110
111 field->index = report->maxfield++;
112 report->field[field->index] = field;
113 field->usage = (struct hid_usage *)(field + 1);
Jiri Slaby282bfd42008-03-28 17:06:41 +0100114 field->value = (s32 *)(field->usage + usages);
Jiri Kosinadde58452006-12-08 18:40:44 +0100115 field->report = report;
116
117 return field;
118}
119
120/*
121 * Open a collection. The type/usage is pushed on the stack.
122 */
123
124static int open_collection(struct hid_parser *parser, unsigned type)
125{
126 struct hid_collection *collection;
127 unsigned usage;
128
129 usage = parser->local.usage[0];
130
131 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100132 hid_err(parser->device, "collection stack overflow\n");
Sachin Kamata6fbaac2012-09-17 16:41:56 +0530133 return -EINVAL;
Jiri Kosinadde58452006-12-08 18:40:44 +0100134 }
135
136 if (parser->device->maxcollection == parser->device->collection_size) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700137 collection = kmalloc(
138 array3_size(sizeof(struct hid_collection),
139 parser->device->collection_size,
140 2),
141 GFP_KERNEL);
Jiri Kosinadde58452006-12-08 18:40:44 +0100142 if (collection == NULL) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100143 hid_err(parser->device, "failed to reallocate collection array\n");
Sachin Kamata6fbaac2012-09-17 16:41:56 +0530144 return -ENOMEM;
Jiri Kosinadde58452006-12-08 18:40:44 +0100145 }
146 memcpy(collection, parser->device->collection,
147 sizeof(struct hid_collection) *
148 parser->device->collection_size);
149 memset(collection + parser->device->collection_size, 0,
150 sizeof(struct hid_collection) *
151 parser->device->collection_size);
152 kfree(parser->device->collection);
153 parser->device->collection = collection;
154 parser->device->collection_size *= 2;
155 }
156
157 parser->collection_stack[parser->collection_stack_ptr++] =
158 parser->device->maxcollection;
159
160 collection = parser->device->collection +
161 parser->device->maxcollection++;
162 collection->type = type;
163 collection->usage = usage;
164 collection->level = parser->collection_stack_ptr - 1;
165
166 if (type == HID_COLLECTION_APPLICATION)
167 parser->device->maxapplication++;
168
169 return 0;
170}
171
172/*
173 * Close a collection.
174 */
175
176static int close_collection(struct hid_parser *parser)
177{
178 if (!parser->collection_stack_ptr) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100179 hid_err(parser->device, "collection stack underflow\n");
Sachin Kamata6fbaac2012-09-17 16:41:56 +0530180 return -EINVAL;
Jiri Kosinadde58452006-12-08 18:40:44 +0100181 }
182 parser->collection_stack_ptr--;
183 return 0;
184}
185
186/*
187 * Climb up the stack, search for the specified collection type
188 * and return the usage.
189 */
190
191static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
192{
Joe Perches504499f2010-12-09 19:29:08 -0800193 struct hid_collection *collection = parser->device->collection;
Jiri Kosinadde58452006-12-08 18:40:44 +0100194 int n;
Joe Perches504499f2010-12-09 19:29:08 -0800195
196 for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
197 unsigned index = parser->collection_stack[n];
198 if (collection[index].type == type)
199 return collection[index].usage;
200 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100201 return 0; /* we know nothing about this usage type */
202}
203
204/*
205 * Add a usage to the temporary parser table.
206 */
207
208static int hid_add_usage(struct hid_parser *parser, unsigned usage)
209{
210 if (parser->local.usage_index >= HID_MAX_USAGES) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100211 hid_err(parser->device, "usage index exceeded\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100212 return -1;
213 }
214 parser->local.usage[parser->local.usage_index] = usage;
215 parser->local.collection_index[parser->local.usage_index] =
216 parser->collection_stack_ptr ?
217 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
218 parser->local.usage_index++;
219 return 0;
220}
221
222/*
223 * Register a new field for this report.
224 */
225
226static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
227{
228 struct hid_report *report;
229 struct hid_field *field;
Benjamin Tissoiresf07b3c12018-04-24 10:04:33 +0200230 unsigned int usages;
231 unsigned int offset;
232 unsigned int i;
233 unsigned int application;
Jiri Kosinadde58452006-12-08 18:40:44 +0100234
Benjamin Tissoiresf07b3c12018-04-24 10:04:33 +0200235 application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
236
237 report = hid_register_report(parser->device, report_type,
238 parser->global.report_id, application);
Joe Perchesa3789a12010-12-09 19:29:07 -0800239 if (!report) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100240 hid_err(parser->device, "hid_register_report failed\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100241 return -1;
242 }
243
Jiri Kosinabb2e1972012-05-14 15:02:56 +0200244 /* Handle both signed and unsigned cases properly */
srinivas pandruvada0cd516c2012-05-10 16:45:31 -0700245 if ((parser->global.logical_minimum < 0 &&
246 parser->global.logical_maximum <
247 parser->global.logical_minimum) ||
248 (parser->global.logical_minimum >= 0 &&
249 (__u32)parser->global.logical_maximum <
250 (__u32)parser->global.logical_minimum)) {
251 dbg_hid("logical range invalid 0x%x 0x%x\n",
252 parser->global.logical_minimum,
253 parser->global.logical_maximum);
Jiri Kosinadde58452006-12-08 18:40:44 +0100254 return -1;
255 }
256
257 offset = report->size;
258 report->size += parser->global.report_size * parser->global.report_count;
259
260 if (!parser->local.usage_index) /* Ignore padding fields */
261 return 0;
262
Benjamin Tissoirescc6b54a2013-09-11 21:56:57 +0200263 usages = max_t(unsigned, parser->local.usage_index,
264 parser->global.report_count);
Jiri Kosinadde58452006-12-08 18:40:44 +0100265
Joe Perchesa3789a12010-12-09 19:29:07 -0800266 field = hid_register_field(report, usages, parser->global.report_count);
267 if (!field)
Jiri Kosinadde58452006-12-08 18:40:44 +0100268 return 0;
269
270 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
271 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
Benjamin Tissoiresf07b3c12018-04-24 10:04:33 +0200272 field->application = application;
Jiri Kosinadde58452006-12-08 18:40:44 +0100273
274 for (i = 0; i < usages; i++) {
Benjamin Tissoirescc6b54a2013-09-11 21:56:57 +0200275 unsigned j = i;
Jiri Kosinadde58452006-12-08 18:40:44 +0100276 /* Duplicate the last usage we parsed if we have excess values */
277 if (i >= parser->local.usage_index)
278 j = parser->local.usage_index - 1;
279 field->usage[i].hid = parser->local.usage[j];
280 field->usage[i].collection_index =
281 parser->local.collection_index[j];
Benjamin Tissoirescc6b54a2013-09-11 21:56:57 +0200282 field->usage[i].usage_index = i;
Jiri Kosinadde58452006-12-08 18:40:44 +0100283 }
284
285 field->maxusage = usages;
286 field->flags = flags;
287 field->report_offset = offset;
288 field->report_type = report_type;
289 field->report_size = parser->global.report_size;
290 field->report_count = parser->global.report_count;
291 field->logical_minimum = parser->global.logical_minimum;
292 field->logical_maximum = parser->global.logical_maximum;
293 field->physical_minimum = parser->global.physical_minimum;
294 field->physical_maximum = parser->global.physical_maximum;
295 field->unit_exponent = parser->global.unit_exponent;
296 field->unit = parser->global.unit;
297
298 return 0;
299}
300
301/*
302 * Read data value from item.
303 */
304
305static u32 item_udata(struct hid_item *item)
306{
307 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200308 case 1: return item->data.u8;
309 case 2: return item->data.u16;
310 case 4: return item->data.u32;
Jiri Kosinadde58452006-12-08 18:40:44 +0100311 }
312 return 0;
313}
314
315static s32 item_sdata(struct hid_item *item)
316{
317 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200318 case 1: return item->data.s8;
319 case 2: return item->data.s16;
320 case 4: return item->data.s32;
Jiri Kosinadde58452006-12-08 18:40:44 +0100321 }
322 return 0;
323}
324
325/*
326 * Process a global item.
327 */
328
329static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
330{
Nikolai Kondrashovad0e669b2013-10-13 15:09:52 +0300331 __s32 raw_value;
Jiri Kosinadde58452006-12-08 18:40:44 +0100332 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200333 case HID_GLOBAL_ITEM_TAG_PUSH:
Jiri Kosinadde58452006-12-08 18:40:44 +0100334
Jiri Slaby880d29f2008-06-18 23:55:41 +0200335 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100336 hid_err(parser->device, "global environment stack overflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100337 return -1;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200338 }
339
340 memcpy(parser->global_stack + parser->global_stack_ptr++,
341 &parser->global, sizeof(struct hid_global));
342 return 0;
343
344 case HID_GLOBAL_ITEM_TAG_POP:
345
346 if (!parser->global_stack_ptr) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100347 hid_err(parser->device, "global environment stack underflow\n");
Jiri Slaby880d29f2008-06-18 23:55:41 +0200348 return -1;
349 }
350
351 memcpy(&parser->global, parser->global_stack +
352 --parser->global_stack_ptr, sizeof(struct hid_global));
353 return 0;
354
355 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
356 parser->global.usage_page = item_udata(item);
357 return 0;
358
359 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
360 parser->global.logical_minimum = item_sdata(item);
361 return 0;
362
363 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
364 if (parser->global.logical_minimum < 0)
365 parser->global.logical_maximum = item_sdata(item);
366 else
367 parser->global.logical_maximum = item_udata(item);
368 return 0;
369
370 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
371 parser->global.physical_minimum = item_sdata(item);
372 return 0;
373
374 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
375 if (parser->global.physical_minimum < 0)
376 parser->global.physical_maximum = item_sdata(item);
377 else
378 parser->global.physical_maximum = item_udata(item);
379 return 0;
380
381 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
Nikolai Kondrashovad0e669b2013-10-13 15:09:52 +0300382 /* Many devices provide unit exponent as a two's complement
383 * nibble due to the common misunderstanding of HID
384 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
385 * both this and the standard encoding. */
386 raw_value = item_sdata(item);
Benjamin Tissoires77463832012-11-14 16:59:15 +0100387 if (!(raw_value & 0xfffffff0))
388 parser->global.unit_exponent = hid_snto32(raw_value, 4);
389 else
390 parser->global.unit_exponent = raw_value;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200391 return 0;
392
393 case HID_GLOBAL_ITEM_TAG_UNIT:
394 parser->global.unit = item_udata(item);
395 return 0;
396
397 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
398 parser->global.report_size = item_udata(item);
Marek Vasut23408f92012-08-05 23:57:14 +0200399 if (parser->global.report_size > 128) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100400 hid_err(parser->device, "invalid report_size %d\n",
Jiri Slaby880d29f2008-06-18 23:55:41 +0200401 parser->global.report_size);
402 return -1;
403 }
404 return 0;
405
406 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
407 parser->global.report_count = item_udata(item);
408 if (parser->global.report_count > HID_MAX_USAGES) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100409 hid_err(parser->device, "invalid report_count %d\n",
Jiri Slaby880d29f2008-06-18 23:55:41 +0200410 parser->global.report_count);
411 return -1;
412 }
413 return 0;
414
415 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
416 parser->global.report_id = item_udata(item);
Kees Cook43622022013-08-28 22:29:55 +0200417 if (parser->global.report_id == 0 ||
418 parser->global.report_id >= HID_MAX_IDS) {
419 hid_err(parser->device, "report_id %u is invalid\n",
420 parser->global.report_id);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200421 return -1;
422 }
423 return 0;
424
425 default:
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100426 hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200427 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100428 }
429}
430
431/*
432 * Process a local item.
433 */
434
435static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
436{
437 __u32 data;
438 unsigned n;
Benjamin Tissoiresba532192015-07-24 12:02:22 -0400439 __u32 count;
Jiri Kosinadde58452006-12-08 18:40:44 +0100440
Jiri Kosinadde58452006-12-08 18:40:44 +0100441 data = item_udata(item);
442
443 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200444 case HID_LOCAL_ITEM_TAG_DELIMITER:
Jiri Kosinadde58452006-12-08 18:40:44 +0100445
Jiri Slaby880d29f2008-06-18 23:55:41 +0200446 if (data) {
447 /*
448 * We treat items before the first delimiter
449 * as global to all usage sets (branch 0).
450 * In the moment we process only these global
451 * items and the first delimiter set.
452 */
453 if (parser->local.delimiter_depth != 0) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100454 hid_err(parser->device, "nested delimiters\n");
Jiri Slaby880d29f2008-06-18 23:55:41 +0200455 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100456 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200457 parser->local.delimiter_depth++;
458 parser->local.delimiter_branch++;
459 } else {
460 if (parser->local.delimiter_depth < 1) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +0100461 hid_err(parser->device, "bogus close delimiter\n");
Jiri Slaby880d29f2008-06-18 23:55:41 +0200462 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100463 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200464 parser->local.delimiter_depth--;
465 }
Paul Chavent38ead6e2013-07-07 17:43:56 +0200466 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100467
Jiri Slaby880d29f2008-06-18 23:55:41 +0200468 case HID_LOCAL_ITEM_TAG_USAGE:
Jiri Kosinadde58452006-12-08 18:40:44 +0100469
Jiri Slaby880d29f2008-06-18 23:55:41 +0200470 if (parser->local.delimiter_branch > 1) {
471 dbg_hid("alternative usage ignored\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100472 return 0;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200473 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100474
Jiri Slaby880d29f2008-06-18 23:55:41 +0200475 if (item->size <= 2)
476 data = (parser->global.usage_page << 16) + data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100477
Jiri Slaby880d29f2008-06-18 23:55:41 +0200478 return hid_add_usage(parser, data);
479
480 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
481
482 if (parser->local.delimiter_branch > 1) {
483 dbg_hid("alternative usage ignored\n");
484 return 0;
485 }
486
487 if (item->size <= 2)
488 data = (parser->global.usage_page << 16) + data;
489
490 parser->local.usage_minimum = data;
491 return 0;
492
493 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
494
495 if (parser->local.delimiter_branch > 1) {
496 dbg_hid("alternative usage ignored\n");
497 return 0;
498 }
499
500 if (item->size <= 2)
501 data = (parser->global.usage_page << 16) + data;
502
Benjamin Tissoiresba532192015-07-24 12:02:22 -0400503 count = data - parser->local.usage_minimum;
504 if (count + parser->local.usage_index >= HID_MAX_USAGES) {
505 /*
506 * We do not warn if the name is not set, we are
507 * actually pre-scanning the device.
508 */
509 if (dev_name(&parser->device->dev))
510 hid_warn(parser->device,
511 "ignoring exceeding usage max\n");
512 data = HID_MAX_USAGES - parser->local.usage_index +
513 parser->local.usage_minimum - 1;
514 if (data <= 0) {
515 hid_err(parser->device,
516 "no more usage index available\n");
517 return -1;
518 }
519 }
520
Jiri Slaby880d29f2008-06-18 23:55:41 +0200521 for (n = parser->local.usage_minimum; n <= data; n++)
522 if (hid_add_usage(parser, n)) {
523 dbg_hid("hid_add_usage failed\n");
524 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100525 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200526 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100527
Jiri Slaby880d29f2008-06-18 23:55:41 +0200528 default:
Jiri Kosinadde58452006-12-08 18:40:44 +0100529
Jiri Slaby880d29f2008-06-18 23:55:41 +0200530 dbg_hid("unknown local item tag 0x%x\n", item->tag);
531 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100532 }
533 return 0;
534}
535
536/*
537 * Process a main item.
538 */
539
540static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
541{
542 __u32 data;
543 int ret;
544
545 data = item_udata(item);
546
547 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200548 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
549 ret = open_collection(parser, data & 0xff);
550 break;
551 case HID_MAIN_ITEM_TAG_END_COLLECTION:
552 ret = close_collection(parser);
553 break;
554 case HID_MAIN_ITEM_TAG_INPUT:
555 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
556 break;
557 case HID_MAIN_ITEM_TAG_OUTPUT:
558 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
559 break;
560 case HID_MAIN_ITEM_TAG_FEATURE:
561 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
562 break;
563 default:
Hans de Goede7cb47742017-12-06 17:54:38 +0100564 hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200565 ret = 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100566 }
567
568 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
569
570 return ret;
571}
572
573/*
574 * Process a reserved item.
575 */
576
577static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
578{
Jiri Kosina58037eb2007-05-30 15:07:13 +0200579 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
Jiri Kosinadde58452006-12-08 18:40:44 +0100580 return 0;
581}
582
583/*
584 * Free a report and all registered fields. The field->usage and
585 * field->value table's are allocated behind the field, so we need
586 * only to free(field) itself.
587 */
588
589static void hid_free_report(struct hid_report *report)
590{
591 unsigned n;
592
593 for (n = 0; n < report->maxfield; n++)
594 kfree(report->field[n]);
595 kfree(report);
596}
597
598/*
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200599 * Close report. This function returns the device
600 * state to the point prior to hid_open_report().
Jiri Kosinadde58452006-12-08 18:40:44 +0100601 */
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200602static void hid_close_report(struct hid_device *device)
Jiri Kosinadde58452006-12-08 18:40:44 +0100603{
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200604 unsigned i, j;
Jiri Kosinadde58452006-12-08 18:40:44 +0100605
606 for (i = 0; i < HID_REPORT_TYPES; i++) {
607 struct hid_report_enum *report_enum = device->report_enum + i;
608
Kees Cook43622022013-08-28 22:29:55 +0200609 for (j = 0; j < HID_MAX_IDS; j++) {
Jiri Kosinadde58452006-12-08 18:40:44 +0100610 struct hid_report *report = report_enum->report_id_hash[j];
611 if (report)
612 hid_free_report(report);
613 }
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200614 memset(report_enum, 0, sizeof(*report_enum));
615 INIT_LIST_HEAD(&report_enum->report_list);
Jiri Kosinadde58452006-12-08 18:40:44 +0100616 }
617
618 kfree(device->rdesc);
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200619 device->rdesc = NULL;
620 device->rsize = 0;
621
Jiri Kosina767fe782007-01-24 23:05:07 +0100622 kfree(device->collection);
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200623 device->collection = NULL;
624 device->collection_size = 0;
625 device->maxcollection = 0;
626 device->maxapplication = 0;
627
628 device->status &= ~HID_STAT_PARSED;
629}
630
631/*
632 * Free a device structure, all reports, and all fields.
633 */
634
635static void hid_device_release(struct device *dev)
636{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800637 struct hid_device *hid = to_hid_device(dev);
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200638
639 hid_close_report(hid);
640 kfree(hid->dev_rdesc);
641 kfree(hid);
Jiri Kosinadde58452006-12-08 18:40:44 +0100642}
643
644/*
645 * Fetch a report description item from the data stream. We support long
646 * items, though they are not used yet.
647 */
648
649static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
650{
651 u8 b;
652
653 if ((end - start) <= 0)
654 return NULL;
655
656 b = *start++;
657
658 item->type = (b >> 2) & 3;
659 item->tag = (b >> 4) & 15;
660
661 if (item->tag == HID_ITEM_TAG_LONG) {
662
663 item->format = HID_ITEM_FORMAT_LONG;
664
665 if ((end - start) < 2)
666 return NULL;
667
668 item->size = *start++;
669 item->tag = *start++;
670
671 if ((end - start) < item->size)
672 return NULL;
673
674 item->data.longdata = start;
675 start += item->size;
676 return start;
677 }
678
679 item->format = HID_ITEM_FORMAT_SHORT;
680 item->size = b & 3;
681
682 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200683 case 0:
684 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100685
Jiri Slaby880d29f2008-06-18 23:55:41 +0200686 case 1:
687 if ((end - start) < 1)
688 return NULL;
689 item->data.u8 = *start++;
690 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100691
Jiri Slaby880d29f2008-06-18 23:55:41 +0200692 case 2:
693 if ((end - start) < 2)
694 return NULL;
695 item->data.u16 = get_unaligned_le16(start);
696 start = (__u8 *)((__le16 *)start + 1);
697 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100698
Jiri Slaby880d29f2008-06-18 23:55:41 +0200699 case 3:
700 item->size++;
701 if ((end - start) < 4)
702 return NULL;
703 item->data.u32 = get_unaligned_le32(start);
704 start = (__u8 *)((__le32 *)start + 1);
705 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100706 }
707
708 return NULL;
709}
710
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200711static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
Henrik Rydberg734c6602012-04-23 12:07:03 +0200712{
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200713 struct hid_device *hid = parser->device;
714
Henrik Rydberg4fa3a582012-05-01 08:40:01 +0200715 if (usage == HID_DG_CONTACTID)
716 hid->group = HID_GROUP_MULTITOUCH;
Henrik Rydberg734c6602012-04-23 12:07:03 +0200717}
718
Benjamin Tissoiresf961bd32013-08-22 14:51:08 +0200719static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
720{
721 if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
722 parser->global.report_size == 8)
723 parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
724}
725
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200726static void hid_scan_collection(struct hid_parser *parser, unsigned type)
727{
728 struct hid_device *hid = parser->device;
Andrew Duggane39f2d52014-12-12 10:17:26 -0800729 int i;
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200730
731 if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
732 type == HID_COLLECTION_PHYSICAL)
733 hid->group = HID_GROUP_SENSOR_HUB;
Alan Wube3b1632014-11-03 18:26:12 -0800734
735 if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
Dennis Chen0a76ac82017-01-19 06:32:09 -0500736 hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
Alan Wube3b1632014-11-03 18:26:12 -0800737 hid->group == HID_GROUP_MULTITOUCH)
738 hid->group = HID_GROUP_GENERIC;
Andrew Duggane39f2d52014-12-12 10:17:26 -0800739
740 if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
741 for (i = 0; i < parser->local.usage_index; i++)
742 if (parser->local.usage[i] == HID_GD_POINTER)
743 parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
744
745 if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
746 parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200747}
748
749static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
750{
751 __u32 data;
752 int i;
753
754 data = item_udata(item);
755
756 switch (item->tag) {
757 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
758 hid_scan_collection(parser, data & 0xff);
759 break;
760 case HID_MAIN_ITEM_TAG_END_COLLECTION:
761 break;
762 case HID_MAIN_ITEM_TAG_INPUT:
Benjamin Tissoirese24d0d32014-03-31 13:27:10 -0400763 /* ignore constant inputs, they will be ignored by hid-input */
764 if (data & HID_MAIN_ITEM_CONSTANT)
765 break;
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200766 for (i = 0; i < parser->local.usage_index; i++)
767 hid_scan_input_usage(parser, parser->local.usage[i]);
768 break;
769 case HID_MAIN_ITEM_TAG_OUTPUT:
770 break;
771 case HID_MAIN_ITEM_TAG_FEATURE:
Benjamin Tissoiresf961bd32013-08-22 14:51:08 +0200772 for (i = 0; i < parser->local.usage_index; i++)
773 hid_scan_feature_usage(parser, parser->local.usage[i]);
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200774 break;
775 }
776
777 /* Reset the local parser environment */
778 memset(&parser->local, 0, sizeof(parser->local));
779
780 return 0;
781}
782
Henrik Rydberg734c6602012-04-23 12:07:03 +0200783/*
784 * Scan a report descriptor before the device is added to the bus.
785 * Sets device groups and other properties that determine what driver
786 * to load.
787 */
788static int hid_scan_report(struct hid_device *hid)
789{
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200790 struct hid_parser *parser;
791 struct hid_item item;
Henrik Rydberg734c6602012-04-23 12:07:03 +0200792 __u8 *start = hid->dev_rdesc;
793 __u8 *end = start + hid->dev_rsize;
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200794 static int (*dispatch_type[])(struct hid_parser *parser,
795 struct hid_item *item) = {
796 hid_scan_main,
797 hid_parser_global,
798 hid_parser_local,
799 hid_parser_reserved
800 };
Henrik Rydberg734c6602012-04-23 12:07:03 +0200801
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200802 parser = vzalloc(sizeof(struct hid_parser));
803 if (!parser)
804 return -ENOMEM;
805
806 parser->device = hid;
Henrik Rydberg734c6602012-04-23 12:07:03 +0200807 hid->group = HID_GROUP_GENERIC;
Henrik Rydberg734c6602012-04-23 12:07:03 +0200808
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200809 /*
810 * The parsing is simpler than the one in hid_open_report() as we should
811 * be robust against hid errors. Those errors will be raised by
812 * hid_open_report() anyway.
813 */
814 while ((start = fetch_item(start, end, &item)) != NULL)
815 dispatch_type[item.type](parser, &item);
816
Benjamin Tissoiresf961bd32013-08-22 14:51:08 +0200817 /*
818 * Handle special flags set during scanning.
819 */
820 if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
821 (hid->group == HID_GROUP_MULTITOUCH))
822 hid->group = HID_GROUP_MULTITOUCH_WIN_8;
823
Benjamin Tissoiresba391e52014-05-21 11:15:56 -0400824 /*
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700825 * Vendor specific handlings
826 */
827 switch (hid->vendor) {
828 case USB_VENDOR_ID_WACOM:
829 hid->group = HID_GROUP_WACOM;
830 break;
Benjamin Tissoiresc241c5e2014-09-30 13:18:23 -0400831 case USB_VENDOR_ID_SYNAPTICS:
Jiri Kosina84379d82017-04-11 11:10:16 +0200832 if (hid->group == HID_GROUP_GENERIC)
Andrew Duggane39f2d52014-12-12 10:17:26 -0800833 if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
834 && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
835 /*
836 * hid-rmi should take care of them,
837 * not hid-generic
838 */
Jiri Kosina0ca4cd72017-06-09 13:15:37 +0200839 hid->group = HID_GROUP_RMI;
Benjamin Tissoiresc241c5e2014-09-30 13:18:23 -0400840 break;
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700841 }
842
Benjamin Tissoires3dc8fc02013-08-22 14:51:07 +0200843 vfree(parser);
Henrik Rydberg734c6602012-04-23 12:07:03 +0200844 return 0;
845}
846
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200847/**
848 * hid_parse_report - parse device report
849 *
850 * @device: hid device
851 * @start: report start
852 * @size: report size
853 *
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200854 * Allocate the device report as read by the bus driver. This function should
855 * only be called from parse() in ll drivers.
856 */
857int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
858{
859 hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
860 if (!hid->dev_rdesc)
861 return -ENOMEM;
862 hid->dev_rsize = size;
863 return 0;
864}
865EXPORT_SYMBOL_GPL(hid_parse_report);
866
Kees Cook331415f2013-09-11 21:56:50 +0200867static const char * const hid_report_names[] = {
868 "HID_INPUT_REPORT",
869 "HID_OUTPUT_REPORT",
870 "HID_FEATURE_REPORT",
871};
872/**
873 * hid_validate_values - validate existing device report's value indexes
874 *
875 * @device: hid device
876 * @type: which report type to examine
877 * @id: which report ID to examine (0 for first)
878 * @field_index: which report field to examine
879 * @report_counts: expected number of values
880 *
881 * Validate the number of values in a given field of a given report, after
882 * parsing.
883 */
884struct hid_report *hid_validate_values(struct hid_device *hid,
885 unsigned int type, unsigned int id,
886 unsigned int field_index,
887 unsigned int report_counts)
888{
889 struct hid_report *report;
890
891 if (type > HID_FEATURE_REPORT) {
892 hid_err(hid, "invalid HID report type %u\n", type);
893 return NULL;
894 }
895
896 if (id >= HID_MAX_IDS) {
897 hid_err(hid, "invalid HID report id %u\n", id);
898 return NULL;
899 }
900
901 /*
902 * Explicitly not using hid_get_report() here since it depends on
903 * ->numbered being checked, which may not always be the case when
904 * drivers go to access report values.
905 */
Kees Cook1b15d2e2014-04-17 13:22:09 -0700906 if (id == 0) {
907 /*
908 * Validating on id 0 means we should examine the first
909 * report in the list.
910 */
911 report = list_entry(
912 hid->report_enum[type].report_list.next,
913 struct hid_report, list);
914 } else {
915 report = hid->report_enum[type].report_id_hash[id];
916 }
Kees Cook331415f2013-09-11 21:56:50 +0200917 if (!report) {
918 hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
919 return NULL;
920 }
921 if (report->maxfield <= field_index) {
922 hid_err(hid, "not enough fields in %s %u\n",
923 hid_report_names[type], id);
924 return NULL;
925 }
926 if (report->field[field_index]->report_count < report_counts) {
927 hid_err(hid, "not enough values in %s %u field %u\n",
928 hid_report_names[type], id, field_index);
929 return NULL;
930 }
931 return report;
932}
933EXPORT_SYMBOL_GPL(hid_validate_values);
934
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200935/**
936 * hid_open_report - open a driver-specific device report
937 *
938 * @device: hid device
939 *
Jiri Kosinadde58452006-12-08 18:40:44 +0100940 * Parse a report description into a hid_device structure. Reports are
941 * enumerated, fields are attached to these reports.
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200942 * 0 returned on success, otherwise nonzero error value.
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200943 *
944 * This function (or the equivalent hid_parse() macro) should only be
945 * called from probe() in drivers, before starting the device.
Jiri Kosinadde58452006-12-08 18:40:44 +0100946 */
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200947int hid_open_report(struct hid_device *device)
Jiri Kosinadde58452006-12-08 18:40:44 +0100948{
Jiri Kosinadde58452006-12-08 18:40:44 +0100949 struct hid_parser *parser;
950 struct hid_item item;
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200951 unsigned int size;
952 __u8 *start;
Kevin Daughtridge86e6b772012-09-20 12:00:32 -0700953 __u8 *buf;
Jiri Kosinadde58452006-12-08 18:40:44 +0100954 __u8 *end;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200955 int ret;
Jiri Kosinadde58452006-12-08 18:40:44 +0100956 static int (*dispatch_type[])(struct hid_parser *parser,
957 struct hid_item *item) = {
958 hid_parser_main,
959 hid_parser_global,
960 hid_parser_local,
961 hid_parser_reserved
962 };
963
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200964 if (WARN_ON(device->status & HID_STAT_PARSED))
965 return -EBUSY;
966
967 start = device->dev_rdesc;
968 if (WARN_ON(!start))
969 return -ENODEV;
970 size = device->dev_rsize;
971
Kevin Daughtridge86e6b772012-09-20 12:00:32 -0700972 buf = kmemdup(start, size, GFP_KERNEL);
973 if (buf == NULL)
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200974 return -ENOMEM;
Kevin Daughtridge86e6b772012-09-20 12:00:32 -0700975
976 if (device->driver->report_fixup)
977 start = device->driver->report_fixup(device, buf, &size);
978 else
979 start = buf;
980
981 start = kmemdup(start, size, GFP_KERNEL);
982 kfree(buf);
983 if (start == NULL)
984 return -ENOMEM;
985
986 device->rdesc = start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100987 device->rsize = size;
988
Joe Perchesfe258022010-12-09 19:29:04 -0800989 parser = vzalloc(sizeof(struct hid_parser));
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200990 if (!parser) {
991 ret = -ENOMEM;
992 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100993 }
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200994
Jiri Kosinadde58452006-12-08 18:40:44 +0100995 parser->device = device;
996
997 end = start + size;
Henrik Rydberga7197c2e2012-04-22 14:21:40 +0200998
999 device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1000 sizeof(struct hid_collection), GFP_KERNEL);
1001 if (!device->collection) {
1002 ret = -ENOMEM;
1003 goto err;
1004 }
1005 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1006
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001007 ret = -EINVAL;
Jiri Kosinadde58452006-12-08 18:40:44 +01001008 while ((start = fetch_item(start, end, &item)) != NULL) {
1009
1010 if (item.format != HID_ITEM_FORMAT_SHORT) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001011 hid_err(device, "unexpected long global item\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001012 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +01001013 }
1014
1015 if (dispatch_type[item.type](parser, &item)) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001016 hid_err(device, "item %u %u %u %u parsing failed\n",
Joe Perches4291ee32010-12-09 19:29:03 -08001017 item.format, (unsigned)item.size,
1018 (unsigned)item.type, (unsigned)item.tag);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001019 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +01001020 }
1021
1022 if (start == end) {
1023 if (parser->collection_stack_ptr) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001024 hid_err(device, "unbalanced collection at end of report description\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001025 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +01001026 }
1027 if (parser->local.delimiter_depth) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001028 hid_err(device, "unbalanced delimiter at end of report description\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001029 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +01001030 }
Jiri Kosina47a80ed2007-03-12 14:55:12 +01001031 vfree(parser);
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02001032 device->status |= HID_STAT_PARSED;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001033 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +01001034 }
1035 }
1036
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001037 hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001038err:
Jiri Kosina47a80ed2007-03-12 14:55:12 +01001039 vfree(parser);
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02001040 hid_close_report(device);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001041 return ret;
Jiri Kosinadde58452006-12-08 18:40:44 +01001042}
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02001043EXPORT_SYMBOL_GPL(hid_open_report);
Jiri Kosinadde58452006-12-08 18:40:44 +01001044
1045/*
1046 * Convert a signed n-bit integer to signed 32-bit integer. Common
1047 * cases are done through the compiler, the screwed things has to be
1048 * done by hand.
1049 */
1050
1051static s32 snto32(__u32 value, unsigned n)
1052{
1053 switch (n) {
Jiri Slaby880d29f2008-06-18 23:55:41 +02001054 case 8: return ((__s8)value);
1055 case 16: return ((__s16)value);
1056 case 32: return ((__s32)value);
Jiri Kosinadde58452006-12-08 18:40:44 +01001057 }
Andy Shevchenko08585e42017-06-13 12:22:22 +03001058 return value & (1 << (n - 1)) ? value | (~0U << n) : value;
Jiri Kosinadde58452006-12-08 18:40:44 +01001059}
1060
Benjamin Tissoires77463832012-11-14 16:59:15 +01001061s32 hid_snto32(__u32 value, unsigned n)
1062{
1063 return snto32(value, n);
1064}
1065EXPORT_SYMBOL_GPL(hid_snto32);
1066
Jiri Kosinadde58452006-12-08 18:40:44 +01001067/*
1068 * Convert a signed 32-bit integer to a signed n-bit integer.
1069 */
1070
1071static u32 s32ton(__s32 value, unsigned n)
1072{
1073 s32 a = value >> (n - 1);
1074 if (a && a != -1)
1075 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1076 return value & ((1 << n) - 1);
1077}
1078
1079/*
1080 * Extract/implement a data field from/to a little endian report (bit array).
1081 *
1082 * Code sort-of follows HID spec:
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001083 * http://www.usb.org/developers/hidpage/HID1_11.pdf
Jiri Kosinadde58452006-12-08 18:40:44 +01001084 *
1085 * While the USB HID spec allows unlimited length bit fields in "report
1086 * descriptors", most devices never use more than 16 bits.
1087 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1088 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1089 */
1090
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001091static u32 __extract(u8 *report, unsigned offset, int n)
Jiri Kosinadde58452006-12-08 18:40:44 +01001092{
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001093 unsigned int idx = offset / 8;
1094 unsigned int bit_nr = 0;
1095 unsigned int bit_shift = offset % 8;
1096 int bits_to_copy = 8 - bit_shift;
1097 u32 value = 0;
1098 u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
Jiri Kosinadde58452006-12-08 18:40:44 +01001099
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001100 while (n > 0) {
1101 value |= ((u32)report[idx] >> bit_shift) << bit_nr;
1102 n -= bits_to_copy;
1103 bit_nr += bits_to_copy;
1104 bits_to_copy = 8;
1105 bit_shift = 0;
1106 idx++;
1107 }
1108
1109 return value & mask;
1110}
1111
1112u32 hid_field_extract(const struct hid_device *hid, u8 *report,
1113 unsigned offset, unsigned n)
1114{
1115 if (n > 32) {
Goffredo Baroncelli04fba782015-05-30 11:00:26 +02001116 hid_warn(hid, "hid_field_extract() called with n (%d) > 32! (%s)\n",
Joe Perches4291ee32010-12-09 19:29:03 -08001117 n, current->comm);
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001118 n = 32;
1119 }
Jiri Kosinadde58452006-12-08 18:40:44 +01001120
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001121 return __extract(report, offset, n);
Jiri Kosinadde58452006-12-08 18:40:44 +01001122}
Goffredo Baroncelli04fba782015-05-30 11:00:26 +02001123EXPORT_SYMBOL_GPL(hid_field_extract);
Jiri Kosinadde58452006-12-08 18:40:44 +01001124
1125/*
1126 * "implement" : set bits in a little endian bit stream.
1127 * Same concepts as "extract" (see comments above).
1128 * The data mangled in the bit stream remains in little endian
1129 * order the whole time. It make more sense to talk about
1130 * endianness of register values by considering a register
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001131 * a "cached" copy of the little endian bit stream.
Jiri Kosinadde58452006-12-08 18:40:44 +01001132 */
Jiri Kosinadde58452006-12-08 18:40:44 +01001133
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001134static void __implement(u8 *report, unsigned offset, int n, u32 value)
1135{
1136 unsigned int idx = offset / 8;
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001137 unsigned int bit_shift = offset % 8;
1138 int bits_to_set = 8 - bit_shift;
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001139
1140 while (n - bits_to_set >= 0) {
Dmitry Torokhov95d1c892016-04-06 10:19:58 -07001141 report[idx] &= ~(0xff << bit_shift);
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001142 report[idx] |= value << bit_shift;
1143 value >>= bits_to_set;
1144 n -= bits_to_set;
1145 bits_to_set = 8;
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001146 bit_shift = 0;
1147 idx++;
1148 }
1149
1150 /* last nibble */
1151 if (n) {
Dmitry Torokhov95d1c892016-04-06 10:19:58 -07001152 u8 bit_mask = ((1U << n) - 1);
1153 report[idx] &= ~(bit_mask << bit_shift);
1154 report[idx] |= value << bit_shift;
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001155 }
1156}
1157
1158static void implement(const struct hid_device *hid, u8 *report,
1159 unsigned offset, unsigned n, u32 value)
1160{
Dmitry Torokhov95d1c892016-04-06 10:19:58 -07001161 if (unlikely(n > 32)) {
Joe Perches4291ee32010-12-09 19:29:03 -08001162 hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
1163 __func__, n, current->comm);
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001164 n = 32;
Dmitry Torokhov95d1c892016-04-06 10:19:58 -07001165 } else if (n < 32) {
1166 u32 m = (1U << n) - 1;
Jiri Kosinadde58452006-12-08 18:40:44 +01001167
Dmitry Torokhov95d1c892016-04-06 10:19:58 -07001168 if (unlikely(value > m)) {
1169 hid_warn(hid,
1170 "%s() called with too large value %d (n: %d)! (%s)\n",
1171 __func__, value, n, current->comm);
1172 WARN_ON(1);
1173 value &= m;
1174 }
1175 }
Jiri Kosinadde58452006-12-08 18:40:44 +01001176
Dmitry Torokhov5137b3542016-01-18 22:40:37 -08001177 __implement(report, offset, n, value);
Jiri Kosinadde58452006-12-08 18:40:44 +01001178}
1179
1180/*
1181 * Search an array for a value.
1182 */
1183
Joe Perches16ee4cc2010-12-09 19:29:05 -08001184static int search(__s32 *array, __s32 value, unsigned n)
Jiri Kosinadde58452006-12-08 18:40:44 +01001185{
1186 while (n--) {
1187 if (*array++ == value)
1188 return 0;
1189 }
1190 return -1;
1191}
1192
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001193/**
1194 * hid_match_report - check if driver's raw_event should be called
1195 *
1196 * @hid: hid device
1197 * @report_type: type to match against
1198 *
1199 * compare hid->driver->report_table->report_type to report->type
1200 */
1201static int hid_match_report(struct hid_device *hid, struct hid_report *report)
Jiri Kosinadde58452006-12-08 18:40:44 +01001202{
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001203 const struct hid_report_id *id = hid->driver->report_table;
1204
1205 if (!id) /* NULL means all */
1206 return 1;
1207
1208 for (; id->report_type != HID_TERMINATOR; id++)
1209 if (id->report_type == HID_ANY_ID ||
1210 id->report_type == report->type)
1211 return 1;
1212 return 0;
1213}
1214
1215/**
1216 * hid_match_usage - check if driver's event should be called
1217 *
1218 * @hid: hid device
1219 * @usage: usage to match against
1220 *
1221 * compare hid->driver->usage_table->usage_{type,code} to
1222 * usage->usage_{type,code}
1223 */
1224static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
1225{
1226 const struct hid_usage_id *id = hid->driver->usage_table;
1227
1228 if (!id) /* NULL means all */
1229 return 1;
1230
1231 for (; id->usage_type != HID_ANY_ID - 1; id++)
1232 if ((id->usage_hid == HID_ANY_ID ||
1233 id->usage_hid == usage->hid) &&
1234 (id->usage_type == HID_ANY_ID ||
1235 id->usage_type == usage->type) &&
1236 (id->usage_code == HID_ANY_ID ||
1237 id->usage_code == usage->code))
1238 return 1;
1239 return 0;
1240}
1241
1242static void hid_process_event(struct hid_device *hid, struct hid_field *field,
1243 struct hid_usage *usage, __s32 value, int interrupt)
1244{
1245 struct hid_driver *hdrv = hid->driver;
1246 int ret;
1247
Henrik Rydberg9bfc8da2012-09-01 21:47:11 +02001248 if (!list_empty(&hid->debug_list))
1249 hid_dump_input(hid, usage, value);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001250
1251 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
1252 ret = hdrv->event(hid, field, usage, value);
1253 if (ret != 0) {
1254 if (ret < 0)
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001255 hid_err(hid, "%s's event failed with %d\n",
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001256 hdrv->name, ret);
1257 return;
1258 }
1259 }
1260
Jiri Kosinadde58452006-12-08 18:40:44 +01001261 if (hid->claimed & HID_CLAIMED_INPUT)
1262 hidinput_hid_event(hid, field, usage, value);
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001263 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1264 hid->hiddev_hid_event(hid, field, usage, value);
Jiri Kosinadde58452006-12-08 18:40:44 +01001265}
1266
1267/*
1268 * Analyse a received field, and fetch the data from it. The field
1269 * content is stored for next report processing (we do differential
1270 * reporting to the layer).
1271 */
1272
Adrian Bunkabdff0f2008-03-31 01:53:56 +02001273static void hid_input_field(struct hid_device *hid, struct hid_field *field,
1274 __u8 *data, int interrupt)
Jiri Kosinadde58452006-12-08 18:40:44 +01001275{
1276 unsigned n;
1277 unsigned count = field->report_count;
1278 unsigned offset = field->report_offset;
1279 unsigned size = field->report_size;
1280 __s32 min = field->logical_minimum;
1281 __s32 max = field->logical_maximum;
1282 __s32 *value;
1283
Kees Cook6da2ec52018-06-12 13:55:00 -07001284 value = kmalloc_array(count, sizeof(__s32), GFP_ATOMIC);
Joe Perchesa3789a12010-12-09 19:29:07 -08001285 if (!value)
Jiri Kosinadde58452006-12-08 18:40:44 +01001286 return;
1287
1288 for (n = 0; n < count; n++) {
1289
Joe Perches4291ee32010-12-09 19:29:03 -08001290 value[n] = min < 0 ?
Goffredo Baroncelli04fba782015-05-30 11:00:26 +02001291 snto32(hid_field_extract(hid, data, offset + n * size,
1292 size), size) :
1293 hid_field_extract(hid, data, offset + n * size, size);
Jiri Kosinadde58452006-12-08 18:40:44 +01001294
Joe Perches4291ee32010-12-09 19:29:03 -08001295 /* Ignore report if ErrorRollOver */
1296 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
1297 value[n] >= min && value[n] <= max &&
Benjamin Tissoires50220de2016-01-19 12:34:58 +01001298 value[n] - min < field->maxusage &&
Joe Perches4291ee32010-12-09 19:29:03 -08001299 field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1300 goto exit;
Jiri Kosinadde58452006-12-08 18:40:44 +01001301 }
1302
1303 for (n = 0; n < count; n++) {
1304
1305 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1306 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1307 continue;
1308 }
1309
1310 if (field->value[n] >= min && field->value[n] <= max
Benjamin Tissoires50220de2016-01-19 12:34:58 +01001311 && field->value[n] - min < field->maxusage
Jiri Kosinadde58452006-12-08 18:40:44 +01001312 && field->usage[field->value[n] - min].hid
1313 && search(value, field->value[n], count))
1314 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1315
1316 if (value[n] >= min && value[n] <= max
Benjamin Tissoires50220de2016-01-19 12:34:58 +01001317 && value[n] - min < field->maxusage
Jiri Kosinadde58452006-12-08 18:40:44 +01001318 && field->usage[value[n] - min].hid
1319 && search(field->value, value[n], count))
1320 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1321 }
1322
1323 memcpy(field->value, value, count * sizeof(__s32));
1324exit:
1325 kfree(value);
1326}
Jiri Kosinadde58452006-12-08 18:40:44 +01001327
1328/*
1329 * Output the field into the report.
1330 */
1331
Joe Perches4291ee32010-12-09 19:29:03 -08001332static void hid_output_field(const struct hid_device *hid,
1333 struct hid_field *field, __u8 *data)
Jiri Kosinadde58452006-12-08 18:40:44 +01001334{
1335 unsigned count = field->report_count;
1336 unsigned offset = field->report_offset;
1337 unsigned size = field->report_size;
1338 unsigned n;
1339
1340 for (n = 0; n < count; n++) {
1341 if (field->logical_minimum < 0) /* signed values */
Joe Perches4291ee32010-12-09 19:29:03 -08001342 implement(hid, data, offset + n * size, size,
1343 s32ton(field->value[n], size));
Jiri Kosinadde58452006-12-08 18:40:44 +01001344 else /* unsigned values */
Joe Perches4291ee32010-12-09 19:29:03 -08001345 implement(hid, data, offset + n * size, size,
1346 field->value[n]);
Jiri Kosinadde58452006-12-08 18:40:44 +01001347 }
1348}
1349
1350/*
Jiri Kosina27ce4052013-07-10 19:56:27 +02001351 * Create a report. 'data' has to be allocated using
1352 * hid_alloc_report_buf() so that it has proper size.
Jiri Kosinadde58452006-12-08 18:40:44 +01001353 */
1354
Jiri Kosina229695e2006-12-08 18:40:53 +01001355void hid_output_report(struct hid_report *report, __u8 *data)
Jiri Kosinadde58452006-12-08 18:40:44 +01001356{
1357 unsigned n;
1358
1359 if (report->id > 0)
1360 *data++ = report->id;
1361
Pete Zaitcev75c28df2010-04-12 12:16:11 -06001362 memset(data, 0, ((report->size - 1) >> 3) + 1);
Jiri Kosinadde58452006-12-08 18:40:44 +01001363 for (n = 0; n < report->maxfield; n++)
Joe Perches4291ee32010-12-09 19:29:03 -08001364 hid_output_field(report->device, report->field[n], data);
Jiri Kosinadde58452006-12-08 18:40:44 +01001365}
Jiri Kosina229695e2006-12-08 18:40:53 +01001366EXPORT_SYMBOL_GPL(hid_output_report);
Jiri Kosinadde58452006-12-08 18:40:44 +01001367
1368/*
Jiri Kosina27ce4052013-07-10 19:56:27 +02001369 * Allocator for buffer that is going to be passed to hid_output_report()
1370 */
1371u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
1372{
1373 /*
1374 * 7 extra bytes are necessary to achieve proper functionality
1375 * of implement() working on 8 byte chunks
1376 */
1377
Aaron Ma6de0b132018-01-08 10:41:41 +08001378 u32 len = hid_report_len(report) + 7;
Jiri Kosina27ce4052013-07-10 19:56:27 +02001379
1380 return kmalloc(len, flags);
1381}
1382EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
1383
1384/*
Jiri Kosinadde58452006-12-08 18:40:44 +01001385 * Set a field value. The report this field belongs to has to be
1386 * created and transferred to the device, to set this value in the
1387 * device.
1388 */
1389
1390int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1391{
Kees Cookbe67b682013-08-28 22:32:01 +02001392 unsigned size;
1393
1394 if (!field)
1395 return -1;
1396
1397 size = field->report_size;
Jiri Kosinadde58452006-12-08 18:40:44 +01001398
Jiri Kosinacd667ce2009-06-12 15:20:57 +02001399 hid_dump_input(field->report->device, field->usage + offset, value);
Jiri Kosinadde58452006-12-08 18:40:44 +01001400
1401 if (offset >= field->report_count) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001402 hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
1403 offset, field->report_count);
Jiri Kosinadde58452006-12-08 18:40:44 +01001404 return -1;
1405 }
1406 if (field->logical_minimum < 0) {
1407 if (value != snto32(s32ton(value, size), size)) {
Jiri Kosina8c3d52f2011-12-15 11:00:38 +01001408 hid_err(field->report->device, "value %d is out of range\n", value);
Jiri Kosinadde58452006-12-08 18:40:44 +01001409 return -1;
1410 }
1411 }
1412 field->value[offset] = value;
1413 return 0;
1414}
Jiri Kosina229695e2006-12-08 18:40:53 +01001415EXPORT_SYMBOL_GPL(hid_set_field);
Jiri Kosinadde58452006-12-08 18:40:44 +01001416
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001417static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1418 const u8 *data)
1419{
1420 struct hid_report *report;
1421 unsigned int n = 0; /* Normally report number is 0 */
1422
1423 /* Device uses numbered reports, data[0] is report number */
1424 if (report_enum->numbered)
1425 n = *data;
1426
1427 report = report_enum->report_id_hash[n];
1428 if (report == NULL)
1429 dbg_hid("undefined report_id %u received\n", n);
1430
1431 return report;
1432}
1433
Benjamin Tissoires4fa5a7f2014-02-10 12:58:48 -05001434/*
1435 * Implement a generic .request() callback, using .raw_request()
1436 * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
1437 */
1438void __hid_request(struct hid_device *hid, struct hid_report *report,
1439 int reqtype)
1440{
1441 char *buf;
1442 int ret;
Aaron Ma6de0b132018-01-08 10:41:41 +08001443 u32 len;
Benjamin Tissoires4fa5a7f2014-02-10 12:58:48 -05001444
Benjamin Tissoires4fa5a7f2014-02-10 12:58:48 -05001445 buf = hid_alloc_report_buf(report, GFP_KERNEL);
1446 if (!buf)
1447 return;
1448
1449 len = hid_report_len(report);
1450
1451 if (reqtype == HID_REQ_SET_REPORT)
1452 hid_output_report(report, buf);
1453
1454 ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
1455 report->type, reqtype);
1456 if (ret < 0) {
1457 dbg_hid("unable to complete request: %d\n", ret);
1458 goto out;
1459 }
1460
1461 if (reqtype == HID_REQ_GET_REPORT)
1462 hid_input_report(hid, report->type, buf, ret, 0);
1463
1464out:
1465 kfree(buf);
1466}
1467EXPORT_SYMBOL_GPL(__hid_request);
1468
Aaron Ma6de0b132018-01-08 10:41:41 +08001469int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001470 int interrupt)
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001471{
1472 struct hid_report_enum *report_enum = hid->report_enum + type;
1473 struct hid_report *report;
Benjamin Tissoires6d85d032013-01-31 17:22:23 +01001474 struct hid_driver *hdrv;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001475 unsigned int a;
Aaron Ma6de0b132018-01-08 10:41:41 +08001476 u32 rsize, csize = size;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001477 u8 *cdata = data;
Jiri Kosinab6787242012-04-27 00:56:08 +02001478 int ret = 0;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001479
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001480 report = hid_get_report(report_enum, data);
1481 if (!report)
Jiri Kosinab6787242012-04-27 00:56:08 +02001482 goto out;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001483
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001484 if (report_enum->numbered) {
1485 cdata++;
1486 csize--;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001487 }
1488
1489 rsize = ((report->size - 1) >> 3) + 1;
1490
Armando Visconti966922f2011-05-19 21:41:22 +02001491 if (rsize > HID_MAX_BUFFER_SIZE)
1492 rsize = HID_MAX_BUFFER_SIZE;
1493
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001494 if (csize < rsize) {
1495 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1496 csize, rsize);
1497 memset(cdata + csize, 0, rsize - csize);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001498 }
1499
1500 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1501 hid->hiddev_report_event(hid, report);
Jiri Kosinab6787242012-04-27 00:56:08 +02001502 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1503 ret = hidraw_report_event(hid, data, size);
1504 if (ret)
1505 goto out;
1506 }
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001507
Benjamin Tissoirescc6b54a2013-09-11 21:56:57 +02001508 if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
Matthieu CASTETb94e3c92012-06-28 16:53:11 +02001509 for (a = 0; a < report->maxfield; a++)
1510 hid_input_field(hid, report->field[a], cdata, interrupt);
Benjamin Tissoires6d85d032013-01-31 17:22:23 +01001511 hdrv = hid->driver;
1512 if (hdrv && hdrv->report)
1513 hdrv->report(hid, report);
Matthieu CASTETb94e3c92012-06-28 16:53:11 +02001514 }
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001515
1516 if (hid->claimed & HID_CLAIMED_INPUT)
1517 hidinput_report_event(hid, report);
Jiri Kosinab6787242012-04-27 00:56:08 +02001518out:
1519 return ret;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001520}
1521EXPORT_SYMBOL_GPL(hid_report_raw_event);
1522
1523/**
1524 * hid_input_report - report data from lower layer (usb, bt...)
1525 *
1526 * @hid: hid device
1527 * @type: HID report type (HID_*_REPORT)
1528 * @data: report contents
1529 * @size: size of data parameter
Jiri Kosinaff9b00a2009-10-01 16:03:13 +02001530 * @interrupt: distinguish between interrupt and control transfers
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001531 *
1532 * This is data entry for lower layers.
1533 */
Aaron Ma6de0b132018-01-08 10:41:41 +08001534int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt)
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001535{
Julia Lawall76c317d2009-07-19 17:26:13 +02001536 struct hid_report_enum *report_enum;
1537 struct hid_driver *hdrv;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001538 struct hid_report *report;
Jiri Kosina45dc1ac2011-08-10 14:02:59 +02001539 int ret = 0;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001540
David Herrmann4ea54542011-08-10 14:02:07 +02001541 if (!hid)
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001542 return -ENODEV;
David Herrmann4ea54542011-08-10 14:02:07 +02001543
Andrew de los Reyesc849a612013-02-18 09:20:21 -08001544 if (down_trylock(&hid->driver_input_lock))
David Herrmann4ea54542011-08-10 14:02:07 +02001545 return -EBUSY;
1546
1547 if (!hid->driver) {
1548 ret = -ENODEV;
1549 goto unlock;
1550 }
Julia Lawall76c317d2009-07-19 17:26:13 +02001551 report_enum = hid->report_enum + type;
1552 hdrv = hid->driver;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001553
1554 if (!size) {
1555 dbg_hid("empty report\n");
David Herrmann4ea54542011-08-10 14:02:07 +02001556 ret = -1;
1557 goto unlock;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001558 }
1559
Matthieu CASTETb94e3c92012-06-28 16:53:11 +02001560 /* Avoid unnecessary overhead if debugfs is disabled */
Benjamin Tissoiresa5f04b92013-04-17 19:38:13 +02001561 if (!list_empty(&hid->debug_list))
1562 hid_dump_report(hid, type, data, size);
Matthieu CASTETb94e3c92012-06-28 16:53:11 +02001563
Jiri Kosinaf77e3472010-03-18 14:11:53 +01001564 report = hid_get_report(report_enum, data);
1565
David Herrmann4ea54542011-08-10 14:02:07 +02001566 if (!report) {
1567 ret = -1;
1568 goto unlock;
1569 }
Jiri Kosinaf77e3472010-03-18 14:11:53 +01001570
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001571 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1572 ret = hdrv->raw_event(hid, report, data, size);
Felix Rueegg556483e2013-10-08 19:33:47 +02001573 if (ret < 0)
David Herrmann4ea54542011-08-10 14:02:07 +02001574 goto unlock;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001575 }
1576
Jiri Kosinab6787242012-04-27 00:56:08 +02001577 ret = hid_report_raw_event(hid, type, data, size, interrupt);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001578
David Herrmann4ea54542011-08-10 14:02:07 +02001579unlock:
Andrew de los Reyesc849a612013-02-18 09:20:21 -08001580 up(&hid->driver_input_lock);
Jiri Kosina45dc1ac2011-08-10 14:02:59 +02001581 return ret;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001582}
1583EXPORT_SYMBOL_GPL(hid_input_report);
1584
Benjamin Tissoiresd5d3e202017-11-20 11:48:41 +01001585bool hid_match_one_id(const struct hid_device *hdev,
1586 const struct hid_device_id *id)
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001587{
Henrik Rydberg7431fb72012-04-23 12:07:04 +02001588 return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
Henrik Rydberg4d53b802012-04-23 12:07:02 +02001589 (id->group == HID_GROUP_ANY || id->group == hdev->group) &&
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001590 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1591 (id->product == HID_ANY_ID || id->product == hdev->product);
1592}
1593
Benjamin Tissoiresd5d3e202017-11-20 11:48:41 +01001594const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001595 const struct hid_device_id *id)
1596{
1597 for (; id->bus; id++)
1598 if (hid_match_one_id(hdev, id))
1599 return id;
1600
1601 return NULL;
1602}
1603
1604static const struct hid_device_id hid_hiddev_list[] = {
Richard Hughesc0bd6a42008-08-27 11:19:37 +02001605 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1606 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001607 { }
1608};
1609
1610static bool hid_hiddev(struct hid_device *hdev)
1611{
1612 return !!hid_match_id(hdev, hid_hiddev_list);
1613}
1614
Alan Ott6d3bfb72011-01-23 22:50:18 -05001615
1616static ssize_t
1617read_report_descriptor(struct file *filp, struct kobject *kobj,
1618 struct bin_attribute *attr,
1619 char *buf, loff_t off, size_t count)
1620{
Geliang Tang2cf83832015-12-27 17:25:24 +08001621 struct device *dev = kobj_to_dev(kobj);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001622 struct hid_device *hdev = to_hid_device(dev);
Alan Ott6d3bfb72011-01-23 22:50:18 -05001623
1624 if (off >= hdev->rsize)
1625 return 0;
1626
1627 if (off + count > hdev->rsize)
1628 count = hdev->rsize - off;
1629
1630 memcpy(buf, hdev->rdesc + off, count);
1631
1632 return count;
1633}
1634
Olivier Gaya8774172015-02-17 14:00:03 +01001635static ssize_t
1636show_country(struct device *dev, struct device_attribute *attr,
1637 char *buf)
1638{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001639 struct hid_device *hdev = to_hid_device(dev);
Olivier Gaya8774172015-02-17 14:00:03 +01001640
1641 return sprintf(buf, "%02x\n", hdev->country & 0xff);
1642}
1643
Alan Ott6d3bfb72011-01-23 22:50:18 -05001644static struct bin_attribute dev_bin_attr_report_desc = {
1645 .attr = { .name = "report_descriptor", .mode = 0444 },
1646 .read = read_report_descriptor,
1647 .size = HID_MAX_DESCRIPTOR_SIZE,
1648};
1649
Bhumika Goyalad8378e2017-08-21 17:13:09 +05301650static const struct device_attribute dev_attr_country = {
Olivier Gaya8774172015-02-17 14:00:03 +01001651 .attr = { .name = "country", .mode = 0444 },
1652 .show = show_country,
1653};
1654
Jiri Slaby93c10132008-06-27 00:04:24 +02001655int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1656{
1657 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1658 "Joystick", "Gamepad", "Keyboard", "Keypad",
1659 "Multi-Axis Controller"
1660 };
1661 const char *type, *bus;
Richard Purdie79b568b2015-09-18 16:31:33 -07001662 char buf[64] = "";
Jiri Slaby93c10132008-06-27 00:04:24 +02001663 unsigned int i;
1664 int len;
Alan Ott6d3bfb72011-01-23 22:50:18 -05001665 int ret;
Jiri Slaby93c10132008-06-27 00:04:24 +02001666
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001667 if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1668 connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
Daniel Mack3a343ee2010-07-12 19:28:27 +02001669 if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
1670 connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
Jiri Slaby93c10132008-06-27 00:04:24 +02001671 if (hdev->bus != BUS_USB)
1672 connect_mask &= ~HID_CONNECT_HIDDEV;
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001673 if (hid_hiddev(hdev))
1674 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Jiri Slaby93c10132008-06-27 00:04:24 +02001675
1676 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1677 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1678 hdev->claimed |= HID_CLAIMED_INPUT;
Benjamin Tissoiresb77c3922011-09-21 16:56:54 +02001679
Jiri Slaby93c10132008-06-27 00:04:24 +02001680 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1681 !hdev->hiddev_connect(hdev,
1682 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1683 hdev->claimed |= HID_CLAIMED_HIDDEV;
1684 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1685 hdev->claimed |= HID_CLAIMED_HIDRAW;
1686
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001687 if (connect_mask & HID_CONNECT_DRIVER)
1688 hdev->claimed |= HID_CLAIMED_DRIVER;
1689
David Herrmann4bc19f62012-07-20 11:49:09 +02001690 /* Drivers with the ->raw_event callback set are not required to connect
1691 * to any other listener. */
1692 if (!hdev->claimed && !hdev->driver->raw_event) {
1693 hid_err(hdev, "device has no listeners, quitting\n");
Jiri Slaby93c10132008-06-27 00:04:24 +02001694 return -ENODEV;
1695 }
1696
1697 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1698 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1699 hdev->ff_init(hdev);
1700
1701 len = 0;
1702 if (hdev->claimed & HID_CLAIMED_INPUT)
1703 len += sprintf(buf + len, "input");
1704 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1705 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
Jaejoong Kim733aca92017-03-03 17:54:01 +09001706 ((struct hiddev *)hdev->hiddev)->minor);
Jiri Slaby93c10132008-06-27 00:04:24 +02001707 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1708 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1709 ((struct hidraw *)hdev->hidraw)->minor);
1710
1711 type = "Device";
1712 for (i = 0; i < hdev->maxcollection; i++) {
1713 struct hid_collection *col = &hdev->collection[i];
1714 if (col->type == HID_COLLECTION_APPLICATION &&
1715 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1716 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1717 type = types[col->usage & 0xffff];
1718 break;
1719 }
1720 }
1721
1722 switch (hdev->bus) {
1723 case BUS_USB:
1724 bus = "USB";
1725 break;
1726 case BUS_BLUETOOTH:
1727 bus = "BLUETOOTH";
1728 break;
Daniel Martin067807272015-09-17 17:02:22 +02001729 case BUS_I2C:
1730 bus = "I2C";
1731 break;
Jiri Slaby93c10132008-06-27 00:04:24 +02001732 default:
1733 bus = "<UNKNOWN>";
1734 }
1735
Olivier Gaya8774172015-02-17 14:00:03 +01001736 ret = device_create_file(&hdev->dev, &dev_attr_country);
1737 if (ret)
1738 hid_warn(hdev,
1739 "can't create sysfs country code attribute err: %d\n", ret);
1740
Joe Perches4291ee32010-12-09 19:29:03 -08001741 hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1742 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1743 type, hdev->name, hdev->phys);
Jiri Slaby93c10132008-06-27 00:04:24 +02001744
1745 return 0;
1746}
1747EXPORT_SYMBOL_GPL(hid_connect);
1748
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001749void hid_disconnect(struct hid_device *hdev)
1750{
Olivier Gaya8774172015-02-17 14:00:03 +01001751 device_remove_file(&hdev->dev, &dev_attr_country);
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001752 if (hdev->claimed & HID_CLAIMED_INPUT)
1753 hidinput_disconnect(hdev);
1754 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1755 hdev->hiddev_disconnect(hdev);
1756 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1757 hidraw_disconnect(hdev);
Benjamin Tissoires9c5c6ed2014-11-03 15:33:32 -05001758 hdev->claimed = 0;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001759}
1760EXPORT_SYMBOL_GPL(hid_disconnect);
1761
Dmitry Torokhovaaac0822017-06-06 23:59:34 -07001762/**
1763 * hid_hw_start - start underlying HW
1764 * @hdev: hid device
1765 * @connect_mask: which outputs to connect, see HID_CONNECT_*
1766 *
1767 * Call this in probe function *after* hid_parse. This will setup HW
1768 * buffers and start the device (if not defeirred to device open).
1769 * hid_hw_stop must be called if this was successful.
1770 */
1771int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1772{
1773 int error;
1774
1775 error = hdev->ll_driver->start(hdev);
1776 if (error)
1777 return error;
1778
1779 if (connect_mask) {
1780 error = hid_connect(hdev, connect_mask);
1781 if (error) {
1782 hdev->ll_driver->stop(hdev);
1783 return error;
1784 }
1785 }
1786
1787 return 0;
1788}
1789EXPORT_SYMBOL_GPL(hid_hw_start);
1790
1791/**
1792 * hid_hw_stop - stop underlying HW
1793 * @hdev: hid device
1794 *
1795 * This is usually called from remove function or from probe when something
1796 * failed and hid_hw_start was called already.
1797 */
1798void hid_hw_stop(struct hid_device *hdev)
1799{
1800 hid_disconnect(hdev);
1801 hdev->ll_driver->stop(hdev);
1802}
1803EXPORT_SYMBOL_GPL(hid_hw_stop);
1804
1805/**
1806 * hid_hw_open - signal underlying HW to start delivering events
1807 * @hdev: hid device
1808 *
1809 * Tell underlying HW to start delivering events from the device.
1810 * This function should be called sometime after successful call
Hisao Tanabed6c70a82018-05-05 17:41:13 +09001811 * to hid_hw_start().
Dmitry Torokhovaaac0822017-06-06 23:59:34 -07001812 */
1813int hid_hw_open(struct hid_device *hdev)
1814{
1815 int ret;
1816
1817 ret = mutex_lock_killable(&hdev->ll_open_lock);
1818 if (ret)
1819 return ret;
1820
1821 if (!hdev->ll_open_count++) {
1822 ret = hdev->ll_driver->open(hdev);
1823 if (ret)
1824 hdev->ll_open_count--;
1825 }
1826
1827 mutex_unlock(&hdev->ll_open_lock);
1828 return ret;
1829}
1830EXPORT_SYMBOL_GPL(hid_hw_open);
1831
1832/**
1833 * hid_hw_close - signal underlaying HW to stop delivering events
1834 *
1835 * @hdev: hid device
1836 *
1837 * This function indicates that we are not interested in the events
1838 * from this device anymore. Delivery of events may or may not stop,
1839 * depending on the number of users still outstanding.
1840 */
1841void hid_hw_close(struct hid_device *hdev)
1842{
1843 mutex_lock(&hdev->ll_open_lock);
1844 if (!--hdev->ll_open_count)
1845 hdev->ll_driver->close(hdev);
1846 mutex_unlock(&hdev->ll_open_lock);
1847}
1848EXPORT_SYMBOL_GPL(hid_hw_close);
1849
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001850struct hid_dynid {
1851 struct list_head list;
1852 struct hid_device_id id;
1853};
1854
1855/**
1856 * store_new_id - add a new HID device ID to this driver and re-probe devices
1857 * @driver: target device driver
1858 * @buf: buffer for scanning device ID data
1859 * @count: input size
1860 *
1861 * Adds a new dynamic hid device ID to this driver,
1862 * and causes the driver to probe for all devices again.
1863 */
Greg Kroah-Hartmanc2810322017-06-09 11:03:04 +02001864static ssize_t new_id_store(struct device_driver *drv, const char *buf,
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001865 size_t count)
1866{
Geliang Tangba91a962015-12-27 17:25:22 +08001867 struct hid_driver *hdrv = to_hid_driver(drv);
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001868 struct hid_dynid *dynid;
1869 __u32 bus, vendor, product;
1870 unsigned long driver_data = 0;
1871 int ret;
1872
1873 ret = sscanf(buf, "%x %x %x %lx",
1874 &bus, &vendor, &product, &driver_data);
1875 if (ret < 3)
1876 return -EINVAL;
1877
1878 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1879 if (!dynid)
1880 return -ENOMEM;
1881
1882 dynid->id.bus = bus;
Henrik Rydberg4d53b802012-04-23 12:07:02 +02001883 dynid->id.group = HID_GROUP_ANY;
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001884 dynid->id.vendor = vendor;
1885 dynid->id.product = product;
1886 dynid->id.driver_data = driver_data;
1887
1888 spin_lock(&hdrv->dyn_lock);
1889 list_add_tail(&dynid->list, &hdrv->dyn_list);
1890 spin_unlock(&hdrv->dyn_lock);
1891
Alan Sterncef9bc52012-01-24 13:34:41 -05001892 ret = driver_attach(&hdrv->driver);
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001893
1894 return ret ? : count;
1895}
Greg Kroah-Hartmanc2810322017-06-09 11:03:04 +02001896static DRIVER_ATTR_WO(new_id);
1897
1898static struct attribute *hid_drv_attrs[] = {
1899 &driver_attr_new_id.attr,
1900 NULL,
1901};
1902ATTRIBUTE_GROUPS(hid_drv);
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001903
1904static void hid_free_dynids(struct hid_driver *hdrv)
1905{
1906 struct hid_dynid *dynid, *n;
1907
1908 spin_lock(&hdrv->dyn_lock);
1909 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1910 list_del(&dynid->list);
1911 kfree(dynid);
1912 }
1913 spin_unlock(&hdrv->dyn_lock);
1914}
1915
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01001916const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1917 struct hid_driver *hdrv)
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001918{
1919 struct hid_dynid *dynid;
1920
1921 spin_lock(&hdrv->dyn_lock);
1922 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1923 if (hid_match_one_id(hdev, &dynid->id)) {
1924 spin_unlock(&hdrv->dyn_lock);
1925 return &dynid->id;
1926 }
1927 }
1928 spin_unlock(&hdrv->dyn_lock);
1929
1930 return hid_match_id(hdev, hdrv->id_table);
1931}
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01001932EXPORT_SYMBOL_GPL(hid_match_device);
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001933
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001934static int hid_bus_match(struct device *dev, struct device_driver *drv)
1935{
Geliang Tangba91a962015-12-27 17:25:22 +08001936 struct hid_driver *hdrv = to_hid_driver(drv);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001937 struct hid_device *hdev = to_hid_device(dev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001938
Henrik Rydberg070748e2012-04-23 12:07:05 +02001939 return hid_match_device(hdev, hdrv) != NULL;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001940}
1941
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +01001942/**
1943 * hid_compare_device_paths - check if both devices share the same path
1944 * @hdev_a: hid device
1945 * @hdev_b: hid device
1946 * @separator: char to use as separator
1947 *
1948 * Check if two devices share the same path up to the last occurrence of
1949 * the separator char. Both paths must exist (i.e., zero-length paths
1950 * don't match).
1951 */
1952bool hid_compare_device_paths(struct hid_device *hdev_a,
1953 struct hid_device *hdev_b, char separator)
1954{
1955 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
1956 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
1957
1958 if (n1 != n2 || n1 <= 0 || n2 <= 0)
1959 return false;
1960
1961 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
1962}
1963EXPORT_SYMBOL_GPL(hid_compare_device_paths);
1964
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001965static int hid_device_probe(struct device *dev)
1966{
Geliang Tangba91a962015-12-27 17:25:22 +08001967 struct hid_driver *hdrv = to_hid_driver(dev->driver);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001968 struct hid_device *hdev = to_hid_device(dev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001969 const struct hid_device_id *id;
1970 int ret = 0;
1971
Andrew de los Reyesc849a612013-02-18 09:20:21 -08001972 if (down_interruptible(&hdev->driver_input_lock)) {
1973 ret = -EINTR;
Binoy Jayan6f68f0a2017-06-14 12:59:51 +05301974 goto end;
Andrew de los Reyesc849a612013-02-18 09:20:21 -08001975 }
1976 hdev->io_started = false;
David Herrmann4ea54542011-08-10 14:02:07 +02001977
Benjamin Tissoires8f732852018-05-31 13:49:29 +02001978 clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
1979
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001980 if (!hdev->driver) {
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001981 id = hid_match_device(hdev, hdrv);
Dan Carpenterba623a72011-08-24 14:27:46 +03001982 if (id == NULL) {
Henrik Rydberg4fa3a582012-05-01 08:40:01 +02001983 ret = -ENODEV;
1984 goto unlock;
Dan Carpenterba623a72011-08-24 14:27:46 +03001985 }
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001986
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01001987 if (hdrv->match) {
1988 if (!hdrv->match(hdev, hid_ignore_special_drivers)) {
1989 ret = -ENODEV;
1990 goto unlock;
1991 }
1992 } else {
1993 /*
1994 * hid-generic implements .match(), so if
1995 * hid_ignore_special_drivers is set, we can safely
1996 * return.
1997 */
1998 if (hid_ignore_special_drivers) {
1999 ret = -ENODEV;
2000 goto unlock;
2001 }
2002 }
2003
Benjamin Tissoires2904e682018-03-20 12:04:51 +01002004 /* reset the quirks that has been previously set */
2005 hdev->quirks = hid_lookup_quirk(hdev);
Jiri Slabyc500c972008-05-16 11:49:16 +02002006 hdev->driver = hdrv;
2007 if (hdrv->probe) {
2008 ret = hdrv->probe(hdev, id);
2009 } else { /* default probe */
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002010 ret = hid_open_report(hdev);
Jiri Slabyc500c972008-05-16 11:49:16 +02002011 if (!ret)
Jiri Slaby93c10132008-06-27 00:04:24 +02002012 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002013 }
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002014 if (ret) {
2015 hid_close_report(hdev);
Jiri Slabyc500c972008-05-16 11:49:16 +02002016 hdev->driver = NULL;
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002017 }
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002018 }
Dan Carpenterba623a72011-08-24 14:27:46 +03002019unlock:
Andrew de los Reyesc849a612013-02-18 09:20:21 -08002020 if (!hdev->io_started)
2021 up(&hdev->driver_input_lock);
Binoy Jayan6f68f0a2017-06-14 12:59:51 +05302022end:
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002023 return ret;
2024}
2025
2026static int hid_device_remove(struct device *dev)
2027{
Geliang Tangee79a8f2015-12-27 17:25:21 +08002028 struct hid_device *hdev = to_hid_device(dev);
David Herrmann4ea54542011-08-10 14:02:07 +02002029 struct hid_driver *hdrv;
Andrew de los Reyesc849a612013-02-18 09:20:21 -08002030 int ret = 0;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002031
Andrew de los Reyesc849a612013-02-18 09:20:21 -08002032 if (down_interruptible(&hdev->driver_input_lock)) {
2033 ret = -EINTR;
Binoy Jayan6f68f0a2017-06-14 12:59:51 +05302034 goto end;
Andrew de los Reyesc849a612013-02-18 09:20:21 -08002035 }
2036 hdev->io_started = false;
David Herrmann4ea54542011-08-10 14:02:07 +02002037
2038 hdrv = hdev->driver;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002039 if (hdrv) {
2040 if (hdrv->remove)
2041 hdrv->remove(hdev);
Jiri Slabyc500c972008-05-16 11:49:16 +02002042 else /* default remove */
2043 hid_hw_stop(hdev);
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002044 hid_close_report(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002045 hdev->driver = NULL;
2046 }
2047
Andrew de los Reyesc849a612013-02-18 09:20:21 -08002048 if (!hdev->io_started)
2049 up(&hdev->driver_input_lock);
Binoy Jayan6f68f0a2017-06-14 12:59:51 +05302050end:
Andrew de los Reyesc849a612013-02-18 09:20:21 -08002051 return ret;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002052}
2053
Henrik Rydberg4d53b802012-04-23 12:07:02 +02002054static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
2055 char *buf)
2056{
2057 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Henrik Rydberg4d53b802012-04-23 12:07:02 +02002058
Rasmus Villemoesdfa0c5f2015-11-14 22:08:08 +01002059 return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
2060 hdev->bus, hdev->group, hdev->vendor, hdev->product);
Henrik Rydberg4d53b802012-04-23 12:07:02 +02002061}
Greg Kroah-Hartman0d4260e2013-08-23 14:24:38 -07002062static DEVICE_ATTR_RO(modalias);
Henrik Rydberg4d53b802012-04-23 12:07:02 +02002063
Greg Kroah-Hartman0d4260e2013-08-23 14:24:38 -07002064static struct attribute *hid_dev_attrs[] = {
2065 &dev_attr_modalias.attr,
2066 NULL,
Henrik Rydberg4d53b802012-04-23 12:07:02 +02002067};
Andy Lutomirski54f32fd2015-11-19 08:19:31 -08002068static struct bin_attribute *hid_dev_bin_attrs[] = {
2069 &dev_bin_attr_report_desc,
2070 NULL
2071};
2072static const struct attribute_group hid_dev_group = {
2073 .attrs = hid_dev_attrs,
2074 .bin_attrs = hid_dev_bin_attrs,
2075};
2076__ATTRIBUTE_GROUPS(hid_dev);
Henrik Rydberg4d53b802012-04-23 12:07:02 +02002077
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002078static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
2079{
Dennis Chend193c162017-01-19 06:32:10 -05002080 struct hid_device *hdev = to_hid_device(dev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002081
2082 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
2083 hdev->bus, hdev->vendor, hdev->product))
2084 return -ENOMEM;
2085
2086 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
2087 return -ENOMEM;
2088
2089 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
2090 return -ENOMEM;
2091
2092 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
2093 return -ENOMEM;
2094
Henrik Rydberg4d53b802012-04-23 12:07:02 +02002095 if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
2096 hdev->bus, hdev->group, hdev->vendor, hdev->product))
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002097 return -ENOMEM;
2098
2099 return 0;
2100}
2101
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002102struct bus_type hid_bus_type = {
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002103 .name = "hid",
Greg Kroah-Hartman0d4260e2013-08-23 14:24:38 -07002104 .dev_groups = hid_dev_groups,
Greg Kroah-Hartmanc2810322017-06-09 11:03:04 +02002105 .drv_groups = hid_drv_groups,
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002106 .match = hid_bus_match,
2107 .probe = hid_device_probe,
2108 .remove = hid_device_remove,
2109 .uevent = hid_uevent,
2110};
Benjamin Tissoiresd5d3e202017-11-20 11:48:41 +01002111EXPORT_SYMBOL(hid_bus_type);
Jiri Slabyd458a9d2008-05-16 11:49:20 +02002112
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002113int hid_add_device(struct hid_device *hdev)
2114{
2115 static atomic_t id = ATOMIC_INIT(0);
2116 int ret;
2117
2118 if (WARN_ON(hdev->status & HID_STAT_ADDED))
2119 return -EBUSY;
2120
Benjamin Tissoiresd5d3e202017-11-20 11:48:41 +01002121 hdev->quirks = hid_lookup_quirk(hdev);
2122
Jiri Slabyd458a9d2008-05-16 11:49:20 +02002123 /* we need to kill them here, otherwise they will stay allocated to
2124 * wait for coming driver */
Lamarque V. Souza4529eef2012-12-06 12:39:55 -02002125 if (hid_ignore(hdev))
Jiri Slabyd458a9d2008-05-16 11:49:20 +02002126 return -ENODEV;
2127
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002128 /*
Benjamin Tissoires3c867262014-02-20 15:24:49 -05002129 * Check for the mandatory transport channel.
2130 */
2131 if (!hdev->ll_driver->raw_request) {
2132 hid_err(hdev, "transport driver missing .raw_request()\n");
2133 return -EINVAL;
2134 }
2135
2136 /*
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002137 * Read the device report descriptor once and use as template
2138 * for the driver-specific modifications.
2139 */
2140 ret = hdev->ll_driver->parse(hdev);
2141 if (ret)
2142 return ret;
2143 if (!hdev->dev_rdesc)
2144 return -ENODEV;
2145
Henrik Rydberg734c6602012-04-23 12:07:03 +02002146 /*
2147 * Scan generic devices for group information
2148 */
Benjamin Tissoires4392bf32016-02-12 17:10:37 +01002149 if (hid_ignore_special_drivers) {
2150 hdev->group = HID_GROUP_GENERIC;
2151 } else if (!hdev->group &&
Benjamin Tissoires6e65d9d2017-11-20 11:48:42 +01002152 !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
Henrik Rydberg734c6602012-04-23 12:07:03 +02002153 ret = hid_scan_report(hdev);
2154 if (ret)
2155 hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2156 }
2157
Kay Sievers6bbe5862008-10-31 00:12:32 +01002158 /* XXX hack, any other cleaner solution after the driver core
2159 * is converted to allow more than 20 bytes as the device name? */
2160 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
2161 hdev->vendor, hdev->product, atomic_inc_return(&id));
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002162
Bruno Prémont4da361b2010-03-15 14:55:40 +01002163 hid_debug_register(hdev, dev_name(&hdev->dev));
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002164 ret = device_add(&hdev->dev);
2165 if (!ret)
2166 hdev->status |= HID_STAT_ADDED;
Bruno Prémont4da361b2010-03-15 14:55:40 +01002167 else
2168 hid_debug_unregister(hdev);
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02002169
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002170 return ret;
2171}
2172EXPORT_SYMBOL_GPL(hid_add_device);
2173
2174/**
2175 * hid_allocate_device - allocate new hid device descriptor
2176 *
2177 * Allocate and initialize hid device, so that hid_destroy_device might be
2178 * used to free it.
2179 *
2180 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
2181 * error value.
2182 */
2183struct hid_device *hid_allocate_device(void)
2184{
2185 struct hid_device *hdev;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002186 int ret = -ENOMEM;
2187
2188 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
2189 if (hdev == NULL)
2190 return ERR_PTR(ret);
2191
2192 device_initialize(&hdev->dev);
2193 hdev->dev.release = hid_device_release;
2194 hdev->dev.bus = &hid_bus_type;
Fu, Zhonghui64bebef2015-09-24 14:06:31 +08002195 device_enable_async_suspend(&hdev->dev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002196
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002197 hid_close_report(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002198
Jiri Kosinacd667ce2009-06-12 15:20:57 +02002199 init_waitqueue_head(&hdev->debug_wait);
2200 INIT_LIST_HEAD(&hdev->debug_list);
Jiri Kosina1deb9d32013-05-06 13:05:50 +02002201 spin_lock_init(&hdev->debug_list_lock);
Andrew de los Reyesc849a612013-02-18 09:20:21 -08002202 sema_init(&hdev->driver_input_lock, 1);
Dmitry Torokhovaaac0822017-06-06 23:59:34 -07002203 mutex_init(&hdev->ll_open_lock);
Jiri Kosinacd667ce2009-06-12 15:20:57 +02002204
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002205 return hdev;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002206}
2207EXPORT_SYMBOL_GPL(hid_allocate_device);
2208
2209static void hid_remove_device(struct hid_device *hdev)
2210{
2211 if (hdev->status & HID_STAT_ADDED) {
2212 device_del(&hdev->dev);
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02002213 hid_debug_unregister(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002214 hdev->status &= ~HID_STAT_ADDED;
2215 }
Henrik Rydberga7197c2e2012-04-22 14:21:40 +02002216 kfree(hdev->dev_rdesc);
2217 hdev->dev_rdesc = NULL;
2218 hdev->dev_rsize = 0;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002219}
2220
2221/**
2222 * hid_destroy_device - free previously allocated device
2223 *
2224 * @hdev: hid device
2225 *
2226 * If you allocate hid_device through hid_allocate_device, you should ever
2227 * free by this function.
2228 */
2229void hid_destroy_device(struct hid_device *hdev)
2230{
2231 hid_remove_device(hdev);
2232 put_device(&hdev->dev);
2233}
2234EXPORT_SYMBOL_GPL(hid_destroy_device);
2235
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002236
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002237static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002238{
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002239 struct hid_driver *hdrv = data;
2240 struct hid_device *hdev = to_hid_device(dev);
2241
2242 if (hdev->driver == hdrv &&
Benjamin Tissoires8f732852018-05-31 13:49:29 +02002243 !hdrv->match(hdev, hid_ignore_special_drivers) &&
2244 !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002245 return device_reprobe(dev);
2246
2247 return 0;
2248}
2249
2250static int __hid_bus_driver_added(struct device_driver *drv, void *data)
2251{
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002252 struct hid_driver *hdrv = to_hid_driver(drv);
2253
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002254 if (hdrv->match) {
2255 bus_for_each_dev(&hid_bus_type, NULL, hdrv,
2256 __hid_bus_reprobe_drivers);
2257 }
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002258
2259 return 0;
2260}
2261
2262static int __bus_removed_driver(struct device_driver *drv, void *data)
2263{
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002264 return bus_rescan_devices(&hid_bus_type);
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002265}
2266
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002267int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
2268 const char *mod_name)
2269{
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002270 int ret;
2271
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002272 hdrv->driver.name = hdrv->name;
2273 hdrv->driver.bus = &hid_bus_type;
2274 hdrv->driver.owner = owner;
2275 hdrv->driver.mod_name = mod_name;
2276
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01002277 INIT_LIST_HEAD(&hdrv->dyn_list);
2278 spin_lock_init(&hdrv->dyn_lock);
2279
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002280 ret = driver_register(&hdrv->driver);
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002281
Benjamin Tissoiresc17a7472017-12-08 15:29:44 +01002282 if (ret == 0)
2283 bus_for_each_drv(&hid_bus_type, NULL, NULL,
2284 __hid_bus_driver_added);
2285
2286 return ret;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002287}
2288EXPORT_SYMBOL_GPL(__hid_register_driver);
2289
2290void hid_unregister_driver(struct hid_driver *hdrv)
2291{
2292 driver_unregister(&hdrv->driver);
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01002293 hid_free_dynids(hdrv);
Benjamin Tissoirese04a0442017-11-20 11:48:44 +01002294
2295 bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002296}
2297EXPORT_SYMBOL_GPL(hid_unregister_driver);
2298
Oliver Neukum0361a282008-12-17 15:38:03 +01002299int hid_check_keys_pressed(struct hid_device *hid)
2300{
2301 struct hid_input *hidinput;
2302 int i;
2303
Jiri Kosinae5288eb2009-05-02 00:02:57 +02002304 if (!(hid->claimed & HID_CLAIMED_INPUT))
2305 return 0;
2306
Oliver Neukum0361a282008-12-17 15:38:03 +01002307 list_for_each_entry(hidinput, &hid->inputs, list) {
2308 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
2309 if (hidinput->input->key[i])
2310 return 1;
2311 }
2312
2313 return 0;
2314}
2315
2316EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
2317
Jiri Kosina86166b72007-05-14 09:57:40 +02002318static int __init hid_init(void)
2319{
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002320 int ret;
2321
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02002322 if (hid_debug)
Joe Perches4291ee32010-12-09 19:29:03 -08002323 pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
2324 "debugfs is now used for inspecting the device (report descriptor, reports)\n");
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02002325
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002326 ret = bus_register(&hid_bus_type);
2327 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002328 pr_err("can't register hid bus\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002329 goto err;
2330 }
2331
2332 ret = hidraw_init();
2333 if (ret)
2334 goto err_bus;
2335
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02002336 hid_debug_init();
2337
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002338 return 0;
2339err_bus:
2340 bus_unregister(&hid_bus_type);
2341err:
2342 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +02002343}
2344
2345static void __exit hid_exit(void)
2346{
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02002347 hid_debug_exit();
Jiri Kosina86166b72007-05-14 09:57:40 +02002348 hidraw_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02002349 bus_unregister(&hid_bus_type);
Benjamin Tissoiresd5d3e202017-11-20 11:48:41 +01002350 hid_quirks_exit(HID_BUS_ANY);
Jiri Kosina86166b72007-05-14 09:57:40 +02002351}
2352
2353module_init(hid_init);
2354module_exit(hid_exit);
2355
Jiri Kosina88adb722009-10-02 18:29:34 +02002356MODULE_AUTHOR("Andreas Gal");
2357MODULE_AUTHOR("Vojtech Pavlik");
2358MODULE_AUTHOR("Jiri Kosina");
Grant Grundler7021b602017-01-05 11:07:04 -08002359MODULE_LICENSE("GPL");