16 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/string.h>
29 #define UIO_MAX_DEVICES (1U << MINORBITS)
45 static struct cdev *uio_cdev;
60 #define to_map(map) container_of(map, struct uio_map, kobj)
72 return sprintf(buf,
"0x%llx\n", (
unsigned long long)mem->
addr);
101 &name_attribute.
attr,
102 &addr_attribute.
attr,
103 &size_attribute.
attr,
104 &offset_attribute.
attr,
108 static void map_release(
struct kobject *kobj)
126 return entry->
show(mem, buf);
129 static const struct sysfs_ops map_sysfs_ops = {
130 .show = map_type_show,
133 static struct kobj_type map_attr_type = {
134 .release = map_release,
135 .sysfs_ops = &map_sysfs_ops,
136 .default_attrs = attrs,
143 #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
165 const char *porttypes[] = {
"none",
"x86",
"gpio",
"other"};
188 static struct attribute *portio_attrs[] = {
189 &portio_name_attribute.
attr,
190 &portio_start_attribute.
attr,
191 &portio_size_attribute.
attr,
192 &portio_porttype_attribute.
attr,
196 static void portio_release(
struct kobject *kobj)
214 return entry->
show(port, buf);
217 static const struct sysfs_ops portio_sysfs_ops = {
218 .show = portio_type_show,
221 static struct kobj_type portio_attr_type = {
222 .release = portio_release,
223 .sysfs_ops = &portio_sysfs_ops,
224 .default_attrs = portio_attrs,
256 static struct class uio_class = {
258 .dev_attrs = uio_class_attributes,
264 static int uio_dev_add_attributes(
struct uio_device *idev)
269 int portio_found = 0;
276 mem = &idev->
info->mem[mi];
301 port = &idev->
info->port[pi];
311 portio = kzalloc(
sizeof(*portio),
GFP_KERNEL);
329 for (pi--; pi >= 0; pi--) {
330 port = &idev->
info->port[pi];
336 for (mi--; mi>=0; mi--) {
337 mem = &idev->
info->mem[mi];
342 dev_err(idev->
dev,
"error creating sysfs files (%d)\n", ret);
346 static void uio_dev_del_attributes(
struct uio_device *idev)
353 mem = &idev->
info->mem[
i];
361 port = &idev->
info->port[
i];
369 static int uio_get_minor(
struct uio_device *idev)
396 static void uio_free_minor(
struct uio_device *idev)
445 idev =
idr_find(&uio_idr, iminor(inode));
452 if (!try_module_get(idev->
owner)) {
460 goto err_alloc_listener;
467 if (idev->
info->open) {
468 ret = idev->
info->open(idev->
info, inode);
478 module_put(idev->
owner);
484 static int uio_fasync(
int fd,
struct file *filep,
int on)
492 static int uio_release(
struct inode *inode,
struct file *filep)
498 if (idev->
info->release)
499 ret = idev->
info->release(idev->
info, inode);
501 module_put(idev->
owner);
511 if (!idev->
info->irq)
514 poll_wait(filep, &idev->
wait, wait);
520 static ssize_t uio_read(
struct file *filep,
char __user *buf,
521 size_t count, loff_t *ppos)
529 if (!idev->
info->irq)
532 if (count !=
sizeof(
s32))
569 static ssize_t uio_write(
struct file *filep,
const char __user *buf,
570 size_t count, loff_t *ppos)
577 if (!idev->
info->irq)
580 if (count !=
sizeof(
s32))
583 if (!idev->
info->irqcontrol)
589 retval = idev->
info->irqcontrol(idev->
info, irq_on);
591 return retval ? retval :
sizeof(
s32);
618 static int uio_vma_fault(
struct vm_area_struct *vma,
struct vm_fault *vmf)
624 int mi = uio_find_mem_index(vma);
626 return VM_FAULT_SIGBUS;
643 static const struct vm_operations_struct uio_vm_ops = {
644 .open = uio_vma_open,
645 .close = uio_vma_close,
646 .fault = uio_vma_fault,
652 int mi = uio_find_mem_index(vma);
667 vma->
vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
668 vma->
vm_ops = &uio_vm_ops;
678 unsigned long requested_pages, actual_pages;
686 mi = uio_find_mem_index(vma);
693 if (requested_pages > actual_pages)
696 if (idev->
info->mmap) {
697 ret = idev->
info->mmap(idev->
info, vma);
701 switch (idev->
info->mem[mi].memtype) {
703 return uio_mmap_physical(vma);
706 return uio_mmap_logical(vma);
715 .release = uio_release,
720 .fasync = uio_fasync,
724 static int uio_major_init(
void)
726 static const char name[] =
"uio";
741 cdev->
ops = &uio_fops;
748 uio_major =
MAJOR(uio_dev);
759 static void uio_major_cleanup(
void)
765 static int init_uio_class(
void)
770 ret = uio_major_init();
777 goto err_class_register;
787 static void release_uio_class(
void)
808 if (!parent || !info || !info->
name || !info->
version)
824 ret = uio_get_minor(idev);
830 "uio%d", idev->
minor);
831 if (IS_ERR(idev->
dev)) {
833 ret = PTR_ERR(idev->
dev);
834 goto err_device_create;
837 ret = uio_dev_add_attributes(idev);
839 goto err_uio_dev_add_attributes;
847 goto err_request_irq;
853 uio_dev_del_attributes(idev);
854 err_uio_dev_add_attributes:
857 uio_free_minor(idev);
879 uio_free_minor(idev);
884 uio_dev_del_attributes(idev);
893 static int __init uio_init(
void)
895 return init_uio_class();
898 static void __exit uio_exit(
void)