blob: d4317db85b54bccb453339acdc8172a79c2c67ff [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
42#define DRIVER_VERSION "v2.6"
Jiri Kosinaf142b3a42007-04-16 11:29:28 +020043#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
Jiri Kosina53149802007-01-09 13:24:25 +010044#define DRIVER_DESC "HID core driver"
Jiri Kosinadde58452006-12-08 18:40:44 +010045#define DRIVER_LICENSE "GPL"
46
Jiri Kosina58037eb2007-05-30 15:07:13 +020047int hid_debug = 0;
Anssi Hannula377e10f2008-03-22 23:50:13 +010048module_param_named(debug, hid_debug, int, 0600);
49MODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
Jiri Kosina58037eb2007-05-30 15:07:13 +020050EXPORT_SYMBOL_GPL(hid_debug);
Jiri Kosina58037eb2007-05-30 15:07:13 +020051
Jiri Kosinadde58452006-12-08 18:40:44 +010052/*
Jiri Kosinadde58452006-12-08 18:40:44 +010053 * Register a new report for a device.
54 */
55
56static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
57{
58 struct hid_report_enum *report_enum = device->report_enum + type;
59 struct hid_report *report;
60
61 if (report_enum->report_id_hash[id])
62 return report_enum->report_id_hash[id];
63
64 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
65 return NULL;
66
67 if (id != 0)
68 report_enum->numbered = 1;
69
70 report->id = id;
71 report->type = type;
72 report->size = 0;
73 report->device = device;
74 report_enum->report_id_hash[id] = report;
75
76 list_add_tail(&report->list, &report_enum->report_list);
77
78 return report;
79}
80
81/*
82 * Register a new field for this report.
83 */
84
85static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
86{
87 struct hid_field *field;
88
89 if (report->maxfield == HID_MAX_FIELDS) {
Jiri Kosina58037eb2007-05-30 15:07:13 +020090 dbg_hid("too many fields in report\n");
Jiri Kosinadde58452006-12-08 18:40:44 +010091 return NULL;
92 }
93
94 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
95 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
96
97 field->index = report->maxfield++;
98 report->field[field->index] = field;
99 field->usage = (struct hid_usage *)(field + 1);
Jiri Slaby282bfd42008-03-28 17:06:41 +0100100 field->value = (s32 *)(field->usage + usages);
Jiri Kosinadde58452006-12-08 18:40:44 +0100101 field->report = report;
102
103 return field;
104}
105
106/*
107 * Open a collection. The type/usage is pushed on the stack.
108 */
109
110static int open_collection(struct hid_parser *parser, unsigned type)
111{
112 struct hid_collection *collection;
113 unsigned usage;
114
115 usage = parser->local.usage[0];
116
117 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200118 dbg_hid("collection stack overflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100119 return -1;
120 }
121
122 if (parser->device->maxcollection == parser->device->collection_size) {
123 collection = kmalloc(sizeof(struct hid_collection) *
124 parser->device->collection_size * 2, GFP_KERNEL);
125 if (collection == NULL) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200126 dbg_hid("failed to reallocate collection array\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100127 return -1;
128 }
129 memcpy(collection, parser->device->collection,
130 sizeof(struct hid_collection) *
131 parser->device->collection_size);
132 memset(collection + parser->device->collection_size, 0,
133 sizeof(struct hid_collection) *
134 parser->device->collection_size);
135 kfree(parser->device->collection);
136 parser->device->collection = collection;
137 parser->device->collection_size *= 2;
138 }
139
140 parser->collection_stack[parser->collection_stack_ptr++] =
141 parser->device->maxcollection;
142
143 collection = parser->device->collection +
144 parser->device->maxcollection++;
145 collection->type = type;
146 collection->usage = usage;
147 collection->level = parser->collection_stack_ptr - 1;
148
149 if (type == HID_COLLECTION_APPLICATION)
150 parser->device->maxapplication++;
151
152 return 0;
153}
154
155/*
156 * Close a collection.
157 */
158
159static int close_collection(struct hid_parser *parser)
160{
161 if (!parser->collection_stack_ptr) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200162 dbg_hid("collection stack underflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100163 return -1;
164 }
165 parser->collection_stack_ptr--;
166 return 0;
167}
168
169/*
170 * Climb up the stack, search for the specified collection type
171 * and return the usage.
172 */
173
174static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
175{
176 int n;
177 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
178 if (parser->device->collection[parser->collection_stack[n]].type == type)
179 return parser->device->collection[parser->collection_stack[n]].usage;
180 return 0; /* we know nothing about this usage type */
181}
182
183/*
184 * Add a usage to the temporary parser table.
185 */
186
187static int hid_add_usage(struct hid_parser *parser, unsigned usage)
188{
189 if (parser->local.usage_index >= HID_MAX_USAGES) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200190 dbg_hid("usage index exceeded\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100191 return -1;
192 }
193 parser->local.usage[parser->local.usage_index] = usage;
194 parser->local.collection_index[parser->local.usage_index] =
195 parser->collection_stack_ptr ?
196 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
197 parser->local.usage_index++;
198 return 0;
199}
200
201/*
202 * Register a new field for this report.
203 */
204
205static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
206{
207 struct hid_report *report;
208 struct hid_field *field;
209 int usages;
210 unsigned offset;
211 int i;
212
213 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200214 dbg_hid("hid_register_report failed\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100215 return -1;
216 }
217
218 if (parser->global.logical_maximum < parser->global.logical_minimum) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200219 dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
Jiri Kosinadde58452006-12-08 18:40:44 +0100220 return -1;
221 }
222
223 offset = report->size;
224 report->size += parser->global.report_size * parser->global.report_count;
225
226 if (!parser->local.usage_index) /* Ignore padding fields */
227 return 0;
228
229 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
230
231 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
232 return 0;
233
234 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
235 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
236 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
237
238 for (i = 0; i < usages; i++) {
239 int j = i;
240 /* Duplicate the last usage we parsed if we have excess values */
241 if (i >= parser->local.usage_index)
242 j = parser->local.usage_index - 1;
243 field->usage[i].hid = parser->local.usage[j];
244 field->usage[i].collection_index =
245 parser->local.collection_index[j];
246 }
247
248 field->maxusage = usages;
249 field->flags = flags;
250 field->report_offset = offset;
251 field->report_type = report_type;
252 field->report_size = parser->global.report_size;
253 field->report_count = parser->global.report_count;
254 field->logical_minimum = parser->global.logical_minimum;
255 field->logical_maximum = parser->global.logical_maximum;
256 field->physical_minimum = parser->global.physical_minimum;
257 field->physical_maximum = parser->global.physical_maximum;
258 field->unit_exponent = parser->global.unit_exponent;
259 field->unit = parser->global.unit;
260
261 return 0;
262}
263
264/*
265 * Read data value from item.
266 */
267
268static u32 item_udata(struct hid_item *item)
269{
270 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200271 case 1: return item->data.u8;
272 case 2: return item->data.u16;
273 case 4: return item->data.u32;
Jiri Kosinadde58452006-12-08 18:40:44 +0100274 }
275 return 0;
276}
277
278static s32 item_sdata(struct hid_item *item)
279{
280 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200281 case 1: return item->data.s8;
282 case 2: return item->data.s16;
283 case 4: return item->data.s32;
Jiri Kosinadde58452006-12-08 18:40:44 +0100284 }
285 return 0;
286}
287
288/*
289 * Process a global item.
290 */
291
292static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
293{
294 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200295 case HID_GLOBAL_ITEM_TAG_PUSH:
Jiri Kosinadde58452006-12-08 18:40:44 +0100296
Jiri Slaby880d29f2008-06-18 23:55:41 +0200297 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
298 dbg_hid("global enviroment stack overflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100299 return -1;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200300 }
301
302 memcpy(parser->global_stack + parser->global_stack_ptr++,
303 &parser->global, sizeof(struct hid_global));
304 return 0;
305
306 case HID_GLOBAL_ITEM_TAG_POP:
307
308 if (!parser->global_stack_ptr) {
309 dbg_hid("global enviroment stack underflow\n");
310 return -1;
311 }
312
313 memcpy(&parser->global, parser->global_stack +
314 --parser->global_stack_ptr, sizeof(struct hid_global));
315 return 0;
316
317 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
318 parser->global.usage_page = item_udata(item);
319 return 0;
320
321 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
322 parser->global.logical_minimum = item_sdata(item);
323 return 0;
324
325 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
326 if (parser->global.logical_minimum < 0)
327 parser->global.logical_maximum = item_sdata(item);
328 else
329 parser->global.logical_maximum = item_udata(item);
330 return 0;
331
332 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
333 parser->global.physical_minimum = item_sdata(item);
334 return 0;
335
336 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
337 if (parser->global.physical_minimum < 0)
338 parser->global.physical_maximum = item_sdata(item);
339 else
340 parser->global.physical_maximum = item_udata(item);
341 return 0;
342
343 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
344 parser->global.unit_exponent = item_sdata(item);
345 return 0;
346
347 case HID_GLOBAL_ITEM_TAG_UNIT:
348 parser->global.unit = item_udata(item);
349 return 0;
350
351 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
352 parser->global.report_size = item_udata(item);
353 if (parser->global.report_size > 32) {
354 dbg_hid("invalid report_size %d\n",
355 parser->global.report_size);
356 return -1;
357 }
358 return 0;
359
360 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
361 parser->global.report_count = item_udata(item);
362 if (parser->global.report_count > HID_MAX_USAGES) {
363 dbg_hid("invalid report_count %d\n",
364 parser->global.report_count);
365 return -1;
366 }
367 return 0;
368
369 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
370 parser->global.report_id = item_udata(item);
371 if (parser->global.report_id == 0) {
372 dbg_hid("report_id 0 is invalid\n");
373 return -1;
374 }
375 return 0;
376
377 default:
378 dbg_hid("unknown global tag 0x%x\n", item->tag);
379 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100380 }
381}
382
383/*
384 * Process a local item.
385 */
386
387static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
388{
389 __u32 data;
390 unsigned n;
391
392 if (item->size == 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200393 dbg_hid("item data expected for local item\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100394 return -1;
395 }
396
397 data = item_udata(item);
398
399 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200400 case HID_LOCAL_ITEM_TAG_DELIMITER:
Jiri Kosinadde58452006-12-08 18:40:44 +0100401
Jiri Slaby880d29f2008-06-18 23:55:41 +0200402 if (data) {
403 /*
404 * We treat items before the first delimiter
405 * as global to all usage sets (branch 0).
406 * In the moment we process only these global
407 * items and the first delimiter set.
408 */
409 if (parser->local.delimiter_depth != 0) {
410 dbg_hid("nested delimiters\n");
411 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100412 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200413 parser->local.delimiter_depth++;
414 parser->local.delimiter_branch++;
415 } else {
416 if (parser->local.delimiter_depth < 1) {
417 dbg_hid("bogus close delimiter\n");
418 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100419 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200420 parser->local.delimiter_depth--;
421 }
422 return 1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100423
Jiri Slaby880d29f2008-06-18 23:55:41 +0200424 case HID_LOCAL_ITEM_TAG_USAGE:
Jiri Kosinadde58452006-12-08 18:40:44 +0100425
Jiri Slaby880d29f2008-06-18 23:55:41 +0200426 if (parser->local.delimiter_branch > 1) {
427 dbg_hid("alternative usage ignored\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100428 return 0;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200429 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100430
Jiri Slaby880d29f2008-06-18 23:55:41 +0200431 if (item->size <= 2)
432 data = (parser->global.usage_page << 16) + data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100433
Jiri Slaby880d29f2008-06-18 23:55:41 +0200434 return hid_add_usage(parser, data);
435
436 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
437
438 if (parser->local.delimiter_branch > 1) {
439 dbg_hid("alternative usage ignored\n");
440 return 0;
441 }
442
443 if (item->size <= 2)
444 data = (parser->global.usage_page << 16) + data;
445
446 parser->local.usage_minimum = data;
447 return 0;
448
449 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
450
451 if (parser->local.delimiter_branch > 1) {
452 dbg_hid("alternative usage ignored\n");
453 return 0;
454 }
455
456 if (item->size <= 2)
457 data = (parser->global.usage_page << 16) + data;
458
459 for (n = parser->local.usage_minimum; n <= data; n++)
460 if (hid_add_usage(parser, n)) {
461 dbg_hid("hid_add_usage failed\n");
462 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100463 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200464 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100465
Jiri Slaby880d29f2008-06-18 23:55:41 +0200466 default:
Jiri Kosinadde58452006-12-08 18:40:44 +0100467
Jiri Slaby880d29f2008-06-18 23:55:41 +0200468 dbg_hid("unknown local item tag 0x%x\n", item->tag);
469 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100470 }
471 return 0;
472}
473
474/*
475 * Process a main item.
476 */
477
478static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
479{
480 __u32 data;
481 int ret;
482
483 data = item_udata(item);
484
485 switch (item->tag) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200486 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
487 ret = open_collection(parser, data & 0xff);
488 break;
489 case HID_MAIN_ITEM_TAG_END_COLLECTION:
490 ret = close_collection(parser);
491 break;
492 case HID_MAIN_ITEM_TAG_INPUT:
493 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
494 break;
495 case HID_MAIN_ITEM_TAG_OUTPUT:
496 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
497 break;
498 case HID_MAIN_ITEM_TAG_FEATURE:
499 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
500 break;
501 default:
502 dbg_hid("unknown main item tag 0x%x\n", item->tag);
503 ret = 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100504 }
505
506 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
507
508 return ret;
509}
510
511/*
512 * Process a reserved item.
513 */
514
515static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
516{
Jiri Kosina58037eb2007-05-30 15:07:13 +0200517 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
Jiri Kosinadde58452006-12-08 18:40:44 +0100518 return 0;
519}
520
521/*
522 * Free a report and all registered fields. The field->usage and
523 * field->value table's are allocated behind the field, so we need
524 * only to free(field) itself.
525 */
526
527static void hid_free_report(struct hid_report *report)
528{
529 unsigned n;
530
531 for (n = 0; n < report->maxfield; n++)
532 kfree(report->field[n]);
533 kfree(report);
534}
535
536/*
537 * Free a device structure, all reports, and all fields.
538 */
539
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200540static void hid_device_release(struct device *dev)
Jiri Kosinadde58452006-12-08 18:40:44 +0100541{
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200542 struct hid_device *device = container_of(dev, struct hid_device, dev);
543 unsigned i, j;
Jiri Kosinadde58452006-12-08 18:40:44 +0100544
545 for (i = 0; i < HID_REPORT_TYPES; i++) {
546 struct hid_report_enum *report_enum = device->report_enum + i;
547
548 for (j = 0; j < 256; j++) {
549 struct hid_report *report = report_enum->report_id_hash[j];
550 if (report)
551 hid_free_report(report);
552 }
553 }
554
555 kfree(device->rdesc);
Jiri Kosina767fe782007-01-24 23:05:07 +0100556 kfree(device->collection);
Jiri Kosinadde58452006-12-08 18:40:44 +0100557 kfree(device);
558}
559
560/*
561 * Fetch a report description item from the data stream. We support long
562 * items, though they are not used yet.
563 */
564
565static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
566{
567 u8 b;
568
569 if ((end - start) <= 0)
570 return NULL;
571
572 b = *start++;
573
574 item->type = (b >> 2) & 3;
575 item->tag = (b >> 4) & 15;
576
577 if (item->tag == HID_ITEM_TAG_LONG) {
578
579 item->format = HID_ITEM_FORMAT_LONG;
580
581 if ((end - start) < 2)
582 return NULL;
583
584 item->size = *start++;
585 item->tag = *start++;
586
587 if ((end - start) < item->size)
588 return NULL;
589
590 item->data.longdata = start;
591 start += item->size;
592 return start;
593 }
594
595 item->format = HID_ITEM_FORMAT_SHORT;
596 item->size = b & 3;
597
598 switch (item->size) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200599 case 0:
600 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100601
Jiri Slaby880d29f2008-06-18 23:55:41 +0200602 case 1:
603 if ((end - start) < 1)
604 return NULL;
605 item->data.u8 = *start++;
606 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100607
Jiri Slaby880d29f2008-06-18 23:55:41 +0200608 case 2:
609 if ((end - start) < 2)
610 return NULL;
611 item->data.u16 = get_unaligned_le16(start);
612 start = (__u8 *)((__le16 *)start + 1);
613 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100614
Jiri Slaby880d29f2008-06-18 23:55:41 +0200615 case 3:
616 item->size++;
617 if ((end - start) < 4)
618 return NULL;
619 item->data.u32 = get_unaligned_le32(start);
620 start = (__u8 *)((__le32 *)start + 1);
621 return start;
Jiri Kosinadde58452006-12-08 18:40:44 +0100622 }
623
624 return NULL;
625}
626
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200627/**
628 * hid_parse_report - parse device report
629 *
630 * @device: hid device
631 * @start: report start
632 * @size: report size
633 *
Jiri Kosinadde58452006-12-08 18:40:44 +0100634 * Parse a report description into a hid_device structure. Reports are
635 * enumerated, fields are attached to these reports.
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200636 * 0 returned on success, otherwise nonzero error value.
Jiri Kosinadde58452006-12-08 18:40:44 +0100637 */
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200638int hid_parse_report(struct hid_device *device, __u8 *start,
639 unsigned size)
Jiri Kosinadde58452006-12-08 18:40:44 +0100640{
Jiri Kosinadde58452006-12-08 18:40:44 +0100641 struct hid_parser *parser;
642 struct hid_item item;
643 __u8 *end;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200644 int ret;
Jiri Kosinadde58452006-12-08 18:40:44 +0100645 static int (*dispatch_type[])(struct hid_parser *parser,
646 struct hid_item *item) = {
647 hid_parser_main,
648 hid_parser_global,
649 hid_parser_local,
650 hid_parser_reserved
651 };
652
Jiri Slabyc500c972008-05-16 11:49:16 +0200653 if (device->driver->report_fixup)
654 device->driver->report_fixup(device, start, size);
655
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200656 device->rdesc = kmalloc(size, GFP_KERNEL);
657 if (device->rdesc == NULL)
658 return -ENOMEM;
Jiri Kosinadde58452006-12-08 18:40:44 +0100659 memcpy(device->rdesc, start, size);
660 device->rsize = size;
661
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200662 parser = vmalloc(sizeof(struct hid_parser));
663 if (!parser) {
664 ret = -ENOMEM;
665 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100666 }
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200667
Jiri Kosina47a80ed2007-03-12 14:55:12 +0100668 memset(parser, 0, sizeof(struct hid_parser));
Jiri Kosinadde58452006-12-08 18:40:44 +0100669 parser->device = device;
670
671 end = start + size;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200672 ret = -EINVAL;
Jiri Kosinadde58452006-12-08 18:40:44 +0100673 while ((start = fetch_item(start, end, &item)) != NULL) {
674
675 if (item.format != HID_ITEM_FORMAT_SHORT) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200676 dbg_hid("unexpected long global item\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200677 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100678 }
679
680 if (dispatch_type[item.type](parser, &item)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200681 dbg_hid("item %u %u %u %u parsing failed\n",
Jiri Kosinadde58452006-12-08 18:40:44 +0100682 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200683 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100684 }
685
686 if (start == end) {
687 if (parser->collection_stack_ptr) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200688 dbg_hid("unbalanced collection at end of report description\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200689 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100690 }
691 if (parser->local.delimiter_depth) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200692 dbg_hid("unbalanced delimiter at end of report description\n");
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200693 goto err;
Jiri Kosinadde58452006-12-08 18:40:44 +0100694 }
Jiri Kosina47a80ed2007-03-12 14:55:12 +0100695 vfree(parser);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200696 return 0;
Jiri Kosinadde58452006-12-08 18:40:44 +0100697 }
698 }
699
Jiri Kosina58037eb2007-05-30 15:07:13 +0200700 dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200701err:
Jiri Kosina47a80ed2007-03-12 14:55:12 +0100702 vfree(parser);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200703 return ret;
Jiri Kosinadde58452006-12-08 18:40:44 +0100704}
Jiri Kosina229695e2006-12-08 18:40:53 +0100705EXPORT_SYMBOL_GPL(hid_parse_report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100706
707/*
708 * Convert a signed n-bit integer to signed 32-bit integer. Common
709 * cases are done through the compiler, the screwed things has to be
710 * done by hand.
711 */
712
713static s32 snto32(__u32 value, unsigned n)
714{
715 switch (n) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200716 case 8: return ((__s8)value);
717 case 16: return ((__s16)value);
718 case 32: return ((__s32)value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100719 }
720 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
721}
722
723/*
724 * Convert a signed 32-bit integer to a signed n-bit integer.
725 */
726
727static u32 s32ton(__s32 value, unsigned n)
728{
729 s32 a = value >> (n - 1);
730 if (a && a != -1)
731 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
732 return value & ((1 << n) - 1);
733}
734
735/*
736 * Extract/implement a data field from/to a little endian report (bit array).
737 *
738 * Code sort-of follows HID spec:
739 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
740 *
741 * While the USB HID spec allows unlimited length bit fields in "report
742 * descriptors", most devices never use more than 16 bits.
743 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
744 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
745 */
746
747static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
748{
749 u64 x;
750
Jiri Kosinac4124c92007-11-30 11:12:58 +0100751 if (n > 32)
752 printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
753 n, current->comm);
Jiri Kosinadde58452006-12-08 18:40:44 +0100754
755 report += offset >> 3; /* adjust byte index */
Jiri Kosina229695e2006-12-08 18:40:53 +0100756 offset &= 7; /* now only need bit offset into one byte */
Harvey Harrisonc1050682008-04-29 01:03:31 -0700757 x = get_unaligned_le64(report);
Jiri Kosina229695e2006-12-08 18:40:53 +0100758 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
Jiri Kosinadde58452006-12-08 18:40:44 +0100759 return (u32) x;
760}
761
762/*
763 * "implement" : set bits in a little endian bit stream.
764 * Same concepts as "extract" (see comments above).
765 * The data mangled in the bit stream remains in little endian
766 * order the whole time. It make more sense to talk about
767 * endianness of register values by considering a register
768 * a "cached" copy of the little endiad bit stream.
769 */
770static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
771{
Harvey Harrison6f0168d2008-05-16 11:00:23 +0200772 u64 x;
Jiri Kosinadde58452006-12-08 18:40:44 +0100773 u64 m = (1ULL << n) - 1;
774
Jiri Kosinac4124c92007-11-30 11:12:58 +0100775 if (n > 32)
776 printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
777 n, current->comm);
Jiri Kosinadde58452006-12-08 18:40:44 +0100778
Jiri Kosinac4124c92007-11-30 11:12:58 +0100779 if (value > m)
780 printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
781 value, current->comm);
Jiri Kosinadde58452006-12-08 18:40:44 +0100782 WARN_ON(value > m);
783 value &= m;
784
785 report += offset >> 3;
786 offset &= 7;
787
Harvey Harrison6f0168d2008-05-16 11:00:23 +0200788 x = get_unaligned_le64(report);
789 x &= ~(m << offset);
790 x |= ((u64)value) << offset;
791 put_unaligned_le64(x, report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100792}
793
794/*
795 * Search an array for a value.
796 */
797
798static __inline__ int search(__s32 *array, __s32 value, unsigned n)
799{
800 while (n--) {
801 if (*array++ == value)
802 return 0;
803 }
804 return -1;
805}
806
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200807/**
808 * hid_match_report - check if driver's raw_event should be called
809 *
810 * @hid: hid device
811 * @report_type: type to match against
812 *
813 * compare hid->driver->report_table->report_type to report->type
814 */
815static int hid_match_report(struct hid_device *hid, struct hid_report *report)
Jiri Kosinadde58452006-12-08 18:40:44 +0100816{
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200817 const struct hid_report_id *id = hid->driver->report_table;
818
819 if (!id) /* NULL means all */
820 return 1;
821
822 for (; id->report_type != HID_TERMINATOR; id++)
823 if (id->report_type == HID_ANY_ID ||
824 id->report_type == report->type)
825 return 1;
826 return 0;
827}
828
829/**
830 * hid_match_usage - check if driver's event should be called
831 *
832 * @hid: hid device
833 * @usage: usage to match against
834 *
835 * compare hid->driver->usage_table->usage_{type,code} to
836 * usage->usage_{type,code}
837 */
838static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
839{
840 const struct hid_usage_id *id = hid->driver->usage_table;
841
842 if (!id) /* NULL means all */
843 return 1;
844
845 for (; id->usage_type != HID_ANY_ID - 1; id++)
846 if ((id->usage_hid == HID_ANY_ID ||
847 id->usage_hid == usage->hid) &&
848 (id->usage_type == HID_ANY_ID ||
849 id->usage_type == usage->type) &&
850 (id->usage_code == HID_ANY_ID ||
851 id->usage_code == usage->code))
852 return 1;
853 return 0;
854}
855
856static void hid_process_event(struct hid_device *hid, struct hid_field *field,
857 struct hid_usage *usage, __s32 value, int interrupt)
858{
859 struct hid_driver *hdrv = hid->driver;
860 int ret;
861
Jiri Kosinadde58452006-12-08 18:40:44 +0100862 hid_dump_input(usage, value);
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200863
864 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
865 ret = hdrv->event(hid, field, usage, value);
866 if (ret != 0) {
867 if (ret < 0)
868 dbg_hid("%s's event failed with %d\n",
869 hdrv->name, ret);
870 return;
871 }
872 }
873
Jiri Kosinadde58452006-12-08 18:40:44 +0100874 if (hid->claimed & HID_CLAIMED_INPUT)
875 hidinput_hid_event(hid, field, usage, value);
Jiri Kosinaaa938f72006-12-08 18:41:10 +0100876 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
877 hid->hiddev_hid_event(hid, field, usage, value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100878}
879
880/*
881 * Analyse a received field, and fetch the data from it. The field
882 * content is stored for next report processing (we do differential
883 * reporting to the layer).
884 */
885
Adrian Bunkabdff0f2008-03-31 01:53:56 +0200886static void hid_input_field(struct hid_device *hid, struct hid_field *field,
887 __u8 *data, int interrupt)
Jiri Kosinadde58452006-12-08 18:40:44 +0100888{
889 unsigned n;
890 unsigned count = field->report_count;
891 unsigned offset = field->report_offset;
892 unsigned size = field->report_size;
893 __s32 min = field->logical_minimum;
894 __s32 max = field->logical_maximum;
895 __s32 *value;
896
897 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
898 return;
899
900 for (n = 0; n < count; n++) {
901
902 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
903 extract(data, offset + n * size, size);
904
905 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
906 && value[n] >= min && value[n] <= max
907 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
908 goto exit;
909 }
910
911 for (n = 0; n < count; n++) {
912
913 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
914 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
915 continue;
916 }
917
918 if (field->value[n] >= min && field->value[n] <= max
919 && field->usage[field->value[n] - min].hid
920 && search(value, field->value[n], count))
921 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
922
923 if (value[n] >= min && value[n] <= max
924 && field->usage[value[n] - min].hid
925 && search(field->value, value[n], count))
926 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
927 }
928
929 memcpy(field->value, value, count * sizeof(__s32));
930exit:
931 kfree(value);
932}
Jiri Kosinadde58452006-12-08 18:40:44 +0100933
934/*
935 * Output the field into the report.
936 */
937
938static void hid_output_field(struct hid_field *field, __u8 *data)
939{
940 unsigned count = field->report_count;
941 unsigned offset = field->report_offset;
942 unsigned size = field->report_size;
Simon Budig46386b52007-03-12 13:52:04 +0100943 unsigned bitsused = offset + count * size;
Jiri Kosinadde58452006-12-08 18:40:44 +0100944 unsigned n;
945
Simon Budig46386b52007-03-12 13:52:04 +0100946 /* make sure the unused bits in the last byte are zeros */
947 if (count > 0 && size > 0 && (bitsused % 8) != 0)
948 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
949
Jiri Kosinadde58452006-12-08 18:40:44 +0100950 for (n = 0; n < count; n++) {
951 if (field->logical_minimum < 0) /* signed values */
952 implement(data, offset + n * size, size, s32ton(field->value[n], size));
953 else /* unsigned values */
954 implement(data, offset + n * size, size, field->value[n]);
955 }
956}
957
958/*
959 * Create a report.
960 */
961
Jiri Kosina229695e2006-12-08 18:40:53 +0100962void hid_output_report(struct hid_report *report, __u8 *data)
Jiri Kosinadde58452006-12-08 18:40:44 +0100963{
964 unsigned n;
965
966 if (report->id > 0)
967 *data++ = report->id;
968
969 for (n = 0; n < report->maxfield; n++)
970 hid_output_field(report->field[n], data);
971}
Jiri Kosina229695e2006-12-08 18:40:53 +0100972EXPORT_SYMBOL_GPL(hid_output_report);
Jiri Kosinadde58452006-12-08 18:40:44 +0100973
974/*
975 * Set a field value. The report this field belongs to has to be
976 * created and transferred to the device, to set this value in the
977 * device.
978 */
979
980int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
981{
982 unsigned size = field->report_size;
983
984 hid_dump_input(field->usage + offset, value);
985
986 if (offset >= field->report_count) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200987 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
Jiri Kosinadde58452006-12-08 18:40:44 +0100988 return -1;
989 }
990 if (field->logical_minimum < 0) {
991 if (value != snto32(s32ton(value, size), size)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200992 dbg_hid("value %d is out of range\n", value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100993 return -1;
994 }
995 }
996 field->value[offset] = value;
997 return 0;
998}
Jiri Kosina229695e2006-12-08 18:40:53 +0100999EXPORT_SYMBOL_GPL(hid_set_field);
Jiri Kosinadde58452006-12-08 18:40:44 +01001000
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001001static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1002 const u8 *data)
1003{
1004 struct hid_report *report;
1005 unsigned int n = 0; /* Normally report number is 0 */
1006
1007 /* Device uses numbered reports, data[0] is report number */
1008 if (report_enum->numbered)
1009 n = *data;
1010
1011 report = report_enum->report_id_hash[n];
1012 if (report == NULL)
1013 dbg_hid("undefined report_id %u received\n", n);
1014
1015 return report;
1016}
1017
1018void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1019 int interrupt)
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001020{
1021 struct hid_report_enum *report_enum = hid->report_enum + type;
1022 struct hid_report *report;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001023 unsigned int a;
1024 int rsize, csize = size;
1025 u8 *cdata = data;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001026
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001027 report = hid_get_report(report_enum, data);
1028 if (!report)
1029 return;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001030
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001031 if (report_enum->numbered) {
1032 cdata++;
1033 csize--;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001034 }
1035
1036 rsize = ((report->size - 1) >> 3) + 1;
1037
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001038 if (csize < rsize) {
1039 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1040 csize, rsize);
1041 memset(cdata + csize, 0, rsize - csize);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001042 }
1043
1044 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1045 hid->hiddev_report_event(hid, report);
Jiri Kosinab54ec3c2008-03-28 14:11:22 +01001046 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1047 /* numbered reports need to be passed with the report num */
1048 if (report_enum->numbered)
1049 hidraw_report_event(hid, data - 1, size + 1);
1050 else
1051 hidraw_report_event(hid, data, size);
1052 }
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001053
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001054 for (a = 0; a < report->maxfield; a++)
1055 hid_input_field(hid, report->field[a], cdata, interrupt);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001056
1057 if (hid->claimed & HID_CLAIMED_INPUT)
1058 hidinput_report_event(hid, report);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001059}
1060EXPORT_SYMBOL_GPL(hid_report_raw_event);
1061
1062/**
1063 * hid_input_report - report data from lower layer (usb, bt...)
1064 *
1065 * @hid: hid device
1066 * @type: HID report type (HID_*_REPORT)
1067 * @data: report contents
1068 * @size: size of data parameter
1069 * @interrupt: called from atomic?
1070 *
1071 * This is data entry for lower layers.
1072 */
1073int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1074{
1075 struct hid_report_enum *report_enum = hid->report_enum + type;
1076 struct hid_driver *hdrv = hid->driver;
1077 struct hid_report *report;
1078 unsigned int i;
1079 int ret;
1080
1081 if (!hid || !hid->driver)
1082 return -ENODEV;
1083
1084 if (!size) {
1085 dbg_hid("empty report\n");
1086 return -1;
1087 }
1088
1089 dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1090
1091 report = hid_get_report(report_enum, data);
1092 if (!report)
1093 return -1;
1094
1095 /* dump the report */
1096 dbg_hid("report %d (size %u) = ", report->id, size);
1097 for (i = 0; i < size; i++)
1098 dbg_hid_line(" %02x", data[i]);
1099 dbg_hid_line("\n");
1100
1101 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1102 ret = hdrv->raw_event(hid, report, data, size);
1103 if (ret != 0)
1104 return ret < 0 ? ret : 0;
1105 }
1106
1107 hid_report_raw_event(hid, type, data, size, interrupt);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001108
1109 return 0;
1110}
1111EXPORT_SYMBOL_GPL(hid_input_report);
1112
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001113static bool hid_match_one_id(struct hid_device *hdev,
1114 const struct hid_device_id *id)
1115{
1116 return id->bus == hdev->bus &&
1117 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1118 (id->product == HID_ANY_ID || id->product == hdev->product);
1119}
1120
1121static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1122 const struct hid_device_id *id)
1123{
1124 for (; id->bus; id++)
1125 if (hid_match_one_id(hdev, id))
1126 return id;
1127
1128 return NULL;
1129}
1130
1131static const struct hid_device_id hid_hiddev_list[] = {
Richard Hughesc0bd6a42008-08-27 11:19:37 +02001132 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1133 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001134 { }
1135};
1136
1137static bool hid_hiddev(struct hid_device *hdev)
1138{
1139 return !!hid_match_id(hdev, hid_hiddev_list);
1140}
1141
Jiri Slaby93c10132008-06-27 00:04:24 +02001142int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1143{
1144 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1145 "Joystick", "Gamepad", "Keyboard", "Keypad",
1146 "Multi-Axis Controller"
1147 };
1148 const char *type, *bus;
1149 char buf[64];
1150 unsigned int i;
1151 int len;
1152
1153 if (hdev->bus != BUS_USB)
1154 connect_mask &= ~HID_CONNECT_HIDDEV;
Jiri Kosina0f37cd02008-08-20 19:13:52 +02001155 if (hid_hiddev(hdev))
1156 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Jiri Slaby93c10132008-06-27 00:04:24 +02001157
1158 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1159 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1160 hdev->claimed |= HID_CLAIMED_INPUT;
1161 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1162 !hdev->hiddev_connect(hdev,
1163 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1164 hdev->claimed |= HID_CLAIMED_HIDDEV;
1165 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1166 hdev->claimed |= HID_CLAIMED_HIDRAW;
1167
1168 if (!hdev->claimed) {
1169 dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
1170 "hidraw\n");
1171 return -ENODEV;
1172 }
1173
1174 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1175 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1176 hdev->ff_init(hdev);
1177
1178 len = 0;
1179 if (hdev->claimed & HID_CLAIMED_INPUT)
1180 len += sprintf(buf + len, "input");
1181 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1182 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1183 hdev->minor);
1184 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1185 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1186 ((struct hidraw *)hdev->hidraw)->minor);
1187
1188 type = "Device";
1189 for (i = 0; i < hdev->maxcollection; i++) {
1190 struct hid_collection *col = &hdev->collection[i];
1191 if (col->type == HID_COLLECTION_APPLICATION &&
1192 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1193 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1194 type = types[col->usage & 0xffff];
1195 break;
1196 }
1197 }
1198
1199 switch (hdev->bus) {
1200 case BUS_USB:
1201 bus = "USB";
1202 break;
1203 case BUS_BLUETOOTH:
1204 bus = "BLUETOOTH";
1205 break;
1206 default:
1207 bus = "<UNKNOWN>";
1208 }
1209
1210 dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1211 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1212 type, hdev->name, hdev->phys);
1213
1214 return 0;
1215}
1216EXPORT_SYMBOL_GPL(hid_connect);
1217
Jiri Kosinabae7eb32009-01-28 23:06:37 +01001218/* a list of devices for which there is a specialized driver on HID bus */
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001219static const struct hid_device_id hid_blacklist[] = {
Jiri Slaby14a21cd2008-06-23 23:31:09 +02001220 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
1221 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
Jiri Kosinadf9bcac2008-10-14 22:45:40 +02001222 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001223 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1224 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1225 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1226 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1227 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1228 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1229 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1230 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1231 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1232 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1233 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1234 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1235 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
Ryan Finniefef3f572009-03-05 10:18:01 +01001236 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1237 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1238 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001239 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1240 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1241 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1242 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1243 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1244 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
Jan Scholzee8a1a02008-11-26 15:33:45 +01001245 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1246 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1247 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001248 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1249 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1250 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1251 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1252 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1253 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
Henrik Rydberga96d6ef2008-11-04 20:03:45 +01001254 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1255 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1256 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001257 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1258 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
Jiri Slabyb5635b12008-06-24 23:24:57 +02001259 { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
Jiri Slaby3b239cd2008-06-24 20:42:25 +02001260 { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
Jiri Slabyfcfacfd2008-06-24 22:48:52 +02001261 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
Jiri Slaby0f221322008-06-23 22:54:08 +02001262 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
1263 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1264 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
Richard Walmsley3f866fb2009-03-04 22:12:04 +13001265 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
Jiri Slaby1f243e32008-06-24 21:11:21 +02001266 { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
Jiri Kosina5181e592008-11-17 01:44:38 +01001267 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
Jiri Kosina578f3a32008-11-20 15:55:38 +01001268 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
Jiri Kosina5181e592008-11-17 01:44:38 +01001269 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
Lukasz Lubojanski42859e02008-12-11 22:07:59 +01001270 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
Jiri Slaby949f8fe2008-07-24 23:35:13 +02001271 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
Jiri Kosina1e093202008-10-17 11:52:23 +02001272 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
Jiri Kosinafdf93aa2009-03-04 16:09:40 +01001273 { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
Jiri Kosina79422742009-03-11 11:43:27 +01001274 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
Jiri Slabyb5635b12008-06-24 23:24:57 +02001275 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
Jiri Slaby5f22a792008-05-16 11:49:19 +02001276 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1277 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1278 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1279 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1280 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1281 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1282 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
Jiri Slaby5f22a792008-05-16 11:49:19 +02001283 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1284 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
Jiri Slaby5f22a792008-05-16 11:49:19 +02001285 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1286 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
Jiri Slaby606bd0a2008-07-04 23:06:45 +02001287 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1288 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1289 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1290 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1291 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1292 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
Christophe Borivant243b7062009-04-17 11:39:39 +02001293 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
Jiri Slaby606bd0a2008-07-04 23:06:45 +02001294 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
Jiri Slaby78a849a682008-06-20 21:26:11 +02001295 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
1296 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
1297 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
1298 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1299 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
Jiri Slaby3b8006e2008-06-25 00:07:50 +02001300 { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
Rafi Rubin94011f92008-11-19 15:54:46 +01001301 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
Jiri Slaby1e762532008-06-24 23:46:21 +02001302 { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
Jiri Slaby980a3da2008-06-25 22:31:48 +02001303 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001304 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001305 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
Jiri Slaby90231e72008-06-23 21:56:07 +02001306 { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
Anssi Hannuladaedb3d2009-02-14 11:45:05 +02001307 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1308 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
1309 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1310 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
Lev Babievf14f5262009-01-04 00:36:56 +01001311 { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
Anssi Hannuladaedb3d2009-02-14 11:45:05 +02001312 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1313 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
Jiri Slaby8c19a512008-06-18 23:36:49 +02001314
1315 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
Jiri Slaby78a849a682008-06-20 21:26:11 +02001316 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001317 { }
1318};
1319
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001320struct hid_dynid {
1321 struct list_head list;
1322 struct hid_device_id id;
1323};
1324
1325/**
1326 * store_new_id - add a new HID device ID to this driver and re-probe devices
1327 * @driver: target device driver
1328 * @buf: buffer for scanning device ID data
1329 * @count: input size
1330 *
1331 * Adds a new dynamic hid device ID to this driver,
1332 * and causes the driver to probe for all devices again.
1333 */
1334static ssize_t store_new_id(struct device_driver *drv, const char *buf,
1335 size_t count)
1336{
1337 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1338 struct hid_dynid *dynid;
1339 __u32 bus, vendor, product;
1340 unsigned long driver_data = 0;
1341 int ret;
1342
1343 ret = sscanf(buf, "%x %x %x %lx",
1344 &bus, &vendor, &product, &driver_data);
1345 if (ret < 3)
1346 return -EINVAL;
1347
1348 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1349 if (!dynid)
1350 return -ENOMEM;
1351
1352 dynid->id.bus = bus;
1353 dynid->id.vendor = vendor;
1354 dynid->id.product = product;
1355 dynid->id.driver_data = driver_data;
1356
1357 spin_lock(&hdrv->dyn_lock);
1358 list_add_tail(&dynid->list, &hdrv->dyn_list);
1359 spin_unlock(&hdrv->dyn_lock);
1360
1361 ret = 0;
1362 if (get_driver(&hdrv->driver)) {
1363 ret = driver_attach(&hdrv->driver);
1364 put_driver(&hdrv->driver);
1365 }
1366
1367 return ret ? : count;
1368}
1369static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1370
1371static void hid_free_dynids(struct hid_driver *hdrv)
1372{
1373 struct hid_dynid *dynid, *n;
1374
1375 spin_lock(&hdrv->dyn_lock);
1376 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1377 list_del(&dynid->list);
1378 kfree(dynid);
1379 }
1380 spin_unlock(&hdrv->dyn_lock);
1381}
1382
1383static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1384 struct hid_driver *hdrv)
1385{
1386 struct hid_dynid *dynid;
1387
1388 spin_lock(&hdrv->dyn_lock);
1389 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1390 if (hid_match_one_id(hdev, &dynid->id)) {
1391 spin_unlock(&hdrv->dyn_lock);
1392 return &dynid->id;
1393 }
1394 }
1395 spin_unlock(&hdrv->dyn_lock);
1396
1397 return hid_match_id(hdev, hdrv->id_table);
1398}
1399
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001400static int hid_bus_match(struct device *dev, struct device_driver *drv)
1401{
1402 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1403 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1404
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001405 if (!hid_match_device(hdev, hdrv))
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001406 return 0;
1407
1408 /* generic wants all non-blacklisted */
1409 if (!strncmp(hdrv->name, "generic-", 8))
1410 return !hid_match_id(hdev, hid_blacklist);
1411
1412 return 1;
1413}
1414
1415static int hid_device_probe(struct device *dev)
1416{
1417 struct hid_driver *hdrv = container_of(dev->driver,
1418 struct hid_driver, driver);
1419 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1420 const struct hid_device_id *id;
1421 int ret = 0;
1422
1423 if (!hdev->driver) {
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001424 id = hid_match_device(hdev, hdrv);
Jiri Slabyc500c972008-05-16 11:49:16 +02001425 if (id == NULL)
1426 return -ENODEV;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001427
Jiri Slabyc500c972008-05-16 11:49:16 +02001428 hdev->driver = hdrv;
1429 if (hdrv->probe) {
1430 ret = hdrv->probe(hdev, id);
1431 } else { /* default probe */
1432 ret = hid_parse(hdev);
1433 if (!ret)
Jiri Slaby93c10132008-06-27 00:04:24 +02001434 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001435 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001436 if (ret)
1437 hdev->driver = NULL;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001438 }
1439 return ret;
1440}
1441
1442static int hid_device_remove(struct device *dev)
1443{
1444 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1445 struct hid_driver *hdrv = hdev->driver;
1446
1447 if (hdrv) {
1448 if (hdrv->remove)
1449 hdrv->remove(hdev);
Jiri Slabyc500c972008-05-16 11:49:16 +02001450 else /* default remove */
1451 hid_hw_stop(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001452 hdev->driver = NULL;
1453 }
1454
1455 return 0;
1456}
1457
1458static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1459{
1460 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1461
1462 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1463 hdev->bus, hdev->vendor, hdev->product))
1464 return -ENOMEM;
1465
1466 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1467 return -ENOMEM;
1468
1469 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1470 return -ENOMEM;
1471
1472 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1473 return -ENOMEM;
1474
1475 if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1476 hdev->bus, hdev->vendor, hdev->product))
1477 return -ENOMEM;
1478
1479 return 0;
1480}
1481
1482static struct bus_type hid_bus_type = {
1483 .name = "hid",
1484 .match = hid_bus_match,
1485 .probe = hid_device_probe,
1486 .remove = hid_device_remove,
1487 .uevent = hid_uevent,
1488};
1489
Jiri Kosinabae7eb32009-01-28 23:06:37 +01001490/* a list of devices that shouldn't be handled by HID core at all */
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001491static const struct hid_device_id hid_ignore_list[] = {
1492 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1493 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1494 { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1495 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1496 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1497 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1498 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1499 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1500 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1501 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1502 { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1503 { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1504 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
Jiri Kosinaac2d9892008-10-20 12:37:43 +02001505 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2)},
Alexey Klimov62a56582008-11-13 05:44:50 +03001506 { HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001507 { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1508 { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1509 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1510 { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1511 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1512 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
Alexey Klimov5f6108c2008-12-08 12:40:14 +01001513 { HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001514 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1515 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1516 { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1517 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1518 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1519 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1520 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1521 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1522 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1523 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1524 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1525 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1526 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1527 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1528 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1529 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1530 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1531 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1532 { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1533 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1534 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1535 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1536 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1537 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1538 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1539 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1540 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1541 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1542 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1543 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1544 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1545 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1546 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1547 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1548 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1549 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1550 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1551 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1552 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1553 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1554 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1555 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1556 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1557 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1558 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1559 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1560 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1561 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1562 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1563 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1564 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1565 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1566 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1567 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1568 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1569 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1570 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1571 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1572 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1573 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1574 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1575 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1576 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1577 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1578 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1579 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1580 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1581 { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1582 { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
Alexey Klimovc91c21c2008-11-13 10:36:11 +01001583 { HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001584 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1585 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1586 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1587 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1588 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1589 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1590 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1591 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1592 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1593 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1594 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1595 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1596 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1597 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1598 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1599 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1600 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001601 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1602 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1603 { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1604 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1605 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1606 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1607 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1608 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1609 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1610 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1611 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1612 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1613 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1614 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1615 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1616 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1617 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
Michael Tokarev35cfd1d2009-02-01 16:11:04 +01001618 { HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001619 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD) },
Jarod Wilson24c88eb62008-10-16 01:26:43 +02001620 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD2) },
1621 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD3) },
Jarod Wilson656f1fb2009-01-28 21:22:35 +01001622 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD4) },
1623 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD5) },
Remi Cattiaud1d3a5f2008-09-09 01:39:33 +02001624 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY1) },
1625 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY2) },
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001626 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1627 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1628 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1629 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1630 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1631 { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1632 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1633 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1634 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1635 { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1636 { }
1637};
1638
Jiri Slabyb4d8e472008-10-22 14:47:18 +02001639/**
1640 * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
1641 *
1642 * There are composite devices for which we want to ignore only a certain
1643 * interface. This is a list of devices for which only the mouse interface will
1644 * be ignored. This allows a dedicated driver to take care of the interface.
1645 */
1646static const struct hid_device_id hid_mouse_ignore_list[] = {
1647 /* appletouch driver */
1648 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1649 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1650 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1651 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1652 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1653 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1654 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1655 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1656 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1657 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1658 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1659 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1660 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1661 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1662 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1663 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1664 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1665 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1666 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1667 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
Jiri Kosinaac26fca2008-11-20 11:27:02 +01001668 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1669 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1670 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
Jiri Slabyb4d8e472008-10-22 14:47:18 +02001671 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1672 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1673 { }
1674};
1675
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001676static bool hid_ignore(struct hid_device *hdev)
1677{
1678 switch (hdev->vendor) {
1679 case USB_VENDOR_ID_CODEMERCS:
1680 /* ignore all Code Mercenaries IOWarrior devices */
1681 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1682 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1683 return true;
1684 break;
1685 case USB_VENDOR_ID_LOGITECH:
1686 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1687 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1688 return true;
1689 break;
1690 }
1691
Jiri Slabyb4d8e472008-10-22 14:47:18 +02001692 if (hdev->type == HID_TYPE_USBMOUSE &&
1693 hid_match_id(hdev, hid_mouse_ignore_list))
1694 return true;
1695
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001696 return !!hid_match_id(hdev, hid_ignore_list);
1697}
1698
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001699int hid_add_device(struct hid_device *hdev)
1700{
1701 static atomic_t id = ATOMIC_INIT(0);
1702 int ret;
1703
1704 if (WARN_ON(hdev->status & HID_STAT_ADDED))
1705 return -EBUSY;
1706
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001707 /* we need to kill them here, otherwise they will stay allocated to
1708 * wait for coming driver */
1709 if (hid_ignore(hdev))
1710 return -ENODEV;
1711
Kay Sievers6bbe5862008-10-31 00:12:32 +01001712 /* XXX hack, any other cleaner solution after the driver core
1713 * is converted to allow more than 20 bytes as the device name? */
1714 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
1715 hdev->vendor, hdev->product, atomic_inc_return(&id));
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001716
1717 ret = device_add(&hdev->dev);
1718 if (!ret)
1719 hdev->status |= HID_STAT_ADDED;
1720
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001721 hid_debug_register(hdev, dev_name(&hdev->dev));
1722
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001723 return ret;
1724}
1725EXPORT_SYMBOL_GPL(hid_add_device);
1726
1727/**
1728 * hid_allocate_device - allocate new hid device descriptor
1729 *
1730 * Allocate and initialize hid device, so that hid_destroy_device might be
1731 * used to free it.
1732 *
1733 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1734 * error value.
1735 */
1736struct hid_device *hid_allocate_device(void)
1737{
1738 struct hid_device *hdev;
1739 unsigned int i;
1740 int ret = -ENOMEM;
1741
1742 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1743 if (hdev == NULL)
1744 return ERR_PTR(ret);
1745
1746 device_initialize(&hdev->dev);
1747 hdev->dev.release = hid_device_release;
1748 hdev->dev.bus = &hid_bus_type;
1749
1750 hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1751 sizeof(struct hid_collection), GFP_KERNEL);
1752 if (hdev->collection == NULL)
1753 goto err;
1754 hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1755
1756 for (i = 0; i < HID_REPORT_TYPES; i++)
1757 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1758
1759 return hdev;
1760err:
1761 put_device(&hdev->dev);
1762 return ERR_PTR(ret);
1763}
1764EXPORT_SYMBOL_GPL(hid_allocate_device);
1765
1766static void hid_remove_device(struct hid_device *hdev)
1767{
1768 if (hdev->status & HID_STAT_ADDED) {
1769 device_del(&hdev->dev);
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001770 hid_debug_unregister(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001771 hdev->status &= ~HID_STAT_ADDED;
1772 }
1773}
1774
1775/**
1776 * hid_destroy_device - free previously allocated device
1777 *
1778 * @hdev: hid device
1779 *
1780 * If you allocate hid_device through hid_allocate_device, you should ever
1781 * free by this function.
1782 */
1783void hid_destroy_device(struct hid_device *hdev)
1784{
1785 hid_remove_device(hdev);
1786 put_device(&hdev->dev);
1787}
1788EXPORT_SYMBOL_GPL(hid_destroy_device);
1789
1790int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1791 const char *mod_name)
1792{
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001793 int ret;
1794
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001795 hdrv->driver.name = hdrv->name;
1796 hdrv->driver.bus = &hid_bus_type;
1797 hdrv->driver.owner = owner;
1798 hdrv->driver.mod_name = mod_name;
1799
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001800 INIT_LIST_HEAD(&hdrv->dyn_list);
1801 spin_lock_init(&hdrv->dyn_lock);
1802
1803 ret = driver_register(&hdrv->driver);
1804 if (ret)
1805 return ret;
1806
1807 ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
1808 if (ret)
1809 driver_unregister(&hdrv->driver);
1810
1811 return ret;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001812}
1813EXPORT_SYMBOL_GPL(__hid_register_driver);
1814
1815void hid_unregister_driver(struct hid_driver *hdrv)
1816{
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001817 driver_remove_file(&hdrv->driver, &driver_attr_new_id);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001818 driver_unregister(&hdrv->driver);
Jiri Slaby3a6f82f2008-11-24 16:20:09 +01001819 hid_free_dynids(hdrv);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001820}
1821EXPORT_SYMBOL_GPL(hid_unregister_driver);
1822
Oliver Neukum0361a282008-12-17 15:38:03 +01001823int hid_check_keys_pressed(struct hid_device *hid)
1824{
1825 struct hid_input *hidinput;
1826 int i;
1827
Jiri Kosinae5288eb2009-05-02 00:02:57 +02001828 if (!(hid->claimed & HID_CLAIMED_INPUT))
1829 return 0;
1830
Oliver Neukum0361a282008-12-17 15:38:03 +01001831 list_for_each_entry(hidinput, &hid->inputs, list) {
1832 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
1833 if (hidinput->input->key[i])
1834 return 1;
1835 }
1836
1837 return 0;
1838}
1839
1840EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
1841
Jiri Kosina86166b72007-05-14 09:57:40 +02001842static int __init hid_init(void)
1843{
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001844 int ret;
1845
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001846 if (hid_debug)
1847 printk(KERN_WARNING "HID: hid_debug parameter has been deprecated. "
1848 "Debugging data are now provided via debugfs\n");
1849
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001850 ret = bus_register(&hid_bus_type);
1851 if (ret) {
1852 printk(KERN_ERR "HID: can't register hid bus\n");
1853 goto err;
1854 }
1855
1856 ret = hidraw_init();
1857 if (ret)
1858 goto err_bus;
1859
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001860 hid_debug_init();
1861
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001862 return 0;
1863err_bus:
1864 bus_unregister(&hid_bus_type);
1865err:
1866 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +02001867}
1868
1869static void __exit hid_exit(void)
1870{
Jiri Kosinaa635f9d2009-06-12 15:20:55 +02001871 hid_debug_exit();
Jiri Kosina86166b72007-05-14 09:57:40 +02001872 hidraw_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001873 bus_unregister(&hid_bus_type);
Jiri Kosina86166b72007-05-14 09:57:40 +02001874}
1875
1876module_init(hid_init);
1877module_exit(hid_exit);
1878
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001879MODULE_LICENSE(DRIVER_LICENSE);
1880