cregit-Linux how code gets into the kernel

Release 4.14 drivers/uio/uio.c

Directory: drivers/uio
/*
 * drivers/uio/uio.c
 *
 * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
 * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de>
 * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
 *
 * Userspace IO
 *
 * Base Functions
 *
 * Licensed under the GPLv2 only.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/idr.h>
#include <linux/sched/signal.h>
#include <linux/string.h>
#include <linux/kobject.h>
#include <linux/cdev.h>
#include <linux/uio_driver.h>


#define UIO_MAX_DEVICES		(1U << MINORBITS)


static int uio_major;

static struct cdev *uio_cdev;
static DEFINE_IDR(uio_idr);

static const struct file_operations uio_fops;

/* Protect idr accesses */
static DEFINE_MUTEX(minor_lock);

/*
 * attributes
 */


struct uio_map {
	
struct kobject kobj;
	
struct uio_mem *mem;
};

#define to_map(map) container_of(map, struct uio_map, kobj)


static ssize_t map_name_show(struct uio_mem *mem, char *buf) { if (unlikely(!mem->name)) mem->name = ""; return sprintf(buf, "%s\n", mem->name); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch43100.00%1100.00%
Total43100.00%1100.00%


static ssize_t map_addr_show(struct uio_mem *mem, char *buf) { return sprintf(buf, "%pa\n", &mem->addr); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch2278.57%133.33%
Brandon Philips414.29%133.33%
Cristian Stoica27.14%133.33%
Total28100.00%3100.00%


static ssize_t map_size_show(struct uio_mem *mem, char *buf) { return sprintf(buf, "%pa\n", &mem->size); }

Contributors

PersonTokensPropCommitsCommitProp
Brandon Philips1450.00%133.33%
Hans-Jürgen Koch1242.86%133.33%
Cristian Stoica27.14%133.33%
Total28100.00%3100.00%


static ssize_t map_offset_show(struct uio_mem *mem, char *buf) { return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch2578.12%133.33%
Kai Jiang618.75%133.33%
Michal Sojka13.12%133.33%
Total32100.00%3100.00%

struct map_sysfs_entry { struct attribute attr; ssize_t (*show)(struct uio_mem *, char *); ssize_t (*store)(struct uio_mem *, const char *, size_t); }; static struct map_sysfs_entry name_attribute = __ATTR(name, S_IRUGO, map_name_show, NULL); static struct map_sysfs_entry addr_attribute = __ATTR(addr, S_IRUGO, map_addr_show, NULL); static struct map_sysfs_entry size_attribute = __ATTR(size, S_IRUGO, map_size_show, NULL); static struct map_sysfs_entry offset_attribute = __ATTR(offset, S_IRUGO, map_offset_show, NULL); static struct attribute *attrs[] = { &name_attribute.attr, &addr_attribute.attr, &size_attribute.attr, &offset_attribute.attr, NULL, /* need to NULL terminate the list of attributes */ };
static void map_release(struct kobject *kobj) { struct uio_map *map = to_map(kobj); kfree(map); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch26100.00%1100.00%
Total26100.00%1100.00%


static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr, char *buf) { struct uio_map *map = to_map(kobj); struct uio_mem *mem = map->mem; struct map_sysfs_entry *entry; entry = container_of(attr, struct map_sysfs_entry, attr); if (!entry->show) return -EIO; return entry->show(mem, buf); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch77100.00%1100.00%
Total77100.00%1100.00%

static const struct sysfs_ops map_sysfs_ops = { .show = map_type_show, }; static struct kobj_type map_attr_type = { .release = map_release, .sysfs_ops = &map_sysfs_ops, .default_attrs = attrs, }; struct uio_portio { struct kobject kobj; struct uio_port *port; }; #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
static ssize_t portio_name_show(struct uio_port *port, char *buf) { if (unlikely(!port->name)) port->name = ""; return sprintf(buf, "%s\n", port->name); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch43100.00%1100.00%
Total43100.00%1100.00%


static ssize_t portio_start_show(struct uio_port *port, char *buf) { return sprintf(buf, "0x%lx\n", port->start); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch27100.00%1100.00%
Total27100.00%1100.00%


static ssize_t portio_size_show(struct uio_port *port, char *buf) { return sprintf(buf, "0x%lx\n", port->size); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch27100.00%1100.00%
Total27100.00%1100.00%


static ssize_t portio_porttype_show(struct uio_port *port, char *buf) { const char *porttypes[] = {"none", "x86", "gpio", "other"}; if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER)) return -EINVAL; return sprintf(buf, "port_%s\n", porttypes[port->porttype]); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch68100.00%1100.00%
Total68100.00%1100.00%

struct portio_sysfs_entry { struct attribute attr; ssize_t (*show)(struct uio_port *, char *); ssize_t (*store)(struct uio_port *, const char *, size_t); }; static struct portio_sysfs_entry portio_name_attribute = __ATTR(name, S_IRUGO, portio_name_show, NULL); static struct portio_sysfs_entry portio_start_attribute = __ATTR(start, S_IRUGO, portio_start_show, NULL); static struct portio_sysfs_entry portio_size_attribute = __ATTR(size, S_IRUGO, portio_size_show, NULL); static struct portio_sysfs_entry portio_porttype_attribute = __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL); static struct attribute *portio_attrs[] = { &portio_name_attribute.attr, &portio_start_attribute.attr, &portio_size_attribute.attr, &portio_porttype_attribute.attr, NULL, };
static void portio_release(struct kobject *kobj) { struct uio_portio *portio = to_portio(kobj); kfree(portio); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch1765.38%266.67%
Greg Kroah-Hartman934.62%133.33%
Total26100.00%3100.00%


static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr, char *buf) { struct uio_portio *portio = to_portio(kobj); struct uio_port *port = portio->port; struct portio_sysfs_entry *entry; entry = container_of(attr, struct portio_sysfs_entry, attr); if (!entry->show) return -EIO; return entry->show(port, buf); }

Contributors

PersonTokensPropCommitsCommitProp
Brandon Philips6685.71%150.00%
Hans-Jürgen Koch1114.29%150.00%
Total77100.00%2100.00%

static const struct sysfs_ops portio_sysfs_ops = { .show = portio_type_show, }; static struct kobj_type portio_attr_type = { .release = portio_release, .sysfs_ops = &portio_sysfs_ops, .default_attrs = portio_attrs, };
static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) { struct uio_device *idev = dev_get_drvdata(dev); return sprintf(buf, "%s\n", idev->info->name); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch4397.73%150.00%
Greg Kroah-Hartman12.27%150.00%
Total44100.00%2100.00%

static DEVICE_ATTR_RO(name);
static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf) { struct uio_device *idev = dev_get_drvdata(dev); return sprintf(buf, "%s\n", idev->info->version); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch4397.73%150.00%
Greg Kroah-Hartman12.27%150.00%
Total44100.00%2100.00%

static DEVICE_ATTR_RO(version);
static ssize_t event_show(struct device *dev, struct device_attribute *attr, char *buf) { struct uio_device *idev = dev_get_drvdata(dev); return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event)); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch4998.00%150.00%
Greg Kroah-Hartman12.00%150.00%
Total50100.00%2100.00%

static DEVICE_ATTR_RO(event); static struct attribute *uio_attrs[] = { &dev_attr_name.attr, &dev_attr_version.attr, &dev_attr_event.attr, NULL, }; ATTRIBUTE_GROUPS(uio); /* UIO class infrastructure */ static struct class uio_class = { .name = "uio", .dev_groups = uio_groups, }; /* * device functions */
static int uio_dev_add_attributes(struct uio_device *idev) { int ret; int mi, pi; int map_found = 0; int portio_found = 0; struct uio_mem *mem; struct uio_map *map; struct uio_port *port; struct uio_portio *portio; for (mi = 0; mi < MAX_UIO_MAPS; mi++) { mem = &idev->info->mem[mi]; if (mem->size == 0) break; if (!map_found) { map_found = 1; idev->map_dir = kobject_create_and_add("maps", &idev->dev->kobj); if (!idev->map_dir) { ret = -ENOMEM; goto err_map; } } map = kzalloc(sizeof(*map), GFP_KERNEL); if (!map) { ret = -ENOMEM; goto err_map; } kobject_init(&map->kobj, &map_attr_type); map->mem = mem; mem->map = map; ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi); if (ret) goto err_map_kobj; ret = kobject_uevent(&map->kobj, KOBJ_ADD); if (ret) goto err_map_kobj; } for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) { port = &idev->info->port[pi]; if (port->size == 0) break; if (!portio_found) { portio_found = 1; idev->portio_dir = kobject_create_and_add("portio", &idev->dev->kobj); if (!idev->portio_dir) { ret = -ENOMEM; goto err_portio; } } portio = kzalloc(sizeof(*portio), GFP_KERNEL); if (!portio) { ret = -ENOMEM; goto err_portio; } kobject_init(&portio->kobj, &portio_attr_type); portio->port = port; port->portio = portio; ret = kobject_add(&portio->kobj, idev->portio_dir, "port%d", pi); if (ret) goto err_portio_kobj; ret = kobject_uevent(&portio->kobj, KOBJ_ADD); if (ret) goto err_portio_kobj; } return 0; err_portio: pi--; err_portio_kobj: for (; pi >= 0; pi--) { port = &idev->info->port[pi]; portio = port->portio; kobject_put(&portio->kobj); } kobject_put(idev->portio_dir); err_map: mi--; err_map_kobj: for (; mi >= 0; mi--) { mem = &idev->info->mem[mi]; map = mem->map; kobject_put(&map->kobj); } kobject_put(idev->map_dir); dev_err(idev->dev, "error creating sysfs files (%d)\n", ret); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch40177.12%222.22%
Greg Kroah-Hartman7514.42%444.44%
Dan Carpenter285.38%111.11%
Cong Ding122.31%111.11%
Suman Anna40.77%111.11%
Total520100.00%9100.00%


static void uio_dev_del_attributes(struct uio_device *idev) { int i; struct uio_mem *mem; struct uio_port *port; for (i = 0; i < MAX_UIO_MAPS; i++) { mem = &idev->info->mem[i]; if (mem->size == 0) break; kobject_put(&mem->map->kobj); } kobject_put(idev->map_dir); for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) { port = &idev->info->port[i]; if (port->size == 0) break; kobject_put(&port->portio->kobj); } kobject_put(idev->portio_dir); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch12596.15%250.00%
Greg Kroah-Hartman53.85%250.00%
Total130100.00%4100.00%


static int uio_get_minor(struct uio_device *idev) { int retval = -ENOMEM; mutex_lock(&minor_lock); retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL); if (retval >= 0) { idev->minor = retval; retval = 0; } else if (retval == -ENOSPC) { dev_err(idev->dev, "too many uio devices\n"); retval = -EINVAL; } mutex_unlock(&minor_lock); return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch5257.78%125.00%
Hillf Danton1820.00%125.00%
Tejun Heo1617.78%125.00%
Damian Hobson-Garcia44.44%125.00%
Total90100.00%4100.00%


static void uio_free_minor(struct uio_device *idev) { mutex_lock(&minor_lock); idr_remove(&uio_idr, idev->minor); mutex_unlock(&minor_lock); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch2163.64%150.00%
Jonathan Corbet1236.36%150.00%
Total33100.00%2100.00%

/** * uio_event_notify - trigger an interrupt event * @info: UIO device capabilities */
void uio_event_notify(struct uio_info *info) { struct uio_device *idev = info->uio_dev; atomic_inc(&idev->event); wake_up_interruptible(&idev->wait); kill_fasync(&idev->async_queue, SIGIO, POLL_IN); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch47100.00%1100.00%
Total47100.00%1100.00%

EXPORT_SYMBOL_GPL(uio_event_notify); /** * uio_interrupt - hardware interrupt handler * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer * @dev_id: Pointer to the devices uio_device structure */
static irqreturn_t uio_interrupt(int irq, void *dev_id) { struct uio_device *idev = (struct uio_device *)dev_id; irqreturn_t ret = idev->info->handler(irq, idev->info); if (ret == IRQ_HANDLED) uio_event_notify(idev->info); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch57100.00%1100.00%
Total57100.00%1100.00%

struct uio_listener { struct uio_device *dev; s32 event_count; };
static int uio_open(struct inode *inode, struct file *filep) { struct uio_device *idev; struct uio_listener *listener; int ret = 0; mutex_lock(&minor_lock); idev = idr_find(&uio_idr, iminor(inode)); mutex_unlock(&minor_lock); if (!idev) { ret = -ENODEV; goto out; } if (!try_module_get(idev->owner)) { ret = -ENODEV; goto out; } listener = kmalloc(sizeof(*listener), GFP_KERNEL); if (!listener) { ret = -ENOMEM; goto err_alloc_listener; } listener->dev = idev; listener->event_count = atomic_read(&idev->event); filep->private_data = listener; if (idev->info->open) { ret = idev->info->open(idev->info, inode); if (ret) goto err_infoopen; } return 0; err_infoopen: kfree(listener); err_alloc_listener: module_put(idev->owner); out: return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch13165.83%125.00%
Uwe Kleine-König3819.10%125.00%
Jonathan Corbet3015.08%250.00%
Total199100.00%4100.00%


static int uio_fasync(int fd, struct file *filep, int on) { struct uio_listener *listener = filep->private_data; struct uio_device *idev = listener->dev; return fasync_helper(fd, filep, on, &idev->async_queue); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch50100.00%1100.00%
Total50100.00%1100.00%


static int uio_release(struct inode *inode, struct file *filep) { int ret = 0; struct uio_listener *listener = filep->private_data; struct uio_device *idev = listener->dev; if (idev->info->release) ret = idev->info->release(idev->info, inode); module_put(idev->owner); kfree(listener); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch77100.00%1100.00%
Total77100.00%1100.00%


static unsigned int uio_poll(struct file *filep, poll_table *wait) { struct uio_listener *listener = filep->private_data; struct uio_device *idev = listener->dev; if (!idev->info->irq) return -EIO; poll_wait(filep, &idev->wait, wait); if (listener->event_count != atomic_read(&idev->event)) return POLLIN | POLLRDNORM; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch8098.77%150.00%
Eric W. Biedermann11.23%150.00%
Total81100.00%2100.00%


static ssize_t uio_read(struct file *filep, char __user *buf, size_t count, loff_t *ppos) { struct uio_listener *listener = filep->private_data; struct uio_device *idev = listener->dev; DECLARE_WAITQUEUE(wait, current); ssize_t retval; s32 event_count; if (!idev->info->irq) return -EIO; if (count != sizeof(s32)) return -EINVAL; add_wait_queue(&idev->wait, &wait); do { set_current_state(TASK_INTERRUPTIBLE); event_count = atomic_read(&idev->event); if (event_count != listener->event_count) { __set_current_state(TASK_RUNNING); if (copy_to_user(buf, &event_count, count)) retval = -EFAULT; else { listener->event_count = event_count; retval = count; } break; } if (filep->f_flags & O_NONBLOCK) { retval = -EAGAIN; break; } if (signal_pending(current)) { retval = -ERESTARTSYS; break; } schedule(); } while (1); __set_current_state(TASK_RUNNING); remove_wait_queue(&idev->wait, &wait); return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch20797.18%133.33%
Michal Hocko52.35%133.33%
Eric W. Biedermann10.47%133.33%
Total213100.00%3100.00%


static ssize_t uio_write(struct file *filep, const char __user *buf, size_t count, loff_t *ppos) { struct uio_listener *listener = filep->private_data; struct uio_device *idev = listener->dev; ssize_t retval; s32 irq_on; if (!idev->info->irq) return -EIO; if (count != sizeof(s32)) return -EINVAL; if (!idev->info->irqcontrol) return -ENOSYS; if (copy_from_user(&irq_on, buf, count)) return -EFAULT; retval = idev->info->irqcontrol(idev->info, irq_on); return retval ? retval : sizeof(s32); }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch12799.22%150.00%
Eric W. Biedermann10.78%150.00%
Total128100.00%2100.00%


static int uio_find_mem_index(struct vm_area_struct *vma) { struct uio_device *idev = vma->vm_private_data; if (vma->vm_pgoff < MAX_UIO_MAPS) { if (idev->info->mem[vma->vm_pgoff].size == 0) return -1; return (int)vma->vm_pgoff; } return -1; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch5079.37%150.00%
Hillf Danton1320.63%150.00%
Total63100.00%2100.00%


static int uio_vma_fault(struct vm_fault *vmf) { struct uio_device *idev = vmf->vma->vm_private_data; struct page *page; unsigned long offset; void *addr; int mi = uio_find_mem_index(vmf->vma); if (mi < 0) return VM_FAULT_SIGBUS; /* * We need to subtract mi because userspace uses offset = N*PAGE_SIZE * to use mem[N]. */ offset = (vmf->pgoff - mi) << PAGE_SHIFT; addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset; if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL) page = virt_to_page(addr); else page = vmalloc_to_page(addr); get_page(page); vmf->page = page; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Hans-Jürgen Koch7656.72%120.00%
Ben Hutchings2720.15%120.00%
Andrew G. Harvey1712.69%120.00%
Nicholas Piggin107.46%120.00%
Dave Jiang42.99%120.00%
Total134100.00%5100.00%

static const struct vm_operations_struct uio_logical_vm_ops = { .fault = uio_vma_fault