Commit ecb3b80f authored by Santosh Nayak's avatar Santosh Nayak Committed by Greg Kroah-Hartman

Staging: vme: Replace semaphore by mutex.

Replace binary semaphore by mutex for code cleanup.
Mutex also gives better performance than semaphore.

Add 'mutex_destroy()' in 'vme_user_remove()' routine.
Signed-off-by: default avatarSantosh Nayak <santoshprasadnayak@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent f56d711b
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/pci.h> #include <linux/pci.h>
#include <linux/semaphore.h> #include <linux/mutex.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/syscalls.h> #include <linux/syscalls.h>
...@@ -95,7 +95,7 @@ struct image_desc { ...@@ -95,7 +95,7 @@ struct image_desc {
void *kern_buf; /* Buffer address in kernel space */ void *kern_buf; /* Buffer address in kernel space */
dma_addr_t pci_buf; /* Buffer address in PCI address space */ dma_addr_t pci_buf; /* Buffer address in PCI address space */
unsigned long long size_buf; /* Buffer size */ unsigned long long size_buf; /* Buffer size */
struct semaphore sem; /* Semaphore for locking image */ struct mutex mutex; /* Mutex for locking image */
struct device *device; /* Sysfs device */ struct device *device; /* Sysfs device */
struct vme_resource *resource; /* VME resource */ struct vme_resource *resource; /* VME resource */
int users; /* Number of current users */ int users; /* Number of current users */
...@@ -168,7 +168,7 @@ static int vme_user_open(struct inode *inode, struct file *file) ...@@ -168,7 +168,7 @@ static int vme_user_open(struct inode *inode, struct file *file)
int err; int err;
unsigned int minor = MINOR(inode->i_rdev); unsigned int minor = MINOR(inode->i_rdev);
down(&image[minor].sem); mutex_lock(&image[minor].mutex);
/* Allow device to be opened if a resource is needed and allocated. */ /* Allow device to be opened if a resource is needed and allocated. */
if (minor < CONTROL_MINOR && image[minor].resource == NULL) { if (minor < CONTROL_MINOR && image[minor].resource == NULL) {
printk(KERN_ERR "No resources allocated for device\n"); printk(KERN_ERR "No resources allocated for device\n");
...@@ -179,12 +179,12 @@ static int vme_user_open(struct inode *inode, struct file *file) ...@@ -179,12 +179,12 @@ static int vme_user_open(struct inode *inode, struct file *file)
/* Increment user count */ /* Increment user count */
image[minor].users++; image[minor].users++;
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return 0; return 0;
err_res: err_res:
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return err; return err;
} }
...@@ -193,12 +193,12 @@ static int vme_user_release(struct inode *inode, struct file *file) ...@@ -193,12 +193,12 @@ static int vme_user_release(struct inode *inode, struct file *file)
{ {
unsigned int minor = MINOR(inode->i_rdev); unsigned int minor = MINOR(inode->i_rdev);
down(&image[minor].sem); mutex_lock(&image[minor].mutex);
/* Decrement user count */ /* Decrement user count */
image[minor].users--; image[minor].users--;
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return 0; return 0;
} }
...@@ -325,14 +325,14 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, ...@@ -325,14 +325,14 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
if (minor == CONTROL_MINOR) if (minor == CONTROL_MINOR)
return 0; return 0;
down(&image[minor].sem); mutex_lock(&image[minor].mutex);
/* XXX Do we *really* want this helper - we can use vme_*_get ? */ /* XXX Do we *really* want this helper - we can use vme_*_get ? */
image_size = vme_get_size(image[minor].resource); image_size = vme_get_size(image[minor].resource);
/* Ensure we are starting at a valid location */ /* Ensure we are starting at a valid location */
if ((*ppos < 0) || (*ppos > (image_size - 1))) { if ((*ppos < 0) || (*ppos > (image_size - 1))) {
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return 0; return 0;
} }
...@@ -353,8 +353,7 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, ...@@ -353,8 +353,7 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
retval = -EINVAL; retval = -EINVAL;
} }
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
if (retval > 0) if (retval > 0)
*ppos += retval; *ppos += retval;
...@@ -372,13 +371,13 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf, ...@@ -372,13 +371,13 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
if (minor == CONTROL_MINOR) if (minor == CONTROL_MINOR)
return 0; return 0;
down(&image[minor].sem); mutex_lock(&image[minor].mutex);
image_size = vme_get_size(image[minor].resource); image_size = vme_get_size(image[minor].resource);
/* Ensure we are starting at a valid location */ /* Ensure we are starting at a valid location */
if ((*ppos < 0) || (*ppos > (image_size - 1))) { if ((*ppos < 0) || (*ppos > (image_size - 1))) {
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return 0; return 0;
} }
...@@ -398,8 +397,8 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf, ...@@ -398,8 +397,8 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
default: default:
retval = -EINVAL; retval = -EINVAL;
} }
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
if (retval > 0) if (retval > 0)
*ppos += retval; *ppos += retval;
...@@ -416,7 +415,7 @@ static loff_t vme_user_llseek(struct file *file, loff_t off, int whence) ...@@ -416,7 +415,7 @@ static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
if (minor == CONTROL_MINOR) if (minor == CONTROL_MINOR)
return -EINVAL; return -EINVAL;
down(&image[minor].sem); mutex_lock(&image[minor].mutex);
image_size = vme_get_size(image[minor].resource); image_size = vme_get_size(image[minor].resource);
switch (whence) { switch (whence) {
...@@ -430,19 +429,19 @@ static loff_t vme_user_llseek(struct file *file, loff_t off, int whence) ...@@ -430,19 +429,19 @@ static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
absolute = image_size + off; absolute = image_size + off;
break; break;
default: default:
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return -EINVAL; return -EINVAL;
break; break;
} }
if ((absolute < 0) || (absolute >= image_size)) { if ((absolute < 0) || (absolute >= image_size)) {
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return -EINVAL; return -EINVAL;
} }
file->f_pos = absolute; file->f_pos = absolute;
up(&image[minor].sem); mutex_unlock(&image[minor].mutex);
return absolute; return absolute;
} }
...@@ -696,7 +695,7 @@ static int __devinit vme_user_probe(struct vme_dev *vdev) ...@@ -696,7 +695,7 @@ static int __devinit vme_user_probe(struct vme_dev *vdev)
for (i = 0; i < VME_DEVS; i++) { for (i = 0; i < VME_DEVS; i++) {
image[i].kern_buf = NULL; image[i].kern_buf = NULL;
image[i].pci_buf = 0; image[i].pci_buf = 0;
sema_init(&image[i].sem, 1); mutex_init(&image[i].mutex);
image[i].device = NULL; image[i].device = NULL;
image[i].resource = NULL; image[i].resource = NULL;
image[i].users = 0; image[i].users = 0;
...@@ -858,8 +857,10 @@ static int __devexit vme_user_remove(struct vme_dev *dev) ...@@ -858,8 +857,10 @@ static int __devexit vme_user_remove(struct vme_dev *dev)
int i; int i;
/* Remove sysfs Entries */ /* Remove sysfs Entries */
for (i = 0; i < VME_DEVS; i++) for (i = 0; i < VME_DEVS; i++) {
mutex_destroy(&image[i].mutex);
device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i)); device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
}
class_destroy(vme_user_sysfs_class); class_destroy(vme_user_sysfs_class);
for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) { for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment