blob: 80792d38d25c103dbaaaab0ea87c2af02f6a4af4 [file] [log] [blame]
Jiri Kosinadde58452006-12-08 18:40:44 +01001/*
Jiri Kosina229695e2006-12-08 18:40:53 +01002 * HID support for Linux
Jiri Kosinadde58452006-12-08 18:40:44 +01003 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Jiri Kosinaf142b3a42007-04-16 11:29:28 +02007 * Copyright (c) 2006-2007 Jiri Kosina
Jiri Kosinadde58452006-12-08 18:40:44 +01008 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010021#include <linux/list.h>
22#include <linux/mm.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010023#include <linux/spinlock.h>
24#include <asm/unaligned.h>
25#include <asm/byteorder.h>
26#include <linux/input.h>
27#include <linux/wait.h>
Jiri Kosina47a80ed2007-03-12 14:55:12 +010028#include <linux/vmalloc.h>
Jiri Kosinac4124c92007-11-30 11:12:58 +010029#include <linux/sched.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010030
Jiri Kosinadde58452006-12-08 18:40:44 +010031#include <linux/hid.h>
32#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010033#include <linux/hid-debug.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020034#include <linux/hidraw.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010035
Jiri Slaby5f22a792008-05-16 11:49:19 +020036#include "hid-ids.h"
37
Jiri Kosinadde58452006-12-08 18:40:44 +010038/*
39 * Version Information
40 */
41
Jiri Kosina53149802007-01-09 13:24:25 +010042#define DRIVER_DESC "HID core driver"
Jiri Kosinadde58452006-12-08 18:40:44 +010043#define DRIVER_LICENSE "GPL"
44
Jiri Kosina58037eb2007-05-30 15:07:13 +020045int hid_debug = 0;
Anssi Hannula377e10f2008-03-22 23:50:13 +010046module_param_named(debug, hid_debug, int, 0600);
Jiri Kosinacd667ce2009-06-12 15:20:57 +020047MODULE_PARM_DESC(debug, "toggle HID debugging messages");
Jiri Kosina58037eb2007-05-30 15:07:13 +020048EXPORT_SYMBOL_GPL(hid_debug);
Jiri Kosina58037eb2007-05-30 15:07:13 +020049
Jiri Kosinadde58452006-12-08 18:40:44 +010050/*
Jiri Kosinadde58452006-12-08 18:40:44 +010051 * Register a new report for a device.
52 */
53
54static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
55{
56 struct hid_report_enum *report_enum = device->report_enum + type;
57 struct hid_report *report;
58
59 if (report_enum->report_id_hash[id])
60 return report_enum->report_id_hash[id];
61
62 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
63 return NULL;
64
65 if (id != 0)
66 report_enum->numbered = 1;
67
68 report->id = id;
69 report->type = type;
70 report->size = 0;
71 report->device = device;
72 report_enum->report_id_hash[id] = report;
73
74 list_add_tail(&report->list, &report_enum->report_list);
75
76 return report;
77}
78
79/*
80 * Register a new field for this report.
81 */
82
83static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
84{
85 struct hid_field *field;
86
87 if (report->maxfield == HID_MAX_FIELDS) {
Jiri Kosina58037eb2007-05-30 15:07:13 +020088 dbg_hid("too many fields in report\n");
Jiri Kosinadde58452006-12-08 18:40:44 +010089 return NULL;
90 }
91
92 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
93 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
94
95 field->index = report->maxfield++;
96 report->field[field->index] = field;
97 field->usage = (struct hid_usage *)(field + 1);
Jiri Slaby282bfd42008-03-28 17:06:41 +010098 field->value = (s32 *)(field->usage + usages);
Jiri Kosinadde58452006-12-08 18:40:44 +010099 field->report = report;
100
101 return field;
102}
103
104/*
105 * Open a collection. The type/usage is pushed on the stack.
106 */
107
108static int open_collection(struct hid_parser *parser, unsigned type)
109{
110 struct hid_collection *collection;
111 unsigned usage;
112
113 usage = parser->local.usage[0];
114
115 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200116 dbg_hid("collection stack overflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100117 return -1;
118 }
119
120 if (parser->device->maxcollection == parser->device->collection_size) {
121 collection = kmalloc(sizeof(struct hid_collection) *
122 parser->device->collection_size * 2, GFP_KERNEL);
123 if (collection == NULL) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200124 dbg_hid("failed to reallocate collection array\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100125 return -1;
126 }
127 memcpy(collection, parser->device->collection,
128 sizeof(struct hid_collection) *
129 parser->device->collection_size);
130 memset(collection + parser->device->collection_size, 0,
131 sizeof(struct hid_collection) *
132 parser->device->collection_size);
133 kfree(parser->device->collection);
134 parser->device->collection = collection;
135 parser->device->collection_size *= 2;
136 }
137
138 parser->collection_stack[parser->collection_stack_ptr++] =
139 parser->device->maxcollection;
140
141 collection = parser->device->collection +
142 parser->device->maxcollection++;
143 collection->type = type;
144 collection->usage = usage;
145 collection->level = parser->collection_stack_ptr - 1;
146
147 if (type == HID_COLLECTION_APPLICATION)
148 parser->device->maxapplication++;
149
150 return 0;
151}
152
153/*
154 * Close a collection.
155 */
156
157static int close_collection(struct hid_parser *parser)
158{
159 if (!parser->collection_stack_ptr) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200160 dbg_hid("collection stack underflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100161 return -1;
162 }
163 parser->collection_stack_ptr--;
164 return 0;
165}
166
167/*
168 * Climb up the stack, search for the specified collection type
169 * and return the usage.
170 */
171
172static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
173{
174 int n;
175 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
176 if (parser->device->collection[parser->collection_stack[n]].type == type)
177 return parser->device->collection[parser->collection_stack[n]].usage;
178 return 0; /* we know nothing about this usage type */
179}
180
181/*
182 * Add a usage to the temporary parser table.
183 */
184
185static int hid_add_usage(struct hid_parser *parser, unsigned usage)
186{
187 if (parser->local.usage_index >= HID_MAX_USAGES) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200188 dbg_hid("usage index exceeded\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100189 return -1;
190 }
191 parser->local.usage[parser->local.usage_index] = usage;
192 parser->local.collection_index[parser->local.usage_index] =
193 parser->collection_stack_ptr ?
194 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
195 parser->local.usage_index++;
196 return 0;
197}
198
199/*
200 * Register a new field for this report.
201 */
202
203static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
204{
205 struct hid_report *report;
206 struct hid_field *field;
207 int usages;
208 unsigned offset;
209 int i;
210
211 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200212 dbg_hid("hid_register_report failed\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100213 return -1;
214 }
215
216 if (parser->global.logical_maximum < parser->global.logical_minimum) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200217 dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
Jiri Kosinadde58452006-12-08 18:40:44 +0100218 return -1;
219 }
220
221 offset = report->size;
222 report->size += parser->global.report_size * parser->global.report_count;
223
224 if (!parser->local.usage_index) /* Ignore padding fields */
225 return 0;
226
227 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
228
229 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
230 return 0;
231
232 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
233 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
234 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
235
236 for (i = 0; i < usages; i++) {
237 int j = i;
238 /* Duplicate the last usage we parsed if we have excess values */
239 if (i >= parser->local.usage_index)
240 j = parser->local.usage_index - 1;
241 field->usage[i].hid = parser->local.usage[j];
242 field->usage[i].collection_index =
243 parser->local.collection_index[j];
244 }
245
246 field->maxusage = usages;
247 field->flags = flags;
248 field->report_offset = offset;
249 field->report_type = report_type;
250 field->report_size = parser->global.report_size;
251 field->report_count = parser->global.report_count;
252 field->logical_minimum = parser->global.logical_minimum;
253 field->logical_maximum = parser->global.logical_maximum;
254 field->physical_minimum = parser->global.physical_minimum;
255 field->physical_maximum = parser->global.physical_maximum;
256 field->unit_exponent = parser->global.unit_exponent;
257 field->unit = parser->global.unit;
258
259 return 0;
260}
261
262/*
263 * Read data value from item.
264 */
265
266static u32 item_udata(struct hid_item *item)
267{
268 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200269 case 1: return item->data.u8;
270 case 2: return item->data.u16;
271 case 4: return item->data.u32;
Jiri Kosinadde58452006-12-08 18:40:44 +0100272 }
273 return 0;
274}
275
276static s32 item_sdata(struct hid_item *item)
277{
278 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200279 case 1: return item->data.s8;
280 case 2: return item->data.s16;
281 case 4: return item->data.s32;
Jiri Kosinadde58452006-12-08 18:40:44 +0100282 }
283 return 0;
284}
285
286/*
287 * Process a global item.
288 */
289
290static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
291{
292 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200293 case HID_GLOBAL_ITEM_TAG_PUSH:
Jiri Kosinadde58452006-12-08 18:40:44 +0100294
Jiri Slaby880d29f2008-06-18 23:55:41 +0200295 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
296 dbg_hid("global enviroment stack overflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100297 return -1;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200298 }
299
300 memcpy(parser->global_stack + parser->global_stack_ptr++,
301 &parser->global, sizeof(struct hid_global));
302 return 0;
303
304 case HID_GLOBAL_ITEM_TAG_POP:
305
306 if (!parser->global_stack_ptr) {
307 dbg_hid("global enviroment stack underflow\n");
308 return -1;
309 }
310
311 memcpy(&parser->global, parser->global_stack +
312 --parser->global_stack_ptr, sizeof(struct hid_global));
313 return 0;
314
315 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
316 parser->global.usage_page = item_udata(item);
317 return 0;
318
319 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
320 parser->global.logical_minimum = item_sdata(item);
321 return 0;
322
323 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
324 if (parser->global.logical_minimum < 0)
325 parser->global.logical_maximum = item_sdata(item);
326 else
327 parser->global.logical_maximum = item_udata(item);
328 return 0;
329
330 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
331 parser->global.physical_minimum = item_sdata(item);
332 return 0;
333
334 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
335 if (parser->global.physical_minimum < 0)
336 parser->global.physical_maximum = item_sdata(item);
337 else
338 parser->global.physical_maximum = item_udata(item);
339 return 0;
340
341 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
342 parser->global.unit_exponent = item_sdata(item);
343 return 0;
344
345 case HID_GLOBAL_ITEM_TAG_UNIT:
346 parser->global.unit = item_udata(item);
347 return 0;
348
349 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
350 parser->global.report_size = item_udata(item);
351 if (parser->global.report_size > 32) {
352 dbg_hid("invalid report_size %d\n",
353 parser->global.report_size);
354 return -1;
355 }
356 return 0;
357
358 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
359 parser->global.report_count = item_udata(item);
360 if (parser->global.report_count > HID_MAX_USAGES) {
361 dbg_hid("invalid report_count %d\n",
362 parser->global.report_count);
363 return -1;
364 }
365 return 0;
366
367 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
368 parser->global.report_id = item_udata(item);
369 if (parser->global.report_id == 0) {
370 dbg_hid("report_id 0 is invalid\n");
371 return -1;
372 }
373 return 0;
374
375 default:
376 dbg_hid("unknown global tag 0x%x\n", item->tag);
377 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100378 }
379}
380
381/*
382 * Process a local item.
383 */
384
385static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
386{
387 __u32 data;
388 unsigned n;
389
390 if (item->size == 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200391 dbg_hid("item data expected for local item\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100392 return -1;
393 }
394
395 data = item_udata(item);
396
397 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200398 case HID_LOCAL_ITEM_TAG_DELIMITER:
Jiri Kosinadde58452006-12-08 18:40:44 +0100399
Jiri Slaby880d29f2008-06-18 23:55:41 +0200400 if (data) {
401 /*
402 * We treat items before the first delimiter
403 * as global to all usage sets (branch 0).
404 * In the moment we process only these global
405 * items and the first delimiter set.
406 */
407 if (parser->local.delimiter_depth != 0) {
408 dbg_hid("nested delimiters\n");
409 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100410 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200411 parser->local.delimiter_depth++;
412 parser->local.delimiter_branch++;
413 } else {
414 if (parser->local.delimiter_depth < 1) {
415 dbg_hid("bogus close delimiter\n");
416 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100417 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200418 parser->local.delimiter_depth--;
419 }
420 return 1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100421
Jiri Slaby880d29f2008-06-18 23:55:41 +0200422 case HID_LOCAL_ITEM_TAG_USAGE:
Jiri Kosinadde58452006-12-08 18:40:44 +0100423
Jiri Slaby880d29f2008-06-18 23:55:41 +0200424 if (parser->local.delimiter_branch > 1) {
425 dbg_hid("alternative usage ignored\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100426 return 0;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200427 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100428
Jiri Slaby880d29f2008-06-18 23:55:41 +0200429 if (item->size <= 2)
430 data = (parser->global.usage_page << 16) + data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100431
Jiri Slaby880d29f2008-06-18 23:55:41 +0200432 return hid_add_usage(parser, data);
433
434 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
435
436 if (parser->local.delimiter_branch > 1) {
437 dbg_hid("alternative usage ignored\n");
438 return 0;
439 }
440
441 if (item->size <= 2)
442 data = (parser->global.usage_page << 16) + data;
443
444 parser->local.usage_minimum = data;
445 return 0;
446
447 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
448
449 if (parser->local.delimiter_branch > 1) {
450 dbg_hid("alternative usage ignored\n");
451 return 0;
452 }
453
454 if (item->size <= 2)
455 data = (parser->global.usage_page << 16) + data;
456
457 for (n = parser->local.usage_minimum; n <= data; n++)
458 if (hid_add_usage(parser, n)) {
459 dbg_hid("hid_add_usage failed\n");
460 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100461 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200462 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100463
Jiri Slaby880d29f2008-06-18 23:55:41 +0200464 default:
Jiri Kosinadde58452006-12-08 18:40:44 +0100465
Jiri Slaby880d29f2008-06-18 23:55:41 +0200466 dbg_hid("unknown local item tag 0x%x\n", item->tag);
467 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100468 }
469 return 0;
470}
471
472/*
473 * Process a main item.
474 */
475
476static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
477{
478 __u32 data;
479 int ret;
480
481 data = item_udata(item);
482
483 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200484 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
485 ret = open_collection(parser, data & 0xff);
486 break;
487 case HID_MAIN_ITEM_TAG_END_COLLECTION:
488 ret = close_collection(parser);
489 break;
490 case HID_MAIN_ITEM_TAG_INPUT:
491 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
492 break;
493 case HID_MAIN_ITEM_TAG_OUTPUT:
494 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
495 break;
496 case HID_MAIN_ITEM_TAG_FEATURE:
497 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
498 break;
499 default:
500 dbg_hid("unknown main item tag 0x%x\n", item->tag);
501 ret = 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100502 }
503
504 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
505
506 return ret;
507}
508
509/*
510 * Process a reserved item.
511 */
512
513static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
514{
Jiri Kosina58037eb2007-05-30 15:07:13 +0200515 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
Jiri Kosinadde58452006-12-08 18:40:44 +0100516 return 0;
517}
518
519/*
520 * Free a report and all registered fields. The field->usage and
521 * field->value table's are allocated behind the field, so we need
522 * only to free(field) itself.
523 */
524
525static void hid_free_report(struct hid_report *report)
526{
527 unsigned n;
528
529 for (n = 0; n < report->maxfield; n++)
530 kfree(report->field[n]);
531 kfree(report);
532}
533
534/*
535 * Free a device structure, all reports, and all fields.
536 */
537
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200538static void hid_device_release(struct device *dev)
Jiri Kosinadde58452006-12-08 18:40:44 +0100539{
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200540 struct hid_device *device = container_of(dev, struct hid_device, dev);
541 unsigned i, j;
Jiri Kosinadde58452006-12-08 18:40:44 +0100542
543 for (i = 0; i < HID_REPORT_TYPES; i++) {
544 struct hid_report_enum *report_enum = device->report_enum + i;
545
546 for (j = 0; j < 256; j++) {
547 struct hid_report *report = report_enum->report_id_hash[j];
548 if (report)
549 hid_free_report(report);
550 }
551 }
552
553 kfree(device->rdesc);
Jiri Kosina767fe782007-01-24 23:05:07 +0100554 kfree(device->collection);
Jiri Kosinadde58452006-12-08 18:40:44 +0100555 kfree(device);
556}
557
558/*
559 * Fetch a report description item from the data stream. We support long
560 * items, though they are not used yet.
561 */
562
563static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
564{
565 u8 b;
566
567 if ((end - start) <= 0)
568 return NULL;
569
570 b = *start++;
571
572 item->type = (b >> 2) & 3;
573 item->tag = (b >> 4) & 15;
574
575 if (item->tag == HID_ITEM_TAG_LONG) {
576
577 item->format = HID_ITEM_FORMAT_LONG;
578
579 if ((end - start) < 2)
580 return NULL;
581
582 item->size = *start++;
583 item->tag = *start++;
584
585 if ((end - start) < item->size)
586 return NULL;
587
588 item->data.longdata = start;
589 start += item->size;
590 return start;
591 }
592
593 item->format = HID_ITEM_FORMAT_SHORT;
594 item->size = b & 3;
595
596 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200597 case 0:
598 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100599
Jiri Slaby880d29f2008-06-18 23:55:41 +0200600 case 1:
601 if ((end - start) < 1)
602 return NULL;
603 item->data.u8 = *start++;
604 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100605
Jiri Slaby880d29f2008-06-18 23:55:41 +0200606 case 2:
607 if ((end - start) < 2)
608 return NULL;
609 item->data.u16 = get_unaligned_le16(start);
610 start = (__u8 *)((__le16 *)start + 1);
611 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100612
Jiri Slaby880d29f2008-06-18 23:55:41 +0200613 case 3:
614 item->size++;
615 if ((end - start) < 4)
616 return NULL;
617 item->data.u32 = get_unaligned_le32(start);
618 start = (__u8 *)((__le32 *)start + 1);
619 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100620 }
621
622 return NULL;
623}
624
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200625/**
626 * hid_parse_report - parse device report
627 *
628 * @device: hid device
629 * @start: report start
630 * @size: report size
631 *
Jiri Kosinadde58452006-12-08 18:40:44 +0100632 * Parse a report description into a hid_device structure. Reports are
633 * enumerated, fields are attached to these reports.
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200634 * 0 returned on success, otherwise nonzero error value.
Jiri Kosinadde58452006-12-08 18:40:44 +0100635 */
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200636int hid_parse_report(struct hid_device *device, __u8 *start,
637 unsigned size)
Jiri Kosinadde58452006-12-08 18:40:44 +0100638{
Jiri Kosinadde58452006-12-08 18:40:44 +0100639 struct hid_parser *parser;
640 struct hid_item item;
641 __u8 *end;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200642 int ret;
Jiri Kosinadde58452006-12-08 18:40:44 +0100643 static int (*dispatch_type[])(struct hid_parser *parser,
644 struct hid_item *item) = {
645 hid_parser_main,
646 hid_parser_global,
647 hid_parser_local,
648 hid_parser_reserved
649 };
650
Jiri Slabyc500c972008-05-16 11:49:16 +0200651 if (device->driver->report_fixup)
652 device->driver->report_fixup(device, start, size);
653
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200654 device->rdesc = kmalloc(size, GFP_KERNEL);
655 if (device->rdesc == NULL)
656 return -ENOMEM;
Jiri Kosinadde58452006-12-08 18:40:44 +0100657 memcpy(device->rdesc, start, size);
658 device->rsize = size;
659
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200660 parser = vmalloc(sizeof(struct hid_parser));
661 if (!parser) {
662 ret = -ENOMEM;
663 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100664 }
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200665
Jiri Kosina47a80ed2007-03-12 14:55:12 +0100666 memset(parser, 0, sizeof(struct hid_parser));
Jiri Kosinadde58452006-12-08 18:40:44 +0100667 parser->device = device;
668
669 end = start + size;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200670 ret = -EINVAL;
Jiri Kosinadde58452006-12-08 18:40:44 +0100671 while ((start = fetch_item(start, end, &item)) != NULL) {
672
673 if (item.format != HID_ITEM_FORMAT_SHORT) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200674 dbg_hid("unexpected long global item\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200675 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100676 }
677
678 if (dispatch_type[item.type](parser, &item)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200679 dbg_hid("item %u %u %u %u parsing failed\n",
Jiri Kosinadde58452006-12-08 18:40:44 +0100680 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200681 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100682 }
683
684 if (start == end) {
685 if (parser->collection_stack_ptr) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200686 dbg_hid("unbalanced collection at end of report description\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200687 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100688 }
689 if (parser->local.delimiter_depth) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200690 dbg_hid("unbalanced delimiter at end of report description\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200691 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100692 }
Jiri Kosina47a80ed2007-03-12 14:55:12 +0100693 vfree(parser);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200694 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100695 }
696 }
697
Jiri Kosina58037eb2007-05-30 15:07:13 +0200698 dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200699err:
Jiri Kosina47a80ed2007-03-12 14:55:12 +0100700 vfree(parser);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200701 return ret;
Jiri Kosinadde58452006-12-08 18:40:44 +0100702}
Jiri Kosina229695e2006-12-08 18:40:53 +0100703EXPORT_SYMBOL_GPL(hid_parse_report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100704
705/*
706 * Convert a signed n-bit integer to signed 32-bit integer. Common
707 * cases are done through the compiler, the screwed things has to be
708 * done by hand.
709 */
710
711static s32 snto32(__u32 value, unsigned n)
712{
713 switch (n) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200714 case 8: return ((__s8)value);
715 case 16: return ((__s16)value);
716 case 32: return ((__s32)value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100717 }
718 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
719}
720
721/*
722 * Convert a signed 32-bit integer to a signed n-bit integer.
723 */
724
725static u32 s32ton(__s32 value, unsigned n)
726{
727 s32 a = value >> (n - 1);
728 if (a && a != -1)
729 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
730 return value & ((1 << n) - 1);
731}
732
733/*
734 * Extract/implement a data field from/to a little endian report (bit array).
735 *
736 * Code sort-of follows HID spec:
737 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
738 *
739 * While the USB HID spec allows unlimited length bit fields in "report
740 * descriptors", most devices never use more than 16 bits.
741 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
742 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
743 */
744
745static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
746{
747 u64 x;
748
Jiri Kosinac4124c92007-11-30 11:12:58 +0100749 if (n > 32)
750 printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
751 n, current->comm);
Jiri Kosinadde58452006-12-08 18:40:44 +0100752
753 report += offset >> 3; /* adjust byte index */
Jiri Kosina229695e2006-12-08 18:40:53 +0100754 offset &= 7; /* now only need bit offset into one byte */
Harvey Harrisonc1050682008-04-29 01:03:31 -0700755 x = get_unaligned_le64(report);
Jiri Kosina229695e2006-12-08 18:40:53 +0100756 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
Jiri Kosinadde58452006-12-08 18:40:44 +0100757 return (u32) x;
758}
759
760/*
761 * "implement" : set bits in a little endian bit stream.
762 * Same concepts as "extract" (see comments above).
763 * The data mangled in the bit stream remains in little endian
764 * order the whole time. It make more sense to talk about
765 * endianness of register values by considering a register
766 * a "cached" copy of the little endiad bit stream.
767 */
768static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
769{
Harvey Harrison6f0168d2008-05-16 11:00:23 +0200770 u64 x;
Jiri Kosinadde58452006-12-08 18:40:44 +0100771 u64 m = (1ULL << n) - 1;
772
Jiri Kosinac4124c92007-11-30 11:12:58 +0100773 if (n > 32)
774 printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
775 n, current->comm);
Jiri Kosinadde58452006-12-08 18:40:44 +0100776
Jiri Kosinac4124c92007-11-30 11:12:58 +0100777 if (value > m)
778 printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
779 value, current->comm);
Jiri Kosinadde58452006-12-08 18:40:44 +0100780 WARN_ON(value > m);
781 value &= m;
782
783 report += offset >> 3;
784 offset &= 7;
785
Harvey Harrison6f0168d2008-05-16 11:00:23 +0200786 x = get_unaligned_le64(report);
787 x &= ~(m << offset);
788 x |= ((u64)value) << offset;
789 put_unaligned_le64(x, report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100790}
791
792/*
793 * Search an array for a value.
794 */
795
796static __inline__ int search(__s32 *array, __s32 value, unsigned n)
797{
798 while (n--) {
799 if (*array++ == value)
800 return 0;
801 }
802 return -1;
803}
804
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200805/**
806 * hid_match_report - check if driver's raw_event should be called
807 *
808 * @hid: hid device
809 * @report_type: type to match against
810 *
811 * compare hid->driver->report_table->report_type to report->type
812 */
813static int hid_match_report(struct hid_device *hid, struct hid_report *report)
Jiri Kosinadde58452006-12-08 18:40:44 +0100814{
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200815 const struct hid_report_id *id = hid->driver->report_table;
816
817 if (!id) /* NULL means all */
818 return 1;
819
820 for (; id->report_type != HID_TERMINATOR; id++)
821 if (id->report_type == HID_ANY_ID ||
822 id->report_type == report->type)
823 return 1;
824 return 0;
825}
826
827/**
828 * hid_match_usage - check if driver's event should be called
829 *
830 * @hid: hid device
831 * @usage: usage to match against
832 *
833 * compare hid->driver->usage_table->usage_{type,code} to
834 * usage->usage_{type,code}
835 */
836static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
837{
838 const struct hid_usage_id *id = hid->driver->usage_table;
839
840 if (!id) /* NULL means all */
841 return 1;
842
843 for (; id->usage_type != HID_ANY_ID - 1; id++)
844 if ((id->usage_hid == HID_ANY_ID ||
845 id->usage_hid == usage->hid) &&
846 (id->usage_type == HID_ANY_ID ||
847 id->usage_type == usage->type) &&
848 (id->usage_code == HID_ANY_ID ||
849 id->usage_code == usage->code))
850 return 1;
851 return 0;
852}
853
854static void hid_process_event(struct hid_device *hid, struct hid_field *field,
855 struct hid_usage *usage, __s32 value, int interrupt)
856{
857 struct hid_driver *hdrv = hid->driver;
858 int ret;
859
Jiri Kosinacd667ce2009-06-12 15:20:57 +0200860 hid_dump_input(hid, usage, value);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200861
862 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
863 ret = hdrv->event(hid, field, usage, value);
864 if (ret != 0) {
865 if (ret < 0)
866 dbg_hid("%s's event failed with %d\n",
867 hdrv->name, ret);
868 return;
869 }
870 }
871
Jiri Kosinadde58452006-12-08 18:40:44 +0100872 if (hid->claimed & HID_CLAIMED_INPUT)
873 hidinput_hid_event(hid, field, usage, value);
Jiri Kosinaaa938f72006-12-08 18:41:10 +0100874 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
875 hid->hiddev_hid_event(hid, field, usage, value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100876}
877
878/*
879 * Analyse a received field, and fetch the data from it. The field
880 * content is stored for next report processing (we do differential
881 * reporting to the layer).
882 */
883
Adrian Bunkabdff0f2008-03-31 01:53:56 +0200884static void hid_input_field(struct hid_device *hid, struct hid_field *field,
885 __u8 *data, int interrupt)
Jiri Kosinadde58452006-12-08 18:40:44 +0100886{
887 unsigned n;
888 unsigned count = field->report_count;
889 unsigned offset = field->report_offset;
890 unsigned size = field->report_size;
891 __s32 min = field->logical_minimum;
892 __s32 max = field->logical_maximum;
893 __s32 *value;
894
895 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
896 return;
897
898 for (n = 0; n < count; n++) {
899
900 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
901 extract(data, offset + n * size, size);
902
903 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
904 && value[n] >= min && value[n] <= max
905 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
906 goto exit;
907 }
908
909 for (n = 0; n < count; n++) {
910
911 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
912 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
913 continue;
914 }
915
916 if (field->value[n] >= min && field->value[n] <= max
917 && field->usage[field->value[n] - min].hid
918 && search(value, field->value[n], count))
919 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
920
921 if (value[n] >= min && value[n] <= max
922 && field->usage[value[n] - min].hid
923 && search(field->value, value[n], count))
924 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
925 }
926
927 memcpy(field->value, value, count * sizeof(__s32));
928exit:
929 kfree(value);
930}
Jiri Kosinadde58452006-12-08 18:40:44 +0100931
932/*
933 * Output the field into the report.
934 */
935
936static void hid_output_field(struct hid_field *field, __u8 *data)
937{
938 unsigned count = field->report_count;
939 unsigned offset = field->report_offset;
940 unsigned size = field->report_size;
Simon Budig46386b52007-03-12 13:52:04 +0100941 unsigned bitsused = offset + count * size;
Jiri Kosinadde58452006-12-08 18:40:44 +0100942 unsigned n;
943
Simon Budig46386b52007-03-12 13:52:04 +0100944 /* make sure the unused bits in the last byte are zeros */
945 if (count > 0 && size > 0 && (bitsused % 8) != 0)
946 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
947
Jiri Kosinadde58452006-12-08 18:40:44 +0100948 for (n = 0; n < count; n++) {
949 if (field->logical_minimum < 0) /* signed values */
950 implement(data, offset + n * size, size, s32ton(field->value[n], size));
951 else /* unsigned values */
952 implement(data, offset + n * size, size, field->value[n]);
953 }
954}
955
956/*
957 * Create a report.
958 */
959
Jiri Kosina229695e2006-12-08 18:40:53 +0100960void hid_output_report(struct hid_report *report, __u8 *data)
Jiri Kosinadde58452006-12-08 18:40:44 +0100961{
962 unsigned n;
963
964 if (report->id > 0)
965 *data++ = report->id;
966
967 for (n = 0; n < report->maxfield; n++)
968 hid_output_field(report->field[n], data);
969}
Jiri Kosina229695e2006-12-08 18:40:53 +0100970EXPORT_SYMBOL_GPL(hid_output_report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100971
972/*
973 * Set a field value. The report this field belongs to has to be
974 * created and transferred to the device, to set this value in the
975 * device.
976 */
977
978int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
979{
980 unsigned size = field->report_size;
981
Jiri Kosinacd667ce2009-06-12 15:20:57 +0200982 hid_dump_input(field->report->device, field->usage + offset, value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100983
984 if (offset >= field->report_count) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200985 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
Jiri Kosinadde58452006-12-08 18:40:44 +0100986 return -1;
987 }
988 if (field->logical_minimum < 0) {
989 if (value != snto32(s32ton(value, size), size)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200990 dbg_hid("value %d is out of range\n", value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100991 return -1;
992 }
993 }
994 field->value[offset] = value;
995 return 0;
996}
Jiri Kosina229695e2006-12-08 18:40:53 +0100997EXPORT_SYMBOL_GPL(hid_set_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100998
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200999static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1000 const u8 *data)
1001{
1002 struct hid_report *report;
1003 unsigned int n = 0; /* Normally report number is 0 */
1004
1005 /* Device uses numbered reports, data[0] is report number */
1006 if (report_enum->numbered)
1007 n = *data;
1008
1009 report = report_enum->report_id_hash[n];
1010 if (report == NULL)
1011 dbg_hid("undefined report_id %u received\n", n);
1012
1013 return report;
1014}
1015
1016void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1017 int interrupt)
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001018{
1019 struct hid_report_enum *report_enum = hid->report_enum + type;
1020 struct hid_report *report;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001021 unsigned int a;
1022 int rsize, csize = size;
1023 u8 *cdata = data;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001024
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001025 report = hid_get_report(report_enum, data);
1026 if (!report)
1027 return;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001028
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001029 if (report_enum->numbered) {
1030 cdata++;
1031 csize--;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001032 }
1033
1034 rsize = ((report->size - 1) >> 3) + 1;
1035
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001036 if (csize < rsize) {
1037 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1038 csize, rsize);
1039 memset(cdata + csize, 0, rsize - csize);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001040 }
1041
1042 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1043 hid->hiddev_report_event(hid, report);
Jiri Kosinab54ec3c2008-03-28 14:11:22 +01001044 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1045 /* numbered reports need to be passed with the report num */
1046 if (report_enum->numbered)
1047 hidraw_report_event(hid, data - 1, size + 1);
1048 else
1049 hidraw_report_event(hid, data, size);
1050 }
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001051
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001052 for (a = 0; a < report->maxfield; a++)
1053 hid_input_field(hid, report->field[a], cdata, interrupt);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001054
1055 if (hid->claimed & HID_CLAIMED_INPUT)
1056 hidinput_report_event(hid, report);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001057}
1058EXPORT_SYMBOL_GPL(hid_report_raw_event);
1059
1060/**
1061 * hid_input_report - report data from lower layer (usb, bt...)
1062 *
1063 * @hid: hid device
1064 * @type: HID report type (HID_*_REPORT)
1065 * @data: report contents
1066 * @size: size of data parameter
Jiri Kosinaff9b00a2009-10-01 16:03:13 +02001067 * @interrupt: distinguish between interrupt and control transfers
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001068 *
1069 * This is data entry for lower layers.
1070 */
1071int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1072{
Julia Lawall76c317d2009-07-19 17:26:13 +02001073 struct hid_report_enum *report_enum;
1074 struct hid_driver *hdrv;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001075 struct hid_report *report;
Jiri Kosinacd667ce2009-06-12 15:20:57 +02001076 char *buf;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001077 unsigned int i;
1078 int ret;
1079
1080 if (!hid || !hid->driver)
1081 return -ENODEV;
Julia Lawall76c317d2009-07-19 17:26:13 +02001082 report_enum = hid->report_enum + type;
1083 hdrv = hid->driver;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001084
1085 if (!size) {
1086 dbg_hid("empty report\n");
1087 return -1;
1088 }
1089
Jiri Kosinad1ff6522009-09-15 11:59:49 +02001090 buf = kmalloc(sizeof(char) * HID_DEBUG_BUFSIZE, GFP_ATOMIC);
Jiri Kosinacd667ce2009-06-12 15:20:57 +02001091
1092 if (!buf) {
1093 report = hid_get_report(report_enum, data);
1094 goto nomem;
1095 }
1096
1097 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1098 "\nreport (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1099 hid_debug_event(hid, buf);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001100
1101 report = hid_get_report(report_enum, data);
Jiri Kosina55dba522009-06-26 10:50:12 +02001102 if (!report) {
1103 kfree(buf);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001104 return -1;
Jiri Kosina55dba522009-06-26 10:50:12 +02001105 }
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001106
1107 /* dump the report */
Jiri Kosinacd667ce2009-06-12 15:20:57 +02001108 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1109 "report %d (size %u) = ", report->id, size);
1110 hid_debug_event(hid, buf);
1111 for (i = 0; i < size; i++) {
1112 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1113 " %02x", data[i]);
1114 hid_debug_event(hid, buf);
1115 }
1116 hid_debug_event(hid, "\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001117
Jiri Kosinacd667ce2009-06-12 15:20:57 +02001118 kfree(buf);
1119
1120nomem:
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001121 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1122 ret = hdrv->raw_event(hid, report, data, size);
1123 if (ret != 0)
1124 return ret < 0 ? ret : 0;
1125 }
1126
1127 hid_report_raw_event(hid, type, data, size, interrupt);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001128
1129 return 0;
1130}
1131EXPORT_SYMBOL_GPL(hid_input_report);
1132
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001133static bool hid_match_one_id(struct hid_device *hdev,
1134 const struct hid_device_id *id)
1135{
1136 return id->bus == hdev->bus &&
1137 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1138 (id->product == HID_ANY_ID || id->product == hdev->product);
1139}
1140
1141static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1142 const struct hid_device_id *id)
1143{
1144 for (; id->bus; id++)
1145 if (hid_match_one_id(hdev, id))
1146 return id;
1147
1148 return NULL;
1149}
1150
1151static const struct hid_device_id hid_hiddev_list[] = {
Richard Hughesc0bd6a42008-08-27 11:19:37 +02001152 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1153 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001154 { }
1155};
1156
1157static bool hid_hiddev(struct hid_device *hdev)
1158{
1159 return !!hid_match_id(hdev, hid_hiddev_list);
1160}
1161
Jiri Slaby93c10132008-06-27 00:04:24 +02001162int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1163{
1164 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1165 "Joystick", "Gamepad", "Keyboard", "Keypad",
1166 "Multi-Axis Controller"
1167 };
1168 const char *type, *bus;
1169 char buf[64];
1170 unsigned int i;
1171 int len;
1172
1173 if (hdev->bus != BUS_USB)
1174 connect_mask &= ~HID_CONNECT_HIDDEV;
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001175 if (hid_hiddev(hdev))
1176 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Jiri Slaby93c10132008-06-27 00:04:24 +02001177
1178 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1179 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1180 hdev->claimed |= HID_CLAIMED_INPUT;
1181 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1182 !hdev->hiddev_connect(hdev,
1183 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1184 hdev->claimed |= HID_CLAIMED_HIDDEV;
1185 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1186 hdev->claimed |= HID_CLAIMED_HIDRAW;
1187
1188 if (!hdev->claimed) {
1189 dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
1190 "hidraw\n");
1191 return -ENODEV;
1192 }
1193
1194 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1195 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1196 hdev->ff_init(hdev);
1197
1198 len = 0;
1199 if (hdev->claimed & HID_CLAIMED_INPUT)
1200 len += sprintf(buf + len, "input");
1201 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1202 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1203 hdev->minor);
1204 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1205 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1206 ((struct hidraw *)hdev->hidraw)->minor);
1207
1208 type = "Device";
1209 for (i = 0; i < hdev->maxcollection; i++) {
1210 struct hid_collection *col = &hdev->collection[i];
1211 if (col->type == HID_COLLECTION_APPLICATION &&
1212 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1213 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1214 type = types[col->usage & 0xffff];
1215 break;
1216 }
1217 }
1218
1219 switch (hdev->bus) {
1220 case BUS_USB:
1221 bus = "USB";
1222 break;
1223 case BUS_BLUETOOTH:
1224 bus = "BLUETOOTH";
1225 break;
1226 default:
1227 bus = "<UNKNOWN>";
1228 }
1229
1230 dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1231 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1232 type, hdev->name, hdev->phys);
1233
1234 return 0;
1235}
1236EXPORT_SYMBOL_GPL(hid_connect);
1237
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001238void hid_disconnect(struct hid_device *hdev)
1239{
1240 if (hdev->claimed & HID_CLAIMED_INPUT)
1241 hidinput_disconnect(hdev);
1242 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1243 hdev->hiddev_disconnect(hdev);
1244 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1245 hidraw_disconnect(hdev);
1246}
1247EXPORT_SYMBOL_GPL(hid_disconnect);
1248
Jiri Kosinabae7eb32009-01-28 23:06:37 +01001249/* a list of devices for which there is a specialized driver on HID bus */
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001250static const struct hid_device_id hid_blacklist[] = {
Jiri Slaby14a21cd2008-06-23 23:31:09 +02001251 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
1252 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
Jiri Kosinadf9bcac2008-10-14 22:45:40 +02001253 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001254 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1255 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1256 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1257 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1258 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1259 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1260 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1261 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1262 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1263 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1264 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1265 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1266 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
Ryan Finniefef3f572009-03-05 10:18:01 +01001267 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1268 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1269 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001270 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1271 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1272 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1273 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1274 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1275 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
Jan Scholzee8a1a02008-11-26 15:33:45 +01001276 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1277 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1278 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001279 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1280 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1281 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1282 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1283 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1284 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
Henrik Rydberga96d6ef2008-11-04 20:03:45 +01001285 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1286 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1287 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001288 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1289 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
Jiri Slabyb5635b12008-06-24 23:24:57 +02001290 { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
Jiri Slaby3b239cd2008-06-24 20:42:25 +02001291 { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
Jiri Slabyfcfacfd2008-06-24 22:48:52 +02001292 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
Jiri Slaby0f221322008-06-23 22:54:08 +02001293 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
1294 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
Jiri Kosinae8d0eab2009-12-02 22:54:11 +01001295 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) },
Jiri Slaby0f221322008-06-23 22:54:08 +02001296 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
Richard Walmsley3f866fb2009-03-04 22:12:04 +13001297 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
Jiri Slaby1f243e32008-06-24 21:11:21 +02001298 { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
Jiri Kosina5181e592008-11-17 01:44:38 +01001299 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
Jiri Kosina578f3a32008-11-20 15:55:38 +01001300 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
Jiri Kosina5181e592008-11-17 01:44:38 +01001301 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
Lukasz Lubojanski42859e02008-12-11 22:07:59 +01001302 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
Jiri Slaby949f8fe2008-07-24 23:35:13 +02001303 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
Jiri Kosina1e093202008-10-17 11:52:23 +02001304 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
Jiri Kosinafdf93aa2009-03-04 16:09:40 +01001305 { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
Jiri Kosina79422742009-03-11 11:43:27 +01001306 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
Jiri Slabyb5635b12008-06-24 23:24:57 +02001307 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
Jiri Slaby5f22a792008-05-16 11:49:19 +02001308 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1309 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1310 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1311 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1312 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1313 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1314 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
Jiri Slaby5f22a792008-05-16 11:49:19 +02001315 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1316 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
Jiri Slaby5f22a792008-05-16 11:49:19 +02001317 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1318 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
Jiri Slaby606bd0a2008-07-04 23:06:45 +02001319 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1320 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1321 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
Jiri Kosinafd30ea82009-06-23 12:11:31 +02001322 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
Jiri Slaby606bd0a2008-07-04 23:06:45 +02001323 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1324 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1325 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
Christophe Borivant243b7062009-04-17 11:39:39 +02001326 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
Jiri Slaby606bd0a2008-07-04 23:06:45 +02001327 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
Jiri Kosina24985cf2009-11-13 10:45:53 +01001328 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
1329 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) },
Jiri Slaby78a849a682008-06-20 21:26:11 +02001330 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
1331 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
1332 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
1333 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1334 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
Jiri Slaby3b8006e2008-06-25 00:07:50 +02001335 { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
Rafi Rubin94011f92008-11-19 15:54:46 +01001336 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
Jiri Slaby1e762532008-06-24 23:46:21 +02001337 { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
Jiri Slaby980a3da2008-06-25 22:31:48 +02001338 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001339 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001340 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
Jiri Slaby90231e72008-06-23 21:56:07 +02001341 { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
Anssi Hannuladaedb3d2009-02-14 11:45:05 +02001342 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1343 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
Ruben Aos Garralda7a84b132009-06-29 09:41:29 +02001344 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
1345 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
Anssi Hannuladaedb3d2009-02-14 11:45:05 +02001346 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1347 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
Lev Babievf14f5262009-01-04 00:36:56 +01001348 { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
Bruno Premont711a6802009-07-13 14:19:58 +02001349 { HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
Jussi Kivilinnafac733f2009-05-13 11:54:38 +03001350 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
Bastien Noceraca2dcd42009-05-11 17:18:12 +02001351 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
Anssi Hannuladaedb3d2009-02-14 11:45:05 +02001352 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1353 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001354
Jiri Slaby78a849a682008-06-20 21:26:11 +02001355 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001356 { }
1357};
1358
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001359struct hid_dynid {
1360 struct list_head list;
1361 struct hid_device_id id;
1362};
1363
1364/**
1365 * store_new_id - add a new HID device ID to this driver and re-probe devices
1366 * @driver: target device driver
1367 * @buf: buffer for scanning device ID data
1368 * @count: input size
1369 *
1370 * Adds a new dynamic hid device ID to this driver,
1371 * and causes the driver to probe for all devices again.
1372 */
1373static ssize_t store_new_id(struct device_driver *drv, const char *buf,
1374 size_t count)
1375{
1376 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1377 struct hid_dynid *dynid;
1378 __u32 bus, vendor, product;
1379 unsigned long driver_data = 0;
1380 int ret;
1381
1382 ret = sscanf(buf, "%x %x %x %lx",
1383 &bus, &vendor, &product, &driver_data);
1384 if (ret < 3)
1385 return -EINVAL;
1386
1387 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1388 if (!dynid)
1389 return -ENOMEM;
1390
1391 dynid->id.bus = bus;
1392 dynid->id.vendor = vendor;
1393 dynid->id.product = product;
1394 dynid->id.driver_data = driver_data;
1395
1396 spin_lock(&hdrv->dyn_lock);
1397 list_add_tail(&dynid->list, &hdrv->dyn_list);
1398 spin_unlock(&hdrv->dyn_lock);
1399
1400 ret = 0;
1401 if (get_driver(&hdrv->driver)) {
1402 ret = driver_attach(&hdrv->driver);
1403 put_driver(&hdrv->driver);
1404 }
1405
1406 return ret ? : count;
1407}
1408static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1409
1410static void hid_free_dynids(struct hid_driver *hdrv)
1411{
1412 struct hid_dynid *dynid, *n;
1413
1414 spin_lock(&hdrv->dyn_lock);
1415 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1416 list_del(&dynid->list);
1417 kfree(dynid);
1418 }
1419 spin_unlock(&hdrv->dyn_lock);
1420}
1421
1422static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1423 struct hid_driver *hdrv)
1424{
1425 struct hid_dynid *dynid;
1426
1427 spin_lock(&hdrv->dyn_lock);
1428 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1429 if (hid_match_one_id(hdev, &dynid->id)) {
1430 spin_unlock(&hdrv->dyn_lock);
1431 return &dynid->id;
1432 }
1433 }
1434 spin_unlock(&hdrv->dyn_lock);
1435
1436 return hid_match_id(hdev, hdrv->id_table);
1437}
1438
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001439static int hid_bus_match(struct device *dev, struct device_driver *drv)
1440{
1441 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1442 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1443
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001444 if (!hid_match_device(hdev, hdrv))
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001445 return 0;
1446
1447 /* generic wants all non-blacklisted */
1448 if (!strncmp(hdrv->name, "generic-", 8))
1449 return !hid_match_id(hdev, hid_blacklist);
1450
1451 return 1;
1452}
1453
1454static int hid_device_probe(struct device *dev)
1455{
1456 struct hid_driver *hdrv = container_of(dev->driver,
1457 struct hid_driver, driver);
1458 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1459 const struct hid_device_id *id;
1460 int ret = 0;
1461
1462 if (!hdev->driver) {
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001463 id = hid_match_device(hdev, hdrv);
Jiri Slabyc500c972008-05-16 11:49:16 +02001464 if (id == NULL)
1465 return -ENODEV;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001466
Jiri Slabyc500c972008-05-16 11:49:16 +02001467 hdev->driver = hdrv;
1468 if (hdrv->probe) {
1469 ret = hdrv->probe(hdev, id);
1470 } else { /* default probe */
1471 ret = hid_parse(hdev);
1472 if (!ret)
Jiri Slaby93c10132008-06-27 00:04:24 +02001473 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001474 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001475 if (ret)
1476 hdev->driver = NULL;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001477 }
1478 return ret;
1479}
1480
1481static int hid_device_remove(struct device *dev)
1482{
1483 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1484 struct hid_driver *hdrv = hdev->driver;
1485
1486 if (hdrv) {
1487 if (hdrv->remove)
1488 hdrv->remove(hdev);
Jiri Slabyc500c972008-05-16 11:49:16 +02001489 else /* default remove */
1490 hid_hw_stop(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001491 hdev->driver = NULL;
1492 }
1493
1494 return 0;
1495}
1496
1497static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1498{
1499 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1500
1501 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1502 hdev->bus, hdev->vendor, hdev->product))
1503 return -ENOMEM;
1504
1505 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1506 return -ENOMEM;
1507
1508 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1509 return -ENOMEM;
1510
1511 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1512 return -ENOMEM;
1513
1514 if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1515 hdev->bus, hdev->vendor, hdev->product))
1516 return -ENOMEM;
1517
1518 return 0;
1519}
1520
1521static struct bus_type hid_bus_type = {
1522 .name = "hid",
1523 .match = hid_bus_match,
1524 .probe = hid_device_probe,
1525 .remove = hid_device_remove,
1526 .uevent = hid_uevent,
1527};
1528
Jiri Kosinabae7eb32009-01-28 23:06:37 +01001529/* a list of devices that shouldn't be handled by HID core at all */
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001530static const struct hid_device_id hid_ignore_list[] = {
1531 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1532 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1533 { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1534 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1535 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1536 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1537 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1538 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1539 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1540 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1541 { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1542 { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1543 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
Jiri Kosinaac2d9892008-10-20 12:37:43 +02001544 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2)},
Alexey Klimov62a56582008-11-13 05:44:50 +03001545 { HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001546 { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1547 { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1548 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1549 { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1550 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1551 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
Alexey Klimov5f6108c2008-12-08 12:40:14 +01001552 { HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001553 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1554 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1555 { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1556 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1557 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1558 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1559 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1560 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1561 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1562 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1563 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1564 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1565 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1566 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1567 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1568 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1569 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1570 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1571 { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1572 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1573 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1574 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1575 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1576 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1577 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1578 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1579 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1580 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1581 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1582 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1583 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1584 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1585 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1586 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1587 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1588 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1589 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1590 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1591 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1592 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1593 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1594 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1595 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1596 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1597 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1598 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1599 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1600 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1601 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1602 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1603 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1604 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1605 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1606 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1607 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1608 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1609 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1610 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1611 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1612 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1613 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1614 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1615 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1616 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1617 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1618 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1619 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1620 { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1621 { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
Alexey Klimovc91c21c2008-11-13 10:36:11 +01001622 { HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001623 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
Lamarque Vieira Souzaeb8141c2009-10-02 15:04:44 +02001624 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_KYE, 0x0058) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001625 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1626 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1627 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1628 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1629 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1630 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1631 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1632 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1633 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1634 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1635 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1636 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1637 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1638 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1639 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1640 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001641 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1642 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1643 { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1644 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1645 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1646 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1647 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1648 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1649 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1650 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1651 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1652 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1653 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1654 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1655 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1656 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1657 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
Henning Glawe4cfae3e2009-08-02 22:18:12 +02001658 { HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
Michael Tokarev35cfd1d2009-02-01 16:11:04 +01001659 { HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
Remi Cattiaud1d3a5f2008-09-09 01:39:33 +02001660 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY1) },
1661 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY2) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001662 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1663 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1664 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1665 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1666 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1667 { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1668 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1669 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1670 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1671 { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1672 { }
1673};
1674
Jiri Slabyb4d8e472008-10-22 14:47:18 +02001675/**
1676 * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1677 *
1678 * There are composite devices for which we want to ignore only a certain
1679 * interface. This is a list of devices for which only the mouse interface will
1680 * be ignored. This allows a dedicated driver to take care of the interface.
1681 */
1682static const struct hid_device_id hid_mouse_ignore_list[] = {
1683 /* appletouch driver */
1684 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1685 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1686 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1687 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1688 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1689 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1690 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1691 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1692 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1693 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1694 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1695 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1696 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1697 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1698 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1699 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1700 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1701 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1702 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1703 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
Jiri Kosinaac26fca2008-11-20 11:27:02 +01001704 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1705 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1706 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
Jiri Slabyb4d8e472008-10-22 14:47:18 +02001707 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1708 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1709 { }
1710};
1711
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001712static bool hid_ignore(struct hid_device *hdev)
1713{
1714 switch (hdev->vendor) {
1715 case USB_VENDOR_ID_CODEMERCS:
1716 /* ignore all Code Mercenaries IOWarrior devices */
1717 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1718 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1719 return true;
1720 break;
1721 case USB_VENDOR_ID_LOGITECH:
1722 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1723 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1724 return true;
1725 break;
Jarod Wilson31f7fd72009-07-31 10:56:36 -04001726 case USB_VENDOR_ID_SOUNDGRAPH:
1727 if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
1728 hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
1729 return true;
1730 break;
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001731 }
1732
Jiri Slabyb4d8e472008-10-22 14:47:18 +02001733 if (hdev->type == HID_TYPE_USBMOUSE &&
1734 hid_match_id(hdev, hid_mouse_ignore_list))
1735 return true;
1736
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001737 return !!hid_match_id(hdev, hid_ignore_list);
1738}
1739
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001740int hid_add_device(struct hid_device *hdev)
1741{
1742 static atomic_t id = ATOMIC_INIT(0);
1743 int ret;
1744
1745 if (WARN_ON(hdev->status & HID_STAT_ADDED))
1746 return -EBUSY;
1747
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001748 /* we need to kill them here, otherwise they will stay allocated to
1749 * wait for coming driver */
1750 if (hid_ignore(hdev))
1751 return -ENODEV;
1752
Kay Sievers6bbe5862008-10-31 00:12:32 +01001753 /* XXX hack, any other cleaner solution after the driver core
1754 * is converted to allow more than 20 bytes as the device name? */
1755 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
1756 hdev->vendor, hdev->product, atomic_inc_return(&id));
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001757
1758 ret = device_add(&hdev->dev);
1759 if (!ret)
1760 hdev->status |= HID_STAT_ADDED;
1761
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001762 hid_debug_register(hdev, dev_name(&hdev->dev));
1763
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001764 return ret;
1765}
1766EXPORT_SYMBOL_GPL(hid_add_device);
1767
1768/**
1769 * hid_allocate_device - allocate new hid device descriptor
1770 *
1771 * Allocate and initialize hid device, so that hid_destroy_device might be
1772 * used to free it.
1773 *
1774 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1775 * error value.
1776 */
1777struct hid_device *hid_allocate_device(void)
1778{
1779 struct hid_device *hdev;
1780 unsigned int i;
1781 int ret = -ENOMEM;
1782
1783 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1784 if (hdev == NULL)
1785 return ERR_PTR(ret);
1786
1787 device_initialize(&hdev->dev);
1788 hdev->dev.release = hid_device_release;
1789 hdev->dev.bus = &hid_bus_type;
1790
1791 hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1792 sizeof(struct hid_collection), GFP_KERNEL);
1793 if (hdev->collection == NULL)
1794 goto err;
1795 hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1796
1797 for (i = 0; i < HID_REPORT_TYPES; i++)
1798 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1799
Jiri Kosinacd667ce2009-06-12 15:20:57 +02001800 init_waitqueue_head(&hdev->debug_wait);
1801 INIT_LIST_HEAD(&hdev->debug_list);
1802
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001803 return hdev;
1804err:
1805 put_device(&hdev->dev);
1806 return ERR_PTR(ret);
1807}
1808EXPORT_SYMBOL_GPL(hid_allocate_device);
1809
1810static void hid_remove_device(struct hid_device *hdev)
1811{
1812 if (hdev->status & HID_STAT_ADDED) {
1813 device_del(&hdev->dev);
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001814 hid_debug_unregister(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001815 hdev->status &= ~HID_STAT_ADDED;
1816 }
1817}
1818
1819/**
1820 * hid_destroy_device - free previously allocated device
1821 *
1822 * @hdev: hid device
1823 *
1824 * If you allocate hid_device through hid_allocate_device, you should ever
1825 * free by this function.
1826 */
1827void hid_destroy_device(struct hid_device *hdev)
1828{
1829 hid_remove_device(hdev);
1830 put_device(&hdev->dev);
1831}
1832EXPORT_SYMBOL_GPL(hid_destroy_device);
1833
1834int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1835 const char *mod_name)
1836{
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001837 int ret;
1838
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001839 hdrv->driver.name = hdrv->name;
1840 hdrv->driver.bus = &hid_bus_type;
1841 hdrv->driver.owner = owner;
1842 hdrv->driver.mod_name = mod_name;
1843
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001844 INIT_LIST_HEAD(&hdrv->dyn_list);
1845 spin_lock_init(&hdrv->dyn_lock);
1846
1847 ret = driver_register(&hdrv->driver);
1848 if (ret)
1849 return ret;
1850
1851 ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
1852 if (ret)
1853 driver_unregister(&hdrv->driver);
1854
1855 return ret;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001856}
1857EXPORT_SYMBOL_GPL(__hid_register_driver);
1858
1859void hid_unregister_driver(struct hid_driver *hdrv)
1860{
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001861 driver_remove_file(&hdrv->driver, &driver_attr_new_id);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001862 driver_unregister(&hdrv->driver);
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001863 hid_free_dynids(hdrv);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001864}
1865EXPORT_SYMBOL_GPL(hid_unregister_driver);
1866
Oliver Neukum0361a282008-12-17 15:38:03 +01001867int hid_check_keys_pressed(struct hid_device *hid)
1868{
1869 struct hid_input *hidinput;
1870 int i;
1871
Jiri Kosinae5288eb2009-05-02 00:02:57 +02001872 if (!(hid->claimed & HID_CLAIMED_INPUT))
1873 return 0;
1874
Oliver Neukum0361a282008-12-17 15:38:03 +01001875 list_for_each_entry(hidinput, &hid->inputs, list) {
1876 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
1877 if (hidinput->input->key[i])
1878 return 1;
1879 }
1880
1881 return 0;
1882}
1883
1884EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
1885
Jiri Kosina86166b72007-05-14 09:57:40 +02001886static int __init hid_init(void)
1887{
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001888 int ret;
1889
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001890 if (hid_debug)
Jiri Kosinacd667ce2009-06-12 15:20:57 +02001891 printk(KERN_WARNING "HID: hid_debug is now used solely for parser and driver debugging.\n"
1892 "HID: debugfs is now used for inspecting the device (report descriptor, reports)\n");
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001893
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001894 ret = bus_register(&hid_bus_type);
1895 if (ret) {
1896 printk(KERN_ERR "HID: can't register hid bus\n");
1897 goto err;
1898 }
1899
1900 ret = hidraw_init();
1901 if (ret)
1902 goto err_bus;
1903
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001904 hid_debug_init();
1905
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001906 return 0;
1907err_bus:
1908 bus_unregister(&hid_bus_type);
1909err:
1910 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +02001911}
1912
1913static void __exit hid_exit(void)
1914{
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001915 hid_debug_exit();
Jiri Kosina86166b72007-05-14 09:57:40 +02001916 hidraw_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001917 bus_unregister(&hid_bus_type);
Jiri Kosina86166b72007-05-14 09:57:40 +02001918}
1919
1920module_init(hid_init);
1921module_exit(hid_exit);
1922
Jiri Kosina88adb722009-10-02 18:29:34 +02001923MODULE_AUTHOR("Andreas Gal");
1924MODULE_AUTHOR("Vojtech Pavlik");
1925MODULE_AUTHOR("Jiri Kosina");
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001926MODULE_LICENSE(DRIVER_LICENSE);
1927