1 // SPDX-License-Identifier: GPL-2.0
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/poll.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
21 #include <linux/idr.h>
22 #include <linux/sched/signal.h>
23 #include <linux/string.h>
24 #include <linux/kobject.h>
25 #include <linux/cdev.h>
26 #include <linux/uio_driver.h>
28 #define UIO_MAX_DEVICES (1U << MINORBITS)
31 static struct cdev *uio_cdev;
32 static DEFINE_IDR(uio_idr);
33 static const struct file_operations uio_fops;
35 /* Protect idr accesses */
36 static DEFINE_MUTEX(minor_lock);
46 #define to_map(map) container_of(map, struct uio_map, kobj)
48 static ssize_t map_name_show(struct uio_mem *mem, char *buf)
50 if (unlikely(!mem->name))
53 return sprintf(buf, "%s\n", mem->name);
56 static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
58 return sprintf(buf, "%pa\n", &mem->addr);
61 static ssize_t map_size_show(struct uio_mem *mem, char *buf)
63 return sprintf(buf, "%pa\n", &mem->size);
66 static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
68 return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs);
71 struct map_sysfs_entry {
72 struct attribute attr;
73 ssize_t (*show)(struct uio_mem *, char *);
74 ssize_t (*store)(struct uio_mem *, const char *, size_t);
77 static struct map_sysfs_entry name_attribute =
78 __ATTR(name, S_IRUGO, map_name_show, NULL);
79 static struct map_sysfs_entry addr_attribute =
80 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
81 static struct map_sysfs_entry size_attribute =
82 __ATTR(size, S_IRUGO, map_size_show, NULL);
83 static struct map_sysfs_entry offset_attribute =
84 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
86 static struct attribute *attrs[] = {
90 &offset_attribute.attr,
91 NULL, /* need to NULL terminate the list of attributes */
94 static void map_release(struct kobject *kobj)
96 struct uio_map *map = to_map(kobj);
100 static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
103 struct uio_map *map = to_map(kobj);
104 struct uio_mem *mem = map->mem;
105 struct map_sysfs_entry *entry;
107 entry = container_of(attr, struct map_sysfs_entry, attr);
112 return entry->show(mem, buf);
115 static const struct sysfs_ops map_sysfs_ops = {
116 .show = map_type_show,
119 static struct kobj_type map_attr_type = {
120 .release = map_release,
121 .sysfs_ops = &map_sysfs_ops,
122 .default_attrs = attrs,
127 struct uio_port *port;
129 #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
131 static ssize_t portio_name_show(struct uio_port *port, char *buf)
133 if (unlikely(!port->name))
136 return sprintf(buf, "%s\n", port->name);
139 static ssize_t portio_start_show(struct uio_port *port, char *buf)
141 return sprintf(buf, "0x%lx\n", port->start);
144 static ssize_t portio_size_show(struct uio_port *port, char *buf)
146 return sprintf(buf, "0x%lx\n", port->size);
149 static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
151 const char *porttypes[] = {"none", "x86", "gpio", "other"};
153 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
156 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
159 struct portio_sysfs_entry {
160 struct attribute attr;
161 ssize_t (*show)(struct uio_port *, char *);
162 ssize_t (*store)(struct uio_port *, const char *, size_t);
165 static struct portio_sysfs_entry portio_name_attribute =
166 __ATTR(name, S_IRUGO, portio_name_show, NULL);
167 static struct portio_sysfs_entry portio_start_attribute =
168 __ATTR(start, S_IRUGO, portio_start_show, NULL);
169 static struct portio_sysfs_entry portio_size_attribute =
170 __ATTR(size, S_IRUGO, portio_size_show, NULL);
171 static struct portio_sysfs_entry portio_porttype_attribute =
172 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
174 static struct attribute *portio_attrs[] = {
175 &portio_name_attribute.attr,
176 &portio_start_attribute.attr,
177 &portio_size_attribute.attr,
178 &portio_porttype_attribute.attr,
182 static void portio_release(struct kobject *kobj)
184 struct uio_portio *portio = to_portio(kobj);
188 static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
191 struct uio_portio *portio = to_portio(kobj);
192 struct uio_port *port = portio->port;
193 struct portio_sysfs_entry *entry;
195 entry = container_of(attr, struct portio_sysfs_entry, attr);
200 return entry->show(port, buf);
203 static const struct sysfs_ops portio_sysfs_ops = {
204 .show = portio_type_show,
207 static struct kobj_type portio_attr_type = {
208 .release = portio_release,
209 .sysfs_ops = &portio_sysfs_ops,
210 .default_attrs = portio_attrs,
213 static ssize_t name_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct uio_device *idev = dev_get_drvdata(dev);
219 mutex_lock(&idev->info_lock);
222 dev_err(dev, "the device has been unregistered\n");
226 ret = sprintf(buf, "%s\n", idev->info->name);
229 mutex_unlock(&idev->info_lock);
232 static DEVICE_ATTR_RO(name);
234 static ssize_t version_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct uio_device *idev = dev_get_drvdata(dev);
240 mutex_lock(&idev->info_lock);
243 dev_err(dev, "the device has been unregistered\n");
247 ret = sprintf(buf, "%s\n", idev->info->version);
250 mutex_unlock(&idev->info_lock);
253 static DEVICE_ATTR_RO(version);
255 static ssize_t event_show(struct device *dev,
256 struct device_attribute *attr, char *buf)
258 struct uio_device *idev = dev_get_drvdata(dev);
259 return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
261 static DEVICE_ATTR_RO(event);
263 static struct attribute *uio_attrs[] = {
265 &dev_attr_version.attr,
266 &dev_attr_event.attr,
269 ATTRIBUTE_GROUPS(uio);
271 /* UIO class infrastructure */
272 static struct class uio_class = {
274 .dev_groups = uio_groups,
280 static int uio_dev_add_attributes(struct uio_device *idev)
285 int portio_found = 0;
288 struct uio_port *port;
289 struct uio_portio *portio;
291 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
292 mem = &idev->info->mem[mi];
297 idev->map_dir = kobject_create_and_add("maps",
299 if (!idev->map_dir) {
304 map = kzalloc(sizeof(*map), GFP_KERNEL);
309 kobject_init(&map->kobj, &map_attr_type);
312 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
315 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
320 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
321 port = &idev->info->port[pi];
326 idev->portio_dir = kobject_create_and_add("portio",
328 if (!idev->portio_dir) {
333 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
338 kobject_init(&portio->kobj, &portio_attr_type);
340 port->portio = portio;
341 ret = kobject_add(&portio->kobj, idev->portio_dir,
344 goto err_portio_kobj;
345 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
347 goto err_portio_kobj;
355 for (; pi >= 0; pi--) {
356 port = &idev->info->port[pi];
357 portio = port->portio;
358 kobject_put(&portio->kobj);
360 kobject_put(idev->portio_dir);
364 for (; mi >= 0; mi--) {
365 mem = &idev->info->mem[mi];
367 kobject_put(&map->kobj);
369 kobject_put(idev->map_dir);
370 dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
374 static void uio_dev_del_attributes(struct uio_device *idev)
378 struct uio_port *port;
380 for (i = 0; i < MAX_UIO_MAPS; i++) {
381 mem = &idev->info->mem[i];
384 kobject_put(&mem->map->kobj);
386 kobject_put(idev->map_dir);
388 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
389 port = &idev->info->port[i];
392 kobject_put(&port->portio->kobj);
394 kobject_put(idev->portio_dir);
397 static int uio_get_minor(struct uio_device *idev)
399 int retval = -ENOMEM;
401 mutex_lock(&minor_lock);
402 retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
404 idev->minor = retval;
406 } else if (retval == -ENOSPC) {
407 dev_err(&idev->dev, "too many uio devices\n");
410 mutex_unlock(&minor_lock);
414 static void uio_free_minor(struct uio_device *idev)
416 mutex_lock(&minor_lock);
417 idr_remove(&uio_idr, idev->minor);
418 mutex_unlock(&minor_lock);
422 * uio_event_notify - trigger an interrupt event
423 * @info: UIO device capabilities
425 void uio_event_notify(struct uio_info *info)
427 struct uio_device *idev = info->uio_dev;
429 atomic_inc(&idev->event);
430 wake_up_interruptible(&idev->wait);
431 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
433 EXPORT_SYMBOL_GPL(uio_event_notify);
436 * uio_interrupt - hardware interrupt handler
437 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
438 * @dev_id: Pointer to the devices uio_device structure
440 static irqreturn_t uio_interrupt(int irq, void *dev_id)
442 struct uio_device *idev = (struct uio_device *)dev_id;
445 ret = idev->info->handler(irq, idev->info);
446 if (ret == IRQ_HANDLED)
447 uio_event_notify(idev->info);
452 struct uio_listener {
453 struct uio_device *dev;
457 static int uio_open(struct inode *inode, struct file *filep)
459 struct uio_device *idev;
460 struct uio_listener *listener;
463 mutex_lock(&minor_lock);
464 idev = idr_find(&uio_idr, iminor(inode));
465 mutex_unlock(&minor_lock);
471 get_device(&idev->dev);
473 if (!try_module_get(idev->owner)) {
478 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
481 goto err_alloc_listener;
484 listener->dev = idev;
485 listener->event_count = atomic_read(&idev->event);
486 filep->private_data = listener;
488 mutex_lock(&idev->info_lock);
490 mutex_unlock(&idev->info_lock);
492 goto err_alloc_listener;
495 if (idev->info && idev->info->open)
496 ret = idev->info->open(idev->info, inode);
497 mutex_unlock(&idev->info_lock);
507 module_put(idev->owner);
510 put_device(&idev->dev);
516 static int uio_fasync(int fd, struct file *filep, int on)
518 struct uio_listener *listener = filep->private_data;
519 struct uio_device *idev = listener->dev;
521 return fasync_helper(fd, filep, on, &idev->async_queue);
524 static int uio_release(struct inode *inode, struct file *filep)
527 struct uio_listener *listener = filep->private_data;
528 struct uio_device *idev = listener->dev;
530 mutex_lock(&idev->info_lock);
531 if (idev->info && idev->info->release)
532 ret = idev->info->release(idev->info, inode);
533 mutex_unlock(&idev->info_lock);
535 module_put(idev->owner);
537 put_device(&idev->dev);
541 static __poll_t uio_poll(struct file *filep, poll_table *wait)
543 struct uio_listener *listener = filep->private_data;
544 struct uio_device *idev = listener->dev;
547 mutex_lock(&idev->info_lock);
548 if (!idev->info || !idev->info->irq)
550 mutex_unlock(&idev->info_lock);
555 poll_wait(filep, &idev->wait, wait);
556 if (listener->event_count != atomic_read(&idev->event))
557 return EPOLLIN | EPOLLRDNORM;
561 static ssize_t uio_read(struct file *filep, char __user *buf,
562 size_t count, loff_t *ppos)
564 struct uio_listener *listener = filep->private_data;
565 struct uio_device *idev = listener->dev;
566 DECLARE_WAITQUEUE(wait, current);
570 mutex_lock(&idev->info_lock);
571 if (!idev->info || !idev->info->irq)
573 mutex_unlock(&idev->info_lock);
578 if (count != sizeof(s32))
581 add_wait_queue(&idev->wait, &wait);
584 set_current_state(TASK_INTERRUPTIBLE);
586 event_count = atomic_read(&idev->event);
587 if (event_count != listener->event_count) {
588 __set_current_state(TASK_RUNNING);
589 if (copy_to_user(buf, &event_count, count))
592 listener->event_count = event_count;
598 if (filep->f_flags & O_NONBLOCK) {
603 if (signal_pending(current)) {
604 retval = -ERESTARTSYS;
610 __set_current_state(TASK_RUNNING);
611 remove_wait_queue(&idev->wait, &wait);
616 static ssize_t uio_write(struct file *filep, const char __user *buf,
617 size_t count, loff_t *ppos)
619 struct uio_listener *listener = filep->private_data;
620 struct uio_device *idev = listener->dev;
624 if (count != sizeof(s32))
627 if (copy_from_user(&irq_on, buf, count))
630 mutex_lock(&idev->info_lock);
636 if (!idev->info || !idev->info->irq) {
641 if (!idev->info->irqcontrol) {
646 retval = idev->info->irqcontrol(idev->info, irq_on);
649 mutex_unlock(&idev->info_lock);
650 return retval ? retval : sizeof(s32);
653 static int uio_find_mem_index(struct vm_area_struct *vma)
655 struct uio_device *idev = vma->vm_private_data;
657 if (vma->vm_pgoff < MAX_UIO_MAPS) {
658 if (idev->info->mem[vma->vm_pgoff].size == 0)
660 return (int)vma->vm_pgoff;
665 static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
667 struct uio_device *idev = vmf->vma->vm_private_data;
669 unsigned long offset;
674 mutex_lock(&idev->info_lock);
676 ret = VM_FAULT_SIGBUS;
680 mi = uio_find_mem_index(vmf->vma);
682 ret = VM_FAULT_SIGBUS;
687 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
690 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
692 addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
693 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
694 page = virt_to_page(addr);
696 page = vmalloc_to_page(addr);
701 mutex_unlock(&idev->info_lock);
706 static const struct vm_operations_struct uio_logical_vm_ops = {
707 .fault = uio_vma_fault,
710 static int uio_mmap_logical(struct vm_area_struct *vma)
712 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
713 vma->vm_ops = &uio_logical_vm_ops;
717 static const struct vm_operations_struct uio_physical_vm_ops = {
718 #ifdef CONFIG_HAVE_IOREMAP_PROT
719 .access = generic_access_phys,
723 static int uio_mmap_physical(struct vm_area_struct *vma)
725 struct uio_device *idev = vma->vm_private_data;
726 int mi = uio_find_mem_index(vma);
731 mem = idev->info->mem + mi;
733 if (mem->addr & ~PAGE_MASK)
735 if (vma->vm_end - vma->vm_start > mem->size)
738 vma->vm_ops = &uio_physical_vm_ops;
739 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
742 * We cannot use the vm_iomap_memory() helper here,
743 * because vma->vm_pgoff is the map index we looked
744 * up above in uio_find_mem_index(), rather than an
745 * actual page offset into the mmap.
747 * So we just do the physical mmap without a page
750 return remap_pfn_range(vma,
752 mem->addr >> PAGE_SHIFT,
753 vma->vm_end - vma->vm_start,
757 static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
759 struct uio_listener *listener = filep->private_data;
760 struct uio_device *idev = listener->dev;
762 unsigned long requested_pages, actual_pages;
765 if (vma->vm_end < vma->vm_start)
768 vma->vm_private_data = idev;
770 mutex_lock(&idev->info_lock);
776 mi = uio_find_mem_index(vma);
782 requested_pages = vma_pages(vma);
783 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
784 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
785 if (requested_pages > actual_pages) {
790 if (idev->info->mmap) {
791 ret = idev->info->mmap(idev->info, vma);
795 switch (idev->info->mem[mi].memtype) {
797 ret = uio_mmap_physical(vma);
799 case UIO_MEM_LOGICAL:
800 case UIO_MEM_VIRTUAL:
801 ret = uio_mmap_logical(vma);
808 mutex_unlock(&idev->info_lock);
812 static const struct file_operations uio_fops = {
813 .owner = THIS_MODULE,
815 .release = uio_release,
820 .fasync = uio_fasync,
821 .llseek = noop_llseek,
824 static int uio_major_init(void)
826 static const char name[] = "uio";
827 struct cdev *cdev = NULL;
831 result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
840 cdev->owner = THIS_MODULE;
841 cdev->ops = &uio_fops;
842 kobject_set_name(&cdev->kobj, "%s", name);
844 result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
848 uio_major = MAJOR(uio_dev);
852 kobject_put(&cdev->kobj);
854 unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
859 static void uio_major_cleanup(void)
861 unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
865 static int init_uio_class(void)
869 /* This is the first time in here, set everything up properly */
870 ret = uio_major_init();
874 ret = class_register(&uio_class);
876 printk(KERN_ERR "class_register failed for uio\n");
877 goto err_class_register;
887 static void release_uio_class(void)
889 class_unregister(&uio_class);
893 static void uio_device_release(struct device *dev)
895 struct uio_device *idev = dev_get_drvdata(dev);
901 * uio_register_device - register a new userspace IO device
902 * @owner: module that creates the new device
903 * @parent: parent device
904 * @info: UIO device capabilities
906 * returns zero on success or a negative error code.
908 int __uio_register_device(struct module *owner,
909 struct device *parent,
910 struct uio_info *info)
912 struct uio_device *idev;
915 if (!parent || !info || !info->name || !info->version)
918 info->uio_dev = NULL;
920 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
927 mutex_init(&idev->info_lock);
928 init_waitqueue_head(&idev->wait);
929 atomic_set(&idev->event, 0);
931 ret = uio_get_minor(idev);
935 idev->dev.devt = MKDEV(uio_major, idev->minor);
936 idev->dev.class = &uio_class;
937 idev->dev.parent = parent;
938 idev->dev.release = uio_device_release;
939 dev_set_drvdata(&idev->dev, idev);
941 ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
943 goto err_device_create;
945 ret = device_register(&idev->dev);
947 goto err_device_create;
949 ret = uio_dev_add_attributes(idev);
951 goto err_uio_dev_add_attributes;
953 if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
955 * Note that we deliberately don't use devm_request_irq
956 * here. The parent module can unregister the UIO device
957 * and call pci_disable_msi, which requires that this
958 * irq has been freed. However, the device may have open
959 * FDs at the time of unregister and therefore may not be
960 * freed until they are released.
962 ret = request_irq(info->irq, uio_interrupt,
963 info->irq_flags, info->name, idev);
965 goto err_request_irq;
968 info->uio_dev = idev;
972 uio_dev_del_attributes(idev);
973 err_uio_dev_add_attributes:
974 device_unregister(&idev->dev);
976 uio_free_minor(idev);
979 EXPORT_SYMBOL_GPL(__uio_register_device);
982 * uio_unregister_device - unregister a industrial IO device
983 * @info: UIO device capabilities
986 void uio_unregister_device(struct uio_info *info)
988 struct uio_device *idev;
990 if (!info || !info->uio_dev)
993 idev = info->uio_dev;
995 uio_free_minor(idev);
997 mutex_lock(&idev->info_lock);
998 uio_dev_del_attributes(idev);
1000 if (info->irq && info->irq != UIO_IRQ_CUSTOM)
1001 free_irq(info->irq, idev);
1004 mutex_unlock(&idev->info_lock);
1006 device_unregister(&idev->dev);
1010 EXPORT_SYMBOL_GPL(uio_unregister_device);
1012 static int __init uio_init(void)
1014 return init_uio_class();
1017 static void __exit uio_exit(void)
1019 release_uio_class();
1020 idr_destroy(&uio_idr);
1023 module_init(uio_init)
1024 module_exit(uio_exit)
1025 MODULE_LICENSE("GPL v2");