RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / hid / usbhid / hiddev.c
blobe793127f971eae29903298b7cb6d3ec64a156325
1 /*
2 * Copyright (c) 2001 Paul Stewart
3 * Copyright (c) 2001 Vojtech Pavlik
5 * HID char devices, giving access to raw HID device events.
7 */
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to Paul Stewart <stewart@wetlogic.net>
28 #include <linux/poll.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/smp_lock.h>
33 #include <linux/input.h>
34 #include <linux/usb.h>
35 #include <linux/hid.h>
36 #include <linux/hiddev.h>
37 #include "usbhid.h"
39 #ifdef CONFIG_USB_DYNAMIC_MINORS
40 #define HIDDEV_MINOR_BASE 0
41 #define HIDDEV_MINORS 256
42 #else
43 #define HIDDEV_MINOR_BASE 96
44 #define HIDDEV_MINORS 16
45 #endif
46 #define HIDDEV_BUFFER_SIZE 64
48 struct hiddev {
49 int exist;
50 int open;
51 wait_queue_head_t wait;
52 struct hid_device *hid;
53 struct list_head list;
54 spinlock_t list_lock;
57 struct hiddev_list {
58 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
59 int head;
60 int tail;
61 unsigned flags;
62 struct fasync_struct *fasync;
63 struct hiddev *hiddev;
64 struct list_head node;
67 static struct hiddev *hiddev_table[HIDDEV_MINORS];
70 * Find a report, given the report's type and ID. The ID can be specified
71 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
72 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
73 * given type which follows old_id.
75 static struct hid_report *
76 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
78 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
79 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
80 struct hid_report_enum *report_enum;
81 struct hid_report *report;
82 struct list_head *list;
84 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
85 rinfo->report_type > HID_REPORT_TYPE_MAX)
86 return NULL;
88 report_enum = hid->report_enum +
89 (rinfo->report_type - HID_REPORT_TYPE_MIN);
91 switch (flags) {
92 case 0: /* Nothing to do -- report_id is already set correctly */
93 break;
95 case HID_REPORT_ID_FIRST:
96 if (list_empty(&report_enum->report_list))
97 return NULL;
99 list = report_enum->report_list.next;
100 report = list_entry(list, struct hid_report, list);
101 rinfo->report_id = report->id;
102 break;
104 case HID_REPORT_ID_NEXT:
105 report = report_enum->report_id_hash[rid];
106 if (!report)
107 return NULL;
109 list = report->list.next;
110 if (list == &report_enum->report_list)
111 return NULL;
113 report = list_entry(list, struct hid_report, list);
114 rinfo->report_id = report->id;
115 break;
117 default:
118 return NULL;
121 return report_enum->report_id_hash[rinfo->report_id];
125 * Perform an exhaustive search of the report table for a usage, given its
126 * type and usage id.
128 static struct hid_field *
129 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
131 int i, j;
132 struct hid_report *report;
133 struct hid_report_enum *report_enum;
134 struct hid_field *field;
136 if (uref->report_type < HID_REPORT_TYPE_MIN ||
137 uref->report_type > HID_REPORT_TYPE_MAX)
138 return NULL;
140 report_enum = hid->report_enum +
141 (uref->report_type - HID_REPORT_TYPE_MIN);
143 list_for_each_entry(report, &report_enum->report_list, list) {
144 for (i = 0; i < report->maxfield; i++) {
145 field = report->field[i];
146 for (j = 0; j < field->maxusage; j++) {
147 if (field->usage[j].hid == uref->usage_code) {
148 uref->report_id = report->id;
149 uref->field_index = i;
150 uref->usage_index = j;
151 return field;
157 return NULL;
160 static void hiddev_send_event(struct hid_device *hid,
161 struct hiddev_usage_ref *uref)
163 struct hiddev *hiddev = hid->hiddev;
164 struct hiddev_list *list;
165 unsigned long flags;
167 spin_lock_irqsave(&hiddev->list_lock, flags);
168 list_for_each_entry(list, &hiddev->list, node) {
169 if (uref->field_index != HID_FIELD_INDEX_NONE ||
170 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
171 list->buffer[list->head] = *uref;
172 list->head = (list->head + 1) &
173 (HIDDEV_BUFFER_SIZE - 1);
174 kill_fasync(&list->fasync, SIGIO, POLL_IN);
177 spin_unlock_irqrestore(&hiddev->list_lock, flags);
179 wake_up_interruptible(&hiddev->wait);
183 * This is where hid.c calls into hiddev to pass an event that occurred over
184 * the interrupt pipe
186 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
187 struct hid_usage *usage, __s32 value)
189 unsigned type = field->report_type;
190 struct hiddev_usage_ref uref;
192 uref.report_type =
193 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
194 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
195 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
196 uref.report_id = field->report->id;
197 uref.field_index = field->index;
198 uref.usage_index = (usage - field->usage);
199 uref.usage_code = usage->hid;
200 uref.value = value;
202 hiddev_send_event(hid, &uref);
204 EXPORT_SYMBOL_GPL(hiddev_hid_event);
206 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
208 unsigned type = report->type;
209 struct hiddev_usage_ref uref;
211 memset(&uref, 0, sizeof(uref));
212 uref.report_type =
213 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
214 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
215 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
216 uref.report_id = report->id;
217 uref.field_index = HID_FIELD_INDEX_NONE;
219 hiddev_send_event(hid, &uref);
223 * fasync file op
225 static int hiddev_fasync(int fd, struct file *file, int on)
227 int retval;
228 struct hiddev_list *list = file->private_data;
230 retval = fasync_helper(fd, file, on, &list->fasync);
232 return retval < 0 ? retval : 0;
237 * release file op
239 static int hiddev_release(struct inode * inode, struct file * file)
241 struct hiddev_list *list = file->private_data;
242 unsigned long flags;
244 hiddev_fasync(-1, file, 0);
246 spin_lock_irqsave(&list->hiddev->list_lock, flags);
247 list_del(&list->node);
248 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
250 if (!--list->hiddev->open) {
251 if (list->hiddev->exist)
252 usbhid_close(list->hiddev->hid);
253 else
254 kfree(list->hiddev);
257 kfree(list);
259 return 0;
263 * open file op
265 static int hiddev_open(struct inode *inode, struct file *file)
267 struct hiddev_list *list;
268 unsigned long flags;
270 int i = iminor(inode) - HIDDEV_MINOR_BASE;
272 if (i >= HIDDEV_MINORS || !hiddev_table[i])
273 return -ENODEV;
275 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
276 return -ENOMEM;
278 list->hiddev = hiddev_table[i];
280 spin_lock_irqsave(&list->hiddev->list_lock, flags);
281 list_add_tail(&list->node, &hiddev_table[i]->list);
282 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
284 file->private_data = list;
286 if (!list->hiddev->open++)
287 if (list->hiddev->exist)
288 usbhid_open(hiddev_table[i]->hid);
290 return 0;
294 * "write" file op
296 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
298 return -EINVAL;
302 * "read" file op
304 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
306 DECLARE_WAITQUEUE(wait, current);
307 struct hiddev_list *list = file->private_data;
308 int event_size;
309 int retval = 0;
311 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
312 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
314 if (count < event_size)
315 return 0;
317 while (retval == 0) {
318 if (list->head == list->tail) {
319 add_wait_queue(&list->hiddev->wait, &wait);
320 set_current_state(TASK_INTERRUPTIBLE);
322 while (list->head == list->tail) {
323 if (file->f_flags & O_NONBLOCK) {
324 retval = -EAGAIN;
325 break;
327 if (signal_pending(current)) {
328 retval = -ERESTARTSYS;
329 break;
331 if (!list->hiddev->exist) {
332 retval = -EIO;
333 break;
336 schedule();
337 set_current_state(TASK_INTERRUPTIBLE);
340 set_current_state(TASK_RUNNING);
341 remove_wait_queue(&list->hiddev->wait, &wait);
344 if (retval)
345 return retval;
348 while (list->head != list->tail &&
349 retval + event_size <= count) {
350 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
351 if (list->buffer[list->tail].field_index !=
352 HID_FIELD_INDEX_NONE) {
353 struct hiddev_event event;
354 event.hid = list->buffer[list->tail].usage_code;
355 event.value = list->buffer[list->tail].value;
356 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
357 return -EFAULT;
358 retval += sizeof(struct hiddev_event);
360 } else {
361 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
362 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
363 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
364 return -EFAULT;
365 retval += sizeof(struct hiddev_usage_ref);
368 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
373 return retval;
377 * "poll" file op
378 * No kernel lock - fine
380 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
382 struct hiddev_list *list = file->private_data;
384 poll_wait(file, &list->hiddev->wait, wait);
385 if (list->head != list->tail)
386 return POLLIN | POLLRDNORM;
387 if (!list->hiddev->exist)
388 return POLLERR | POLLHUP;
389 return 0;
393 * "ioctl" file op
395 static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
397 struct hiddev_list *list = file->private_data;
398 struct hiddev *hiddev = list->hiddev;
399 struct hid_device *hid = hiddev->hid;
400 struct usb_device *dev = hid_to_usb_dev(hid);
401 struct hiddev_collection_info cinfo;
402 struct hiddev_report_info rinfo;
403 struct hiddev_field_info finfo;
404 struct hiddev_usage_ref_multi *uref_multi = NULL;
405 struct hiddev_usage_ref *uref;
406 struct hiddev_devinfo dinfo;
407 struct hid_report *report;
408 struct hid_field *field;
409 struct usbhid_device *usbhid = hid->driver_data;
410 void __user *user_arg = (void __user *)arg;
411 int i;
413 if (!hiddev->exist)
414 return -EIO;
416 switch (cmd) {
418 case HIDIOCGVERSION:
419 return put_user(HID_VERSION, (int __user *)arg);
421 case HIDIOCAPPLICATION:
422 if (arg < 0 || arg >= hid->maxapplication)
423 return -EINVAL;
425 for (i = 0; i < hid->maxcollection; i++)
426 if (hid->collection[i].type ==
427 HID_COLLECTION_APPLICATION && arg-- == 0)
428 break;
430 if (i == hid->maxcollection)
431 return -EINVAL;
433 return hid->collection[i].usage;
435 case HIDIOCGDEVINFO:
436 dinfo.bustype = BUS_USB;
437 dinfo.busnum = dev->bus->busnum;
438 dinfo.devnum = dev->devnum;
439 dinfo.ifnum = usbhid->ifnum;
440 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
441 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
442 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
443 dinfo.num_applications = hid->maxapplication;
444 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
445 return -EFAULT;
447 return 0;
449 case HIDIOCGFLAG:
450 if (put_user(list->flags, (int __user *)arg))
451 return -EFAULT;
453 return 0;
455 case HIDIOCSFLAG:
457 int newflags;
458 if (get_user(newflags, (int __user *)arg))
459 return -EFAULT;
461 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
462 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
463 (newflags & HIDDEV_FLAG_UREF) == 0))
464 return -EINVAL;
466 list->flags = newflags;
468 return 0;
471 case HIDIOCGSTRING:
473 int idx, len;
474 char *buf;
476 if (get_user(idx, (int __user *)arg))
477 return -EFAULT;
479 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
480 return -ENOMEM;
482 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
483 kfree(buf);
484 return -EINVAL;
487 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
488 kfree(buf);
489 return -EFAULT;
492 kfree(buf);
494 return len;
497 case HIDIOCINITREPORT:
498 usbhid_init_reports(hid);
500 return 0;
502 case HIDIOCGREPORT:
503 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
504 return -EFAULT;
506 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
507 return -EINVAL;
509 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
510 return -EINVAL;
512 usbhid_submit_report(hid, report, USB_DIR_IN);
513 usbhid_wait_io(hid);
515 return 0;
517 case HIDIOCSREPORT:
518 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
519 return -EFAULT;
521 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
522 return -EINVAL;
524 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
525 return -EINVAL;
527 usbhid_submit_report(hid, report, USB_DIR_OUT);
528 usbhid_wait_io(hid);
530 return 0;
532 case HIDIOCGREPORTINFO:
533 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
534 return -EFAULT;
536 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
537 return -EINVAL;
539 rinfo.num_fields = report->maxfield;
541 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
542 return -EFAULT;
544 return 0;
546 case HIDIOCGFIELDINFO:
547 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
548 return -EFAULT;
549 rinfo.report_type = finfo.report_type;
550 rinfo.report_id = finfo.report_id;
551 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
552 return -EINVAL;
554 if (finfo.field_index >= report->maxfield)
555 return -EINVAL;
557 field = report->field[finfo.field_index];
558 memset(&finfo, 0, sizeof(finfo));
559 finfo.report_type = rinfo.report_type;
560 finfo.report_id = rinfo.report_id;
561 finfo.field_index = field->report_count - 1;
562 finfo.maxusage = field->maxusage;
563 finfo.flags = field->flags;
564 finfo.physical = field->physical;
565 finfo.logical = field->logical;
566 finfo.application = field->application;
567 finfo.logical_minimum = field->logical_minimum;
568 finfo.logical_maximum = field->logical_maximum;
569 finfo.physical_minimum = field->physical_minimum;
570 finfo.physical_maximum = field->physical_maximum;
571 finfo.unit_exponent = field->unit_exponent;
572 finfo.unit = field->unit;
574 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
575 return -EFAULT;
577 return 0;
579 case HIDIOCGUCODE:
580 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
581 if (!uref_multi)
582 return -ENOMEM;
583 uref = &uref_multi->uref;
584 if (copy_from_user(uref, user_arg, sizeof(*uref)))
585 goto fault;
587 rinfo.report_type = uref->report_type;
588 rinfo.report_id = uref->report_id;
589 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
590 goto inval;
592 if (uref->field_index >= report->maxfield)
593 goto inval;
595 field = report->field[uref->field_index];
596 if (uref->usage_index >= field->maxusage)
597 goto inval;
599 uref->usage_code = field->usage[uref->usage_index].hid;
601 if (copy_to_user(user_arg, uref, sizeof(*uref)))
602 goto fault;
604 kfree(uref_multi);
605 return 0;
607 case HIDIOCGUSAGE:
608 case HIDIOCSUSAGE:
609 case HIDIOCGUSAGES:
610 case HIDIOCSUSAGES:
611 case HIDIOCGCOLLECTIONINDEX:
612 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
613 if (!uref_multi)
614 return -ENOMEM;
615 uref = &uref_multi->uref;
616 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
617 if (copy_from_user(uref_multi, user_arg,
618 sizeof(*uref_multi)))
619 goto fault;
620 } else {
621 if (copy_from_user(uref, user_arg, sizeof(*uref)))
622 goto fault;
625 if (cmd != HIDIOCGUSAGE &&
626 cmd != HIDIOCGUSAGES &&
627 uref->report_type == HID_REPORT_TYPE_INPUT)
628 goto inval;
630 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
631 field = hiddev_lookup_usage(hid, uref);
632 if (field == NULL)
633 goto inval;
634 } else {
635 rinfo.report_type = uref->report_type;
636 rinfo.report_id = uref->report_id;
637 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
638 goto inval;
640 if (uref->field_index >= report->maxfield)
641 goto inval;
643 field = report->field[uref->field_index];
645 if (cmd == HIDIOCGCOLLECTIONINDEX) {
646 if (uref->usage_index >= field->maxusage)
647 goto inval;
648 } else if (uref->usage_index >= field->report_count)
649 goto inval;
651 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
652 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
653 uref->usage_index + uref_multi->num_values > field->report_count))
654 goto inval;
657 switch (cmd) {
658 case HIDIOCGUSAGE:
659 uref->value = field->value[uref->usage_index];
660 if (copy_to_user(user_arg, uref, sizeof(*uref)))
661 goto fault;
662 goto goodreturn;
664 case HIDIOCSUSAGE:
665 field->value[uref->usage_index] = uref->value;
666 goto goodreturn;
668 case HIDIOCGCOLLECTIONINDEX:
669 kfree(uref_multi);
670 return field->usage[uref->usage_index].collection_index;
671 case HIDIOCGUSAGES:
672 for (i = 0; i < uref_multi->num_values; i++)
673 uref_multi->values[i] =
674 field->value[uref->usage_index + i];
675 if (copy_to_user(user_arg, uref_multi,
676 sizeof(*uref_multi)))
677 goto fault;
678 goto goodreturn;
679 case HIDIOCSUSAGES:
680 for (i = 0; i < uref_multi->num_values; i++)
681 field->value[uref->usage_index + i] =
682 uref_multi->values[i];
683 goto goodreturn;
686 goodreturn:
687 kfree(uref_multi);
688 return 0;
689 fault:
690 kfree(uref_multi);
691 return -EFAULT;
692 inval:
693 kfree(uref_multi);
694 return -EINVAL;
696 case HIDIOCGCOLLECTIONINFO:
697 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
698 return -EFAULT;
700 if (cinfo.index >= hid->maxcollection)
701 return -EINVAL;
703 cinfo.type = hid->collection[cinfo.index].type;
704 cinfo.usage = hid->collection[cinfo.index].usage;
705 cinfo.level = hid->collection[cinfo.index].level;
707 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
708 return -EFAULT;
709 return 0;
711 default:
713 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
714 return -EINVAL;
716 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
717 int len;
718 if (!hid->name)
719 return 0;
720 len = strlen(hid->name) + 1;
721 if (len > _IOC_SIZE(cmd))
722 len = _IOC_SIZE(cmd);
723 return copy_to_user(user_arg, hid->name, len) ?
724 -EFAULT : len;
727 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
728 int len;
729 if (!hid->phys)
730 return 0;
731 len = strlen(hid->phys) + 1;
732 if (len > _IOC_SIZE(cmd))
733 len = _IOC_SIZE(cmd);
734 return copy_to_user(user_arg, hid->phys, len) ?
735 -EFAULT : len;
738 return -EINVAL;
741 static const struct file_operations hiddev_fops = {
742 .owner = THIS_MODULE,
743 .read = hiddev_read,
744 .write = hiddev_write,
745 .poll = hiddev_poll,
746 .open = hiddev_open,
747 .release = hiddev_release,
748 .ioctl = hiddev_ioctl,
749 .fasync = hiddev_fasync,
752 static struct usb_class_driver hiddev_class = {
753 .name = "hiddev%d",
754 .fops = &hiddev_fops,
755 .minor_base = HIDDEV_MINOR_BASE,
759 * This is where hid.c calls us to connect a hid device to the hiddev driver
761 int hiddev_connect(struct hid_device *hid)
763 struct hiddev *hiddev;
764 struct usbhid_device *usbhid = hid->driver_data;
765 int i;
766 int retval;
768 for (i = 0; i < hid->maxcollection; i++)
769 if (hid->collection[i].type ==
770 HID_COLLECTION_APPLICATION &&
771 !IS_INPUT_APPLICATION(hid->collection[i].usage))
772 break;
774 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
775 return -1;
777 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
778 return -1;
780 retval = usb_register_dev(usbhid->intf, &hiddev_class);
781 if (retval) {
782 err_hid("Not able to get a minor for this device.");
783 kfree(hiddev);
784 return -1;
787 init_waitqueue_head(&hiddev->wait);
788 INIT_LIST_HEAD(&hiddev->list);
789 spin_lock_init(&hiddev->list_lock);
790 hiddev->hid = hid;
791 hiddev->exist = 1;
793 hid->minor = usbhid->intf->minor;
794 hid->hiddev = hiddev;
796 hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
798 return 0;
802 * This is where hid.c calls us to disconnect a hiddev device from the
803 * corresponding hid device (usually because the usb device has disconnected)
805 static struct usb_class_driver hiddev_class;
806 void hiddev_disconnect(struct hid_device *hid)
808 struct hiddev *hiddev = hid->hiddev;
809 struct usbhid_device *usbhid = hid->driver_data;
811 hiddev->exist = 0;
813 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
814 usb_deregister_dev(usbhid->intf, &hiddev_class);
816 if (hiddev->open) {
817 usbhid_close(hiddev->hid);
818 wake_up_interruptible(&hiddev->wait);
819 } else {
820 kfree(hiddev);
824 /* Currently this driver is a USB driver. It's not a conventional one in
825 * the sense that it doesn't probe at the USB level. Instead it waits to
826 * be connected by HID through the hiddev_connect / hiddev_disconnect
827 * routines. The reason to register as a USB device is to gain part of the
828 * minor number space from the USB major.
830 * In theory, should the HID code be generalized to more than one physical
831 * medium (say, IEEE 1384), this driver will probably need to register its
832 * own major number, and in doing so, no longer need to register with USB.
833 * At that point the probe routine and hiddev_driver struct below will no
834 * longer be useful.
838 /* We never attach in this manner, and rely on HID to connect us. This
839 * is why there is no disconnect routine defined in the usb_driver either.
841 static int hiddev_usbd_probe(struct usb_interface *intf,
842 const struct usb_device_id *hiddev_info)
844 return -ENODEV;
848 static /* const */ struct usb_driver hiddev_driver = {
849 .name = "hiddev",
850 .probe = hiddev_usbd_probe,
853 int __init hiddev_init(void)
855 return usb_register(&hiddev_driver);
858 void hiddev_exit(void)
860 usb_deregister(&hiddev_driver);