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/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>
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
44 #define HIDDEV_MINOR_BASE 96
45 #define HIDDEV_MINORS 16
47 #define HIDDEV_BUFFER_SIZE 64
52 wait_queue_head_t wait;
53 struct hid_device *hid;
54 struct hiddev_list *list;
58 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
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);
89 case 0: /* Nothing to do -- report_id is already set correctly */
92 case HID_REPORT_ID_FIRST:
93 list = report_enum->report_list.next;
94 if (list == &report_enum->report_list)
96 rinfo->report_id = ((struct hid_report *) list)->id;
99 case HID_REPORT_ID_NEXT:
100 list = (struct list_head *)
101 report_enum->report_id_hash[rinfo->report_id & HID_REPORT_ID_MASK];
105 if (list == &report_enum->report_list)
107 rinfo->report_id = ((struct hid_report *) list)->id;
114 return report_enum->report_id_hash[rinfo->report_id];
118 * Perform an exhaustive search of the report table for a usage, given its
121 static struct hid_field *
122 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
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;
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;
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);
173 wake_up_interruptible(&hiddev->wait);
177 * This is where hid.c calls into hiddev to pass an event that occurred over
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;
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;
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));
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);
218 static int hiddev_fasync(int fd, struct file *file, int on)
221 struct hiddev_list *list = file->private_data;
222 retval = fasync_helper(fd, file, on, &list->fasync);
223 return retval < 0 ? retval : 0;
227 * De-allocate a hiddev structure
229 static struct usb_class_driver hiddev_class;
230 static void hiddev_cleanup(struct hiddev *hiddev)
232 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
233 usb_deregister_dev(hiddev->hid->intf, &hiddev_class);
240 static int hiddev_release(struct inode * inode, struct file * file)
242 struct hiddev_list *list = file->private_data;
243 struct hiddev_list **listptr;
245 listptr = &list->hiddev->list;
246 hiddev_fasync(-1, file, 0);
248 while (*listptr && (*listptr != list))
249 listptr = &((*listptr)->next);
250 *listptr = (*listptr)->next;
252 if (!--list->hiddev->open) {
253 if (list->hiddev->exist)
254 hid_close(list->hiddev->hid);
256 hiddev_cleanup(list->hiddev);
267 static int hiddev_open(struct inode * inode, struct file * file) {
268 struct hiddev_list *list;
270 int i = iminor(inode) - HIDDEV_MINOR_BASE;
272 if (i >= HIDDEV_MINORS || !hiddev_table[i])
275 if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
277 memset(list, 0, sizeof(struct hiddev_list));
279 list->hiddev = hiddev_table[i];
280 list->next = hiddev_table[i]->list;
281 hiddev_table[i]->list = list;
283 file->private_data = list;
285 if (!list->hiddev->open++)
286 if (list->hiddev->exist)
287 hid_open(hiddev_table[i]->hid);
295 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
303 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
305 DECLARE_WAITQUEUE(wait, current);
306 struct hiddev_list *list = file->private_data;
310 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
311 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
313 if (count < event_size)
316 while (retval == 0) {
317 if (list->head == list->tail) {
318 add_wait_queue(&list->hiddev->wait, &wait);
319 set_current_state(TASK_INTERRUPTIBLE);
321 while (list->head == list->tail) {
322 if (file->f_flags & O_NONBLOCK) {
326 if (signal_pending(current)) {
327 retval = -ERESTARTSYS;
330 if (!list->hiddev->exist) {
338 set_current_state(TASK_RUNNING);
339 remove_wait_queue(&list->hiddev->wait, &wait);
346 while (list->head != list->tail &&
347 retval + event_size <= count) {
348 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
349 if (list->buffer[list->tail].field_index !=
350 HID_FIELD_INDEX_NONE) {
351 struct hiddev_event event;
352 event.hid = list->buffer[list->tail].usage_code;
353 event.value = list->buffer[list->tail].value;
354 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
356 retval += sizeof(struct hiddev_event);
359 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
360 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
361 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
363 retval += sizeof(struct hiddev_usage_ref);
366 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
376 * No kernel lock - fine
378 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
380 struct hiddev_list *list = file->private_data;
381 poll_wait(file, &list->hiddev->wait, wait);
382 if (list->head != list->tail)
383 return POLLIN | POLLRDNORM;
384 if (!list->hiddev->exist)
385 return POLLERR | POLLHUP;
392 static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
394 struct hiddev_list *list = file->private_data;
395 struct hiddev *hiddev = list->hiddev;
396 struct hid_device *hid = hiddev->hid;
397 struct usb_device *dev = hid->dev;
398 struct hiddev_collection_info cinfo;
399 struct hiddev_report_info rinfo;
400 struct hiddev_field_info finfo;
401 struct hiddev_usage_ref_multi *uref_multi=NULL;
402 struct hiddev_usage_ref *uref;
403 struct hiddev_devinfo dinfo;
404 struct hid_report *report;
405 struct hid_field *field;
406 void __user *user_arg = (void __user *)arg;
415 return put_user(HID_VERSION, (int __user *)arg);
417 case HIDIOCAPPLICATION:
418 if (arg < 0 || arg >= hid->maxapplication)
421 for (i = 0; i < hid->maxcollection; i++)
422 if (hid->collection[i].type ==
423 HID_COLLECTION_APPLICATION && arg-- == 0)
426 if (i == hid->maxcollection)
429 return hid->collection[i].usage;
432 dinfo.bustype = BUS_USB;
433 dinfo.busnum = dev->bus->busnum;
434 dinfo.devnum = dev->devnum;
435 dinfo.ifnum = hid->ifnum;
436 dinfo.vendor = dev->descriptor.idVendor;
437 dinfo.product = dev->descriptor.idProduct;
438 dinfo.version = dev->descriptor.bcdDevice;
439 dinfo.num_applications = hid->maxapplication;
440 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
446 if (put_user(list->flags, (int __user *)arg))
454 if (get_user(newflags, (int __user *)arg))
457 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
458 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
459 (newflags & HIDDEV_FLAG_UREF) == 0))
462 list->flags = newflags;
472 if (get_user(idx, (int __user *)arg))
475 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
478 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
483 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
493 case HIDIOCINITREPORT:
494 hid_init_reports(hid);
499 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
502 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
505 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
508 hid_submit_report(hid, report, USB_DIR_IN);
514 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
517 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
520 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
523 hid_submit_report(hid, report, USB_DIR_OUT);
527 case HIDIOCGREPORTINFO:
528 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
531 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
534 rinfo.num_fields = report->maxfield;
536 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
541 case HIDIOCGFIELDINFO:
542 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
544 rinfo.report_type = finfo.report_type;
545 rinfo.report_id = finfo.report_id;
546 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
549 if (finfo.field_index >= report->maxfield)
552 field = report->field[finfo.field_index];
553 memset(&finfo, 0, sizeof(finfo));
554 finfo.report_type = rinfo.report_type;
555 finfo.report_id = rinfo.report_id;
556 finfo.field_index = field->report_count - 1;
557 finfo.maxusage = field->maxusage;
558 finfo.flags = field->flags;
559 finfo.physical = field->physical;
560 finfo.logical = field->logical;
561 finfo.application = field->application;
562 finfo.logical_minimum = field->logical_minimum;
563 finfo.logical_maximum = field->logical_maximum;
564 finfo.physical_minimum = field->physical_minimum;
565 finfo.physical_maximum = field->physical_maximum;
566 finfo.unit_exponent = field->unit_exponent;
567 finfo.unit = field->unit;
569 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
575 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
578 uref = &uref_multi->uref;
579 if (copy_from_user(uref, user_arg, sizeof(*uref)))
582 rinfo.report_type = uref->report_type;
583 rinfo.report_id = uref->report_id;
584 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
587 if (uref->field_index >= report->maxfield)
590 field = report->field[uref->field_index];
591 if (uref->usage_index >= field->maxusage)
594 uref->usage_code = field->usage[uref->usage_index].hid;
596 if (copy_to_user(user_arg, uref, sizeof(*uref)))
606 case HIDIOCGCOLLECTIONINDEX:
607 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
610 uref = &uref_multi->uref;
611 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
612 if (copy_from_user(uref_multi, user_arg,
613 sizeof(*uref_multi)))
616 if (copy_from_user(uref, user_arg, sizeof(*uref)))
620 if (cmd != HIDIOCGUSAGE &&
621 cmd != HIDIOCGUSAGES &&
622 uref->report_type == HID_REPORT_TYPE_INPUT)
625 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
626 field = hiddev_lookup_usage(hid, uref);
630 rinfo.report_type = uref->report_type;
631 rinfo.report_id = uref->report_id;
632 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
635 if (uref->field_index >= report->maxfield)
638 field = report->field[uref->field_index];
639 if (uref->usage_index >= field->maxusage)
642 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
643 if (uref_multi->num_values >= HID_MAX_USAGES ||
644 uref->usage_index >= field->maxusage ||
645 (uref->usage_index + uref_multi->num_values) >= field->maxusage)
652 uref->value = field->value[uref->usage_index];
653 if (copy_to_user(user_arg, uref, sizeof(*uref)))
658 field->value[uref->usage_index] = uref->value;
661 case HIDIOCGCOLLECTIONINDEX:
663 return field->usage[uref->usage_index].collection_index;
665 for (i = 0; i < uref_multi->num_values; i++)
666 uref_multi->values[i] =
667 field->value[uref->usage_index + i];
668 if (copy_to_user(user_arg, uref_multi,
669 sizeof(*uref_multi)))
673 for (i = 0; i < uref_multi->num_values; i++)
674 field->value[uref->usage_index + i] =
675 uref_multi->values[i];
689 case HIDIOCGCOLLECTIONINFO:
690 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
693 if (cinfo.index >= hid->maxcollection)
696 cinfo.type = hid->collection[cinfo.index].type;
697 cinfo.usage = hid->collection[cinfo.index].usage;
698 cinfo.level = hid->collection[cinfo.index].level;
700 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
706 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
709 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
713 len = strlen(hid->name) + 1;
714 if (len > _IOC_SIZE(cmd))
715 len = _IOC_SIZE(cmd);
716 return copy_to_user(user_arg, hid->name, len) ?
720 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
724 len = strlen(hid->phys) + 1;
725 if (len > _IOC_SIZE(cmd))
726 len = _IOC_SIZE(cmd);
727 return copy_to_user(user_arg, hid->phys, len) ?
734 static struct file_operations hiddev_fops = {
735 .owner = THIS_MODULE,
737 .write = hiddev_write,
740 .release = hiddev_release,
741 .ioctl = hiddev_ioctl,
742 .fasync = hiddev_fasync,
745 static struct usb_class_driver hiddev_class = {
746 .name = "usb/hid/hiddev%d",
747 .fops = &hiddev_fops,
748 .mode = S_IFCHR | S_IRUGO | S_IWUSR,
749 .minor_base = HIDDEV_MINOR_BASE,
753 * This is where hid.c calls us to connect a hid device to the hiddev driver
755 int hiddev_connect(struct hid_device *hid)
757 struct hiddev *hiddev;
761 for (i = 0; i < hid->maxcollection; i++)
762 if (hid->collection[i].type ==
763 HID_COLLECTION_APPLICATION &&
764 !IS_INPUT_APPLICATION(hid->collection[i].usage))
767 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
770 if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
772 memset(hiddev, 0, sizeof(struct hiddev));
774 retval = usb_register_dev(hid->intf, &hiddev_class);
776 err("Not able to get a minor for this device.");
781 init_waitqueue_head(&hiddev->wait);
783 hiddev_table[hid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
788 hid->minor = hid->intf->minor;
789 hid->hiddev = hiddev;
795 * This is where hid.c calls us to disconnect a hiddev device from the
796 * corresponding hid device (usually because the usb device has disconnected)
798 void hiddev_disconnect(struct hid_device *hid)
800 struct hiddev *hiddev = hid->hiddev;
805 hid_close(hiddev->hid);
806 wake_up_interruptible(&hiddev->wait);
808 hiddev_cleanup(hiddev);
812 /* Currently this driver is a USB driver. It's not a conventional one in
813 * the sense that it doesn't probe at the USB level. Instead it waits to
814 * be connected by HID through the hiddev_connect / hiddev_disconnect
815 * routines. The reason to register as a USB device is to gain part of the
816 * minor number space from the USB major.
818 * In theory, should the HID code be generalized to more than one physical
819 * medium (say, IEEE 1384), this driver will probably need to register its
820 * own major number, and in doing so, no longer need to register with USB.
821 * At that point the probe routine and hiddev_driver struct below will no
826 /* We never attach in this manner, and rely on HID to connect us. This
827 * is why there is no disconnect routine defined in the usb_driver either.
829 static int hiddev_usbd_probe(struct usb_interface *intf,
830 const struct usb_device_id *hiddev_info)
836 static /* const */ struct usb_driver hiddev_driver = {
837 .owner = THIS_MODULE,
839 .probe = hiddev_usbd_probe,
842 int __init hiddev_init(void)
844 devfs_mk_dir("usb/hid");
845 return usb_register(&hiddev_driver);
848 void hiddev_exit(void)
850 usb_deregister(&hiddev_driver);
851 devfs_remove("usb/hid");