summaryrefslogtreecommitdiff
path: root/drivers/media
diff options
context:
space:
mode:
authorSean Young <sean@mess.org>2017-09-23 21:44:18 +0300
committerMauro Carvalho Chehab <mchehab@s-opensource.com>2017-12-14 18:35:18 +0300
commit71695aff9fe036857596965635e2607cf561a230 (patch)
treed92dd7d09aedee87d6ad93f47b6f56dd29364514 /drivers/media
parent95bc71e199e50487054adfd8222c5105deddbbd9 (diff)
downloadlinux-71695aff9fe036857596965635e2607cf561a230.tar.xz
media: lirc: use kfifo rather than lirc_buffer for raw IR
Since the only mode lirc devices can handle is raw IR, handle this in a plain kfifo. Remove lirc_buffer since this is no longer needed. Signed-off-by: Sean Young <sean@mess.org> Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
Diffstat (limited to 'drivers/media')
-rw-r--r--drivers/media/rc/ir-lirc-codec.c81
-rw-r--r--drivers/media/rc/lirc_dev.c199
2 files changed, 83 insertions, 197 deletions
diff --git a/drivers/media/rc/ir-lirc-codec.c b/drivers/media/rc/ir-lirc-codec.c
index f933e7617882..2fa1f905a266 100644
--- a/drivers/media/rc/ir-lirc-codec.c
+++ b/drivers/media/rc/ir-lirc-codec.c
@@ -66,8 +66,6 @@ void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
} else {
if (dev->gap) {
- int gap_sample;
-
dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
dev->gap_start));
@@ -76,9 +74,7 @@ void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
dev->gap_duration = min_t(u64, dev->gap_duration,
LIRC_VALUE_MASK);
- gap_sample = LIRC_SPACE(dev->gap_duration);
- lirc_buffer_write(dev->lirc_dev->buf,
- (unsigned char *)&gap_sample);
+ kfifo_put(&dev->rawir, LIRC_SPACE(dev->gap_duration));
dev->gap = false;
}
@@ -88,10 +84,8 @@ void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
TO_US(ev.duration), TO_STR(ev.pulse));
}
- lirc_buffer_write(dev->lirc_dev->buf,
- (unsigned char *) &sample);
-
- wake_up(&dev->lirc_dev->buf->wait_poll);
+ kfifo_put(&dev->rawir, sample);
+ wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
}
static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
@@ -408,6 +402,68 @@ static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
return ret;
}
+static unsigned int ir_lirc_poll(struct file *file,
+ struct poll_table_struct *wait)
+{
+ struct rc_dev *rcdev = file->private_data;
+ struct lirc_dev *d = rcdev->lirc_dev;
+ unsigned int events = 0;
+
+ poll_wait(file, &rcdev->wait_poll, wait);
+
+ if (!d->attached)
+ events = POLLHUP | POLLERR;
+ else if (rcdev->driver_type == RC_DRIVER_IR_RAW &&
+ !kfifo_is_empty(&rcdev->rawir))
+ events = POLLIN | POLLRDNORM;
+
+ return events;
+}
+
+static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
+ size_t length, loff_t *ppos)
+{
+ struct rc_dev *rcdev = file->private_data;
+ struct lirc_dev *d = rcdev->lirc_dev;
+ unsigned int copied;
+ int ret;
+
+ if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
+ return -EINVAL;
+
+ if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
+ return -EINVAL;
+
+ if (!d->attached)
+ return -ENODEV;
+
+ do {
+ if (kfifo_is_empty(&rcdev->rawir)) {
+ if (file->f_flags & O_NONBLOCK)
+ return -EAGAIN;
+
+ ret = wait_event_interruptible(rcdev->wait_poll,
+ !kfifo_is_empty(&rcdev->rawir) ||
+ !d->attached);
+ if (ret)
+ return ret;
+ }
+
+ if (!d->attached)
+ return -ENODEV;
+
+ ret = mutex_lock_interruptible(&rcdev->lock);
+ if (ret)
+ return ret;
+ ret = kfifo_to_user(&rcdev->rawir, buffer, length, &copied);
+ mutex_unlock(&rcdev->lock);
+ if (ret)
+ return ret;
+ } while (copied == 0);
+
+ return copied;
+}
+
static const struct file_operations lirc_fops = {
.owner = THIS_MODULE,
.write = ir_lirc_transmit_ir,
@@ -415,8 +471,8 @@ static const struct file_operations lirc_fops = {
#ifdef CONFIG_COMPAT
.compat_ioctl = ir_lirc_ioctl,
#endif
- .read = lirc_dev_fop_read,
- .poll = lirc_dev_fop_poll,
+ .read = ir_lirc_read,
+ .poll = ir_lirc_poll,
.open = lirc_dev_fop_open,
.release = lirc_dev_fop_close,
.llseek = no_llseek,
@@ -433,9 +489,6 @@ int ir_lirc_register(struct rc_dev *dev)
snprintf(ldev->name, sizeof(ldev->name), "ir-lirc-codec (%s)",
dev->driver_name);
- ldev->buf = NULL;
- ldev->chunk_size = sizeof(int);
- ldev->buffer_size = LIRCBUF_SIZE;
ldev->fops = &lirc_fops;
ldev->dev.parent = &dev->dev;
ldev->rdev = dev;
diff --git a/drivers/media/rc/lirc_dev.c b/drivers/media/rc/lirc_dev.c
index 95058ea01e62..9a0ad8d9a0cb 100644
--- a/drivers/media/rc/lirc_dev.c
+++ b/drivers/media/rc/lirc_dev.c
@@ -44,40 +44,14 @@ static struct class *lirc_class;
static void lirc_release_device(struct device *ld)
{
struct lirc_dev *d = container_of(ld, struct lirc_dev, dev);
+ struct rc_dev *rcdev = d->rdev;
- put_device(d->dev.parent);
+ if (rcdev->driver_type == RC_DRIVER_IR_RAW)
+ kfifo_free(&rcdev->rawir);
- if (d->buf_internal) {
- lirc_buffer_free(d->buf);
- kfree(d->buf);
- d->buf = NULL;
- }
kfree(d);
module_put(THIS_MODULE);
-}
-
-static int lirc_allocate_buffer(struct lirc_dev *d)
-{
- int err;
-
- if (d->buf) {
- d->buf_internal = false;
- return 0;
- }
-
- d->buf = kmalloc(sizeof(*d->buf), GFP_KERNEL);
- if (!d->buf)
- return -ENOMEM;
-
- err = lirc_buffer_init(d->buf, d->chunk_size, d->buffer_size);
- if (err) {
- kfree(d->buf);
- d->buf = NULL;
- return err;
- }
-
- d->buf_internal = true;
- return 0;
+ put_device(d->dev.parent);
}
struct lirc_dev *
@@ -128,31 +102,16 @@ int lirc_register_device(struct lirc_dev *d)
return -EINVAL;
}
- if (!d->buf && d->chunk_size < 1) {
- pr_err("chunk_size must be set!\n");
- return -EINVAL;
- }
-
- if (!d->buf && d->buffer_size < 1) {
- pr_err("buffer_size must be set!\n");
- return -EINVAL;
- }
-
- if (!d->buf && !(d->fops && d->fops->read &&
- d->fops->poll && d->fops->unlocked_ioctl)) {
- dev_err(&d->dev, "undefined read, poll, ioctl\n");
- return -EBADRQC;
- }
-
/* some safety check 8-) */
d->name[sizeof(d->name) - 1] = '\0';
if (rcdev->driver_type == RC_DRIVER_IR_RAW) {
- err = lirc_allocate_buffer(d);
- if (err)
- return err;
+ if (kfifo_alloc(&rcdev->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL))
+ return -ENOMEM;
}
+ init_waitqueue_head(&rcdev->wait_poll);
+
minor = ida_simple_get(&lirc_ida, 0, LIRC_MAX_DEVICES, GFP_KERNEL);
if (minor < 0)
return minor;
@@ -182,9 +141,13 @@ EXPORT_SYMBOL(lirc_register_device);
void lirc_unregister_device(struct lirc_dev *d)
{
+ struct rc_dev *rcdev;
+
if (!d)
return;
+ rcdev = d->rdev;
+
dev_dbg(&d->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
d->name, d->minor);
@@ -194,7 +157,7 @@ void lirc_unregister_device(struct lirc_dev *d)
if (d->open) {
dev_dbg(&d->dev, LOGHEAD "releasing opened driver\n",
d->name, d->minor);
- wake_up_interruptible(&d->buf->wait_poll);
+ wake_up_poll(&rcdev->wait_poll, POLLHUP);
}
mutex_unlock(&d->mutex);
@@ -208,6 +171,7 @@ EXPORT_SYMBOL(lirc_unregister_device);
int lirc_dev_fop_open(struct inode *inode, struct file *file)
{
struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev);
+ struct rc_dev *rcdev = d->rdev;
int retval;
dev_dbg(&d->dev, LOGHEAD "open called\n", d->name, d->minor);
@@ -232,8 +196,8 @@ int lirc_dev_fop_open(struct inode *inode, struct file *file)
goto out;
}
- if (d->buf)
- lirc_buffer_clear(d->buf);
+ if (rcdev->driver_type == RC_DRIVER_IR_RAW)
+ kfifo_reset_out(&rcdev->rawir);
d->open++;
@@ -265,137 +229,6 @@ int lirc_dev_fop_close(struct inode *inode, struct file *file)
}
EXPORT_SYMBOL(lirc_dev_fop_close);
-unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
-{
- struct rc_dev *rcdev = file->private_data;
- struct lirc_dev *d = rcdev->lirc_dev;
- unsigned int ret;
-
- if (!d->attached)
- return POLLHUP | POLLERR;
-
- if (d->buf) {
- poll_wait(file, &d->buf->wait_poll, wait);
-
- if (lirc_buffer_empty(d->buf))
- ret = 0;
- else
- ret = POLLIN | POLLRDNORM;
- } else {
- ret = POLLERR;
- }
-
- dev_dbg(&d->dev, LOGHEAD "poll result = %d\n", d->name, d->minor, ret);
-
- return ret;
-}
-EXPORT_SYMBOL(lirc_dev_fop_poll);
-
-ssize_t lirc_dev_fop_read(struct file *file,
- char __user *buffer,
- size_t length,
- loff_t *ppos)
-{
- struct rc_dev *rcdev = file->private_data;
- struct lirc_dev *d = rcdev->lirc_dev;
- unsigned char *buf;
- int ret, written = 0;
- DECLARE_WAITQUEUE(wait, current);
-
- buf = kzalloc(d->buf->chunk_size, GFP_KERNEL);
- if (!buf)
- return -ENOMEM;
-
- dev_dbg(&d->dev, LOGHEAD "read called\n", d->name, d->minor);
-
- ret = mutex_lock_interruptible(&d->mutex);
- if (ret) {
- kfree(buf);
- return ret;
- }
-
- if (!d->attached) {
- ret = -ENODEV;
- goto out_locked;
- }
-
- if (rcdev->driver_type != RC_DRIVER_IR_RAW) {
- ret = -EINVAL;
- goto out_locked;
- }
-
- if (length % d->buf->chunk_size) {
- ret = -EINVAL;
- goto out_locked;
- }
-
- /*
- * 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(&d->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(d->buf)) {
- /* According to the read(2) man page, 'written' can be
- * returned as less than 'length', instead of blocking
- * again, returning -EWOULDBLOCK, or returning
- * -ERESTARTSYS
- */
- if (written)
- break;
- if (file->f_flags & O_NONBLOCK) {
- ret = -EWOULDBLOCK;
- break;
- }
- if (signal_pending(current)) {
- ret = -ERESTARTSYS;
- break;
- }
-
- mutex_unlock(&d->mutex);
- set_current_state(TASK_INTERRUPTIBLE);
- schedule();
- set_current_state(TASK_RUNNING);
-
- ret = mutex_lock_interruptible(&d->mutex);
- if (ret) {
- remove_wait_queue(&d->buf->wait_poll, &wait);
- goto out_unlocked;
- }
-
- if (!d->attached) {
- ret = -ENODEV;
- goto out_locked;
- }
- } else {
- lirc_buffer_read(d->buf, buf);
- ret = copy_to_user((void __user *)buffer+written, buf,
- d->buf->chunk_size);
- if (!ret)
- written += d->buf->chunk_size;
- else
- ret = -EFAULT;
- }
- }
-
- remove_wait_queue(&d->buf->wait_poll, &wait);
-
-out_locked:
- mutex_unlock(&d->mutex);
-
-out_unlocked:
- kfree(buf);
-
- return ret ? ret : written;
-}
-EXPORT_SYMBOL(lirc_dev_fop_read);
-
int __init lirc_dev_init(void)
{
int retval;