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 #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>
36 #include <linux/hiddev.h>
38 #ifdef CONFIG_USB_DYNAMIC_MINORS
39 #define HIDDEV_MINOR_BASE 0
40 #define HIDDEV_MINORS 256
42 #define HIDDEV_MINOR_BASE 96
43 #define HIDDEV_MINORS 16
45 #define HIDDEV_BUFFER_SIZE 64
50 wait_queue_head_t wait;
51 struct hid_device *hid;
52 struct hiddev_list *list;
56 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
60 struct fasync_struct *fasync;
61 struct hiddev *hiddev;
62 struct hiddev_list *next;
65 static struct hiddev *hiddev_table[HIDDEV_MINORS];
68 * Find a report, given the report's type and ID. The ID can be specified
69 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
70 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
71 * given type which follows old_id.
73 static struct hid_report *
74 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
76 unsigned flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
77 struct hid_report_enum *report_enum;
78 struct list_head *list;
80 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
81 rinfo->report_type > HID_REPORT_TYPE_MAX) return NULL;
83 report_enum = hid->report_enum +
84 (rinfo->report_type - HID_REPORT_TYPE_MIN);
87 case 0: /* Nothing to do -- report_id is already set correctly */
90 case HID_REPORT_ID_FIRST:
91 list = report_enum->report_list.next;
92 if (list == &report_enum->report_list)
94 rinfo->report_id = ((struct hid_report *) list)->id;
97 case HID_REPORT_ID_NEXT:
98 list = (struct list_head *)
99 report_enum->report_id_hash[rinfo->report_id & HID_REPORT_ID_MASK];
103 if (list == &report_enum->report_list)
105 rinfo->report_id = ((struct hid_report *) list)->id;
112 return report_enum->report_id_hash[rinfo->report_id];
116 * Perform an exhaustive search of the report table for a usage, given its
119 static struct hid_field *
120 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
123 struct hid_report *report;
124 struct hid_report_enum *report_enum;
125 struct hid_field *field;
127 if (uref->report_type < HID_REPORT_TYPE_MIN ||
128 uref->report_type > HID_REPORT_TYPE_MAX) return NULL;
130 report_enum = hid->report_enum +
131 (uref->report_type - HID_REPORT_TYPE_MIN);
133 list_for_each_entry(report, &report_enum->report_list, list)
134 for (i = 0; i < report->maxfield; i++) {
135 field = report->field[i];
136 for (j = 0; j < field->maxusage; j++) {
137 if (field->usage[j].hid == uref->usage_code) {
138 uref->report_id = report->id;
139 uref->field_index = i;
140 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, struct pt_regs *regs)
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);
212 static int hiddev_fasync(int fd, struct file *file, int on)
215 struct hiddev_list *list = file->private_data;
216 retval = fasync_helper(fd, file, on, &list->fasync);
217 return retval < 0 ? retval : 0;
224 static int hiddev_release(struct inode * inode, struct file * file)
226 struct hiddev_list *list = file->private_data;
227 struct hiddev_list **listptr;
229 listptr = &list->hiddev->list;
230 hiddev_fasync(-1, file, 0);
232 while (*listptr && (*listptr != list))
233 listptr = &((*listptr)->next);
234 *listptr = (*listptr)->next;
236 if (!--list->hiddev->open) {
237 if (list->hiddev->exist)
238 hid_close(list->hiddev->hid);
251 static int hiddev_open(struct inode * inode, struct file * file) {
252 struct hiddev_list *list;
254 int i = iminor(inode) - HIDDEV_MINOR_BASE;
256 if (i >= HIDDEV_MINORS || !hiddev_table[i])
259 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
262 list->hiddev = hiddev_table[i];
263 list->next = hiddev_table[i]->list;
264 hiddev_table[i]->list = list;
266 file->private_data = list;
268 if (!list->hiddev->open++)
269 if (list->hiddev->exist)
270 hid_open(hiddev_table[i]->hid);
278 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
286 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
288 DECLARE_WAITQUEUE(wait, current);
289 struct hiddev_list *list = file->private_data;
293 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
294 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
296 if (count < event_size)
299 while (retval == 0) {
300 if (list->head == list->tail) {
301 add_wait_queue(&list->hiddev->wait, &wait);
302 set_current_state(TASK_INTERRUPTIBLE);
304 while (list->head == list->tail) {
305 if (file->f_flags & O_NONBLOCK) {
309 if (signal_pending(current)) {
310 retval = -ERESTARTSYS;
313 if (!list->hiddev->exist) {
319 set_current_state(TASK_INTERRUPTIBLE);
322 set_current_state(TASK_RUNNING);
323 remove_wait_queue(&list->hiddev->wait, &wait);
330 while (list->head != list->tail &&
331 retval + event_size <= count) {
332 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
333 if (list->buffer[list->tail].field_index !=
334 HID_FIELD_INDEX_NONE) {
335 struct hiddev_event event;
336 event.hid = list->buffer[list->tail].usage_code;
337 event.value = list->buffer[list->tail].value;
338 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
340 retval += sizeof(struct hiddev_event);
343 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
344 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
345 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
347 retval += sizeof(struct hiddev_usage_ref);
350 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
360 * No kernel lock - fine
362 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
364 struct hiddev_list *list = file->private_data;
365 poll_wait(file, &list->hiddev->wait, wait);
366 if (list->head != list->tail)
367 return POLLIN | POLLRDNORM;
368 if (!list->hiddev->exist)
369 return POLLERR | POLLHUP;
376 static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
378 struct hiddev_list *list = file->private_data;
379 struct hiddev *hiddev = list->hiddev;
380 struct hid_device *hid = hiddev->hid;
381 struct usb_device *dev = hid->dev;
382 struct hiddev_collection_info cinfo;
383 struct hiddev_report_info rinfo;
384 struct hiddev_field_info finfo;
385 struct hiddev_usage_ref_multi *uref_multi=NULL;
386 struct hiddev_usage_ref *uref;
387 struct hiddev_devinfo dinfo;
388 struct hid_report *report;
389 struct hid_field *field;
390 void __user *user_arg = (void __user *)arg;
399 return put_user(HID_VERSION, (int __user *)arg);
401 case HIDIOCAPPLICATION:
402 if (arg < 0 || arg >= hid->maxapplication)
405 for (i = 0; i < hid->maxcollection; i++)
406 if (hid->collection[i].type ==
407 HID_COLLECTION_APPLICATION && arg-- == 0)
410 if (i == hid->maxcollection)
413 return hid->collection[i].usage;
416 dinfo.bustype = BUS_USB;
417 dinfo.busnum = dev->bus->busnum;
418 dinfo.devnum = dev->devnum;
419 dinfo.ifnum = hid->ifnum;
420 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
421 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
422 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
423 dinfo.num_applications = hid->maxapplication;
424 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
430 if (put_user(list->flags, (int __user *)arg))
438 if (get_user(newflags, (int __user *)arg))
441 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
442 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
443 (newflags & HIDDEV_FLAG_UREF) == 0))
446 list->flags = newflags;
456 if (get_user(idx, (int __user *)arg))
459 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
462 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
467 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
477 case HIDIOCINITREPORT:
478 hid_init_reports(hid);
483 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
486 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
489 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
492 hid_submit_report(hid, report, USB_DIR_IN);
498 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
501 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
504 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
507 hid_submit_report(hid, report, USB_DIR_OUT);
512 case HIDIOCGREPORTINFO:
513 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
516 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
519 rinfo.num_fields = report->maxfield;
521 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
526 case HIDIOCGFIELDINFO:
527 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
529 rinfo.report_type = finfo.report_type;
530 rinfo.report_id = finfo.report_id;
531 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
534 if (finfo.field_index >= report->maxfield)
537 field = report->field[finfo.field_index];
538 memset(&finfo, 0, sizeof(finfo));
539 finfo.report_type = rinfo.report_type;
540 finfo.report_id = rinfo.report_id;
541 finfo.field_index = field->report_count - 1;
542 finfo.maxusage = field->maxusage;
543 finfo.flags = field->flags;
544 finfo.physical = field->physical;
545 finfo.logical = field->logical;
546 finfo.application = field->application;
547 finfo.logical_minimum = field->logical_minimum;
548 finfo.logical_maximum = field->logical_maximum;
549 finfo.physical_minimum = field->physical_minimum;
550 finfo.physical_maximum = field->physical_maximum;
551 finfo.unit_exponent = field->unit_exponent;
552 finfo.unit = field->unit;
554 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
560 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
563 uref = &uref_multi->uref;
564 if (copy_from_user(uref, user_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(user_arg, uref, sizeof(*uref)))
591 case HIDIOCGCOLLECTIONINDEX:
592 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
595 uref = &uref_multi->uref;
596 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
597 if (copy_from_user(uref_multi, user_arg,
598 sizeof(*uref_multi)))
601 if (copy_from_user(uref, user_arg, sizeof(*uref)))
605 if (cmd != HIDIOCGUSAGE &&
606 cmd != HIDIOCGUSAGES &&
607 uref->report_type == HID_REPORT_TYPE_INPUT)
610 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
611 field = hiddev_lookup_usage(hid, uref);
615 rinfo.report_type = uref->report_type;
616 rinfo.report_id = uref->report_id;
617 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
620 if (uref->field_index >= report->maxfield)
623 field = report->field[uref->field_index];
625 if (cmd == HIDIOCGCOLLECTIONINDEX) {
626 if (uref->usage_index >= field->maxusage)
628 } else if (uref->usage_index >= field->report_count)
631 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
632 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
633 uref->usage_index + uref_multi->num_values > field->report_count))
639 uref->value = field->value[uref->usage_index];
640 if (copy_to_user(user_arg, uref, sizeof(*uref)))
645 field->value[uref->usage_index] = uref->value;
648 case HIDIOCGCOLLECTIONINDEX:
650 return field->usage[uref->usage_index].collection_index;
652 for (i = 0; i < uref_multi->num_values; i++)
653 uref_multi->values[i] =
654 field->value[uref->usage_index + i];
655 if (copy_to_user(user_arg, uref_multi,
656 sizeof(*uref_multi)))
660 for (i = 0; i < uref_multi->num_values; i++)
661 field->value[uref->usage_index + i] =
662 uref_multi->values[i];
676 case HIDIOCGCOLLECTIONINFO:
677 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
680 if (cinfo.index >= hid->maxcollection)
683 cinfo.type = hid->collection[cinfo.index].type;
684 cinfo.usage = hid->collection[cinfo.index].usage;
685 cinfo.level = hid->collection[cinfo.index].level;
687 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
693 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
696 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
700 len = strlen(hid->name) + 1;
701 if (len > _IOC_SIZE(cmd))
702 len = _IOC_SIZE(cmd);
703 return copy_to_user(user_arg, hid->name, len) ?
707 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
711 len = strlen(hid->phys) + 1;
712 if (len > _IOC_SIZE(cmd))
713 len = _IOC_SIZE(cmd);
714 return copy_to_user(user_arg, hid->phys, len) ?
721 static struct file_operations hiddev_fops = {
722 .owner = THIS_MODULE,
724 .write = hiddev_write,
727 .release = hiddev_release,
728 .ioctl = hiddev_ioctl,
729 .fasync = hiddev_fasync,
732 static struct usb_class_driver hiddev_class = {
734 .fops = &hiddev_fops,
735 .minor_base = HIDDEV_MINOR_BASE,
739 * This is where hid.c calls us to connect a hid device to the hiddev driver
741 int hiddev_connect(struct hid_device *hid)
743 struct hiddev *hiddev;
747 for (i = 0; i < hid->maxcollection; i++)
748 if (hid->collection[i].type ==
749 HID_COLLECTION_APPLICATION &&
750 !IS_INPUT_APPLICATION(hid->collection[i].usage))
753 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
756 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
759 retval = usb_register_dev(hid->intf, &hiddev_class);
761 err("Not able to get a minor for this device.");
766 init_waitqueue_head(&hiddev->wait);
768 hiddev_table[hid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
773 hid->minor = hid->intf->minor;
774 hid->hiddev = hiddev;
780 * This is where hid.c calls us to disconnect a hiddev device from the
781 * corresponding hid device (usually because the usb device has disconnected)
783 static struct usb_class_driver hiddev_class;
784 void hiddev_disconnect(struct hid_device *hid)
786 struct hiddev *hiddev = hid->hiddev;
790 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
791 usb_deregister_dev(hiddev->hid->intf, &hiddev_class);
794 hid_close(hiddev->hid);
795 wake_up_interruptible(&hiddev->wait);
801 /* Currently this driver is a USB driver. It's not a conventional one in
802 * the sense that it doesn't probe at the USB level. Instead it waits to
803 * be connected by HID through the hiddev_connect / hiddev_disconnect
804 * routines. The reason to register as a USB device is to gain part of the
805 * minor number space from the USB major.
807 * In theory, should the HID code be generalized to more than one physical
808 * medium (say, IEEE 1384), this driver will probably need to register its
809 * own major number, and in doing so, no longer need to register with USB.
810 * At that point the probe routine and hiddev_driver struct below will no
815 /* We never attach in this manner, and rely on HID to connect us. This
816 * is why there is no disconnect routine defined in the usb_driver either.
818 static int hiddev_usbd_probe(struct usb_interface *intf,
819 const struct usb_device_id *hiddev_info)
825 static /* const */ struct usb_driver hiddev_driver = {
827 .probe = hiddev_usbd_probe,
830 int __init hiddev_init(void)
832 return usb_register(&hiddev_driver);
835 void hiddev_exit(void)
837 usb_deregister(&hiddev_driver);