0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/module.h>
0016 #include <linux/init.h>
0017 #include <linux/poll.h>
0018 #include <linux/device.h>
0019 #include <linux/slab.h>
0020 #include <linux/mm.h>
0021 #include <linux/idr.h>
0022 #include <linux/sched/signal.h>
0023 #include <linux/string.h>
0024 #include <linux/kobject.h>
0025 #include <linux/cdev.h>
0026 #include <linux/uio_driver.h>
0027
0028 #define UIO_MAX_DEVICES (1U << MINORBITS)
0029
0030 static int uio_major;
0031 static struct cdev *uio_cdev;
0032 static DEFINE_IDR(uio_idr);
0033 static const struct file_operations uio_fops;
0034
0035
0036 static DEFINE_MUTEX(minor_lock);
0037
0038
0039
0040
0041
0042 struct uio_map {
0043 struct kobject kobj;
0044 struct uio_mem *mem;
0045 };
0046 #define to_map(map) container_of(map, struct uio_map, kobj)
0047
0048 static ssize_t map_name_show(struct uio_mem *mem, char *buf)
0049 {
0050 if (unlikely(!mem->name))
0051 mem->name = "";
0052
0053 return sprintf(buf, "%s\n", mem->name);
0054 }
0055
0056 static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
0057 {
0058 return sprintf(buf, "%pa\n", &mem->addr);
0059 }
0060
0061 static ssize_t map_size_show(struct uio_mem *mem, char *buf)
0062 {
0063 return sprintf(buf, "%pa\n", &mem->size);
0064 }
0065
0066 static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
0067 {
0068 return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs);
0069 }
0070
0071 struct map_sysfs_entry {
0072 struct attribute attr;
0073 ssize_t (*show)(struct uio_mem *, char *);
0074 ssize_t (*store)(struct uio_mem *, const char *, size_t);
0075 };
0076
0077 static struct map_sysfs_entry name_attribute =
0078 __ATTR(name, S_IRUGO, map_name_show, NULL);
0079 static struct map_sysfs_entry addr_attribute =
0080 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
0081 static struct map_sysfs_entry size_attribute =
0082 __ATTR(size, S_IRUGO, map_size_show, NULL);
0083 static struct map_sysfs_entry offset_attribute =
0084 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
0085
0086 static struct attribute *map_attrs[] = {
0087 &name_attribute.attr,
0088 &addr_attribute.attr,
0089 &size_attribute.attr,
0090 &offset_attribute.attr,
0091 NULL,
0092 };
0093 ATTRIBUTE_GROUPS(map);
0094
0095 static void map_release(struct kobject *kobj)
0096 {
0097 struct uio_map *map = to_map(kobj);
0098 kfree(map);
0099 }
0100
0101 static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
0102 char *buf)
0103 {
0104 struct uio_map *map = to_map(kobj);
0105 struct uio_mem *mem = map->mem;
0106 struct map_sysfs_entry *entry;
0107
0108 entry = container_of(attr, struct map_sysfs_entry, attr);
0109
0110 if (!entry->show)
0111 return -EIO;
0112
0113 return entry->show(mem, buf);
0114 }
0115
0116 static const struct sysfs_ops map_sysfs_ops = {
0117 .show = map_type_show,
0118 };
0119
0120 static struct kobj_type map_attr_type = {
0121 .release = map_release,
0122 .sysfs_ops = &map_sysfs_ops,
0123 .default_groups = map_groups,
0124 };
0125
0126 struct uio_portio {
0127 struct kobject kobj;
0128 struct uio_port *port;
0129 };
0130 #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
0131
0132 static ssize_t portio_name_show(struct uio_port *port, char *buf)
0133 {
0134 if (unlikely(!port->name))
0135 port->name = "";
0136
0137 return sprintf(buf, "%s\n", port->name);
0138 }
0139
0140 static ssize_t portio_start_show(struct uio_port *port, char *buf)
0141 {
0142 return sprintf(buf, "0x%lx\n", port->start);
0143 }
0144
0145 static ssize_t portio_size_show(struct uio_port *port, char *buf)
0146 {
0147 return sprintf(buf, "0x%lx\n", port->size);
0148 }
0149
0150 static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
0151 {
0152 const char *porttypes[] = {"none", "x86", "gpio", "other"};
0153
0154 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
0155 return -EINVAL;
0156
0157 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
0158 }
0159
0160 struct portio_sysfs_entry {
0161 struct attribute attr;
0162 ssize_t (*show)(struct uio_port *, char *);
0163 ssize_t (*store)(struct uio_port *, const char *, size_t);
0164 };
0165
0166 static struct portio_sysfs_entry portio_name_attribute =
0167 __ATTR(name, S_IRUGO, portio_name_show, NULL);
0168 static struct portio_sysfs_entry portio_start_attribute =
0169 __ATTR(start, S_IRUGO, portio_start_show, NULL);
0170 static struct portio_sysfs_entry portio_size_attribute =
0171 __ATTR(size, S_IRUGO, portio_size_show, NULL);
0172 static struct portio_sysfs_entry portio_porttype_attribute =
0173 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
0174
0175 static struct attribute *portio_attrs[] = {
0176 &portio_name_attribute.attr,
0177 &portio_start_attribute.attr,
0178 &portio_size_attribute.attr,
0179 &portio_porttype_attribute.attr,
0180 NULL,
0181 };
0182 ATTRIBUTE_GROUPS(portio);
0183
0184 static void portio_release(struct kobject *kobj)
0185 {
0186 struct uio_portio *portio = to_portio(kobj);
0187 kfree(portio);
0188 }
0189
0190 static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
0191 char *buf)
0192 {
0193 struct uio_portio *portio = to_portio(kobj);
0194 struct uio_port *port = portio->port;
0195 struct portio_sysfs_entry *entry;
0196
0197 entry = container_of(attr, struct portio_sysfs_entry, attr);
0198
0199 if (!entry->show)
0200 return -EIO;
0201
0202 return entry->show(port, buf);
0203 }
0204
0205 static const struct sysfs_ops portio_sysfs_ops = {
0206 .show = portio_type_show,
0207 };
0208
0209 static struct kobj_type portio_attr_type = {
0210 .release = portio_release,
0211 .sysfs_ops = &portio_sysfs_ops,
0212 .default_groups = portio_groups,
0213 };
0214
0215 static ssize_t name_show(struct device *dev,
0216 struct device_attribute *attr, char *buf)
0217 {
0218 struct uio_device *idev = dev_get_drvdata(dev);
0219 int ret;
0220
0221 mutex_lock(&idev->info_lock);
0222 if (!idev->info) {
0223 ret = -EINVAL;
0224 dev_err(dev, "the device has been unregistered\n");
0225 goto out;
0226 }
0227
0228 ret = sprintf(buf, "%s\n", idev->info->name);
0229
0230 out:
0231 mutex_unlock(&idev->info_lock);
0232 return ret;
0233 }
0234 static DEVICE_ATTR_RO(name);
0235
0236 static ssize_t version_show(struct device *dev,
0237 struct device_attribute *attr, char *buf)
0238 {
0239 struct uio_device *idev = dev_get_drvdata(dev);
0240 int ret;
0241
0242 mutex_lock(&idev->info_lock);
0243 if (!idev->info) {
0244 ret = -EINVAL;
0245 dev_err(dev, "the device has been unregistered\n");
0246 goto out;
0247 }
0248
0249 ret = sprintf(buf, "%s\n", idev->info->version);
0250
0251 out:
0252 mutex_unlock(&idev->info_lock);
0253 return ret;
0254 }
0255 static DEVICE_ATTR_RO(version);
0256
0257 static ssize_t event_show(struct device *dev,
0258 struct device_attribute *attr, char *buf)
0259 {
0260 struct uio_device *idev = dev_get_drvdata(dev);
0261 return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
0262 }
0263 static DEVICE_ATTR_RO(event);
0264
0265 static struct attribute *uio_attrs[] = {
0266 &dev_attr_name.attr,
0267 &dev_attr_version.attr,
0268 &dev_attr_event.attr,
0269 NULL,
0270 };
0271 ATTRIBUTE_GROUPS(uio);
0272
0273
0274 static struct class uio_class = {
0275 .name = "uio",
0276 .dev_groups = uio_groups,
0277 };
0278
0279 static bool uio_class_registered;
0280
0281
0282
0283
0284 static int uio_dev_add_attributes(struct uio_device *idev)
0285 {
0286 int ret;
0287 int mi, pi;
0288 int map_found = 0;
0289 int portio_found = 0;
0290 struct uio_mem *mem;
0291 struct uio_map *map;
0292 struct uio_port *port;
0293 struct uio_portio *portio;
0294
0295 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
0296 mem = &idev->info->mem[mi];
0297 if (mem->size == 0)
0298 break;
0299 if (!map_found) {
0300 map_found = 1;
0301 idev->map_dir = kobject_create_and_add("maps",
0302 &idev->dev.kobj);
0303 if (!idev->map_dir) {
0304 ret = -ENOMEM;
0305 goto err_map;
0306 }
0307 }
0308 map = kzalloc(sizeof(*map), GFP_KERNEL);
0309 if (!map) {
0310 ret = -ENOMEM;
0311 goto err_map;
0312 }
0313 kobject_init(&map->kobj, &map_attr_type);
0314 map->mem = mem;
0315 mem->map = map;
0316 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
0317 if (ret)
0318 goto err_map_kobj;
0319 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
0320 if (ret)
0321 goto err_map_kobj;
0322 }
0323
0324 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
0325 port = &idev->info->port[pi];
0326 if (port->size == 0)
0327 break;
0328 if (!portio_found) {
0329 portio_found = 1;
0330 idev->portio_dir = kobject_create_and_add("portio",
0331 &idev->dev.kobj);
0332 if (!idev->portio_dir) {
0333 ret = -ENOMEM;
0334 goto err_portio;
0335 }
0336 }
0337 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
0338 if (!portio) {
0339 ret = -ENOMEM;
0340 goto err_portio;
0341 }
0342 kobject_init(&portio->kobj, &portio_attr_type);
0343 portio->port = port;
0344 port->portio = portio;
0345 ret = kobject_add(&portio->kobj, idev->portio_dir,
0346 "port%d", pi);
0347 if (ret)
0348 goto err_portio_kobj;
0349 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
0350 if (ret)
0351 goto err_portio_kobj;
0352 }
0353
0354 return 0;
0355
0356 err_portio:
0357 pi--;
0358 err_portio_kobj:
0359 for (; pi >= 0; pi--) {
0360 port = &idev->info->port[pi];
0361 portio = port->portio;
0362 kobject_put(&portio->kobj);
0363 }
0364 kobject_put(idev->portio_dir);
0365 err_map:
0366 mi--;
0367 err_map_kobj:
0368 for (; mi >= 0; mi--) {
0369 mem = &idev->info->mem[mi];
0370 map = mem->map;
0371 kobject_put(&map->kobj);
0372 }
0373 kobject_put(idev->map_dir);
0374 dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
0375 return ret;
0376 }
0377
0378 static void uio_dev_del_attributes(struct uio_device *idev)
0379 {
0380 int i;
0381 struct uio_mem *mem;
0382 struct uio_port *port;
0383
0384 for (i = 0; i < MAX_UIO_MAPS; i++) {
0385 mem = &idev->info->mem[i];
0386 if (mem->size == 0)
0387 break;
0388 kobject_put(&mem->map->kobj);
0389 }
0390 kobject_put(idev->map_dir);
0391
0392 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
0393 port = &idev->info->port[i];
0394 if (port->size == 0)
0395 break;
0396 kobject_put(&port->portio->kobj);
0397 }
0398 kobject_put(idev->portio_dir);
0399 }
0400
0401 static int uio_get_minor(struct uio_device *idev)
0402 {
0403 int retval;
0404
0405 mutex_lock(&minor_lock);
0406 retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
0407 if (retval >= 0) {
0408 idev->minor = retval;
0409 retval = 0;
0410 } else if (retval == -ENOSPC) {
0411 dev_err(&idev->dev, "too many uio devices\n");
0412 retval = -EINVAL;
0413 }
0414 mutex_unlock(&minor_lock);
0415 return retval;
0416 }
0417
0418 static void uio_free_minor(unsigned long minor)
0419 {
0420 mutex_lock(&minor_lock);
0421 idr_remove(&uio_idr, minor);
0422 mutex_unlock(&minor_lock);
0423 }
0424
0425
0426
0427
0428
0429 void uio_event_notify(struct uio_info *info)
0430 {
0431 struct uio_device *idev = info->uio_dev;
0432
0433 atomic_inc(&idev->event);
0434 wake_up_interruptible(&idev->wait);
0435 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
0436 }
0437 EXPORT_SYMBOL_GPL(uio_event_notify);
0438
0439
0440
0441
0442
0443
0444 static irqreturn_t uio_interrupt(int irq, void *dev_id)
0445 {
0446 struct uio_device *idev = (struct uio_device *)dev_id;
0447 irqreturn_t ret;
0448
0449 ret = idev->info->handler(irq, idev->info);
0450 if (ret == IRQ_HANDLED)
0451 uio_event_notify(idev->info);
0452
0453 return ret;
0454 }
0455
0456 struct uio_listener {
0457 struct uio_device *dev;
0458 s32 event_count;
0459 };
0460
0461 static int uio_open(struct inode *inode, struct file *filep)
0462 {
0463 struct uio_device *idev;
0464 struct uio_listener *listener;
0465 int ret = 0;
0466
0467 mutex_lock(&minor_lock);
0468 idev = idr_find(&uio_idr, iminor(inode));
0469 mutex_unlock(&minor_lock);
0470 if (!idev) {
0471 ret = -ENODEV;
0472 goto out;
0473 }
0474
0475 get_device(&idev->dev);
0476
0477 if (!try_module_get(idev->owner)) {
0478 ret = -ENODEV;
0479 goto err_module_get;
0480 }
0481
0482 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
0483 if (!listener) {
0484 ret = -ENOMEM;
0485 goto err_alloc_listener;
0486 }
0487
0488 listener->dev = idev;
0489 listener->event_count = atomic_read(&idev->event);
0490 filep->private_data = listener;
0491
0492 mutex_lock(&idev->info_lock);
0493 if (!idev->info) {
0494 mutex_unlock(&idev->info_lock);
0495 ret = -EINVAL;
0496 goto err_infoopen;
0497 }
0498
0499 if (idev->info->open)
0500 ret = idev->info->open(idev->info, inode);
0501 mutex_unlock(&idev->info_lock);
0502 if (ret)
0503 goto err_infoopen;
0504
0505 return 0;
0506
0507 err_infoopen:
0508 kfree(listener);
0509
0510 err_alloc_listener:
0511 module_put(idev->owner);
0512
0513 err_module_get:
0514 put_device(&idev->dev);
0515
0516 out:
0517 return ret;
0518 }
0519
0520 static int uio_fasync(int fd, struct file *filep, int on)
0521 {
0522 struct uio_listener *listener = filep->private_data;
0523 struct uio_device *idev = listener->dev;
0524
0525 return fasync_helper(fd, filep, on, &idev->async_queue);
0526 }
0527
0528 static int uio_release(struct inode *inode, struct file *filep)
0529 {
0530 int ret = 0;
0531 struct uio_listener *listener = filep->private_data;
0532 struct uio_device *idev = listener->dev;
0533
0534 mutex_lock(&idev->info_lock);
0535 if (idev->info && idev->info->release)
0536 ret = idev->info->release(idev->info, inode);
0537 mutex_unlock(&idev->info_lock);
0538
0539 module_put(idev->owner);
0540 kfree(listener);
0541 put_device(&idev->dev);
0542 return ret;
0543 }
0544
0545 static __poll_t uio_poll(struct file *filep, poll_table *wait)
0546 {
0547 struct uio_listener *listener = filep->private_data;
0548 struct uio_device *idev = listener->dev;
0549 __poll_t ret = 0;
0550
0551 mutex_lock(&idev->info_lock);
0552 if (!idev->info || !idev->info->irq)
0553 ret = -EIO;
0554 mutex_unlock(&idev->info_lock);
0555
0556 if (ret)
0557 return ret;
0558
0559 poll_wait(filep, &idev->wait, wait);
0560 if (listener->event_count != atomic_read(&idev->event))
0561 return EPOLLIN | EPOLLRDNORM;
0562 return 0;
0563 }
0564
0565 static ssize_t uio_read(struct file *filep, char __user *buf,
0566 size_t count, loff_t *ppos)
0567 {
0568 struct uio_listener *listener = filep->private_data;
0569 struct uio_device *idev = listener->dev;
0570 DECLARE_WAITQUEUE(wait, current);
0571 ssize_t retval = 0;
0572 s32 event_count;
0573
0574 if (count != sizeof(s32))
0575 return -EINVAL;
0576
0577 add_wait_queue(&idev->wait, &wait);
0578
0579 do {
0580 mutex_lock(&idev->info_lock);
0581 if (!idev->info || !idev->info->irq) {
0582 retval = -EIO;
0583 mutex_unlock(&idev->info_lock);
0584 break;
0585 }
0586 mutex_unlock(&idev->info_lock);
0587
0588 set_current_state(TASK_INTERRUPTIBLE);
0589
0590 event_count = atomic_read(&idev->event);
0591 if (event_count != listener->event_count) {
0592 __set_current_state(TASK_RUNNING);
0593 if (copy_to_user(buf, &event_count, count))
0594 retval = -EFAULT;
0595 else {
0596 listener->event_count = event_count;
0597 retval = count;
0598 }
0599 break;
0600 }
0601
0602 if (filep->f_flags & O_NONBLOCK) {
0603 retval = -EAGAIN;
0604 break;
0605 }
0606
0607 if (signal_pending(current)) {
0608 retval = -ERESTARTSYS;
0609 break;
0610 }
0611 schedule();
0612 } while (1);
0613
0614 __set_current_state(TASK_RUNNING);
0615 remove_wait_queue(&idev->wait, &wait);
0616
0617 return retval;
0618 }
0619
0620 static ssize_t uio_write(struct file *filep, const char __user *buf,
0621 size_t count, loff_t *ppos)
0622 {
0623 struct uio_listener *listener = filep->private_data;
0624 struct uio_device *idev = listener->dev;
0625 ssize_t retval;
0626 s32 irq_on;
0627
0628 if (count != sizeof(s32))
0629 return -EINVAL;
0630
0631 if (copy_from_user(&irq_on, buf, count))
0632 return -EFAULT;
0633
0634 mutex_lock(&idev->info_lock);
0635 if (!idev->info) {
0636 retval = -EINVAL;
0637 goto out;
0638 }
0639
0640 if (!idev->info->irq) {
0641 retval = -EIO;
0642 goto out;
0643 }
0644
0645 if (!idev->info->irqcontrol) {
0646 retval = -ENOSYS;
0647 goto out;
0648 }
0649
0650 retval = idev->info->irqcontrol(idev->info, irq_on);
0651
0652 out:
0653 mutex_unlock(&idev->info_lock);
0654 return retval ? retval : sizeof(s32);
0655 }
0656
0657 static int uio_find_mem_index(struct vm_area_struct *vma)
0658 {
0659 struct uio_device *idev = vma->vm_private_data;
0660
0661 if (vma->vm_pgoff < MAX_UIO_MAPS) {
0662 if (idev->info->mem[vma->vm_pgoff].size == 0)
0663 return -1;
0664 return (int)vma->vm_pgoff;
0665 }
0666 return -1;
0667 }
0668
0669 static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
0670 {
0671 struct uio_device *idev = vmf->vma->vm_private_data;
0672 struct page *page;
0673 unsigned long offset;
0674 void *addr;
0675 vm_fault_t ret = 0;
0676 int mi;
0677
0678 mutex_lock(&idev->info_lock);
0679 if (!idev->info) {
0680 ret = VM_FAULT_SIGBUS;
0681 goto out;
0682 }
0683
0684 mi = uio_find_mem_index(vmf->vma);
0685 if (mi < 0) {
0686 ret = VM_FAULT_SIGBUS;
0687 goto out;
0688 }
0689
0690
0691
0692
0693
0694 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
0695
0696 addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
0697 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
0698 page = virt_to_page(addr);
0699 else
0700 page = vmalloc_to_page(addr);
0701 get_page(page);
0702 vmf->page = page;
0703
0704 out:
0705 mutex_unlock(&idev->info_lock);
0706
0707 return ret;
0708 }
0709
0710 static const struct vm_operations_struct uio_logical_vm_ops = {
0711 .fault = uio_vma_fault,
0712 };
0713
0714 static int uio_mmap_logical(struct vm_area_struct *vma)
0715 {
0716 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
0717 vma->vm_ops = &uio_logical_vm_ops;
0718 return 0;
0719 }
0720
0721 static const struct vm_operations_struct uio_physical_vm_ops = {
0722 #ifdef CONFIG_HAVE_IOREMAP_PROT
0723 .access = generic_access_phys,
0724 #endif
0725 };
0726
0727 static int uio_mmap_physical(struct vm_area_struct *vma)
0728 {
0729 struct uio_device *idev = vma->vm_private_data;
0730 int mi = uio_find_mem_index(vma);
0731 struct uio_mem *mem;
0732
0733 if (mi < 0)
0734 return -EINVAL;
0735 mem = idev->info->mem + mi;
0736
0737 if (mem->addr & ~PAGE_MASK)
0738 return -ENODEV;
0739 if (vma->vm_end - vma->vm_start > mem->size)
0740 return -EINVAL;
0741
0742 vma->vm_ops = &uio_physical_vm_ops;
0743 if (idev->info->mem[mi].memtype == UIO_MEM_PHYS)
0744 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755 return remap_pfn_range(vma,
0756 vma->vm_start,
0757 mem->addr >> PAGE_SHIFT,
0758 vma->vm_end - vma->vm_start,
0759 vma->vm_page_prot);
0760 }
0761
0762 static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
0763 {
0764 struct uio_listener *listener = filep->private_data;
0765 struct uio_device *idev = listener->dev;
0766 int mi;
0767 unsigned long requested_pages, actual_pages;
0768 int ret = 0;
0769
0770 if (vma->vm_end < vma->vm_start)
0771 return -EINVAL;
0772
0773 vma->vm_private_data = idev;
0774
0775 mutex_lock(&idev->info_lock);
0776 if (!idev->info) {
0777 ret = -EINVAL;
0778 goto out;
0779 }
0780
0781 mi = uio_find_mem_index(vma);
0782 if (mi < 0) {
0783 ret = -EINVAL;
0784 goto out;
0785 }
0786
0787 requested_pages = vma_pages(vma);
0788 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
0789 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
0790 if (requested_pages > actual_pages) {
0791 ret = -EINVAL;
0792 goto out;
0793 }
0794
0795 if (idev->info->mmap) {
0796 ret = idev->info->mmap(idev->info, vma);
0797 goto out;
0798 }
0799
0800 switch (idev->info->mem[mi].memtype) {
0801 case UIO_MEM_IOVA:
0802 case UIO_MEM_PHYS:
0803 ret = uio_mmap_physical(vma);
0804 break;
0805 case UIO_MEM_LOGICAL:
0806 case UIO_MEM_VIRTUAL:
0807 ret = uio_mmap_logical(vma);
0808 break;
0809 default:
0810 ret = -EINVAL;
0811 }
0812
0813 out:
0814 mutex_unlock(&idev->info_lock);
0815 return ret;
0816 }
0817
0818 static const struct file_operations uio_fops = {
0819 .owner = THIS_MODULE,
0820 .open = uio_open,
0821 .release = uio_release,
0822 .read = uio_read,
0823 .write = uio_write,
0824 .mmap = uio_mmap,
0825 .poll = uio_poll,
0826 .fasync = uio_fasync,
0827 .llseek = noop_llseek,
0828 };
0829
0830 static int uio_major_init(void)
0831 {
0832 static const char name[] = "uio";
0833 struct cdev *cdev = NULL;
0834 dev_t uio_dev = 0;
0835 int result;
0836
0837 result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
0838 if (result)
0839 goto out;
0840
0841 result = -ENOMEM;
0842 cdev = cdev_alloc();
0843 if (!cdev)
0844 goto out_unregister;
0845
0846 cdev->owner = THIS_MODULE;
0847 cdev->ops = &uio_fops;
0848 kobject_set_name(&cdev->kobj, "%s", name);
0849
0850 result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
0851 if (result)
0852 goto out_put;
0853
0854 uio_major = MAJOR(uio_dev);
0855 uio_cdev = cdev;
0856 return 0;
0857 out_put:
0858 kobject_put(&cdev->kobj);
0859 out_unregister:
0860 unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
0861 out:
0862 return result;
0863 }
0864
0865 static void uio_major_cleanup(void)
0866 {
0867 unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
0868 cdev_del(uio_cdev);
0869 }
0870
0871 static int init_uio_class(void)
0872 {
0873 int ret;
0874
0875
0876 ret = uio_major_init();
0877 if (ret)
0878 goto exit;
0879
0880 ret = class_register(&uio_class);
0881 if (ret) {
0882 printk(KERN_ERR "class_register failed for uio\n");
0883 goto err_class_register;
0884 }
0885
0886 uio_class_registered = true;
0887
0888 return 0;
0889
0890 err_class_register:
0891 uio_major_cleanup();
0892 exit:
0893 return ret;
0894 }
0895
0896 static void release_uio_class(void)
0897 {
0898 uio_class_registered = false;
0899 class_unregister(&uio_class);
0900 uio_major_cleanup();
0901 }
0902
0903 static void uio_device_release(struct device *dev)
0904 {
0905 struct uio_device *idev = dev_get_drvdata(dev);
0906
0907 kfree(idev);
0908 }
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918 int __uio_register_device(struct module *owner,
0919 struct device *parent,
0920 struct uio_info *info)
0921 {
0922 struct uio_device *idev;
0923 int ret = 0;
0924
0925 if (!uio_class_registered)
0926 return -EPROBE_DEFER;
0927
0928 if (!parent || !info || !info->name || !info->version)
0929 return -EINVAL;
0930
0931 info->uio_dev = NULL;
0932
0933 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
0934 if (!idev) {
0935 return -ENOMEM;
0936 }
0937
0938 idev->owner = owner;
0939 idev->info = info;
0940 mutex_init(&idev->info_lock);
0941 init_waitqueue_head(&idev->wait);
0942 atomic_set(&idev->event, 0);
0943
0944 ret = uio_get_minor(idev);
0945 if (ret) {
0946 kfree(idev);
0947 return ret;
0948 }
0949
0950 device_initialize(&idev->dev);
0951 idev->dev.devt = MKDEV(uio_major, idev->minor);
0952 idev->dev.class = &uio_class;
0953 idev->dev.parent = parent;
0954 idev->dev.release = uio_device_release;
0955 dev_set_drvdata(&idev->dev, idev);
0956
0957 ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
0958 if (ret)
0959 goto err_device_create;
0960
0961 ret = device_add(&idev->dev);
0962 if (ret)
0963 goto err_device_create;
0964
0965 ret = uio_dev_add_attributes(idev);
0966 if (ret)
0967 goto err_uio_dev_add_attributes;
0968
0969 info->uio_dev = idev;
0970
0971 if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
0972
0973
0974
0975
0976
0977
0978
0979
0980 ret = request_irq(info->irq, uio_interrupt,
0981 info->irq_flags, info->name, idev);
0982 if (ret) {
0983 info->uio_dev = NULL;
0984 goto err_request_irq;
0985 }
0986 }
0987
0988 return 0;
0989
0990 err_request_irq:
0991 uio_dev_del_attributes(idev);
0992 err_uio_dev_add_attributes:
0993 device_del(&idev->dev);
0994 err_device_create:
0995 uio_free_minor(idev->minor);
0996 put_device(&idev->dev);
0997 return ret;
0998 }
0999 EXPORT_SYMBOL_GPL(__uio_register_device);
1000
1001 static void devm_uio_unregister_device(struct device *dev, void *res)
1002 {
1003 uio_unregister_device(*(struct uio_info **)res);
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014 int __devm_uio_register_device(struct module *owner,
1015 struct device *parent,
1016 struct uio_info *info)
1017 {
1018 struct uio_info **ptr;
1019 int ret;
1020
1021 ptr = devres_alloc(devm_uio_unregister_device, sizeof(*ptr),
1022 GFP_KERNEL);
1023 if (!ptr)
1024 return -ENOMEM;
1025
1026 *ptr = info;
1027 ret = __uio_register_device(owner, parent, info);
1028 if (ret) {
1029 devres_free(ptr);
1030 return ret;
1031 }
1032
1033 devres_add(parent, ptr);
1034
1035 return 0;
1036 }
1037 EXPORT_SYMBOL_GPL(__devm_uio_register_device);
1038
1039
1040
1041
1042
1043
1044 void uio_unregister_device(struct uio_info *info)
1045 {
1046 struct uio_device *idev;
1047 unsigned long minor;
1048
1049 if (!info || !info->uio_dev)
1050 return;
1051
1052 idev = info->uio_dev;
1053 minor = idev->minor;
1054
1055 mutex_lock(&idev->info_lock);
1056 uio_dev_del_attributes(idev);
1057
1058 if (info->irq && info->irq != UIO_IRQ_CUSTOM)
1059 free_irq(info->irq, idev);
1060
1061 idev->info = NULL;
1062 mutex_unlock(&idev->info_lock);
1063
1064 wake_up_interruptible(&idev->wait);
1065 kill_fasync(&idev->async_queue, SIGIO, POLL_HUP);
1066
1067 device_unregister(&idev->dev);
1068
1069 uio_free_minor(minor);
1070
1071 return;
1072 }
1073 EXPORT_SYMBOL_GPL(uio_unregister_device);
1074
1075 static int __init uio_init(void)
1076 {
1077 return init_uio_class();
1078 }
1079
1080 static void __exit uio_exit(void)
1081 {
1082 release_uio_class();
1083 idr_destroy(&uio_idr);
1084 }
1085
1086 module_init(uio_init)
1087 module_exit(uio_exit)
1088 MODULE_LICENSE("GPL v2");