summaryrefslogtreecommitdiffstats
path: root/drivers/media/rc/lirc_dev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/rc/lirc_dev.c')
-rw-r--r--drivers/media/rc/lirc_dev.c981
1 files changed, 642 insertions, 339 deletions
diff --git a/drivers/media/rc/lirc_dev.c b/drivers/media/rc/lirc_dev.c
index e16d1138ca48..cc863044c880 100644
--- a/drivers/media/rc/lirc_dev.c
+++ b/drivers/media/rc/lirc_dev.c
@@ -18,466 +18,775 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
-#include <linux/sched/signal.h>
-#include <linux/ioctl.h>
-#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/device.h>
-#include <linux/cdev.h>
#include <linux/idr.h>
+#include <linux/poll.h>
+#include <linux/sched.h>
+#include <linux/wait.h>
-#include <media/rc-core.h>
-#include <media/lirc.h>
-#include <media/lirc_dev.h>
+#include "rc-core-priv.h"
+#include <uapi/linux/lirc.h>
-#define LOGHEAD "lirc_dev (%s[%d]): "
+#define LIRCBUF_SIZE 256
static dev_t lirc_base_dev;
/* Used to keep track of allocated lirc devices */
-#define LIRC_MAX_DEVICES 256
static DEFINE_IDA(lirc_ida);
/* Only used for sysfs but defined to void otherwise */
static struct class *lirc_class;
-static void lirc_release_device(struct device *ld)
+/**
+ * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
+ *
+ * @dev: the struct rc_dev descriptor of the device
+ * @ev: the struct ir_raw_event descriptor of the pulse/space
+ */
+void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
{
- struct lirc_dev *d = container_of(ld, struct lirc_dev, dev);
-
- put_device(d->dev.parent);
+ unsigned long flags;
+ struct lirc_fh *fh;
+ int sample;
- if (d->buf_internal) {
- lirc_buffer_free(d->buf);
- kfree(d->buf);
- d->buf = NULL;
- }
- kfree(d);
- module_put(THIS_MODULE);
-}
+ /* Packet start */
+ if (ev.reset) {
+ /*
+ * Userspace expects a long space event before the start of
+ * the signal to use as a sync. This may be done with repeat
+ * packets and normal samples. But if a reset has been sent
+ * then we assume that a long time has passed, so we send a
+ * space with the maximum time value.
+ */
+ sample = LIRC_SPACE(LIRC_VALUE_MASK);
+ IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
-static int lirc_allocate_buffer(struct lirc_dev *d)
-{
- int err;
+ /* Carrier reports */
+ } else if (ev.carrier_report) {
+ sample = LIRC_FREQUENCY(ev.carrier);
+ IR_dprintk(2, "carrier report (freq: %d)\n", sample);
- if (d->buf) {
- d->buf_internal = false;
- return 0;
- }
+ /* Packet end */
+ } else if (ev.timeout) {
+ if (dev->gap)
+ return;
- d->buf = kmalloc(sizeof(*d->buf), GFP_KERNEL);
- if (!d->buf)
- return -ENOMEM;
+ dev->gap_start = ktime_get();
+ dev->gap = true;
+ dev->gap_duration = ev.duration;
- err = lirc_buffer_init(d->buf, d->chunk_size, d->buffer_size);
- if (err) {
- kfree(d->buf);
- d->buf = NULL;
- return err;
- }
+ sample = LIRC_TIMEOUT(ev.duration / 1000);
+ IR_dprintk(2, "timeout report (duration: %d)\n", sample);
- d->buf_internal = true;
- return 0;
-}
+ /* Normal sample */
+ } else {
+ if (dev->gap) {
+ dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
+ dev->gap_start));
+
+ /* Convert to ms and cap by LIRC_VALUE_MASK */
+ do_div(dev->gap_duration, 1000);
+ dev->gap_duration = min_t(u64, dev->gap_duration,
+ LIRC_VALUE_MASK);
+
+ spin_lock_irqsave(&dev->lirc_fh_lock, flags);
+ list_for_each_entry(fh, &dev->lirc_fh, list)
+ kfifo_put(&fh->rawir,
+ LIRC_SPACE(dev->gap_duration));
+ spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
+ dev->gap = false;
+ }
-struct lirc_dev *
-lirc_allocate_device(void)
-{
- 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);
+ sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
+ LIRC_SPACE(ev.duration / 1000);
+ IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
+ TO_US(ev.duration), TO_STR(ev.pulse));
}
- return d;
-}
-EXPORT_SYMBOL(lirc_allocate_device);
-
-void lirc_free_device(struct lirc_dev *d)
-{
- if (!d)
- return;
-
- put_device(&d->dev);
+ spin_lock_irqsave(&dev->lirc_fh_lock, flags);
+ list_for_each_entry(fh, &dev->lirc_fh, list) {
+ if (LIRC_IS_TIMEOUT(sample) && !fh->send_timeout_reports)
+ continue;
+ if (kfifo_put(&fh->rawir, sample))
+ wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
+ }
+ spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
}
-EXPORT_SYMBOL(lirc_free_device);
-int lirc_register_device(struct lirc_dev *d)
+/**
+ * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
+ * userspace. This can be called in atomic context.
+ * @dev: the struct rc_dev descriptor of the device
+ * @lsc: the struct lirc_scancode describing the decoded scancode
+ */
+void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
{
- int minor;
- int err;
+ unsigned long flags;
+ struct lirc_fh *fh;
- if (!d) {
- pr_err("driver pointer must be not NULL!\n");
- return -EBADRQC;
- }
+ lsc->timestamp = ktime_get_ns();
- if (!d->dev.parent) {
- pr_err("dev parent pointer not filled in!\n");
- return -EINVAL;
+ spin_lock_irqsave(&dev->lirc_fh_lock, flags);
+ list_for_each_entry(fh, &dev->lirc_fh, list) {
+ if (kfifo_put(&fh->scancodes, *lsc))
+ wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
}
+ spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
+}
+EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
- if (!d->fops) {
- pr_err("fops pointer not filled in!\n");
- return -EINVAL;
- }
+static int ir_lirc_open(struct inode *inode, struct file *file)
+{
+ struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
+ lirc_cdev);
+ struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
+ unsigned long flags;
+ int retval;
- if (!d->buf && d->chunk_size < 1) {
- pr_err("chunk_size must be set!\n");
- return -EINVAL;
- }
+ if (!fh)
+ return -ENOMEM;
- if (!d->buf && d->buffer_size < 1) {
- pr_err("buffer_size must be set!\n");
- return -EINVAL;
- }
+ get_device(&dev->dev);
- if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
- dev_err(&d->dev, "code length must be less than %d bits\n",
- BUFLEN * 8);
- return -EBADRQC;
+ if (!dev->registered) {
+ retval = -ENODEV;
+ goto out_fh;
}
- 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;
+ if (dev->driver_type == RC_DRIVER_IR_RAW) {
+ if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
+ retval = -ENOMEM;
+ goto out_fh;
+ }
}
- /* some safety check 8-) */
- d->name[sizeof(d->name) - 1] = '\0';
-
- if (d->features == 0)
- d->features = LIRC_CAN_REC_LIRCCODE;
-
- if (LIRC_CAN_REC(d->features)) {
- err = lirc_allocate_buffer(d);
- if (err)
- return err;
+ if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
+ if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
+ retval = -ENOMEM;
+ goto out_rawir;
+ }
}
- minor = ida_simple_get(&lirc_ida, 0, LIRC_MAX_DEVICES, GFP_KERNEL);
- if (minor < 0)
- return minor;
+ fh->send_mode = LIRC_MODE_PULSE;
+ fh->rc = dev;
+ fh->send_timeout_reports = true;
- d->minor = minor;
- d->dev.devt = MKDEV(MAJOR(lirc_base_dev), d->minor);
- dev_set_name(&d->dev, "lirc%d", d->minor);
+ if (dev->driver_type == RC_DRIVER_SCANCODE)
+ fh->rec_mode = LIRC_MODE_SCANCODE;
+ else
+ fh->rec_mode = LIRC_MODE_MODE2;
- cdev_init(&d->cdev, d->fops);
- d->cdev.owner = d->owner;
- d->attached = true;
+ retval = rc_open(dev);
+ if (retval)
+ goto out_kfifo;
- err = cdev_device_add(&d->cdev, &d->dev);
- if (err) {
- ida_simple_remove(&lirc_ida, minor);
- return err;
- }
+ init_waitqueue_head(&fh->wait_poll);
- get_device(d->dev.parent);
+ file->private_data = fh;
+ spin_lock_irqsave(&dev->lirc_fh_lock, flags);
+ list_add(&fh->list, &dev->lirc_fh);
+ spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
- dev_info(&d->dev, "lirc_dev: driver %s registered at minor = %d\n",
- d->name, d->minor);
+ nonseekable_open(inode, file);
return 0;
+out_kfifo:
+ if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
+ kfifo_free(&fh->scancodes);
+out_rawir:
+ if (dev->driver_type == RC_DRIVER_IR_RAW)
+ kfifo_free(&fh->rawir);
+out_fh:
+ kfree(fh);
+ put_device(&dev->dev);
+
+ return retval;
}
-EXPORT_SYMBOL(lirc_register_device);
-void lirc_unregister_device(struct lirc_dev *d)
+static int ir_lirc_close(struct inode *inode, struct file *file)
{
- if (!d)
- return;
-
- dev_dbg(&d->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
- d->name, d->minor);
+ struct lirc_fh *fh = file->private_data;
+ struct rc_dev *dev = fh->rc;
+ unsigned long flags;
- mutex_lock(&d->mutex);
+ spin_lock_irqsave(&dev->lirc_fh_lock, flags);
+ list_del(&fh->list);
+ spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
- d->attached = false;
- if (d->open) {
- dev_dbg(&d->dev, LOGHEAD "releasing opened driver\n",
- d->name, d->minor);
- wake_up_interruptible(&d->buf->wait_poll);
- }
+ if (dev->driver_type == RC_DRIVER_IR_RAW)
+ kfifo_free(&fh->rawir);
+ if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
+ kfifo_free(&fh->scancodes);
+ kfree(fh);
- mutex_unlock(&d->mutex);
+ rc_close(dev);
+ put_device(&dev->dev);
- cdev_device_del(&d->cdev, &d->dev);
- ida_simple_remove(&lirc_ida, d->minor);
- put_device(&d->dev);
+ return 0;
}
-EXPORT_SYMBOL(lirc_unregister_device);
-int lirc_dev_fop_open(struct inode *inode, struct file *file)
+static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
+ size_t n, loff_t *ppos)
{
- struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev);
- int retval;
+ struct lirc_fh *fh = file->private_data;
+ struct rc_dev *dev = fh->rc;
+ unsigned int *txbuf;
+ struct ir_raw_event *raw = NULL;
+ ssize_t ret;
+ size_t count;
+ ktime_t start;
+ s64 towait;
+ unsigned int duration = 0; /* signal duration in us */
+ int i;
+
+ ret = mutex_lock_interruptible(&dev->lock);
+ if (ret)
+ return ret;
- dev_dbg(&d->dev, LOGHEAD "open called\n", d->name, d->minor);
+ if (!dev->registered) {
+ ret = -ENODEV;
+ goto out_unlock;
+ }
- retval = mutex_lock_interruptible(&d->mutex);
- if (retval)
- return retval;
+ start = ktime_get();
- if (!d->attached) {
- retval = -ENODEV;
- goto out;
+ if (!dev->tx_ir) {
+ ret = -EINVAL;
+ goto out_unlock;
}
- if (d->open) {
- retval = -EBUSY;
- goto out;
- }
+ if (fh->send_mode == LIRC_MODE_SCANCODE) {
+ struct lirc_scancode scan;
- if (d->rdev) {
- retval = rc_open(d->rdev);
- if (retval)
- goto out;
- }
+ if (n != sizeof(scan)) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
- if (d->buf)
- lirc_buffer_clear(d->buf);
+ if (copy_from_user(&scan, buf, sizeof(scan))) {
+ ret = -EFAULT;
+ goto out_unlock;
+ }
- d->open++;
+ if (scan.flags || scan.keycode || scan.timestamp) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
- lirc_init_pdata(inode, file);
- nonseekable_open(inode, file);
- mutex_unlock(&d->mutex);
+ /*
+ * The scancode field in lirc_scancode is 64-bit simply
+ * to future-proof it, since there are IR protocols encode
+ * use more than 32 bits. For now only 32-bit protocols
+ * are supported.
+ */
+ if (scan.scancode > U32_MAX ||
+ !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
- return 0;
+ raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
+ if (!raw) {
+ ret = -ENOMEM;
+ goto out_unlock;
+ }
-out:
- mutex_unlock(&d->mutex);
- return retval;
-}
-EXPORT_SYMBOL(lirc_dev_fop_open);
+ ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
+ raw, LIRCBUF_SIZE);
+ if (ret < 0)
+ goto out_kfree_raw;
-int lirc_dev_fop_close(struct inode *inode, struct file *file)
-{
- struct lirc_dev *d = file->private_data;
+ count = ret;
- mutex_lock(&d->mutex);
+ txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
+ if (!txbuf) {
+ ret = -ENOMEM;
+ goto out_kfree_raw;
+ }
- rc_close(d->rdev);
- d->open--;
+ for (i = 0; i < count; i++)
+ /* Convert from NS to US */
+ txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000);
- mutex_unlock(&d->mutex);
+ if (dev->s_tx_carrier) {
+ int carrier = ir_raw_encode_carrier(scan.rc_proto);
- return 0;
-}
-EXPORT_SYMBOL(lirc_dev_fop_close);
+ if (carrier > 0)
+ dev->s_tx_carrier(dev, carrier);
+ }
+ } else {
+ if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
-unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
-{
- struct lirc_dev *d = file->private_data;
- unsigned int ret;
+ count = n / sizeof(unsigned int);
+ if (count > LIRCBUF_SIZE || count % 2 == 0) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
- if (!d->attached)
- return POLLHUP | POLLERR;
+ txbuf = memdup_user(buf, n);
+ if (IS_ERR(txbuf)) {
+ ret = PTR_ERR(txbuf);
+ goto out_unlock;
+ }
+ }
- if (d->buf) {
- poll_wait(file, &d->buf->wait_poll, wait);
+ for (i = 0; i < count; i++) {
+ if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
+ ret = -EINVAL;
+ goto out_kfree;
+ }
- if (lirc_buffer_empty(d->buf))
- ret = 0;
- else
- ret = POLLIN | POLLRDNORM;
- } else {
- ret = POLLERR;
+ duration += txbuf[i];
}
- dev_dbg(&d->dev, LOGHEAD "poll result = %d\n", d->name, d->minor, ret);
+ ret = dev->tx_ir(dev, txbuf, count);
+ if (ret < 0)
+ goto out_kfree;
+
+ kfree(txbuf);
+ kfree(raw);
+ mutex_unlock(&dev->lock);
+
+ /*
+ * The lircd gap calculation expects the write function to
+ * wait for the actual IR signal to be transmitted before
+ * returning.
+ */
+ towait = ktime_us_delta(ktime_add_us(start, duration),
+ ktime_get());
+ if (towait > 0) {
+ set_current_state(TASK_INTERRUPTIBLE);
+ schedule_timeout(usecs_to_jiffies(towait));
+ }
+ return n;
+out_kfree:
+ kfree(txbuf);
+out_kfree_raw:
+ kfree(raw);
+out_unlock:
+ mutex_unlock(&dev->lock);
return ret;
}
-EXPORT_SYMBOL(lirc_dev_fop_poll);
-long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+static long ir_lirc_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
{
- struct lirc_dev *d = file->private_data;
- __u32 mode;
- int result;
-
- dev_dbg(&d->dev, LOGHEAD "ioctl called (0x%x)\n",
- d->name, d->minor, cmd);
+ struct lirc_fh *fh = file->private_data;
+ struct rc_dev *dev = fh->rc;
+ u32 __user *argp = (u32 __user *)(arg);
+ u32 val = 0;
+ int ret;
+
+ if (_IOC_DIR(cmd) & _IOC_WRITE) {
+ ret = get_user(val, argp);
+ if (ret)
+ return ret;
+ }
- result = mutex_lock_interruptible(&d->mutex);
- if (result)
- return result;
+ ret = mutex_lock_interruptible(&dev->lock);
+ if (ret)
+ return ret;
- if (!d->attached) {
- result = -ENODEV;
+ if (!dev->registered) {
+ ret = -ENODEV;
goto out;
}
switch (cmd) {
case LIRC_GET_FEATURES:
- result = put_user(d->features, (__u32 __user *)arg);
- break;
- case LIRC_GET_REC_MODE:
- if (!LIRC_CAN_REC(d->features)) {
- result = -ENOTTY;
- break;
+ if (dev->driver_type == RC_DRIVER_SCANCODE)
+ val |= LIRC_CAN_REC_SCANCODE;
+
+ if (dev->driver_type == RC_DRIVER_IR_RAW) {
+ val |= LIRC_CAN_REC_MODE2;
+ if (dev->rx_resolution)
+ val |= LIRC_CAN_GET_REC_RESOLUTION;
+ }
+
+ if (dev->tx_ir) {
+ val |= LIRC_CAN_SEND_PULSE;
+ if (dev->s_tx_mask)
+ val |= LIRC_CAN_SET_TRANSMITTER_MASK;
+ if (dev->s_tx_carrier)
+ val |= LIRC_CAN_SET_SEND_CARRIER;
+ if (dev->s_tx_duty_cycle)
+ val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
}
- result = put_user(LIRC_REC2MODE
- (d->features & LIRC_CAN_REC_MASK),
- (__u32 __user *)arg);
+ if (dev->s_rx_carrier_range)
+ val |= LIRC_CAN_SET_REC_CARRIER |
+ LIRC_CAN_SET_REC_CARRIER_RANGE;
+
+ if (dev->s_learning_mode)
+ val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
+
+ if (dev->s_carrier_report)
+ val |= LIRC_CAN_MEASURE_CARRIER;
+
+ if (dev->max_timeout)
+ val |= LIRC_CAN_SET_REC_TIMEOUT;
+
break;
+
+ /* mode support */
+ case LIRC_GET_REC_MODE:
+ if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
+ ret = -ENOTTY;
+ else
+ val = fh->rec_mode;
+ break;
+
case LIRC_SET_REC_MODE:
- if (!LIRC_CAN_REC(d->features)) {
- result = -ENOTTY;
+ switch (dev->driver_type) {
+ case RC_DRIVER_IR_RAW_TX:
+ ret = -ENOTTY;
+ break;
+ case RC_DRIVER_SCANCODE:
+ if (val != LIRC_MODE_SCANCODE)
+ ret = -EINVAL;
+ break;
+ case RC_DRIVER_IR_RAW:
+ if (!(val == LIRC_MODE_MODE2 ||
+ val == LIRC_MODE_SCANCODE))
+ ret = -EINVAL;
break;
}
- result = get_user(mode, (__u32 __user *)arg);
- if (!result && !(LIRC_MODE2REC(mode) & d->features))
- result = -EINVAL;
- /*
- * FIXME: We should actually set the mode somehow but
- * for now, lirc_serial doesn't support mode changing either
- */
+ if (!ret)
+ fh->rec_mode = val;
+ break;
+
+ case LIRC_GET_SEND_MODE:
+ if (!dev->tx_ir)
+ ret = -ENOTTY;
+ else
+ val = fh->send_mode;
+ break;
+
+ case LIRC_SET_SEND_MODE:
+ if (!dev->tx_ir)
+ ret = -ENOTTY;
+ else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
+ ret = -EINVAL;
+ else
+ fh->send_mode = val;
+ break;
+
+ /* TX settings */
+ case LIRC_SET_TRANSMITTER_MASK:
+ if (!dev->s_tx_mask)
+ ret = -ENOTTY;
+ else
+ ret = dev->s_tx_mask(dev, val);
+ break;
+
+ case LIRC_SET_SEND_CARRIER:
+ if (!dev->s_tx_carrier)
+ ret = -ENOTTY;
+ else
+ ret = dev->s_tx_carrier(dev, val);
+ break;
+
+ case LIRC_SET_SEND_DUTY_CYCLE:
+ if (!dev->s_tx_duty_cycle)
+ ret = -ENOTTY;
+ else if (val <= 0 || val >= 100)
+ ret = -EINVAL;
+ else
+ ret = dev->s_tx_duty_cycle(dev, val);
+ break;
+
+ /* RX settings */
+ case LIRC_SET_REC_CARRIER:
+ if (!dev->s_rx_carrier_range)
+ ret = -ENOTTY;
+ else if (val <= 0)
+ ret = -EINVAL;
+ else
+ ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
+ val);
+ break;
+
+ case LIRC_SET_REC_CARRIER_RANGE:
+ if (!dev->s_rx_carrier_range)
+ ret = -ENOTTY;
+ else if (val <= 0)
+ ret = -EINVAL;
+ else
+ fh->carrier_low = val;
+ break;
+
+ case LIRC_GET_REC_RESOLUTION:
+ if (!dev->rx_resolution)
+ ret = -ENOTTY;
+ else
+ val = dev->rx_resolution / 1000;
break;
- case LIRC_GET_LENGTH:
- result = put_user(d->code_length, (__u32 __user *)arg);
+
+ case LIRC_SET_WIDEBAND_RECEIVER:
+ if (!dev->s_learning_mode)
+ ret = -ENOTTY;
+ else
+ ret = dev->s_learning_mode(dev, !!val);
break;
+
+ case LIRC_SET_MEASURE_CARRIER_MODE:
+ if (!dev->s_carrier_report)
+ ret = -ENOTTY;
+ else
+ ret = dev->s_carrier_report(dev, !!val);
+ break;
+
+ /* Generic timeout support */
+ case LIRC_GET_MIN_TIMEOUT:
+ if (!dev->max_timeout)
+ ret = -ENOTTY;
+ else
+ val = DIV_ROUND_UP(dev->min_timeout, 1000);
+ break;
+
+ case LIRC_GET_MAX_TIMEOUT:
+ if (!dev->max_timeout)
+ ret = -ENOTTY;
+ else
+ val = dev->max_timeout / 1000;
+ break;
+
+ case LIRC_SET_REC_TIMEOUT:
+ if (!dev->max_timeout) {
+ ret = -ENOTTY;
+ } else if (val > U32_MAX / 1000) {
+ /* Check for multiply overflow */
+ ret = -EINVAL;
+ } else {
+ u32 tmp = val * 1000;
+
+ if (tmp < dev->min_timeout || tmp > dev->max_timeout)
+ ret = -EINVAL;
+ else if (dev->s_timeout)
+ ret = dev->s_timeout(dev, tmp);
+ else if (!ret)
+ dev->timeout = tmp;
+ }
+ break;
+
+ case LIRC_SET_REC_TIMEOUT_REPORTS:
+ if (!dev->timeout)
+ ret = -ENOTTY;
+ else
+ fh->send_timeout_reports = !!val;
+ break;
+
default:
- result = -ENOTTY;
+ ret = -ENOTTY;
}
+ if (!ret && _IOC_DIR(cmd) & _IOC_READ)
+ ret = put_user(val, argp);
+
out:
- mutex_unlock(&d->mutex);
- return result;
+ mutex_unlock(&dev->lock);
+ return ret;
}
-EXPORT_SYMBOL(lirc_dev_fop_ioctl);
-ssize_t lirc_dev_fop_read(struct file *file,
- char __user *buffer,
- size_t length,
- loff_t *ppos)
+static __poll_t ir_lirc_poll(struct file *file, struct poll_table_struct *wait)
{
- struct lirc_dev *d = file->private_data;
- unsigned char *buf;
- int ret, written = 0;
- DECLARE_WAITQUEUE(wait, current);
+ struct lirc_fh *fh = file->private_data;
+ struct rc_dev *rcdev = fh->rc;
+ __poll_t events = 0;
+
+ poll_wait(file, &fh->wait_poll, wait);
+
+ if (!rcdev->registered) {
+ events = EPOLLHUP | EPOLLERR;
+ } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
+ if (fh->rec_mode == LIRC_MODE_SCANCODE &&
+ !kfifo_is_empty(&fh->scancodes))
+ events = EPOLLIN | EPOLLRDNORM;
+
+ if (fh->rec_mode == LIRC_MODE_MODE2 &&
+ !kfifo_is_empty(&fh->rawir))
+ events = EPOLLIN | EPOLLRDNORM;
+ }
- buf = kzalloc(d->buf->chunk_size, GFP_KERNEL);
- if (!buf)
- return -ENOMEM;
+ return events;
+}
- dev_dbg(&d->dev, LOGHEAD "read called\n", d->name, d->minor);
+static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer,
+ size_t length)
+{
+ struct lirc_fh *fh = file->private_data;
+ struct rc_dev *rcdev = fh->rc;
+ unsigned int copied;
+ int ret;
- ret = mutex_lock_interruptible(&d->mutex);
- if (ret) {
- kfree(buf);
- return ret;
- }
+ if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
+ return -EINVAL;
- if (!d->attached) {
- ret = -ENODEV;
- goto out_locked;
- }
+ do {
+ if (kfifo_is_empty(&fh->rawir)) {
+ if (file->f_flags & O_NONBLOCK)
+ return -EAGAIN;
- if (!LIRC_CAN_REC(d->features)) {
- ret = -EINVAL;
- goto out_locked;
- }
+ ret = wait_event_interruptible(fh->wait_poll,
+ !kfifo_is_empty(&fh->rawir) ||
+ !rcdev->registered);
+ if (ret)
+ return ret;
+ }
- if (length % d->buf->chunk_size) {
- ret = -EINVAL;
- goto out_locked;
- }
+ if (!rcdev->registered)
+ return -ENODEV;
- /*
- * 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);
+ ret = mutex_lock_interruptible(&rcdev->lock);
+ if (ret)
+ return ret;
+ ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
+ mutex_unlock(&rcdev->lock);
+ if (ret)
+ return ret;
+ } while (copied == 0);
- /*
- * 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;
+ return copied;
+}
+
+static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer,
+ size_t length)
+{
+ struct lirc_fh *fh = file->private_data;
+ struct rc_dev *rcdev = fh->rc;
+ unsigned int copied;
+ int ret;
+
+ if (length < sizeof(struct lirc_scancode) ||
+ length % sizeof(struct lirc_scancode))
+ return -EINVAL;
+
+ do {
+ if (kfifo_is_empty(&fh->scancodes)) {
+ if (file->f_flags & O_NONBLOCK)
+ return -EAGAIN;
+
+ ret = wait_event_interruptible(fh->wait_poll,
+ !kfifo_is_empty(&fh->scancodes) ||
+ !rcdev->registered);
+ if (ret)
+ return ret;
}
- }
- remove_wait_queue(&d->buf->wait_poll, &wait);
+ if (!rcdev->registered)
+ return -ENODEV;
+
+ ret = mutex_lock_interruptible(&rcdev->lock);
+ if (ret)
+ return ret;
+ ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
+ mutex_unlock(&rcdev->lock);
+ if (ret)
+ return ret;
+ } while (copied == 0);
+
+ return copied;
+}
-out_locked:
- mutex_unlock(&d->mutex);
+static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
+ size_t length, loff_t *ppos)
+{
+ struct lirc_fh *fh = file->private_data;
+ struct rc_dev *rcdev = fh->rc;
+
+ if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
+ return -EINVAL;
-out_unlocked:
- kfree(buf);
+ if (!rcdev->registered)
+ return -ENODEV;
- return ret ? ret : written;
+ if (fh->rec_mode == LIRC_MODE_MODE2)
+ return ir_lirc_read_mode2(file, buffer, length);
+ else /* LIRC_MODE_SCANCODE */
+ return ir_lirc_read_scancode(file, buffer, length);
}
-EXPORT_SYMBOL(lirc_dev_fop_read);
-void lirc_init_pdata(struct inode *inode, struct file *file)
+static const struct file_operations lirc_fops = {
+ .owner = THIS_MODULE,
+ .write = ir_lirc_transmit_ir,
+ .unlocked_ioctl = ir_lirc_ioctl,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = ir_lirc_ioctl,
+#endif
+ .read = ir_lirc_read,
+ .poll = ir_lirc_poll,
+ .open = ir_lirc_open,
+ .release = ir_lirc_close,
+ .llseek = no_llseek,
+};
+
+static void lirc_release_device(struct device *ld)
{
- struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev);
+ struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
- file->private_data = d;
+ put_device(&rcdev->dev);
}
-EXPORT_SYMBOL(lirc_init_pdata);
-void *lirc_get_pdata(struct file *file)
+int ir_lirc_register(struct rc_dev *dev)
{
- struct lirc_dev *d = file->private_data;
+ int err, minor;
+
+ minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
+ if (minor < 0)
+ return minor;
+
+ device_initialize(&dev->lirc_dev);
+ dev->lirc_dev.class = lirc_class;
+ dev->lirc_dev.parent = &dev->dev;
+ dev->lirc_dev.release = lirc_release_device;
+ dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
+ dev_set_name(&dev->lirc_dev, "lirc%d", minor);
- return d->data;
+ INIT_LIST_HEAD(&dev->lirc_fh);
+ spin_lock_init(&dev->lirc_fh_lock);
+
+ cdev_init(&dev->lirc_cdev, &lirc_fops);
+
+ err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
+ if (err)
+ goto out_ida;
+
+ get_device(&dev->dev);
+
+ dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d",
+ dev->driver_name, minor);
+
+ return 0;
+
+out_ida:
+ ida_simple_remove(&lirc_ida, minor);
+ return err;
}
-EXPORT_SYMBOL(lirc_get_pdata);
+void ir_lirc_unregister(struct rc_dev *dev)
+{
+ unsigned long flags;
+ struct lirc_fh *fh;
+
+ dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
+ dev->driver_name, MINOR(dev->lirc_dev.devt));
+
+ spin_lock_irqsave(&dev->lirc_fh_lock, flags);
+ list_for_each_entry(fh, &dev->lirc_fh, list)
+ wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
+ spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
-static int __init lirc_dev_init(void)
+ cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
+ ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
+}
+
+int __init lirc_dev_init(void)
{
int retval;
@@ -487,7 +796,7 @@ static int __init lirc_dev_init(void)
return PTR_ERR(lirc_class);
}
- retval = alloc_chrdev_region(&lirc_base_dev, 0, LIRC_MAX_DEVICES,
+ retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX,
"BaseRemoteCtl");
if (retval) {
class_destroy(lirc_class);
@@ -501,16 +810,10 @@ static int __init lirc_dev_init(void)
return 0;
}
-static void __exit lirc_dev_exit(void)
+void __exit lirc_dev_exit(void)
{
class_destroy(lirc_class);
- unregister_chrdev_region(lirc_base_dev, LIRC_MAX_DEVICES);
- pr_info("module unloaded\n");
+ unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
}
-module_init(lirc_dev_init);
-module_exit(lirc_dev_exit);
-
-MODULE_DESCRIPTION("LIRC base driver module");
-MODULE_AUTHOR("Artur Lipowski");
-MODULE_LICENSE("GPL");
+MODULE_ALIAS("lirc_dev");