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 * buffer, size_t count, loff_t *ppos)
306 static ssize_t hiddev_read(struct file * file, char * 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;
417 return put_user(HID_VERSION, (int *) arg);
419 case HIDIOCAPPLICATION:
420 if (arg < 0 || arg >= hid->maxapplication)
423 for (i = 0; i < hid->maxcollection; i++)
424 if (hid->collection[i].type ==
425 HID_COLLECTION_APPLICATION && arg-- == 0)
428 if (i == hid->maxcollection)
431 return hid->collection[i].usage;
434 dinfo.bustype = BUS_USB;
435 dinfo.busnum = dev->bus->busnum;
436 dinfo.devnum = dev->devnum;
437 dinfo.ifnum = hid->ifnum;
438 dinfo.vendor = dev->descriptor.idVendor;
439 dinfo.product = dev->descriptor.idProduct;
440 dinfo.version = dev->descriptor.bcdDevice;
441 dinfo.num_applications = hid->maxapplication;
442 if (copy_to_user((void *) arg, &dinfo, sizeof(dinfo)))
448 if (put_user(list->flags, (int *) arg))
456 if (get_user(newflags, (int *) arg))
459 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
460 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
461 (newflags & HIDDEV_FLAG_UREF) == 0))
464 list->flags = newflags;
474 if (get_user(idx, (int *) arg))
477 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
480 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
485 if (copy_to_user((void *) (arg+sizeof(int)), buf, len+1)) {
495 case HIDIOCINITREPORT:
496 hid_init_reports(hid);
501 if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
504 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
507 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
510 hid_submit_report(hid, report, USB_DIR_IN);
516 if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
519 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
522 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
525 hid_submit_report(hid, report, USB_DIR_OUT);
529 case HIDIOCGREPORTINFO:
530 if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
533 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
536 rinfo.num_fields = report->maxfield;
538 if (copy_to_user((void *) arg, &rinfo, sizeof(rinfo)))
543 case HIDIOCGFIELDINFO:
544 if (copy_from_user(&finfo, (void *) arg, sizeof(finfo)))
546 rinfo.report_type = finfo.report_type;
547 rinfo.report_id = finfo.report_id;
548 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
551 if (finfo.field_index >= report->maxfield)
554 field = report->field[finfo.field_index];
555 memset(&finfo, 0, sizeof(finfo));
556 finfo.report_type = rinfo.report_type;
557 finfo.report_id = rinfo.report_id;
558 finfo.field_index = field->report_count - 1;
559 finfo.maxusage = field->maxusage;
560 finfo.flags = field->flags;
561 finfo.physical = field->physical;
562 finfo.logical = field->logical;
563 finfo.application = field->application;
564 finfo.logical_minimum = field->logical_minimum;
565 finfo.logical_maximum = field->logical_maximum;
566 finfo.physical_minimum = field->physical_minimum;
567 finfo.physical_maximum = field->physical_maximum;
568 finfo.unit_exponent = field->unit_exponent;
569 finfo.unit = field->unit;
571 if (copy_to_user((void *) arg, &finfo, sizeof(finfo)))
577 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
580 uref = &uref_multi->uref;
581 if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
584 rinfo.report_type = uref->report_type;
585 rinfo.report_id = uref->report_id;
586 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
589 if (uref->field_index >= report->maxfield)
592 field = report->field[uref->field_index];
593 if (uref->usage_index >= field->maxusage)
596 uref->usage_code = field->usage[uref->usage_index].hid;
598 if (copy_to_user((void *) arg, uref, sizeof(*uref)))
608 case HIDIOCGCOLLECTIONINDEX:
609 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
612 uref = &uref_multi->uref;
613 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
614 if (copy_from_user(uref_multi, (void *) arg,
615 sizeof(*uref_multi)))
618 if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
622 if (cmd != HIDIOCGUSAGE &&
623 cmd != HIDIOCGUSAGES &&
624 uref->report_type == HID_REPORT_TYPE_INPUT)
627 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
628 field = hiddev_lookup_usage(hid, uref);
632 rinfo.report_type = uref->report_type;
633 rinfo.report_id = uref->report_id;
634 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
637 if (uref->field_index >= report->maxfield)
640 field = report->field[uref->field_index];
641 if (uref->usage_index >= field->maxusage)
644 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
645 if (uref_multi->num_values >= HID_MAX_USAGES ||
646 uref->usage_index >= field->maxusage ||
647 (uref->usage_index + uref_multi->num_values) >= field->maxusage)
654 uref->value = field->value[uref->usage_index];
655 if (copy_to_user((void *) arg, uref, sizeof(*uref)))
660 field->value[uref->usage_index] = uref->value;
663 case HIDIOCGCOLLECTIONINDEX:
665 return field->usage[uref->usage_index].collection_index;
667 for (i = 0; i < uref_multi->num_values; i++)
668 uref_multi->values[i] =
669 field->value[uref->usage_index + i];
670 if (copy_to_user((void *) arg, uref_multi,
671 sizeof(*uref_multi)))
675 for (i = 0; i < uref_multi->num_values; i++)
676 field->value[uref->usage_index + i] =
677 uref_multi->values[i];
691 case HIDIOCGCOLLECTIONINFO:
692 if (copy_from_user(&cinfo, (void *) arg, sizeof(cinfo)))
695 if (cinfo.index >= hid->maxcollection)
698 cinfo.type = hid->collection[cinfo.index].type;
699 cinfo.usage = hid->collection[cinfo.index].usage;
700 cinfo.level = hid->collection[cinfo.index].level;
702 if (copy_to_user((void *) arg, &cinfo, sizeof(cinfo)))
708 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
711 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
715 len = strlen(hid->name) + 1;
716 if (len > _IOC_SIZE(cmd))
717 len = _IOC_SIZE(cmd);
718 return copy_to_user((char *) arg, hid->name, len) ?
722 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
726 len = strlen(hid->phys) + 1;
727 if (len > _IOC_SIZE(cmd))
728 len = _IOC_SIZE(cmd);
729 return copy_to_user((char *) arg, hid->phys, len) ?
736 static struct file_operations hiddev_fops = {
737 .owner = THIS_MODULE,
739 .write = hiddev_write,
742 .release = hiddev_release,
743 .ioctl = hiddev_ioctl,
744 .fasync = hiddev_fasync,
747 static struct usb_class_driver hiddev_class = {
748 .name = "usb/hid/hiddev%d",
749 .fops = &hiddev_fops,
750 .mode = S_IFCHR | S_IRUGO | S_IWUSR,
751 .minor_base = HIDDEV_MINOR_BASE,
755 * This is where hid.c calls us to connect a hid device to the hiddev driver
757 int hiddev_connect(struct hid_device *hid)
759 struct hiddev *hiddev;
763 for (i = 0; i < hid->maxcollection; i++)
764 if (hid->collection[i].type ==
765 HID_COLLECTION_APPLICATION &&
766 !IS_INPUT_APPLICATION(hid->collection[i].usage))
769 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
772 if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
774 memset(hiddev, 0, sizeof(struct hiddev));
776 retval = usb_register_dev(hid->intf, &hiddev_class);
778 err("Not able to get a minor for this device.");
783 init_waitqueue_head(&hiddev->wait);
785 hiddev_table[hid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
790 hid->minor = hid->intf->minor;
791 hid->hiddev = hiddev;
797 * This is where hid.c calls us to disconnect a hiddev device from the
798 * corresponding hid device (usually because the usb device has disconnected)
800 void hiddev_disconnect(struct hid_device *hid)
802 struct hiddev *hiddev = hid->hiddev;
807 hid_close(hiddev->hid);
808 wake_up_interruptible(&hiddev->wait);
810 hiddev_cleanup(hiddev);
814 /* Currently this driver is a USB driver. It's not a conventional one in
815 * the sense that it doesn't probe at the USB level. Instead it waits to
816 * be connected by HID through the hiddev_connect / hiddev_disconnect
817 * routines. The reason to register as a USB device is to gain part of the
818 * minor number space from the USB major.
820 * In theory, should the HID code be generalized to more than one physical
821 * medium (say, IEEE 1384), this driver will probably need to register its
822 * own major number, and in doing so, no longer need to register with USB.
823 * At that point the probe routine and hiddev_driver struct below will no
828 /* We never attach in this manner, and rely on HID to connect us. This
829 * is why there is no disconnect routine defined in the usb_driver either.
831 static int hiddev_usbd_probe(struct usb_interface *intf,
832 const struct usb_device_id *hiddev_info)
838 static /* const */ struct usb_driver hiddev_driver = {
839 .owner = THIS_MODULE,
841 .probe = hiddev_usbd_probe,
844 int __init hiddev_init(void)
846 devfs_mk_dir("usb/hid");
847 return usb_register(&hiddev_driver);
850 void hiddev_exit(void)
852 usb_deregister(&hiddev_driver);
853 devfs_remove("usb/hid");