Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
dde5845a | 2 | /* |
229695e5 | 3 | * HID support for Linux |
dde5845a JK |
4 | * |
5 | * Copyright (c) 1999 Andreas Gal | |
6 | * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> | |
7 | * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc | |
6b1968d5 | 8 | * Copyright (c) 2006-2012 Jiri Kosina |
dde5845a JK |
9 | */ |
10 | ||
11 | /* | |
dde5845a JK |
12 | */ |
13 | ||
4291ee30 JP |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
15 | ||
dde5845a JK |
16 | #include <linux/module.h> |
17 | #include <linux/slab.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/kernel.h> | |
dde5845a JK |
20 | #include <linux/list.h> |
21 | #include <linux/mm.h> | |
dde5845a JK |
22 | #include <linux/spinlock.h> |
23 | #include <asm/unaligned.h> | |
24 | #include <asm/byteorder.h> | |
25 | #include <linux/input.h> | |
26 | #include <linux/wait.h> | |
47a80edb | 27 | #include <linux/vmalloc.h> |
c4124c9b | 28 | #include <linux/sched.h> |
4ea54542 | 29 | #include <linux/semaphore.h> |
dde5845a | 30 | |
dde5845a JK |
31 | #include <linux/hid.h> |
32 | #include <linux/hiddev.h> | |
c080d89a | 33 | #include <linux/hid-debug.h> |
86166b7b | 34 | #include <linux/hidraw.h> |
dde5845a | 35 | |
5f22a799 JS |
36 | #include "hid-ids.h" |
37 | ||
dde5845a JK |
38 | /* |
39 | * Version Information | |
40 | */ | |
41 | ||
53149801 | 42 | #define DRIVER_DESC "HID core driver" |
dde5845a | 43 | |
58037eb9 | 44 | int hid_debug = 0; |
377e10fb | 45 | module_param_named(debug, hid_debug, int, 0600); |
cd667ce2 | 46 | MODULE_PARM_DESC(debug, "toggle HID debugging messages"); |
58037eb9 | 47 | EXPORT_SYMBOL_GPL(hid_debug); |
58037eb9 | 48 | |
6b1968d5 JK |
49 | static int hid_ignore_special_drivers = 0; |
50 | module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600); | |
643727a9 | 51 | MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver"); |
6b1968d5 | 52 | |
dde5845a JK |
53 | /* |
54 | * Register a new report for a device. | |
55 | */ | |
56 | ||
f07b3c1d BT |
57 | struct hid_report *hid_register_report(struct hid_device *device, |
58 | unsigned int type, unsigned int id, | |
59 | unsigned int application) | |
dde5845a JK |
60 | { |
61 | struct hid_report_enum *report_enum = device->report_enum + type; | |
62 | struct hid_report *report; | |
63 | ||
43622021 KC |
64 | if (id >= HID_MAX_IDS) |
65 | return NULL; | |
dde5845a JK |
66 | if (report_enum->report_id_hash[id]) |
67 | return report_enum->report_id_hash[id]; | |
68 | ||
a3789a17 JP |
69 | report = kzalloc(sizeof(struct hid_report), GFP_KERNEL); |
70 | if (!report) | |
dde5845a JK |
71 | return NULL; |
72 | ||
73 | if (id != 0) | |
74 | report_enum->numbered = 1; | |
75 | ||
76 | report->id = id; | |
77 | report->type = type; | |
78 | report->size = 0; | |
79 | report->device = device; | |
f07b3c1d | 80 | report->application = application; |
dde5845a JK |
81 | report_enum->report_id_hash[id] = report; |
82 | ||
83 | list_add_tail(&report->list, &report_enum->report_list); | |
84 | ||
85 | return report; | |
86 | } | |
90a006ab | 87 | EXPORT_SYMBOL_GPL(hid_register_report); |
dde5845a JK |
88 | |
89 | /* | |
90 | * Register a new field for this report. | |
91 | */ | |
92 | ||
ed9be64e | 93 | static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages) |
dde5845a JK |
94 | { |
95 | struct hid_field *field; | |
96 | ||
97 | if (report->maxfield == HID_MAX_FIELDS) { | |
8c3d52fc | 98 | hid_err(report->device, "too many fields in report\n"); |
dde5845a JK |
99 | return NULL; |
100 | } | |
101 | ||
a3789a17 JP |
102 | field = kzalloc((sizeof(struct hid_field) + |
103 | usages * sizeof(struct hid_usage) + | |
1c1813a7 | 104 | 2 * usages * sizeof(unsigned int)), GFP_KERNEL); |
a3789a17 JP |
105 | if (!field) |
106 | return NULL; | |
dde5845a JK |
107 | |
108 | field->index = report->maxfield++; | |
109 | report->field[field->index] = field; | |
110 | field->usage = (struct hid_usage *)(field + 1); | |
282bfd4c | 111 | field->value = (s32 *)(field->usage + usages); |
1c1813a7 | 112 | field->new_value = (s32 *)(field->value + usages); |
dde5845a JK |
113 | field->report = report; |
114 | ||
115 | return field; | |
116 | } | |
117 | ||
118 | /* | |
119 | * Open a collection. The type/usage is pushed on the stack. | |
120 | */ | |
121 | ||
122 | static int open_collection(struct hid_parser *parser, unsigned type) | |
123 | { | |
124 | struct hid_collection *collection; | |
125 | unsigned usage; | |
ee46967f | 126 | int collection_index; |
dde5845a JK |
127 | |
128 | usage = parser->local.usage[0]; | |
129 | ||
08a8a7cf BT |
130 | if (parser->collection_stack_ptr == parser->collection_stack_size) { |
131 | unsigned int *collection_stack; | |
132 | unsigned int new_size = parser->collection_stack_size + | |
133 | HID_COLLECTION_STACK_SIZE; | |
134 | ||
135 | collection_stack = krealloc(parser->collection_stack, | |
136 | new_size * sizeof(unsigned int), | |
137 | GFP_KERNEL); | |
138 | if (!collection_stack) | |
139 | return -ENOMEM; | |
140 | ||
141 | parser->collection_stack = collection_stack; | |
142 | parser->collection_stack_size = new_size; | |
dde5845a JK |
143 | } |
144 | ||
145 | if (parser->device->maxcollection == parser->device->collection_size) { | |
6da2ec56 KC |
146 | collection = kmalloc( |
147 | array3_size(sizeof(struct hid_collection), | |
148 | parser->device->collection_size, | |
149 | 2), | |
150 | GFP_KERNEL); | |
dde5845a | 151 | if (collection == NULL) { |
8c3d52fc | 152 | hid_err(parser->device, "failed to reallocate collection array\n"); |
a6fbaacf | 153 | return -ENOMEM; |
dde5845a JK |
154 | } |
155 | memcpy(collection, parser->device->collection, | |
156 | sizeof(struct hid_collection) * | |
157 | parser->device->collection_size); | |
158 | memset(collection + parser->device->collection_size, 0, | |
159 | sizeof(struct hid_collection) * | |
160 | parser->device->collection_size); | |
161 | kfree(parser->device->collection); | |
162 | parser->device->collection = collection; | |
163 | parser->device->collection_size *= 2; | |
164 | } | |
165 | ||
166 | parser->collection_stack[parser->collection_stack_ptr++] = | |
167 | parser->device->maxcollection; | |
168 | ||
ee46967f PH |
169 | collection_index = parser->device->maxcollection++; |
170 | collection = parser->device->collection + collection_index; | |
dde5845a JK |
171 | collection->type = type; |
172 | collection->usage = usage; | |
173 | collection->level = parser->collection_stack_ptr - 1; | |
1950f462 PZ |
174 | collection->parent_idx = (collection->level == 0) ? -1 : |
175 | parser->collection_stack[collection->level - 1]; | |
dde5845a JK |
176 | |
177 | if (type == HID_COLLECTION_APPLICATION) | |
178 | parser->device->maxapplication++; | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
183 | /* | |
184 | * Close a collection. | |
185 | */ | |
186 | ||
187 | static int close_collection(struct hid_parser *parser) | |
188 | { | |
189 | if (!parser->collection_stack_ptr) { | |
8c3d52fc | 190 | hid_err(parser->device, "collection stack underflow\n"); |
a6fbaacf | 191 | return -EINVAL; |
dde5845a JK |
192 | } |
193 | parser->collection_stack_ptr--; | |
194 | return 0; | |
195 | } | |
196 | ||
197 | /* | |
198 | * Climb up the stack, search for the specified collection type | |
199 | * and return the usage. | |
200 | */ | |
201 | ||
202 | static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type) | |
203 | { | |
504499f2 | 204 | struct hid_collection *collection = parser->device->collection; |
dde5845a | 205 | int n; |
504499f2 JP |
206 | |
207 | for (n = parser->collection_stack_ptr - 1; n >= 0; n--) { | |
208 | unsigned index = parser->collection_stack[n]; | |
209 | if (collection[index].type == type) | |
210 | return collection[index].usage; | |
211 | } | |
dde5845a JK |
212 | return 0; /* we know nothing about this usage type */ |
213 | } | |
214 | ||
1cb0d2ae CS |
215 | /* |
216 | * Concatenate usage which defines 16 bits or less with the | |
217 | * currently defined usage page to form a 32 bit usage | |
218 | */ | |
219 | ||
220 | static void complete_usage(struct hid_parser *parser, unsigned int index) | |
221 | { | |
222 | parser->local.usage[index] &= 0xFFFF; | |
223 | parser->local.usage[index] |= | |
224 | (parser->global.usage_page & 0xFFFF) << 16; | |
225 | } | |
226 | ||
dde5845a JK |
227 | /* |
228 | * Add a usage to the temporary parser table. | |
229 | */ | |
230 | ||
58e75155 | 231 | static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size) |
dde5845a JK |
232 | { |
233 | if (parser->local.usage_index >= HID_MAX_USAGES) { | |
8c3d52fc | 234 | hid_err(parser->device, "usage index exceeded\n"); |
dde5845a JK |
235 | return -1; |
236 | } | |
237 | parser->local.usage[parser->local.usage_index] = usage; | |
1cb0d2ae CS |
238 | |
239 | /* | |
240 | * If Usage item only includes usage id, concatenate it with | |
241 | * currently defined usage page | |
242 | */ | |
243 | if (size <= 2) | |
244 | complete_usage(parser, parser->local.usage_index); | |
245 | ||
58e75155 | 246 | parser->local.usage_size[parser->local.usage_index] = size; |
dde5845a JK |
247 | parser->local.collection_index[parser->local.usage_index] = |
248 | parser->collection_stack_ptr ? | |
249 | parser->collection_stack[parser->collection_stack_ptr - 1] : 0; | |
250 | parser->local.usage_index++; | |
251 | return 0; | |
252 | } | |
253 | ||
254 | /* | |
255 | * Register a new field for this report. | |
256 | */ | |
257 | ||
258 | static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags) | |
259 | { | |
260 | struct hid_report *report; | |
261 | struct hid_field *field; | |
f07b3c1d BT |
262 | unsigned int usages; |
263 | unsigned int offset; | |
264 | unsigned int i; | |
265 | unsigned int application; | |
dde5845a | 266 | |
f07b3c1d BT |
267 | application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION); |
268 | ||
269 | report = hid_register_report(parser->device, report_type, | |
270 | parser->global.report_id, application); | |
a3789a17 | 271 | if (!report) { |
8c3d52fc | 272 | hid_err(parser->device, "hid_register_report failed\n"); |
dde5845a JK |
273 | return -1; |
274 | } | |
275 | ||
bb2e1976 | 276 | /* Handle both signed and unsigned cases properly */ |
0cd516c2 | 277 | if ((parser->global.logical_minimum < 0 && |
278 | parser->global.logical_maximum < | |
279 | parser->global.logical_minimum) || | |
280 | (parser->global.logical_minimum >= 0 && | |
281 | (__u32)parser->global.logical_maximum < | |
282 | (__u32)parser->global.logical_minimum)) { | |
283 | dbg_hid("logical range invalid 0x%x 0x%x\n", | |
284 | parser->global.logical_minimum, | |
285 | parser->global.logical_maximum); | |
dde5845a JK |
286 | return -1; |
287 | } | |
288 | ||
289 | offset = report->size; | |
290 | report->size += parser->global.report_size * parser->global.report_count; | |
291 | ||
8ec321e9 AS |
292 | /* Total size check: Allow for possible report index byte */ |
293 | if (report->size > (HID_MAX_BUFFER_SIZE - 1) << 3) { | |
294 | hid_err(parser->device, "report is too long\n"); | |
295 | return -1; | |
296 | } | |
297 | ||
dde5845a JK |
298 | if (!parser->local.usage_index) /* Ignore padding fields */ |
299 | return 0; | |
300 | ||
cc6b54aa BT |
301 | usages = max_t(unsigned, parser->local.usage_index, |
302 | parser->global.report_count); | |
dde5845a | 303 | |
ed9be64e | 304 | field = hid_register_field(report, usages); |
a3789a17 | 305 | if (!field) |
dde5845a JK |
306 | return 0; |
307 | ||
308 | field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL); | |
309 | field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL); | |
f07b3c1d | 310 | field->application = application; |
dde5845a JK |
311 | |
312 | for (i = 0; i < usages; i++) { | |
cc6b54aa | 313 | unsigned j = i; |
dde5845a JK |
314 | /* Duplicate the last usage we parsed if we have excess values */ |
315 | if (i >= parser->local.usage_index) | |
316 | j = parser->local.usage_index - 1; | |
317 | field->usage[i].hid = parser->local.usage[j]; | |
318 | field->usage[i].collection_index = | |
319 | parser->local.collection_index[j]; | |
cc6b54aa | 320 | field->usage[i].usage_index = i; |
5a4abb36 | 321 | field->usage[i].resolution_multiplier = 1; |
dde5845a JK |
322 | } |
323 | ||
324 | field->maxusage = usages; | |
325 | field->flags = flags; | |
326 | field->report_offset = offset; | |
327 | field->report_type = report_type; | |
328 | field->report_size = parser->global.report_size; | |
329 | field->report_count = parser->global.report_count; | |
330 | field->logical_minimum = parser->global.logical_minimum; | |
331 | field->logical_maximum = parser->global.logical_maximum; | |
332 | field->physical_minimum = parser->global.physical_minimum; | |
333 | field->physical_maximum = parser->global.physical_maximum; | |
334 | field->unit_exponent = parser->global.unit_exponent; | |
335 | field->unit = parser->global.unit; | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
340 | /* | |
341 | * Read data value from item. | |
342 | */ | |
343 | ||
344 | static u32 item_udata(struct hid_item *item) | |
345 | { | |
346 | switch (item->size) { | |
880d29f1 JS |
347 | case 1: return item->data.u8; |
348 | case 2: return item->data.u16; | |
349 | case 4: return item->data.u32; | |
dde5845a JK |
350 | } |
351 | return 0; | |
352 | } | |
353 | ||
354 | static s32 item_sdata(struct hid_item *item) | |
355 | { | |
356 | switch (item->size) { | |
880d29f1 JS |
357 | case 1: return item->data.s8; |
358 | case 2: return item->data.s16; | |
359 | case 4: return item->data.s32; | |
dde5845a JK |
360 | } |
361 | return 0; | |
362 | } | |
363 | ||
364 | /* | |
365 | * Process a global item. | |
366 | */ | |
367 | ||
368 | static int hid_parser_global(struct hid_parser *parser, struct hid_item *item) | |
369 | { | |
ad0e669b | 370 | __s32 raw_value; |
dde5845a | 371 | switch (item->tag) { |
880d29f1 | 372 | case HID_GLOBAL_ITEM_TAG_PUSH: |
dde5845a | 373 | |
880d29f1 | 374 | if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) { |
8c3d52fc | 375 | hid_err(parser->device, "global environment stack overflow\n"); |
880d29f1 JS |
376 | return -1; |
377 | } | |
dde5845a | 378 | |
880d29f1 JS |
379 | memcpy(parser->global_stack + parser->global_stack_ptr++, |
380 | &parser->global, sizeof(struct hid_global)); | |
381 | return 0; | |
dde5845a | 382 | |
880d29f1 | 383 | case HID_GLOBAL_ITEM_TAG_POP: |
dde5845a | 384 | |
880d29f1 | 385 | if (!parser->global_stack_ptr) { |
8c3d52fc | 386 | hid_err(parser->device, "global environment stack underflow\n"); |
880d29f1 JS |
387 | return -1; |
388 | } | |
dde5845a | 389 | |
880d29f1 JS |
390 | memcpy(&parser->global, parser->global_stack + |
391 | --parser->global_stack_ptr, sizeof(struct hid_global)); | |
392 | return 0; | |
dde5845a | 393 | |
880d29f1 JS |
394 | case HID_GLOBAL_ITEM_TAG_USAGE_PAGE: |
395 | parser->global.usage_page = item_udata(item); | |
396 | return 0; | |
dde5845a | 397 | |
880d29f1 JS |
398 | case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM: |
399 | parser->global.logical_minimum = item_sdata(item); | |
400 | return 0; | |
dde5845a | 401 | |
880d29f1 JS |
402 | case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM: |
403 | if (parser->global.logical_minimum < 0) | |
404 | parser->global.logical_maximum = item_sdata(item); | |
405 | else | |
406 | parser->global.logical_maximum = item_udata(item); | |
407 | return 0; | |
dde5845a | 408 | |
880d29f1 JS |
409 | case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM: |
410 | parser->global.physical_minimum = item_sdata(item); | |
411 | return 0; | |
dde5845a | 412 | |
880d29f1 JS |
413 | case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM: |
414 | if (parser->global.physical_minimum < 0) | |
415 | parser->global.physical_maximum = item_sdata(item); | |
416 | else | |
417 | parser->global.physical_maximum = item_udata(item); | |
418 | return 0; | |
dde5845a | 419 | |
880d29f1 | 420 | case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT: |
ad0e669b NK |
421 | /* Many devices provide unit exponent as a two's complement |
422 | * nibble due to the common misunderstanding of HID | |
423 | * specification 1.11, 6.2.2.7 Global Items. Attempt to handle | |
424 | * both this and the standard encoding. */ | |
425 | raw_value = item_sdata(item); | |
77463838 BT |
426 | if (!(raw_value & 0xfffffff0)) |
427 | parser->global.unit_exponent = hid_snto32(raw_value, 4); | |
428 | else | |
429 | parser->global.unit_exponent = raw_value; | |
880d29f1 | 430 | return 0; |
dde5845a | 431 | |
880d29f1 JS |
432 | case HID_GLOBAL_ITEM_TAG_UNIT: |
433 | parser->global.unit = item_udata(item); | |
434 | return 0; | |
dde5845a | 435 | |
880d29f1 JS |
436 | case HID_GLOBAL_ITEM_TAG_REPORT_SIZE: |
437 | parser->global.report_size = item_udata(item); | |
71f6fa90 | 438 | if (parser->global.report_size > 256) { |
8c3d52fc | 439 | hid_err(parser->device, "invalid report_size %d\n", |
880d29f1 JS |
440 | parser->global.report_size); |
441 | return -1; | |
442 | } | |
443 | return 0; | |
dde5845a | 444 | |
880d29f1 JS |
445 | case HID_GLOBAL_ITEM_TAG_REPORT_COUNT: |
446 | parser->global.report_count = item_udata(item); | |
447 | if (parser->global.report_count > HID_MAX_USAGES) { | |
8c3d52fc | 448 | hid_err(parser->device, "invalid report_count %d\n", |
880d29f1 JS |
449 | parser->global.report_count); |
450 | return -1; | |
451 | } | |
452 | return 0; | |
dde5845a | 453 | |
880d29f1 JS |
454 | case HID_GLOBAL_ITEM_TAG_REPORT_ID: |
455 | parser->global.report_id = item_udata(item); | |
43622021 KC |
456 | if (parser->global.report_id == 0 || |
457 | parser->global.report_id >= HID_MAX_IDS) { | |
458 | hid_err(parser->device, "report_id %u is invalid\n", | |
459 | parser->global.report_id); | |
dde5845a | 460 | return -1; |
880d29f1 JS |
461 | } |
462 | return 0; | |
463 | ||
464 | default: | |
8c3d52fc | 465 | hid_err(parser->device, "unknown global tag 0x%x\n", item->tag); |
880d29f1 | 466 | return -1; |
dde5845a JK |
467 | } |
468 | } | |
469 | ||
470 | /* | |
471 | * Process a local item. | |
472 | */ | |
473 | ||
474 | static int hid_parser_local(struct hid_parser *parser, struct hid_item *item) | |
475 | { | |
476 | __u32 data; | |
477 | unsigned n; | |
ba532198 | 478 | __u32 count; |
dde5845a | 479 | |
dde5845a JK |
480 | data = item_udata(item); |
481 | ||
482 | switch (item->tag) { | |
880d29f1 JS |
483 | case HID_LOCAL_ITEM_TAG_DELIMITER: |
484 | ||
485 | if (data) { | |
486 | /* | |
487 | * We treat items before the first delimiter | |
488 | * as global to all usage sets (branch 0). | |
489 | * In the moment we process only these global | |
490 | * items and the first delimiter set. | |
491 | */ | |
492 | if (parser->local.delimiter_depth != 0) { | |
8c3d52fc | 493 | hid_err(parser->device, "nested delimiters\n"); |
880d29f1 | 494 | return -1; |
dde5845a | 495 | } |
880d29f1 JS |
496 | parser->local.delimiter_depth++; |
497 | parser->local.delimiter_branch++; | |
498 | } else { | |
499 | if (parser->local.delimiter_depth < 1) { | |
8c3d52fc | 500 | hid_err(parser->device, "bogus close delimiter\n"); |
880d29f1 | 501 | return -1; |
dde5845a | 502 | } |
880d29f1 JS |
503 | parser->local.delimiter_depth--; |
504 | } | |
38ead6ef | 505 | return 0; |
dde5845a | 506 | |
880d29f1 | 507 | case HID_LOCAL_ITEM_TAG_USAGE: |
dde5845a | 508 | |
880d29f1 JS |
509 | if (parser->local.delimiter_branch > 1) { |
510 | dbg_hid("alternative usage ignored\n"); | |
511 | return 0; | |
512 | } | |
dde5845a | 513 | |
58e75155 | 514 | return hid_add_usage(parser, data, item->size); |
dde5845a | 515 | |
880d29f1 | 516 | case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM: |
dde5845a | 517 | |
880d29f1 JS |
518 | if (parser->local.delimiter_branch > 1) { |
519 | dbg_hid("alternative usage ignored\n"); | |
dde5845a | 520 | return 0; |
880d29f1 | 521 | } |
dde5845a | 522 | |
880d29f1 JS |
523 | parser->local.usage_minimum = data; |
524 | return 0; | |
dde5845a | 525 | |
880d29f1 | 526 | case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM: |
dde5845a | 527 | |
880d29f1 JS |
528 | if (parser->local.delimiter_branch > 1) { |
529 | dbg_hid("alternative usage ignored\n"); | |
dde5845a | 530 | return 0; |
880d29f1 | 531 | } |
dde5845a | 532 | |
ba532198 BT |
533 | count = data - parser->local.usage_minimum; |
534 | if (count + parser->local.usage_index >= HID_MAX_USAGES) { | |
535 | /* | |
536 | * We do not warn if the name is not set, we are | |
537 | * actually pre-scanning the device. | |
538 | */ | |
539 | if (dev_name(&parser->device->dev)) | |
540 | hid_warn(parser->device, | |
541 | "ignoring exceeding usage max\n"); | |
542 | data = HID_MAX_USAGES - parser->local.usage_index + | |
543 | parser->local.usage_minimum - 1; | |
544 | if (data <= 0) { | |
545 | hid_err(parser->device, | |
546 | "no more usage index available\n"); | |
547 | return -1; | |
548 | } | |
549 | } | |
550 | ||
880d29f1 | 551 | for (n = parser->local.usage_minimum; n <= data; n++) |
58e75155 | 552 | if (hid_add_usage(parser, n, item->size)) { |
880d29f1 JS |
553 | dbg_hid("hid_add_usage failed\n"); |
554 | return -1; | |
555 | } | |
556 | return 0; | |
557 | ||
558 | default: | |
559 | ||
560 | dbg_hid("unknown local item tag 0x%x\n", item->tag); | |
561 | return 0; | |
dde5845a JK |
562 | } |
563 | return 0; | |
564 | } | |
565 | ||
58e75155 NSJ |
566 | /* |
567 | * Concatenate Usage Pages into Usages where relevant: | |
568 | * As per specification, 6.2.2.8: "When the parser encounters a main item it | |
569 | * concatenates the last declared Usage Page with a Usage to form a complete | |
570 | * usage value." | |
571 | */ | |
572 | ||
1cb0d2ae | 573 | static void hid_concatenate_last_usage_page(struct hid_parser *parser) |
58e75155 NSJ |
574 | { |
575 | int i; | |
1cb0d2ae CS |
576 | unsigned int usage_page; |
577 | unsigned int current_page; | |
578 | ||
579 | if (!parser->local.usage_index) | |
580 | return; | |
581 | ||
582 | usage_page = parser->global.usage_page; | |
583 | ||
584 | /* | |
585 | * Concatenate usage page again only if last declared Usage Page | |
586 | * has not been already used in previous usages concatenation | |
587 | */ | |
588 | for (i = parser->local.usage_index - 1; i >= 0; i--) { | |
589 | if (parser->local.usage_size[i] > 2) | |
590 | /* Ignore extended usages */ | |
591 | continue; | |
58e75155 | 592 | |
1cb0d2ae CS |
593 | current_page = parser->local.usage[i] >> 16; |
594 | if (current_page == usage_page) | |
595 | break; | |
596 | ||
597 | complete_usage(parser, i); | |
598 | } | |
58e75155 NSJ |
599 | } |
600 | ||
dde5845a JK |
601 | /* |
602 | * Process a main item. | |
603 | */ | |
604 | ||
605 | static int hid_parser_main(struct hid_parser *parser, struct hid_item *item) | |
606 | { | |
607 | __u32 data; | |
608 | int ret; | |
609 | ||
1cb0d2ae | 610 | hid_concatenate_last_usage_page(parser); |
58e75155 | 611 | |
dde5845a JK |
612 | data = item_udata(item); |
613 | ||
614 | switch (item->tag) { | |
880d29f1 JS |
615 | case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION: |
616 | ret = open_collection(parser, data & 0xff); | |
617 | break; | |
618 | case HID_MAIN_ITEM_TAG_END_COLLECTION: | |
619 | ret = close_collection(parser); | |
620 | break; | |
621 | case HID_MAIN_ITEM_TAG_INPUT: | |
622 | ret = hid_add_field(parser, HID_INPUT_REPORT, data); | |
623 | break; | |
624 | case HID_MAIN_ITEM_TAG_OUTPUT: | |
625 | ret = hid_add_field(parser, HID_OUTPUT_REPORT, data); | |
626 | break; | |
627 | case HID_MAIN_ITEM_TAG_FEATURE: | |
628 | ret = hid_add_field(parser, HID_FEATURE_REPORT, data); | |
629 | break; | |
630 | default: | |
7cb4774e | 631 | hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag); |
880d29f1 | 632 | ret = 0; |
dde5845a JK |
633 | } |
634 | ||
635 | memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */ | |
636 | ||
637 | return ret; | |
638 | } | |
639 | ||
640 | /* | |
641 | * Process a reserved item. | |
642 | */ | |
643 | ||
644 | static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item) | |
645 | { | |
58037eb9 | 646 | dbg_hid("reserved item type, tag 0x%x\n", item->tag); |
dde5845a JK |
647 | return 0; |
648 | } | |
649 | ||
650 | /* | |
651 | * Free a report and all registered fields. The field->usage and | |
652 | * field->value table's are allocated behind the field, so we need | |
653 | * only to free(field) itself. | |
654 | */ | |
655 | ||
656 | static void hid_free_report(struct hid_report *report) | |
657 | { | |
658 | unsigned n; | |
659 | ||
660 | for (n = 0; n < report->maxfield; n++) | |
661 | kfree(report->field[n]); | |
662 | kfree(report); | |
663 | } | |
664 | ||
665 | /* | |
a7197c2e HR |
666 | * Close report. This function returns the device |
667 | * state to the point prior to hid_open_report(). | |
dde5845a | 668 | */ |
a7197c2e | 669 | static void hid_close_report(struct hid_device *device) |
dde5845a | 670 | { |
85cdaf52 | 671 | unsigned i, j; |
dde5845a JK |
672 | |
673 | for (i = 0; i < HID_REPORT_TYPES; i++) { | |
674 | struct hid_report_enum *report_enum = device->report_enum + i; | |
675 | ||
43622021 | 676 | for (j = 0; j < HID_MAX_IDS; j++) { |
dde5845a JK |
677 | struct hid_report *report = report_enum->report_id_hash[j]; |
678 | if (report) | |
679 | hid_free_report(report); | |
680 | } | |
a7197c2e HR |
681 | memset(report_enum, 0, sizeof(*report_enum)); |
682 | INIT_LIST_HEAD(&report_enum->report_list); | |
dde5845a JK |
683 | } |
684 | ||
685 | kfree(device->rdesc); | |
a7197c2e HR |
686 | device->rdesc = NULL; |
687 | device->rsize = 0; | |
688 | ||
767fe787 | 689 | kfree(device->collection); |
a7197c2e HR |
690 | device->collection = NULL; |
691 | device->collection_size = 0; | |
692 | device->maxcollection = 0; | |
693 | device->maxapplication = 0; | |
694 | ||
695 | device->status &= ~HID_STAT_PARSED; | |
696 | } | |
697 | ||
698 | /* | |
699 | * Free a device structure, all reports, and all fields. | |
700 | */ | |
701 | ||
702 | static void hid_device_release(struct device *dev) | |
703 | { | |
ee79a8f8 | 704 | struct hid_device *hid = to_hid_device(dev); |
a7197c2e HR |
705 | |
706 | hid_close_report(hid); | |
707 | kfree(hid->dev_rdesc); | |
708 | kfree(hid); | |
dde5845a JK |
709 | } |
710 | ||
711 | /* | |
712 | * Fetch a report description item from the data stream. We support long | |
713 | * items, though they are not used yet. | |
714 | */ | |
715 | ||
716 | static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item) | |
717 | { | |
718 | u8 b; | |
719 | ||
720 | if ((end - start) <= 0) | |
721 | return NULL; | |
722 | ||
723 | b = *start++; | |
724 | ||
725 | item->type = (b >> 2) & 3; | |
726 | item->tag = (b >> 4) & 15; | |
727 | ||
728 | if (item->tag == HID_ITEM_TAG_LONG) { | |
729 | ||
730 | item->format = HID_ITEM_FORMAT_LONG; | |
731 | ||
732 | if ((end - start) < 2) | |
733 | return NULL; | |
734 | ||
735 | item->size = *start++; | |
736 | item->tag = *start++; | |
737 | ||
738 | if ((end - start) < item->size) | |
739 | return NULL; | |
740 | ||
741 | item->data.longdata = start; | |
742 | start += item->size; | |
743 | return start; | |
744 | } | |
745 | ||
746 | item->format = HID_ITEM_FORMAT_SHORT; | |
747 | item->size = b & 3; | |
748 | ||
749 | switch (item->size) { | |
880d29f1 JS |
750 | case 0: |
751 | return start; | |
dde5845a | 752 | |
880d29f1 JS |
753 | case 1: |
754 | if ((end - start) < 1) | |
755 | return NULL; | |
756 | item->data.u8 = *start++; | |
757 | return start; | |
758 | ||
759 | case 2: | |
760 | if ((end - start) < 2) | |
761 | return NULL; | |
762 | item->data.u16 = get_unaligned_le16(start); | |
763 | start = (__u8 *)((__le16 *)start + 1); | |
764 | return start; | |
765 | ||
766 | case 3: | |
767 | item->size++; | |
768 | if ((end - start) < 4) | |
769 | return NULL; | |
770 | item->data.u32 = get_unaligned_le32(start); | |
771 | start = (__u8 *)((__le32 *)start + 1); | |
772 | return start; | |
dde5845a JK |
773 | } |
774 | ||
775 | return NULL; | |
776 | } | |
777 | ||
3dc8fc08 | 778 | static void hid_scan_input_usage(struct hid_parser *parser, u32 usage) |
734c6609 | 779 | { |
3dc8fc08 BT |
780 | struct hid_device *hid = parser->device; |
781 | ||
4fa3a583 HR |
782 | if (usage == HID_DG_CONTACTID) |
783 | hid->group = HID_GROUP_MULTITOUCH; | |
734c6609 HR |
784 | } |
785 | ||
f961bd35 BT |
786 | static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage) |
787 | { | |
788 | if (usage == 0xff0000c5 && parser->global.report_count == 256 && | |
789 | parser->global.report_size == 8) | |
790 | parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8; | |
2dbc6f11 BH |
791 | |
792 | if (usage == 0xff0000c6 && parser->global.report_count == 1 && | |
793 | parser->global.report_size == 8) | |
794 | parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8; | |
f961bd35 BT |
795 | } |
796 | ||
3dc8fc08 BT |
797 | static void hid_scan_collection(struct hid_parser *parser, unsigned type) |
798 | { | |
799 | struct hid_device *hid = parser->device; | |
e39f2d59 | 800 | int i; |
3dc8fc08 BT |
801 | |
802 | if (((parser->global.usage_page << 16) == HID_UP_SENSOR) && | |
803 | type == HID_COLLECTION_PHYSICAL) | |
804 | hid->group = HID_GROUP_SENSOR_HUB; | |
be3b1634 AW |
805 | |
806 | if (hid->vendor == USB_VENDOR_ID_MICROSOFT && | |
0a76ac80 | 807 | hid->product == USB_DEVICE_ID_MS_POWER_COVER && |
be3b1634 AW |
808 | hid->group == HID_GROUP_MULTITOUCH) |
809 | hid->group = HID_GROUP_GENERIC; | |
e39f2d59 AD |
810 | |
811 | if ((parser->global.usage_page << 16) == HID_UP_GENDESK) | |
812 | for (i = 0; i < parser->local.usage_index; i++) | |
813 | if (parser->local.usage[i] == HID_GD_POINTER) | |
814 | parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER; | |
815 | ||
816 | if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR) | |
817 | parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC; | |
14c9c014 SB |
818 | |
819 | if ((parser->global.usage_page << 16) == HID_UP_GOOGLEVENDOR) | |
820 | for (i = 0; i < parser->local.usage_index; i++) | |
821 | if (parser->local.usage[i] == | |
822 | (HID_UP_GOOGLEVENDOR | 0x0001)) | |
823 | parser->device->group = | |
824 | HID_GROUP_VIVALDI; | |
3dc8fc08 BT |
825 | } |
826 | ||
827 | static int hid_scan_main(struct hid_parser *parser, struct hid_item *item) | |
828 | { | |
829 | __u32 data; | |
830 | int i; | |
831 | ||
1cb0d2ae | 832 | hid_concatenate_last_usage_page(parser); |
58e75155 | 833 | |
3dc8fc08 BT |
834 | data = item_udata(item); |
835 | ||
836 | switch (item->tag) { | |
837 | case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION: | |
838 | hid_scan_collection(parser, data & 0xff); | |
839 | break; | |
840 | case HID_MAIN_ITEM_TAG_END_COLLECTION: | |
841 | break; | |
842 | case HID_MAIN_ITEM_TAG_INPUT: | |
e24d0d39 BT |
843 | /* ignore constant inputs, they will be ignored by hid-input */ |
844 | if (data & HID_MAIN_ITEM_CONSTANT) | |
845 | break; | |
3dc8fc08 BT |
846 | for (i = 0; i < parser->local.usage_index; i++) |
847 | hid_scan_input_usage(parser, parser->local.usage[i]); | |
848 | break; | |
849 | case HID_MAIN_ITEM_TAG_OUTPUT: | |
850 | break; | |
851 | case HID_MAIN_ITEM_TAG_FEATURE: | |
f961bd35 BT |
852 | for (i = 0; i < parser->local.usage_index; i++) |
853 | hid_scan_feature_usage(parser, parser->local.usage[i]); | |
3dc8fc08 BT |
854 | break; |
855 | } | |
856 | ||
857 | /* Reset the local parser environment */ | |
858 | memset(&parser->local, 0, sizeof(parser->local)); | |
859 | ||
860 | return 0; | |
861 | } | |
862 | ||
734c6609 HR |
863 | /* |
864 | * Scan a report descriptor before the device is added to the bus. | |
865 | * Sets device groups and other properties that determine what driver | |
866 | * to load. | |
867 | */ | |
868 | static int hid_scan_report(struct hid_device *hid) | |
869 | { | |
3dc8fc08 BT |
870 | struct hid_parser *parser; |
871 | struct hid_item item; | |
734c6609 HR |
872 | __u8 *start = hid->dev_rdesc; |
873 | __u8 *end = start + hid->dev_rsize; | |
3dc8fc08 BT |
874 | static int (*dispatch_type[])(struct hid_parser *parser, |
875 | struct hid_item *item) = { | |
876 | hid_scan_main, | |
877 | hid_parser_global, | |
878 | hid_parser_local, | |
879 | hid_parser_reserved | |
880 | }; | |
881 | ||
882 | parser = vzalloc(sizeof(struct hid_parser)); | |
883 | if (!parser) | |
884 | return -ENOMEM; | |
734c6609 | 885 | |
3dc8fc08 | 886 | parser->device = hid; |
734c6609 | 887 | hid->group = HID_GROUP_GENERIC; |
734c6609 | 888 | |
3dc8fc08 BT |
889 | /* |
890 | * The parsing is simpler than the one in hid_open_report() as we should | |
891 | * be robust against hid errors. Those errors will be raised by | |
892 | * hid_open_report() anyway. | |
893 | */ | |
894 | while ((start = fetch_item(start, end, &item)) != NULL) | |
895 | dispatch_type[item.type](parser, &item); | |
896 | ||
f961bd35 BT |
897 | /* |
898 | * Handle special flags set during scanning. | |
899 | */ | |
900 | if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) && | |
901 | (hid->group == HID_GROUP_MULTITOUCH)) | |
902 | hid->group = HID_GROUP_MULTITOUCH_WIN_8; | |
903 | ||
29b47391 BT |
904 | /* |
905 | * Vendor specific handlings | |
906 | */ | |
907 | switch (hid->vendor) { | |
908 | case USB_VENDOR_ID_WACOM: | |
909 | hid->group = HID_GROUP_WACOM; | |
910 | break; | |
c241c5ee | 911 | case USB_VENDOR_ID_SYNAPTICS: |
84379d83 | 912 | if (hid->group == HID_GROUP_GENERIC) |
e39f2d59 AD |
913 | if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC) |
914 | && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER)) | |
915 | /* | |
916 | * hid-rmi should take care of them, | |
917 | * not hid-generic | |
918 | */ | |
0ca4cd7b | 919 | hid->group = HID_GROUP_RMI; |
c241c5ee | 920 | break; |
29b47391 BT |
921 | } |
922 | ||
08a8a7cf | 923 | kfree(parser->collection_stack); |
3dc8fc08 | 924 | vfree(parser); |
734c6609 HR |
925 | return 0; |
926 | } | |
927 | ||
85cdaf52 JS |
928 | /** |
929 | * hid_parse_report - parse device report | |
930 | * | |
5f94e9c8 | 931 | * @hid: hid device |
85cdaf52 JS |
932 | * @start: report start |
933 | * @size: report size | |
934 | * | |
a7197c2e HR |
935 | * Allocate the device report as read by the bus driver. This function should |
936 | * only be called from parse() in ll drivers. | |
937 | */ | |
938 | int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size) | |
939 | { | |
940 | hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL); | |
941 | if (!hid->dev_rdesc) | |
942 | return -ENOMEM; | |
943 | hid->dev_rsize = size; | |
944 | return 0; | |
945 | } | |
946 | EXPORT_SYMBOL_GPL(hid_parse_report); | |
947 | ||
331415ff KC |
948 | static const char * const hid_report_names[] = { |
949 | "HID_INPUT_REPORT", | |
950 | "HID_OUTPUT_REPORT", | |
951 | "HID_FEATURE_REPORT", | |
952 | }; | |
953 | /** | |
954 | * hid_validate_values - validate existing device report's value indexes | |
955 | * | |
5f94e9c8 | 956 | * @hid: hid device |
331415ff KC |
957 | * @type: which report type to examine |
958 | * @id: which report ID to examine (0 for first) | |
959 | * @field_index: which report field to examine | |
960 | * @report_counts: expected number of values | |
961 | * | |
962 | * Validate the number of values in a given field of a given report, after | |
963 | * parsing. | |
964 | */ | |
965 | struct hid_report *hid_validate_values(struct hid_device *hid, | |
966 | unsigned int type, unsigned int id, | |
967 | unsigned int field_index, | |
968 | unsigned int report_counts) | |
969 | { | |
970 | struct hid_report *report; | |
971 | ||
972 | if (type > HID_FEATURE_REPORT) { | |
973 | hid_err(hid, "invalid HID report type %u\n", type); | |
974 | return NULL; | |
975 | } | |
976 | ||
977 | if (id >= HID_MAX_IDS) { | |
978 | hid_err(hid, "invalid HID report id %u\n", id); | |
979 | return NULL; | |
980 | } | |
981 | ||
982 | /* | |
983 | * Explicitly not using hid_get_report() here since it depends on | |
984 | * ->numbered being checked, which may not always be the case when | |
985 | * drivers go to access report values. | |
986 | */ | |
1b15d2e5 KC |
987 | if (id == 0) { |
988 | /* | |
989 | * Validating on id 0 means we should examine the first | |
990 | * report in the list. | |
991 | */ | |
992 | report = list_entry( | |
993 | hid->report_enum[type].report_list.next, | |
994 | struct hid_report, list); | |
995 | } else { | |
996 | report = hid->report_enum[type].report_id_hash[id]; | |
997 | } | |
331415ff KC |
998 | if (!report) { |
999 | hid_err(hid, "missing %s %u\n", hid_report_names[type], id); | |
1000 | return NULL; | |
1001 | } | |
1002 | if (report->maxfield <= field_index) { | |
1003 | hid_err(hid, "not enough fields in %s %u\n", | |
1004 | hid_report_names[type], id); | |
1005 | return NULL; | |
1006 | } | |
1007 | if (report->field[field_index]->report_count < report_counts) { | |
1008 | hid_err(hid, "not enough values in %s %u field %u\n", | |
1009 | hid_report_names[type], id, field_index); | |
1010 | return NULL; | |
1011 | } | |
1012 | return report; | |
1013 | } | |
1014 | EXPORT_SYMBOL_GPL(hid_validate_values); | |
1015 | ||
5a4abb36 PH |
1016 | static int hid_calculate_multiplier(struct hid_device *hid, |
1017 | struct hid_field *multiplier) | |
1018 | { | |
1019 | int m; | |
1020 | __s32 v = *multiplier->value; | |
1021 | __s32 lmin = multiplier->logical_minimum; | |
1022 | __s32 lmax = multiplier->logical_maximum; | |
1023 | __s32 pmin = multiplier->physical_minimum; | |
1024 | __s32 pmax = multiplier->physical_maximum; | |
1025 | ||
1026 | /* | |
1027 | * "Because OS implementations will generally divide the control's | |
1028 | * reported count by the Effective Resolution Multiplier, designers | |
1029 | * should take care not to establish a potential Effective | |
1030 | * Resolution Multiplier of zero." | |
1031 | * HID Usage Table, v1.12, Section 4.3.1, p31 | |
1032 | */ | |
1033 | if (lmax - lmin == 0) | |
1034 | return 1; | |
1035 | /* | |
1036 | * Handling the unit exponent is left as an exercise to whoever | |
1037 | * finds a device where that exponent is not 0. | |
1038 | */ | |
1039 | m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin); | |
1040 | if (unlikely(multiplier->unit_exponent != 0)) { | |
1041 | hid_warn(hid, | |
1042 | "unsupported Resolution Multiplier unit exponent %d\n", | |
1043 | multiplier->unit_exponent); | |
1044 | } | |
1045 | ||
1046 | /* There are no devices with an effective multiplier > 255 */ | |
1047 | if (unlikely(m == 0 || m > 255 || m < -255)) { | |
1048 | hid_warn(hid, "unsupported Resolution Multiplier %d\n", m); | |
1049 | m = 1; | |
1050 | } | |
1051 | ||
1052 | return m; | |
1053 | } | |
1054 | ||
1055 | static void hid_apply_multiplier_to_field(struct hid_device *hid, | |
1056 | struct hid_field *field, | |
1057 | struct hid_collection *multiplier_collection, | |
1058 | int effective_multiplier) | |
1059 | { | |
1060 | struct hid_collection *collection; | |
1061 | struct hid_usage *usage; | |
1062 | int i; | |
1063 | ||
1064 | /* | |
1065 | * If multiplier_collection is NULL, the multiplier applies | |
1066 | * to all fields in the report. | |
1067 | * Otherwise, it is the Logical Collection the multiplier applies to | |
1068 | * but our field may be in a subcollection of that collection. | |
1069 | */ | |
1070 | for (i = 0; i < field->maxusage; i++) { | |
1071 | usage = &field->usage[i]; | |
1072 | ||
1073 | collection = &hid->collection[usage->collection_index]; | |
ee46967f PH |
1074 | while (collection->parent_idx != -1 && |
1075 | collection != multiplier_collection) | |
1076 | collection = &hid->collection[collection->parent_idx]; | |
5a4abb36 | 1077 | |
ee46967f PH |
1078 | if (collection->parent_idx != -1 || |
1079 | multiplier_collection == NULL) | |
5a4abb36 PH |
1080 | usage->resolution_multiplier = effective_multiplier; |
1081 | ||
1082 | } | |
1083 | } | |
1084 | ||
1085 | static void hid_apply_multiplier(struct hid_device *hid, | |
1086 | struct hid_field *multiplier) | |
1087 | { | |
1088 | struct hid_report_enum *rep_enum; | |
1089 | struct hid_report *rep; | |
1090 | struct hid_field *field; | |
1091 | struct hid_collection *multiplier_collection; | |
1092 | int effective_multiplier; | |
1093 | int i; | |
1094 | ||
1095 | /* | |
1096 | * "The Resolution Multiplier control must be contained in the same | |
1097 | * Logical Collection as the control(s) to which it is to be applied. | |
1098 | * If no Resolution Multiplier is defined, then the Resolution | |
1099 | * Multiplier defaults to 1. If more than one control exists in a | |
1100 | * Logical Collection, the Resolution Multiplier is associated with | |
1101 | * all controls in the collection. If no Logical Collection is | |
1102 | * defined, the Resolution Multiplier is associated with all | |
1103 | * controls in the report." | |
1104 | * HID Usage Table, v1.12, Section 4.3.1, p30 | |
1105 | * | |
1106 | * Thus, search from the current collection upwards until we find a | |
1107 | * logical collection. Then search all fields for that same parent | |
1108 | * collection. Those are the fields the multiplier applies to. | |
1109 | * | |
1110 | * If we have more than one multiplier, it will overwrite the | |
1111 | * applicable fields later. | |
1112 | */ | |
1113 | multiplier_collection = &hid->collection[multiplier->usage->collection_index]; | |
ee46967f | 1114 | while (multiplier_collection->parent_idx != -1 && |
5a4abb36 | 1115 | multiplier_collection->type != HID_COLLECTION_LOGICAL) |
ee46967f | 1116 | multiplier_collection = &hid->collection[multiplier_collection->parent_idx]; |
5a4abb36 PH |
1117 | |
1118 | effective_multiplier = hid_calculate_multiplier(hid, multiplier); | |
1119 | ||
1120 | rep_enum = &hid->report_enum[HID_INPUT_REPORT]; | |
1121 | list_for_each_entry(rep, &rep_enum->report_list, list) { | |
1122 | for (i = 0; i < rep->maxfield; i++) { | |
1123 | field = rep->field[i]; | |
1124 | hid_apply_multiplier_to_field(hid, field, | |
1125 | multiplier_collection, | |
1126 | effective_multiplier); | |
1127 | } | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | /* | |
1132 | * hid_setup_resolution_multiplier - set up all resolution multipliers | |
1133 | * | |
1134 | * @device: hid device | |
1135 | * | |
1136 | * Search for all Resolution Multiplier Feature Reports and apply their | |
1137 | * value to all matching Input items. This only updates the internal struct | |
1138 | * fields. | |
1139 | * | |
1140 | * The Resolution Multiplier is applied by the hardware. If the multiplier | |
1141 | * is anything other than 1, the hardware will send pre-multiplied events | |
1142 | * so that the same physical interaction generates an accumulated | |
1143 | * accumulated_value = value * * multiplier | |
1144 | * This may be achieved by sending | |
1145 | * - "value * multiplier" for each event, or | |
1146 | * - "value" but "multiplier" times as frequently, or | |
1147 | * - a combination of the above | |
1148 | * The only guarantee is that the same physical interaction always generates | |
1149 | * an accumulated 'value * multiplier'. | |
1150 | * | |
1151 | * This function must be called before any event processing and after | |
1152 | * any SetRequest to the Resolution Multiplier. | |
1153 | */ | |
1154 | void hid_setup_resolution_multiplier(struct hid_device *hid) | |
1155 | { | |
1156 | struct hid_report_enum *rep_enum; | |
1157 | struct hid_report *rep; | |
1158 | struct hid_usage *usage; | |
1159 | int i, j; | |
1160 | ||
1161 | rep_enum = &hid->report_enum[HID_FEATURE_REPORT]; | |
1162 | list_for_each_entry(rep, &rep_enum->report_list, list) { | |
1163 | for (i = 0; i < rep->maxfield; i++) { | |
1164 | /* Ignore if report count is out of bounds. */ | |
1165 | if (rep->field[i]->report_count < 1) | |
1166 | continue; | |
1167 | ||
1168 | for (j = 0; j < rep->field[i]->maxusage; j++) { | |
1169 | usage = &rep->field[i]->usage[j]; | |
1170 | if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER) | |
1171 | hid_apply_multiplier(hid, | |
1172 | rep->field[i]); | |
1173 | } | |
1174 | } | |
1175 | } | |
1176 | } | |
1177 | EXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier); | |
1178 | ||
a7197c2e HR |
1179 | /** |
1180 | * hid_open_report - open a driver-specific device report | |
1181 | * | |
1182 | * @device: hid device | |
1183 | * | |
dde5845a JK |
1184 | * Parse a report description into a hid_device structure. Reports are |
1185 | * enumerated, fields are attached to these reports. | |
85cdaf52 | 1186 | * 0 returned on success, otherwise nonzero error value. |
a7197c2e HR |
1187 | * |
1188 | * This function (or the equivalent hid_parse() macro) should only be | |
1189 | * called from probe() in drivers, before starting the device. | |
dde5845a | 1190 | */ |
a7197c2e | 1191 | int hid_open_report(struct hid_device *device) |
dde5845a | 1192 | { |
dde5845a JK |
1193 | struct hid_parser *parser; |
1194 | struct hid_item item; | |
a7197c2e HR |
1195 | unsigned int size; |
1196 | __u8 *start; | |
86e6b77e | 1197 | __u8 *buf; |
dde5845a | 1198 | __u8 *end; |
b3a81c77 | 1199 | __u8 *next; |
85cdaf52 | 1200 | int ret; |
dde5845a JK |
1201 | static int (*dispatch_type[])(struct hid_parser *parser, |
1202 | struct hid_item *item) = { | |
1203 | hid_parser_main, | |
1204 | hid_parser_global, | |
1205 | hid_parser_local, | |
1206 | hid_parser_reserved | |
1207 | }; | |
1208 | ||
a7197c2e HR |
1209 | if (WARN_ON(device->status & HID_STAT_PARSED)) |
1210 | return -EBUSY; | |
1211 | ||
1212 | start = device->dev_rdesc; | |
1213 | if (WARN_ON(!start)) | |
1214 | return -ENODEV; | |
1215 | size = device->dev_rsize; | |
1216 | ||
86e6b77e KD |
1217 | buf = kmemdup(start, size, GFP_KERNEL); |
1218 | if (buf == NULL) | |
1219 | return -ENOMEM; | |
1220 | ||
c500c971 | 1221 | if (device->driver->report_fixup) |
86e6b77e KD |
1222 | start = device->driver->report_fixup(device, buf, &size); |
1223 | else | |
1224 | start = buf; | |
c500c971 | 1225 | |
86e6b77e KD |
1226 | start = kmemdup(start, size, GFP_KERNEL); |
1227 | kfree(buf); | |
1228 | if (start == NULL) | |
85cdaf52 | 1229 | return -ENOMEM; |
86e6b77e KD |
1230 | |
1231 | device->rdesc = start; | |
dde5845a JK |
1232 | device->rsize = size; |
1233 | ||
fe258020 | 1234 | parser = vzalloc(sizeof(struct hid_parser)); |
85cdaf52 JS |
1235 | if (!parser) { |
1236 | ret = -ENOMEM; | |
b034ed50 | 1237 | goto alloc_err; |
dde5845a | 1238 | } |
85cdaf52 | 1239 | |
dde5845a JK |
1240 | parser->device = device; |
1241 | ||
1242 | end = start + size; | |
a7197c2e HR |
1243 | |
1244 | device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS, | |
1245 | sizeof(struct hid_collection), GFP_KERNEL); | |
1246 | if (!device->collection) { | |
1247 | ret = -ENOMEM; | |
1248 | goto err; | |
1249 | } | |
1250 | device->collection_size = HID_DEFAULT_NUM_COLLECTIONS; | |
1251 | ||
85cdaf52 | 1252 | ret = -EINVAL; |
b3a81c77 MM |
1253 | while ((next = fetch_item(start, end, &item)) != NULL) { |
1254 | start = next; | |
dde5845a JK |
1255 | |
1256 | if (item.format != HID_ITEM_FORMAT_SHORT) { | |
8c3d52fc | 1257 | hid_err(device, "unexpected long global item\n"); |
85cdaf52 | 1258 | goto err; |
dde5845a JK |
1259 | } |
1260 | ||
1261 | if (dispatch_type[item.type](parser, &item)) { | |
8c3d52fc | 1262 | hid_err(device, "item %u %u %u %u parsing failed\n", |
4291ee30 JP |
1263 | item.format, (unsigned)item.size, |
1264 | (unsigned)item.type, (unsigned)item.tag); | |
85cdaf52 | 1265 | goto err; |
dde5845a JK |
1266 | } |
1267 | ||
1268 | if (start == end) { | |
1269 | if (parser->collection_stack_ptr) { | |
8c3d52fc | 1270 | hid_err(device, "unbalanced collection at end of report description\n"); |
85cdaf52 | 1271 | goto err; |
dde5845a JK |
1272 | } |
1273 | if (parser->local.delimiter_depth) { | |
8c3d52fc | 1274 | hid_err(device, "unbalanced delimiter at end of report description\n"); |
85cdaf52 | 1275 | goto err; |
dde5845a | 1276 | } |
5a4abb36 PH |
1277 | |
1278 | /* | |
1279 | * fetch initial values in case the device's | |
1280 | * default multiplier isn't the recommended 1 | |
1281 | */ | |
1282 | hid_setup_resolution_multiplier(device); | |
1283 | ||
b2dd9f2e | 1284 | kfree(parser->collection_stack); |
47a80edb | 1285 | vfree(parser); |
a7197c2e | 1286 | device->status |= HID_STAT_PARSED; |
5a4abb36 | 1287 | |
85cdaf52 | 1288 | return 0; |
dde5845a JK |
1289 | } |
1290 | } | |
1291 | ||
b3a81c77 MM |
1292 | hid_err(device, "item fetching failed at offset %u/%u\n", |
1293 | size - (unsigned int)(end - start), size); | |
85cdaf52 | 1294 | err: |
b2dd9f2e | 1295 | kfree(parser->collection_stack); |
b034ed50 | 1296 | alloc_err: |
47a80edb | 1297 | vfree(parser); |
a7197c2e | 1298 | hid_close_report(device); |
85cdaf52 | 1299 | return ret; |
dde5845a | 1300 | } |
a7197c2e | 1301 | EXPORT_SYMBOL_GPL(hid_open_report); |
dde5845a JK |
1302 | |
1303 | /* | |
1304 | * Convert a signed n-bit integer to signed 32-bit integer. Common | |
1305 | * cases are done through the compiler, the screwed things has to be | |
1306 | * done by hand. | |
1307 | */ | |
1308 | ||
1309 | static s32 snto32(__u32 value, unsigned n) | |
1310 | { | |
a0312af1 RD |
1311 | if (!value || !n) |
1312 | return 0; | |
1313 | ||
dde5845a | 1314 | switch (n) { |
880d29f1 JS |
1315 | case 8: return ((__s8)value); |
1316 | case 16: return ((__s16)value); | |
1317 | case 32: return ((__s32)value); | |
dde5845a | 1318 | } |
08585e43 | 1319 | return value & (1 << (n - 1)) ? value | (~0U << n) : value; |
dde5845a JK |
1320 | } |
1321 | ||
77463838 BT |
1322 | s32 hid_snto32(__u32 value, unsigned n) |
1323 | { | |
1324 | return snto32(value, n); | |
1325 | } | |
1326 | EXPORT_SYMBOL_GPL(hid_snto32); | |
1327 | ||
dde5845a JK |
1328 | /* |
1329 | * Convert a signed 32-bit integer to a signed n-bit integer. | |
1330 | */ | |
1331 | ||
1332 | static u32 s32ton(__s32 value, unsigned n) | |
1333 | { | |
1334 | s32 a = value >> (n - 1); | |
1335 | if (a && a != -1) | |
1336 | return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1; | |
1337 | return value & ((1 << n) - 1); | |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * Extract/implement a data field from/to a little endian report (bit array). | |
1342 | * | |
1343 | * Code sort-of follows HID spec: | |
5137b354 | 1344 | * http://www.usb.org/developers/hidpage/HID1_11.pdf |
dde5845a JK |
1345 | * |
1346 | * While the USB HID spec allows unlimited length bit fields in "report | |
1347 | * descriptors", most devices never use more than 16 bits. | |
1348 | * One model of UPS is claimed to report "LINEV" as a 32-bit field. | |
1349 | * Search linux-kernel and linux-usb-devel archives for "hid-core extract". | |
1350 | */ | |
1351 | ||
5137b354 | 1352 | static u32 __extract(u8 *report, unsigned offset, int n) |
dde5845a | 1353 | { |
5137b354 DT |
1354 | unsigned int idx = offset / 8; |
1355 | unsigned int bit_nr = 0; | |
1356 | unsigned int bit_shift = offset % 8; | |
1357 | int bits_to_copy = 8 - bit_shift; | |
1358 | u32 value = 0; | |
1359 | u32 mask = n < 32 ? (1U << n) - 1 : ~0U; | |
1360 | ||
1361 | while (n > 0) { | |
1362 | value |= ((u32)report[idx] >> bit_shift) << bit_nr; | |
1363 | n -= bits_to_copy; | |
1364 | bit_nr += bits_to_copy; | |
1365 | bits_to_copy = 8; | |
1366 | bit_shift = 0; | |
1367 | idx++; | |
1368 | } | |
1369 | ||
1370 | return value & mask; | |
1371 | } | |
dde5845a | 1372 | |
5137b354 DT |
1373 | u32 hid_field_extract(const struct hid_device *hid, u8 *report, |
1374 | unsigned offset, unsigned n) | |
1375 | { | |
15fc1b5c | 1376 | if (n > 32) { |
0af10eed JC |
1377 | hid_warn_once(hid, "%s() called with n (%d) > 32! (%s)\n", |
1378 | __func__, n, current->comm); | |
15fc1b5c | 1379 | n = 32; |
5137b354 | 1380 | } |
dde5845a | 1381 | |
5137b354 | 1382 | return __extract(report, offset, n); |
dde5845a | 1383 | } |
04fba786 | 1384 | EXPORT_SYMBOL_GPL(hid_field_extract); |
dde5845a JK |
1385 | |
1386 | /* | |
1387 | * "implement" : set bits in a little endian bit stream. | |
1388 | * Same concepts as "extract" (see comments above). | |
1389 | * The data mangled in the bit stream remains in little endian | |
1390 | * order the whole time. It make more sense to talk about | |
1391 | * endianness of register values by considering a register | |
5137b354 | 1392 | * a "cached" copy of the little endian bit stream. |
dde5845a | 1393 | */ |
5137b354 DT |
1394 | |
1395 | static void __implement(u8 *report, unsigned offset, int n, u32 value) | |
1396 | { | |
1397 | unsigned int idx = offset / 8; | |
5137b354 DT |
1398 | unsigned int bit_shift = offset % 8; |
1399 | int bits_to_set = 8 - bit_shift; | |
5137b354 DT |
1400 | |
1401 | while (n - bits_to_set >= 0) { | |
95d1c895 | 1402 | report[idx] &= ~(0xff << bit_shift); |
5137b354 DT |
1403 | report[idx] |= value << bit_shift; |
1404 | value >>= bits_to_set; | |
1405 | n -= bits_to_set; | |
1406 | bits_to_set = 8; | |
5137b354 DT |
1407 | bit_shift = 0; |
1408 | idx++; | |
1409 | } | |
1410 | ||
1411 | /* last nibble */ | |
1412 | if (n) { | |
95d1c895 DT |
1413 | u8 bit_mask = ((1U << n) - 1); |
1414 | report[idx] &= ~(bit_mask << bit_shift); | |
1415 | report[idx] |= value << bit_shift; | |
5137b354 DT |
1416 | } |
1417 | } | |
1418 | ||
1419 | static void implement(const struct hid_device *hid, u8 *report, | |
1420 | unsigned offset, unsigned n, u32 value) | |
dde5845a | 1421 | { |
95d1c895 | 1422 | if (unlikely(n > 32)) { |
4291ee30 JP |
1423 | hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n", |
1424 | __func__, n, current->comm); | |
5137b354 | 1425 | n = 32; |
95d1c895 DT |
1426 | } else if (n < 32) { |
1427 | u32 m = (1U << n) - 1; | |
1428 | ||
1429 | if (unlikely(value > m)) { | |
1430 | hid_warn(hid, | |
1431 | "%s() called with too large value %d (n: %d)! (%s)\n", | |
1432 | __func__, value, n, current->comm); | |
1433 | WARN_ON(1); | |
1434 | value &= m; | |
1435 | } | |
5137b354 | 1436 | } |
dde5845a | 1437 | |
5137b354 | 1438 | __implement(report, offset, n, value); |
dde5845a JK |
1439 | } |
1440 | ||
1441 | /* | |
1442 | * Search an array for a value. | |
1443 | */ | |
1444 | ||
16ee4cc8 | 1445 | static int search(__s32 *array, __s32 value, unsigned n) |
dde5845a JK |
1446 | { |
1447 | while (n--) { | |
1448 | if (*array++ == value) | |
1449 | return 0; | |
1450 | } | |
1451 | return -1; | |
1452 | } | |
1453 | ||
85cdaf52 JS |
1454 | /** |
1455 | * hid_match_report - check if driver's raw_event should be called | |
1456 | * | |
1457 | * @hid: hid device | |
5f94e9c8 | 1458 | * @report: hid report to match against |
85cdaf52 JS |
1459 | * |
1460 | * compare hid->driver->report_table->report_type to report->type | |
1461 | */ | |
1462 | static int hid_match_report(struct hid_device *hid, struct hid_report *report) | |
dde5845a | 1463 | { |
85cdaf52 JS |
1464 | const struct hid_report_id *id = hid->driver->report_table; |
1465 | ||
1466 | if (!id) /* NULL means all */ | |
1467 | return 1; | |
1468 | ||
1469 | for (; id->report_type != HID_TERMINATOR; id++) | |
1470 | if (id->report_type == HID_ANY_ID || | |
1471 | id->report_type == report->type) | |
1472 | return 1; | |
1473 | return 0; | |
1474 | } | |
1475 | ||
1476 | /** | |
1477 | * hid_match_usage - check if driver's event should be called | |
1478 | * | |
1479 | * @hid: hid device | |
1480 | * @usage: usage to match against | |
1481 | * | |
1482 | * compare hid->driver->usage_table->usage_{type,code} to | |
1483 | * usage->usage_{type,code} | |
1484 | */ | |
1485 | static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage) | |
1486 | { | |
1487 | const struct hid_usage_id *id = hid->driver->usage_table; | |
1488 | ||
1489 | if (!id) /* NULL means all */ | |
1490 | return 1; | |
1491 | ||
1492 | for (; id->usage_type != HID_ANY_ID - 1; id++) | |
1493 | if ((id->usage_hid == HID_ANY_ID || | |
1494 | id->usage_hid == usage->hid) && | |
1495 | (id->usage_type == HID_ANY_ID || | |
1496 | id->usage_type == usage->type) && | |
1497 | (id->usage_code == HID_ANY_ID || | |
1498 | id->usage_code == usage->code)) | |
1499 | return 1; | |
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | static void hid_process_event(struct hid_device *hid, struct hid_field *field, | |
1504 | struct hid_usage *usage, __s32 value, int interrupt) | |
1505 | { | |
1506 | struct hid_driver *hdrv = hid->driver; | |
1507 | int ret; | |
1508 | ||
9bfc8da0 HR |
1509 | if (!list_empty(&hid->debug_list)) |
1510 | hid_dump_input(hid, usage, value); | |
85cdaf52 JS |
1511 | |
1512 | if (hdrv && hdrv->event && hid_match_usage(hid, usage)) { | |
1513 | ret = hdrv->event(hid, field, usage, value); | |
1514 | if (ret != 0) { | |
1515 | if (ret < 0) | |
8c3d52fc | 1516 | hid_err(hid, "%s's event failed with %d\n", |
85cdaf52 JS |
1517 | hdrv->name, ret); |
1518 | return; | |
1519 | } | |
1520 | } | |
1521 | ||
dde5845a JK |
1522 | if (hid->claimed & HID_CLAIMED_INPUT) |
1523 | hidinput_hid_event(hid, field, usage, value); | |
aa938f79 JK |
1524 | if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event) |
1525 | hid->hiddev_hid_event(hid, field, usage, value); | |
dde5845a JK |
1526 | } |
1527 | ||
74acc277 BT |
1528 | /* |
1529 | * Checks if the given value is valid within this field | |
1530 | */ | |
1531 | static inline int hid_array_value_is_valid(struct hid_field *field, | |
1532 | __s32 value) | |
1533 | { | |
1534 | __s32 min = field->logical_minimum; | |
1535 | ||
1536 | /* | |
1537 | * Value needs to be between logical min and max, and | |
1538 | * (value - min) is used as an index in the usage array. | |
1539 | * This array is of size field->maxusage | |
1540 | */ | |
1541 | return value >= min && | |
1542 | value <= field->logical_maximum && | |
1543 | value - min < field->maxusage; | |
1544 | } | |
1545 | ||
dde5845a | 1546 | /* |
b79c1aba BT |
1547 | * Fetch the field from the data. The field content is stored for next |
1548 | * report processing (we do differential reporting to the layer). | |
dde5845a | 1549 | */ |
b79c1aba BT |
1550 | static void hid_input_fetch_field(struct hid_device *hid, |
1551 | struct hid_field *field, | |
1552 | __u8 *data) | |
dde5845a JK |
1553 | { |
1554 | unsigned n; | |
1555 | unsigned count = field->report_count; | |
1556 | unsigned offset = field->report_offset; | |
1557 | unsigned size = field->report_size; | |
1558 | __s32 min = field->logical_minimum; | |
dde5845a JK |
1559 | __s32 *value; |
1560 | ||
1c1813a7 BT |
1561 | value = field->new_value; |
1562 | memset(value, 0, count * sizeof(__s32)); | |
b79c1aba | 1563 | field->ignored = false; |
dde5845a JK |
1564 | |
1565 | for (n = 0; n < count; n++) { | |
1566 | ||
4291ee30 | 1567 | value[n] = min < 0 ? |
04fba786 GB |
1568 | snto32(hid_field_extract(hid, data, offset + n * size, |
1569 | size), size) : | |
1570 | hid_field_extract(hid, data, offset + n * size, size); | |
dde5845a | 1571 | |
4291ee30 JP |
1572 | /* Ignore report if ErrorRollOver */ |
1573 | if (!(field->flags & HID_MAIN_ITEM_VARIABLE) && | |
74acc277 | 1574 | hid_array_value_is_valid(field, value[n]) && |
b79c1aba BT |
1575 | field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) { |
1576 | field->ignored = true; | |
1c1813a7 | 1577 | return; |
b79c1aba | 1578 | } |
dde5845a | 1579 | } |
b79c1aba | 1580 | } |
dde5845a | 1581 | |
b79c1aba BT |
1582 | /* |
1583 | * Process a received variable field. | |
1584 | */ | |
dde5845a | 1585 | |
b79c1aba BT |
1586 | static void hid_input_var_field(struct hid_device *hid, |
1587 | struct hid_field *field, | |
1588 | int interrupt) | |
1589 | { | |
1590 | unsigned int count = field->report_count; | |
1591 | __s32 *value = field->new_value; | |
1592 | unsigned int n; | |
1593 | ||
1594 | for (n = 0; n < count; n++) | |
1595 | hid_process_event(hid, | |
1596 | field, | |
1597 | &field->usage[n], | |
1598 | value[n], | |
1599 | interrupt); | |
1600 | ||
1601 | memcpy(field->value, value, count * sizeof(__s32)); | |
1602 | } | |
dde5845a | 1603 | |
b79c1aba BT |
1604 | /* |
1605 | * Process a received array field. The field content is stored for | |
1606 | * next report processing (we do differential reporting to the layer). | |
1607 | */ | |
1608 | ||
1609 | static void hid_input_array_field(struct hid_device *hid, | |
1610 | struct hid_field *field, | |
1611 | int interrupt) | |
1612 | { | |
1613 | unsigned int n; | |
1614 | unsigned int count = field->report_count; | |
1615 | __s32 min = field->logical_minimum; | |
1616 | __s32 *value; | |
1617 | ||
1618 | value = field->new_value; | |
1619 | ||
1620 | /* ErrorRollOver */ | |
1621 | if (field->ignored) | |
1622 | return; | |
1623 | ||
1624 | for (n = 0; n < count; n++) { | |
74acc277 BT |
1625 | if (hid_array_value_is_valid(field, field->value[n]) && |
1626 | search(value, field->value[n], count)) | |
1627 | hid_process_event(hid, | |
1628 | field, | |
1629 | &field->usage[field->value[n] - min], | |
1630 | 0, | |
1631 | interrupt); | |
1632 | ||
1633 | if (hid_array_value_is_valid(field, value[n]) && | |
1634 | search(field->value, value[n], count)) | |
1635 | hid_process_event(hid, | |
1636 | field, | |
1637 | &field->usage[value[n] - min], | |
1638 | 1, | |
1639 | interrupt); | |
dde5845a JK |
1640 | } |
1641 | ||
1642 | memcpy(field->value, value, count * sizeof(__s32)); | |
dde5845a | 1643 | } |
dde5845a | 1644 | |
b79c1aba BT |
1645 | /* |
1646 | * Analyse a received report, and fetch the data from it. The field | |
1647 | * content is stored for next report processing (we do differential | |
1648 | * reporting to the layer). | |
1649 | */ | |
1650 | static void hid_process_report(struct hid_device *hid, | |
1651 | struct hid_report *report, | |
1652 | __u8 *data, | |
1653 | int interrupt) | |
1654 | { | |
1655 | unsigned int a; | |
1656 | struct hid_field *field; | |
1657 | ||
1658 | for (a = 0; a < report->maxfield; a++) { | |
1659 | field = report->field[a]; | |
1660 | ||
1661 | hid_input_fetch_field(hid, field, data); | |
1662 | ||
1663 | if (field->flags & HID_MAIN_ITEM_VARIABLE) | |
1664 | hid_input_var_field(hid, field, interrupt); | |
1665 | else | |
1666 | hid_input_array_field(hid, field, interrupt); | |
1667 | } | |
1668 | } | |
1669 | ||
dde5845a JK |
1670 | /* |
1671 | * Output the field into the report. | |
1672 | */ | |
1673 | ||
4291ee30 JP |
1674 | static void hid_output_field(const struct hid_device *hid, |
1675 | struct hid_field *field, __u8 *data) | |
dde5845a JK |
1676 | { |
1677 | unsigned count = field->report_count; | |
1678 | unsigned offset = field->report_offset; | |
1679 | unsigned size = field->report_size; | |
1680 | unsigned n; | |
1681 | ||
1682 | for (n = 0; n < count; n++) { | |
1683 | if (field->logical_minimum < 0) /* signed values */ | |
4291ee30 JP |
1684 | implement(hid, data, offset + n * size, size, |
1685 | s32ton(field->value[n], size)); | |
dde5845a | 1686 | else /* unsigned values */ |
4291ee30 JP |
1687 | implement(hid, data, offset + n * size, size, |
1688 | field->value[n]); | |
dde5845a JK |
1689 | } |
1690 | } | |
1691 | ||
bce1305c MZ |
1692 | /* |
1693 | * Compute the size of a report. | |
1694 | */ | |
1695 | static size_t hid_compute_report_size(struct hid_report *report) | |
1696 | { | |
1697 | if (report->size) | |
1698 | return ((report->size - 1) >> 3) + 1; | |
1699 | ||
1700 | return 0; | |
1701 | } | |
1702 | ||
dde5845a | 1703 | /* |
27ce4050 JK |
1704 | * Create a report. 'data' has to be allocated using |
1705 | * hid_alloc_report_buf() so that it has proper size. | |
dde5845a JK |
1706 | */ |
1707 | ||
229695e5 | 1708 | void hid_output_report(struct hid_report *report, __u8 *data) |
dde5845a JK |
1709 | { |
1710 | unsigned n; | |
1711 | ||
1712 | if (report->id > 0) | |
1713 | *data++ = report->id; | |
1714 | ||
bce1305c | 1715 | memset(data, 0, hid_compute_report_size(report)); |
dde5845a | 1716 | for (n = 0; n < report->maxfield; n++) |
4291ee30 | 1717 | hid_output_field(report->device, report->field[n], data); |
dde5845a | 1718 | } |
229695e5 | 1719 | EXPORT_SYMBOL_GPL(hid_output_report); |
dde5845a | 1720 | |
27ce4050 JK |
1721 | /* |
1722 | * Allocator for buffer that is going to be passed to hid_output_report() | |
1723 | */ | |
1724 | u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags) | |
1725 | { | |
1726 | /* | |
1727 | * 7 extra bytes are necessary to achieve proper functionality | |
1728 | * of implement() working on 8 byte chunks | |
1729 | */ | |
1730 | ||
6de0b13c | 1731 | u32 len = hid_report_len(report) + 7; |
27ce4050 JK |
1732 | |
1733 | return kmalloc(len, flags); | |
1734 | } | |
1735 | EXPORT_SYMBOL_GPL(hid_alloc_report_buf); | |
1736 | ||
dde5845a JK |
1737 | /* |
1738 | * Set a field value. The report this field belongs to has to be | |
1739 | * created and transferred to the device, to set this value in the | |
1740 | * device. | |
1741 | */ | |
1742 | ||
1743 | int hid_set_field(struct hid_field *field, unsigned offset, __s32 value) | |
1744 | { | |
be67b68d KC |
1745 | unsigned size; |
1746 | ||
1747 | if (!field) | |
1748 | return -1; | |
1749 | ||
1750 | size = field->report_size; | |
dde5845a | 1751 | |
cd667ce2 | 1752 | hid_dump_input(field->report->device, field->usage + offset, value); |
dde5845a JK |
1753 | |
1754 | if (offset >= field->report_count) { | |
8c3d52fc JK |
1755 | hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n", |
1756 | offset, field->report_count); | |
dde5845a JK |
1757 | return -1; |
1758 | } | |
1759 | if (field->logical_minimum < 0) { | |
1760 | if (value != snto32(s32ton(value, size), size)) { | |
8c3d52fc | 1761 | hid_err(field->report->device, "value %d is out of range\n", value); |
dde5845a JK |
1762 | return -1; |
1763 | } | |
1764 | } | |
1765 | field->value[offset] = value; | |
1766 | return 0; | |
1767 | } | |
229695e5 | 1768 | EXPORT_SYMBOL_GPL(hid_set_field); |
dde5845a | 1769 | |
85cdaf52 JS |
1770 | static struct hid_report *hid_get_report(struct hid_report_enum *report_enum, |
1771 | const u8 *data) | |
aa8de2f0 | 1772 | { |
aa8de2f0 | 1773 | struct hid_report *report; |
85cdaf52 | 1774 | unsigned int n = 0; /* Normally report number is 0 */ |
aa8de2f0 | 1775 | |
85cdaf52 JS |
1776 | /* Device uses numbered reports, data[0] is report number */ |
1777 | if (report_enum->numbered) | |
1778 | n = *data; | |
aa8de2f0 | 1779 | |
85cdaf52 JS |
1780 | report = report_enum->report_id_hash[n]; |
1781 | if (report == NULL) | |
1782 | dbg_hid("undefined report_id %u received\n", n); | |
aa8de2f0 | 1783 | |
85cdaf52 JS |
1784 | return report; |
1785 | } | |
aa8de2f0 | 1786 | |
4fa5a7f7 BT |
1787 | /* |
1788 | * Implement a generic .request() callback, using .raw_request() | |
1789 | * DO NOT USE in hid drivers directly, but through hid_hw_request instead. | |
1790 | */ | |
d43c17ea | 1791 | int __hid_request(struct hid_device *hid, struct hid_report *report, |
4fa5a7f7 BT |
1792 | int reqtype) |
1793 | { | |
1794 | char *buf; | |
1795 | int ret; | |
6de0b13c | 1796 | u32 len; |
4fa5a7f7 | 1797 | |
4fa5a7f7 BT |
1798 | buf = hid_alloc_report_buf(report, GFP_KERNEL); |
1799 | if (!buf) | |
d43c17ea | 1800 | return -ENOMEM; |
4fa5a7f7 BT |
1801 | |
1802 | len = hid_report_len(report); | |
1803 | ||
1804 | if (reqtype == HID_REQ_SET_REPORT) | |
1805 | hid_output_report(report, buf); | |
1806 | ||
1807 | ret = hid->ll_driver->raw_request(hid, report->id, buf, len, | |
1808 | report->type, reqtype); | |
1809 | if (ret < 0) { | |
1810 | dbg_hid("unable to complete request: %d\n", ret); | |
1811 | goto out; | |
1812 | } | |
1813 | ||
1814 | if (reqtype == HID_REQ_GET_REPORT) | |
1815 | hid_input_report(hid, report->type, buf, ret, 0); | |
1816 | ||
d43c17ea BT |
1817 | ret = 0; |
1818 | ||
4fa5a7f7 BT |
1819 | out: |
1820 | kfree(buf); | |
d43c17ea | 1821 | return ret; |
4fa5a7f7 BT |
1822 | } |
1823 | EXPORT_SYMBOL_GPL(__hid_request); | |
1824 | ||
6de0b13c | 1825 | int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, |
85cdaf52 JS |
1826 | int interrupt) |
1827 | { | |
1828 | struct hid_report_enum *report_enum = hid->report_enum + type; | |
1829 | struct hid_report *report; | |
6d85d037 | 1830 | struct hid_driver *hdrv; |
6de0b13c | 1831 | u32 rsize, csize = size; |
85cdaf52 | 1832 | u8 *cdata = data; |
b6787242 | 1833 | int ret = 0; |
aa8de2f0 | 1834 | |
85cdaf52 JS |
1835 | report = hid_get_report(report_enum, data); |
1836 | if (!report) | |
b6787242 | 1837 | goto out; |
aa8de2f0 | 1838 | |
85cdaf52 JS |
1839 | if (report_enum->numbered) { |
1840 | cdata++; | |
1841 | csize--; | |
aa8de2f0 JK |
1842 | } |
1843 | ||
bce1305c | 1844 | rsize = hid_compute_report_size(report); |
aa8de2f0 | 1845 | |
5ebdffd2 JK |
1846 | if (report_enum->numbered && rsize >= HID_MAX_BUFFER_SIZE) |
1847 | rsize = HID_MAX_BUFFER_SIZE - 1; | |
1848 | else if (rsize > HID_MAX_BUFFER_SIZE) | |
966922f2 AV |
1849 | rsize = HID_MAX_BUFFER_SIZE; |
1850 | ||
85cdaf52 JS |
1851 | if (csize < rsize) { |
1852 | dbg_hid("report %d is too short, (%d < %d)\n", report->id, | |
1853 | csize, rsize); | |
1854 | memset(cdata + csize, 0, rsize - csize); | |
aa8de2f0 JK |
1855 | } |
1856 | ||
1857 | if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event) | |
1858 | hid->hiddev_report_event(hid, report); | |
b6787242 JK |
1859 | if (hid->claimed & HID_CLAIMED_HIDRAW) { |
1860 | ret = hidraw_report_event(hid, data, size); | |
1861 | if (ret) | |
1862 | goto out; | |
1863 | } | |
aa8de2f0 | 1864 | |
cc6b54aa | 1865 | if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) { |
b79c1aba | 1866 | hid_process_report(hid, report, cdata, interrupt); |
6d85d037 BT |
1867 | hdrv = hid->driver; |
1868 | if (hdrv && hdrv->report) | |
1869 | hdrv->report(hid, report); | |
b94e3c94 | 1870 | } |
aa8de2f0 JK |
1871 | |
1872 | if (hid->claimed & HID_CLAIMED_INPUT) | |
1873 | hidinput_report_event(hid, report); | |
b6787242 JK |
1874 | out: |
1875 | return ret; | |
85cdaf52 JS |
1876 | } |
1877 | EXPORT_SYMBOL_GPL(hid_report_raw_event); | |
1878 | ||
1879 | /** | |
1880 | * hid_input_report - report data from lower layer (usb, bt...) | |
1881 | * | |
1882 | * @hid: hid device | |
1883 | * @type: HID report type (HID_*_REPORT) | |
1884 | * @data: report contents | |
1885 | * @size: size of data parameter | |
ff9b00a2 | 1886 | * @interrupt: distinguish between interrupt and control transfers |
85cdaf52 JS |
1887 | * |
1888 | * This is data entry for lower layers. | |
1889 | */ | |
6de0b13c | 1890 | int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt) |
85cdaf52 | 1891 | { |
76c317d6 JL |
1892 | struct hid_report_enum *report_enum; |
1893 | struct hid_driver *hdrv; | |
85cdaf52 | 1894 | struct hid_report *report; |
45dc1ac7 | 1895 | int ret = 0; |
85cdaf52 | 1896 | |
4ea54542 | 1897 | if (!hid) |
85cdaf52 | 1898 | return -ENODEV; |
4ea54542 | 1899 | |
c849a614 | 1900 | if (down_trylock(&hid->driver_input_lock)) |
4ea54542 DH |
1901 | return -EBUSY; |
1902 | ||
1903 | if (!hid->driver) { | |
1904 | ret = -ENODEV; | |
1905 | goto unlock; | |
1906 | } | |
76c317d6 JL |
1907 | report_enum = hid->report_enum + type; |
1908 | hdrv = hid->driver; | |
85cdaf52 JS |
1909 | |
1910 | if (!size) { | |
1911 | dbg_hid("empty report\n"); | |
4ea54542 DH |
1912 | ret = -1; |
1913 | goto unlock; | |
85cdaf52 JS |
1914 | } |
1915 | ||
b94e3c94 | 1916 | /* Avoid unnecessary overhead if debugfs is disabled */ |
a5f04b9d BT |
1917 | if (!list_empty(&hid->debug_list)) |
1918 | hid_dump_report(hid, type, data, size); | |
85cdaf52 | 1919 | |
1caea61e JK |
1920 | report = hid_get_report(report_enum, data); |
1921 | ||
4ea54542 DH |
1922 | if (!report) { |
1923 | ret = -1; | |
1924 | goto unlock; | |
1925 | } | |
85cdaf52 JS |
1926 | |
1927 | if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) { | |
1928 | ret = hdrv->raw_event(hid, report, data, size); | |
556483e2 | 1929 | if (ret < 0) |
4ea54542 | 1930 | goto unlock; |
85cdaf52 JS |
1931 | } |
1932 | ||
b6787242 | 1933 | ret = hid_report_raw_event(hid, type, data, size, interrupt); |
aa8de2f0 | 1934 | |
4ea54542 | 1935 | unlock: |
c849a614 | 1936 | up(&hid->driver_input_lock); |
45dc1ac7 | 1937 | return ret; |
aa8de2f0 JK |
1938 | } |
1939 | EXPORT_SYMBOL_GPL(hid_input_report); | |
1940 | ||
d5d3e202 BT |
1941 | bool hid_match_one_id(const struct hid_device *hdev, |
1942 | const struct hid_device_id *id) | |
0f37cd03 | 1943 | { |
7431fb76 | 1944 | return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) && |
4d53b801 | 1945 | (id->group == HID_GROUP_ANY || id->group == hdev->group) && |
0f37cd03 JK |
1946 | (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) && |
1947 | (id->product == HID_ANY_ID || id->product == hdev->product); | |
1948 | } | |
1949 | ||
d5d3e202 | 1950 | const struct hid_device_id *hid_match_id(const struct hid_device *hdev, |
0f37cd03 JK |
1951 | const struct hid_device_id *id) |
1952 | { | |
1953 | for (; id->bus; id++) | |
1954 | if (hid_match_one_id(hdev, id)) | |
1955 | return id; | |
1956 | ||
1957 | return NULL; | |
1958 | } | |
1959 | ||
1960 | static const struct hid_device_id hid_hiddev_list[] = { | |
c0bd6a42 RH |
1961 | { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) }, |
1962 | { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) }, | |
0f37cd03 JK |
1963 | { } |
1964 | }; | |
1965 | ||
1966 | static bool hid_hiddev(struct hid_device *hdev) | |
1967 | { | |
1968 | return !!hid_match_id(hdev, hid_hiddev_list); | |
1969 | } | |
1970 | ||
6d3bfb74 AO |
1971 | |
1972 | static ssize_t | |
1973 | read_report_descriptor(struct file *filp, struct kobject *kobj, | |
1974 | struct bin_attribute *attr, | |
1975 | char *buf, loff_t off, size_t count) | |
1976 | { | |
2cf83833 | 1977 | struct device *dev = kobj_to_dev(kobj); |
ee79a8f8 | 1978 | struct hid_device *hdev = to_hid_device(dev); |
6d3bfb74 AO |
1979 | |
1980 | if (off >= hdev->rsize) | |
1981 | return 0; | |
1982 | ||
1983 | if (off + count > hdev->rsize) | |
1984 | count = hdev->rsize - off; | |
1985 | ||
1986 | memcpy(buf, hdev->rdesc + off, count); | |
1987 | ||
1988 | return count; | |
1989 | } | |
1990 | ||
a877417e OG |
1991 | static ssize_t |
1992 | show_country(struct device *dev, struct device_attribute *attr, | |
1993 | char *buf) | |
1994 | { | |
ee79a8f8 | 1995 | struct hid_device *hdev = to_hid_device(dev); |
a877417e OG |
1996 | |
1997 | return sprintf(buf, "%02x\n", hdev->country & 0xff); | |
1998 | } | |
1999 | ||
6d3bfb74 AO |
2000 | static struct bin_attribute dev_bin_attr_report_desc = { |
2001 | .attr = { .name = "report_descriptor", .mode = 0444 }, | |
2002 | .read = read_report_descriptor, | |
2003 | .size = HID_MAX_DESCRIPTOR_SIZE, | |
2004 | }; | |
2005 | ||
ad8378ed | 2006 | static const struct device_attribute dev_attr_country = { |
a877417e OG |
2007 | .attr = { .name = "country", .mode = 0444 }, |
2008 | .show = show_country, | |
2009 | }; | |
2010 | ||
93c10132 JS |
2011 | int hid_connect(struct hid_device *hdev, unsigned int connect_mask) |
2012 | { | |
2013 | static const char *types[] = { "Device", "Pointer", "Mouse", "Device", | |
2014 | "Joystick", "Gamepad", "Keyboard", "Keypad", | |
2015 | "Multi-Axis Controller" | |
2016 | }; | |
2017 | const char *type, *bus; | |
79b568b9 | 2018 | char buf[64] = ""; |
93c10132 JS |
2019 | unsigned int i; |
2020 | int len; | |
6d3bfb74 | 2021 | int ret; |
93c10132 | 2022 | |
b5e5a37e BN |
2023 | if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE) |
2024 | connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV); | |
3a343ee4 DM |
2025 | if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE) |
2026 | connect_mask |= HID_CONNECT_HIDINPUT_FORCE; | |
93c10132 JS |
2027 | if (hdev->bus != BUS_USB) |
2028 | connect_mask &= ~HID_CONNECT_HIDDEV; | |
0f37cd03 JK |
2029 | if (hid_hiddev(hdev)) |
2030 | connect_mask |= HID_CONNECT_HIDDEV_FORCE; | |
93c10132 JS |
2031 | |
2032 | if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev, | |
2033 | connect_mask & HID_CONNECT_HIDINPUT_FORCE)) | |
2034 | hdev->claimed |= HID_CLAIMED_INPUT; | |
b77c3920 | 2035 | |
93c10132 JS |
2036 | if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect && |
2037 | !hdev->hiddev_connect(hdev, | |
2038 | connect_mask & HID_CONNECT_HIDDEV_FORCE)) | |
2039 | hdev->claimed |= HID_CLAIMED_HIDDEV; | |
2040 | if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev)) | |
2041 | hdev->claimed |= HID_CLAIMED_HIDRAW; | |
2042 | ||
7704ac93 BT |
2043 | if (connect_mask & HID_CONNECT_DRIVER) |
2044 | hdev->claimed |= HID_CLAIMED_DRIVER; | |
2045 | ||
4bc19f62 DH |
2046 | /* Drivers with the ->raw_event callback set are not required to connect |
2047 | * to any other listener. */ | |
2048 | if (!hdev->claimed && !hdev->driver->raw_event) { | |
2049 | hid_err(hdev, "device has no listeners, quitting\n"); | |
93c10132 JS |
2050 | return -ENODEV; |
2051 | } | |
2052 | ||
2053 | if ((hdev->claimed & HID_CLAIMED_INPUT) && | |
2054 | (connect_mask & HID_CONNECT_FF) && hdev->ff_init) | |
2055 | hdev->ff_init(hdev); | |
2056 | ||
2057 | len = 0; | |
2058 | if (hdev->claimed & HID_CLAIMED_INPUT) | |
2059 | len += sprintf(buf + len, "input"); | |
2060 | if (hdev->claimed & HID_CLAIMED_HIDDEV) | |
2061 | len += sprintf(buf + len, "%shiddev%d", len ? "," : "", | |
733aca90 | 2062 | ((struct hiddev *)hdev->hiddev)->minor); |
93c10132 JS |
2063 | if (hdev->claimed & HID_CLAIMED_HIDRAW) |
2064 | len += sprintf(buf + len, "%shidraw%d", len ? "," : "", | |
2065 | ((struct hidraw *)hdev->hidraw)->minor); | |
2066 | ||
2067 | type = "Device"; | |
2068 | for (i = 0; i < hdev->maxcollection; i++) { | |
2069 | struct hid_collection *col = &hdev->collection[i]; | |
2070 | if (col->type == HID_COLLECTION_APPLICATION && | |
2071 | (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK && | |
2072 | (col->usage & 0xffff) < ARRAY_SIZE(types)) { | |
2073 | type = types[col->usage & 0xffff]; | |
2074 | break; | |
2075 | } | |
2076 | } | |
2077 | ||
2078 | switch (hdev->bus) { | |
2079 | case BUS_USB: | |
2080 | bus = "USB"; | |
2081 | break; | |
2082 | case BUS_BLUETOOTH: | |
2083 | bus = "BLUETOOTH"; | |
2084 | break; | |
06780727 DM |
2085 | case BUS_I2C: |
2086 | bus = "I2C"; | |
2087 | break; | |
48e33bef MB |
2088 | case BUS_VIRTUAL: |
2089 | bus = "VIRTUAL"; | |
2090 | break; | |
93c10132 JS |
2091 | default: |
2092 | bus = "<UNKNOWN>"; | |
2093 | } | |
2094 | ||
a877417e OG |
2095 | ret = device_create_file(&hdev->dev, &dev_attr_country); |
2096 | if (ret) | |
2097 | hid_warn(hdev, | |
2098 | "can't create sysfs country code attribute err: %d\n", ret); | |
2099 | ||
4291ee30 JP |
2100 | hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n", |
2101 | buf, bus, hdev->version >> 8, hdev->version & 0xff, | |
2102 | type, hdev->name, hdev->phys); | |
93c10132 JS |
2103 | |
2104 | return 0; | |
2105 | } | |
2106 | EXPORT_SYMBOL_GPL(hid_connect); | |
2107 | ||
c4c259bc JK |
2108 | void hid_disconnect(struct hid_device *hdev) |
2109 | { | |
a877417e | 2110 | device_remove_file(&hdev->dev, &dev_attr_country); |
c4c259bc JK |
2111 | if (hdev->claimed & HID_CLAIMED_INPUT) |
2112 | hidinput_disconnect(hdev); | |
2113 | if (hdev->claimed & HID_CLAIMED_HIDDEV) | |
2114 | hdev->hiddev_disconnect(hdev); | |
2115 | if (hdev->claimed & HID_CLAIMED_HIDRAW) | |
2116 | hidraw_disconnect(hdev); | |
9c5c6ed7 | 2117 | hdev->claimed = 0; |
c4c259bc JK |
2118 | } |
2119 | EXPORT_SYMBOL_GPL(hid_disconnect); | |
2120 | ||
aaac082d DT |
2121 | /** |
2122 | * hid_hw_start - start underlying HW | |
2123 | * @hdev: hid device | |
2124 | * @connect_mask: which outputs to connect, see HID_CONNECT_* | |
2125 | * | |
2126 | * Call this in probe function *after* hid_parse. This will setup HW | |
2127 | * buffers and start the device (if not defeirred to device open). | |
2128 | * hid_hw_stop must be called if this was successful. | |
2129 | */ | |
2130 | int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask) | |
2131 | { | |
2132 | int error; | |
2133 | ||
2134 | error = hdev->ll_driver->start(hdev); | |
2135 | if (error) | |
2136 | return error; | |
2137 | ||
2138 | if (connect_mask) { | |
2139 | error = hid_connect(hdev, connect_mask); | |
2140 | if (error) { | |
2141 | hdev->ll_driver->stop(hdev); | |
2142 | return error; | |
2143 | } | |
2144 | } | |
2145 | ||
2146 | return 0; | |
2147 | } | |
2148 | EXPORT_SYMBOL_GPL(hid_hw_start); | |
2149 | ||
2150 | /** | |
2151 | * hid_hw_stop - stop underlying HW | |
2152 | * @hdev: hid device | |
2153 | * | |
2154 | * This is usually called from remove function or from probe when something | |
2155 | * failed and hid_hw_start was called already. | |
2156 | */ | |
2157 | void hid_hw_stop(struct hid_device *hdev) | |
2158 | { | |
2159 | hid_disconnect(hdev); | |
2160 | hdev->ll_driver->stop(hdev); | |
2161 | } | |
2162 | EXPORT_SYMBOL_GPL(hid_hw_stop); | |
2163 | ||
2164 | /** | |
2165 | * hid_hw_open - signal underlying HW to start delivering events | |
2166 | * @hdev: hid device | |
2167 | * | |
2168 | * Tell underlying HW to start delivering events from the device. | |
2169 | * This function should be called sometime after successful call | |
d6c70a86 | 2170 | * to hid_hw_start(). |
aaac082d DT |
2171 | */ |
2172 | int hid_hw_open(struct hid_device *hdev) | |
2173 | { | |
2174 | int ret; | |
2175 | ||
2176 | ret = mutex_lock_killable(&hdev->ll_open_lock); | |
2177 | if (ret) | |
2178 | return ret; | |
2179 | ||
2180 | if (!hdev->ll_open_count++) { | |
2181 | ret = hdev->ll_driver->open(hdev); | |
2182 | if (ret) | |
2183 | hdev->ll_open_count--; | |
2184 | } | |
2185 | ||
2186 | mutex_unlock(&hdev->ll_open_lock); | |
2187 | return ret; | |
2188 | } | |
2189 | EXPORT_SYMBOL_GPL(hid_hw_open); | |
2190 | ||
2191 | /** | |
2192 | * hid_hw_close - signal underlaying HW to stop delivering events | |
2193 | * | |
2194 | * @hdev: hid device | |
2195 | * | |
2196 | * This function indicates that we are not interested in the events | |
2197 | * from this device anymore. Delivery of events may or may not stop, | |
2198 | * depending on the number of users still outstanding. | |
2199 | */ | |
2200 | void hid_hw_close(struct hid_device *hdev) | |
2201 | { | |
2202 | mutex_lock(&hdev->ll_open_lock); | |
2203 | if (!--hdev->ll_open_count) | |
2204 | hdev->ll_driver->close(hdev); | |
2205 | mutex_unlock(&hdev->ll_open_lock); | |
2206 | } | |
2207 | EXPORT_SYMBOL_GPL(hid_hw_close); | |
2208 | ||
f65a0b1f BT |
2209 | /** |
2210 | * hid_hw_request - send report request to device | |
2211 | * | |
2212 | * @hdev: hid device | |
2213 | * @report: report to send | |
2214 | * @reqtype: hid request type | |
2215 | */ | |
2216 | void hid_hw_request(struct hid_device *hdev, | |
2217 | struct hid_report *report, int reqtype) | |
2218 | { | |
2219 | if (hdev->ll_driver->request) | |
2220 | return hdev->ll_driver->request(hdev, report, reqtype); | |
2221 | ||
2222 | __hid_request(hdev, report, reqtype); | |
2223 | } | |
2224 | EXPORT_SYMBOL_GPL(hid_hw_request); | |
2225 | ||
2226 | /** | |
2227 | * hid_hw_raw_request - send report request to device | |
2228 | * | |
2229 | * @hdev: hid device | |
2230 | * @reportnum: report ID | |
2231 | * @buf: in/out data to transfer | |
2232 | * @len: length of buf | |
2233 | * @rtype: HID report type | |
2234 | * @reqtype: HID_REQ_GET_REPORT or HID_REQ_SET_REPORT | |
2235 | * | |
2236 | * Return: count of data transferred, negative if error | |
2237 | * | |
2238 | * Same behavior as hid_hw_request, but with raw buffers instead. | |
2239 | */ | |
2240 | int hid_hw_raw_request(struct hid_device *hdev, | |
2241 | unsigned char reportnum, __u8 *buf, | |
2242 | size_t len, unsigned char rtype, int reqtype) | |
2243 | { | |
2244 | if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf) | |
2245 | return -EINVAL; | |
2246 | ||
2247 | return hdev->ll_driver->raw_request(hdev, reportnum, buf, len, | |
2248 | rtype, reqtype); | |
2249 | } | |
2250 | EXPORT_SYMBOL_GPL(hid_hw_raw_request); | |
2251 | ||
2252 | /** | |
2253 | * hid_hw_output_report - send output report to device | |
2254 | * | |
2255 | * @hdev: hid device | |
2256 | * @buf: raw data to transfer | |
2257 | * @len: length of buf | |
2258 | * | |
2259 | * Return: count of data transferred, negative if error | |
2260 | */ | |
2261 | int hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len) | |
2262 | { | |
2263 | if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf) | |
2264 | return -EINVAL; | |
2265 | ||
2266 | if (hdev->ll_driver->output_report) | |
2267 | return hdev->ll_driver->output_report(hdev, buf, len); | |
2268 | ||
2269 | return -ENOSYS; | |
2270 | } | |
2271 | EXPORT_SYMBOL_GPL(hid_hw_output_report); | |
2272 | ||
9e356208 BT |
2273 | #ifdef CONFIG_PM |
2274 | int hid_driver_suspend(struct hid_device *hdev, pm_message_t state) | |
2275 | { | |
2276 | if (hdev->driver && hdev->driver->suspend) | |
2277 | return hdev->driver->suspend(hdev, state); | |
2278 | ||
2279 | return 0; | |
2280 | } | |
2281 | EXPORT_SYMBOL_GPL(hid_driver_suspend); | |
2282 | ||
2283 | int hid_driver_reset_resume(struct hid_device *hdev) | |
2284 | { | |
2285 | if (hdev->driver && hdev->driver->reset_resume) | |
2286 | return hdev->driver->reset_resume(hdev); | |
2287 | ||
2288 | return 0; | |
2289 | } | |
2290 | EXPORT_SYMBOL_GPL(hid_driver_reset_resume); | |
2291 | ||
2292 | int hid_driver_resume(struct hid_device *hdev) | |
2293 | { | |
2294 | if (hdev->driver && hdev->driver->resume) | |
2295 | return hdev->driver->resume(hdev); | |
2296 | ||
2297 | return 0; | |
2298 | } | |
2299 | EXPORT_SYMBOL_GPL(hid_driver_resume); | |
2300 | #endif /* CONFIG_PM */ | |
2301 | ||
3a6f82f7 JS |
2302 | struct hid_dynid { |
2303 | struct list_head list; | |
2304 | struct hid_device_id id; | |
2305 | }; | |
2306 | ||
2307 | /** | |
cd2bb7b7 | 2308 | * new_id_store - add a new HID device ID to this driver and re-probe devices |
5f94e9c8 | 2309 | * @drv: target device driver |
3a6f82f7 JS |
2310 | * @buf: buffer for scanning device ID data |
2311 | * @count: input size | |
2312 | * | |
2313 | * Adds a new dynamic hid device ID to this driver, | |
2314 | * and causes the driver to probe for all devices again. | |
2315 | */ | |
c2810325 | 2316 | static ssize_t new_id_store(struct device_driver *drv, const char *buf, |
3a6f82f7 JS |
2317 | size_t count) |
2318 | { | |
ba91a967 | 2319 | struct hid_driver *hdrv = to_hid_driver(drv); |
3a6f82f7 JS |
2320 | struct hid_dynid *dynid; |
2321 | __u32 bus, vendor, product; | |
2322 | unsigned long driver_data = 0; | |
2323 | int ret; | |
2324 | ||
2325 | ret = sscanf(buf, "%x %x %x %lx", | |
2326 | &bus, &vendor, &product, &driver_data); | |
2327 | if (ret < 3) | |
2328 | return -EINVAL; | |
2329 | ||
2330 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); | |
2331 | if (!dynid) | |
2332 | return -ENOMEM; | |
2333 | ||
2334 | dynid->id.bus = bus; | |
4d53b801 | 2335 | dynid->id.group = HID_GROUP_ANY; |
3a6f82f7 JS |
2336 | dynid->id.vendor = vendor; |
2337 | dynid->id.product = product; | |
2338 | dynid->id.driver_data = driver_data; | |
2339 | ||
2340 | spin_lock(&hdrv->dyn_lock); | |
2341 | list_add_tail(&dynid->list, &hdrv->dyn_list); | |
2342 | spin_unlock(&hdrv->dyn_lock); | |
2343 | ||
cef9bc56 | 2344 | ret = driver_attach(&hdrv->driver); |
3a6f82f7 JS |
2345 | |
2346 | return ret ? : count; | |
2347 | } | |
c2810325 GKH |
2348 | static DRIVER_ATTR_WO(new_id); |
2349 | ||
2350 | static struct attribute *hid_drv_attrs[] = { | |
2351 | &driver_attr_new_id.attr, | |
2352 | NULL, | |
2353 | }; | |
2354 | ATTRIBUTE_GROUPS(hid_drv); | |
3a6f82f7 JS |
2355 | |
2356 | static void hid_free_dynids(struct hid_driver *hdrv) | |
2357 | { | |
2358 | struct hid_dynid *dynid, *n; | |
2359 | ||
2360 | spin_lock(&hdrv->dyn_lock); | |
2361 | list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) { | |
2362 | list_del(&dynid->list); | |
2363 | kfree(dynid); | |
2364 | } | |
2365 | spin_unlock(&hdrv->dyn_lock); | |
2366 | } | |
2367 | ||
e04a0442 BT |
2368 | const struct hid_device_id *hid_match_device(struct hid_device *hdev, |
2369 | struct hid_driver *hdrv) | |
3a6f82f7 JS |
2370 | { |
2371 | struct hid_dynid *dynid; | |
2372 | ||
2373 | spin_lock(&hdrv->dyn_lock); | |
2374 | list_for_each_entry(dynid, &hdrv->dyn_list, list) { | |
2375 | if (hid_match_one_id(hdev, &dynid->id)) { | |
2376 | spin_unlock(&hdrv->dyn_lock); | |
2377 | return &dynid->id; | |
2378 | } | |
2379 | } | |
2380 | spin_unlock(&hdrv->dyn_lock); | |
2381 | ||
2382 | return hid_match_id(hdev, hdrv->id_table); | |
2383 | } | |
e04a0442 | 2384 | EXPORT_SYMBOL_GPL(hid_match_device); |
3a6f82f7 | 2385 | |
85cdaf52 JS |
2386 | static int hid_bus_match(struct device *dev, struct device_driver *drv) |
2387 | { | |
ba91a967 | 2388 | struct hid_driver *hdrv = to_hid_driver(drv); |
ee79a8f8 | 2389 | struct hid_device *hdev = to_hid_device(dev); |
85cdaf52 | 2390 | |
070748ed | 2391 | return hid_match_device(hdev, hdrv) != NULL; |
85cdaf52 JS |
2392 | } |
2393 | ||
1a8861f1 DL |
2394 | /** |
2395 | * hid_compare_device_paths - check if both devices share the same path | |
2396 | * @hdev_a: hid device | |
2397 | * @hdev_b: hid device | |
2398 | * @separator: char to use as separator | |
2399 | * | |
2400 | * Check if two devices share the same path up to the last occurrence of | |
2401 | * the separator char. Both paths must exist (i.e., zero-length paths | |
2402 | * don't match). | |
2403 | */ | |
2404 | bool hid_compare_device_paths(struct hid_device *hdev_a, | |
2405 | struct hid_device *hdev_b, char separator) | |
2406 | { | |
2407 | int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys; | |
2408 | int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys; | |
2409 | ||
2410 | if (n1 != n2 || n1 <= 0 || n2 <= 0) | |
2411 | return false; | |
2412 | ||
2413 | return !strncmp(hdev_a->phys, hdev_b->phys, n1); | |
2414 | } | |
2415 | EXPORT_SYMBOL_GPL(hid_compare_device_paths); | |
2416 | ||
85cdaf52 JS |
2417 | static int hid_device_probe(struct device *dev) |
2418 | { | |
ba91a967 | 2419 | struct hid_driver *hdrv = to_hid_driver(dev->driver); |
ee79a8f8 | 2420 | struct hid_device *hdev = to_hid_device(dev); |
85cdaf52 JS |
2421 | const struct hid_device_id *id; |
2422 | int ret = 0; | |
2423 | ||
c849a614 AR |
2424 | if (down_interruptible(&hdev->driver_input_lock)) { |
2425 | ret = -EINTR; | |
6f68f0ac | 2426 | goto end; |
c849a614 AR |
2427 | } |
2428 | hdev->io_started = false; | |
4ea54542 | 2429 | |
8f732850 BT |
2430 | clear_bit(ffs(HID_STAT_REPROBED), &hdev->status); |
2431 | ||
85cdaf52 | 2432 | if (!hdev->driver) { |
3a6f82f7 | 2433 | id = hid_match_device(hdev, hdrv); |
ba623a77 | 2434 | if (id == NULL) { |
4fa3a583 HR |
2435 | ret = -ENODEV; |
2436 | goto unlock; | |
ba623a77 | 2437 | } |
85cdaf52 | 2438 | |
e04a0442 BT |
2439 | if (hdrv->match) { |
2440 | if (!hdrv->match(hdev, hid_ignore_special_drivers)) { | |
2441 | ret = -ENODEV; | |
2442 | goto unlock; | |
2443 | } | |
2444 | } else { | |
2445 | /* | |
2446 | * hid-generic implements .match(), so if | |
2447 | * hid_ignore_special_drivers is set, we can safely | |
2448 | * return. | |
2449 | */ | |
2450 | if (hid_ignore_special_drivers) { | |
2451 | ret = -ENODEV; | |
2452 | goto unlock; | |
2453 | } | |
2454 | } | |
2455 | ||
2904e68f BT |
2456 | /* reset the quirks that has been previously set */ |
2457 | hdev->quirks = hid_lookup_quirk(hdev); | |
c500c971 JS |
2458 | hdev->driver = hdrv; |
2459 | if (hdrv->probe) { | |
2460 | ret = hdrv->probe(hdev, id); | |
2461 | } else { /* default probe */ | |
a7197c2e | 2462 | ret = hid_open_report(hdev); |
c500c971 | 2463 | if (!ret) |
93c10132 | 2464 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
85cdaf52 | 2465 | } |
a7197c2e HR |
2466 | if (ret) { |
2467 | hid_close_report(hdev); | |
c500c971 | 2468 | hdev->driver = NULL; |
a7197c2e | 2469 | } |
85cdaf52 | 2470 | } |
ba623a77 | 2471 | unlock: |
c849a614 AR |
2472 | if (!hdev->io_started) |
2473 | up(&hdev->driver_input_lock); | |
6f68f0ac | 2474 | end: |
85cdaf52 JS |
2475 | return ret; |
2476 | } | |
2477 | ||
fc7a6209 | 2478 | static void hid_device_remove(struct device *dev) |
85cdaf52 | 2479 | { |
ee79a8f8 | 2480 | struct hid_device *hdev = to_hid_device(dev); |
4ea54542 DH |
2481 | struct hid_driver *hdrv; |
2482 | ||
f2145f8d | 2483 | down(&hdev->driver_input_lock); |
c849a614 | 2484 | hdev->io_started = false; |
85cdaf52 | 2485 | |
4ea54542 | 2486 | hdrv = hdev->driver; |
85cdaf52 JS |
2487 | if (hdrv) { |
2488 | if (hdrv->remove) | |
2489 | hdrv->remove(hdev); | |
c500c971 JS |
2490 | else /* default remove */ |
2491 | hid_hw_stop(hdev); | |
a7197c2e | 2492 | hid_close_report(hdev); |
85cdaf52 JS |
2493 | hdev->driver = NULL; |
2494 | } | |
2495 | ||
c849a614 AR |
2496 | if (!hdev->io_started) |
2497 | up(&hdev->driver_input_lock); | |
85cdaf52 JS |
2498 | } |
2499 | ||
4d53b801 HR |
2500 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
2501 | char *buf) | |
2502 | { | |
2503 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); | |
4d53b801 | 2504 | |
dfa0c5fa RV |
2505 | return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n", |
2506 | hdev->bus, hdev->group, hdev->vendor, hdev->product); | |
4d53b801 | 2507 | } |
0d4260e0 | 2508 | static DEVICE_ATTR_RO(modalias); |
4d53b801 | 2509 | |
0d4260e0 GKH |
2510 | static struct attribute *hid_dev_attrs[] = { |
2511 | &dev_attr_modalias.attr, | |
2512 | NULL, | |
4d53b801 | 2513 | }; |
54f32fd5 AL |
2514 | static struct bin_attribute *hid_dev_bin_attrs[] = { |
2515 | &dev_bin_attr_report_desc, | |
2516 | NULL | |
2517 | }; | |
2518 | static const struct attribute_group hid_dev_group = { | |
2519 | .attrs = hid_dev_attrs, | |
2520 | .bin_attrs = hid_dev_bin_attrs, | |
2521 | }; | |
2522 | __ATTRIBUTE_GROUPS(hid_dev); | |
4d53b801 | 2523 | |
85cdaf52 JS |
2524 | static int hid_uevent(struct device *dev, struct kobj_uevent_env *env) |
2525 | { | |
d193c169 | 2526 | struct hid_device *hdev = to_hid_device(dev); |
85cdaf52 JS |
2527 | |
2528 | if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X", | |
2529 | hdev->bus, hdev->vendor, hdev->product)) | |
2530 | return -ENOMEM; | |
2531 | ||
2532 | if (add_uevent_var(env, "HID_NAME=%s", hdev->name)) | |
2533 | return -ENOMEM; | |
2534 | ||
2535 | if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys)) | |
2536 | return -ENOMEM; | |
2537 | ||
2538 | if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq)) | |
2539 | return -ENOMEM; | |
2540 | ||
4d53b801 HR |
2541 | if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X", |
2542 | hdev->bus, hdev->group, hdev->vendor, hdev->product)) | |
85cdaf52 JS |
2543 | return -ENOMEM; |
2544 | ||
2545 | return 0; | |
2546 | } | |
2547 | ||
e04a0442 | 2548 | struct bus_type hid_bus_type = { |
85cdaf52 | 2549 | .name = "hid", |
0d4260e0 | 2550 | .dev_groups = hid_dev_groups, |
c2810325 | 2551 | .drv_groups = hid_drv_groups, |
85cdaf52 JS |
2552 | .match = hid_bus_match, |
2553 | .probe = hid_device_probe, | |
2554 | .remove = hid_device_remove, | |
2555 | .uevent = hid_uevent, | |
2556 | }; | |
d5d3e202 | 2557 | EXPORT_SYMBOL(hid_bus_type); |
d458a9df | 2558 | |
85cdaf52 JS |
2559 | int hid_add_device(struct hid_device *hdev) |
2560 | { | |
2561 | static atomic_t id = ATOMIC_INIT(0); | |
2562 | int ret; | |
2563 | ||
2564 | if (WARN_ON(hdev->status & HID_STAT_ADDED)) | |
2565 | return -EBUSY; | |
2566 | ||
d5d3e202 BT |
2567 | hdev->quirks = hid_lookup_quirk(hdev); |
2568 | ||
d458a9df JS |
2569 | /* we need to kill them here, otherwise they will stay allocated to |
2570 | * wait for coming driver */ | |
4529eefa | 2571 | if (hid_ignore(hdev)) |
d458a9df JS |
2572 | return -ENODEV; |
2573 | ||
3c86726c BT |
2574 | /* |
2575 | * Check for the mandatory transport channel. | |
2576 | */ | |
2577 | if (!hdev->ll_driver->raw_request) { | |
2578 | hid_err(hdev, "transport driver missing .raw_request()\n"); | |
2579 | return -EINVAL; | |
2580 | } | |
2581 | ||
a7197c2e HR |
2582 | /* |
2583 | * Read the device report descriptor once and use as template | |
2584 | * for the driver-specific modifications. | |
2585 | */ | |
2586 | ret = hdev->ll_driver->parse(hdev); | |
2587 | if (ret) | |
2588 | return ret; | |
2589 | if (!hdev->dev_rdesc) | |
2590 | return -ENODEV; | |
2591 | ||
734c6609 HR |
2592 | /* |
2593 | * Scan generic devices for group information | |
2594 | */ | |
4392bf33 BT |
2595 | if (hid_ignore_special_drivers) { |
2596 | hdev->group = HID_GROUP_GENERIC; | |
2597 | } else if (!hdev->group && | |
6e65d9d5 | 2598 | !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) { |
734c6609 HR |
2599 | ret = hid_scan_report(hdev); |
2600 | if (ret) | |
2601 | hid_warn(hdev, "bad device descriptor (%d)\n", ret); | |
2602 | } | |
2603 | ||
6bbe586f KS |
2604 | /* XXX hack, any other cleaner solution after the driver core |
2605 | * is converted to allow more than 20 bytes as the device name? */ | |
2606 | dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus, | |
2607 | hdev->vendor, hdev->product, atomic_inc_return(&id)); | |
85cdaf52 | 2608 | |
4da361b6 | 2609 | hid_debug_register(hdev, dev_name(&hdev->dev)); |
85cdaf52 JS |
2610 | ret = device_add(&hdev->dev); |
2611 | if (!ret) | |
2612 | hdev->status |= HID_STAT_ADDED; | |
4da361b6 BP |
2613 | else |
2614 | hid_debug_unregister(hdev); | |
a635f9dd | 2615 | |
85cdaf52 JS |
2616 | return ret; |
2617 | } | |
2618 | EXPORT_SYMBOL_GPL(hid_add_device); | |
2619 | ||
2620 | /** | |
2621 | * hid_allocate_device - allocate new hid device descriptor | |
2622 | * | |
2623 | * Allocate and initialize hid device, so that hid_destroy_device might be | |
2624 | * used to free it. | |
2625 | * | |
2626 | * New hid_device pointer is returned on success, otherwise ERR_PTR encoded | |
2627 | * error value. | |
2628 | */ | |
2629 | struct hid_device *hid_allocate_device(void) | |
2630 | { | |
2631 | struct hid_device *hdev; | |
85cdaf52 JS |
2632 | int ret = -ENOMEM; |
2633 | ||
2634 | hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); | |
2635 | if (hdev == NULL) | |
2636 | return ERR_PTR(ret); | |
2637 | ||
2638 | device_initialize(&hdev->dev); | |
2639 | hdev->dev.release = hid_device_release; | |
2640 | hdev->dev.bus = &hid_bus_type; | |
64bebefc | 2641 | device_enable_async_suspend(&hdev->dev); |
85cdaf52 | 2642 | |
a7197c2e | 2643 | hid_close_report(hdev); |
85cdaf52 | 2644 | |
cd667ce2 JK |
2645 | init_waitqueue_head(&hdev->debug_wait); |
2646 | INIT_LIST_HEAD(&hdev->debug_list); | |
1deb9d34 | 2647 | spin_lock_init(&hdev->debug_list_lock); |
c849a614 | 2648 | sema_init(&hdev->driver_input_lock, 1); |
aaac082d | 2649 | mutex_init(&hdev->ll_open_lock); |
cd667ce2 | 2650 | |
85cdaf52 | 2651 | return hdev; |
85cdaf52 JS |
2652 | } |
2653 | EXPORT_SYMBOL_GPL(hid_allocate_device); | |
2654 | ||
2655 | static void hid_remove_device(struct hid_device *hdev) | |
2656 | { | |
2657 | if (hdev->status & HID_STAT_ADDED) { | |
2658 | device_del(&hdev->dev); | |
a635f9dd | 2659 | hid_debug_unregister(hdev); |
85cdaf52 JS |
2660 | hdev->status &= ~HID_STAT_ADDED; |
2661 | } | |
a7197c2e HR |
2662 | kfree(hdev->dev_rdesc); |
2663 | hdev->dev_rdesc = NULL; | |
2664 | hdev->dev_rsize = 0; | |
85cdaf52 JS |
2665 | } |
2666 | ||
2667 | /** | |
2668 | * hid_destroy_device - free previously allocated device | |
2669 | * | |
2670 | * @hdev: hid device | |
2671 | * | |
2672 | * If you allocate hid_device through hid_allocate_device, you should ever | |
2673 | * free by this function. | |
2674 | */ | |
2675 | void hid_destroy_device(struct hid_device *hdev) | |
2676 | { | |
2677 | hid_remove_device(hdev); | |
2678 | put_device(&hdev->dev); | |
2679 | } | |
2680 | EXPORT_SYMBOL_GPL(hid_destroy_device); | |
2681 | ||
e04a0442 | 2682 | |
c17a7476 | 2683 | static int __hid_bus_reprobe_drivers(struct device *dev, void *data) |
e04a0442 | 2684 | { |
c17a7476 BT |
2685 | struct hid_driver *hdrv = data; |
2686 | struct hid_device *hdev = to_hid_device(dev); | |
e04a0442 | 2687 | |
c17a7476 | 2688 | if (hdev->driver == hdrv && |
8f732850 BT |
2689 | !hdrv->match(hdev, hid_ignore_special_drivers) && |
2690 | !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status)) | |
c17a7476 | 2691 | return device_reprobe(dev); |
e04a0442 BT |
2692 | |
2693 | return 0; | |
2694 | } | |
2695 | ||
c17a7476 | 2696 | static int __hid_bus_driver_added(struct device_driver *drv, void *data) |
e04a0442 | 2697 | { |
e04a0442 BT |
2698 | struct hid_driver *hdrv = to_hid_driver(drv); |
2699 | ||
c17a7476 BT |
2700 | if (hdrv->match) { |
2701 | bus_for_each_dev(&hid_bus_type, NULL, hdrv, | |
2702 | __hid_bus_reprobe_drivers); | |
2703 | } | |
e04a0442 BT |
2704 | |
2705 | return 0; | |
2706 | } | |
2707 | ||
c17a7476 BT |
2708 | static int __bus_removed_driver(struct device_driver *drv, void *data) |
2709 | { | |
2710 | return bus_rescan_devices(&hid_bus_type); | |
2711 | } | |
2712 | ||
85cdaf52 JS |
2713 | int __hid_register_driver(struct hid_driver *hdrv, struct module *owner, |
2714 | const char *mod_name) | |
2715 | { | |
c17a7476 BT |
2716 | int ret; |
2717 | ||
85cdaf52 JS |
2718 | hdrv->driver.name = hdrv->name; |
2719 | hdrv->driver.bus = &hid_bus_type; | |
2720 | hdrv->driver.owner = owner; | |
2721 | hdrv->driver.mod_name = mod_name; | |
2722 | ||
3a6f82f7 JS |
2723 | INIT_LIST_HEAD(&hdrv->dyn_list); |
2724 | spin_lock_init(&hdrv->dyn_lock); | |
2725 | ||
c17a7476 BT |
2726 | ret = driver_register(&hdrv->driver); |
2727 | ||
2728 | if (ret == 0) | |
2729 | bus_for_each_drv(&hid_bus_type, NULL, NULL, | |
2730 | __hid_bus_driver_added); | |
e04a0442 | 2731 | |
c17a7476 | 2732 | return ret; |
85cdaf52 JS |
2733 | } |
2734 | EXPORT_SYMBOL_GPL(__hid_register_driver); | |
2735 | ||
2736 | void hid_unregister_driver(struct hid_driver *hdrv) | |
2737 | { | |
2738 | driver_unregister(&hdrv->driver); | |
3a6f82f7 | 2739 | hid_free_dynids(hdrv); |
e04a0442 BT |
2740 | |
2741 | bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver); | |
85cdaf52 JS |
2742 | } |
2743 | EXPORT_SYMBOL_GPL(hid_unregister_driver); | |
2744 | ||
0361a28d ON |
2745 | int hid_check_keys_pressed(struct hid_device *hid) |
2746 | { | |
2747 | struct hid_input *hidinput; | |
2748 | int i; | |
2749 | ||
e5288eb5 JK |
2750 | if (!(hid->claimed & HID_CLAIMED_INPUT)) |
2751 | return 0; | |
2752 | ||
0361a28d ON |
2753 | list_for_each_entry(hidinput, &hid->inputs, list) { |
2754 | for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++) | |
2755 | if (hidinput->input->key[i]) | |
2756 | return 1; | |
2757 | } | |
2758 | ||
2759 | return 0; | |
2760 | } | |
0361a28d ON |
2761 | EXPORT_SYMBOL_GPL(hid_check_keys_pressed); |
2762 | ||
86166b7b JK |
2763 | static int __init hid_init(void) |
2764 | { | |
85cdaf52 JS |
2765 | int ret; |
2766 | ||
a635f9dd | 2767 | if (hid_debug) |
4291ee30 JP |
2768 | pr_warn("hid_debug is now used solely for parser and driver debugging.\n" |
2769 | "debugfs is now used for inspecting the device (report descriptor, reports)\n"); | |
a635f9dd | 2770 | |
85cdaf52 JS |
2771 | ret = bus_register(&hid_bus_type); |
2772 | if (ret) { | |
4291ee30 | 2773 | pr_err("can't register hid bus\n"); |
85cdaf52 JS |
2774 | goto err; |
2775 | } | |
2776 | ||
2777 | ret = hidraw_init(); | |
2778 | if (ret) | |
2779 | goto err_bus; | |
2780 | ||
a635f9dd JK |
2781 | hid_debug_init(); |
2782 | ||
85cdaf52 JS |
2783 | return 0; |
2784 | err_bus: | |
2785 | bus_unregister(&hid_bus_type); | |
2786 | err: | |
2787 | return ret; | |
86166b7b JK |
2788 | } |
2789 | ||
2790 | static void __exit hid_exit(void) | |
2791 | { | |
a635f9dd | 2792 | hid_debug_exit(); |
86166b7b | 2793 | hidraw_exit(); |
85cdaf52 | 2794 | bus_unregister(&hid_bus_type); |
d5d3e202 | 2795 | hid_quirks_exit(HID_BUS_ANY); |
86166b7b JK |
2796 | } |
2797 | ||
2798 | module_init(hid_init); | |
2799 | module_exit(hid_exit); | |
2800 | ||
88adb72b JK |
2801 | MODULE_AUTHOR("Andreas Gal"); |
2802 | MODULE_AUTHOR("Vojtech Pavlik"); | |
2803 | MODULE_AUTHOR("Jiri Kosina"); | |
7021b600 | 2804 | MODULE_LICENSE("GPL"); |