1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/uio/uio.c 4 * 5 * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de> 6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de> 7 * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de> 8 * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com> 9 * 10 * Userspace IO 11 * 12 * Base Functions 13 */ 14 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> 20 #include <linux/mm.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> 27 #include <linux/dma-mapping.h> 28 29 #define UIO_MAX_DEVICES (1U << MINORBITS) 30 31 static int uio_major; 32 static struct cdev *uio_cdev; 33 static DEFINE_IDR(uio_idr); 34 static const struct file_operations uio_fops; 35 36 /* Protect idr accesses */ 37 static DEFINE_MUTEX(minor_lock); 38 39 /* 40 * attributes 41 */ 42 43 struct uio_map { 44 struct kobject kobj; 45 struct uio_mem *mem; 46 }; 47 #define to_map(map) container_of(map, struct uio_map, kobj) 48 49 static ssize_t map_name_show(struct uio_mem *mem, char *buf) 50 { 51 if (unlikely(!mem->name)) 52 mem->name = ""; 53 54 return sprintf(buf, "%s\n", mem->name); 55 } 56 57 static ssize_t map_addr_show(struct uio_mem *mem, char *buf) 58 { 59 return sprintf(buf, "%pa\n", &mem->addr); 60 } 61 62 static ssize_t map_size_show(struct uio_mem *mem, char *buf) 63 { 64 return sprintf(buf, "%pa\n", &mem->size); 65 } 66 67 static ssize_t map_offset_show(struct uio_mem *mem, char *buf) 68 { 69 return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs); 70 } 71 72 struct map_sysfs_entry { 73 struct attribute attr; 74 ssize_t (*show)(struct uio_mem *, char *); 75 ssize_t (*store)(struct uio_mem *, const char *, size_t); 76 }; 77 78 static struct map_sysfs_entry name_attribute = 79 __ATTR(name, S_IRUGO, map_name_show, NULL); 80 static struct map_sysfs_entry addr_attribute = 81 __ATTR(addr, S_IRUGO, map_addr_show, NULL); 82 static struct map_sysfs_entry size_attribute = 83 __ATTR(size, S_IRUGO, map_size_show, NULL); 84 static struct map_sysfs_entry offset_attribute = 85 __ATTR(offset, S_IRUGO, map_offset_show, NULL); 86 87 static struct attribute *map_attrs[] = { 88 &name_attribute.attr, 89 &addr_attribute.attr, 90 &size_attribute.attr, 91 &offset_attribute.attr, 92 NULL, /* need to NULL terminate the list of attributes */ 93 }; 94 ATTRIBUTE_GROUPS(map); 95 96 static void map_release(struct kobject *kobj) 97 { 98 struct uio_map *map = to_map(kobj); 99 kfree(map); 100 } 101 102 static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr, 103 char *buf) 104 { 105 struct uio_map *map = to_map(kobj); 106 struct uio_mem *mem = map->mem; 107 struct map_sysfs_entry *entry; 108 109 entry = container_of(attr, struct map_sysfs_entry, attr); 110 111 if (!entry->show) 112 return -EIO; 113 114 return entry->show(mem, buf); 115 } 116 117 static const struct sysfs_ops map_sysfs_ops = { 118 .show = map_type_show, 119 }; 120 121 static struct kobj_type map_attr_type = { 122 .release = map_release, 123 .sysfs_ops = &map_sysfs_ops, 124 .default_groups = map_groups, 125 }; 126 127 struct uio_portio { 128 struct kobject kobj; 129 struct uio_port *port; 130 }; 131 #define to_portio(portio) container_of(portio, struct uio_portio, kobj) 132 133 static ssize_t portio_name_show(struct uio_port *port, char *buf) 134 { 135 if (unlikely(!port->name)) 136 port->name = ""; 137 138 return sprintf(buf, "%s\n", port->name); 139 } 140 141 static ssize_t portio_start_show(struct uio_port *port, char *buf) 142 { 143 return sprintf(buf, "0x%lx\n", port->start); 144 } 145 146 static ssize_t portio_size_show(struct uio_port *port, char *buf) 147 { 148 return sprintf(buf, "0x%lx\n", port->size); 149 } 150 151 static ssize_t portio_porttype_show(struct uio_port *port, char *buf) 152 { 153 const char *porttypes[] = {"none", "x86", "gpio", "other"}; 154 155 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER)) 156 return -EINVAL; 157 158 return sprintf(buf, "port_%s\n", porttypes[port->porttype]); 159 } 160 161 struct portio_sysfs_entry { 162 struct attribute attr; 163 ssize_t (*show)(struct uio_port *, char *); 164 ssize_t (*store)(struct uio_port *, const char *, size_t); 165 }; 166 167 static struct portio_sysfs_entry portio_name_attribute = 168 __ATTR(name, S_IRUGO, portio_name_show, NULL); 169 static struct portio_sysfs_entry portio_start_attribute = 170 __ATTR(start, S_IRUGO, portio_start_show, NULL); 171 static struct portio_sysfs_entry portio_size_attribute = 172 __ATTR(size, S_IRUGO, portio_size_show, NULL); 173 static struct portio_sysfs_entry portio_porttype_attribute = 174 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL); 175 176 static struct attribute *portio_attrs[] = { 177 &portio_name_attribute.attr, 178 &portio_start_attribute.attr, 179 &portio_size_attribute.attr, 180 &portio_porttype_attribute.attr, 181 NULL, 182 }; 183 ATTRIBUTE_GROUPS(portio); 184 185 static void portio_release(struct kobject *kobj) 186 { 187 struct uio_portio *portio = to_portio(kobj); 188 kfree(portio); 189 } 190 191 static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr, 192 char *buf) 193 { 194 struct uio_portio *portio = to_portio(kobj); 195 struct uio_port *port = portio->port; 196 struct portio_sysfs_entry *entry; 197 198 entry = container_of(attr, struct portio_sysfs_entry, attr); 199 200 if (!entry->show) 201 return -EIO; 202 203 return entry->show(port, buf); 204 } 205 206 static const struct sysfs_ops portio_sysfs_ops = { 207 .show = portio_type_show, 208 }; 209 210 static struct kobj_type portio_attr_type = { 211 .release = portio_release, 212 .sysfs_ops = &portio_sysfs_ops, 213 .default_groups = portio_groups, 214 }; 215 216 static ssize_t name_show(struct device *dev, 217 struct device_attribute *attr, char *buf) 218 { 219 struct uio_device *idev = dev_get_drvdata(dev); 220 int ret; 221 222 mutex_lock(&idev->info_lock); 223 if (!idev->info) { 224 ret = -EINVAL; 225 dev_err(dev, "the device has been unregistered\n"); 226 goto out; 227 } 228 229 ret = sprintf(buf, "%s\n", idev->info->name); 230 231 out: 232 mutex_unlock(&idev->info_lock); 233 return ret; 234 } 235 static DEVICE_ATTR_RO(name); 236 237 static ssize_t version_show(struct device *dev, 238 struct device_attribute *attr, char *buf) 239 { 240 struct uio_device *idev = dev_get_drvdata(dev); 241 int ret; 242 243 mutex_lock(&idev->info_lock); 244 if (!idev->info) { 245 ret = -EINVAL; 246 dev_err(dev, "the device has been unregistered\n"); 247 goto out; 248 } 249 250 ret = sprintf(buf, "%s\n", idev->info->version); 251 252 out: 253 mutex_unlock(&idev->info_lock); 254 return ret; 255 } 256 static DEVICE_ATTR_RO(version); 257 258 static ssize_t event_show(struct device *dev, 259 struct device_attribute *attr, char *buf) 260 { 261 struct uio_device *idev = dev_get_drvdata(dev); 262 return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event)); 263 } 264 static DEVICE_ATTR_RO(event); 265 266 static struct attribute *uio_attrs[] = { 267 &dev_attr_name.attr, 268 &dev_attr_version.attr, 269 &dev_attr_event.attr, 270 NULL, 271 }; 272 ATTRIBUTE_GROUPS(uio); 273 274 /* UIO class infrastructure */ 275 static struct class uio_class = { 276 .name = "uio", 277 .dev_groups = uio_groups, 278 }; 279 280 static bool uio_class_registered; 281 282 /* 283 * device functions 284 */ 285 static int uio_dev_add_attributes(struct uio_device *idev) 286 { 287 int ret; 288 int mi, pi; 289 int map_found = 0; 290 int portio_found = 0; 291 struct uio_mem *mem; 292 struct uio_map *map; 293 struct uio_port *port; 294 struct uio_portio *portio; 295 296 for (mi = 0; mi < MAX_UIO_MAPS; mi++) { 297 mem = &idev->info->mem[mi]; 298 if (mem->size == 0) 299 break; 300 if (!map_found) { 301 map_found = 1; 302 idev->map_dir = kobject_create_and_add("maps", 303 &idev->dev.kobj); 304 if (!idev->map_dir) { 305 ret = -ENOMEM; 306 goto err_map; 307 } 308 } 309 map = kzalloc(sizeof(*map), GFP_KERNEL); 310 if (!map) { 311 ret = -ENOMEM; 312 goto err_map; 313 } 314 kobject_init(&map->kobj, &map_attr_type); 315 map->mem = mem; 316 mem->map = map; 317 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi); 318 if (ret) 319 goto err_map_kobj; 320 ret = kobject_uevent(&map->kobj, KOBJ_ADD); 321 if (ret) 322 goto err_map_kobj; 323 } 324 325 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) { 326 port = &idev->info->port[pi]; 327 if (port->size == 0) 328 break; 329 if (!portio_found) { 330 portio_found = 1; 331 idev->portio_dir = kobject_create_and_add("portio", 332 &idev->dev.kobj); 333 if (!idev->portio_dir) { 334 ret = -ENOMEM; 335 goto err_portio; 336 } 337 } 338 portio = kzalloc(sizeof(*portio), GFP_KERNEL); 339 if (!portio) { 340 ret = -ENOMEM; 341 goto err_portio; 342 } 343 kobject_init(&portio->kobj, &portio_attr_type); 344 portio->port = port; 345 port->portio = portio; 346 ret = kobject_add(&portio->kobj, idev->portio_dir, 347 "port%d", pi); 348 if (ret) 349 goto err_portio_kobj; 350 ret = kobject_uevent(&portio->kobj, KOBJ_ADD); 351 if (ret) 352 goto err_portio_kobj; 353 } 354 355 return 0; 356 357 err_portio: 358 pi--; 359 err_portio_kobj: 360 for (; pi >= 0; pi--) { 361 port = &idev->info->port[pi]; 362 portio = port->portio; 363 kobject_put(&portio->kobj); 364 } 365 kobject_put(idev->portio_dir); 366 err_map: 367 mi--; 368 err_map_kobj: 369 for (; mi >= 0; mi--) { 370 mem = &idev->info->mem[mi]; 371 map = mem->map; 372 kobject_put(&map->kobj); 373 } 374 kobject_put(idev->map_dir); 375 dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret); 376 return ret; 377 } 378 379 static void uio_dev_del_attributes(struct uio_device *idev) 380 { 381 int i; 382 struct uio_mem *mem; 383 struct uio_port *port; 384 385 for (i = 0; i < MAX_UIO_MAPS; i++) { 386 mem = &idev->info->mem[i]; 387 if (mem->size == 0) 388 break; 389 kobject_put(&mem->map->kobj); 390 } 391 kobject_put(idev->map_dir); 392 393 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) { 394 port = &idev->info->port[i]; 395 if (port->size == 0) 396 break; 397 kobject_put(&port->portio->kobj); 398 } 399 kobject_put(idev->portio_dir); 400 } 401 402 static int uio_get_minor(struct uio_device *idev) 403 { 404 int retval; 405 406 mutex_lock(&minor_lock); 407 retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL); 408 if (retval >= 0) { 409 idev->minor = retval; 410 retval = 0; 411 } else if (retval == -ENOSPC) { 412 dev_err(&idev->dev, "too many uio devices\n"); 413 retval = -EINVAL; 414 } 415 mutex_unlock(&minor_lock); 416 return retval; 417 } 418 419 static void uio_free_minor(unsigned long minor) 420 { 421 mutex_lock(&minor_lock); 422 idr_remove(&uio_idr, minor); 423 mutex_unlock(&minor_lock); 424 } 425 426 /** 427 * uio_event_notify - trigger an interrupt event 428 * @info: UIO device capabilities 429 */ 430 void uio_event_notify(struct uio_info *info) 431 { 432 struct uio_device *idev = info->uio_dev; 433 434 atomic_inc(&idev->event); 435 wake_up_interruptible(&idev->wait); 436 kill_fasync(&idev->async_queue, SIGIO, POLL_IN); 437 } 438 EXPORT_SYMBOL_GPL(uio_event_notify); 439 440 /** 441 * uio_interrupt - hardware interrupt handler 442 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer 443 * @dev_id: Pointer to the devices uio_device structure 444 */ 445 static irqreturn_t uio_interrupt_handler(int irq, void *dev_id) 446 { 447 struct uio_device *idev = (struct uio_device *)dev_id; 448 irqreturn_t ret; 449 450 ret = idev->info->handler(irq, idev->info); 451 if (ret == IRQ_HANDLED) 452 ret = IRQ_WAKE_THREAD; 453 454 return ret; 455 } 456 457 static irqreturn_t uio_interrupt_thread(int irq, void *dev_id) 458 { 459 struct uio_device *idev = (struct uio_device *)dev_id; 460 461 uio_event_notify(idev->info); 462 463 return IRQ_HANDLED; 464 } 465 466 struct uio_listener { 467 struct uio_device *dev; 468 s32 event_count; 469 }; 470 471 static int uio_open(struct inode *inode, struct file *filep) 472 { 473 struct uio_device *idev; 474 struct uio_listener *listener; 475 int ret = 0; 476 477 mutex_lock(&minor_lock); 478 idev = idr_find(&uio_idr, iminor(inode)); 479 if (!idev) { 480 ret = -ENODEV; 481 mutex_unlock(&minor_lock); 482 goto out; 483 } 484 get_device(&idev->dev); 485 mutex_unlock(&minor_lock); 486 487 if (!try_module_get(idev->owner)) { 488 ret = -ENODEV; 489 goto err_module_get; 490 } 491 492 listener = kmalloc(sizeof(*listener), GFP_KERNEL); 493 if (!listener) { 494 ret = -ENOMEM; 495 goto err_alloc_listener; 496 } 497 498 listener->dev = idev; 499 listener->event_count = atomic_read(&idev->event); 500 filep->private_data = listener; 501 502 mutex_lock(&idev->info_lock); 503 if (!idev->info) { 504 mutex_unlock(&idev->info_lock); 505 ret = -EINVAL; 506 goto err_infoopen; 507 } 508 509 if (idev->info->open) 510 ret = idev->info->open(idev->info, inode); 511 mutex_unlock(&idev->info_lock); 512 if (ret) 513 goto err_infoopen; 514 515 return 0; 516 517 err_infoopen: 518 kfree(listener); 519 520 err_alloc_listener: 521 module_put(idev->owner); 522 523 err_module_get: 524 put_device(&idev->dev); 525 526 out: 527 return ret; 528 } 529 530 static int uio_fasync(int fd, struct file *filep, int on) 531 { 532 struct uio_listener *listener = filep->private_data; 533 struct uio_device *idev = listener->dev; 534 535 return fasync_helper(fd, filep, on, &idev->async_queue); 536 } 537 538 static int uio_release(struct inode *inode, struct file *filep) 539 { 540 int ret = 0; 541 struct uio_listener *listener = filep->private_data; 542 struct uio_device *idev = listener->dev; 543 544 mutex_lock(&idev->info_lock); 545 if (idev->info && idev->info->release) 546 ret = idev->info->release(idev->info, inode); 547 mutex_unlock(&idev->info_lock); 548 549 module_put(idev->owner); 550 kfree(listener); 551 put_device(&idev->dev); 552 return ret; 553 } 554 555 static __poll_t uio_poll(struct file *filep, poll_table *wait) 556 { 557 struct uio_listener *listener = filep->private_data; 558 struct uio_device *idev = listener->dev; 559 __poll_t ret = 0; 560 561 mutex_lock(&idev->info_lock); 562 if (!idev->info || !idev->info->irq) 563 ret = -EIO; 564 mutex_unlock(&idev->info_lock); 565 566 if (ret) 567 return ret; 568 569 poll_wait(filep, &idev->wait, wait); 570 if (listener->event_count != atomic_read(&idev->event)) 571 return EPOLLIN | EPOLLRDNORM; 572 return 0; 573 } 574 575 static ssize_t uio_read(struct file *filep, char __user *buf, 576 size_t count, loff_t *ppos) 577 { 578 struct uio_listener *listener = filep->private_data; 579 struct uio_device *idev = listener->dev; 580 DECLARE_WAITQUEUE(wait, current); 581 ssize_t retval = 0; 582 s32 event_count; 583 584 if (count != sizeof(s32)) 585 return -EINVAL; 586 587 add_wait_queue(&idev->wait, &wait); 588 589 do { 590 mutex_lock(&idev->info_lock); 591 if (!idev->info || !idev->info->irq) { 592 retval = -EIO; 593 mutex_unlock(&idev->info_lock); 594 break; 595 } 596 mutex_unlock(&idev->info_lock); 597 598 set_current_state(TASK_INTERRUPTIBLE); 599 600 event_count = atomic_read(&idev->event); 601 if (event_count != listener->event_count) { 602 __set_current_state(TASK_RUNNING); 603 if (copy_to_user(buf, &event_count, count)) 604 retval = -EFAULT; 605 else { 606 listener->event_count = event_count; 607 retval = count; 608 } 609 break; 610 } 611 612 if (filep->f_flags & O_NONBLOCK) { 613 retval = -EAGAIN; 614 break; 615 } 616 617 if (signal_pending(current)) { 618 retval = -ERESTARTSYS; 619 break; 620 } 621 schedule(); 622 } while (1); 623 624 __set_current_state(TASK_RUNNING); 625 remove_wait_queue(&idev->wait, &wait); 626 627 return retval; 628 } 629 630 static ssize_t uio_write(struct file *filep, const char __user *buf, 631 size_t count, loff_t *ppos) 632 { 633 struct uio_listener *listener = filep->private_data; 634 struct uio_device *idev = listener->dev; 635 ssize_t retval; 636 s32 irq_on; 637 638 if (count != sizeof(s32)) 639 return -EINVAL; 640 641 if (copy_from_user(&irq_on, buf, count)) 642 return -EFAULT; 643 644 mutex_lock(&idev->info_lock); 645 if (!idev->info) { 646 retval = -EINVAL; 647 goto out; 648 } 649 650 if (!idev->info->irq) { 651 retval = -EIO; 652 goto out; 653 } 654 655 if (!idev->info->irqcontrol) { 656 retval = -ENOSYS; 657 goto out; 658 } 659 660 retval = idev->info->irqcontrol(idev->info, irq_on); 661 662 out: 663 mutex_unlock(&idev->info_lock); 664 return retval ? retval : sizeof(s32); 665 } 666 667 static int uio_find_mem_index(struct vm_area_struct *vma) 668 { 669 struct uio_device *idev = vma->vm_private_data; 670 671 if (vma->vm_pgoff < MAX_UIO_MAPS) { 672 if (idev->info->mem[vma->vm_pgoff].size == 0) 673 return -1; 674 return (int)vma->vm_pgoff; 675 } 676 return -1; 677 } 678 679 static vm_fault_t uio_vma_fault(struct vm_fault *vmf) 680 { 681 struct uio_device *idev = vmf->vma->vm_private_data; 682 struct page *page; 683 unsigned long offset; 684 void *addr; 685 vm_fault_t ret = 0; 686 int mi; 687 688 mutex_lock(&idev->info_lock); 689 if (!idev->info) { 690 ret = VM_FAULT_SIGBUS; 691 goto out; 692 } 693 694 mi = uio_find_mem_index(vmf->vma); 695 if (mi < 0) { 696 ret = VM_FAULT_SIGBUS; 697 goto out; 698 } 699 700 /* 701 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 702 * to use mem[N]. 703 */ 704 offset = (vmf->pgoff - mi) << PAGE_SHIFT; 705 706 addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset; 707 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL) 708 page = virt_to_page(addr); 709 else 710 page = vmalloc_to_page(addr); 711 get_page(page); 712 vmf->page = page; 713 714 out: 715 mutex_unlock(&idev->info_lock); 716 717 return ret; 718 } 719 720 static const struct vm_operations_struct uio_logical_vm_ops = { 721 .fault = uio_vma_fault, 722 }; 723 724 static int uio_mmap_logical(struct vm_area_struct *vma) 725 { 726 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP); 727 vma->vm_ops = &uio_logical_vm_ops; 728 return 0; 729 } 730 731 static const struct vm_operations_struct uio_physical_vm_ops = { 732 #ifdef CONFIG_HAVE_IOREMAP_PROT 733 .access = generic_access_phys, 734 #endif 735 }; 736 737 static int uio_mmap_physical(struct vm_area_struct *vma) 738 { 739 struct uio_device *idev = vma->vm_private_data; 740 int mi = uio_find_mem_index(vma); 741 struct uio_mem *mem; 742 743 if (mi < 0) 744 return -EINVAL; 745 mem = idev->info->mem + mi; 746 747 if (mem->addr & ~PAGE_MASK) 748 return -ENODEV; 749 if (vma->vm_end - vma->vm_start > mem->size) 750 return -EINVAL; 751 752 vma->vm_ops = &uio_physical_vm_ops; 753 if (idev->info->mem[mi].memtype == UIO_MEM_PHYS) 754 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 755 756 /* 757 * We cannot use the vm_iomap_memory() helper here, 758 * because vma->vm_pgoff is the map index we looked 759 * up above in uio_find_mem_index(), rather than an 760 * actual page offset into the mmap. 761 * 762 * So we just do the physical mmap without a page 763 * offset. 764 */ 765 return remap_pfn_range(vma, 766 vma->vm_start, 767 mem->addr >> PAGE_SHIFT, 768 vma->vm_end - vma->vm_start, 769 vma->vm_page_prot); 770 } 771 772 static int uio_mmap_dma_coherent(struct vm_area_struct *vma) 773 { 774 struct uio_device *idev = vma->vm_private_data; 775 struct uio_mem *mem; 776 void *addr; 777 int ret = 0; 778 int mi; 779 780 mi = uio_find_mem_index(vma); 781 if (mi < 0) 782 return -EINVAL; 783 784 mem = idev->info->mem + mi; 785 786 if (mem->addr & ~PAGE_MASK) 787 return -ENODEV; 788 if (mem->dma_addr & ~PAGE_MASK) 789 return -ENODEV; 790 if (!mem->dma_device) 791 return -ENODEV; 792 if (vma->vm_end - vma->vm_start > mem->size) 793 return -EINVAL; 794 795 dev_warn(mem->dma_device, 796 "use of UIO_MEM_DMA_COHERENT is highly discouraged"); 797 798 /* 799 * UIO uses offset to index into the maps for a device. 800 * We need to clear vm_pgoff for dma_mmap_coherent. 801 */ 802 vma->vm_pgoff = 0; 803 804 addr = (void *)(uintptr_t)mem->addr; 805 ret = dma_mmap_coherent(mem->dma_device, 806 vma, 807 addr, 808 mem->dma_addr, 809 vma->vm_end - vma->vm_start); 810 vma->vm_pgoff = mi; 811 812 return ret; 813 } 814 815 static int uio_mmap(struct file *filep, struct vm_area_struct *vma) 816 { 817 struct uio_listener *listener = filep->private_data; 818 struct uio_device *idev = listener->dev; 819 int mi; 820 unsigned long requested_pages, actual_pages; 821 int ret = 0; 822 823 if (vma->vm_end < vma->vm_start) 824 return -EINVAL; 825 826 vma->vm_private_data = idev; 827 828 mutex_lock(&idev->info_lock); 829 if (!idev->info) { 830 ret = -EINVAL; 831 goto out; 832 } 833 834 mi = uio_find_mem_index(vma); 835 if (mi < 0) { 836 ret = -EINVAL; 837 goto out; 838 } 839 840 requested_pages = vma_pages(vma); 841 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK) 842 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT; 843 if (requested_pages > actual_pages) { 844 ret = -EINVAL; 845 goto out; 846 } 847 848 if (idev->info->mmap) { 849 ret = idev->info->mmap(idev->info, vma); 850 goto out; 851 } 852 853 switch (idev->info->mem[mi].memtype) { 854 case UIO_MEM_IOVA: 855 case UIO_MEM_PHYS: 856 ret = uio_mmap_physical(vma); 857 break; 858 case UIO_MEM_LOGICAL: 859 case UIO_MEM_VIRTUAL: 860 ret = uio_mmap_logical(vma); 861 break; 862 case UIO_MEM_DMA_COHERENT: 863 ret = uio_mmap_dma_coherent(vma); 864 break; 865 default: 866 ret = -EINVAL; 867 } 868 869 out: 870 mutex_unlock(&idev->info_lock); 871 return ret; 872 } 873 874 static const struct file_operations uio_fops = { 875 .owner = THIS_MODULE, 876 .open = uio_open, 877 .release = uio_release, 878 .read = uio_read, 879 .write = uio_write, 880 .mmap = uio_mmap, 881 .poll = uio_poll, 882 .fasync = uio_fasync, 883 .llseek = noop_llseek, 884 }; 885 886 static int uio_major_init(void) 887 { 888 static const char name[] = "uio"; 889 struct cdev *cdev = NULL; 890 dev_t uio_dev = 0; 891 int result; 892 893 result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name); 894 if (result) 895 goto out; 896 897 result = -ENOMEM; 898 cdev = cdev_alloc(); 899 if (!cdev) 900 goto out_unregister; 901 902 cdev->owner = THIS_MODULE; 903 cdev->ops = &uio_fops; 904 kobject_set_name(&cdev->kobj, "%s", name); 905 906 result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES); 907 if (result) 908 goto out_put; 909 910 uio_major = MAJOR(uio_dev); 911 uio_cdev = cdev; 912 return 0; 913 out_put: 914 kobject_put(&cdev->kobj); 915 out_unregister: 916 unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES); 917 out: 918 return result; 919 } 920 921 static void uio_major_cleanup(void) 922 { 923 unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES); 924 cdev_del(uio_cdev); 925 } 926 927 static int init_uio_class(void) 928 { 929 int ret; 930 931 /* This is the first time in here, set everything up properly */ 932 ret = uio_major_init(); 933 if (ret) 934 goto exit; 935 936 ret = class_register(&uio_class); 937 if (ret) { 938 printk(KERN_ERR "class_register failed for uio\n"); 939 goto err_class_register; 940 } 941 942 uio_class_registered = true; 943 944 return 0; 945 946 err_class_register: 947 uio_major_cleanup(); 948 exit: 949 return ret; 950 } 951 952 static void release_uio_class(void) 953 { 954 uio_class_registered = false; 955 class_unregister(&uio_class); 956 uio_major_cleanup(); 957 } 958 959 static void uio_device_release(struct device *dev) 960 { 961 struct uio_device *idev = dev_get_drvdata(dev); 962 963 kfree(idev); 964 } 965 966 /** 967 * __uio_register_device - register a new userspace IO device 968 * @owner: module that creates the new device 969 * @parent: parent device 970 * @info: UIO device capabilities 971 * 972 * returns zero on success or a negative error code. 973 */ 974 int __uio_register_device(struct module *owner, 975 struct device *parent, 976 struct uio_info *info) 977 { 978 struct uio_device *idev; 979 int ret = 0; 980 981 if (!uio_class_registered) 982 return -EPROBE_DEFER; 983 984 if (!parent || !info || !info->name || !info->version) 985 return -EINVAL; 986 987 info->uio_dev = NULL; 988 989 idev = kzalloc(sizeof(*idev), GFP_KERNEL); 990 if (!idev) { 991 return -ENOMEM; 992 } 993 994 idev->owner = owner; 995 idev->info = info; 996 mutex_init(&idev->info_lock); 997 init_waitqueue_head(&idev->wait); 998 atomic_set(&idev->event, 0); 999 1000 ret = uio_get_minor(idev); 1001 if (ret) { 1002 kfree(idev); 1003 return ret; 1004 } 1005 1006 device_initialize(&idev->dev); 1007 idev->dev.devt = MKDEV(uio_major, idev->minor); 1008 idev->dev.class = &uio_class; 1009 idev->dev.parent = parent; 1010 idev->dev.release = uio_device_release; 1011 dev_set_drvdata(&idev->dev, idev); 1012 1013 ret = dev_set_name(&idev->dev, "uio%d", idev->minor); 1014 if (ret) 1015 goto err_device_create; 1016 1017 ret = device_add(&idev->dev); 1018 if (ret) 1019 goto err_device_create; 1020 1021 ret = uio_dev_add_attributes(idev); 1022 if (ret) 1023 goto err_uio_dev_add_attributes; 1024 1025 info->uio_dev = idev; 1026 1027 if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) { 1028 /* 1029 * Note that we deliberately don't use devm_request_irq 1030 * here. The parent module can unregister the UIO device 1031 * and call pci_disable_msi, which requires that this 1032 * irq has been freed. However, the device may have open 1033 * FDs at the time of unregister and therefore may not be 1034 * freed until they are released. 1035 */ 1036 ret = request_threaded_irq(info->irq, uio_interrupt_handler, uio_interrupt_thread, 1037 info->irq_flags, info->name, idev); 1038 if (ret) { 1039 info->uio_dev = NULL; 1040 goto err_request_irq; 1041 } 1042 } 1043 1044 return 0; 1045 1046 err_request_irq: 1047 uio_dev_del_attributes(idev); 1048 err_uio_dev_add_attributes: 1049 device_del(&idev->dev); 1050 err_device_create: 1051 uio_free_minor(idev->minor); 1052 put_device(&idev->dev); 1053 return ret; 1054 } 1055 EXPORT_SYMBOL_GPL(__uio_register_device); 1056 1057 static void devm_uio_unregister_device(struct device *dev, void *res) 1058 { 1059 uio_unregister_device(*(struct uio_info **)res); 1060 } 1061 1062 /** 1063 * __devm_uio_register_device - Resource managed uio_register_device() 1064 * @owner: module that creates the new device 1065 * @parent: parent device 1066 * @info: UIO device capabilities 1067 * 1068 * returns zero on success or a negative error code. 1069 */ 1070 int __devm_uio_register_device(struct module *owner, 1071 struct device *parent, 1072 struct uio_info *info) 1073 { 1074 struct uio_info **ptr; 1075 int ret; 1076 1077 ptr = devres_alloc(devm_uio_unregister_device, sizeof(*ptr), 1078 GFP_KERNEL); 1079 if (!ptr) 1080 return -ENOMEM; 1081 1082 *ptr = info; 1083 ret = __uio_register_device(owner, parent, info); 1084 if (ret) { 1085 devres_free(ptr); 1086 return ret; 1087 } 1088 1089 devres_add(parent, ptr); 1090 1091 return 0; 1092 } 1093 EXPORT_SYMBOL_GPL(__devm_uio_register_device); 1094 1095 /** 1096 * uio_unregister_device - unregister a industrial IO device 1097 * @info: UIO device capabilities 1098 * 1099 */ 1100 void uio_unregister_device(struct uio_info *info) 1101 { 1102 struct uio_device *idev; 1103 unsigned long minor; 1104 1105 if (!info || !info->uio_dev) 1106 return; 1107 1108 idev = info->uio_dev; 1109 minor = idev->minor; 1110 1111 mutex_lock(&idev->info_lock); 1112 uio_dev_del_attributes(idev); 1113 1114 if (info->irq && info->irq != UIO_IRQ_CUSTOM) 1115 free_irq(info->irq, idev); 1116 1117 idev->info = NULL; 1118 mutex_unlock(&idev->info_lock); 1119 1120 wake_up_interruptible(&idev->wait); 1121 kill_fasync(&idev->async_queue, SIGIO, POLL_HUP); 1122 1123 uio_free_minor(minor); 1124 device_unregister(&idev->dev); 1125 1126 return; 1127 } 1128 EXPORT_SYMBOL_GPL(uio_unregister_device); 1129 1130 static int __init uio_init(void) 1131 { 1132 return init_uio_class(); 1133 } 1134 1135 static void __exit uio_exit(void) 1136 { 1137 release_uio_class(); 1138 idr_destroy(&uio_idr); 1139 } 1140 1141 module_init(uio_init) 1142 module_exit(uio_exit) 1143 MODULE_LICENSE("GPL v2"); 1144