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>
35 #include <linux/hid.h>
36 #include <linux/hiddev.h>
39 #ifdef CONFIG_USB_DYNAMIC_MINORS
40 #define HIDDEV_MINOR_BASE 0
41 #define HIDDEV_MINORS 256
43 #define HIDDEV_MINOR_BASE 96
44 #define HIDDEV_MINORS 16
46 #define HIDDEV_BUFFER_SIZE 64
51 wait_queue_head_t wait;
52 struct hid_device *hid;
53 struct list_head list;
58 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
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)
88 report_enum = hid->report_enum +
89 (rinfo->report_type - HID_REPORT_TYPE_MIN);
92 case 0: /* Nothing to do -- report_id is already set correctly */
95 case HID_REPORT_ID_FIRST:
96 if (list_empty(&report_enum->report_list))
99 list = report_enum->report_list.next;
100 report = list_entry(list, struct hid_report, list);
101 rinfo->report_id = report->id;
104 case HID_REPORT_ID_NEXT:
105 report = report_enum->report_id_hash[rid];
109 list = report->list.next;
110 if (list == &report_enum->report_list)
113 report = list_entry(list, struct hid_report, list);
114 rinfo->report_id = report->id;
121 return report_enum->report_id_hash[rinfo->report_id];
125 * Perform an exhaustive search of the report table for a usage, given its
128 static struct hid_field *
129 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
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)
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;
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;
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
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;
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;
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));
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);
225 static int hiddev_fasync(int fd, struct file *file, int on)
228 struct hiddev_list *list = file->private_data;
230 retval = fasync_helper(fd, file, on, &list->fasync);
232 return retval < 0 ? retval : 0;
239 static int hiddev_release(struct inode * inode, struct file * file)
241 struct hiddev_list *list = file->private_data;
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);
265 static int hiddev_open(struct inode *inode, struct file *file)
267 struct hiddev_list *list;
270 int i = iminor(inode) - HIDDEV_MINOR_BASE;
272 if (i >= HIDDEV_MINORS || !hiddev_table[i])
275 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
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);
296 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
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;
311 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
312 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
314 if (count < event_size)
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) {
327 if (signal_pending(current)) {
328 retval = -ERESTARTSYS;
331 if (!list->hiddev->exist) {
337 set_current_state(TASK_INTERRUPTIBLE);
340 set_current_state(TASK_RUNNING);
341 remove_wait_queue(&list->hiddev->wait, &wait);
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)))
358 retval += sizeof(struct hiddev_event);
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)))
365 retval += sizeof(struct hiddev_usage_ref);
368 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
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;
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;
419 return put_user(HID_VERSION, (int __user *)arg);
421 case HIDIOCAPPLICATION:
422 if (arg < 0 || arg >= hid->maxapplication)
425 for (i = 0; i < hid->maxcollection; i++)
426 if (hid->collection[i].type ==
427 HID_COLLECTION_APPLICATION && arg-- == 0)
430 if (i == hid->maxcollection)
433 return hid->collection[i].usage;
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)))
450 if (put_user(list->flags, (int __user *)arg))
458 if (get_user(newflags, (int __user *)arg))
461 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
462 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
463 (newflags & HIDDEV_FLAG_UREF) == 0))
466 list->flags = newflags;
476 if (get_user(idx, (int __user *)arg))
479 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
482 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
487 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
497 case HIDIOCINITREPORT:
498 usbhid_init_reports(hid);
503 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
506 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
509 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
512 usbhid_submit_report(hid, report, USB_DIR_IN);
518 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
521 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
524 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
527 usbhid_submit_report(hid, report, USB_DIR_OUT);
532 case HIDIOCGREPORTINFO:
533 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
536 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
539 rinfo.num_fields = report->maxfield;
541 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
546 case HIDIOCGFIELDINFO:
547 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
549 rinfo.report_type = finfo.report_type;
550 rinfo.report_id = finfo.report_id;
551 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
554 if (finfo.field_index >= report->maxfield)
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)))
580 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
583 uref = &uref_multi->uref;
584 if (copy_from_user(uref, user_arg, sizeof(*uref)))
587 rinfo.report_type = uref->report_type;
588 rinfo.report_id = uref->report_id;
589 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
592 if (uref->field_index >= report->maxfield)
595 field = report->field[uref->field_index];
596 if (uref->usage_index >= field->maxusage)
599 uref->usage_code = field->usage[uref->usage_index].hid;
601 if (copy_to_user(user_arg, uref, sizeof(*uref)))
611 case HIDIOCGCOLLECTIONINDEX:
612 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
615 uref = &uref_multi->uref;
616 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
617 if (copy_from_user(uref_multi, user_arg,
618 sizeof(*uref_multi)))
621 if (copy_from_user(uref, user_arg, sizeof(*uref)))
625 if (cmd != HIDIOCGUSAGE &&
626 cmd != HIDIOCGUSAGES &&
627 uref->report_type == HID_REPORT_TYPE_INPUT)
630 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
631 field = hiddev_lookup_usage(hid, uref);
635 rinfo.report_type = uref->report_type;
636 rinfo.report_id = uref->report_id;
637 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
640 if (uref->field_index >= report->maxfield)
643 field = report->field[uref->field_index];
645 if (cmd == HIDIOCGCOLLECTIONINDEX) {
646 if (uref->usage_index >= field->maxusage)
648 } else if (uref->usage_index >= field->report_count)
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))
659 uref->value = field->value[uref->usage_index];
660 if (copy_to_user(user_arg, uref, sizeof(*uref)))
665 field->value[uref->usage_index] = uref->value;
668 case HIDIOCGCOLLECTIONINDEX:
670 return field->usage[uref->usage_index].collection_index;
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)))
680 for (i = 0; i < uref_multi->num_values; i++)
681 field->value[uref->usage_index + i] =
682 uref_multi->values[i];
696 case HIDIOCGCOLLECTIONINFO:
697 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
700 if (cinfo.index >= hid->maxcollection)
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)))
713 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
716 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(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) ?
727 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(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) ?
741 static const struct file_operations hiddev_fops = {
742 .owner = THIS_MODULE,
744 .write = hiddev_write,
747 .release = hiddev_release,
748 .ioctl = hiddev_ioctl,
749 .fasync = hiddev_fasync,
752 static struct usb_class_driver hiddev_class = {
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;
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))
774 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
777 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
780 retval = usb_register_dev(usbhid->intf, &hiddev_class);
782 err("Not able to get a minor for this device.");
787 init_waitqueue_head(&hiddev->wait);
788 INIT_LIST_HEAD(&hiddev->list);
789 spin_lock_init(&hiddev->list_lock);
793 hid->minor = usbhid->intf->minor;
794 hid->hiddev = hiddev;
796 hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
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;
813 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
814 usb_deregister_dev(usbhid->intf, &hiddev_class);
817 usbhid_close(hiddev->hid);
818 wake_up_interruptible(&hiddev->wait);
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
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)
848 static /* const */ struct usb_driver hiddev_driver = {
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);