4 * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
5 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
6 * Copyright(C) 2006, Hans J. Koch <hjk@linutronix.de>
7 * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
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>
21 #include <linux/idr.h>
22 #include <linux/string.h>
23 #include <linux/kobject.h>
24 #include <linux/uio_driver.h>
26 #define UIO_MAX_DEVICES 255
33 struct fasync_struct *async_queue;
34 wait_queue_head_t wait;
36 struct uio_info *info;
37 struct kobject *map_dir;
38 struct kobject *portio_dir;
42 static DEFINE_IDR(uio_idr);
43 static const struct file_operations uio_fops;
45 /* UIO class infrastructure */
46 static struct uio_class {
51 /* Protect idr accesses */
52 static DEFINE_MUTEX(minor_lock);
62 #define to_map(map) container_of(map, struct uio_map, kobj)
64 static ssize_t map_name_show(struct uio_mem *mem, char *buf)
66 if (unlikely(!mem->name))
69 return sprintf(buf, "%s\n", mem->name);
72 static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
74 return sprintf(buf, "0x%lx\n", mem->addr);
77 static ssize_t map_size_show(struct uio_mem *mem, char *buf)
79 return sprintf(buf, "0x%lx\n", mem->size);
82 static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
84 return sprintf(buf, "0x%lx\n", mem->addr & ~PAGE_MASK);
87 struct map_sysfs_entry {
88 struct attribute attr;
89 ssize_t (*show)(struct uio_mem *, char *);
90 ssize_t (*store)(struct uio_mem *, const char *, size_t);
93 static struct map_sysfs_entry name_attribute =
94 __ATTR(name, S_IRUGO, map_name_show, NULL);
95 static struct map_sysfs_entry addr_attribute =
96 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
97 static struct map_sysfs_entry size_attribute =
98 __ATTR(size, S_IRUGO, map_size_show, NULL);
99 static struct map_sysfs_entry offset_attribute =
100 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
102 static struct attribute *attrs[] = {
103 &name_attribute.attr,
104 &addr_attribute.attr,
105 &size_attribute.attr,
106 &offset_attribute.attr,
107 NULL, /* need to NULL terminate the list of attributes */
110 static void map_release(struct kobject *kobj)
112 struct uio_map *map = to_map(kobj);
116 static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
119 struct uio_map *map = to_map(kobj);
120 struct uio_mem *mem = map->mem;
121 struct map_sysfs_entry *entry;
123 entry = container_of(attr, struct map_sysfs_entry, attr);
128 return entry->show(mem, buf);
131 static struct sysfs_ops map_sysfs_ops = {
132 .show = map_type_show,
135 static struct kobj_type map_attr_type = {
136 .release = map_release,
137 .sysfs_ops = &map_sysfs_ops,
138 .default_attrs = attrs,
143 struct uio_port *port;
145 #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
147 static ssize_t portio_name_show(struct uio_port *port, char *buf)
149 if (unlikely(!port->name))
152 return sprintf(buf, "%s\n", port->name);
155 static ssize_t portio_start_show(struct uio_port *port, char *buf)
157 return sprintf(buf, "0x%lx\n", port->start);
160 static ssize_t portio_size_show(struct uio_port *port, char *buf)
162 return sprintf(buf, "0x%lx\n", port->size);
165 static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
167 const char *porttypes[] = {"none", "x86", "gpio", "other"};
169 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
172 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
175 struct portio_sysfs_entry {
176 struct attribute attr;
177 ssize_t (*show)(struct uio_port *, char *);
178 ssize_t (*store)(struct uio_port *, const char *, size_t);
181 static struct portio_sysfs_entry portio_name_attribute =
182 __ATTR(name, S_IRUGO, portio_name_show, NULL);
183 static struct portio_sysfs_entry portio_start_attribute =
184 __ATTR(start, S_IRUGO, portio_start_show, NULL);
185 static struct portio_sysfs_entry portio_size_attribute =
186 __ATTR(size, S_IRUGO, portio_size_show, NULL);
187 static struct portio_sysfs_entry portio_porttype_attribute =
188 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
190 static struct attribute *portio_attrs[] = {
191 &portio_name_attribute.attr,
192 &portio_start_attribute.attr,
193 &portio_size_attribute.attr,
194 &portio_porttype_attribute.attr,
198 static void portio_release(struct kobject *kobj)
200 struct uio_portio *portio = to_portio(kobj);
204 static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
207 struct uio_portio *portio = to_portio(kobj);
208 struct uio_port *port = portio->port;
209 struct portio_sysfs_entry *entry;
211 entry = container_of(attr, struct portio_sysfs_entry, attr);
216 return entry->show(port, buf);
219 static struct sysfs_ops portio_sysfs_ops = {
220 .show = portio_type_show,
223 static struct kobj_type portio_attr_type = {
224 .release = portio_release,
225 .sysfs_ops = &portio_sysfs_ops,
226 .default_attrs = portio_attrs,
229 static ssize_t show_name(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct uio_device *idev = dev_get_drvdata(dev);
234 return sprintf(buf, "%s\n", idev->info->name);
238 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
240 static ssize_t show_version(struct device *dev,
241 struct device_attribute *attr, char *buf)
243 struct uio_device *idev = dev_get_drvdata(dev);
245 return sprintf(buf, "%s\n", idev->info->version);
249 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
251 static ssize_t show_event(struct device *dev,
252 struct device_attribute *attr, char *buf)
254 struct uio_device *idev = dev_get_drvdata(dev);
256 return sprintf(buf, "%u\n",
257 (unsigned int)atomic_read(&idev->event));
261 static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
263 static struct attribute *uio_attrs[] = {
265 &dev_attr_version.attr,
266 &dev_attr_event.attr,
270 static struct attribute_group uio_attr_grp = {
277 static int uio_dev_add_attributes(struct uio_device *idev)
282 int portio_found = 0;
285 struct uio_port *port;
286 struct uio_portio *portio;
288 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
292 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
293 mem = &idev->info->mem[mi];
298 idev->map_dir = kobject_create_and_add("maps",
303 map = kzalloc(sizeof(*map), GFP_KERNEL);
306 kobject_init(&map->kobj, &map_attr_type);
309 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
312 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
317 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
318 port = &idev->info->port[pi];
323 idev->portio_dir = kobject_create_and_add("portio",
325 if (!idev->portio_dir)
328 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
331 kobject_init(&portio->kobj, &portio_attr_type);
333 port->portio = portio;
334 ret = kobject_add(&portio->kobj, idev->portio_dir,
338 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
346 for (pi--; pi >= 0; pi--) {
347 port = &idev->info->port[pi];
348 portio = port->portio;
349 kobject_put(&portio->kobj);
351 kobject_put(idev->portio_dir);
353 for (mi--; mi>=0; mi--) {
354 mem = &idev->info->mem[mi];
356 kobject_put(&map->kobj);
358 kobject_put(idev->map_dir);
359 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
361 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
365 static void uio_dev_del_attributes(struct uio_device *idev)
369 struct uio_port *port;
371 for (i = 0; i < MAX_UIO_MAPS; i++) {
372 mem = &idev->info->mem[i];
375 kobject_put(&mem->map->kobj);
377 kobject_put(idev->map_dir);
379 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
380 port = &idev->info->port[i];
383 kobject_put(&port->portio->kobj);
385 kobject_put(idev->portio_dir);
387 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
390 static int uio_get_minor(struct uio_device *idev)
392 int retval = -ENOMEM;
395 mutex_lock(&minor_lock);
396 if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
399 retval = idr_get_new(&uio_idr, idev, &id);
401 if (retval == -EAGAIN)
405 idev->minor = id & MAX_ID_MASK;
407 mutex_unlock(&minor_lock);
411 static void uio_free_minor(struct uio_device *idev)
413 mutex_lock(&minor_lock);
414 idr_remove(&uio_idr, idev->minor);
415 mutex_unlock(&minor_lock);
419 * uio_event_notify - trigger an interrupt event
420 * @info: UIO device capabilities
422 void uio_event_notify(struct uio_info *info)
424 struct uio_device *idev = info->uio_dev;
426 atomic_inc(&idev->event);
427 wake_up_interruptible(&idev->wait);
428 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
430 EXPORT_SYMBOL_GPL(uio_event_notify);
433 * uio_interrupt - hardware interrupt handler
434 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
435 * @dev_id: Pointer to the devices uio_device structure
437 static irqreturn_t uio_interrupt(int irq, void *dev_id)
439 struct uio_device *idev = (struct uio_device *)dev_id;
440 irqreturn_t ret = idev->info->handler(irq, idev->info);
442 if (ret == IRQ_HANDLED)
443 uio_event_notify(idev->info);
448 struct uio_listener {
449 struct uio_device *dev;
453 static int uio_open(struct inode *inode, struct file *filep)
455 struct uio_device *idev;
456 struct uio_listener *listener;
459 mutex_lock(&minor_lock);
460 idev = idr_find(&uio_idr, iminor(inode));
461 mutex_unlock(&minor_lock);
467 if (!try_module_get(idev->owner)) {
472 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
475 goto err_alloc_listener;
478 listener->dev = idev;
479 listener->event_count = atomic_read(&idev->event);
480 filep->private_data = listener;
482 if (idev->info->open) {
483 ret = idev->info->open(idev->info, inode);
493 module_put(idev->owner);
499 static int uio_fasync(int fd, struct file *filep, int on)
501 struct uio_listener *listener = filep->private_data;
502 struct uio_device *idev = listener->dev;
504 return fasync_helper(fd, filep, on, &idev->async_queue);
507 static int uio_release(struct inode *inode, struct file *filep)
510 struct uio_listener *listener = filep->private_data;
511 struct uio_device *idev = listener->dev;
513 if (idev->info->release)
514 ret = idev->info->release(idev->info, inode);
516 module_put(idev->owner);
521 static unsigned int uio_poll(struct file *filep, poll_table *wait)
523 struct uio_listener *listener = filep->private_data;
524 struct uio_device *idev = listener->dev;
526 if (idev->info->irq == UIO_IRQ_NONE)
529 poll_wait(filep, &idev->wait, wait);
530 if (listener->event_count != atomic_read(&idev->event))
531 return POLLIN | POLLRDNORM;
535 static ssize_t uio_read(struct file *filep, char __user *buf,
536 size_t count, loff_t *ppos)
538 struct uio_listener *listener = filep->private_data;
539 struct uio_device *idev = listener->dev;
540 DECLARE_WAITQUEUE(wait, current);
544 if (idev->info->irq == UIO_IRQ_NONE)
547 if (count != sizeof(s32))
550 add_wait_queue(&idev->wait, &wait);
553 set_current_state(TASK_INTERRUPTIBLE);
555 event_count = atomic_read(&idev->event);
556 if (event_count != listener->event_count) {
557 if (copy_to_user(buf, &event_count, count))
560 listener->event_count = event_count;
566 if (filep->f_flags & O_NONBLOCK) {
571 if (signal_pending(current)) {
572 retval = -ERESTARTSYS;
578 __set_current_state(TASK_RUNNING);
579 remove_wait_queue(&idev->wait, &wait);
584 static ssize_t uio_write(struct file *filep, const char __user *buf,
585 size_t count, loff_t *ppos)
587 struct uio_listener *listener = filep->private_data;
588 struct uio_device *idev = listener->dev;
592 if (idev->info->irq == UIO_IRQ_NONE)
595 if (count != sizeof(s32))
598 if (!idev->info->irqcontrol)
601 if (copy_from_user(&irq_on, buf, count))
604 retval = idev->info->irqcontrol(idev->info, irq_on);
606 return retval ? retval : sizeof(s32);
609 static int uio_find_mem_index(struct vm_area_struct *vma)
612 struct uio_device *idev = vma->vm_private_data;
614 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
615 if (idev->info->mem[mi].size == 0)
617 if (vma->vm_pgoff == mi)
623 static void uio_vma_open(struct vm_area_struct *vma)
625 struct uio_device *idev = vma->vm_private_data;
629 static void uio_vma_close(struct vm_area_struct *vma)
631 struct uio_device *idev = vma->vm_private_data;
635 static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
637 struct uio_device *idev = vma->vm_private_data;
639 unsigned long offset;
641 int mi = uio_find_mem_index(vma);
643 return VM_FAULT_SIGBUS;
646 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
649 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
651 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
652 page = virt_to_page(idev->info->mem[mi].addr + offset);
654 page = vmalloc_to_page((void *)idev->info->mem[mi].addr
661 static struct vm_operations_struct uio_vm_ops = {
662 .open = uio_vma_open,
663 .close = uio_vma_close,
664 .fault = uio_vma_fault,
667 static int uio_mmap_physical(struct vm_area_struct *vma)
669 struct uio_device *idev = vma->vm_private_data;
670 int mi = uio_find_mem_index(vma);
674 vma->vm_flags |= VM_IO | VM_RESERVED;
676 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
678 return remap_pfn_range(vma,
680 idev->info->mem[mi].addr >> PAGE_SHIFT,
681 vma->vm_end - vma->vm_start,
685 static int uio_mmap_logical(struct vm_area_struct *vma)
687 vma->vm_flags |= VM_RESERVED;
688 vma->vm_ops = &uio_vm_ops;
693 static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
695 struct uio_listener *listener = filep->private_data;
696 struct uio_device *idev = listener->dev;
698 unsigned long requested_pages, actual_pages;
701 if (vma->vm_end < vma->vm_start)
704 vma->vm_private_data = idev;
706 mi = uio_find_mem_index(vma);
710 requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
711 actual_pages = (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)
759 if (uio_class != NULL) {
760 kref_get(&uio_class->kref);
764 /* This is the first time in here, set everything up properly */
765 ret = uio_major_init();
769 uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
775 kref_init(&uio_class->kref);
776 uio_class->class = class_create(THIS_MODULE, "uio");
777 if (IS_ERR(uio_class->class)) {
778 ret = IS_ERR(uio_class->class);
779 printk(KERN_ERR "class_create failed for uio\n");
780 goto err_class_create;
793 static void release_uio_class(struct kref *kref)
795 /* Ok, we cheat as we know we only have one uio_class */
796 class_destroy(uio_class->class);
802 static void uio_class_destroy(void)
805 kref_put(&uio_class->kref, release_uio_class);
809 * uio_register_device - register a new userspace IO device
810 * @owner: module that creates the new device
811 * @parent: parent device
812 * @info: UIO device capabilities
814 * returns zero on success or a negative error code.
816 int __uio_register_device(struct module *owner,
817 struct device *parent,
818 struct uio_info *info)
820 struct uio_device *idev;
823 if (!parent || !info || !info->name || !info->version)
826 info->uio_dev = NULL;
828 ret = init_uio_class();
832 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
840 init_waitqueue_head(&idev->wait);
841 atomic_set(&idev->event, 0);
843 ret = uio_get_minor(idev);
847 idev->dev = device_create(uio_class->class, parent,
848 MKDEV(uio_major, idev->minor), idev,
849 "uio%d", idev->minor);
850 if (IS_ERR(idev->dev)) {
851 printk(KERN_ERR "UIO: device register failed\n");
852 ret = PTR_ERR(idev->dev);
853 goto err_device_create;
856 ret = uio_dev_add_attributes(idev);
858 goto err_uio_dev_add_attributes;
860 info->uio_dev = idev;
862 if (idev->info->irq >= 0) {
863 ret = request_irq(idev->info->irq, uio_interrupt,
864 idev->info->irq_flags, idev->info->name, idev);
866 goto err_request_irq;
872 uio_dev_del_attributes(idev);
873 err_uio_dev_add_attributes:
874 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
876 uio_free_minor(idev);
883 EXPORT_SYMBOL_GPL(__uio_register_device);
886 * uio_unregister_device - unregister a industrial IO device
887 * @info: UIO device capabilities
890 void uio_unregister_device(struct uio_info *info)
892 struct uio_device *idev;
894 if (!info || !info->uio_dev)
897 idev = info->uio_dev;
899 uio_free_minor(idev);
902 free_irq(info->irq, idev);
904 uio_dev_del_attributes(idev);
906 dev_set_drvdata(idev->dev, NULL);
907 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
913 EXPORT_SYMBOL_GPL(uio_unregister_device);
915 static int __init uio_init(void)
920 static void __exit uio_exit(void)
924 module_init(uio_init)
925 module_exit(uio_exit)
926 MODULE_LICENSE("GPL v2");