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