HID: provide a helper for validating hid reports
[firefly-linux-kernel-4.4.55.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 #define DRIVER_LICENSE "GPL"
47
48 int hid_debug = 0;
49 module_param_named(debug, hid_debug, int, 0600);
50 MODULE_PARM_DESC(debug, "toggle HID debugging messages");
51 EXPORT_SYMBOL_GPL(hid_debug);
52
53 static int hid_ignore_special_drivers = 0;
54 module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
55 MODULE_PARM_DESC(debug, "Ignore any special drivers and handle all devices by generic driver");
56
57 /*
58  * Register a new report for a device.
59  */
60
61 struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
62 {
63         struct hid_report_enum *report_enum = device->report_enum + type;
64         struct hid_report *report;
65
66         if (id >= HID_MAX_IDS)
67                 return NULL;
68         if (report_enum->report_id_hash[id])
69                 return report_enum->report_id_hash[id];
70
71         report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
72         if (!report)
73                 return NULL;
74
75         if (id != 0)
76                 report_enum->numbered = 1;
77
78         report->id = id;
79         report->type = type;
80         report->size = 0;
81         report->device = device;
82         report_enum->report_id_hash[id] = report;
83
84         list_add_tail(&report->list, &report_enum->report_list);
85
86         return report;
87 }
88 EXPORT_SYMBOL_GPL(hid_register_report);
89
90 /*
91  * Register a new field for this report.
92  */
93
94 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
95 {
96         struct hid_field *field;
97         int i;
98
99         if (report->maxfield == HID_MAX_FIELDS) {
100                 hid_err(report->device, "too many fields in report\n");
101                 return NULL;
102         }
103
104         field = kzalloc((sizeof(struct hid_field) +
105                          usages * sizeof(struct hid_usage) +
106                          values * sizeof(unsigned)), GFP_KERNEL);
107         if (!field)
108                 return NULL;
109
110         field->index = report->maxfield++;
111         report->field[field->index] = field;
112         field->usage = (struct hid_usage *)(field + 1);
113         field->value = (s32 *)(field->usage + usages);
114         field->report = report;
115
116         for (i = 0; i < usages; i++)
117                 field->usage[i].usage_index = i;
118
119         return field;
120 }
121
122 /*
123  * Open a collection. The type/usage is pushed on the stack.
124  */
125
126 static int open_collection(struct hid_parser *parser, unsigned type)
127 {
128         struct hid_collection *collection;
129         unsigned usage;
130
131         usage = parser->local.usage[0];
132
133         if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
134                 hid_err(parser->device, "collection stack overflow\n");
135                 return -EINVAL;
136         }
137
138         if (parser->device->maxcollection == parser->device->collection_size) {
139                 collection = kmalloc(sizeof(struct hid_collection) *
140                                 parser->device->collection_size * 2, GFP_KERNEL);
141                 if (collection == NULL) {
142                         hid_err(parser->device, "failed to reallocate collection array\n");
143                         return -ENOMEM;
144                 }
145                 memcpy(collection, parser->device->collection,
146                         sizeof(struct hid_collection) *
147                         parser->device->collection_size);
148                 memset(collection + parser->device->collection_size, 0,
149                         sizeof(struct hid_collection) *
150                         parser->device->collection_size);
151                 kfree(parser->device->collection);
152                 parser->device->collection = collection;
153                 parser->device->collection_size *= 2;
154         }
155
156         parser->collection_stack[parser->collection_stack_ptr++] =
157                 parser->device->maxcollection;
158
159         collection = parser->device->collection +
160                 parser->device->maxcollection++;
161         collection->type = type;
162         collection->usage = usage;
163         collection->level = parser->collection_stack_ptr - 1;
164
165         if (type == HID_COLLECTION_APPLICATION)
166                 parser->device->maxapplication++;
167
168         return 0;
169 }
170
171 /*
172  * Close a collection.
173  */
174
175 static int close_collection(struct hid_parser *parser)
176 {
177         if (!parser->collection_stack_ptr) {
178                 hid_err(parser->device, "collection stack underflow\n");
179                 return -EINVAL;
180         }
181         parser->collection_stack_ptr--;
182         return 0;
183 }
184
185 /*
186  * Climb up the stack, search for the specified collection type
187  * and return the usage.
188  */
189
190 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
191 {
192         struct hid_collection *collection = parser->device->collection;
193         int n;
194
195         for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
196                 unsigned index = parser->collection_stack[n];
197                 if (collection[index].type == type)
198                         return collection[index].usage;
199         }
200         return 0; /* we know nothing about this usage type */
201 }
202
203 /*
204  * Add a usage to the temporary parser table.
205  */
206
207 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
208 {
209         if (parser->local.usage_index >= HID_MAX_USAGES) {
210                 hid_err(parser->device, "usage index exceeded\n");
211                 return -1;
212         }
213         parser->local.usage[parser->local.usage_index] = usage;
214         parser->local.collection_index[parser->local.usage_index] =
215                 parser->collection_stack_ptr ?
216                 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
217         parser->local.usage_index++;
218         return 0;
219 }
220
221 /*
222  * Register a new field for this report.
223  */
224
225 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
226 {
227         struct hid_report *report;
228         struct hid_field *field;
229         int usages;
230         unsigned offset;
231         int i;
232
233         report = hid_register_report(parser->device, report_type, parser->global.report_id);
234         if (!report) {
235                 hid_err(parser->device, "hid_register_report failed\n");
236                 return -1;
237         }
238
239         /* Handle both signed and unsigned cases properly */
240         if ((parser->global.logical_minimum < 0 &&
241                 parser->global.logical_maximum <
242                 parser->global.logical_minimum) ||
243                 (parser->global.logical_minimum >= 0 &&
244                 (__u32)parser->global.logical_maximum <
245                 (__u32)parser->global.logical_minimum)) {
246                 dbg_hid("logical range invalid 0x%x 0x%x\n",
247                         parser->global.logical_minimum,
248                         parser->global.logical_maximum);
249                 return -1;
250         }
251
252         offset = report->size;
253         report->size += parser->global.report_size * parser->global.report_count;
254
255         if (!parser->local.usage_index) /* Ignore padding fields */
256                 return 0;
257
258         usages = max_t(int, parser->local.usage_index, parser->global.report_count);
259
260         field = hid_register_field(report, usages, parser->global.report_count);
261         if (!field)
262                 return 0;
263
264         field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
265         field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
266         field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
267
268         for (i = 0; i < usages; i++) {
269                 int j = i;
270                 /* Duplicate the last usage we parsed if we have excess values */
271                 if (i >= parser->local.usage_index)
272                         j = parser->local.usage_index - 1;
273                 field->usage[i].hid = parser->local.usage[j];
274                 field->usage[i].collection_index =
275                         parser->local.collection_index[j];
276         }
277
278         field->maxusage = usages;
279         field->flags = flags;
280         field->report_offset = offset;
281         field->report_type = report_type;
282         field->report_size = parser->global.report_size;
283         field->report_count = parser->global.report_count;
284         field->logical_minimum = parser->global.logical_minimum;
285         field->logical_maximum = parser->global.logical_maximum;
286         field->physical_minimum = parser->global.physical_minimum;
287         field->physical_maximum = parser->global.physical_maximum;
288         field->unit_exponent = parser->global.unit_exponent;
289         field->unit = parser->global.unit;
290
291         return 0;
292 }
293
294 /*
295  * Read data value from item.
296  */
297
298 static u32 item_udata(struct hid_item *item)
299 {
300         switch (item->size) {
301         case 1: return item->data.u8;
302         case 2: return item->data.u16;
303         case 4: return item->data.u32;
304         }
305         return 0;
306 }
307
308 static s32 item_sdata(struct hid_item *item)
309 {
310         switch (item->size) {
311         case 1: return item->data.s8;
312         case 2: return item->data.s16;
313         case 4: return item->data.s32;
314         }
315         return 0;
316 }
317
318 /*
319  * Process a global item.
320  */
321
322 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
323 {
324         __u32 raw_value;
325         switch (item->tag) {
326         case HID_GLOBAL_ITEM_TAG_PUSH:
327
328                 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
329                         hid_err(parser->device, "global environment stack overflow\n");
330                         return -1;
331                 }
332
333                 memcpy(parser->global_stack + parser->global_stack_ptr++,
334                         &parser->global, sizeof(struct hid_global));
335                 return 0;
336
337         case HID_GLOBAL_ITEM_TAG_POP:
338
339                 if (!parser->global_stack_ptr) {
340                         hid_err(parser->device, "global environment stack underflow\n");
341                         return -1;
342                 }
343
344                 memcpy(&parser->global, parser->global_stack +
345                         --parser->global_stack_ptr, sizeof(struct hid_global));
346                 return 0;
347
348         case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
349                 parser->global.usage_page = item_udata(item);
350                 return 0;
351
352         case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
353                 parser->global.logical_minimum = item_sdata(item);
354                 return 0;
355
356         case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
357                 if (parser->global.logical_minimum < 0)
358                         parser->global.logical_maximum = item_sdata(item);
359                 else
360                         parser->global.logical_maximum = item_udata(item);
361                 return 0;
362
363         case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
364                 parser->global.physical_minimum = item_sdata(item);
365                 return 0;
366
367         case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
368                 if (parser->global.physical_minimum < 0)
369                         parser->global.physical_maximum = item_sdata(item);
370                 else
371                         parser->global.physical_maximum = item_udata(item);
372                 return 0;
373
374         case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
375                 /* Units exponent negative numbers are given through a
376                  * two's complement.
377                  * See "6.2.2.7 Global Items" for more information. */
378                 raw_value = item_udata(item);
379                 if (!(raw_value & 0xfffffff0))
380                         parser->global.unit_exponent = hid_snto32(raw_value, 4);
381                 else
382                         parser->global.unit_exponent = raw_value;
383                 return 0;
384
385         case HID_GLOBAL_ITEM_TAG_UNIT:
386                 parser->global.unit = item_udata(item);
387                 return 0;
388
389         case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
390                 parser->global.report_size = item_udata(item);
391                 if (parser->global.report_size > 128) {
392                         hid_err(parser->device, "invalid report_size %d\n",
393                                         parser->global.report_size);
394                         return -1;
395                 }
396                 return 0;
397
398         case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
399                 parser->global.report_count = item_udata(item);
400                 if (parser->global.report_count > HID_MAX_USAGES) {
401                         hid_err(parser->device, "invalid report_count %d\n",
402                                         parser->global.report_count);
403                         return -1;
404                 }
405                 return 0;
406
407         case HID_GLOBAL_ITEM_TAG_REPORT_ID:
408                 parser->global.report_id = item_udata(item);
409                 if (parser->global.report_id == 0 ||
410                     parser->global.report_id >= HID_MAX_IDS) {
411                         hid_err(parser->device, "report_id %u is invalid\n",
412                                 parser->global.report_id);
413                         return -1;
414                 }
415                 return 0;
416
417         default:
418                 hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
419                 return -1;
420         }
421 }
422
423 /*
424  * Process a local item.
425  */
426
427 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
428 {
429         __u32 data;
430         unsigned n;
431
432         data = item_udata(item);
433
434         switch (item->tag) {
435         case HID_LOCAL_ITEM_TAG_DELIMITER:
436
437                 if (data) {
438                         /*
439                          * We treat items before the first delimiter
440                          * as global to all usage sets (branch 0).
441                          * In the moment we process only these global
442                          * items and the first delimiter set.
443                          */
444                         if (parser->local.delimiter_depth != 0) {
445                                 hid_err(parser->device, "nested delimiters\n");
446                                 return -1;
447                         }
448                         parser->local.delimiter_depth++;
449                         parser->local.delimiter_branch++;
450                 } else {
451                         if (parser->local.delimiter_depth < 1) {
452                                 hid_err(parser->device, "bogus close delimiter\n");
453                                 return -1;
454                         }
455                         parser->local.delimiter_depth--;
456                 }
457                 return 1;
458
459         case HID_LOCAL_ITEM_TAG_USAGE:
460
461                 if (parser->local.delimiter_branch > 1) {
462                         dbg_hid("alternative usage ignored\n");
463                         return 0;
464                 }
465
466                 if (item->size <= 2)
467                         data = (parser->global.usage_page << 16) + data;
468
469                 return hid_add_usage(parser, data);
470
471         case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
472
473                 if (parser->local.delimiter_branch > 1) {
474                         dbg_hid("alternative usage ignored\n");
475                         return 0;
476                 }
477
478                 if (item->size <= 2)
479                         data = (parser->global.usage_page << 16) + data;
480
481                 parser->local.usage_minimum = data;
482                 return 0;
483
484         case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
485
486                 if (parser->local.delimiter_branch > 1) {
487                         dbg_hid("alternative usage ignored\n");
488                         return 0;
489                 }
490
491                 if (item->size <= 2)
492                         data = (parser->global.usage_page << 16) + data;
493
494                 for (n = parser->local.usage_minimum; n <= data; n++)
495                         if (hid_add_usage(parser, n)) {
496                                 dbg_hid("hid_add_usage failed\n");
497                                 return -1;
498                         }
499                 return 0;
500
501         default:
502
503                 dbg_hid("unknown local item tag 0x%x\n", item->tag);
504                 return 0;
505         }
506         return 0;
507 }
508
509 /*
510  * Process a main item.
511  */
512
513 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
514 {
515         __u32 data;
516         int ret;
517
518         data = item_udata(item);
519
520         switch (item->tag) {
521         case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
522                 ret = open_collection(parser, data & 0xff);
523                 break;
524         case HID_MAIN_ITEM_TAG_END_COLLECTION:
525                 ret = close_collection(parser);
526                 break;
527         case HID_MAIN_ITEM_TAG_INPUT:
528                 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
529                 break;
530         case HID_MAIN_ITEM_TAG_OUTPUT:
531                 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
532                 break;
533         case HID_MAIN_ITEM_TAG_FEATURE:
534                 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
535                 break;
536         default:
537                 hid_err(parser->device, "unknown main item tag 0x%x\n", item->tag);
538                 ret = 0;
539         }
540
541         memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
542
543         return ret;
544 }
545
546 /*
547  * Process a reserved item.
548  */
549
550 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
551 {
552         dbg_hid("reserved item type, tag 0x%x\n", item->tag);
553         return 0;
554 }
555
556 /*
557  * Free a report and all registered fields. The field->usage and
558  * field->value table's are allocated behind the field, so we need
559  * only to free(field) itself.
560  */
561
562 static void hid_free_report(struct hid_report *report)
563 {
564         unsigned n;
565
566         for (n = 0; n < report->maxfield; n++)
567                 kfree(report->field[n]);
568         kfree(report);
569 }
570
571 /*
572  * Close report. This function returns the device
573  * state to the point prior to hid_open_report().
574  */
575 static void hid_close_report(struct hid_device *device)
576 {
577         unsigned i, j;
578
579         for (i = 0; i < HID_REPORT_TYPES; i++) {
580                 struct hid_report_enum *report_enum = device->report_enum + i;
581
582                 for (j = 0; j < HID_MAX_IDS; j++) {
583                         struct hid_report *report = report_enum->report_id_hash[j];
584                         if (report)
585                                 hid_free_report(report);
586                 }
587                 memset(report_enum, 0, sizeof(*report_enum));
588                 INIT_LIST_HEAD(&report_enum->report_list);
589         }
590
591         kfree(device->rdesc);
592         device->rdesc = NULL;
593         device->rsize = 0;
594
595         kfree(device->collection);
596         device->collection = NULL;
597         device->collection_size = 0;
598         device->maxcollection = 0;
599         device->maxapplication = 0;
600
601         device->status &= ~HID_STAT_PARSED;
602 }
603
604 /*
605  * Free a device structure, all reports, and all fields.
606  */
607
608 static void hid_device_release(struct device *dev)
609 {
610         struct hid_device *hid = container_of(dev, struct hid_device, dev);
611
612         hid_close_report(hid);
613         kfree(hid->dev_rdesc);
614         kfree(hid);
615 }
616
617 /*
618  * Fetch a report description item from the data stream. We support long
619  * items, though they are not used yet.
620  */
621
622 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
623 {
624         u8 b;
625
626         if ((end - start) <= 0)
627                 return NULL;
628
629         b = *start++;
630
631         item->type = (b >> 2) & 3;
632         item->tag  = (b >> 4) & 15;
633
634         if (item->tag == HID_ITEM_TAG_LONG) {
635
636                 item->format = HID_ITEM_FORMAT_LONG;
637
638                 if ((end - start) < 2)
639                         return NULL;
640
641                 item->size = *start++;
642                 item->tag  = *start++;
643
644                 if ((end - start) < item->size)
645                         return NULL;
646
647                 item->data.longdata = start;
648                 start += item->size;
649                 return start;
650         }
651
652         item->format = HID_ITEM_FORMAT_SHORT;
653         item->size = b & 3;
654
655         switch (item->size) {
656         case 0:
657                 return start;
658
659         case 1:
660                 if ((end - start) < 1)
661                         return NULL;
662                 item->data.u8 = *start++;
663                 return start;
664
665         case 2:
666                 if ((end - start) < 2)
667                         return NULL;
668                 item->data.u16 = get_unaligned_le16(start);
669                 start = (__u8 *)((__le16 *)start + 1);
670                 return start;
671
672         case 3:
673                 item->size++;
674                 if ((end - start) < 4)
675                         return NULL;
676                 item->data.u32 = get_unaligned_le32(start);
677                 start = (__u8 *)((__le32 *)start + 1);
678                 return start;
679         }
680
681         return NULL;
682 }
683
684 static void hid_scan_usage(struct hid_device *hid, u32 usage)
685 {
686         if (usage == HID_DG_CONTACTID)
687                 hid->group = HID_GROUP_MULTITOUCH;
688 }
689
690 /*
691  * Scan a report descriptor before the device is added to the bus.
692  * Sets device groups and other properties that determine what driver
693  * to load.
694  */
695 static int hid_scan_report(struct hid_device *hid)
696 {
697         unsigned int page = 0, delim = 0;
698         __u8 *start = hid->dev_rdesc;
699         __u8 *end = start + hid->dev_rsize;
700         unsigned int u, u_min = 0, u_max = 0;
701         struct hid_item item;
702
703         hid->group = HID_GROUP_GENERIC;
704         while ((start = fetch_item(start, end, &item)) != NULL) {
705                 if (item.format != HID_ITEM_FORMAT_SHORT)
706                         return -EINVAL;
707                 if (item.type == HID_ITEM_TYPE_GLOBAL) {
708                         if (item.tag == HID_GLOBAL_ITEM_TAG_USAGE_PAGE)
709                                 page = item_udata(&item) << 16;
710                 } else if (item.type == HID_ITEM_TYPE_LOCAL) {
711                         if (delim > 1)
712                                 break;
713                         u = item_udata(&item);
714                         if (item.size <= 2)
715                                 u += page;
716                         switch (item.tag) {
717                         case HID_LOCAL_ITEM_TAG_DELIMITER:
718                                 delim += !!u;
719                                 break;
720                         case HID_LOCAL_ITEM_TAG_USAGE:
721                                 hid_scan_usage(hid, u);
722                                 break;
723                         case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
724                                 u_min = u;
725                                 break;
726                         case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
727                                 u_max = u;
728                                 for (u = u_min; u <= u_max; u++)
729                                         hid_scan_usage(hid, u);
730                                 break;
731                         }
732                 } else if (page == HID_UP_SENSOR &&
733                         item.type == HID_ITEM_TYPE_MAIN &&
734                         item.tag == HID_MAIN_ITEM_TAG_BEGIN_COLLECTION &&
735                         (item_udata(&item) & 0xff) == HID_COLLECTION_PHYSICAL)
736                         hid->group = HID_GROUP_SENSOR_HUB;
737         }
738
739         return 0;
740 }
741
742 /**
743  * hid_parse_report - parse device report
744  *
745  * @device: hid device
746  * @start: report start
747  * @size: report size
748  *
749  * Allocate the device report as read by the bus driver. This function should
750  * only be called from parse() in ll drivers.
751  */
752 int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
753 {
754         hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
755         if (!hid->dev_rdesc)
756                 return -ENOMEM;
757         hid->dev_rsize = size;
758         return 0;
759 }
760 EXPORT_SYMBOL_GPL(hid_parse_report);
761
762 static const char * const hid_report_names[] = {
763         "HID_INPUT_REPORT",
764         "HID_OUTPUT_REPORT",
765         "HID_FEATURE_REPORT",
766 };
767 /**
768  * hid_validate_values - validate existing device report's value indexes
769  *
770  * @device: hid device
771  * @type: which report type to examine
772  * @id: which report ID to examine (0 for first)
773  * @field_index: which report field to examine
774  * @report_counts: expected number of values
775  *
776  * Validate the number of values in a given field of a given report, after
777  * parsing.
778  */
779 struct hid_report *hid_validate_values(struct hid_device *hid,
780                                        unsigned int type, unsigned int id,
781                                        unsigned int field_index,
782                                        unsigned int report_counts)
783 {
784         struct hid_report *report;
785
786         if (type > HID_FEATURE_REPORT) {
787                 hid_err(hid, "invalid HID report type %u\n", type);
788                 return NULL;
789         }
790
791         if (id >= HID_MAX_IDS) {
792                 hid_err(hid, "invalid HID report id %u\n", id);
793                 return NULL;
794         }
795
796         /*
797          * Explicitly not using hid_get_report() here since it depends on
798          * ->numbered being checked, which may not always be the case when
799          * drivers go to access report values.
800          */
801         report = hid->report_enum[type].report_id_hash[id];
802         if (!report) {
803                 hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
804                 return NULL;
805         }
806         if (report->maxfield <= field_index) {
807                 hid_err(hid, "not enough fields in %s %u\n",
808                         hid_report_names[type], id);
809                 return NULL;
810         }
811         if (report->field[field_index]->report_count < report_counts) {
812                 hid_err(hid, "not enough values in %s %u field %u\n",
813                         hid_report_names[type], id, field_index);
814                 return NULL;
815         }
816         return report;
817 }
818 EXPORT_SYMBOL_GPL(hid_validate_values);
819
820 /**
821  * hid_open_report - open a driver-specific device report
822  *
823  * @device: hid device
824  *
825  * Parse a report description into a hid_device structure. Reports are
826  * enumerated, fields are attached to these reports.
827  * 0 returned on success, otherwise nonzero error value.
828  *
829  * This function (or the equivalent hid_parse() macro) should only be
830  * called from probe() in drivers, before starting the device.
831  */
832 int hid_open_report(struct hid_device *device)
833 {
834         struct hid_parser *parser;
835         struct hid_item item;
836         unsigned int size;
837         __u8 *start;
838         __u8 *buf;
839         __u8 *end;
840         int ret;
841         static int (*dispatch_type[])(struct hid_parser *parser,
842                                       struct hid_item *item) = {
843                 hid_parser_main,
844                 hid_parser_global,
845                 hid_parser_local,
846                 hid_parser_reserved
847         };
848
849         if (WARN_ON(device->status & HID_STAT_PARSED))
850                 return -EBUSY;
851
852         start = device->dev_rdesc;
853         if (WARN_ON(!start))
854                 return -ENODEV;
855         size = device->dev_rsize;
856
857         buf = kmemdup(start, size, GFP_KERNEL);
858         if (buf == NULL)
859                 return -ENOMEM;
860
861         if (device->driver->report_fixup)
862                 start = device->driver->report_fixup(device, buf, &size);
863         else
864                 start = buf;
865
866         start = kmemdup(start, size, GFP_KERNEL);
867         kfree(buf);
868         if (start == NULL)
869                 return -ENOMEM;
870
871         device->rdesc = start;
872         device->rsize = size;
873
874         parser = vzalloc(sizeof(struct hid_parser));
875         if (!parser) {
876                 ret = -ENOMEM;
877                 goto err;
878         }
879
880         parser->device = device;
881
882         end = start + size;
883
884         device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
885                                      sizeof(struct hid_collection), GFP_KERNEL);
886         if (!device->collection) {
887                 ret = -ENOMEM;
888                 goto err;
889         }
890         device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
891
892         ret = -EINVAL;
893         while ((start = fetch_item(start, end, &item)) != NULL) {
894
895                 if (item.format != HID_ITEM_FORMAT_SHORT) {
896                         hid_err(device, "unexpected long global item\n");
897                         goto err;
898                 }
899
900                 if (dispatch_type[item.type](parser, &item)) {
901                         hid_err(device, "item %u %u %u %u parsing failed\n",
902                                 item.format, (unsigned)item.size,
903                                 (unsigned)item.type, (unsigned)item.tag);
904                         goto err;
905                 }
906
907                 if (start == end) {
908                         if (parser->collection_stack_ptr) {
909                                 hid_err(device, "unbalanced collection at end of report description\n");
910                                 goto err;
911                         }
912                         if (parser->local.delimiter_depth) {
913                                 hid_err(device, "unbalanced delimiter at end of report description\n");
914                                 goto err;
915                         }
916                         vfree(parser);
917                         device->status |= HID_STAT_PARSED;
918                         return 0;
919                 }
920         }
921
922         hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
923 err:
924         vfree(parser);
925         hid_close_report(device);
926         return ret;
927 }
928 EXPORT_SYMBOL_GPL(hid_open_report);
929
930 /*
931  * Convert a signed n-bit integer to signed 32-bit integer. Common
932  * cases are done through the compiler, the screwed things has to be
933  * done by hand.
934  */
935
936 static s32 snto32(__u32 value, unsigned n)
937 {
938         switch (n) {
939         case 8:  return ((__s8)value);
940         case 16: return ((__s16)value);
941         case 32: return ((__s32)value);
942         }
943         return value & (1 << (n - 1)) ? value | (-1 << n) : value;
944 }
945
946 s32 hid_snto32(__u32 value, unsigned n)
947 {
948         return snto32(value, n);
949 }
950 EXPORT_SYMBOL_GPL(hid_snto32);
951
952 /*
953  * Convert a signed 32-bit integer to a signed n-bit integer.
954  */
955
956 static u32 s32ton(__s32 value, unsigned n)
957 {
958         s32 a = value >> (n - 1);
959         if (a && a != -1)
960                 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
961         return value & ((1 << n) - 1);
962 }
963
964 /*
965  * Extract/implement a data field from/to a little endian report (bit array).
966  *
967  * Code sort-of follows HID spec:
968  *     http://www.usb.org/developers/devclass_docs/HID1_11.pdf
969  *
970  * While the USB HID spec allows unlimited length bit fields in "report
971  * descriptors", most devices never use more than 16 bits.
972  * One model of UPS is claimed to report "LINEV" as a 32-bit field.
973  * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
974  */
975
976 static __u32 extract(const struct hid_device *hid, __u8 *report,
977                      unsigned offset, unsigned n)
978 {
979         u64 x;
980
981         if (n > 32)
982                 hid_warn(hid, "extract() called with n (%d) > 32! (%s)\n",
983                          n, current->comm);
984
985         report += offset >> 3;  /* adjust byte index */
986         offset &= 7;            /* now only need bit offset into one byte */
987         x = get_unaligned_le64(report);
988         x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
989         return (u32) x;
990 }
991
992 /*
993  * "implement" : set bits in a little endian bit stream.
994  * Same concepts as "extract" (see comments above).
995  * The data mangled in the bit stream remains in little endian
996  * order the whole time. It make more sense to talk about
997  * endianness of register values by considering a register
998  * a "cached" copy of the little endiad bit stream.
999  */
1000 static void implement(const struct hid_device *hid, __u8 *report,
1001                       unsigned offset, unsigned n, __u32 value)
1002 {
1003         u64 x;
1004         u64 m = (1ULL << n) - 1;
1005
1006         if (n > 32)
1007                 hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
1008                          __func__, n, current->comm);
1009
1010         if (value > m)
1011                 hid_warn(hid, "%s() called with too large value %d! (%s)\n",
1012                          __func__, value, current->comm);
1013         WARN_ON(value > m);
1014         value &= m;
1015
1016         report += offset >> 3;
1017         offset &= 7;
1018
1019         x = get_unaligned_le64(report);
1020         x &= ~(m << offset);
1021         x |= ((u64)value) << offset;
1022         put_unaligned_le64(x, report);
1023 }
1024
1025 /*
1026  * Search an array for a value.
1027  */
1028
1029 static int search(__s32 *array, __s32 value, unsigned n)
1030 {
1031         while (n--) {
1032                 if (*array++ == value)
1033                         return 0;
1034         }
1035         return -1;
1036 }
1037
1038 /**
1039  * hid_match_report - check if driver's raw_event should be called
1040  *
1041  * @hid: hid device
1042  * @report_type: type to match against
1043  *
1044  * compare hid->driver->report_table->report_type to report->type
1045  */
1046 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1047 {
1048         const struct hid_report_id *id = hid->driver->report_table;
1049
1050         if (!id) /* NULL means all */
1051                 return 1;
1052
1053         for (; id->report_type != HID_TERMINATOR; id++)
1054                 if (id->report_type == HID_ANY_ID ||
1055                                 id->report_type == report->type)
1056                         return 1;
1057         return 0;
1058 }
1059
1060 /**
1061  * hid_match_usage - check if driver's event should be called
1062  *
1063  * @hid: hid device
1064  * @usage: usage to match against
1065  *
1066  * compare hid->driver->usage_table->usage_{type,code} to
1067  * usage->usage_{type,code}
1068  */
1069 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
1070 {
1071         const struct hid_usage_id *id = hid->driver->usage_table;
1072
1073         if (!id) /* NULL means all */
1074                 return 1;
1075
1076         for (; id->usage_type != HID_ANY_ID - 1; id++)
1077                 if ((id->usage_hid == HID_ANY_ID ||
1078                                 id->usage_hid == usage->hid) &&
1079                                 (id->usage_type == HID_ANY_ID ||
1080                                 id->usage_type == usage->type) &&
1081                                 (id->usage_code == HID_ANY_ID ||
1082                                  id->usage_code == usage->code))
1083                         return 1;
1084         return 0;
1085 }
1086
1087 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
1088                 struct hid_usage *usage, __s32 value, int interrupt)
1089 {
1090         struct hid_driver *hdrv = hid->driver;
1091         int ret;
1092
1093         if (!list_empty(&hid->debug_list))
1094                 hid_dump_input(hid, usage, value);
1095
1096         if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
1097                 ret = hdrv->event(hid, field, usage, value);
1098                 if (ret != 0) {
1099                         if (ret < 0)
1100                                 hid_err(hid, "%s's event failed with %d\n",
1101                                                 hdrv->name, ret);
1102                         return;
1103                 }
1104         }
1105
1106         if (hid->claimed & HID_CLAIMED_INPUT)
1107                 hidinput_hid_event(hid, field, usage, value);
1108         if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1109                 hid->hiddev_hid_event(hid, field, usage, value);
1110 }
1111
1112 /*
1113  * Analyse a received field, and fetch the data from it. The field
1114  * content is stored for next report processing (we do differential
1115  * reporting to the layer).
1116  */
1117
1118 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
1119                             __u8 *data, int interrupt)
1120 {
1121         unsigned n;
1122         unsigned count = field->report_count;
1123         unsigned offset = field->report_offset;
1124         unsigned size = field->report_size;
1125         __s32 min = field->logical_minimum;
1126         __s32 max = field->logical_maximum;
1127         __s32 *value;
1128
1129         value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC);
1130         if (!value)
1131                 return;
1132
1133         for (n = 0; n < count; n++) {
1134
1135                 value[n] = min < 0 ?
1136                         snto32(extract(hid, data, offset + n * size, size),
1137                                size) :
1138                         extract(hid, data, offset + n * size, size);
1139
1140                 /* Ignore report if ErrorRollOver */
1141                 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
1142                     value[n] >= min && value[n] <= max &&
1143                     field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1144                         goto exit;
1145         }
1146
1147         for (n = 0; n < count; n++) {
1148
1149                 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1150                         hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1151                         continue;
1152                 }
1153
1154                 if (field->value[n] >= min && field->value[n] <= max
1155                         && field->usage[field->value[n] - min].hid
1156                         && search(value, field->value[n], count))
1157                                 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1158
1159                 if (value[n] >= min && value[n] <= max
1160                         && field->usage[value[n] - min].hid
1161                         && search(field->value, value[n], count))
1162                                 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1163         }
1164
1165         memcpy(field->value, value, count * sizeof(__s32));
1166 exit:
1167         kfree(value);
1168 }
1169
1170 /*
1171  * Output the field into the report.
1172  */
1173
1174 static void hid_output_field(const struct hid_device *hid,
1175                              struct hid_field *field, __u8 *data)
1176 {
1177         unsigned count = field->report_count;
1178         unsigned offset = field->report_offset;
1179         unsigned size = field->report_size;
1180         unsigned n;
1181
1182         for (n = 0; n < count; n++) {
1183                 if (field->logical_minimum < 0) /* signed values */
1184                         implement(hid, data, offset + n * size, size,
1185                                   s32ton(field->value[n], size));
1186                 else                            /* unsigned values */
1187                         implement(hid, data, offset + n * size, size,
1188                                   field->value[n]);
1189         }
1190 }
1191
1192 /*
1193  * Create a report.
1194  */
1195
1196 void hid_output_report(struct hid_report *report, __u8 *data)
1197 {
1198         unsigned n;
1199
1200         if (report->id > 0)
1201                 *data++ = report->id;
1202
1203         memset(data, 0, ((report->size - 1) >> 3) + 1);
1204         for (n = 0; n < report->maxfield; n++)
1205                 hid_output_field(report->device, report->field[n], data);
1206 }
1207 EXPORT_SYMBOL_GPL(hid_output_report);
1208
1209 /*
1210  * Set a field value. The report this field belongs to has to be
1211  * created and transferred to the device, to set this value in the
1212  * device.
1213  */
1214
1215 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1216 {
1217         unsigned size;
1218
1219         if (!field)
1220                 return -1;
1221
1222         size = field->report_size;
1223
1224         hid_dump_input(field->report->device, field->usage + offset, value);
1225
1226         if (offset >= field->report_count) {
1227                 hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
1228                                 offset, field->report_count);
1229                 return -1;
1230         }
1231         if (field->logical_minimum < 0) {
1232                 if (value != snto32(s32ton(value, size), size)) {
1233                         hid_err(field->report->device, "value %d is out of range\n", value);
1234                         return -1;
1235                 }
1236         }
1237         field->value[offset] = value;
1238         return 0;
1239 }
1240 EXPORT_SYMBOL_GPL(hid_set_field);
1241
1242 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1243                 const u8 *data)
1244 {
1245         struct hid_report *report;
1246         unsigned int n = 0;     /* Normally report number is 0 */
1247
1248         /* Device uses numbered reports, data[0] is report number */
1249         if (report_enum->numbered)
1250                 n = *data;
1251
1252         report = report_enum->report_id_hash[n];
1253         if (report == NULL)
1254                 dbg_hid("undefined report_id %u received\n", n);
1255
1256         return report;
1257 }
1258
1259 int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1260                 int interrupt)
1261 {
1262         struct hid_report_enum *report_enum = hid->report_enum + type;
1263         struct hid_report *report;
1264         struct hid_driver *hdrv;
1265         unsigned int a;
1266         int rsize, csize = size;
1267         u8 *cdata = data;
1268         int ret = 0;
1269
1270         report = hid_get_report(report_enum, data);
1271         if (!report)
1272                 goto out;
1273
1274         if (report_enum->numbered) {
1275                 cdata++;
1276                 csize--;
1277         }
1278
1279         rsize = ((report->size - 1) >> 3) + 1;
1280
1281         if (rsize > HID_MAX_BUFFER_SIZE)
1282                 rsize = HID_MAX_BUFFER_SIZE;
1283
1284         if (csize < rsize) {
1285                 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1286                                 csize, rsize);
1287                 memset(cdata + csize, 0, rsize - csize);
1288         }
1289
1290         if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1291                 hid->hiddev_report_event(hid, report);
1292         if (hid->claimed & HID_CLAIMED_HIDRAW) {
1293                 ret = hidraw_report_event(hid, data, size);
1294                 if (ret)
1295                         goto out;
1296         }
1297
1298         if (hid->claimed != HID_CLAIMED_HIDRAW) {
1299                 for (a = 0; a < report->maxfield; a++)
1300                         hid_input_field(hid, report->field[a], cdata, interrupt);
1301                 hdrv = hid->driver;
1302                 if (hdrv && hdrv->report)
1303                         hdrv->report(hid, report);
1304         }
1305
1306         if (hid->claimed & HID_CLAIMED_INPUT)
1307                 hidinput_report_event(hid, report);
1308 out:
1309         return ret;
1310 }
1311 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1312
1313 /**
1314  * hid_input_report - report data from lower layer (usb, bt...)
1315  *
1316  * @hid: hid device
1317  * @type: HID report type (HID_*_REPORT)
1318  * @data: report contents
1319  * @size: size of data parameter
1320  * @interrupt: distinguish between interrupt and control transfers
1321  *
1322  * This is data entry for lower layers.
1323  */
1324 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1325 {
1326         struct hid_report_enum *report_enum;
1327         struct hid_driver *hdrv;
1328         struct hid_report *report;
1329         int ret = 0;
1330
1331         if (!hid)
1332                 return -ENODEV;
1333
1334         if (down_trylock(&hid->driver_input_lock))
1335                 return -EBUSY;
1336
1337         if (!hid->driver) {
1338                 ret = -ENODEV;
1339                 goto unlock;
1340         }
1341         report_enum = hid->report_enum + type;
1342         hdrv = hid->driver;
1343
1344         if (!size) {
1345                 dbg_hid("empty report\n");
1346                 ret = -1;
1347                 goto unlock;
1348         }
1349
1350         /* Avoid unnecessary overhead if debugfs is disabled */
1351         if (!list_empty(&hid->debug_list))
1352                 hid_dump_report(hid, type, data, size);
1353
1354         report = hid_get_report(report_enum, data);
1355
1356         if (!report) {
1357                 ret = -1;
1358                 goto unlock;
1359         }
1360
1361         if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1362                 ret = hdrv->raw_event(hid, report, data, size);
1363                 if (ret != 0) {
1364                         ret = ret < 0 ? ret : 0;
1365                         goto unlock;
1366                 }
1367         }
1368
1369         ret = hid_report_raw_event(hid, type, data, size, interrupt);
1370
1371 unlock:
1372         up(&hid->driver_input_lock);
1373         return ret;
1374 }
1375 EXPORT_SYMBOL_GPL(hid_input_report);
1376
1377 static bool hid_match_one_id(struct hid_device *hdev,
1378                 const struct hid_device_id *id)
1379 {
1380         return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
1381                 (id->group == HID_GROUP_ANY || id->group == hdev->group) &&
1382                 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1383                 (id->product == HID_ANY_ID || id->product == hdev->product);
1384 }
1385
1386 const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1387                 const struct hid_device_id *id)
1388 {
1389         for (; id->bus; id++)
1390                 if (hid_match_one_id(hdev, id))
1391                         return id;
1392
1393         return NULL;
1394 }
1395
1396 static const struct hid_device_id hid_hiddev_list[] = {
1397         { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1398         { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1399         { }
1400 };
1401
1402 static bool hid_hiddev(struct hid_device *hdev)
1403 {
1404         return !!hid_match_id(hdev, hid_hiddev_list);
1405 }
1406
1407
1408 static ssize_t
1409 read_report_descriptor(struct file *filp, struct kobject *kobj,
1410                 struct bin_attribute *attr,
1411                 char *buf, loff_t off, size_t count)
1412 {
1413         struct device *dev = container_of(kobj, struct device, kobj);
1414         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1415
1416         if (off >= hdev->rsize)
1417                 return 0;
1418
1419         if (off + count > hdev->rsize)
1420                 count = hdev->rsize - off;
1421
1422         memcpy(buf, hdev->rdesc + off, count);
1423
1424         return count;
1425 }
1426
1427 static struct bin_attribute dev_bin_attr_report_desc = {
1428         .attr = { .name = "report_descriptor", .mode = 0444 },
1429         .read = read_report_descriptor,
1430         .size = HID_MAX_DESCRIPTOR_SIZE,
1431 };
1432
1433 int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1434 {
1435         static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1436                 "Joystick", "Gamepad", "Keyboard", "Keypad",
1437                 "Multi-Axis Controller"
1438         };
1439         const char *type, *bus;
1440         char buf[64];
1441         unsigned int i;
1442         int len;
1443         int ret;
1444
1445         if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1446                 connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
1447         if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
1448                 connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
1449         if (hdev->bus != BUS_USB)
1450                 connect_mask &= ~HID_CONNECT_HIDDEV;
1451         if (hid_hiddev(hdev))
1452                 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1453
1454         if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1455                                 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1456                 hdev->claimed |= HID_CLAIMED_INPUT;
1457
1458         if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1459                         !hdev->hiddev_connect(hdev,
1460                                 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1461                 hdev->claimed |= HID_CLAIMED_HIDDEV;
1462         if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1463                 hdev->claimed |= HID_CLAIMED_HIDRAW;
1464
1465         /* Drivers with the ->raw_event callback set are not required to connect
1466          * to any other listener. */
1467         if (!hdev->claimed && !hdev->driver->raw_event) {
1468                 hid_err(hdev, "device has no listeners, quitting\n");
1469                 return -ENODEV;
1470         }
1471
1472         if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1473                         (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1474                 hdev->ff_init(hdev);
1475
1476         len = 0;
1477         if (hdev->claimed & HID_CLAIMED_INPUT)
1478                 len += sprintf(buf + len, "input");
1479         if (hdev->claimed & HID_CLAIMED_HIDDEV)
1480                 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1481                                 hdev->minor);
1482         if (hdev->claimed & HID_CLAIMED_HIDRAW)
1483                 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1484                                 ((struct hidraw *)hdev->hidraw)->minor);
1485
1486         type = "Device";
1487         for (i = 0; i < hdev->maxcollection; i++) {
1488                 struct hid_collection *col = &hdev->collection[i];
1489                 if (col->type == HID_COLLECTION_APPLICATION &&
1490                    (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1491                    (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1492                         type = types[col->usage & 0xffff];
1493                         break;
1494                 }
1495         }
1496
1497         switch (hdev->bus) {
1498         case BUS_USB:
1499                 bus = "USB";
1500                 break;
1501         case BUS_BLUETOOTH:
1502                 bus = "BLUETOOTH";
1503                 break;
1504         default:
1505                 bus = "<UNKNOWN>";
1506         }
1507
1508         ret = device_create_bin_file(&hdev->dev, &dev_bin_attr_report_desc);
1509         if (ret)
1510                 hid_warn(hdev,
1511                          "can't create sysfs report descriptor attribute err: %d\n", ret);
1512
1513         hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1514                  buf, bus, hdev->version >> 8, hdev->version & 0xff,
1515                  type, hdev->name, hdev->phys);
1516
1517         return 0;
1518 }
1519 EXPORT_SYMBOL_GPL(hid_connect);
1520
1521 void hid_disconnect(struct hid_device *hdev)
1522 {
1523         device_remove_bin_file(&hdev->dev, &dev_bin_attr_report_desc);
1524         if (hdev->claimed & HID_CLAIMED_INPUT)
1525                 hidinput_disconnect(hdev);
1526         if (hdev->claimed & HID_CLAIMED_HIDDEV)
1527                 hdev->hiddev_disconnect(hdev);
1528         if (hdev->claimed & HID_CLAIMED_HIDRAW)
1529                 hidraw_disconnect(hdev);
1530 }
1531 EXPORT_SYMBOL_GPL(hid_disconnect);
1532
1533 /*
1534  * A list of devices for which there is a specialized driver on HID bus.
1535  *
1536  * Please note that for multitouch devices (driven by hid-multitouch driver),
1537  * there is a proper autodetection and autoloading in place (based on presence
1538  * of HID_DG_CONTACTID), so those devices don't need to be added to this list,
1539  * as we are doing the right thing in hid_scan_usage().
1540  *
1541  * Autodetection for (USB) HID sensor hubs exists too. If a collection of type
1542  * physical is found inside a usage page of type sensor, hid-sensor-hub will be
1543  * used as a driver. See hid_scan_report().
1544  */
1545 static const struct hid_device_id hid_have_special_driver[] = {
1546         { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
1547         { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1548         { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649) },
1549         { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802) },
1550         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1551         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE) },
1552         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICTRACKPAD) },
1553         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1554         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1555         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1556         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1557         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1558         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1559         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1560         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1561         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1562         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1563         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1564         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI) },
1565         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO) },
1566         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS) },
1567         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1568         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1569         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1570         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1571         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1572         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1573         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
1574         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL2) },
1575         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL3) },
1576         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1577         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) },
1578         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1579         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1580         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
1581         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1582         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1583         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1584         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1585         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1586         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1587         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
1588         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
1589         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
1590         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
1591         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
1592         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
1593         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
1594         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
1595         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
1596         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI) },
1597         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO) },
1598         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS) },
1599         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI) },
1600         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO) },
1601         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS) },
1602         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI) },
1603         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO) },
1604         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_JIS) },
1605         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI) },
1606         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO) },
1607         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS) },
1608         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI) },
1609         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO) },
1610         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS) },
1611         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI) },
1612         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO) },
1613         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS) },
1614         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI) },
1615         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO) },
1616         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS) },
1617         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI) },
1618         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO) },
1619         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS) },
1620         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI) },
1621         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO) },
1622         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS) },
1623         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI) },
1624         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO) },
1625         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1626         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1627         { HID_USB_DEVICE(USB_VENDOR_ID_AUREAL, USB_DEVICE_ID_AUREAL_W01RN) },
1628         { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
1629         { HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE) },
1630         { HID_USB_DEVICE(USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_EMPREX_REMOTE_2) },
1631         { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1632         { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION_SOLAR) },
1633         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1634         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS) },
1635         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS2) },
1636         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_AK1D) },
1637         { HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_PRODIKEYS_PCMIDI) },
1638         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
1639         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1640         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) },
1641         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_4) },
1642         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
1643         { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) },
1644         { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011) },
1645         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
1646         { HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
1647         { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
1648         { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1649         { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR) },
1650         { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
1651         { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1652         { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
1653         { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1654         { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
1655         { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
1656         { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) },
1657         { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD) },
1658         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ION, USB_DEVICE_ID_ICADE) },
1659         { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
1660         { HID_USB_DEVICE(USB_VENDOR_ID_KEYTOUCH, USB_DEVICE_ID_KEYTOUCH_IEC) },
1661         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
1662         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_EASYPEN_I405X) },
1663         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_MOUSEPEN_I608X) },
1664         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_EASYPEN_M610X) },
1665         { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
1666         { HID_USB_DEVICE(USB_VENDOR_ID_LCPOWER, USB_DEVICE_ID_LCPOWER_LC1000 ) },
1667 #if IS_ENABLED(CONFIG_HID_LENOVO_TPKBD)
1668         { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
1669 #endif
1670         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1671         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1672         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1673         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1674         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3) },
1675         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1676         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1677         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
1678         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1679         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
1680         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1681         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1682         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD_CORD) },
1683         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1684         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1685         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1686         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ) },
1687         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1688         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FLIGHT_SYSTEM_G940) },
1689         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1690         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1691         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL) },
1692         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFGT_WHEEL) },
1693         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
1694         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL) },
1695 #if IS_ENABLED(CONFIG_HID_LOGITECH_DJ)
1696         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
1697         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
1698 #endif
1699         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
1700         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
1701         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
1702         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) },
1703         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
1704         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
1705         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500) },
1706         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
1707         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
1708         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP) },
1709         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
1710         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1711         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K) },
1712         { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
1713         { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
1714         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN) },
1715         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1) },
1716         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2) },
1717         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3) },
1718         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4) },
1719         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5) },
1720         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6) },
1721         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7) },
1722         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8) },
1723         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9) },
1724         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10) },
1725         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11) },
1726         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12) },
1727         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13) },
1728         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14) },
1729         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15) },
1730         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16) },
1731         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17) },
1732         { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18) },
1733         { HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_PKB1700) },
1734         { HID_USB_DEVICE(USB_VENDOR_ID_ORTEK, USB_DEVICE_ID_ORTEK_WKB2000) },
1735         { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1736         { HID_USB_DEVICE(USB_VENDOR_ID_PRIMAX, USB_DEVICE_ID_PRIMAX_KEYBOARD) },
1737 #if IS_ENABLED(CONFIG_HID_ROCCAT)
1738         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
1739         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
1740         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
1741         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
1742         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
1743         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) },
1744         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_LUA) },
1745         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
1746         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
1747         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
1748 #endif
1749         { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_PS1000) },
1750         { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1751         { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_WIRELESS_KBD_MOUSE) },
1752         { HID_USB_DEVICE(USB_VENDOR_ID_SKYCABLE, USB_DEVICE_ID_SKYCABLE_WIRELESS_PRESENTER) },
1753         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE) },
1754         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1755         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER) },
1756         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1757         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE) },
1758         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE) },
1759         { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) },
1760         { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1761         { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
1762         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1763         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
1764         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323) },
1765         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324) },
1766         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1767         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653) },
1768         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1769         { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65a) },
1770         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_TIVO, USB_DEVICE_ID_TIVO_SLIDE_BT) },
1771         { HID_USB_DEVICE(USB_VENDOR_ID_TIVO, USB_DEVICE_ID_TIVO_SLIDE) },
1772         { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED, USB_DEVICE_ID_TOPSEED_CYBERLINK) },
1773         { HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED2, USB_DEVICE_ID_TOPSEED2_RF_COMBO) },
1774         { HID_USB_DEVICE(USB_VENDOR_ID_TWINHAN, USB_DEVICE_ID_TWINHAN_IR_REMOTE) },
1775         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
1776         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
1777         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
1778         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
1779         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
1780         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
1781         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
1782         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
1783         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SUPER_JOY_BOX_3) },
1784         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD) },
1785         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_3_PRO) },
1786         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_DUAL_BOX_PRO) },
1787         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_5_PRO) },
1788         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
1789         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
1790         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_5_8_INCH) },
1791         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SLIM_TABLET_12_1_INCH) },
1792         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_Q_PAD) },
1793         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_PID_0038) },
1794         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_10_6_INCH) },
1795         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_MEDIA_TABLET_14_1_INCH) },
1796         { HID_USB_DEVICE(USB_VENDOR_ID_WALTOP, USB_DEVICE_ID_WALTOP_SIRIUS_BATTERY_FREE_TABLET) },
1797         { HID_USB_DEVICE(USB_VENDOR_ID_X_TENSIONS, USB_DEVICE_ID_SPEEDLINK_VAD_CEZANNE) },
1798         { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1799         { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1800         { HID_USB_DEVICE(USB_VENDOR_ID_ZYDACRON, USB_DEVICE_ID_ZYDACRON_REMOTE_CONTROL) },
1801
1802         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
1803         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1804         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
1805         { }
1806 };
1807
1808 struct hid_dynid {
1809         struct list_head list;
1810         struct hid_device_id id;
1811 };
1812
1813 /**
1814  * store_new_id - add a new HID device ID to this driver and re-probe devices
1815  * @driver: target device driver
1816  * @buf: buffer for scanning device ID data
1817  * @count: input size
1818  *
1819  * Adds a new dynamic hid device ID to this driver,
1820  * and causes the driver to probe for all devices again.
1821  */
1822 static ssize_t store_new_id(struct device_driver *drv, const char *buf,
1823                 size_t count)
1824 {
1825         struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1826         struct hid_dynid *dynid;
1827         __u32 bus, vendor, product;
1828         unsigned long driver_data = 0;
1829         int ret;
1830
1831         ret = sscanf(buf, "%x %x %x %lx",
1832                         &bus, &vendor, &product, &driver_data);
1833         if (ret < 3)
1834                 return -EINVAL;
1835
1836         dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1837         if (!dynid)
1838                 return -ENOMEM;
1839
1840         dynid->id.bus = bus;
1841         dynid->id.group = HID_GROUP_ANY;
1842         dynid->id.vendor = vendor;
1843         dynid->id.product = product;
1844         dynid->id.driver_data = driver_data;
1845
1846         spin_lock(&hdrv->dyn_lock);
1847         list_add_tail(&dynid->list, &hdrv->dyn_list);
1848         spin_unlock(&hdrv->dyn_lock);
1849
1850         ret = driver_attach(&hdrv->driver);
1851
1852         return ret ? : count;
1853 }
1854 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1855
1856 static void hid_free_dynids(struct hid_driver *hdrv)
1857 {
1858         struct hid_dynid *dynid, *n;
1859
1860         spin_lock(&hdrv->dyn_lock);
1861         list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
1862                 list_del(&dynid->list);
1863                 kfree(dynid);
1864         }
1865         spin_unlock(&hdrv->dyn_lock);
1866 }
1867
1868 static const struct hid_device_id *hid_match_device(struct hid_device *hdev,
1869                 struct hid_driver *hdrv)
1870 {
1871         struct hid_dynid *dynid;
1872
1873         spin_lock(&hdrv->dyn_lock);
1874         list_for_each_entry(dynid, &hdrv->dyn_list, list) {
1875                 if (hid_match_one_id(hdev, &dynid->id)) {
1876                         spin_unlock(&hdrv->dyn_lock);
1877                         return &dynid->id;
1878                 }
1879         }
1880         spin_unlock(&hdrv->dyn_lock);
1881
1882         return hid_match_id(hdev, hdrv->id_table);
1883 }
1884
1885 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1886 {
1887         struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1888         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1889
1890         return hid_match_device(hdev, hdrv) != NULL;
1891 }
1892
1893 static int hid_device_probe(struct device *dev)
1894 {
1895         struct hid_driver *hdrv = container_of(dev->driver,
1896                         struct hid_driver, driver);
1897         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1898         const struct hid_device_id *id;
1899         int ret = 0;
1900
1901         if (down_interruptible(&hdev->driver_lock))
1902                 return -EINTR;
1903         if (down_interruptible(&hdev->driver_input_lock)) {
1904                 ret = -EINTR;
1905                 goto unlock_driver_lock;
1906         }
1907         hdev->io_started = false;
1908
1909         if (!hdev->driver) {
1910                 id = hid_match_device(hdev, hdrv);
1911                 if (id == NULL) {
1912                         ret = -ENODEV;
1913                         goto unlock;
1914                 }
1915
1916                 hdev->driver = hdrv;
1917                 if (hdrv->probe) {
1918                         ret = hdrv->probe(hdev, id);
1919                 } else { /* default probe */
1920                         ret = hid_open_report(hdev);
1921                         if (!ret)
1922                                 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1923                 }
1924                 if (ret) {
1925                         hid_close_report(hdev);
1926                         hdev->driver = NULL;
1927                 }
1928         }
1929 unlock:
1930         if (!hdev->io_started)
1931                 up(&hdev->driver_input_lock);
1932 unlock_driver_lock:
1933         up(&hdev->driver_lock);
1934         return ret;
1935 }
1936
1937 static int hid_device_remove(struct device *dev)
1938 {
1939         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1940         struct hid_driver *hdrv;
1941         int ret = 0;
1942
1943         if (down_interruptible(&hdev->driver_lock))
1944                 return -EINTR;
1945         if (down_interruptible(&hdev->driver_input_lock)) {
1946                 ret = -EINTR;
1947                 goto unlock_driver_lock;
1948         }
1949         hdev->io_started = false;
1950
1951         hdrv = hdev->driver;
1952         if (hdrv) {
1953                 if (hdrv->remove)
1954                         hdrv->remove(hdev);
1955                 else /* default remove */
1956                         hid_hw_stop(hdev);
1957                 hid_close_report(hdev);
1958                 hdev->driver = NULL;
1959         }
1960
1961         if (!hdev->io_started)
1962                 up(&hdev->driver_input_lock);
1963 unlock_driver_lock:
1964         up(&hdev->driver_lock);
1965         return ret;
1966 }
1967
1968 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
1969                              char *buf)
1970 {
1971         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1972         int len;
1973
1974         len = snprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
1975                        hdev->bus, hdev->group, hdev->vendor, hdev->product);
1976
1977         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
1978 }
1979
1980 static struct device_attribute hid_dev_attrs[] = {
1981         __ATTR_RO(modalias),
1982         __ATTR_NULL,
1983 };
1984
1985 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1986 {
1987         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1988
1989         if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1990                         hdev->bus, hdev->vendor, hdev->product))
1991                 return -ENOMEM;
1992
1993         if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1994                 return -ENOMEM;
1995
1996         if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1997                 return -ENOMEM;
1998
1999         if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
2000                 return -ENOMEM;
2001
2002         if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
2003                            hdev->bus, hdev->group, hdev->vendor, hdev->product))
2004                 return -ENOMEM;
2005
2006         return 0;
2007 }
2008
2009 static struct bus_type hid_bus_type = {
2010         .name           = "hid",
2011         .dev_attrs      = hid_dev_attrs,
2012         .match          = hid_bus_match,
2013         .probe          = hid_device_probe,
2014         .remove         = hid_device_remove,
2015         .uevent         = hid_uevent,
2016 };
2017
2018 /* a list of devices that shouldn't be handled by HID core at all */
2019 static const struct hid_device_id hid_ignore_list[] = {
2020         { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
2021         { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
2022         { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
2023         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
2024         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
2025         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
2026         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
2027         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
2028         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
2029         { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
2030         { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
2031         { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
2032         { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM)},
2033         { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_LCM2)},
2034         { HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
2035         { HID_USB_DEVICE(USB_VENDOR_ID_AXENTIA, USB_DEVICE_ID_AXENTIA_FM_RADIO) },
2036         { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
2037         { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
2038         { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
2039         { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
2040         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
2041         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
2042         { HID_USB_DEVICE(USB_VENDOR_ID_DEALEXTREAME, USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701) },
2043         { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
2044         { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
2045         { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, 0x0004) },
2046         { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, 0x000a) },
2047         { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
2048         { HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC5UH) },
2049         { HID_USB_DEVICE(USB_VENDOR_ID_ETT, USB_DEVICE_ID_TC4UM) },
2050         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
2051         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
2052         { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
2053         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
2054         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
2055         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
2056         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
2057         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
2058         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
2059         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
2060         { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
2061         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
2062         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
2063         { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
2064         { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
2065         { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
2066         { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
2067         { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_RADIOSHARK) },
2068         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
2069         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
2070         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
2071         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
2072         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
2073         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
2074         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
2075         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
2076         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
2077         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
2078         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
2079         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
2080         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
2081         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
2082         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
2083         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
2084         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
2085         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
2086         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
2087         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
2088         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
2089         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
2090         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
2091         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
2092         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
2093         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
2094         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
2095         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
2096         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
2097         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
2098         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
2099         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
2100         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
2101         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
2102         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
2103         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
2104         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
2105         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
2106         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
2107         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
2108         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
2109         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
2110         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
2111         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
2112         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
2113         { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
2114         { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
2115         { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
2116         { HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) },
2117         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
2118         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_KYE, 0x0058) },
2119         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
2120         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) },
2121         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
2122         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) },
2123         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
2124         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) },
2125         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) },
2126         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) },
2127         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) },
2128         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) },
2129         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) },
2130         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
2131         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
2132         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
2133         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) },
2134         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) },
2135         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) },
2136         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
2137         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
2138         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) },
2139         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
2140         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
2141         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
2142         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
2143         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
2144         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) },
2145         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) },
2146         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) },
2147         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) },
2148         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) },
2149         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) },
2150         { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) },
2151         { HID_USB_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_BEATPAD) },
2152         { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
2153         { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
2154         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
2155         { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
2156         { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
2157         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
2158         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
2159         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
2160         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
2161         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
2162         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
2163         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
2164         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
2165         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
2166         { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
2167         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
2168         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
2169         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
2170         { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
2171         { HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
2172         { HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
2173 #if defined(CONFIG_MOUSE_SYNAPTICS_USB) || defined(CONFIG_MOUSE_SYNAPTICS_USB_MODULE)
2174         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_TP) },
2175         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_INT_TP) },
2176         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_CPAD) },
2177         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_STICK) },
2178         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_WP) },
2179         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_COMP_TP) },
2180         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_WTP) },
2181         { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_DPAD) },
2182 #endif
2183         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
2184         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
2185         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
2186         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
2187         { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
2188         { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
2189         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
2190         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
2191         { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
2192         { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
2193         { }
2194 };
2195
2196 /**
2197  * hid_mouse_ignore_list - mouse devices which should not be handled by the hid layer
2198  *
2199  * There are composite devices for which we want to ignore only a certain
2200  * interface. This is a list of devices for which only the mouse interface will
2201  * be ignored. This allows a dedicated driver to take care of the interface.
2202  */
2203 static const struct hid_device_id hid_mouse_ignore_list[] = {
2204         /* appletouch driver */
2205         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
2206         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
2207         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
2208         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
2209         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
2210         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
2211         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
2212         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
2213         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
2214         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
2215         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
2216         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
2217         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
2218         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
2219         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
2220         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
2221         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
2222         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
2223         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
2224         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
2225         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI) },
2226         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO) },
2227         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS) },
2228         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI) },
2229         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO) },
2230         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS) },
2231         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI) },
2232         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO) },
2233         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) },
2234         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI) },
2235         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO) },
2236         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS) },
2237         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI) },
2238         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO) },
2239         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS) },
2240         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI) },
2241         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO) },
2242         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS) },
2243         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI) },
2244         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO) },
2245         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS) },
2246         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI) },
2247         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO) },
2248         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS) },
2249         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI) },
2250         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO) },
2251         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS) },
2252         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI) },
2253         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO) },
2254         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS) },
2255         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
2256         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
2257         { }
2258 };
2259
2260 bool hid_ignore(struct hid_device *hdev)
2261 {
2262         if (hdev->quirks & HID_QUIRK_NO_IGNORE)
2263                 return false;
2264         if (hdev->quirks & HID_QUIRK_IGNORE)
2265                 return true;
2266
2267         switch (hdev->vendor) {
2268         case USB_VENDOR_ID_CODEMERCS:
2269                 /* ignore all Code Mercenaries IOWarrior devices */
2270                 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
2271                                 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
2272                         return true;
2273                 break;
2274         case USB_VENDOR_ID_LOGITECH:
2275                 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
2276                                 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
2277                         return true;
2278                 /*
2279                  * The Keene FM transmitter USB device has the same USB ID as
2280                  * the Logitech AudioHub Speaker, but it should ignore the hid.
2281                  * Check if the name is that of the Keene device.
2282                  * For reference: the name of the AudioHub is
2283                  * "HOLTEK  AudioHub Speaker".
2284                  */
2285                 if (hdev->product == USB_DEVICE_ID_LOGITECH_AUDIOHUB &&
2286                         !strcmp(hdev->name, "HOLTEK  B-LINK USB Audio  "))
2287                                 return true;
2288                 break;
2289         case USB_VENDOR_ID_SOUNDGRAPH:
2290                 if (hdev->product >= USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST &&
2291                     hdev->product <= USB_DEVICE_ID_SOUNDGRAPH_IMON_LAST)
2292                         return true;
2293                 break;
2294         case USB_VENDOR_ID_HANWANG:
2295                 if (hdev->product >= USB_DEVICE_ID_HANWANG_TABLET_FIRST &&
2296                     hdev->product <= USB_DEVICE_ID_HANWANG_TABLET_LAST)
2297                         return true;
2298                 break;
2299         case USB_VENDOR_ID_JESS:
2300                 if (hdev->product == USB_DEVICE_ID_JESS_YUREX &&
2301                                 hdev->type == HID_TYPE_USBNONE)
2302                         return true;
2303                 break;
2304         case USB_VENDOR_ID_DWAV:
2305                 /* These are handled by usbtouchscreen. hdev->type is probably
2306                  * HID_TYPE_USBNONE, but we say !HID_TYPE_USBMOUSE to match
2307                  * usbtouchscreen. */
2308                 if ((hdev->product == USB_DEVICE_ID_EGALAX_TOUCHCONTROLLER ||
2309                      hdev->product == USB_DEVICE_ID_DWAV_TOUCHCONTROLLER) &&
2310                     hdev->type != HID_TYPE_USBMOUSE)
2311                         return true;
2312                 break;
2313         case USB_VENDOR_ID_VELLEMAN:
2314                 /* These are not HID devices.  They are handled by comedi. */
2315                 if ((hdev->product >= USB_DEVICE_ID_VELLEMAN_K8055_FIRST &&
2316                      hdev->product <= USB_DEVICE_ID_VELLEMAN_K8055_LAST) ||
2317                     (hdev->product >= USB_DEVICE_ID_VELLEMAN_K8061_FIRST &&
2318                      hdev->product <= USB_DEVICE_ID_VELLEMAN_K8061_LAST))
2319                         return true;
2320                 break;
2321         case USB_VENDOR_ID_ATMEL_V_USB:
2322                 /* Masterkit MA901 usb radio based on Atmel tiny85 chip and
2323                  * it has the same USB ID as many Atmel V-USB devices. This
2324                  * usb radio is handled by radio-ma901.c driver so we want
2325                  * ignore the hid. Check the name, bus, product and ignore
2326                  * if we have MA901 usb radio.
2327                  */
2328                 if (hdev->product == USB_DEVICE_ID_ATMEL_V_USB &&
2329                         hdev->bus == BUS_USB &&
2330                         strncmp(hdev->name, "www.masterkit.ru MA901", 22) == 0)
2331                         return true;
2332                 break;
2333         }
2334
2335         if (hdev->type == HID_TYPE_USBMOUSE &&
2336                         hid_match_id(hdev, hid_mouse_ignore_list))
2337                 return true;
2338
2339         return !!hid_match_id(hdev, hid_ignore_list);
2340 }
2341 EXPORT_SYMBOL_GPL(hid_ignore);
2342
2343 int hid_add_device(struct hid_device *hdev)
2344 {
2345         static atomic_t id = ATOMIC_INIT(0);
2346         int ret;
2347
2348         if (WARN_ON(hdev->status & HID_STAT_ADDED))
2349                 return -EBUSY;
2350
2351         /* we need to kill them here, otherwise they will stay allocated to
2352          * wait for coming driver */
2353         if (hid_ignore(hdev))
2354                 return -ENODEV;
2355
2356         /*
2357          * Read the device report descriptor once and use as template
2358          * for the driver-specific modifications.
2359          */
2360         ret = hdev->ll_driver->parse(hdev);
2361         if (ret)
2362                 return ret;
2363         if (!hdev->dev_rdesc)
2364                 return -ENODEV;
2365
2366         /*
2367          * Scan generic devices for group information
2368          */
2369         if (hid_ignore_special_drivers ||
2370             !hid_match_id(hdev, hid_have_special_driver)) {
2371                 ret = hid_scan_report(hdev);
2372                 if (ret)
2373                         hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2374         }
2375
2376         /* XXX hack, any other cleaner solution after the driver core
2377          * is converted to allow more than 20 bytes as the device name? */
2378         dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
2379                      hdev->vendor, hdev->product, atomic_inc_return(&id));
2380
2381         hid_debug_register(hdev, dev_name(&hdev->dev));
2382         ret = device_add(&hdev->dev);
2383         if (!ret)
2384                 hdev->status |= HID_STAT_ADDED;
2385         else
2386                 hid_debug_unregister(hdev);
2387
2388         return ret;
2389 }
2390 EXPORT_SYMBOL_GPL(hid_add_device);
2391
2392 /**
2393  * hid_allocate_device - allocate new hid device descriptor
2394  *
2395  * Allocate and initialize hid device, so that hid_destroy_device might be
2396  * used to free it.
2397  *
2398  * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
2399  * error value.
2400  */
2401 struct hid_device *hid_allocate_device(void)
2402 {
2403         struct hid_device *hdev;
2404         int ret = -ENOMEM;
2405
2406         hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
2407         if (hdev == NULL)
2408                 return ERR_PTR(ret);
2409
2410         device_initialize(&hdev->dev);
2411         hdev->dev.release = hid_device_release;
2412         hdev->dev.bus = &hid_bus_type;
2413
2414         hid_close_report(hdev);
2415
2416         init_waitqueue_head(&hdev->debug_wait);
2417         INIT_LIST_HEAD(&hdev->debug_list);
2418         spin_lock_init(&hdev->debug_list_lock);
2419         sema_init(&hdev->driver_lock, 1);
2420         sema_init(&hdev->driver_input_lock, 1);
2421
2422         return hdev;
2423 }
2424 EXPORT_SYMBOL_GPL(hid_allocate_device);
2425
2426 static void hid_remove_device(struct hid_device *hdev)
2427 {
2428         if (hdev->status & HID_STAT_ADDED) {
2429                 device_del(&hdev->dev);
2430                 hid_debug_unregister(hdev);
2431                 hdev->status &= ~HID_STAT_ADDED;
2432         }
2433         kfree(hdev->dev_rdesc);
2434         hdev->dev_rdesc = NULL;
2435         hdev->dev_rsize = 0;
2436 }
2437
2438 /**
2439  * hid_destroy_device - free previously allocated device
2440  *
2441  * @hdev: hid device
2442  *
2443  * If you allocate hid_device through hid_allocate_device, you should ever
2444  * free by this function.
2445  */
2446 void hid_destroy_device(struct hid_device *hdev)
2447 {
2448         hid_remove_device(hdev);
2449         put_device(&hdev->dev);
2450 }
2451 EXPORT_SYMBOL_GPL(hid_destroy_device);
2452
2453 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
2454                 const char *mod_name)
2455 {
2456         int ret;
2457
2458         hdrv->driver.name = hdrv->name;
2459         hdrv->driver.bus = &hid_bus_type;
2460         hdrv->driver.owner = owner;
2461         hdrv->driver.mod_name = mod_name;
2462
2463         INIT_LIST_HEAD(&hdrv->dyn_list);
2464         spin_lock_init(&hdrv->dyn_lock);
2465
2466         ret = driver_register(&hdrv->driver);
2467         if (ret)
2468                 return ret;
2469
2470         ret = driver_create_file(&hdrv->driver, &driver_attr_new_id);
2471         if (ret)
2472                 driver_unregister(&hdrv->driver);
2473
2474         return ret;
2475 }
2476 EXPORT_SYMBOL_GPL(__hid_register_driver);
2477
2478 void hid_unregister_driver(struct hid_driver *hdrv)
2479 {
2480         driver_remove_file(&hdrv->driver, &driver_attr_new_id);
2481         driver_unregister(&hdrv->driver);
2482         hid_free_dynids(hdrv);
2483 }
2484 EXPORT_SYMBOL_GPL(hid_unregister_driver);
2485
2486 int hid_check_keys_pressed(struct hid_device *hid)
2487 {
2488         struct hid_input *hidinput;
2489         int i;
2490
2491         if (!(hid->claimed & HID_CLAIMED_INPUT))
2492                 return 0;
2493
2494         list_for_each_entry(hidinput, &hid->inputs, list) {
2495                 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
2496                         if (hidinput->input->key[i])
2497                                 return 1;
2498         }
2499
2500         return 0;
2501 }
2502
2503 EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
2504
2505 static int __init hid_init(void)
2506 {
2507         int ret;
2508
2509         if (hid_debug)
2510                 pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
2511                         "debugfs is now used for inspecting the device (report descriptor, reports)\n");
2512
2513         ret = bus_register(&hid_bus_type);
2514         if (ret) {
2515                 pr_err("can't register hid bus\n");
2516                 goto err;
2517         }
2518
2519         ret = hidraw_init();
2520         if (ret)
2521                 goto err_bus;
2522
2523         hid_debug_init();
2524
2525         return 0;
2526 err_bus:
2527         bus_unregister(&hid_bus_type);
2528 err:
2529         return ret;
2530 }
2531
2532 static void __exit hid_exit(void)
2533 {
2534         hid_debug_exit();
2535         hidraw_exit();
2536         bus_unregister(&hid_bus_type);
2537 }
2538
2539 module_init(hid_init);
2540 module_exit(hid_exit);
2541
2542 MODULE_AUTHOR("Andreas Gal");
2543 MODULE_AUTHOR("Vojtech Pavlik");
2544 MODULE_AUTHOR("Jiri Kosina");
2545 MODULE_LICENSE(DRIVER_LICENSE);
2546