Commit e5d670dc authored by Ian Abbott's avatar Ian Abbott Committed by Greg Kroah-Hartman

staging: comedi: use file->private_data in file operations

Since the `struct comedi_device` should now be protected from being
freed while an open file object is using it, use the `private_data`
member of the `struct file` to point to it.  Set it in `comedi_open()`
and use it in the other file operation handlers instead of calling
`comedi_dev_from_minor()` and checking the result.
Signed-off-by: default avatarIan Abbott <abbotti@mev.co.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 69e2387f
...@@ -1828,12 +1828,9 @@ static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd, ...@@ -1828,12 +1828,9 @@ static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
unsigned long arg) unsigned long arg)
{ {
const unsigned minor = iminor(file_inode(file)); const unsigned minor = iminor(file_inode(file));
struct comedi_device *dev = comedi_dev_from_minor(minor); struct comedi_device *dev = file->private_data;
int rc; int rc;
if (!dev)
return -ENODEV;
mutex_lock(&dev->mutex); mutex_lock(&dev->mutex);
/* Device config is special, because it must work on /* Device config is special, because it must work on
...@@ -1964,7 +1961,7 @@ static struct vm_operations_struct comedi_vm_ops = { ...@@ -1964,7 +1961,7 @@ static struct vm_operations_struct comedi_vm_ops = {
static int comedi_mmap(struct file *file, struct vm_area_struct *vma) static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
{ {
const unsigned minor = iminor(file_inode(file)); const unsigned minor = iminor(file_inode(file));
struct comedi_device *dev = comedi_dev_from_minor(minor); struct comedi_device *dev = file->private_data;
struct comedi_subdevice *s; struct comedi_subdevice *s;
struct comedi_async *async; struct comedi_async *async;
unsigned long start = vma->vm_start; unsigned long start = vma->vm_start;
...@@ -1973,9 +1970,6 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma) ...@@ -1973,9 +1970,6 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
int i; int i;
int retval; int retval;
if (!dev)
return -ENODEV;
mutex_lock(&dev->mutex); mutex_lock(&dev->mutex);
if (!dev->attached) { if (!dev->attached) {
...@@ -2043,12 +2037,9 @@ static unsigned int comedi_poll(struct file *file, poll_table *wait) ...@@ -2043,12 +2037,9 @@ static unsigned int comedi_poll(struct file *file, poll_table *wait)
{ {
unsigned int mask = 0; unsigned int mask = 0;
const unsigned minor = iminor(file_inode(file)); const unsigned minor = iminor(file_inode(file));
struct comedi_device *dev = comedi_dev_from_minor(minor); struct comedi_device *dev = file->private_data;
struct comedi_subdevice *s; struct comedi_subdevice *s;
if (!dev)
return -ENODEV;
mutex_lock(&dev->mutex); mutex_lock(&dev->mutex);
if (!dev->attached) { if (!dev->attached) {
...@@ -2088,14 +2079,11 @@ static ssize_t comedi_write(struct file *file, const char __user *buf, ...@@ -2088,14 +2079,11 @@ static ssize_t comedi_write(struct file *file, const char __user *buf,
int n, m, count = 0, retval = 0; int n, m, count = 0, retval = 0;
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
const unsigned minor = iminor(file_inode(file)); const unsigned minor = iminor(file_inode(file));
struct comedi_device *dev = comedi_dev_from_minor(minor); struct comedi_device *dev = file->private_data;
bool on_wait_queue = false; bool on_wait_queue = false;
bool attach_locked; bool attach_locked;
unsigned int old_detach_count; unsigned int old_detach_count;
if (!dev)
return -ENODEV;
/* Protect against device detachment during operation. */ /* Protect against device detachment during operation. */
down_read(&dev->attach_lock); down_read(&dev->attach_lock);
attach_locked = true; attach_locked = true;
...@@ -2227,14 +2215,11 @@ static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes, ...@@ -2227,14 +2215,11 @@ static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
int n, m, count = 0, retval = 0; int n, m, count = 0, retval = 0;
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
const unsigned minor = iminor(file_inode(file)); const unsigned minor = iminor(file_inode(file));
struct comedi_device *dev = comedi_dev_from_minor(minor); struct comedi_device *dev = file->private_data;
unsigned int old_detach_count; unsigned int old_detach_count;
bool become_nonbusy = false; bool become_nonbusy = false;
bool attach_locked; bool attach_locked;
if (!dev)
return -ENODEV;
/* Protect against device detachment during operation. */ /* Protect against device detachment during operation. */
down_read(&dev->attach_lock); down_read(&dev->attach_lock);
attach_locked = true; attach_locked = true;
...@@ -2424,6 +2409,7 @@ static int comedi_open(struct inode *inode, struct file *file) ...@@ -2424,6 +2409,7 @@ static int comedi_open(struct inode *inode, struct file *file)
} }
dev->use_count++; dev->use_count++;
file->private_data = dev;
rc = 0; rc = 0;
out: out:
...@@ -2435,25 +2421,17 @@ static int comedi_open(struct inode *inode, struct file *file) ...@@ -2435,25 +2421,17 @@ static int comedi_open(struct inode *inode, struct file *file)
static int comedi_fasync(int fd, struct file *file, int on) static int comedi_fasync(int fd, struct file *file, int on)
{ {
const unsigned minor = iminor(file_inode(file)); struct comedi_device *dev = file->private_data;
struct comedi_device *dev = comedi_dev_from_minor(minor);
if (!dev)
return -ENODEV;
return fasync_helper(fd, file, on, &dev->async_queue); return fasync_helper(fd, file, on, &dev->async_queue);
} }
static int comedi_close(struct inode *inode, struct file *file) static int comedi_close(struct inode *inode, struct file *file)
{ {
const unsigned minor = iminor(inode); struct comedi_device *dev = file->private_data;
struct comedi_device *dev = comedi_dev_from_minor(minor);
struct comedi_subdevice *s = NULL; struct comedi_subdevice *s = NULL;
int i; int i;
if (!dev)
return -ENODEV;
mutex_lock(&dev->mutex); mutex_lock(&dev->mutex);
if (dev->subdevices) { if (dev->subdevices) {
......
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