blob: 3dacbcd7e41c404fe82137d6bcba6e7cd8ad34b9 [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
36/*
37 * Version Information
38 */
39
40#define DRIVER_VERSION "v2.6"
Jiri Kosinaf142b3a42007-04-16 11:29:28 +020041#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
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 +020045#ifdef CONFIG_HID_DEBUG
46int hid_debug = 0;
Anssi Hannula377e10f2008-03-22 23:50:13 +010047module_param_named(debug, hid_debug, int, 0600);
48MODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
Jiri Kosina58037eb2007-05-30 15:07:13 +020049EXPORT_SYMBOL_GPL(hid_debug);
50#endif
51
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) {
271 case 1: return item->data.u8;
272 case 2: return item->data.u16;
273 case 4: return item->data.u32;
274 }
275 return 0;
276}
277
278static s32 item_sdata(struct hid_item *item)
279{
280 switch (item->size) {
281 case 1: return item->data.s8;
282 case 2: return item->data.s16;
283 case 4: return item->data.s32;
284 }
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) {
295
296 case HID_GLOBAL_ITEM_TAG_PUSH:
297
298 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200299 dbg_hid("global enviroment stack overflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100300 return -1;
301 }
302
303 memcpy(parser->global_stack + parser->global_stack_ptr++,
304 &parser->global, sizeof(struct hid_global));
305 return 0;
306
307 case HID_GLOBAL_ITEM_TAG_POP:
308
309 if (!parser->global_stack_ptr) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200310 dbg_hid("global enviroment stack underflow\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100311 return -1;
312 }
313
314 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
315 sizeof(struct hid_global));
316 return 0;
317
318 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
319 parser->global.usage_page = item_udata(item);
320 return 0;
321
322 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
323 parser->global.logical_minimum = item_sdata(item);
324 return 0;
325
326 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
327 if (parser->global.logical_minimum < 0)
328 parser->global.logical_maximum = item_sdata(item);
329 else
330 parser->global.logical_maximum = item_udata(item);
331 return 0;
332
333 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
334 parser->global.physical_minimum = item_sdata(item);
335 return 0;
336
337 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
338 if (parser->global.physical_minimum < 0)
339 parser->global.physical_maximum = item_sdata(item);
340 else
341 parser->global.physical_maximum = item_udata(item);
342 return 0;
343
344 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
345 parser->global.unit_exponent = item_sdata(item);
346 return 0;
347
348 case HID_GLOBAL_ITEM_TAG_UNIT:
349 parser->global.unit = item_udata(item);
350 return 0;
351
352 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
353 if ((parser->global.report_size = item_udata(item)) > 32) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200354 dbg_hid("invalid report_size %d\n", parser->global.report_size);
Jiri Kosinadde58452006-12-08 18:40:44 +0100355 return -1;
356 }
357 return 0;
358
359 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
360 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200361 dbg_hid("invalid report_count %d\n", parser->global.report_count);
Jiri Kosinadde58452006-12-08 18:40:44 +0100362 return -1;
363 }
364 return 0;
365
366 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
367 if ((parser->global.report_id = item_udata(item)) == 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200368 dbg_hid("report_id 0 is invalid\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100369 return -1;
370 }
371 return 0;
372
373 default:
Jiri Kosina58037eb2007-05-30 15:07:13 +0200374 dbg_hid("unknown global tag 0x%x\n", item->tag);
Jiri Kosinadde58452006-12-08 18:40:44 +0100375 return -1;
376 }
377}
378
379/*
380 * Process a local item.
381 */
382
383static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
384{
385 __u32 data;
386 unsigned n;
387
388 if (item->size == 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200389 dbg_hid("item data expected for local item\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100390 return -1;
391 }
392
393 data = item_udata(item);
394
395 switch (item->tag) {
396
397 case HID_LOCAL_ITEM_TAG_DELIMITER:
398
399 if (data) {
400 /*
401 * We treat items before the first delimiter
402 * as global to all usage sets (branch 0).
403 * In the moment we process only these global
404 * items and the first delimiter set.
405 */
406 if (parser->local.delimiter_depth != 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200407 dbg_hid("nested delimiters\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100408 return -1;
409 }
410 parser->local.delimiter_depth++;
411 parser->local.delimiter_branch++;
412 } else {
413 if (parser->local.delimiter_depth < 1) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200414 dbg_hid("bogus close delimiter\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100415 return -1;
416 }
417 parser->local.delimiter_depth--;
418 }
419 return 1;
420
421 case HID_LOCAL_ITEM_TAG_USAGE:
422
423 if (parser->local.delimiter_branch > 1) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200424 dbg_hid("alternative usage ignored\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100425 return 0;
426 }
427
428 if (item->size <= 2)
429 data = (parser->global.usage_page << 16) + data;
430
431 return hid_add_usage(parser, data);
432
433 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
434
435 if (parser->local.delimiter_branch > 1) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200436 dbg_hid("alternative usage ignored\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100437 return 0;
438 }
439
440 if (item->size <= 2)
441 data = (parser->global.usage_page << 16) + data;
442
443 parser->local.usage_minimum = data;
444 return 0;
445
446 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
447
448 if (parser->local.delimiter_branch > 1) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200449 dbg_hid("alternative usage ignored\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100450 return 0;
451 }
452
453 if (item->size <= 2)
454 data = (parser->global.usage_page << 16) + data;
455
456 for (n = parser->local.usage_minimum; n <= data; n++)
457 if (hid_add_usage(parser, n)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200458 dbg_hid("hid_add_usage failed\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100459 return -1;
460 }
461 return 0;
462
463 default:
464
Jiri Kosina58037eb2007-05-30 15:07:13 +0200465 dbg_hid("unknown local item tag 0x%x\n", item->tag);
Jiri Kosinadde58452006-12-08 18:40:44 +0100466 return 0;
467 }
468 return 0;
469}
470
471/*
472 * Process a main item.
473 */
474
475static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
476{
477 __u32 data;
478 int ret;
479
480 data = item_udata(item);
481
482 switch (item->tag) {
483 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
484 ret = open_collection(parser, data & 0xff);
485 break;
486 case HID_MAIN_ITEM_TAG_END_COLLECTION:
487 ret = close_collection(parser);
488 break;
489 case HID_MAIN_ITEM_TAG_INPUT:
490 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
491 break;
492 case HID_MAIN_ITEM_TAG_OUTPUT:
493 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
494 break;
495 case HID_MAIN_ITEM_TAG_FEATURE:
496 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
497 break;
498 default:
Jiri Kosina58037eb2007-05-30 15:07:13 +0200499 dbg_hid("unknown main item tag 0x%x\n", item->tag);
Jiri Kosinadde58452006-12-08 18:40:44 +0100500 ret = 0;
501 }
502
503 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
504
505 return ret;
506}
507
508/*
509 * Process a reserved item.
510 */
511
512static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
513{
Jiri Kosina58037eb2007-05-30 15:07:13 +0200514 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
Jiri Kosinadde58452006-12-08 18:40:44 +0100515 return 0;
516}
517
518/*
519 * Free a report and all registered fields. The field->usage and
520 * field->value table's are allocated behind the field, so we need
521 * only to free(field) itself.
522 */
523
524static void hid_free_report(struct hid_report *report)
525{
526 unsigned n;
527
528 for (n = 0; n < report->maxfield; n++)
529 kfree(report->field[n]);
530 kfree(report);
531}
532
533/*
534 * Free a device structure, all reports, and all fields.
535 */
536
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200537static void hid_device_release(struct device *dev)
Jiri Kosinadde58452006-12-08 18:40:44 +0100538{
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200539 struct hid_device *device = container_of(dev, struct hid_device, dev);
540 unsigned i, j;
Jiri Kosinadde58452006-12-08 18:40:44 +0100541
542 for (i = 0; i < HID_REPORT_TYPES; i++) {
543 struct hid_report_enum *report_enum = device->report_enum + i;
544
545 for (j = 0; j < 256; j++) {
546 struct hid_report *report = report_enum->report_id_hash[j];
547 if (report)
548 hid_free_report(report);
549 }
550 }
551
552 kfree(device->rdesc);
Jiri Kosina767fe782007-01-24 23:05:07 +0100553 kfree(device->collection);
Jiri Kosinadde58452006-12-08 18:40:44 +0100554 kfree(device);
555}
556
557/*
558 * Fetch a report description item from the data stream. We support long
559 * items, though they are not used yet.
560 */
561
562static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
563{
564 u8 b;
565
566 if ((end - start) <= 0)
567 return NULL;
568
569 b = *start++;
570
571 item->type = (b >> 2) & 3;
572 item->tag = (b >> 4) & 15;
573
574 if (item->tag == HID_ITEM_TAG_LONG) {
575
576 item->format = HID_ITEM_FORMAT_LONG;
577
578 if ((end - start) < 2)
579 return NULL;
580
581 item->size = *start++;
582 item->tag = *start++;
583
584 if ((end - start) < item->size)
585 return NULL;
586
587 item->data.longdata = start;
588 start += item->size;
589 return start;
590 }
591
592 item->format = HID_ITEM_FORMAT_SHORT;
593 item->size = b & 3;
594
595 switch (item->size) {
596
597 case 0:
598 return start;
599
600 case 1:
601 if ((end - start) < 1)
602 return NULL;
603 item->data.u8 = *start++;
604 return start;
605
606 case 2:
607 if ((end - start) < 2)
608 return NULL;
Harvey Harrisonc1050682008-04-29 01:03:31 -0700609 item->data.u16 = get_unaligned_le16(start);
Jiri Kosinadde58452006-12-08 18:40:44 +0100610 start = (__u8 *)((__le16 *)start + 1);
611 return start;
612
613 case 3:
614 item->size++;
615 if ((end - start) < 4)
616 return NULL;
Harvey Harrisonc1050682008-04-29 01:03:31 -0700617 item->data.u32 = get_unaligned_le32(start);
Jiri Kosinadde58452006-12-08 18:40:44 +0100618 start = (__u8 *)((__le32 *)start + 1);
619 return start;
620 }
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) {
714 case 8: return ((__s8)value);
715 case 16: return ((__s16)value);
716 case 32: return ((__s32)value);
717 }
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 Kosinadde58452006-12-08 18:40:44 +0100860 hid_dump_input(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
982 hid_dump_input(field->usage + offset, value);
983
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 hid_dump_field(field, 8);
987 return -1;
988 }
989 if (field->logical_minimum < 0) {
990 if (value != snto32(s32ton(value, size), size)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200991 dbg_hid("value %d is out of range\n", value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100992 return -1;
993 }
994 }
995 field->value[offset] = value;
996 return 0;
997}
Jiri Kosina229695e2006-12-08 18:40:53 +0100998EXPORT_SYMBOL_GPL(hid_set_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100999
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001000static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1001 const u8 *data)
1002{
1003 struct hid_report *report;
1004 unsigned int n = 0; /* Normally report number is 0 */
1005
1006 /* Device uses numbered reports, data[0] is report number */
1007 if (report_enum->numbered)
1008 n = *data;
1009
1010 report = report_enum->report_id_hash[n];
1011 if (report == NULL)
1012 dbg_hid("undefined report_id %u received\n", n);
1013
1014 return report;
1015}
1016
1017void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1018 int interrupt)
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001019{
1020 struct hid_report_enum *report_enum = hid->report_enum + type;
1021 struct hid_report *report;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001022 unsigned int a;
1023 int rsize, csize = size;
1024 u8 *cdata = data;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001025
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001026 report = hid_get_report(report_enum, data);
1027 if (!report)
1028 return;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001029
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001030 if (report_enum->numbered) {
1031 cdata++;
1032 csize--;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001033 }
1034
1035 rsize = ((report->size - 1) >> 3) + 1;
1036
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001037 if (csize < rsize) {
1038 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1039 csize, rsize);
1040 memset(cdata + csize, 0, rsize - csize);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001041 }
1042
1043 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1044 hid->hiddev_report_event(hid, report);
Jiri Kosinab54ec3c2008-03-28 14:11:22 +01001045 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1046 /* numbered reports need to be passed with the report num */
1047 if (report_enum->numbered)
1048 hidraw_report_event(hid, data - 1, size + 1);
1049 else
1050 hidraw_report_event(hid, data, size);
1051 }
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001052
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001053 for (a = 0; a < report->maxfield; a++)
1054 hid_input_field(hid, report->field[a], cdata, interrupt);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001055
1056 if (hid->claimed & HID_CLAIMED_INPUT)
1057 hidinput_report_event(hid, report);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001058}
1059EXPORT_SYMBOL_GPL(hid_report_raw_event);
1060
1061/**
1062 * hid_input_report - report data from lower layer (usb, bt...)
1063 *
1064 * @hid: hid device
1065 * @type: HID report type (HID_*_REPORT)
1066 * @data: report contents
1067 * @size: size of data parameter
1068 * @interrupt: called from atomic?
1069 *
1070 * This is data entry for lower layers.
1071 */
1072int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1073{
1074 struct hid_report_enum *report_enum = hid->report_enum + type;
1075 struct hid_driver *hdrv = hid->driver;
1076 struct hid_report *report;
1077 unsigned int i;
1078 int ret;
1079
1080 if (!hid || !hid->driver)
1081 return -ENODEV;
1082
1083 if (!size) {
1084 dbg_hid("empty report\n");
1085 return -1;
1086 }
1087
1088 dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1089
1090 report = hid_get_report(report_enum, data);
1091 if (!report)
1092 return -1;
1093
1094 /* dump the report */
1095 dbg_hid("report %d (size %u) = ", report->id, size);
1096 for (i = 0; i < size; i++)
1097 dbg_hid_line(" %02x", data[i]);
1098 dbg_hid_line("\n");
1099
1100 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1101 ret = hdrv->raw_event(hid, report, data, size);
1102 if (ret != 0)
1103 return ret < 0 ? ret : 0;
1104 }
1105
1106 hid_report_raw_event(hid, type, data, size, interrupt);
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +01001107
1108 return 0;
1109}
1110EXPORT_SYMBOL_GPL(hid_input_report);
1111
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001112static bool hid_match_one_id(struct hid_device *hdev,
1113 const struct hid_device_id *id)
1114{
1115 return id->bus == hdev->bus &&
1116 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1117 (id->product == HID_ANY_ID || id->product == hdev->product);
1118}
1119
1120static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1121 const struct hid_device_id *id)
1122{
1123 for (; id->bus; id++)
1124 if (hid_match_one_id(hdev, id))
1125 return id;
1126
1127 return NULL;
1128}
1129
1130static const struct hid_device_id hid_blacklist[] = {
1131 { }
1132};
1133
1134static int hid_bus_match(struct device *dev, struct device_driver *drv)
1135{
1136 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1137 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1138
1139 if (!hid_match_id(hdev, hdrv->id_table))
1140 return 0;
1141
1142 /* generic wants all non-blacklisted */
1143 if (!strncmp(hdrv->name, "generic-", 8))
1144 return !hid_match_id(hdev, hid_blacklist);
1145
1146 return 1;
1147}
1148
1149static int hid_device_probe(struct device *dev)
1150{
1151 struct hid_driver *hdrv = container_of(dev->driver,
1152 struct hid_driver, driver);
1153 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1154 const struct hid_device_id *id;
1155 int ret = 0;
1156
1157 if (!hdev->driver) {
Jiri Slabyc500c972008-05-16 11:49:16 +02001158 id = hid_match_id(hdev, hdrv->id_table);
1159 if (id == NULL)
1160 return -ENODEV;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001161
Jiri Slabyc500c972008-05-16 11:49:16 +02001162 hdev->driver = hdrv;
1163 if (hdrv->probe) {
1164 ret = hdrv->probe(hdev, id);
1165 } else { /* default probe */
1166 ret = hid_parse(hdev);
1167 if (!ret)
1168 ret = hid_hw_start(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001169 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001170 if (ret)
1171 hdev->driver = NULL;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001172 }
1173 return ret;
1174}
1175
1176static int hid_device_remove(struct device *dev)
1177{
1178 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1179 struct hid_driver *hdrv = hdev->driver;
1180
1181 if (hdrv) {
1182 if (hdrv->remove)
1183 hdrv->remove(hdev);
Jiri Slabyc500c972008-05-16 11:49:16 +02001184 else /* default remove */
1185 hid_hw_stop(hdev);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001186 hdev->driver = NULL;
1187 }
1188
1189 return 0;
1190}
1191
1192static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1193{
1194 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1195
1196 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1197 hdev->bus, hdev->vendor, hdev->product))
1198 return -ENOMEM;
1199
1200 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1201 return -ENOMEM;
1202
1203 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1204 return -ENOMEM;
1205
1206 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1207 return -ENOMEM;
1208
1209 if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1210 hdev->bus, hdev->vendor, hdev->product))
1211 return -ENOMEM;
1212
1213 return 0;
1214}
1215
1216static struct bus_type hid_bus_type = {
1217 .name = "hid",
1218 .match = hid_bus_match,
1219 .probe = hid_device_probe,
1220 .remove = hid_device_remove,
1221 .uevent = hid_uevent,
1222};
1223
1224int hid_add_device(struct hid_device *hdev)
1225{
1226 static atomic_t id = ATOMIC_INIT(0);
1227 int ret;
1228
1229 if (WARN_ON(hdev->status & HID_STAT_ADDED))
1230 return -EBUSY;
1231
1232 /* XXX hack, any other cleaner solution < 20 bus_id bytes? */
1233 sprintf(hdev->dev.bus_id, "%04X:%04X:%04X.%04X", hdev->bus,
1234 hdev->vendor, hdev->product, atomic_inc_return(&id));
1235
1236 ret = device_add(&hdev->dev);
1237 if (!ret)
1238 hdev->status |= HID_STAT_ADDED;
1239
1240 return ret;
1241}
1242EXPORT_SYMBOL_GPL(hid_add_device);
1243
1244/**
1245 * hid_allocate_device - allocate new hid device descriptor
1246 *
1247 * Allocate and initialize hid device, so that hid_destroy_device might be
1248 * used to free it.
1249 *
1250 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1251 * error value.
1252 */
1253struct hid_device *hid_allocate_device(void)
1254{
1255 struct hid_device *hdev;
1256 unsigned int i;
1257 int ret = -ENOMEM;
1258
1259 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1260 if (hdev == NULL)
1261 return ERR_PTR(ret);
1262
1263 device_initialize(&hdev->dev);
1264 hdev->dev.release = hid_device_release;
1265 hdev->dev.bus = &hid_bus_type;
1266
1267 hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1268 sizeof(struct hid_collection), GFP_KERNEL);
1269 if (hdev->collection == NULL)
1270 goto err;
1271 hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1272
1273 for (i = 0; i < HID_REPORT_TYPES; i++)
1274 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1275
1276 return hdev;
1277err:
1278 put_device(&hdev->dev);
1279 return ERR_PTR(ret);
1280}
1281EXPORT_SYMBOL_GPL(hid_allocate_device);
1282
1283static void hid_remove_device(struct hid_device *hdev)
1284{
1285 if (hdev->status & HID_STAT_ADDED) {
1286 device_del(&hdev->dev);
1287 hdev->status &= ~HID_STAT_ADDED;
1288 }
1289}
1290
1291/**
1292 * hid_destroy_device - free previously allocated device
1293 *
1294 * @hdev: hid device
1295 *
1296 * If you allocate hid_device through hid_allocate_device, you should ever
1297 * free by this function.
1298 */
1299void hid_destroy_device(struct hid_device *hdev)
1300{
1301 hid_remove_device(hdev);
1302 put_device(&hdev->dev);
1303}
1304EXPORT_SYMBOL_GPL(hid_destroy_device);
1305
1306int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1307 const char *mod_name)
1308{
1309 hdrv->driver.name = hdrv->name;
1310 hdrv->driver.bus = &hid_bus_type;
1311 hdrv->driver.owner = owner;
1312 hdrv->driver.mod_name = mod_name;
1313
1314 return driver_register(&hdrv->driver);
1315}
1316EXPORT_SYMBOL_GPL(__hid_register_driver);
1317
1318void hid_unregister_driver(struct hid_driver *hdrv)
1319{
1320 driver_unregister(&hdrv->driver);
1321}
1322EXPORT_SYMBOL_GPL(hid_unregister_driver);
1323
Jiri Kosina86166b72007-05-14 09:57:40 +02001324static int __init hid_init(void)
1325{
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001326 int ret;
1327
1328 ret = bus_register(&hid_bus_type);
1329 if (ret) {
1330 printk(KERN_ERR "HID: can't register hid bus\n");
1331 goto err;
1332 }
1333
1334 ret = hidraw_init();
1335 if (ret)
1336 goto err_bus;
1337
1338 return 0;
1339err_bus:
1340 bus_unregister(&hid_bus_type);
1341err:
1342 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +02001343}
1344
1345static void __exit hid_exit(void)
1346{
1347 hidraw_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001348 bus_unregister(&hid_bus_type);
Jiri Kosina86166b72007-05-14 09:57:40 +02001349}
1350
1351module_init(hid_init);
1352module_exit(hid_exit);
1353
Jiri Kosinaaa938f72006-12-08 18:41:10 +01001354MODULE_LICENSE(DRIVER_LICENSE);
1355