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/input.h>
33 #include <linux/usb.h>
34 #include <linux/hid.h>
35 #include <linux/hiddev.h>
36 #include <linux/compat.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 2048
51 struct mutex existancelock;
52 wait_queue_head_t wait;
53 struct hid_device *hid;
54 struct list_head list;
59 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
63 struct fasync_struct *fasync;
64 struct hiddev *hiddev;
65 struct list_head node;
66 struct mutex thread_lock;
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)
227 struct hiddev_list *list = file->private_data;
229 return fasync_helper(fd, file, on, &list->fasync);
236 static int hiddev_release(struct inode * inode, struct file * file)
238 struct hiddev_list *list = file->private_data;
241 spin_lock_irqsave(&list->hiddev->list_lock, flags);
242 list_del(&list->node);
243 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
245 mutex_lock(&list->hiddev->existancelock);
246 if (!--list->hiddev->open) {
247 if (list->hiddev->exist) {
248 usbhid_close(list->hiddev->hid);
249 usbhid_put_power(list->hiddev->hid);
256 mutex_unlock(&list->hiddev->existancelock);
264 static int hiddev_open(struct inode *inode, struct file *file)
266 struct hiddev_list *list;
267 struct usb_interface *intf;
268 struct hid_device *hid;
269 struct hiddev *hiddev;
272 intf = usbhid_find_interface(iminor(inode));
275 hid = usb_get_intfdata(intf);
276 hiddev = hid->hiddev;
278 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
280 mutex_init(&list->thread_lock);
281 list->hiddev = hiddev;
282 file->private_data = list;
285 * no need for locking because the USB major number
286 * is shared which usbcore guards against disconnect
288 if (list->hiddev->exist) {
289 if (!list->hiddev->open++) {
290 res = usbhid_open(hiddev->hid);
301 spin_lock_irq(&list->hiddev->list_lock);
302 list_add_tail(&list->node, &hiddev->list);
303 spin_unlock_irq(&list->hiddev->list_lock);
305 mutex_lock(&hiddev->existancelock);
306 if (!list->hiddev->open++)
307 if (list->hiddev->exist) {
308 struct hid_device *hid = hiddev->hid;
309 res = usbhid_get_power(hid);
316 mutex_unlock(&hiddev->existancelock);
319 mutex_unlock(&hiddev->existancelock);
321 file->private_data = NULL;
329 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
337 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
340 struct hiddev_list *list = file->private_data;
344 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
345 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
347 if (count < event_size)
350 /* lock against other threads */
351 retval = mutex_lock_interruptible(&list->thread_lock);
355 while (retval == 0) {
356 if (list->head == list->tail) {
357 prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
359 while (list->head == list->tail) {
360 if (file->f_flags & O_NONBLOCK) {
364 if (signal_pending(current)) {
365 retval = -ERESTARTSYS;
368 if (!list->hiddev->exist) {
373 /* let O_NONBLOCK tasks run */
374 mutex_unlock(&list->thread_lock);
376 if (mutex_lock_interruptible(&list->thread_lock)) {
377 finish_wait(&list->hiddev->wait, &wait);
380 set_current_state(TASK_INTERRUPTIBLE);
382 finish_wait(&list->hiddev->wait, &wait);
387 mutex_unlock(&list->thread_lock);
392 while (list->head != list->tail &&
393 retval + event_size <= count) {
394 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
395 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
396 struct hiddev_event event;
398 event.hid = list->buffer[list->tail].usage_code;
399 event.value = list->buffer[list->tail].value;
400 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
401 mutex_unlock(&list->thread_lock);
404 retval += sizeof(struct hiddev_event);
407 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
408 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
410 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
411 mutex_unlock(&list->thread_lock);
414 retval += sizeof(struct hiddev_usage_ref);
417 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
421 mutex_unlock(&list->thread_lock);
428 * No kernel lock - fine
430 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
432 struct hiddev_list *list = file->private_data;
434 poll_wait(file, &list->hiddev->wait, wait);
435 if (list->head != list->tail)
436 return POLLIN | POLLRDNORM;
437 if (!list->hiddev->exist)
438 return POLLERR | POLLHUP;
445 static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
447 struct hid_device *hid = hiddev->hid;
448 struct hiddev_report_info rinfo;
449 struct hiddev_usage_ref_multi *uref_multi = NULL;
450 struct hiddev_usage_ref *uref;
451 struct hid_report *report;
452 struct hid_field *field;
455 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
458 uref = &uref_multi->uref;
459 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
460 if (copy_from_user(uref_multi, user_arg,
461 sizeof(*uref_multi)))
464 if (copy_from_user(uref, user_arg, sizeof(*uref)))
470 rinfo.report_type = uref->report_type;
471 rinfo.report_id = uref->report_id;
472 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
475 if (uref->field_index >= report->maxfield)
478 field = report->field[uref->field_index];
479 if (uref->usage_index >= field->maxusage)
482 uref->usage_code = field->usage[uref->usage_index].hid;
484 if (copy_to_user(user_arg, uref, sizeof(*uref)))
490 if (cmd != HIDIOCGUSAGE &&
491 cmd != HIDIOCGUSAGES &&
492 uref->report_type == HID_REPORT_TYPE_INPUT)
495 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
496 field = hiddev_lookup_usage(hid, uref);
500 rinfo.report_type = uref->report_type;
501 rinfo.report_id = uref->report_id;
502 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
505 if (uref->field_index >= report->maxfield)
508 field = report->field[uref->field_index];
510 if (cmd == HIDIOCGCOLLECTIONINDEX) {
511 if (uref->usage_index >= field->maxusage)
513 } else if (uref->usage_index >= field->report_count)
516 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
517 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
518 uref->usage_index + uref_multi->num_values > field->report_count))
524 uref->value = field->value[uref->usage_index];
525 if (copy_to_user(user_arg, uref, sizeof(*uref)))
530 field->value[uref->usage_index] = uref->value;
533 case HIDIOCGCOLLECTIONINDEX:
534 i = field->usage[uref->usage_index].collection_index;
538 for (i = 0; i < uref_multi->num_values; i++)
539 uref_multi->values[i] =
540 field->value[uref->usage_index + i];
541 if (copy_to_user(user_arg, uref_multi,
542 sizeof(*uref_multi)))
546 for (i = 0; i < uref_multi->num_values; i++)
547 field->value[uref->usage_index + i] =
548 uref_multi->values[i];
564 static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
566 struct hid_device *hid = hiddev->hid;
567 struct usb_device *dev = hid_to_usb_dev(hid);
571 if (get_user(idx, (int __user *)user_arg))
574 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
577 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
582 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
592 static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
594 struct hiddev_list *list = file->private_data;
595 struct hiddev *hiddev = list->hiddev;
596 struct hid_device *hid;
597 struct hiddev_collection_info cinfo;
598 struct hiddev_report_info rinfo;
599 struct hiddev_field_info finfo;
600 struct hiddev_devinfo dinfo;
601 struct hid_report *report;
602 struct hid_field *field;
603 void __user *user_arg = (void __user *)arg;
606 /* Called without BKL by compat methods so no BKL taken */
608 mutex_lock(&hiddev->existancelock);
609 if (!hiddev->exist) {
619 r = put_user(HID_VERSION, (int __user *)arg) ?
623 case HIDIOCAPPLICATION:
624 if (arg < 0 || arg >= hid->maxapplication)
627 for (i = 0; i < hid->maxcollection; i++)
628 if (hid->collection[i].type ==
629 HID_COLLECTION_APPLICATION && arg-- == 0)
632 if (i < hid->maxcollection)
633 r = hid->collection[i].usage;
638 struct usb_device *dev = hid_to_usb_dev(hid);
639 struct usbhid_device *usbhid = hid->driver_data;
641 dinfo.bustype = BUS_USB;
642 dinfo.busnum = dev->bus->busnum;
643 dinfo.devnum = dev->devnum;
644 dinfo.ifnum = usbhid->ifnum;
645 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
646 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
647 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
648 dinfo.num_applications = hid->maxapplication;
650 r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
656 r = put_user(list->flags, (int __user *)arg) ?
664 if (get_user(newflags, (int __user *)arg)) {
669 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
670 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
671 (newflags & HIDDEV_FLAG_UREF) == 0))
674 list->flags = newflags;
681 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
684 case HIDIOCINITREPORT:
685 usbhid_init_reports(hid);
690 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
695 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
698 report = hiddev_lookup_report(hid, &rinfo);
702 usbhid_submit_report(hid, report, USB_DIR_IN);
709 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
714 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
717 report = hiddev_lookup_report(hid, &rinfo);
721 usbhid_submit_report(hid, report, USB_DIR_OUT);
727 case HIDIOCGREPORTINFO:
728 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
733 report = hiddev_lookup_report(hid, &rinfo);
737 rinfo.num_fields = report->maxfield;
739 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
743 case HIDIOCGFIELDINFO:
744 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
749 rinfo.report_type = finfo.report_type;
750 rinfo.report_id = finfo.report_id;
752 report = hiddev_lookup_report(hid, &rinfo);
756 if (finfo.field_index >= report->maxfield)
759 field = report->field[finfo.field_index];
760 memset(&finfo, 0, sizeof(finfo));
761 finfo.report_type = rinfo.report_type;
762 finfo.report_id = rinfo.report_id;
763 finfo.field_index = field->report_count - 1;
764 finfo.maxusage = field->maxusage;
765 finfo.flags = field->flags;
766 finfo.physical = field->physical;
767 finfo.logical = field->logical;
768 finfo.application = field->application;
769 finfo.logical_minimum = field->logical_minimum;
770 finfo.logical_maximum = field->logical_maximum;
771 finfo.physical_minimum = field->physical_minimum;
772 finfo.physical_maximum = field->physical_maximum;
773 finfo.unit_exponent = field->unit_exponent;
774 finfo.unit = field->unit;
776 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
786 case HIDIOCGCOLLECTIONINDEX:
787 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
790 case HIDIOCGCOLLECTIONINFO:
791 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
796 if (cinfo.index >= hid->maxcollection)
799 cinfo.type = hid->collection[cinfo.index].type;
800 cinfo.usage = hid->collection[cinfo.index].usage;
801 cinfo.level = hid->collection[cinfo.index].level;
803 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
808 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
811 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
812 int len = strlen(hid->name) + 1;
813 if (len > _IOC_SIZE(cmd))
814 len = _IOC_SIZE(cmd);
815 r = copy_to_user(user_arg, hid->name, len) ?
820 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
821 int len = strlen(hid->phys) + 1;
822 if (len > _IOC_SIZE(cmd))
823 len = _IOC_SIZE(cmd);
824 r = copy_to_user(user_arg, hid->phys, len) ?
831 mutex_unlock(&hiddev->existancelock);
836 static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
838 return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
842 static const struct file_operations hiddev_fops = {
843 .owner = THIS_MODULE,
845 .write = hiddev_write,
848 .release = hiddev_release,
849 .unlocked_ioctl = hiddev_ioctl,
850 .fasync = hiddev_fasync,
852 .compat_ioctl = hiddev_compat_ioctl,
854 .llseek = noop_llseek,
857 static char *hiddev_devnode(struct device *dev, mode_t *mode)
859 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
862 static struct usb_class_driver hiddev_class = {
864 .devnode = hiddev_devnode,
865 .fops = &hiddev_fops,
866 .minor_base = HIDDEV_MINOR_BASE,
870 * This is where hid.c calls us to connect a hid device to the hiddev driver
872 int hiddev_connect(struct hid_device *hid, unsigned int force)
874 struct hiddev *hiddev;
875 struct usbhid_device *usbhid = hid->driver_data;
880 for (i = 0; i < hid->maxcollection; i++)
881 if (hid->collection[i].type ==
882 HID_COLLECTION_APPLICATION &&
883 !IS_INPUT_APPLICATION(hid->collection[i].usage))
886 if (i == hid->maxcollection)
890 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
893 init_waitqueue_head(&hiddev->wait);
894 INIT_LIST_HEAD(&hiddev->list);
895 spin_lock_init(&hiddev->list_lock);
896 mutex_init(&hiddev->existancelock);
897 hid->hiddev = hiddev;
900 retval = usb_register_dev(usbhid->intf, &hiddev_class);
902 hid_err(hid, "Not able to get a minor for this device\n");
911 * This is where hid.c calls us to disconnect a hiddev device from the
912 * corresponding hid device (usually because the usb device has disconnected)
914 static struct usb_class_driver hiddev_class;
915 void hiddev_disconnect(struct hid_device *hid)
917 struct hiddev *hiddev = hid->hiddev;
918 struct usbhid_device *usbhid = hid->driver_data;
920 mutex_lock(&hiddev->existancelock);
923 usb_deregister_dev(usbhid->intf, &hiddev_class);
926 usbhid_close(hiddev->hid);
927 wake_up_interruptible(&hiddev->wait);
931 mutex_unlock(&hiddev->existancelock);