|
@@ -215,7 +215,20 @@ 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);
|
|
|
+ int ret;
|
|
|
+
|
|
|
+ mutex_lock(&idev->info_lock);
|
|
|
+ if (!idev->info) {
|
|
|
+ ret = -EINVAL;
|
|
|
+ dev_err(dev, "the device has been unregistered\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ ret = sprintf(buf, "%s\n", idev->info->name);
|
|
|
+
|
|
|
+out:
|
|
|
+ mutex_unlock(&idev->info_lock);
|
|
|
+ return ret;
|
|
|
}
|
|
|
static DEVICE_ATTR_RO(name);
|
|
|
|
|
@@ -223,7 +236,20 @@ 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);
|
|
|
+ int ret;
|
|
|
+
|
|
|
+ mutex_lock(&idev->info_lock);
|
|
|
+ if (!idev->info) {
|
|
|
+ ret = -EINVAL;
|
|
|
+ dev_err(dev, "the device has been unregistered\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ ret = sprintf(buf, "%s\n", idev->info->version);
|
|
|
+
|
|
|
+out:
|
|
|
+ mutex_unlock(&idev->info_lock);
|
|
|
+ return ret;
|
|
|
}
|
|
|
static DEVICE_ATTR_RO(version);
|
|
|
|
|
@@ -415,11 +441,15 @@ EXPORT_SYMBOL_GPL(uio_event_notify);
|
|
|
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);
|
|
|
+ irqreturn_t ret;
|
|
|
+
|
|
|
+ mutex_lock(&idev->info_lock);
|
|
|
|
|
|
+ ret = idev->info->handler(irq, idev->info);
|
|
|
if (ret == IRQ_HANDLED)
|
|
|
uio_event_notify(idev->info);
|
|
|
|
|
|
+ mutex_unlock(&idev->info_lock);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -460,6 +490,12 @@ static int uio_open(struct inode *inode, struct file *filep)
|
|
|
filep->private_data = listener;
|
|
|
|
|
|
mutex_lock(&idev->info_lock);
|
|
|
+ if (!idev->info) {
|
|
|
+ mutex_unlock(&idev->info_lock);
|
|
|
+ ret = -EINVAL;
|
|
|
+ goto err_alloc_listener;
|
|
|
+ }
|
|
|
+
|
|
|
if (idev->info && idev->info->open)
|
|
|
ret = idev->info->open(idev->info, inode);
|
|
|
mutex_unlock(&idev->info_lock);
|
|
@@ -590,6 +626,11 @@ static ssize_t uio_write(struct file *filep, const char __user *buf,
|
|
|
s32 irq_on;
|
|
|
|
|
|
mutex_lock(&idev->info_lock);
|
|
|
+ if (!idev->info) {
|
|
|
+ retval = -EINVAL;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
if (!idev->info || !idev->info->irq) {
|
|
|
retval = -EIO;
|
|
|
goto out;
|
|
@@ -635,10 +676,20 @@ static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
|
|
|
struct page *page;
|
|
|
unsigned long offset;
|
|
|
void *addr;
|
|
|
+ int ret = 0;
|
|
|
+ int mi;
|
|
|
|
|
|
- int mi = uio_find_mem_index(vmf->vma);
|
|
|
- if (mi < 0)
|
|
|
- return VM_FAULT_SIGBUS;
|
|
|
+ mutex_lock(&idev->info_lock);
|
|
|
+ if (!idev->info) {
|
|
|
+ ret = VM_FAULT_SIGBUS;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ mi = uio_find_mem_index(vmf->vma);
|
|
|
+ if (mi < 0) {
|
|
|
+ ret = VM_FAULT_SIGBUS;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
|
|
|
/*
|
|
|
* We need to subtract mi because userspace uses offset = N*PAGE_SIZE
|
|
@@ -653,7 +704,11 @@ static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
|
|
|
page = vmalloc_to_page(addr);
|
|
|
get_page(page);
|
|
|
vmf->page = page;
|
|
|
- return 0;
|
|
|
+
|
|
|
+out:
|
|
|
+ mutex_unlock(&idev->info_lock);
|
|
|
+
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
static const struct vm_operations_struct uio_logical_vm_ops = {
|
|
@@ -678,6 +733,7 @@ static int uio_mmap_physical(struct vm_area_struct *vma)
|
|
|
struct uio_device *idev = vma->vm_private_data;
|
|
|
int mi = uio_find_mem_index(vma);
|
|
|
struct uio_mem *mem;
|
|
|
+
|
|
|
if (mi < 0)
|
|
|
return -EINVAL;
|
|
|
mem = idev->info->mem + mi;
|
|
@@ -719,30 +775,46 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
|
|
|
|
|
|
vma->vm_private_data = idev;
|
|
|
|
|
|
+ mutex_lock(&idev->info_lock);
|
|
|
+ if (!idev->info) {
|
|
|
+ ret = -EINVAL;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
mi = uio_find_mem_index(vma);
|
|
|
- if (mi < 0)
|
|
|
- return -EINVAL;
|
|
|
+ if (mi < 0) {
|
|
|
+ ret = -EINVAL;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
|
|
|
requested_pages = vma_pages(vma);
|
|
|
actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
|
|
|
+ idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
|
|
|
- if (requested_pages > actual_pages)
|
|
|
- return -EINVAL;
|
|
|
+ if (requested_pages > actual_pages) {
|
|
|
+ ret = -EINVAL;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
|
|
|
if (idev->info->mmap) {
|
|
|
ret = idev->info->mmap(idev->info, vma);
|
|
|
- return ret;
|
|
|
+ goto out;
|
|
|
}
|
|
|
|
|
|
switch (idev->info->mem[mi].memtype) {
|
|
|
case UIO_MEM_PHYS:
|
|
|
- return uio_mmap_physical(vma);
|
|
|
+ ret = uio_mmap_physical(vma);
|
|
|
+ break;
|
|
|
case UIO_MEM_LOGICAL:
|
|
|
case UIO_MEM_VIRTUAL:
|
|
|
- return uio_mmap_logical(vma);
|
|
|
+ ret = uio_mmap_logical(vma);
|
|
|
+ break;
|
|
|
default:
|
|
|
- return -EINVAL;
|
|
|
+ ret = -EINVAL;
|
|
|
}
|
|
|
+
|
|
|
+out:
|
|
|
+ mutex_unlock(&idev->info_lock);
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
static const struct file_operations uio_fops = {
|
|
@@ -932,12 +1004,12 @@ void uio_unregister_device(struct uio_info *info)
|
|
|
|
|
|
uio_free_minor(idev);
|
|
|
|
|
|
+ mutex_lock(&idev->info_lock);
|
|
|
uio_dev_del_attributes(idev);
|
|
|
|
|
|
if (info->irq && info->irq != UIO_IRQ_CUSTOM)
|
|
|
free_irq(info->irq, idev);
|
|
|
|
|
|
- mutex_lock(&idev->info_lock);
|
|
|
idev->info = NULL;
|
|
|
mutex_unlock(&idev->info_lock);
|
|
|
|