2010-07-03 12:06:57 +08:00
|
|
|
/*
|
|
|
|
* LIRC base driver
|
|
|
|
*
|
|
|
|
* by Artur Lipowski <alipowski@interia.pl>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-07-06 17:01:16 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
#include <linux/module.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/cdev.h>
|
2017-06-25 20:32:05 +08:00
|
|
|
#include <linux/idr.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
#include "rc-core-priv.h"
|
2010-07-03 12:06:57 +08:00
|
|
|
#include <media/lirc.h>
|
2010-07-17 01:25:33 +08:00
|
|
|
#include <media/lirc_dev.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
#define LOGHEAD "lirc_dev (%s[%d]): "
|
|
|
|
|
|
|
|
static dev_t lirc_base_dev;
|
|
|
|
|
2017-06-25 20:32:05 +08:00
|
|
|
/* Used to keep track of allocated lirc devices */
|
|
|
|
#define LIRC_MAX_DEVICES 256
|
|
|
|
static DEFINE_IDA(lirc_ida);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
/* Only used for sysfs but defined to void otherwise */
|
|
|
|
static struct class *lirc_class;
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
static void lirc_release_device(struct device *ld)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
struct lirc_dev *d = container_of(ld, struct lirc_dev, dev);
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
put_device(d->dev.parent);
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (d->buf_internal) {
|
|
|
|
lirc_buffer_free(d->buf);
|
|
|
|
kfree(d->buf);
|
|
|
|
d->buf = NULL;
|
|
|
|
}
|
|
|
|
kfree(d);
|
|
|
|
module_put(THIS_MODULE);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
static int lirc_allocate_buffer(struct lirc_dev *d)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
int err;
|
2016-07-06 17:01:13 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (d->buf) {
|
|
|
|
d->buf_internal = false;
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-06 17:01:13 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
d->buf = kmalloc(sizeof(*d->buf), GFP_KERNEL);
|
|
|
|
if (!d->buf)
|
|
|
|
return -ENOMEM;
|
2017-05-01 21:32:34 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
err = lirc_buffer_init(d->buf, d->chunk_size, d->buffer_size);
|
|
|
|
if (err) {
|
|
|
|
kfree(d->buf);
|
|
|
|
d->buf = NULL;
|
|
|
|
return err;
|
2016-07-06 17:01:13 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
d->buf_internal = true;
|
|
|
|
return 0;
|
2016-07-06 17:01:13 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:15 +08:00
|
|
|
struct lirc_dev *
|
|
|
|
lirc_allocate_device(void)
|
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
struct lirc_dev *d;
|
|
|
|
|
|
|
|
d = kzalloc(sizeof(*d), GFP_KERNEL);
|
|
|
|
if (d) {
|
|
|
|
mutex_init(&d->mutex);
|
|
|
|
device_initialize(&d->dev);
|
|
|
|
d->dev.class = lirc_class;
|
|
|
|
d->dev.release = lirc_release_device;
|
|
|
|
__module_get(THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
2017-06-25 20:32:15 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_allocate_device);
|
|
|
|
|
|
|
|
void lirc_free_device(struct lirc_dev *d)
|
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d)
|
|
|
|
return;
|
|
|
|
|
|
|
|
put_device(&d->dev);
|
2017-06-25 20:32:15 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_free_device);
|
|
|
|
|
2017-09-22 03:13:34 +08:00
|
|
|
int lirc_register_device(struct lirc_dev *d)
|
2016-07-06 17:01:13 +08:00
|
|
|
{
|
2017-09-24 00:05:59 +08:00
|
|
|
struct rc_dev *rcdev = d->rdev;
|
2017-06-25 20:32:05 +08:00
|
|
|
int minor;
|
2010-07-03 12:06:57 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!d) {
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_err("driver pointer must be not NULL!\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return -EBADRQC;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->dev.parent) {
|
|
|
|
pr_err("dev parent pointer not filled in!\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return -EINVAL;
|
2010-10-18 23:02:01 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 00:04:21 +08:00
|
|
|
if (!d->fops) {
|
|
|
|
pr_err("fops pointer not filled in!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->buf && d->chunk_size < 1) {
|
2017-06-25 20:31:45 +08:00
|
|
|
pr_err("chunk_size must be set!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->buf && d->buffer_size < 1) {
|
2017-06-25 20:31:45 +08:00
|
|
|
pr_err("buffer_size must be set!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->buf && !(d->fops && d->fops->read &&
|
|
|
|
d->fops->poll && d->fops->unlocked_ioctl)) {
|
|
|
|
dev_err(&d->dev, "undefined read, poll, ioctl\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return -EBADRQC;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
/* some safety check 8-) */
|
|
|
|
d->name[sizeof(d->name) - 1] = '\0';
|
|
|
|
|
2017-09-24 00:05:59 +08:00
|
|
|
if (rcdev->driver_type == RC_DRIVER_IR_RAW) {
|
2017-06-25 20:32:36 +08:00
|
|
|
err = lirc_allocate_buffer(d);
|
|
|
|
if (err)
|
2017-06-30 16:41:57 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:05 +08:00
|
|
|
minor = ida_simple_get(&lirc_ida, 0, LIRC_MAX_DEVICES, GFP_KERNEL);
|
2017-06-25 20:32:36 +08:00
|
|
|
if (minor < 0)
|
2017-06-25 20:32:05 +08:00
|
|
|
return minor;
|
2017-05-02 00:04:21 +08:00
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
d->minor = minor;
|
2017-06-25 20:32:36 +08:00
|
|
|
d->dev.devt = MKDEV(MAJOR(lirc_base_dev), d->minor);
|
|
|
|
dev_set_name(&d->dev, "lirc%d", d->minor);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
cdev_init(&d->cdev, d->fops);
|
|
|
|
d->cdev.owner = d->owner;
|
|
|
|
d->attached = true;
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
err = cdev_device_add(&d->cdev, &d->dev);
|
2017-06-30 16:41:57 +08:00
|
|
|
if (err) {
|
2017-06-25 20:32:05 +08:00
|
|
|
ida_simple_remove(&lirc_ida, minor);
|
2017-06-30 16:41:57 +08:00
|
|
|
return err;
|
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
get_device(d->dev.parent);
|
2017-08-04 22:12:03 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_info(&d->dev, "lirc_dev: driver %s registered at minor = %d\n",
|
|
|
|
d->name, d->minor);
|
2017-05-02 00:04:11 +08:00
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
return 0;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
2017-09-22 03:13:34 +08:00
|
|
|
EXPORT_SYMBOL(lirc_register_device);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-22 03:13:34 +08:00
|
|
|
void lirc_unregister_device(struct lirc_dev *d)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d)
|
2017-06-25 20:31:24 +08:00
|
|
|
return;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_dbg(&d->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
|
2017-06-25 20:31:24 +08:00
|
|
|
d->name, d->minor);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_lock(&d->mutex);
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
d->attached = false;
|
|
|
|
if (d->open) {
|
|
|
|
dev_dbg(&d->dev, LOGHEAD "releasing opened driver\n",
|
2017-06-25 20:31:24 +08:00
|
|
|
d->name, d->minor);
|
2017-06-25 20:32:36 +08:00
|
|
|
wake_up_interruptible(&d->buf->wait_poll);
|
2017-01-30 23:49:58 +08:00
|
|
|
}
|
2016-07-06 17:01:26 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
cdev_device_del(&d->cdev, &d->dev);
|
2017-06-25 20:32:05 +08:00
|
|
|
ida_simple_remove(&lirc_ida, d->minor);
|
2017-06-25 20:32:36 +08:00
|
|
|
put_device(&d->dev);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
2017-09-22 03:13:34 +08:00
|
|
|
EXPORT_SYMBOL(lirc_unregister_device);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
int lirc_dev_fop_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev);
|
2017-06-25 20:31:19 +08:00
|
|
|
int retval;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_dbg(&d->dev, LOGHEAD "open called\n", d->name, d->minor);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
retval = mutex_lock_interruptible(&d->mutex);
|
2017-06-30 16:41:57 +08:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->attached) {
|
2017-06-30 16:41:57 +08:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (d->open) {
|
2017-06-30 16:41:57 +08:00
|
|
|
retval = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (d->rdev) {
|
|
|
|
retval = rc_open(d->rdev);
|
[media] media: lirc: Allow lirc dev to talk to rc device
The use case is simple, if any rc device has allowed protocols =
RC_TYPE_LIRC and map_name = RC_MAP_LIRC set, the driver open will be never
called. The reason for this is, all of the key maps except lirc have some
KEYS in there map, so during rc_register_device process these keys are
matched against the input drivers and open is performed, so for the case
of RC_MAP_EMPTY, a vt/keyboard is matched and the driver open is
performed.
In case of lirc, there is no match and result is that there is no open
performed, however the lirc-dev will go ahead and create a /dev/lirc0
node. Now when lircd/mode2 opens this device, no data is available
because the driver was never opened.
Other case pointed by Sean Young, As rc device gets opened via the
input interface. If the input device is never opened (e.g. embedded with
no console) then the rc open is never called and lirc will not work
either. So that's another case.
lirc_dev seems to have no link with actual rc device w.r.t open/close.
This patch adds rc_dev pointer to lirc_driver structure for cases like
this, so that it can do the open/close of the real driver in accordance
to lircd/mode2 open/close.
Without this patch its impossible to open a rc device which has
RC_TYPE_LIRC ad RC_MAP_LIRC set.
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@st.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2013-07-22 15:23:07 +08:00
|
|
|
if (retval)
|
2017-06-30 16:41:57 +08:00
|
|
|
goto out;
|
[media] media: lirc: Allow lirc dev to talk to rc device
The use case is simple, if any rc device has allowed protocols =
RC_TYPE_LIRC and map_name = RC_MAP_LIRC set, the driver open will be never
called. The reason for this is, all of the key maps except lirc have some
KEYS in there map, so during rc_register_device process these keys are
matched against the input drivers and open is performed, so for the case
of RC_MAP_EMPTY, a vt/keyboard is matched and the driver open is
performed.
In case of lirc, there is no match and result is that there is no open
performed, however the lirc-dev will go ahead and create a /dev/lirc0
node. Now when lircd/mode2 opens this device, no data is available
because the driver was never opened.
Other case pointed by Sean Young, As rc device gets opened via the
input interface. If the input device is never opened (e.g. embedded with
no console) then the rc open is never called and lirc will not work
either. So that's another case.
lirc_dev seems to have no link with actual rc device w.r.t open/close.
This patch adds rc_dev pointer to lirc_driver structure for cases like
this, so that it can do the open/close of the real driver in accordance
to lircd/mode2 open/close.
Without this patch its impossible to open a rc device which has
RC_TYPE_LIRC ad RC_MAP_LIRC set.
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@st.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2013-07-22 15:23:07 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (d->buf)
|
|
|
|
lirc_buffer_clear(d->buf);
|
2017-05-02 00:03:46 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
d->open++;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
file->private_data = d->rdev;
|
2010-08-16 00:51:56 +08:00
|
|
|
nonseekable_open(inode, file);
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-08-16 00:51:56 +08:00
|
|
|
|
2017-06-25 20:31:19 +08:00
|
|
|
return 0;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
|
|
|
out:
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_unlock(&d->mutex);
|
2017-06-30 16:41:57 +08:00
|
|
|
return retval;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_open);
|
|
|
|
|
|
|
|
int lirc_dev_fop_close(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-09-23 22:41:13 +08:00
|
|
|
struct rc_dev *rcdev = file->private_data;
|
|
|
|
struct lirc_dev *d = rcdev->lirc_dev;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_lock(&d->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
rc_close(rcdev);
|
2017-06-25 20:32:36 +08:00
|
|
|
d->open--;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_close);
|
|
|
|
|
|
|
|
unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
|
|
|
|
{
|
2017-09-23 22:41:13 +08:00
|
|
|
struct rc_dev *rcdev = file->private_data;
|
|
|
|
struct lirc_dev *d = rcdev->lirc_dev;
|
2010-07-03 12:06:57 +08:00
|
|
|
unsigned int ret;
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->attached)
|
2017-05-02 00:04:37 +08:00
|
|
|
return POLLHUP | POLLERR;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (d->buf) {
|
|
|
|
poll_wait(file, &d->buf->wait_poll, wait);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (lirc_buffer_empty(d->buf))
|
2010-07-03 12:06:57 +08:00
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = POLLIN | POLLRDNORM;
|
2017-06-25 20:32:36 +08:00
|
|
|
} else {
|
2010-07-03 12:06:57 +08:00
|
|
|
ret = POLLERR;
|
2017-06-25 20:32:36 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_dbg(&d->dev, LOGHEAD "poll result = %d\n", d->name, d->minor, ret);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_poll);
|
|
|
|
|
|
|
|
ssize_t lirc_dev_fop_read(struct file *file,
|
2010-11-17 13:13:39 +08:00
|
|
|
char __user *buffer,
|
2010-07-03 12:06:57 +08:00
|
|
|
size_t length,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
2017-09-23 22:41:13 +08:00
|
|
|
struct rc_dev *rcdev = file->private_data;
|
|
|
|
struct lirc_dev *d = rcdev->lirc_dev;
|
2010-10-18 23:02:01 +08:00
|
|
|
unsigned char *buf;
|
2017-06-30 16:41:57 +08:00
|
|
|
int ret, written = 0;
|
2010-07-03 12:06:57 +08:00
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
buf = kzalloc(d->buf->chunk_size, GFP_KERNEL);
|
2010-10-18 23:02:01 +08:00
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_dbg(&d->dev, LOGHEAD "read called\n", d->name, d->minor);
|
|
|
|
|
|
|
|
ret = mutex_lock_interruptible(&d->mutex);
|
2017-06-30 16:41:57 +08:00
|
|
|
if (ret) {
|
|
|
|
kfree(buf);
|
|
|
|
return ret;
|
2010-11-17 13:20:15 +08:00
|
|
|
}
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->attached) {
|
2010-11-17 13:20:15 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto out_locked;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-09-24 00:05:59 +08:00
|
|
|
if (rcdev->driver_type != RC_DRIVER_IR_RAW) {
|
2017-06-30 16:41:57 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out_locked;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (length % d->buf->chunk_size) {
|
2010-11-17 13:20:15 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out_locked;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we add ourselves to the task queue before buffer check
|
|
|
|
* to avoid losing scan code (in case when queue is awaken somewhere
|
|
|
|
* between while condition checking and scheduling)
|
|
|
|
*/
|
2017-06-25 20:32:36 +08:00
|
|
|
add_wait_queue(&d->buf->wait_poll, &wait);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* while we didn't provide 'length' bytes, device is opened in blocking
|
|
|
|
* mode and 'copy_to_user' is happy, wait for data.
|
|
|
|
*/
|
|
|
|
while (written < length && ret == 0) {
|
2017-06-25 20:32:36 +08:00
|
|
|
if (lirc_buffer_empty(d->buf)) {
|
2010-07-03 12:06:57 +08:00
|
|
|
/* According to the read(2) man page, 'written' can be
|
|
|
|
* returned as less than 'length', instead of blocking
|
|
|
|
* again, returning -EWOULDBLOCK, or returning
|
2016-07-06 17:01:25 +08:00
|
|
|
* -ERESTARTSYS
|
|
|
|
*/
|
2010-07-03 12:06:57 +08:00
|
|
|
if (written)
|
|
|
|
break;
|
|
|
|
if (file->f_flags & O_NONBLOCK) {
|
|
|
|
ret = -EWOULDBLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (signal_pending(current)) {
|
|
|
|
ret = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
2016-11-01 01:52:25 +08:00
|
|
|
schedule();
|
|
|
|
set_current_state(TASK_RUNNING);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
ret = mutex_lock_interruptible(&d->mutex);
|
2017-06-30 16:41:57 +08:00
|
|
|
if (ret) {
|
2017-06-25 20:32:36 +08:00
|
|
|
remove_wait_queue(&d->buf->wait_poll, &wait);
|
V4L/DVB: IR/lirc_dev: fix locking in lirc_dev_fop_read
On Wed, Jul 07, 2010 at 02:52:58PM +0200, Jiri Slaby wrote:
> Hi,
>
> stanse found a locking error in lirc_dev_fop_read:
> if (mutex_lock_interruptible(&ir->irctl_lock))
> return -ERESTARTSYS;
> ...
> while (written < length && ret == 0) {
> if (mutex_lock_interruptible(&ir->irctl_lock)) { #1
> ret = -ERESTARTSYS;
> break;
> }
> ...
> }
>
> remove_wait_queue(&ir->buf->wait_poll, &wait);
> set_current_state(TASK_RUNNING);
> mutex_unlock(&ir->irctl_lock); #2
>
> If lock at #1 fails, it beaks out of the loop, with the lock unlocked,
> but there is another "unlock" at #2.
This should do the trick. Completely untested beyond compiling, but its
not exactly a complicated fix, and in practice, I'm not aware of anyone
ever actually tripping that locking bug, so there's zero functional change
in typical use here.
Signed-off-by: Jarod Wilson <jarod@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
2010-07-07 22:29:44 +08:00
|
|
|
goto out_unlocked;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->attached) {
|
2010-07-03 12:06:57 +08:00
|
|
|
ret = -ENODEV;
|
2016-11-01 01:52:27 +08:00
|
|
|
goto out_locked;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-25 20:32:36 +08:00
|
|
|
lirc_buffer_read(d->buf, buf);
|
2014-08-21 06:41:03 +08:00
|
|
|
ret = copy_to_user((void __user *)buffer+written, buf,
|
2017-06-25 20:32:36 +08:00
|
|
|
d->buf->chunk_size);
|
2010-11-17 13:20:15 +08:00
|
|
|
if (!ret)
|
2017-06-25 20:32:36 +08:00
|
|
|
written += d->buf->chunk_size;
|
2010-11-17 13:20:15 +08:00
|
|
|
else
|
|
|
|
ret = -EFAULT;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
remove_wait_queue(&d->buf->wait_poll, &wait);
|
2010-11-17 13:20:15 +08:00
|
|
|
|
|
|
|
out_locked:
|
2017-06-25 20:32:36 +08:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
V4L/DVB: IR/lirc_dev: fix locking in lirc_dev_fop_read
On Wed, Jul 07, 2010 at 02:52:58PM +0200, Jiri Slaby wrote:
> Hi,
>
> stanse found a locking error in lirc_dev_fop_read:
> if (mutex_lock_interruptible(&ir->irctl_lock))
> return -ERESTARTSYS;
> ...
> while (written < length && ret == 0) {
> if (mutex_lock_interruptible(&ir->irctl_lock)) { #1
> ret = -ERESTARTSYS;
> break;
> }
> ...
> }
>
> remove_wait_queue(&ir->buf->wait_poll, &wait);
> set_current_state(TASK_RUNNING);
> mutex_unlock(&ir->irctl_lock); #2
>
> If lock at #1 fails, it beaks out of the loop, with the lock unlocked,
> but there is another "unlock" at #2.
This should do the trick. Completely untested beyond compiling, but its
not exactly a complicated fix, and in practice, I'm not aware of anyone
ever actually tripping that locking bug, so there's zero functional change
in typical use here.
Signed-off-by: Jarod Wilson <jarod@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
2010-07-07 22:29:44 +08:00
|
|
|
out_unlocked:
|
2010-10-18 23:02:01 +08:00
|
|
|
kfree(buf);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
return ret ? ret : written;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_read);
|
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
int __init lirc_dev_init(void)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
lirc_class = class_create(THIS_MODULE, "lirc");
|
|
|
|
if (IS_ERR(lirc_class)) {
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_err("class_create failed\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return PTR_ERR(lirc_class);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:05 +08:00
|
|
|
retval = alloc_chrdev_region(&lirc_base_dev, 0, LIRC_MAX_DEVICES,
|
2017-05-02 00:04:47 +08:00
|
|
|
"BaseRemoteCtl");
|
2010-07-03 12:06:57 +08:00
|
|
|
if (retval) {
|
|
|
|
class_destroy(lirc_class);
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_err("alloc_chrdev_region failed\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return retval;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_info("IR Remote Control driver registered, major %d\n",
|
|
|
|
MAJOR(lirc_base_dev));
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2016-07-06 17:01:17 +08:00
|
|
|
return 0;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
void __exit lirc_dev_exit(void)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
|
|
|
class_destroy(lirc_class);
|
2017-06-25 20:32:05 +08:00
|
|
|
unregister_chrdev_region(lirc_base_dev, LIRC_MAX_DEVICES);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|