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];
69 /* forward reference to make our lives easier */
70 extern struct usb_driver hiddev_driver;
73 * Find a report, given the report's type and ID. The ID can be specified
74 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
75 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
76 * given type which follows old_id.
78 static struct hid_report *
79 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
81 unsigned flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
82 struct hid_report_enum *report_enum;
83 struct list_head *list;
85 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
86 rinfo->report_type > HID_REPORT_TYPE_MAX) return NULL;
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 list = report_enum->report_list.next;
97 if (list == &report_enum->report_list)
99 rinfo->report_id = ((struct hid_report *) list)->id;
102 case HID_REPORT_ID_NEXT:
103 list = (struct list_head *)
104 report_enum->report_id_hash[rinfo->report_id & HID_REPORT_ID_MASK];
108 if (list == &report_enum->report_list)
110 rinfo->report_id = ((struct hid_report *) list)->id;
117 return report_enum->report_id_hash[rinfo->report_id];
121 * Perform an exhaustive search of the report table for a usage, given its
124 static struct hid_field *
125 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
128 struct hid_report *report;
129 struct hid_report_enum *report_enum;
130 struct list_head *list;
131 struct hid_field *field;
133 if (uref->report_type < HID_REPORT_TYPE_MIN ||
134 uref->report_type > HID_REPORT_TYPE_MAX) return NULL;
136 report_enum = hid->report_enum +
137 (uref->report_type - HID_REPORT_TYPE_MIN);
138 list = report_enum->report_list.next;
139 while (list != &report_enum->report_list) {
140 report = (struct hid_report *) list;
141 for (i = 0; i < report->maxfield; i++) {
142 field = report->field[i];
143 for (j = 0; j < field->maxusage; j++) {
144 if (field->usage[j].hid == uref->usage_code) {
145 uref->report_id = report->id;
146 uref->field_index = i;
147 uref->usage_index = j;
158 static void hiddev_send_event(struct hid_device *hid,
159 struct hiddev_usage_ref *uref)
161 struct hiddev *hiddev = hid->hiddev;
162 struct hiddev_list *list = hiddev->list;
165 if (uref->field_index != HID_FIELD_INDEX_NONE ||
166 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
167 list->buffer[list->head] = *uref;
168 list->head = (list->head + 1) &
169 (HIDDEV_BUFFER_SIZE - 1);
170 kill_fasync(&list->fasync, SIGIO, POLL_IN);
176 wake_up_interruptible(&hiddev->wait);
180 * This is where hid.c calls into hiddev to pass an event that occurred over
183 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
184 struct hid_usage *usage, __s32 value, struct pt_regs *regs)
186 unsigned type = field->report_type;
187 struct hiddev_usage_ref uref;
190 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
191 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
192 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
193 uref.report_id = field->report->id;
194 uref.field_index = field->index;
195 uref.usage_index = (usage - field->usage);
196 uref.usage_code = usage->hid;
199 hiddev_send_event(hid, &uref);
203 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
205 unsigned type = report->type;
206 struct hiddev_usage_ref uref;
208 memset(&uref, 0, sizeof(uref));
210 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
211 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
212 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
213 uref.report_id = report->id;
214 uref.field_index = HID_FIELD_INDEX_NONE;
216 hiddev_send_event(hid, &uref);
221 static int hiddev_fasync(int fd, struct file *file, int on)
224 struct hiddev_list *list = file->private_data;
225 retval = fasync_helper(fd, file, on, &list->fasync);
226 return retval < 0 ? retval : 0;
230 * De-allocate a hiddev structure
232 static struct usb_class_driver hiddev_class;
233 static void hiddev_cleanup(struct hiddev *hiddev)
235 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
236 usb_deregister_dev(hiddev->hid->intf, &hiddev_class);
243 static int hiddev_release(struct inode * inode, struct file * file)
245 struct hiddev_list *list = file->private_data;
246 struct hiddev_list **listptr;
248 listptr = &list->hiddev->list;
249 hiddev_fasync(-1, file, 0);
251 while (*listptr && (*listptr != list))
252 listptr = &((*listptr)->next);
253 *listptr = (*listptr)->next;
255 if (!--list->hiddev->open) {
256 if (list->hiddev->exist)
257 hid_close(list->hiddev->hid);
259 hiddev_cleanup(list->hiddev);
270 static int hiddev_open(struct inode * inode, struct file * file) {
271 struct hiddev_list *list;
273 int i = iminor(inode) - HIDDEV_MINOR_BASE;
275 if (i >= HIDDEV_MINORS || !hiddev_table[i])
278 if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
280 memset(list, 0, sizeof(struct hiddev_list));
282 list->hiddev = hiddev_table[i];
283 list->next = hiddev_table[i]->list;
284 hiddev_table[i]->list = list;
286 file->private_data = list;
288 if (!list->hiddev->open++)
289 if (list->hiddev->exist)
290 hid_open(hiddev_table[i]->hid);
298 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
306 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
308 DECLARE_WAITQUEUE(wait, current);
309 struct hiddev_list *list = file->private_data;
313 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
314 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
316 if (count < event_size)
319 while (retval == 0) {
320 if (list->head == list->tail) {
321 add_wait_queue(&list->hiddev->wait, &wait);
322 set_current_state(TASK_INTERRUPTIBLE);
324 while (list->head == list->tail) {
325 if (file->f_flags & O_NONBLOCK) {
329 if (signal_pending(current)) {
330 retval = -ERESTARTSYS;
333 if (!list->hiddev->exist) {
341 set_current_state(TASK_RUNNING);
342 remove_wait_queue(&list->hiddev->wait, &wait);
349 while (list->head != list->tail &&
350 retval + event_size <= count) {
351 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
352 if (list->buffer[list->tail].field_index !=
353 HID_FIELD_INDEX_NONE) {
354 struct hiddev_event event;
355 event.hid = list->buffer[list->tail].usage_code;
356 event.value = list->buffer[list->tail].value;
357 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
359 retval += sizeof(struct hiddev_event);
362 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
363 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
364 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
366 retval += sizeof(struct hiddev_usage_ref);
369 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
379 * No kernel lock - fine
381 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
383 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->dev;
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 void __user *user_arg = (void __user *)arg;
418 return put_user(HID_VERSION, (int __user *)arg);
420 case HIDIOCAPPLICATION:
421 if (arg < 0 || arg >= hid->maxapplication)
424 for (i = 0; i < hid->maxcollection; i++)
425 if (hid->collection[i].type ==
426 HID_COLLECTION_APPLICATION && arg-- == 0)
429 if (i == hid->maxcollection)
432 return hid->collection[i].usage;
435 dinfo.bustype = BUS_USB;
436 dinfo.busnum = dev->bus->busnum;
437 dinfo.devnum = dev->devnum;
438 dinfo.ifnum = hid->ifnum;
439 dinfo.vendor = dev->descriptor.idVendor;
440 dinfo.product = dev->descriptor.idProduct;
441 dinfo.version = dev->descriptor.bcdDevice;
442 dinfo.num_applications = hid->maxapplication;
443 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
449 if (put_user(list->flags, (int __user *)arg))
457 if (get_user(newflags, (int __user *)arg))
460 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
461 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
462 (newflags & HIDDEV_FLAG_UREF) == 0))
465 list->flags = newflags;
475 if (get_user(idx, (int __user *)arg))
478 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
481 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
486 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
496 case HIDIOCINITREPORT:
497 hid_init_reports(hid);
502 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
505 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
508 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
511 hid_submit_report(hid, report, USB_DIR_IN);
517 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
520 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
523 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
526 hid_submit_report(hid, report, USB_DIR_OUT);
530 case HIDIOCGREPORTINFO:
531 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
534 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
537 rinfo.num_fields = report->maxfield;
539 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
544 case HIDIOCGFIELDINFO:
545 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
547 rinfo.report_type = finfo.report_type;
548 rinfo.report_id = finfo.report_id;
549 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
552 if (finfo.field_index >= report->maxfield)
555 field = report->field[finfo.field_index];
556 memset(&finfo, 0, sizeof(finfo));
557 finfo.report_type = rinfo.report_type;
558 finfo.report_id = rinfo.report_id;
559 finfo.field_index = field->report_count - 1;
560 finfo.maxusage = field->maxusage;
561 finfo.flags = field->flags;
562 finfo.physical = field->physical;
563 finfo.logical = field->logical;
564 finfo.application = field->application;
565 finfo.logical_minimum = field->logical_minimum;
566 finfo.logical_maximum = field->logical_maximum;
567 finfo.physical_minimum = field->physical_minimum;
568 finfo.physical_maximum = field->physical_maximum;
569 finfo.unit_exponent = field->unit_exponent;
570 finfo.unit = field->unit;
572 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
578 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
581 uref = &uref_multi->uref;
582 if (copy_from_user(uref, user_arg, sizeof(*uref)))
585 rinfo.report_type = uref->report_type;
586 rinfo.report_id = uref->report_id;
587 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
590 if (uref->field_index >= report->maxfield)
593 field = report->field[uref->field_index];
594 if (uref->usage_index >= field->maxusage)
597 uref->usage_code = field->usage[uref->usage_index].hid;
599 if (copy_to_user(user_arg, uref, sizeof(*uref)))
609 case HIDIOCGCOLLECTIONINDEX:
610 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
613 uref = &uref_multi->uref;
614 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
615 if (copy_from_user(uref_multi, user_arg,
616 sizeof(*uref_multi)))
619 if (copy_from_user(uref, user_arg, sizeof(*uref)))
623 if (cmd != HIDIOCGUSAGE &&
624 cmd != HIDIOCGUSAGES &&
625 uref->report_type == HID_REPORT_TYPE_INPUT)
628 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
629 field = hiddev_lookup_usage(hid, uref);
633 rinfo.report_type = uref->report_type;
634 rinfo.report_id = uref->report_id;
635 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
638 if (uref->field_index >= report->maxfield)
641 field = report->field[uref->field_index];
642 if (uref->usage_index >= field->maxusage)
645 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
646 if (uref_multi->num_values >= HID_MAX_USAGES ||
647 uref->usage_index >= field->maxusage ||
648 (uref->usage_index + uref_multi->num_values) >= field->maxusage)
655 uref->value = field->value[uref->usage_index];
656 if (copy_to_user(user_arg, uref, sizeof(*uref)))
661 field->value[uref->usage_index] = uref->value;
664 case HIDIOCGCOLLECTIONINDEX:
666 return field->usage[uref->usage_index].collection_index;
668 for (i = 0; i < uref_multi->num_values; i++)
669 uref_multi->values[i] =
670 field->value[uref->usage_index + i];
671 if (copy_to_user(user_arg, uref_multi,
672 sizeof(*uref_multi)))
676 for (i = 0; i < uref_multi->num_values; i++)
677 field->value[uref->usage_index + i] =
678 uref_multi->values[i];
692 case HIDIOCGCOLLECTIONINFO:
693 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
696 if (cinfo.index >= hid->maxcollection)
699 cinfo.type = hid->collection[cinfo.index].type;
700 cinfo.usage = hid->collection[cinfo.index].usage;
701 cinfo.level = hid->collection[cinfo.index].level;
703 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
709 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
712 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
716 len = strlen(hid->name) + 1;
717 if (len > _IOC_SIZE(cmd))
718 len = _IOC_SIZE(cmd);
719 return copy_to_user(user_arg, hid->name, len) ?
723 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
727 len = strlen(hid->phys) + 1;
728 if (len > _IOC_SIZE(cmd))
729 len = _IOC_SIZE(cmd);
730 return copy_to_user(user_arg, hid->phys, len) ?
737 static struct file_operations hiddev_fops = {
738 .owner = THIS_MODULE,
740 .write = hiddev_write,
743 .release = hiddev_release,
744 .ioctl = hiddev_ioctl,
745 .fasync = hiddev_fasync,
748 static struct usb_class_driver hiddev_class = {
749 .name = "usb/hid/hiddev%d",
750 .fops = &hiddev_fops,
751 .mode = S_IFCHR | S_IRUGO | S_IWUSR,
752 .minor_base = HIDDEV_MINOR_BASE,
756 * This is where hid.c calls us to connect a hid device to the hiddev driver
758 int hiddev_connect(struct hid_device *hid)
760 struct hiddev *hiddev;
764 for (i = 0; i < hid->maxcollection; i++)
765 if (hid->collection[i].type ==
766 HID_COLLECTION_APPLICATION &&
767 !IS_INPUT_APPLICATION(hid->collection[i].usage))
770 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
773 if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
775 memset(hiddev, 0, sizeof(struct hiddev));
777 retval = usb_register_dev(hid->intf, &hiddev_class);
779 err("Not able to get a minor for this device.");
784 init_waitqueue_head(&hiddev->wait);
786 hiddev_table[hid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
791 hid->minor = hid->intf->minor;
792 hid->hiddev = hiddev;
798 * This is where hid.c calls us to disconnect a hiddev device from the
799 * corresponding hid device (usually because the usb device has disconnected)
801 void hiddev_disconnect(struct hid_device *hid)
803 struct hiddev *hiddev = hid->hiddev;
808 hid_close(hiddev->hid);
809 wake_up_interruptible(&hiddev->wait);
811 hiddev_cleanup(hiddev);
815 /* Currently this driver is a USB driver. It's not a conventional one in
816 * the sense that it doesn't probe at the USB level. Instead it waits to
817 * be connected by HID through the hiddev_connect / hiddev_disconnect
818 * routines. The reason to register as a USB device is to gain part of the
819 * minor number space from the USB major.
821 * In theory, should the HID code be generalized to more than one physical
822 * medium (say, IEEE 1384), this driver will probably need to register its
823 * own major number, and in doing so, no longer need to register with USB.
824 * At that point the probe routine and hiddev_driver struct below will no
829 /* We never attach in this manner, and rely on HID to connect us. This
830 * is why there is no disconnect routine defined in the usb_driver either.
832 static int hiddev_usbd_probe(struct usb_interface *intf,
833 const struct usb_device_id *hiddev_info)
839 static /* const */ struct usb_driver hiddev_driver = {
840 .owner = THIS_MODULE,
842 .probe = hiddev_usbd_probe,
845 int __init hiddev_init(void)
847 devfs_mk_dir("usb/hid");
848 return usb_register(&hiddev_driver);
851 void hiddev_exit(void)
853 usb_deregister(&hiddev_driver);
854 devfs_remove("usb/hid");