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>
|
|
|
|
|
[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
|
|
|
#include <media/rc-core.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;
|
|
|
|
|
|
|
|
struct irctl {
|
|
|
|
struct lirc_driver d;
|
2017-06-25 20:31:55 +08:00
|
|
|
bool attached;
|
2010-07-03 12:06:57 +08:00
|
|
|
int open;
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
struct mutex mutex; /* protect from simultaneous accesses */
|
2010-07-03 12:06:57 +08:00
|
|
|
struct lirc_buffer *buf;
|
2017-05-01 21:32:34 +08:00
|
|
|
bool buf_internal;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-01-30 23:49:58 +08:00
|
|
|
struct device dev;
|
|
|
|
struct cdev cdev;
|
2010-07-03 12:06:57 +08:00
|
|
|
};
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
/* This mutex protects the irctls array */
|
2010-07-03 12:06:57 +08:00
|
|
|
static DEFINE_MUTEX(lirc_dev_lock);
|
|
|
|
|
|
|
|
static struct irctl *irctls[MAX_IRCTL_DEVICES];
|
|
|
|
|
|
|
|
/* Only used for sysfs but defined to void otherwise */
|
|
|
|
static struct class *lirc_class;
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
static void lirc_free_buffer(struct irctl *ir)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-08-04 22:12:03 +08:00
|
|
|
put_device(ir->dev.parent);
|
|
|
|
|
2017-05-01 21:32:34 +08:00
|
|
|
if (ir->buf_internal) {
|
2010-07-03 12:06:57 +08:00
|
|
|
lirc_buffer_free(ir->buf);
|
|
|
|
kfree(ir->buf);
|
2017-06-30 16:41:57 +08:00
|
|
|
ir->buf = NULL;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
2017-06-30 16:41:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lirc_release(struct device *ld)
|
|
|
|
{
|
|
|
|
struct irctl *ir = container_of(ld, struct irctl, dev);
|
2017-01-30 23:49:58 +08:00
|
|
|
|
|
|
|
mutex_lock(&lirc_dev_lock);
|
|
|
|
irctls[ir->d.minor] = NULL;
|
|
|
|
mutex_unlock(&lirc_dev_lock);
|
2017-06-30 16:41:57 +08:00
|
|
|
lirc_free_buffer(ir);
|
2017-01-30 23:49:58 +08:00
|
|
|
kfree(ir);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:01:13 +08:00
|
|
|
static int lirc_allocate_buffer(struct irctl *ir)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2016-07-06 17:01:14 +08:00
|
|
|
int err = 0;
|
2016-07-06 17:01:13 +08:00
|
|
|
struct lirc_driver *d = &ir->d;
|
|
|
|
|
|
|
|
if (d->rbuf) {
|
|
|
|
ir->buf = d->rbuf;
|
2017-05-01 21:32:34 +08:00
|
|
|
ir->buf_internal = false;
|
2016-07-06 17:01:13 +08:00
|
|
|
} else {
|
|
|
|
ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
|
2016-07-06 17:01:14 +08:00
|
|
|
if (!ir->buf) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-07-06 17:01:13 +08:00
|
|
|
|
2017-06-25 20:31:45 +08:00
|
|
|
err = lirc_buffer_init(ir->buf, d->chunk_size, d->buffer_size);
|
2016-07-06 17:01:13 +08:00
|
|
|
if (err) {
|
|
|
|
kfree(ir->buf);
|
2017-05-01 21:32:34 +08:00
|
|
|
ir->buf = NULL;
|
2016-07-06 17:01:14 +08:00
|
|
|
goto out;
|
2016-07-06 17:01:13 +08:00
|
|
|
}
|
2017-05-01 21:32:34 +08:00
|
|
|
|
|
|
|
ir->buf_internal = true;
|
2017-05-02 00:04:11 +08:00
|
|
|
d->rbuf = ir->buf;
|
2016-07-06 17:01:13 +08:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:01:14 +08:00
|
|
|
out:
|
|
|
|
return err;
|
2016-07-06 17:01:13 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 00:04:11 +08:00
|
|
|
int lirc_register_driver(struct lirc_driver *d)
|
2016-07-06 17:01:13 +08:00
|
|
|
{
|
|
|
|
struct irctl *ir;
|
2017-06-25 20:31:24 +08:00
|
|
|
unsigned 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
|
|
|
}
|
|
|
|
|
2010-10-18 23:02:01 +08:00
|
|
|
if (!d->dev) {
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_err("dev 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:31:45 +08:00
|
|
|
if (!d->rbuf && d->chunk_size < 1) {
|
|
|
|
pr_err("chunk_size must be set!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->rbuf && d->buffer_size < 1) {
|
|
|
|
pr_err("buffer_size must be set!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:01:23 +08:00
|
|
|
if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
|
2016-07-06 17:01:16 +08:00
|
|
|
dev_err(d->dev, "code length must be less than %d bits\n",
|
|
|
|
BUFLEN * 8);
|
2016-07-06 17:01:17 +08:00
|
|
|
return -EBADRQC;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 00:03:56 +08:00
|
|
|
if (!d->rbuf && !(d->fops && d->fops->read &&
|
|
|
|
d->fops->poll && d->fops->unlocked_ioctl)) {
|
2016-07-06 17:01:21 +08:00
|
|
|
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';
|
|
|
|
|
|
|
|
if (d->features == 0)
|
|
|
|
d->features = LIRC_CAN_REC_LIRCCODE;
|
|
|
|
|
|
|
|
ir = kzalloc(sizeof(*ir), GFP_KERNEL);
|
|
|
|
if (!ir)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_init(&ir->mutex);
|
|
|
|
ir->d = *d;
|
|
|
|
|
|
|
|
if (LIRC_CAN_REC(d->features)) {
|
|
|
|
err = lirc_allocate_buffer(ir);
|
|
|
|
if (err) {
|
|
|
|
kfree(ir);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
d->rbuf = ir->buf;
|
|
|
|
}
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
mutex_lock(&lirc_dev_lock);
|
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
/* find first free slot for driver */
|
|
|
|
for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
|
|
|
|
if (!irctls[minor])
|
|
|
|
break;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
if (minor == MAX_IRCTL_DEVICES) {
|
|
|
|
dev_err(d->dev, "no free slots for drivers!\n");
|
2017-06-30 16:41:57 +08:00
|
|
|
mutex_unlock(&lirc_dev_lock);
|
|
|
|
lirc_free_buffer(ir);
|
|
|
|
kfree(ir);
|
|
|
|
return -ENOMEM;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
2017-05-02 00:04:21 +08:00
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
irctls[minor] = ir;
|
2017-06-25 20:31:24 +08:00
|
|
|
d->irctl = ir;
|
2010-07-03 12:06:57 +08:00
|
|
|
d->minor = minor;
|
2017-06-30 16:41:57 +08:00
|
|
|
ir->d.minor = minor;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
mutex_unlock(&lirc_dev_lock);
|
2017-05-02 00:04:11 +08:00
|
|
|
|
2017-05-02 00:04:01 +08:00
|
|
|
device_initialize(&ir->dev);
|
2017-01-30 23:49:58 +08:00
|
|
|
ir->dev.devt = MKDEV(MAJOR(lirc_base_dev), ir->d.minor);
|
|
|
|
ir->dev.class = lirc_class;
|
|
|
|
ir->dev.parent = d->dev;
|
|
|
|
ir->dev.release = lirc_release;
|
|
|
|
dev_set_name(&ir->dev, "lirc%d", ir->d.minor);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-05-02 00:04:21 +08:00
|
|
|
cdev_init(&ir->cdev, d->fops);
|
|
|
|
ir->cdev.owner = ir->d.owner;
|
2017-06-25 20:31:55 +08:00
|
|
|
ir->attached = true;
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-06-25 20:31:35 +08:00
|
|
|
err = cdev_device_add(&ir->cdev, &ir->dev);
|
2017-06-30 16:41:57 +08:00
|
|
|
if (err) {
|
|
|
|
put_device(&ir->dev);
|
|
|
|
return err;
|
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-08-04 22:12:03 +08:00
|
|
|
get_device(ir->dev.parent);
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
|
|
|
|
ir->d.name, ir->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
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_register_driver);
|
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
void lirc_unregister_driver(struct lirc_driver *d)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
|
|
|
struct irctl *ir;
|
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
if (!d || !d->irctl)
|
|
|
|
return;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
ir = d->irctl;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
dev_dbg(ir->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-30 16:41:57 +08:00
|
|
|
cdev_device_del(&ir->cdev, &ir->dev);
|
|
|
|
|
|
|
|
mutex_lock(&ir->mutex);
|
|
|
|
|
2017-06-25 20:31:55 +08:00
|
|
|
ir->attached = false;
|
2010-07-03 12:06:57 +08:00
|
|
|
if (ir->open) {
|
|
|
|
dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
|
2017-06-25 20:31:24 +08:00
|
|
|
d->name, d->minor);
|
2010-07-03 12:06:57 +08:00
|
|
|
wake_up_interruptible(&ir->buf->wait_poll);
|
2017-01-30 23:49:58 +08:00
|
|
|
}
|
2016-07-06 17:01:26 +08:00
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
mutex_unlock(&ir->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-01-30 23:49:58 +08:00
|
|
|
put_device(&ir->dev);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_unregister_driver);
|
|
|
|
|
|
|
|
int lirc_dev_fop_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-06-25 20:31:40 +08:00
|
|
|
struct irctl *ir = container_of(inode->i_cdev, struct irctl, cdev);
|
2017-06-25 20:31:19 +08:00
|
|
|
int retval;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
retval = mutex_lock_interruptible(&ir->mutex);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (!ir->attached) {
|
|
|
|
retval = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ir->open) {
|
|
|
|
retval = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
[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 (ir->d.rdev) {
|
|
|
|
retval = rc_open(ir->d.rdev);
|
|
|
|
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-05-02 00:03:46 +08:00
|
|
|
if (ir->buf)
|
|
|
|
lirc_buffer_clear(ir->buf);
|
|
|
|
|
2017-01-30 23:49:58 +08:00
|
|
|
ir->open++;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:31:40 +08:00
|
|
|
lirc_init_pdata(inode, file);
|
2010-08-16 00:51:56 +08:00
|
|
|
nonseekable_open(inode, file);
|
2017-06-30 16:41:57 +08:00
|
|
|
mutex_unlock(&ir->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:
|
|
|
|
mutex_unlock(&ir->mutex);
|
|
|
|
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-06-25 20:31:40 +08:00
|
|
|
struct irctl *ir = file->private_data;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
mutex_lock(&ir->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2014-11-20 20:01:32 +08:00
|
|
|
rc_close(ir->d.rdev);
|
2010-10-18 23:02:01 +08:00
|
|
|
ir->open--;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
|
|
|
mutex_unlock(&ir->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-06-25 20:31:40 +08:00
|
|
|
struct irctl *ir = file->private_data;
|
2010-07-03 12:06:57 +08:00
|
|
|
unsigned int ret;
|
|
|
|
|
2010-11-17 13:12:23 +08:00
|
|
|
if (!ir->attached)
|
2017-05-02 00:04:37 +08:00
|
|
|
return POLLHUP | POLLERR;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2015-01-07 09:53:37 +08:00
|
|
|
if (ir->buf) {
|
|
|
|
poll_wait(file, &ir->buf->wait_poll, wait);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
if (lirc_buffer_empty(ir->buf))
|
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = POLLIN | POLLRDNORM;
|
2015-01-07 09:53:37 +08:00
|
|
|
} else
|
2010-07-03 12:06:57 +08:00
|
|
|
ret = POLLERR;
|
|
|
|
|
|
|
|
dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
|
|
|
|
ir->d.name, ir->d.minor, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_poll);
|
|
|
|
|
2010-08-03 02:43:35 +08:00
|
|
|
long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-06-25 20:31:40 +08:00
|
|
|
struct irctl *ir = file->private_data;
|
2010-10-09 04:24:21 +08:00
|
|
|
__u32 mode;
|
2017-06-30 16:41:57 +08:00
|
|
|
int result;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
|
|
|
|
ir->d.name, ir->d.minor, cmd);
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
result = mutex_lock_interruptible(&ir->mutex);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
if (!ir->attached) {
|
2017-06-30 16:41:57 +08:00
|
|
|
result = -ENODEV;
|
|
|
|
goto out;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case LIRC_GET_FEATURES:
|
2014-08-21 06:41:03 +08:00
|
|
|
result = put_user(ir->d.features, (__u32 __user *)arg);
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
case LIRC_GET_REC_MODE:
|
2016-12-03 01:16:08 +08:00
|
|
|
if (!LIRC_CAN_REC(ir->d.features)) {
|
2016-07-06 17:01:24 +08:00
|
|
|
result = -ENOTTY;
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = put_user(LIRC_REC2MODE
|
|
|
|
(ir->d.features & LIRC_CAN_REC_MASK),
|
2014-08-21 06:41:03 +08:00
|
|
|
(__u32 __user *)arg);
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
case LIRC_SET_REC_MODE:
|
2016-12-03 01:16:08 +08:00
|
|
|
if (!LIRC_CAN_REC(ir->d.features)) {
|
2016-07-06 17:01:24 +08:00
|
|
|
result = -ENOTTY;
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:41:03 +08:00
|
|
|
result = get_user(mode, (__u32 __user *)arg);
|
2010-07-03 12:06:57 +08:00
|
|
|
if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
|
|
|
|
result = -EINVAL;
|
|
|
|
/*
|
|
|
|
* FIXME: We should actually set the mode somehow but
|
|
|
|
* for now, lirc_serial doesn't support mode changing either
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case LIRC_GET_LENGTH:
|
2014-08-21 06:41:03 +08:00
|
|
|
result = put_user(ir->d.code_length, (__u32 __user *)arg);
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
case LIRC_GET_MIN_TIMEOUT:
|
|
|
|
if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
|
|
|
|
ir->d.min_timeout == 0) {
|
2016-07-06 17:01:24 +08:00
|
|
|
result = -ENOTTY;
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:41:03 +08:00
|
|
|
result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
case LIRC_GET_MAX_TIMEOUT:
|
|
|
|
if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
|
|
|
|
ir->d.max_timeout == 0) {
|
2016-07-06 17:01:24 +08:00
|
|
|
result = -ENOTTY;
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:41:03 +08:00
|
|
|
result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
default:
|
2017-01-27 01:19:33 +08:00
|
|
|
result = -ENOTTY;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
out:
|
|
|
|
mutex_unlock(&ir->mutex);
|
2010-07-03 12:06:57 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_ioctl);
|
|
|
|
|
|
|
|
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-06-25 20:31:40 +08:00
|
|
|
struct irctl *ir = file->private_data;
|
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);
|
|
|
|
|
|
|
|
dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
|
|
|
|
|
2017-06-25 20:31:45 +08:00
|
|
|
buf = kzalloc(ir->buf->chunk_size, GFP_KERNEL);
|
2010-10-18 23:02:01 +08:00
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
ret = mutex_lock_interruptible(&ir->mutex);
|
|
|
|
if (ret) {
|
|
|
|
kfree(buf);
|
|
|
|
return ret;
|
2010-11-17 13:20:15 +08:00
|
|
|
}
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
if (!ir->attached) {
|
2010-11-17 13:20:15 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto out_locked;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
if (!LIRC_CAN_REC(ir->d.features)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out_locked;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:31:45 +08:00
|
|
|
if (length % ir->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)
|
|
|
|
*/
|
|
|
|
add_wait_queue(&ir->buf->wait_poll, &wait);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
if (lirc_buffer_empty(ir->buf)) {
|
|
|
|
/* 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-30 16:41:57 +08:00
|
|
|
mutex_unlock(&ir->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-30 16:41:57 +08:00
|
|
|
ret = mutex_lock_interruptible(&ir->mutex);
|
|
|
|
if (ret) {
|
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
|
|
|
remove_wait_queue(&ir->buf->wait_poll, &wait);
|
|
|
|
goto out_unlocked;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ir->attached) {
|
|
|
|
ret = -ENODEV;
|
2016-11-01 01:52:27 +08:00
|
|
|
goto out_locked;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lirc_buffer_read(ir->buf, buf);
|
2014-08-21 06:41:03 +08:00
|
|
|
ret = copy_to_user((void __user *)buffer+written, buf,
|
2010-07-03 12:06:57 +08:00
|
|
|
ir->buf->chunk_size);
|
2010-11-17 13:20:15 +08:00
|
|
|
if (!ret)
|
|
|
|
written += ir->buf->chunk_size;
|
|
|
|
else
|
|
|
|
ret = -EFAULT;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_wait_queue(&ir->buf->wait_poll, &wait);
|
2010-11-17 13:20:15 +08:00
|
|
|
|
|
|
|
out_locked:
|
2017-06-30 16:41:57 +08:00
|
|
|
mutex_unlock(&ir->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-06-25 20:31:40 +08:00
|
|
|
void lirc_init_pdata(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct irctl *ir = container_of(inode->i_cdev, struct irctl, cdev);
|
|
|
|
|
|
|
|
file->private_data = ir;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_init_pdata);
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
void *lirc_get_pdata(struct file *file)
|
|
|
|
{
|
2017-06-25 20:31:40 +08:00
|
|
|
struct irctl *ir = file->private_data;
|
|
|
|
|
|
|
|
return ir->d.data;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_get_pdata);
|
|
|
|
|
|
|
|
|
|
|
|
static int __init lirc_dev_init(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_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
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit lirc_dev_exit(void)
|
|
|
|
{
|
|
|
|
class_destroy(lirc_class);
|
|
|
|
unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_info("module unloaded\n");
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(lirc_dev_init);
|
|
|
|
module_exit(lirc_dev_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("LIRC base driver module");
|
|
|
|
MODULE_AUTHOR("Artur Lipowski");
|
|
|
|
MODULE_LICENSE("GPL");
|