提交 c1cbb702 编写于 作者: J Jarod Wilson 提交者: Mauro Carvalho Chehab

[media] lirc_dev: rework storage for cdev data

Fixes an oops when an lirc driver that doesn't provide its own fops is
unplugged while the lirc cdev is open. Tested with lirc_igorplugusb,
with a special thanks to Timo Boettcher for providing the test hardware.
Signed-off-by: NJarod Wilson <jarod@redhat.com>
Signed-off-by: NMauro Carvalho Chehab <mchehab@redhat.com>
上级 09c8dd8d
...@@ -58,13 +58,12 @@ struct irctl { ...@@ -58,13 +58,12 @@ struct irctl {
struct task_struct *task; struct task_struct *task;
long jiffies_to_wait; long jiffies_to_wait;
struct cdev cdev;
}; };
static DEFINE_MUTEX(lirc_dev_lock); static DEFINE_MUTEX(lirc_dev_lock);
static struct irctl *irctls[MAX_IRCTL_DEVICES]; static struct irctl *irctls[MAX_IRCTL_DEVICES];
static struct cdev cdevs[MAX_IRCTL_DEVICES];
/* Only used for sysfs but defined to void otherwise */ /* Only used for sysfs but defined to void otherwise */
static struct class *lirc_class; static struct class *lirc_class;
...@@ -170,19 +169,20 @@ static int lirc_cdev_add(struct irctl *ir) ...@@ -170,19 +169,20 @@ static int lirc_cdev_add(struct irctl *ir)
{ {
int retval; int retval;
struct lirc_driver *d = &ir->d; struct lirc_driver *d = &ir->d;
struct cdev *cdev = &cdevs[d->minor];
if (d->fops) { if (d->fops) {
cdev_init(&ir->cdev, d->fops); cdev_init(cdev, d->fops);
ir->cdev.owner = d->owner; cdev->owner = d->owner;
} else { } else {
cdev_init(&ir->cdev, &lirc_dev_fops); cdev_init(cdev, &lirc_dev_fops);
ir->cdev.owner = THIS_MODULE; cdev->owner = THIS_MODULE;
} }
kobject_set_name(&ir->cdev.kobj, "lirc%d", d->minor); kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
retval = cdev_add(&ir->cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1); retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
if (retval) if (retval)
kobject_put(&ir->cdev.kobj); kobject_put(&cdev->kobj);
return retval; return retval;
} }
...@@ -363,6 +363,7 @@ EXPORT_SYMBOL(lirc_register_driver); ...@@ -363,6 +363,7 @@ EXPORT_SYMBOL(lirc_register_driver);
int lirc_unregister_driver(int minor) int lirc_unregister_driver(int minor)
{ {
struct irctl *ir; struct irctl *ir;
struct cdev *cdev;
if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between " printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between "
...@@ -377,6 +378,8 @@ int lirc_unregister_driver(int minor) ...@@ -377,6 +378,8 @@ int lirc_unregister_driver(int minor)
return -ENOENT; return -ENOENT;
} }
cdev = &cdevs[minor];
mutex_lock(&lirc_dev_lock); mutex_lock(&lirc_dev_lock);
if (ir->d.minor != minor) { if (ir->d.minor != minor) {
...@@ -400,11 +403,11 @@ int lirc_unregister_driver(int minor) ...@@ -400,11 +403,11 @@ int lirc_unregister_driver(int minor)
wake_up_interruptible(&ir->buf->wait_poll); wake_up_interruptible(&ir->buf->wait_poll);
mutex_lock(&ir->irctl_lock); mutex_lock(&ir->irctl_lock);
ir->d.set_use_dec(ir->d.data); ir->d.set_use_dec(ir->d.data);
module_put(ir->cdev.owner); module_put(cdev->owner);
mutex_unlock(&ir->irctl_lock); mutex_unlock(&ir->irctl_lock);
} else { } else {
lirc_irctl_cleanup(ir); lirc_irctl_cleanup(ir);
cdev_del(&ir->cdev); cdev_del(cdev);
kfree(ir); kfree(ir);
irctls[minor] = NULL; irctls[minor] = NULL;
} }
...@@ -418,6 +421,7 @@ EXPORT_SYMBOL(lirc_unregister_driver); ...@@ -418,6 +421,7 @@ EXPORT_SYMBOL(lirc_unregister_driver);
int lirc_dev_fop_open(struct inode *inode, struct file *file) int lirc_dev_fop_open(struct inode *inode, struct file *file)
{ {
struct irctl *ir; struct irctl *ir;
struct cdev *cdev;
int retval = 0; int retval = 0;
if (iminor(inode) >= MAX_IRCTL_DEVICES) { if (iminor(inode) >= MAX_IRCTL_DEVICES) {
...@@ -447,13 +451,14 @@ int lirc_dev_fop_open(struct inode *inode, struct file *file) ...@@ -447,13 +451,14 @@ int lirc_dev_fop_open(struct inode *inode, struct file *file)
goto error; goto error;
} }
if (try_module_get(ir->cdev.owner)) { cdev = &cdevs[iminor(inode)];
++ir->open; if (try_module_get(cdev->owner)) {
ir->open++;
retval = ir->d.set_use_inc(ir->d.data); retval = ir->d.set_use_inc(ir->d.data);
if (retval) { if (retval) {
module_put(ir->cdev.owner); module_put(cdev->owner);
--ir->open; ir->open--;
} else { } else {
lirc_buffer_clear(ir->buf); lirc_buffer_clear(ir->buf);
} }
...@@ -475,6 +480,7 @@ EXPORT_SYMBOL(lirc_dev_fop_open); ...@@ -475,6 +480,7 @@ EXPORT_SYMBOL(lirc_dev_fop_open);
int lirc_dev_fop_close(struct inode *inode, struct file *file) int lirc_dev_fop_close(struct inode *inode, struct file *file)
{ {
struct irctl *ir = irctls[iminor(inode)]; struct irctl *ir = irctls[iminor(inode)];
struct cdev *cdev = &cdevs[iminor(inode)];
if (!ir) { if (!ir) {
printk(KERN_ERR "%s: called with invalid irctl\n", __func__); printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
...@@ -488,10 +494,10 @@ int lirc_dev_fop_close(struct inode *inode, struct file *file) ...@@ -488,10 +494,10 @@ int lirc_dev_fop_close(struct inode *inode, struct file *file)
ir->open--; ir->open--;
if (ir->attached) { if (ir->attached) {
ir->d.set_use_dec(ir->d.data); ir->d.set_use_dec(ir->d.data);
module_put(ir->cdev.owner); module_put(cdev->owner);
} else { } else {
lirc_irctl_cleanup(ir); lirc_irctl_cleanup(ir);
cdev_del(&ir->cdev); cdev_del(cdev);
irctls[ir->d.minor] = NULL; irctls[ir->d.minor] = NULL;
kfree(ir); kfree(ir);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册