13 * Licensed under the GPLv2 only.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/poll.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
22 #include <linux/idr.h>
23 #include <linux/sched.h>
24 #include <linux/string.h>
25 #include <linux/kobject.h>
26 #include <linux/uio_driver.h>
28 #define UIO_MAX_DEVICES 255
35 struct fasync_struct *async_queue;
36 wait_queue_head_t wait;
38 struct uio_info *info;
39 struct kobject *map_dir;
40 struct kobject *portio_dir;
44 static DEFINE_IDR(uio_idr);
45 static const struct file_operations uio_fops;
47 /* UIO class infrastructure */
48 static struct class *uio_class;
50 /* Protect idr accesses */
51 static DEFINE_MUTEX(minor_lock);
61 #define to_map(map) container_of(map, struct uio_map, kobj)
63 static ssize_t map_name_show(struct uio_mem *mem, char *buf)
65 if (unlikely(!mem->name))
68 return sprintf(buf, "%s\n", mem->name);
71 static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
73 return sprintf(buf, "0x%lx\n", mem->addr);
76 static ssize_t map_size_show(struct uio_mem *mem, char *buf)
78 return sprintf(buf, "0x%lx\n", mem->size);
81 static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
83 return sprintf(buf, "0x%lx\n", mem->addr & ~PAGE_MASK);
86 struct map_sysfs_entry {
87 struct attribute attr;
88 ssize_t (*show)(struct uio_mem *, char *);
89 ssize_t (*store)(struct uio_mem *, const char *, size_t);
92 static struct map_sysfs_entry name_attribute =
93 __ATTR(name, S_IRUGO, map_name_show, NULL);
94 static struct map_sysfs_entry addr_attribute =
95 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
96 static struct map_sysfs_entry size_attribute =
97 __ATTR(size, S_IRUGO, map_size_show, NULL);
98 static struct map_sysfs_entry offset_attribute =
99 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
101 static struct attribute *attrs[] = {
102 &name_attribute.attr,
103 &addr_attribute.attr,
104 &size_attribute.attr,
105 &offset_attribute.attr,
106 NULL, /* need to NULL terminate the list of attributes */
109 static void map_release(struct kobject *kobj)
111 struct uio_map *map = to_map(kobj);
115 static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
118 struct uio_map *map = to_map(kobj);
119 struct uio_mem *mem = map->mem;
120 struct map_sysfs_entry *entry;
122 entry = container_of(attr, struct map_sysfs_entry, attr);
127 return entry->show(mem, buf);
130 static const struct sysfs_ops map_sysfs_ops = {
131 .show = map_type_show,
134 static struct kobj_type map_attr_type = {
135 .release = map_release,
136 .sysfs_ops = &map_sysfs_ops,
137 .default_attrs = attrs,
142 struct uio_port *port;
144 #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
146 static ssize_t portio_name_show(struct uio_port *port, char *buf)
148 if (unlikely(!port->name))
151 return sprintf(buf, "%s\n", port->name);
154 static ssize_t portio_start_show(struct uio_port *port, char *buf)
156 return sprintf(buf, "0x%lx\n", port->start);
159 static ssize_t portio_size_show(struct uio_port *port, char *buf)
161 return sprintf(buf, "0x%lx\n", port->size);
164 static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
166 const char *porttypes[] = {"none", "x86", "gpio", "other"};
168 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
171 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
174 struct portio_sysfs_entry {
175 struct attribute attr;
176 ssize_t (*show)(struct uio_port *, char *);
177 ssize_t (*store)(struct uio_port *, const char *, size_t);
180 static struct portio_sysfs_entry portio_name_attribute =
181 __ATTR(name, S_IRUGO, portio_name_show, NULL);
182 static struct portio_sysfs_entry portio_start_attribute =
183 __ATTR(start, S_IRUGO, portio_start_show, NULL);
184 static struct portio_sysfs_entry portio_size_attribute =
185 __ATTR(size, S_IRUGO, portio_size_show, NULL);
186 static struct portio_sysfs_entry portio_porttype_attribute =
187 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
189 static struct attribute *portio_attrs[] = {
190 &portio_name_attribute.attr,
191 &portio_start_attribute.attr,
192 &portio_size_attribute.attr,
193 &portio_porttype_attribute.attr,
197 static void portio_release(struct kobject *kobj)
199 struct uio_portio *portio = to_portio(kobj);
203 static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
206 struct uio_portio *portio = to_portio(kobj);
207 struct uio_port *port = portio->port;
208 struct portio_sysfs_entry *entry;
210 entry = container_of(attr, struct portio_sysfs_entry, attr);
215 return entry->show(port, buf);
218 static const struct sysfs_ops portio_sysfs_ops = {
219 .show = portio_type_show,
222 static struct kobj_type portio_attr_type = {
223 .release = portio_release,
224 .sysfs_ops = &portio_sysfs_ops,
225 .default_attrs = portio_attrs,
228 static ssize_t show_name(struct device *dev,
229 struct device_attribute *attr, char *buf)
231 struct uio_device *idev = dev_get_drvdata(dev);
233 return sprintf(buf, "%s\n", idev->info->name);
237 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
239 static ssize_t show_version(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct uio_device *idev = dev_get_drvdata(dev);
244 return sprintf(buf, "%s\n", idev->info->version);
248 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
250 static ssize_t show_event(struct device *dev,
251 struct device_attribute *attr, char *buf)
253 struct uio_device *idev = dev_get_drvdata(dev);
255 return sprintf(buf, "%u\n",
256 (unsigned int)atomic_read(&idev->event));
260 static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
262 static struct attribute *uio_attrs[] = {
264 &dev_attr_version.attr,
265 &dev_attr_event.attr,
269 static struct attribute_group uio_attr_grp = {
276 static int uio_dev_add_attributes(struct uio_device *idev)
281 int portio_found = 0;
284 struct uio_port *port;
285 struct uio_portio *portio;
287 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
291 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
292 mem = &idev->info->mem[mi];
297 idev->map_dir = kobject_create_and_add("maps",
302 map = kzalloc(sizeof(*map), GFP_KERNEL);
305 kobject_init(&map->kobj, &map_attr_type);
308 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
311 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
316 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
317 port = &idev->info->port[pi];
322 idev->portio_dir = kobject_create_and_add("portio",
324 if (!idev->portio_dir)
327 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
330 kobject_init(&portio->kobj, &portio_attr_type);
332 port->portio = portio;
333 ret = kobject_add(&portio->kobj, idev->portio_dir,
337 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
345 for (pi--; pi >= 0; pi--) {
346 port = &idev->info->port[pi];
347 portio = port->portio;
348 kobject_put(&portio->kobj);
350 kobject_put(idev->portio_dir);
352 for (mi--; mi>=0; mi--) {
353 mem = &idev->info->mem[mi];
355 kobject_put(&map->kobj);
357 kobject_put(idev->map_dir);
358 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
360 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
364 static void uio_dev_del_attributes(struct uio_device *idev)
368 struct uio_port *port;
370 for (i = 0; i < MAX_UIO_MAPS; i++) {
371 mem = &idev->info->mem[i];
374 kobject_put(&mem->map->kobj);
376 kobject_put(idev->map_dir);
378 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
379 port = &idev->info->port[i];
382 kobject_put(&port->portio->kobj);
384 kobject_put(idev->portio_dir);
386 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
389 static int uio_get_minor(struct uio_device *idev)
391 int retval = -ENOMEM;
394 mutex_lock(&minor_lock);
395 if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
398 retval = idr_get_new(&uio_idr, idev, &id);
400 if (retval == -EAGAIN)
404 idev->minor = id & MAX_ID_MASK;
406 mutex_unlock(&minor_lock);
410 static void uio_free_minor(struct uio_device *idev)
412 mutex_lock(&minor_lock);
413 idr_remove(&uio_idr, idev->minor);
414 mutex_unlock(&minor_lock);
418 * uio_event_notify - trigger an interrupt event
419 * @info: UIO device capabilities
421 void uio_event_notify(struct uio_info *info)
423 struct uio_device *idev = info->uio_dev;
425 atomic_inc(&idev->event);
426 wake_up_interruptible(&idev->wait);
427 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
429 EXPORT_SYMBOL_GPL(uio_event_notify);
432 * uio_interrupt - hardware interrupt handler
433 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
434 * @dev_id: Pointer to the devices uio_device structure
436 static irqreturn_t uio_interrupt(int irq, void *dev_id)
438 struct uio_device *idev = (struct uio_device *)dev_id;
439 irqreturn_t ret = idev->info->handler(irq, idev->info);
441 if (ret == IRQ_HANDLED)
442 uio_event_notify(idev->info);
447 struct uio_listener {
448 struct uio_device *dev;
452 static int uio_open(struct inode *inode, struct file *filep)
454 struct uio_device *idev;
455 struct uio_listener *listener;
458 mutex_lock(&minor_lock);
459 idev = idr_find(&uio_idr, iminor(inode));
460 mutex_unlock(&minor_lock);
466 if (!try_module_get(idev->owner)) {
471 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
474 goto err_alloc_listener;
477 listener->dev = idev;
478 listener->event_count = atomic_read(&idev->event);
479 filep->private_data = listener;
481 if (idev->info->open) {
482 ret = idev->info->open(idev->info, inode);
492 module_put(idev->owner);
498 static int uio_fasync(int fd, struct file *filep, int on)
500 struct uio_listener *listener = filep->private_data;
501 struct uio_device *idev = listener->dev;
503 return fasync_helper(fd, filep, on, &idev->async_queue);
506 static int uio_release(struct inode *inode, struct file *filep)
509 struct uio_listener *listener = filep->private_data;
510 struct uio_device *idev = listener->dev;
512 if (idev->info->release)
513 ret = idev->info->release(idev->info, inode);
515 module_put(idev->owner);
520 static unsigned int uio_poll(struct file *filep, poll_table *wait)
522 struct uio_listener *listener = filep->private_data;
523 struct uio_device *idev = listener->dev;
525 if (idev->info->irq == UIO_IRQ_NONE)
528 poll_wait(filep, &idev->wait, wait);
529 if (listener->event_count != atomic_read(&idev->event))
530 return POLLIN | POLLRDNORM;
534 static ssize_t uio_read(struct file *filep, char __user *buf,
535 size_t count, loff_t *ppos)
537 struct uio_listener *listener = filep->private_data;
538 struct uio_device *idev = listener->dev;
539 DECLARE_WAITQUEUE(wait, current);
543 if (idev->info->irq == UIO_IRQ_NONE)
546 if (count != sizeof(s32))
549 add_wait_queue(&idev->wait, &wait);
552 set_current_state(TASK_INTERRUPTIBLE);
554 event_count = atomic_read(&idev->event);
555 if (event_count != listener->event_count) {
556 if (copy_to_user(buf, &event_count, count))
559 listener->event_count = event_count;
565 if (filep->f_flags & O_NONBLOCK) {
570 if (signal_pending(current)) {
571 retval = -ERESTARTSYS;
577 __set_current_state(TASK_RUNNING);
578 remove_wait_queue(&idev->wait, &wait);
583 static ssize_t uio_write(struct file *filep, const char __user *buf,
584 size_t count, loff_t *ppos)
586 struct uio_listener *listener = filep->private_data;
587 struct uio_device *idev = listener->dev;
591 if (idev->info->irq == UIO_IRQ_NONE)
594 if (count != sizeof(s32))
597 if (!idev->info->irqcontrol)
600 if (copy_from_user(&irq_on, buf, count))
603 retval = idev->info->irqcontrol(idev->info, irq_on);
605 return retval ? retval : sizeof(s32);
608 static int uio_find_mem_index(struct vm_area_struct *vma)
611 struct uio_device *idev = vma->vm_private_data;
613 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
614 if (idev->info->mem[mi].size == 0)
616 if (vma->vm_pgoff == mi)
622 static void uio_vma_open(struct vm_area_struct *vma)
624 struct uio_device *idev = vma->vm_private_data;
628 static void uio_vma_close(struct vm_area_struct *vma)
630 struct uio_device *idev = vma->vm_private_data;
634 static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
636 struct uio_device *idev = vma->vm_private_data;
638 unsigned long offset;
640 int mi = uio_find_mem_index(vma);
642 return VM_FAULT_SIGBUS;
645 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
648 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
650 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
651 page = virt_to_page(idev->info->mem[mi].addr + offset);
653 page = vmalloc_to_page((void *)idev->info->mem[mi].addr
660 static const struct vm_operations_struct uio_vm_ops = {
661 .open = uio_vma_open,
662 .close = uio_vma_close,
663 .fault = uio_vma_fault,
666 static int uio_mmap_physical(struct vm_area_struct *vma)
668 struct uio_device *idev = vma->vm_private_data;
669 int mi = uio_find_mem_index(vma);
673 vma->vm_flags |= VM_IO | VM_RESERVED;
675 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
677 return remap_pfn_range(vma,
679 idev->info->mem[mi].addr >> PAGE_SHIFT,
680 vma->vm_end - vma->vm_start,
684 static int uio_mmap_logical(struct vm_area_struct *vma)
686 vma->vm_flags |= VM_RESERVED;
687 vma->vm_ops = &uio_vm_ops;
692 static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
694 struct uio_listener *listener = filep->private_data;
695 struct uio_device *idev = listener->dev;
697 unsigned long requested_pages, actual_pages;
700 if (vma->vm_end < vma->vm_start)
703 vma->vm_private_data = idev;
705 mi = uio_find_mem_index(vma);
709 requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
710 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
711 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
712 if (requested_pages > actual_pages)
715 if (idev->info->mmap) {
716 ret = idev->info->mmap(idev->info, vma);
720 switch (idev->info->mem[mi].memtype) {
722 return uio_mmap_physical(vma);
723 case UIO_MEM_LOGICAL:
724 case UIO_MEM_VIRTUAL:
725 return uio_mmap_logical(vma);
731 static const struct file_operations uio_fops = {
732 .owner = THIS_MODULE,
734 .release = uio_release,
739 .fasync = uio_fasync,
742 static int uio_major_init(void)
744 uio_major = register_chrdev(0, "uio", &uio_fops);
750 static void uio_major_cleanup(void)
752 unregister_chrdev(uio_major, "uio");
755 static int init_uio_class(void)
760 /* This is the first time in here, set everything up properly */
761 ret = uio_major_init();
765 class = class_create(THIS_MODULE, "uio");
768 printk(KERN_ERR "class_create failed for uio\n");
769 goto err_class_create;
780 static void release_uio_class(void)
782 /* Ok, we cheat as we know we only have one uio_class */
783 class_destroy(uio_class);
789 * uio_register_device - register a new userspace IO device
790 * @owner: module that creates the new device
791 * @parent: parent device
792 * @info: UIO device capabilities
794 * returns zero on success or a negative error code.
796 int __uio_register_device(struct module *owner,
797 struct device *parent,
798 struct uio_info *info)
800 struct uio_device *idev;
803 if (!parent || !info || !info->name || !info->version)
806 info->uio_dev = NULL;
808 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
816 init_waitqueue_head(&idev->wait);
817 atomic_set(&idev->event, 0);
819 ret = uio_get_minor(idev);
823 idev->dev = device_create(uio_class, parent,
824 MKDEV(uio_major, idev->minor), idev,
825 "uio%d", idev->minor);
826 if (IS_ERR(idev->dev)) {
827 printk(KERN_ERR "UIO: device register failed\n");
828 ret = PTR_ERR(idev->dev);
829 goto err_device_create;
832 ret = uio_dev_add_attributes(idev);
834 goto err_uio_dev_add_attributes;
836 info->uio_dev = idev;
838 if (idev->info->irq >= 0) {
839 ret = request_irq(idev->info->irq, uio_interrupt,
840 idev->info->irq_flags, idev->info->name, idev);
842 goto err_request_irq;
848 uio_dev_del_attributes(idev);
849 err_uio_dev_add_attributes:
850 device_destroy(uio_class, MKDEV(uio_major, idev->minor));
852 uio_free_minor(idev);
858 EXPORT_SYMBOL_GPL(__uio_register_device);
861 * uio_unregister_device - unregister a industrial IO device
862 * @info: UIO device capabilities
865 void uio_unregister_device(struct uio_info *info)
867 struct uio_device *idev;
869 if (!info || !info->uio_dev)
872 idev = info->uio_dev;
874 uio_free_minor(idev);
877 free_irq(info->irq, idev);
879 uio_dev_del_attributes(idev);
881 dev_set_drvdata(idev->dev, NULL);
882 device_destroy(uio_class, MKDEV(uio_major, idev->minor));
887 EXPORT_SYMBOL_GPL(uio_unregister_device);
889 static int __init uio_init(void)
891 return init_uio_class();
894 static void __exit uio_exit(void)
899 module_init(uio_init)
900 module_exit(uio_exit)
901 MODULE_LICENSE("GPL v2");