MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / input / hiddev.c
blob44e466d196b44edaeabb12e2854b67677048e908
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/config.h>
29 #include <linux/poll.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/smp_lock.h>
34 #include <linux/input.h>
35 #include <linux/usb.h>
36 #include "hid.h"
37 #include <linux/hiddev.h>
38 #include <linux/devfs_fs_kernel.h>
40 #ifdef CONFIG_USB_DYNAMIC_MINORS
41 #define HIDDEV_MINOR_BASE 0
42 #define HIDDEV_MINORS 256
43 #else
44 #define HIDDEV_MINOR_BASE 96
45 #define HIDDEV_MINORS 16
46 #endif
47 #define HIDDEV_BUFFER_SIZE 64
49 struct hiddev {
50 int exist;
51 int open;
52 wait_queue_head_t wait;
53 struct hid_device *hid;
54 struct hiddev_list *list;
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 hiddev_list *next;
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 flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
79 struct hid_report_enum *report_enum;
80 struct list_head *list;
82 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
83 rinfo->report_type > HID_REPORT_TYPE_MAX) return NULL;
85 report_enum = hid->report_enum +
86 (rinfo->report_type - HID_REPORT_TYPE_MIN);
88 switch (flags) {
89 case 0: /* Nothing to do -- report_id is already set correctly */
90 break;
92 case HID_REPORT_ID_FIRST:
93 list = report_enum->report_list.next;
94 if (list == &report_enum->report_list)
95 return NULL;
96 rinfo->report_id = ((struct hid_report *) list)->id;
97 break;
99 case HID_REPORT_ID_NEXT:
100 list = (struct list_head *)
101 report_enum->report_id_hash[rinfo->report_id & HID_REPORT_ID_MASK];
102 if (list == NULL)
103 return NULL;
104 list = list->next;
105 if (list == &report_enum->report_list)
106 return NULL;
107 rinfo->report_id = ((struct hid_report *) list)->id;
108 break;
110 default:
111 return NULL;
114 return report_enum->report_id_hash[rinfo->report_id];
118 * Perform an exhaustive search of the report table for a usage, given its
119 * type and usage id.
121 static struct hid_field *
122 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
124 int i, j;
125 struct hid_report *report;
126 struct hid_report_enum *report_enum;
127 struct list_head *list;
128 struct hid_field *field;
130 if (uref->report_type < HID_REPORT_TYPE_MIN ||
131 uref->report_type > HID_REPORT_TYPE_MAX) return NULL;
133 report_enum = hid->report_enum +
134 (uref->report_type - HID_REPORT_TYPE_MIN);
135 list = report_enum->report_list.next;
136 while (list != &report_enum->report_list) {
137 report = (struct hid_report *) list;
138 for (i = 0; i < report->maxfield; i++) {
139 field = report->field[i];
140 for (j = 0; j < field->maxusage; j++) {
141 if (field->usage[j].hid == uref->usage_code) {
142 uref->report_id = report->id;
143 uref->field_index = i;
144 uref->usage_index = j;
145 return field;
149 list = list->next;
152 return NULL;
155 static void hiddev_send_event(struct hid_device *hid,
156 struct hiddev_usage_ref *uref)
158 struct hiddev *hiddev = hid->hiddev;
159 struct hiddev_list *list = hiddev->list;
161 while (list) {
162 if (uref->field_index != HID_FIELD_INDEX_NONE ||
163 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
164 list->buffer[list->head] = *uref;
165 list->head = (list->head + 1) &
166 (HIDDEV_BUFFER_SIZE - 1);
167 kill_fasync(&list->fasync, SIGIO, POLL_IN);
170 list = list->next;
173 wake_up_interruptible(&hiddev->wait);
177 * This is where hid.c calls into hiddev to pass an event that occurred over
178 * the interrupt pipe
180 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
181 struct hid_usage *usage, __s32 value, struct pt_regs *regs)
183 unsigned type = field->report_type;
184 struct hiddev_usage_ref uref;
186 uref.report_type =
187 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
188 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
189 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
190 uref.report_id = field->report->id;
191 uref.field_index = field->index;
192 uref.usage_index = (usage - field->usage);
193 uref.usage_code = usage->hid;
194 uref.value = value;
196 hiddev_send_event(hid, &uref);
200 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
202 unsigned type = report->type;
203 struct hiddev_usage_ref uref;
205 memset(&uref, 0, sizeof(uref));
206 uref.report_type =
207 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
208 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
209 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
210 uref.report_id = report->id;
211 uref.field_index = HID_FIELD_INDEX_NONE;
213 hiddev_send_event(hid, &uref);
216 * fasync file op
218 static int hiddev_fasync(int fd, struct file *file, int on)
220 int retval;
221 struct hiddev_list *list = file->private_data;
222 retval = fasync_helper(fd, file, on, &list->fasync);
223 return retval < 0 ? retval : 0;
228 * release file op
230 static int hiddev_release(struct inode * inode, struct file * file)
232 struct hiddev_list *list = file->private_data;
233 struct hiddev_list **listptr;
235 listptr = &list->hiddev->list;
236 hiddev_fasync(-1, file, 0);
238 while (*listptr && (*listptr != list))
239 listptr = &((*listptr)->next);
240 *listptr = (*listptr)->next;
242 if (!--list->hiddev->open) {
243 if (list->hiddev->exist)
244 hid_close(list->hiddev->hid);
245 else
246 kfree(list->hiddev);
249 kfree(list);
251 return 0;
255 * open file op
257 static int hiddev_open(struct inode * inode, struct file * file) {
258 struct hiddev_list *list;
260 int i = iminor(inode) - HIDDEV_MINOR_BASE;
262 if (i >= HIDDEV_MINORS || !hiddev_table[i])
263 return -ENODEV;
265 if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
266 return -ENOMEM;
267 memset(list, 0, sizeof(struct hiddev_list));
269 list->hiddev = hiddev_table[i];
270 list->next = hiddev_table[i]->list;
271 hiddev_table[i]->list = list;
273 file->private_data = list;
275 if (!list->hiddev->open++)
276 if (list->hiddev->exist)
277 hid_open(hiddev_table[i]->hid);
279 return 0;
283 * "write" file op
285 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
287 return -EINVAL;
291 * "read" file op
293 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
295 DECLARE_WAITQUEUE(wait, current);
296 struct hiddev_list *list = file->private_data;
297 int event_size;
298 int retval = 0;
300 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
301 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
303 if (count < event_size)
304 return 0;
306 while (retval == 0) {
307 if (list->head == list->tail) {
308 add_wait_queue(&list->hiddev->wait, &wait);
309 set_current_state(TASK_INTERRUPTIBLE);
311 while (list->head == list->tail) {
312 if (file->f_flags & O_NONBLOCK) {
313 retval = -EAGAIN;
314 break;
316 if (signal_pending(current)) {
317 retval = -ERESTARTSYS;
318 break;
320 if (!list->hiddev->exist) {
321 retval = -EIO;
322 break;
325 schedule();
328 set_current_state(TASK_RUNNING);
329 remove_wait_queue(&list->hiddev->wait, &wait);
332 if (retval)
333 return retval;
336 while (list->head != list->tail &&
337 retval + event_size <= count) {
338 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
339 if (list->buffer[list->tail].field_index !=
340 HID_FIELD_INDEX_NONE) {
341 struct hiddev_event event;
342 event.hid = list->buffer[list->tail].usage_code;
343 event.value = list->buffer[list->tail].value;
344 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
345 return -EFAULT;
346 retval += sizeof(struct hiddev_event);
348 } else {
349 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
350 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
351 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
352 return -EFAULT;
353 retval += sizeof(struct hiddev_usage_ref);
356 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
361 return retval;
365 * "poll" file op
366 * No kernel lock - fine
368 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
370 struct hiddev_list *list = file->private_data;
371 poll_wait(file, &list->hiddev->wait, wait);
372 if (list->head != list->tail)
373 return POLLIN | POLLRDNORM;
374 if (!list->hiddev->exist)
375 return POLLERR | POLLHUP;
376 return 0;
380 * "ioctl" file op
382 static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
384 struct hiddev_list *list = file->private_data;
385 struct hiddev *hiddev = list->hiddev;
386 struct hid_device *hid = hiddev->hid;
387 struct usb_device *dev = hid->dev;
388 struct hiddev_collection_info cinfo;
389 struct hiddev_report_info rinfo;
390 struct hiddev_field_info finfo;
391 struct hiddev_usage_ref_multi *uref_multi=NULL;
392 struct hiddev_usage_ref *uref;
393 struct hiddev_devinfo dinfo;
394 struct hid_report *report;
395 struct hid_field *field;
396 void __user *user_arg = (void __user *)arg;
397 int i;
399 if (!hiddev->exist)
400 return -EIO;
402 switch (cmd) {
404 case HIDIOCGVERSION:
405 return put_user(HID_VERSION, (int __user *)arg);
407 case HIDIOCAPPLICATION:
408 if (arg < 0 || arg >= hid->maxapplication)
409 return -EINVAL;
411 for (i = 0; i < hid->maxcollection; i++)
412 if (hid->collection[i].type ==
413 HID_COLLECTION_APPLICATION && arg-- == 0)
414 break;
416 if (i == hid->maxcollection)
417 return -EINVAL;
419 return hid->collection[i].usage;
421 case HIDIOCGDEVINFO:
422 dinfo.bustype = BUS_USB;
423 dinfo.busnum = dev->bus->busnum;
424 dinfo.devnum = dev->devnum;
425 dinfo.ifnum = hid->ifnum;
426 dinfo.vendor = dev->descriptor.idVendor;
427 dinfo.product = dev->descriptor.idProduct;
428 dinfo.version = dev->descriptor.bcdDevice;
429 dinfo.num_applications = hid->maxapplication;
430 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
431 return -EFAULT;
433 return 0;
435 case HIDIOCGFLAG:
436 if (put_user(list->flags, (int __user *)arg))
437 return -EFAULT;
439 return 0;
441 case HIDIOCSFLAG:
443 int newflags;
444 if (get_user(newflags, (int __user *)arg))
445 return -EFAULT;
447 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
448 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
449 (newflags & HIDDEV_FLAG_UREF) == 0))
450 return -EINVAL;
452 list->flags = newflags;
454 return 0;
457 case HIDIOCGSTRING:
459 int idx, len;
460 char *buf;
462 if (get_user(idx, (int __user *)arg))
463 return -EFAULT;
465 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
466 return -ENOMEM;
468 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
469 kfree(buf);
470 return -EINVAL;
473 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
474 kfree(buf);
475 return -EFAULT;
478 kfree(buf);
480 return len;
483 case HIDIOCINITREPORT:
484 hid_init_reports(hid);
486 return 0;
488 case HIDIOCGREPORT:
489 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
490 return -EFAULT;
492 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
493 return -EINVAL;
495 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
496 return -EINVAL;
498 hid_submit_report(hid, report, USB_DIR_IN);
499 hid_wait_io(hid);
501 return 0;
503 case HIDIOCSREPORT:
504 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
505 return -EFAULT;
507 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
508 return -EINVAL;
510 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
511 return -EINVAL;
513 hid_submit_report(hid, report, USB_DIR_OUT);
515 return 0;
517 case HIDIOCGREPORTINFO:
518 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
519 return -EFAULT;
521 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
522 return -EINVAL;
524 rinfo.num_fields = report->maxfield;
526 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
527 return -EFAULT;
529 return 0;
531 case HIDIOCGFIELDINFO:
532 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
533 return -EFAULT;
534 rinfo.report_type = finfo.report_type;
535 rinfo.report_id = finfo.report_id;
536 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
537 return -EINVAL;
539 if (finfo.field_index >= report->maxfield)
540 return -EINVAL;
542 field = report->field[finfo.field_index];
543 memset(&finfo, 0, sizeof(finfo));
544 finfo.report_type = rinfo.report_type;
545 finfo.report_id = rinfo.report_id;
546 finfo.field_index = field->report_count - 1;
547 finfo.maxusage = field->maxusage;
548 finfo.flags = field->flags;
549 finfo.physical = field->physical;
550 finfo.logical = field->logical;
551 finfo.application = field->application;
552 finfo.logical_minimum = field->logical_minimum;
553 finfo.logical_maximum = field->logical_maximum;
554 finfo.physical_minimum = field->physical_minimum;
555 finfo.physical_maximum = field->physical_maximum;
556 finfo.unit_exponent = field->unit_exponent;
557 finfo.unit = field->unit;
559 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
560 return -EFAULT;
562 return 0;
564 case HIDIOCGUCODE:
565 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
566 if (!uref_multi)
567 return -ENOMEM;
568 uref = &uref_multi->uref;
569 if (copy_from_user(uref, user_arg, sizeof(*uref)))
570 goto fault;
572 rinfo.report_type = uref->report_type;
573 rinfo.report_id = uref->report_id;
574 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
575 goto inval;
577 if (uref->field_index >= report->maxfield)
578 goto inval;
580 field = report->field[uref->field_index];
581 if (uref->usage_index >= field->maxusage)
582 goto inval;
584 uref->usage_code = field->usage[uref->usage_index].hid;
586 if (copy_to_user(user_arg, uref, sizeof(*uref)))
587 goto fault;
589 kfree(uref_multi);
590 return 0;
592 case HIDIOCGUSAGE:
593 case HIDIOCSUSAGE:
594 case HIDIOCGUSAGES:
595 case HIDIOCSUSAGES:
596 case HIDIOCGCOLLECTIONINDEX:
597 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
598 if (!uref_multi)
599 return -ENOMEM;
600 uref = &uref_multi->uref;
601 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
602 if (copy_from_user(uref_multi, user_arg,
603 sizeof(*uref_multi)))
604 goto fault;
605 } else {
606 if (copy_from_user(uref, user_arg, sizeof(*uref)))
607 goto fault;
610 if (cmd != HIDIOCGUSAGE &&
611 cmd != HIDIOCGUSAGES &&
612 uref->report_type == HID_REPORT_TYPE_INPUT)
613 goto inval;
615 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
616 field = hiddev_lookup_usage(hid, uref);
617 if (field == NULL)
618 goto inval;
619 } else {
620 rinfo.report_type = uref->report_type;
621 rinfo.report_id = uref->report_id;
622 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
623 goto inval;
625 if (uref->field_index >= report->maxfield)
626 goto inval;
628 field = report->field[uref->field_index];
630 if (cmd == HIDIOCGCOLLECTIONINDEX) {
631 if (uref->usage_index >= field->maxusage)
632 goto inval;
633 } else if (uref->usage_index >= field->report_count)
634 goto inval;
636 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
637 (uref_multi->num_values >= HID_MAX_MULTI_USAGES ||
638 uref->usage_index + uref_multi->num_values >= field->report_count ||
639 uref->usage_index + uref_multi->num_values < uref->usage_index))
640 goto inval;
643 switch (cmd) {
644 case HIDIOCGUSAGE:
645 uref->value = field->value[uref->usage_index];
646 if (copy_to_user(user_arg, uref, sizeof(*uref)))
647 goto fault;
648 goto goodreturn;
650 case HIDIOCSUSAGE:
651 field->value[uref->usage_index] = uref->value;
652 goto goodreturn;
654 case HIDIOCGCOLLECTIONINDEX:
655 kfree(uref_multi);
656 return field->usage[uref->usage_index].collection_index;
657 case HIDIOCGUSAGES:
658 for (i = 0; i < uref_multi->num_values; i++)
659 uref_multi->values[i] =
660 field->value[uref->usage_index + i];
661 if (copy_to_user(user_arg, uref_multi,
662 sizeof(*uref_multi)))
663 goto fault;
664 goto goodreturn;
665 case HIDIOCSUSAGES:
666 for (i = 0; i < uref_multi->num_values; i++)
667 field->value[uref->usage_index + i] =
668 uref_multi->values[i];
669 goto goodreturn;
672 goodreturn:
673 kfree(uref_multi);
674 return 0;
675 fault:
676 kfree(uref_multi);
677 return -EFAULT;
678 inval:
679 kfree(uref_multi);
680 return -EINVAL;
682 case HIDIOCGCOLLECTIONINFO:
683 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
684 return -EFAULT;
686 if (cinfo.index >= hid->maxcollection)
687 return -EINVAL;
689 cinfo.type = hid->collection[cinfo.index].type;
690 cinfo.usage = hid->collection[cinfo.index].usage;
691 cinfo.level = hid->collection[cinfo.index].level;
693 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
694 return -EFAULT;
695 return 0;
697 default:
699 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
700 return -EINVAL;
702 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
703 int len;
704 if (!hid->name)
705 return 0;
706 len = strlen(hid->name) + 1;
707 if (len > _IOC_SIZE(cmd))
708 len = _IOC_SIZE(cmd);
709 return copy_to_user(user_arg, hid->name, len) ?
710 -EFAULT : len;
713 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
714 int len;
715 if (!hid->phys)
716 return 0;
717 len = strlen(hid->phys) + 1;
718 if (len > _IOC_SIZE(cmd))
719 len = _IOC_SIZE(cmd);
720 return copy_to_user(user_arg, hid->phys, len) ?
721 -EFAULT : len;
724 return -EINVAL;
727 static struct file_operations hiddev_fops = {
728 .owner = THIS_MODULE,
729 .read = hiddev_read,
730 .write = hiddev_write,
731 .poll = hiddev_poll,
732 .open = hiddev_open,
733 .release = hiddev_release,
734 .ioctl = hiddev_ioctl,
735 .fasync = hiddev_fasync,
738 static struct usb_class_driver hiddev_class = {
739 .name = "usb/hid/hiddev%d",
740 .fops = &hiddev_fops,
741 .mode = S_IFCHR | S_IRUGO | S_IWUSR,
742 .minor_base = HIDDEV_MINOR_BASE,
746 * This is where hid.c calls us to connect a hid device to the hiddev driver
748 int hiddev_connect(struct hid_device *hid)
750 struct hiddev *hiddev;
751 int i;
752 int retval;
754 for (i = 0; i < hid->maxcollection; i++)
755 if (hid->collection[i].type ==
756 HID_COLLECTION_APPLICATION &&
757 !IS_INPUT_APPLICATION(hid->collection[i].usage))
758 break;
760 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
761 return -1;
763 if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
764 return -1;
765 memset(hiddev, 0, sizeof(struct hiddev));
767 retval = usb_register_dev(hid->intf, &hiddev_class);
768 if (retval) {
769 err("Not able to get a minor for this device.");
770 kfree(hiddev);
771 return -1;
774 init_waitqueue_head(&hiddev->wait);
776 hiddev_table[hid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
778 hiddev->hid = hid;
779 hiddev->exist = 1;
781 hid->minor = hid->intf->minor;
782 hid->hiddev = hiddev;
784 return 0;
788 * This is where hid.c calls us to disconnect a hiddev device from the
789 * corresponding hid device (usually because the usb device has disconnected)
791 static struct usb_class_driver hiddev_class;
792 void hiddev_disconnect(struct hid_device *hid)
794 struct hiddev *hiddev = hid->hiddev;
796 hiddev->exist = 0;
798 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
799 usb_deregister_dev(hiddev->hid->intf, &hiddev_class);
801 if (hiddev->open) {
802 hid_close(hiddev->hid);
803 wake_up_interruptible(&hiddev->wait);
804 } else {
805 kfree(hiddev);
809 /* Currently this driver is a USB driver. It's not a conventional one in
810 * the sense that it doesn't probe at the USB level. Instead it waits to
811 * be connected by HID through the hiddev_connect / hiddev_disconnect
812 * routines. The reason to register as a USB device is to gain part of the
813 * minor number space from the USB major.
815 * In theory, should the HID code be generalized to more than one physical
816 * medium (say, IEEE 1384), this driver will probably need to register its
817 * own major number, and in doing so, no longer need to register with USB.
818 * At that point the probe routine and hiddev_driver struct below will no
819 * longer be useful.
823 /* We never attach in this manner, and rely on HID to connect us. This
824 * is why there is no disconnect routine defined in the usb_driver either.
826 static int hiddev_usbd_probe(struct usb_interface *intf,
827 const struct usb_device_id *hiddev_info)
829 return -ENODEV;
833 static /* const */ struct usb_driver hiddev_driver = {
834 .owner = THIS_MODULE,
835 .name = "hiddev",
836 .probe = hiddev_usbd_probe,
839 int __init hiddev_init(void)
841 devfs_mk_dir("usb/hid");
842 return usb_register(&hiddev_driver);
845 void hiddev_exit(void)
847 usb_deregister(&hiddev_driver);
848 devfs_remove("usb/hid");