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