2 * Copyright (c) 2001 Paul Stewart
3 * Copyright (c) 2001 Vojtech Pavlik
5 * HID char devices, giving access to raw HID device events.
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 #define HIDDEV_MINOR_BASE 96
29 #define HIDDEV_MINORS 16
30 #define HIDDEV_BUFFER_SIZE 64
32 #include <linux/poll.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/smp_lock.h>
37 #include <linux/input.h>
38 #include <linux/usb.h>
40 #include <linux/hiddev.h>
46 wait_queue_head_t wait;
48 struct hid_device *hid;
49 struct hiddev_list *list;
53 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
57 struct fasync_struct *fasync;
58 struct hiddev *hiddev;
59 struct hiddev_list *next;
62 static struct hiddev *hiddev_table[HIDDEV_MINORS];
63 static devfs_handle_t hiddev_devfs_handle;
66 * Find a report, given the report's type and ID. The ID can be specified
67 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
68 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
69 * given type which follows old_id.
71 static struct hid_report *
72 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
74 unsigned flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
75 struct hid_report_enum *report_enum = NULL;
76 struct list_head *list;
78 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
79 rinfo->report_type > HID_REPORT_TYPE_MAX) return NULL;
81 report_enum = hid->report_enum +
82 (rinfo->report_type - HID_REPORT_TYPE_MIN);
85 case 0: /* Nothing to do -- report_id is already set correctly */
88 case HID_REPORT_ID_FIRST:
89 list = report_enum->report_list.next;
90 if (list == &report_enum->report_list) return NULL;
91 rinfo->report_id = ((struct hid_report *) list)->id;
94 case HID_REPORT_ID_NEXT:
95 list = (struct list_head *)
96 report_enum->report_id_hash[rinfo->report_id &
98 if (list == NULL) return NULL;
100 if (list == &report_enum->report_list) return NULL;
101 rinfo->report_id = ((struct hid_report *) list)->id;
108 return report_enum->report_id_hash[rinfo->report_id];
112 * Perform an exhaustive search of the report table for a usage, given its
115 static struct hid_field *
116 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
119 struct hid_report *report;
120 struct hid_report_enum *report_enum;
121 struct list_head *list;
122 struct hid_field *field;
124 if (uref->report_type < HID_REPORT_TYPE_MIN ||
125 uref->report_type > HID_REPORT_TYPE_MAX) return NULL;
127 report_enum = hid->report_enum +
128 (uref->report_type - HID_REPORT_TYPE_MIN);
129 list = report_enum->report_list.next;
130 while (list != &report_enum->report_list) {
131 report = (struct hid_report *) list;
132 for (i = 0; i < report->maxfield; i++) {
133 field = report->field[i];
134 for (j = 0; j < field->maxusage; j++) {
135 if (field->usage[j].hid == uref->usage_code) {
136 uref->report_id = report->id;
137 uref->field_index = i;
138 uref->usage_index = j;
149 static void hiddev_send_event(struct hid_device *hid,
150 struct hiddev_usage_ref *uref)
152 struct hiddev *hiddev = hid->hiddev;
153 struct hiddev_list *list = hiddev->list;
156 if (uref->field_index != HID_FIELD_INDEX_NONE ||
157 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
158 list->buffer[list->head] = *uref;
159 list->head = (list->head + 1) &
160 (HIDDEV_BUFFER_SIZE - 1);
161 kill_fasync(&list->fasync, SIGIO, POLL_IN);
167 wake_up_interruptible(&hiddev->wait);
171 * This is where hid.c calls into hiddev to pass an event that occurred over
174 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
175 struct hid_usage *usage, __s32 value)
177 unsigned type = field->report_type;
178 struct hiddev_usage_ref uref;
181 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
182 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
183 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
184 uref.report_id = field->report->id;
185 uref.field_index = field->index;
186 uref.usage_index = (usage - field->usage);
187 uref.usage_code = usage->hid;
190 hiddev_send_event(hid, &uref);
194 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
196 unsigned type = report->type;
197 struct hiddev_usage_ref uref;
199 memset(&uref, 0, sizeof(uref));
201 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
202 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
203 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
204 uref.report_id = report->id;
205 uref.field_index = HID_FIELD_INDEX_NONE;
207 hiddev_send_event(hid, &uref);
213 static int hiddev_fasync(int fd, struct file *file, int on)
216 struct hiddev_list *list = file->private_data;
217 retval = fasync_helper(fd, file, on, &list->fasync);
218 return retval < 0 ? retval : 0;
222 * De-allocate a hiddev structure
224 static void hiddev_cleanup(struct hiddev *hiddev)
226 devfs_unregister(hiddev->devfs);
227 hiddev_table[hiddev->minor] = NULL;
234 static int hiddev_release(struct inode * inode, struct file * file)
236 struct hiddev_list *list = file->private_data;
237 struct hiddev_list **listptr;
239 listptr = &list->hiddev->list;
240 hiddev_fasync(-1, file, 0);
242 while (*listptr && (*listptr != list))
243 listptr = &((*listptr)->next);
244 *listptr = (*listptr)->next;
246 if (!--list->hiddev->open) {
247 if (list->hiddev->exist)
248 hid_close(list->hiddev->hid);
250 hiddev_cleanup(list->hiddev);
261 static int hiddev_open(struct inode * inode, struct file * file) {
262 struct hiddev_list *list;
264 int i = MINOR(inode->i_rdev) - HIDDEV_MINOR_BASE;
266 if (i >= HIDDEV_MINORS || !hiddev_table[i])
269 if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
271 memset(list, 0, sizeof(struct hiddev_list));
273 list->hiddev = hiddev_table[i];
274 list->next = hiddev_table[i]->list;
275 hiddev_table[i]->list = list;
277 file->private_data = list;
279 if (!list->hiddev->open++)
280 if (list->hiddev->exist)
281 hid_open(hiddev_table[i]->hid);
289 static ssize_t hiddev_write(struct file * file, const char * buffer,
290 size_t count, loff_t *ppos)
298 static ssize_t hiddev_read(struct file * file, char * buffer, size_t count,
301 DECLARE_WAITQUEUE(wait, current);
302 struct hiddev_list *list = file->private_data;
306 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
307 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
309 if (count < event_size) return 0;
311 while (retval == 0) {
312 if (list->head == list->tail) {
313 add_wait_queue(&list->hiddev->wait, &wait);
314 set_current_state(TASK_INTERRUPTIBLE);
316 while (list->head == list->tail) {
317 if (file->f_flags & O_NONBLOCK) {
321 if (signal_pending(current)) {
322 retval = -ERESTARTSYS;
325 if (!list->hiddev->exist) {
333 set_current_state(TASK_RUNNING);
334 remove_wait_queue(&list->hiddev->wait, &wait);
340 while (list->head != list->tail &&
341 retval + event_size <= count) {
342 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
343 if (list->buffer[list->tail].field_index !=
344 HID_FIELD_INDEX_NONE) {
345 struct hiddev_event event;
346 event.hid = list->buffer[list->tail].usage_code;
347 event.value = list->buffer[list->tail].value;
348 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
350 retval += sizeof(struct hiddev_event);
353 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
354 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
355 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
357 retval += sizeof(struct hiddev_usage_ref);
360 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
370 * No kernel lock - fine
372 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
374 struct hiddev_list *list = file->private_data;
375 poll_wait(file, &list->hiddev->wait, wait);
376 if (list->head != list->tail)
377 return POLLIN | POLLRDNORM;
378 if (!list->hiddev->exist)
379 return POLLERR | POLLHUP;
383 #define GET_TIMEOUT 3
384 #define SET_TIMEOUT 3
389 static int hiddev_ioctl(struct inode *inode, struct file *file,
390 unsigned int cmd, unsigned long arg)
392 struct hiddev_list *list = file->private_data;
393 struct hiddev *hiddev = list->hiddev;
394 struct hid_device *hid = hiddev->hid;
395 struct usb_device *dev = hid->dev;
396 struct hiddev_collection_info cinfo;
397 struct hiddev_report_info rinfo;
398 struct hiddev_field_info finfo;
399 struct hiddev_usage_ref_multi uref_multi;
400 struct hiddev_usage_ref *uref = &uref_multi.uref;
401 struct hiddev_devinfo dinfo;
402 struct hid_report *report;
403 struct hid_field *field;
406 if (!hiddev->exist) return -EIO;
411 return put_user(HID_VERSION, (int *) arg);
413 case HIDIOCAPPLICATION:
414 if (arg < 0 || arg >= hid->maxapplication)
417 for (i = 0; i < hid->maxcollection; i++)
418 if (hid->collection[i].type ==
419 HID_COLLECTION_APPLICATION && arg-- == 0)
422 if (i == hid->maxcollection)
425 return hid->collection[i].usage;
428 dinfo.bustype = BUS_USB;
429 dinfo.busnum = dev->bus->busnum;
430 dinfo.devnum = dev->devnum;
431 dinfo.ifnum = hid->ifnum;
432 dinfo.vendor = dev->descriptor.idVendor;
433 dinfo.product = dev->descriptor.idProduct;
434 dinfo.version = dev->descriptor.bcdDevice;
435 dinfo.num_applications = hid->maxapplication;
436 if (copy_to_user((void *) arg, &dinfo, sizeof(dinfo)))
441 return put_user(list->flags, (int *) arg);
446 if (get_user(newflags, (int *) arg))
449 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
450 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
451 (newflags & HIDDEV_FLAG_UREF) == 0))
454 list->flags = newflags;
464 if (get_user(idx, (int *) arg))
467 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
470 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
475 if (copy_to_user((void *) (arg+sizeof(int)), buf, len+1)) {
485 case HIDIOCINITREPORT:
486 hid_init_reports(hid);
491 if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
494 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
497 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
500 hid_read_report(hid, report);
505 if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
508 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
511 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
514 hid_write_report(hid, report);
518 case HIDIOCGREPORTINFO:
519 if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
522 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
525 rinfo.num_fields = report->maxfield;
527 if (copy_to_user((void *) arg, &rinfo, sizeof(rinfo)))
531 case HIDIOCGFIELDINFO:
532 if (copy_from_user(&finfo, (void *) arg, sizeof(finfo)))
534 rinfo.report_type = finfo.report_type;
535 rinfo.report_id = finfo.report_id;
536 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
539 if (finfo.field_index >= report->maxfield)
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((void *) arg, &finfo, sizeof(finfo)))
564 if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
567 rinfo.report_type = uref->report_type;
568 rinfo.report_id = uref->report_id;
569 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
572 if (uref->field_index >= report->maxfield)
575 field = report->field[uref->field_index];
576 if (uref->usage_index >= field->maxusage)
579 uref->usage_code = field->usage[uref->usage_index].hid;
581 if (copy_to_user((void *) arg, uref, sizeof(*uref)))
589 case HIDIOCGCOLLECTIONINDEX:
590 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
591 if (copy_from_user(&uref_multi, (void *) arg,
595 if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
599 if (cmd != HIDIOCGUSAGE &&
600 cmd != HIDIOCGUSAGES &&
601 uref->report_type == HID_REPORT_TYPE_INPUT)
604 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
605 field = hiddev_lookup_usage(hid, uref);
609 rinfo.report_type = uref->report_type;
610 rinfo.report_id = uref->report_id;
611 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
614 if (uref->field_index >= report->maxfield)
617 field = report->field[uref->field_index];
618 if (uref->usage_index >= field->maxusage)
621 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
622 if (uref_multi.num_values >= HID_MAX_USAGES ||
623 uref->usage_index >= field->maxusage ||
624 (uref->usage_index + uref_multi.num_values) >= field->maxusage)
631 uref->value = field->value[uref->usage_index];
632 return copy_to_user((void *) arg, uref, sizeof(*uref));
635 field->value[uref->usage_index] = uref->value;
638 case HIDIOCGCOLLECTIONINDEX:
639 return field->usage[uref->usage_index].collection_index;
641 for (i = 0; i < uref_multi.num_values; i++)
642 uref_multi.values[i] =
643 field->value[uref->usage_index + i];
644 if (copy_to_user((void *) arg, &uref_multi,
649 for (i = 0; i < uref_multi.num_values; i++)
650 field->value[uref->usage_index + i] =
651 uref_multi.values[i];
656 case HIDIOCGCOLLECTIONINFO:
657 if (copy_from_user(&cinfo, (void *) arg, sizeof(cinfo)))
660 if (cinfo.index >= hid->maxcollection)
663 cinfo.type = hid->collection[cinfo.index].type;
664 cinfo.usage = hid->collection[cinfo.index].usage;
665 cinfo.level = hid->collection[cinfo.index].level;
667 if (copy_to_user((void *) arg, &cinfo, sizeof(cinfo)))
673 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
676 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
678 if (!hid->name) return 0;
679 len = strlen(hid->name) + 1;
680 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
681 return copy_to_user((char *) arg, hid->name, len) ?
688 static struct file_operations hiddev_fops = {
694 release: hiddev_release,
696 fasync: hiddev_fasync,
700 * This is where hid.c calls us to connect a hid device to the hiddev driver
702 int hiddev_connect(struct hid_device *hid)
704 struct hiddev *hiddev;
710 if ((hid->quirks & HID_QUIRK_HIDDEV) == 0) {
711 for (i = 0; i < hid->maxcollection; i++)
712 if (hid->collection[i].type ==
713 HID_COLLECTION_APPLICATION &&
714 !IS_INPUT_APPLICATION(hid->collection[i].usage))
717 if (i == hid->maxcollection)
721 for (minor = 0; minor < HIDDEV_MINORS && hiddev_table[minor]; minor++);
722 if (minor == HIDDEV_MINORS) {
723 printk(KERN_ERR "hiddev: no more free hiddev devices\n");
727 if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
729 memset(hiddev, 0, sizeof(struct hiddev));
731 init_waitqueue_head(&hiddev->wait);
733 hiddev->minor = minor;
734 hiddev_table[minor] = hiddev;
739 sprintf(devfs_name, "hiddev%d", minor);
740 hiddev->devfs = devfs_register(hiddev_devfs_handle, devfs_name,
741 DEVFS_FL_DEFAULT, USB_MAJOR,
742 minor + HIDDEV_MINOR_BASE,
743 S_IFCHR | S_IRUGO | S_IWUSR,
746 hid->hiddev = hiddev;
752 * This is where hid.c calls us to disconnect a hiddev device from the
753 * corresponding hid device (usually because the usb device has disconnected)
755 void hiddev_disconnect(struct hid_device *hid)
757 struct hiddev *hiddev = hid->hiddev;
762 hid_close(hiddev->hid);
763 wake_up_interruptible(&hiddev->wait);
765 hiddev_cleanup(hiddev);
769 /* Currently this driver is a USB driver. It's not a conventional one in
770 * the sense that it doesn't probe at the USB level. Instead it waits to
771 * be connected by HID through the hiddev_connect / hiddev_disconnect
772 * routines. The reason to register as a USB device is to gain part of the
773 * minor number space from the USB major.
775 * In theory, should the HID code be generalized to more than one physical
776 * medium (say, IEEE 1384), this driver will probably need to register its
777 * own major number, and in doing so, no longer need to register with USB.
778 * At that point the probe routine and hiddev_driver struct below will no
783 /* We never attach in this manner, and rely on HID to connect us. This
784 * is why there is no disconnect routine defined in the usb_driver either.
786 static void *hiddev_usbd_probe(struct usb_device *dev, unsigned int ifnum,
787 const struct usb_device_id *hiddev_info)
793 static /* const */ struct usb_driver hiddev_driver = {
795 probe: hiddev_usbd_probe,
797 minor: HIDDEV_MINOR_BASE
800 int __init hiddev_init(void)
802 hiddev_devfs_handle =
803 devfs_mk_dir(devfs_find_handle(NULL, "usb", 0, 0, 0, 0), "hid", NULL);
804 usb_register(&hiddev_driver);
808 void __exit hiddev_exit(void)
810 devfs_unregister(hiddev_devfs_handle);
811 usb_deregister(&hiddev_driver);