From ca4146985db7cbb97816e9b961b8db79e63d9e86 Mon Sep 17 00:00:00 2001 From: Jarod Wilson Date: Sat, 3 Jul 2010 01:07:53 -0300 Subject: V4L/DVB: IR: add ir-core to lirc userspace decoder bridge driver v2: copy of buffer data from userspace done inside this plugin/driver, keeping the actual drivers minimal, and more flexible in what we can deliver to them later on (they may be fed from within kernelspace later on, by an in-kernel IR encoder). Signed-off-by: Jarod Wilson Signed-off-by: Mauro Carvalho Chehab --- drivers/media/IR/ir-lirc-codec.c | 284 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 drivers/media/IR/ir-lirc-codec.c (limited to 'drivers/media/IR/ir-lirc-codec.c') diff --git a/drivers/media/IR/ir-lirc-codec.c b/drivers/media/IR/ir-lirc-codec.c new file mode 100644 index 000000000000..aff31d1b13d5 --- /dev/null +++ b/drivers/media/IR/ir-lirc-codec.c @@ -0,0 +1,284 @@ +/* ir-lirc-codec.c - ir-core to classic lirc interface bridge + * + * Copyright (C) 2010 by Jarod Wilson + * + * 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 version 2 of the License. + * + * 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. + */ + +#include +#include +#include +#include +#include "ir-core-priv.h" +#include "lirc_dev.h" + +#define LIRCBUF_SIZE 256 + +/** + * ir_lirc_decode() - Send raw IR data to lirc_dev to be relayed to the + * lircd userspace daemon for decoding. + * @input_dev: the struct input_dev descriptor of the device + * @duration: the struct ir_raw_event descriptor of the pulse/space + * + * This function returns -EINVAL if the lirc interfaces aren't wired up. + */ +static int ir_lirc_decode(struct input_dev *input_dev, struct ir_raw_event ev) +{ + struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); + + if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC)) + return 0; + + if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf) + return -EINVAL; + + IR_dprintk(2, "LIRC data transfer started (%uus %s)\n", + TO_US(ev.duration), TO_STR(ev.pulse)); + + ir_dev->raw->lirc.lircdata += ev.duration / 1000; + if (ev.pulse) + ir_dev->raw->lirc.lircdata |= PULSE_BIT; + + lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf, + (unsigned char *) &ir_dev->raw->lirc.lircdata); + wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll); + + ir_dev->raw->lirc.lircdata = 0; + + return 0; +} + +static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf, + size_t n, loff_t *ppos) +{ + struct lirc_codec *lirc; + struct ir_input_dev *ir_dev; + int *txbuf; /* buffer with values to transmit */ + int ret = 0, count; + + lirc = lirc_get_pdata(file); + if (!lirc) + return -EFAULT; + + if (n % sizeof(int)) + return -EINVAL; + + count = n / sizeof(int); + if (count > LIRCBUF_SIZE || count % 2 == 0) + return -EINVAL; + + txbuf = kzalloc(sizeof(int) * LIRCBUF_SIZE, GFP_KERNEL); + if (!txbuf) + return -ENOMEM; + + if (copy_from_user(txbuf, buf, n)) { + ret = -EFAULT; + goto out; + } + + ir_dev = lirc->ir_dev; + if (!ir_dev) { + ret = -EFAULT; + goto out; + } + + if (ir_dev->props && ir_dev->props->tx_ir) + ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n); + +out: + kfree(txbuf); + return ret; +} + +static int ir_lirc_ioctl(struct inode *node, struct file *filep, + unsigned int cmd, unsigned long arg) +{ + struct lirc_codec *lirc; + struct ir_input_dev *ir_dev; + int ret = 0; + void *drv_data; + unsigned long val; + + lirc = lirc_get_pdata(filep); + if (!lirc) + return -EFAULT; + + ir_dev = lirc->ir_dev; + if (!ir_dev || !ir_dev->props || !ir_dev->props->priv) + return -EFAULT; + + drv_data = ir_dev->props->priv; + + switch (cmd) { + case LIRC_SET_TRANSMITTER_MASK: + ret = get_user(val, (unsigned long *)arg); + if (ret) + return ret; + + if (ir_dev->props && ir_dev->props->s_tx_mask) + ret = ir_dev->props->s_tx_mask(drv_data, (u32)val); + else + return -EINVAL; + break; + + case LIRC_SET_SEND_CARRIER: + ret = get_user(val, (unsigned long *)arg); + if (ret) + return ret; + + if (ir_dev->props && ir_dev->props->s_tx_carrier) + ir_dev->props->s_tx_carrier(drv_data, (u32)val); + else + return -EINVAL; + break; + + case LIRC_GET_SEND_MODE: + val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK; + ret = put_user(val, (unsigned long *)arg); + break; + + case LIRC_SET_SEND_MODE: + ret = get_user(val, (unsigned long *)arg); + if (ret) + return ret; + + if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK)) + return -EINVAL; + break; + + default: + return lirc_dev_fop_ioctl(node, filep, cmd, arg); + } + + return ret; +} + +static int ir_lirc_open(void *data) +{ + return 0; +} + +static void ir_lirc_close(void *data) +{ + return; +} + +static struct file_operations lirc_fops = { + .owner = THIS_MODULE, + .write = ir_lirc_transmit_ir, + .ioctl = ir_lirc_ioctl, + .read = lirc_dev_fop_read, + .poll = lirc_dev_fop_poll, + .open = lirc_dev_fop_open, + .release = lirc_dev_fop_close, +}; + +static int ir_lirc_register(struct input_dev *input_dev) +{ + struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); + struct lirc_driver *drv; + struct lirc_buffer *rbuf; + int rc = -ENOMEM; + unsigned long features; + + drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL); + if (!drv) + return rc; + + rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL); + if (!drv) + goto rbuf_alloc_failed; + + rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE); + if (rc) + goto rbuf_init_failed; + + features = LIRC_CAN_REC_MODE2; + if (ir_dev->props->tx_ir) { + features |= LIRC_CAN_SEND_PULSE; + if (ir_dev->props->s_tx_mask) + features |= LIRC_CAN_SET_TRANSMITTER_MASK; + if (ir_dev->props->s_tx_carrier) + features |= LIRC_CAN_SET_SEND_CARRIER; + } + + snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)", + ir_dev->driver_name); + drv->minor = -1; + drv->features = features; + drv->data = &ir_dev->raw->lirc; + drv->rbuf = rbuf; + drv->set_use_inc = &ir_lirc_open; + drv->set_use_dec = &ir_lirc_close; + drv->code_length = sizeof(struct ir_raw_event) * 8; + drv->fops = &lirc_fops; + drv->dev = &ir_dev->dev; + drv->owner = THIS_MODULE; + + drv->minor = lirc_register_driver(drv); + if (drv->minor < 0) { + rc = -ENODEV; + goto lirc_register_failed; + } + + ir_dev->raw->lirc.drv = drv; + ir_dev->raw->lirc.ir_dev = ir_dev; + ir_dev->raw->lirc.lircdata = PULSE_MASK; + + return 0; + +lirc_register_failed: +rbuf_init_failed: + kfree(rbuf); +rbuf_alloc_failed: + kfree(drv); + + return rc; +} + +static int ir_lirc_unregister(struct input_dev *input_dev) +{ + struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); + struct lirc_codec *lirc = &ir_dev->raw->lirc; + + lirc_unregister_driver(lirc->drv->minor); + lirc_buffer_free(lirc->drv->rbuf); + kfree(lirc->drv); + + return 0; +} + +static struct ir_raw_handler lirc_handler = { + .protocols = IR_TYPE_LIRC, + .decode = ir_lirc_decode, + .raw_register = ir_lirc_register, + .raw_unregister = ir_lirc_unregister, +}; + +static int __init ir_lirc_codec_init(void) +{ + ir_raw_handler_register(&lirc_handler); + + printk(KERN_INFO "IR LIRC bridge handler initialized\n"); + return 0; +} + +static void __exit ir_lirc_codec_exit(void) +{ + ir_raw_handler_unregister(&lirc_handler); +} + +module_init(ir_lirc_codec_init); +module_exit(ir_lirc_codec_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Jarod Wilson "); +MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); +MODULE_DESCRIPTION("LIRC IR handler bridge"); -- cgit v1.2.3 From 044e5878c2158d701e6f47a9604910589a384ee2 Mon Sep 17 00:00:00 2001 From: Arnd Bergmann Date: Mon, 2 Aug 2010 15:43:35 -0300 Subject: V4L/DVB: lirc: use unlocked_ioctl New code should not rely on the big kernel lock, so use the unlocked_ioctl file operation in lirc. Signed-off-by: Arnd Bergmann Tested-by: Jarod Wilson Acked-by: Jarod Wilson Signed-off-by: Mauro Carvalho Chehab --- drivers/media/IR/ir-lirc-codec.c | 7 +++---- drivers/media/IR/lirc_dev.c | 12 ++++++------ drivers/media/IR/lirc_dev.h | 3 +-- 3 files changed, 10 insertions(+), 12 deletions(-) (limited to 'drivers/media/IR/ir-lirc-codec.c') diff --git a/drivers/media/IR/ir-lirc-codec.c b/drivers/media/IR/ir-lirc-codec.c index aff31d1b13d5..178bc5baab78 100644 --- a/drivers/media/IR/ir-lirc-codec.c +++ b/drivers/media/IR/ir-lirc-codec.c @@ -97,8 +97,7 @@ out: return ret; } -static int ir_lirc_ioctl(struct inode *node, struct file *filep, - unsigned int cmd, unsigned long arg) +static long ir_lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) { struct lirc_codec *lirc; struct ir_input_dev *ir_dev; @@ -154,7 +153,7 @@ static int ir_lirc_ioctl(struct inode *node, struct file *filep, break; default: - return lirc_dev_fop_ioctl(node, filep, cmd, arg); + return lirc_dev_fop_ioctl(filep, cmd, arg); } return ret; @@ -173,7 +172,7 @@ static void ir_lirc_close(void *data) static struct file_operations lirc_fops = { .owner = THIS_MODULE, .write = ir_lirc_transmit_ir, - .ioctl = ir_lirc_ioctl, + .unlocked_ioctl = ir_lirc_ioctl, .read = lirc_dev_fop_read, .poll = lirc_dev_fop_poll, .open = lirc_dev_fop_open, diff --git a/drivers/media/IR/lirc_dev.c b/drivers/media/IR/lirc_dev.c index 64170fa58006..c11b8f706258 100644 --- a/drivers/media/IR/lirc_dev.c +++ b/drivers/media/IR/lirc_dev.c @@ -160,7 +160,7 @@ static struct file_operations fops = { .read = lirc_dev_fop_read, .write = lirc_dev_fop_write, .poll = lirc_dev_fop_poll, - .ioctl = lirc_dev_fop_ioctl, + .unlocked_ioctl = lirc_dev_fop_ioctl, .open = lirc_dev_fop_open, .release = lirc_dev_fop_close, }; @@ -242,9 +242,9 @@ int lirc_register_driver(struct lirc_driver *d) goto out; } else if (!d->rbuf) { if (!(d->fops && d->fops->read && d->fops->poll && - d->fops->ioctl)) { + d->fops->unlocked_ioctl)) { dev_err(d->dev, "lirc_dev: lirc_register_driver: " - "neither read, poll nor ioctl can be NULL!\n"); + "neither read, poll nor unlocked_ioctl can be NULL!\n"); err = -EBADRQC; goto out; } @@ -425,6 +425,7 @@ int lirc_dev_fop_open(struct inode *inode, struct file *file) retval = -ENODEV; goto error; } + file->private_data = ir; dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); @@ -516,12 +517,11 @@ unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) } EXPORT_SYMBOL(lirc_dev_fop_poll); -int lirc_dev_fop_ioctl(struct inode *inode, struct file *file, - unsigned int cmd, unsigned long arg) +long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { unsigned long mode; int result = 0; - struct irctl *ir = irctls[iminor(inode)]; + struct irctl *ir = file->private_data; dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", ir->d.name, ir->d.minor, cmd); diff --git a/drivers/media/IR/lirc_dev.h b/drivers/media/IR/lirc_dev.h index 4afd96a38a43..b1f60663cb39 100644 --- a/drivers/media/IR/lirc_dev.h +++ b/drivers/media/IR/lirc_dev.h @@ -216,8 +216,7 @@ void *lirc_get_pdata(struct file *file); int lirc_dev_fop_open(struct inode *inode, struct file *file); int lirc_dev_fop_close(struct inode *inode, struct file *file); unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait); -int lirc_dev_fop_ioctl(struct inode *inode, struct file *file, - unsigned int cmd, unsigned long arg); +long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg); ssize_t lirc_dev_fop_read(struct file *file, char *buffer, size_t length, loff_t *ppos); ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, size_t length, -- cgit v1.2.3 From 5690085e7ba7f3081c6ab6db3a3b543444ad8a21 Mon Sep 17 00:00:00 2001 From: Jarod Wilson Date: Fri, 16 Jul 2010 14:25:33 -0300 Subject: V4L/DVB: IR/lirc: make lirc userspace and staging modules buildable The lirc userspace needs all the current ioctls defined, and we need to put the header files in places out-of-tree and/or staging lirc drivers (which I plan to prep soon) can easily build with. I've actually tested this in a tree w/all the lirc drivers queued up to be submitted for staging. I'm also reasonably sure that Andy Walls is going to need most of the ioctls anyway for his cx23888 IR driver work. Signed-off-by: Jarod Wilson Signed-off-by: Mauro Carvalho Chehab --- drivers/media/IR/ir-lirc-codec.c | 2 +- drivers/media/IR/lirc_dev.c | 2 +- drivers/media/IR/lirc_dev.h | 225 --------------------------------------- include/media/lirc.h | 34 +++--- include/media/lirc_dev.h | 225 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 245 insertions(+), 243 deletions(-) delete mode 100644 drivers/media/IR/lirc_dev.h create mode 100644 include/media/lirc_dev.h (limited to 'drivers/media/IR/ir-lirc-codec.c') diff --git a/drivers/media/IR/ir-lirc-codec.c b/drivers/media/IR/ir-lirc-codec.c index 178bc5baab78..afb1ada36c78 100644 --- a/drivers/media/IR/ir-lirc-codec.c +++ b/drivers/media/IR/ir-lirc-codec.c @@ -15,9 +15,9 @@ #include #include #include +#include #include #include "ir-core-priv.h" -#include "lirc_dev.h" #define LIRCBUF_SIZE 256 diff --git a/drivers/media/IR/lirc_dev.c b/drivers/media/IR/lirc_dev.c index c11b8f706258..899891bec352 100644 --- a/drivers/media/IR/lirc_dev.c +++ b/drivers/media/IR/lirc_dev.c @@ -37,7 +37,7 @@ #include #include -#include "lirc_dev.h" +#include static int debug; diff --git a/drivers/media/IR/lirc_dev.h b/drivers/media/IR/lirc_dev.h deleted file mode 100644 index b1f60663cb39..000000000000 --- a/drivers/media/IR/lirc_dev.h +++ /dev/null @@ -1,225 +0,0 @@ -/* - * LIRC base driver - * - * by Artur Lipowski - * This code is licensed under GNU GPL - * - */ - -#ifndef _LINUX_LIRC_DEV_H -#define _LINUX_LIRC_DEV_H - -#define MAX_IRCTL_DEVICES 4 -#define BUFLEN 16 - -#define mod(n, div) ((n) % (div)) - -#include -#include -#include -#include -#include -#include - -struct lirc_buffer { - wait_queue_head_t wait_poll; - spinlock_t fifo_lock; - unsigned int chunk_size; - unsigned int size; /* in chunks */ - /* Using chunks instead of bytes pretends to simplify boundary checking - * And should allow for some performance fine tunning later */ - struct kfifo fifo; - u8 fifo_initialized; -}; - -static inline void lirc_buffer_clear(struct lirc_buffer *buf) -{ - unsigned long flags; - - if (buf->fifo_initialized) { - spin_lock_irqsave(&buf->fifo_lock, flags); - kfifo_reset(&buf->fifo); - spin_unlock_irqrestore(&buf->fifo_lock, flags); - } else - WARN(1, "calling %s on an uninitialized lirc_buffer\n", - __func__); -} - -static inline int lirc_buffer_init(struct lirc_buffer *buf, - unsigned int chunk_size, - unsigned int size) -{ - int ret; - - init_waitqueue_head(&buf->wait_poll); - spin_lock_init(&buf->fifo_lock); - buf->chunk_size = chunk_size; - buf->size = size; - ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL); - if (ret == 0) - buf->fifo_initialized = 1; - - return ret; -} - -static inline void lirc_buffer_free(struct lirc_buffer *buf) -{ - if (buf->fifo_initialized) { - kfifo_free(&buf->fifo); - buf->fifo_initialized = 0; - } else - WARN(1, "calling %s on an uninitialized lirc_buffer\n", - __func__); -} - -static inline int lirc_buffer_len(struct lirc_buffer *buf) -{ - int len; - unsigned long flags; - - spin_lock_irqsave(&buf->fifo_lock, flags); - len = kfifo_len(&buf->fifo); - spin_unlock_irqrestore(&buf->fifo_lock, flags); - - return len; -} - -static inline int lirc_buffer_full(struct lirc_buffer *buf) -{ - return lirc_buffer_len(buf) == buf->size * buf->chunk_size; -} - -static inline int lirc_buffer_empty(struct lirc_buffer *buf) -{ - return !lirc_buffer_len(buf); -} - -static inline int lirc_buffer_available(struct lirc_buffer *buf) -{ - return buf->size - (lirc_buffer_len(buf) / buf->chunk_size); -} - -static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf, - unsigned char *dest) -{ - unsigned int ret = 0; - - if (lirc_buffer_len(buf) >= buf->chunk_size) - ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size, - &buf->fifo_lock); - return ret; - -} - -static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf, - unsigned char *orig) -{ - unsigned int ret; - - ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size, - &buf->fifo_lock); - - return ret; -} - -struct lirc_driver { - char name[40]; - int minor; - unsigned long code_length; - unsigned int buffer_size; /* in chunks holding one code each */ - int sample_rate; - unsigned long features; - - unsigned int chunk_size; - - void *data; - int min_timeout; - int max_timeout; - int (*add_to_buf) (void *data, struct lirc_buffer *buf); - struct lirc_buffer *rbuf; - int (*set_use_inc) (void *data); - void (*set_use_dec) (void *data); - struct file_operations *fops; - struct device *dev; - struct module *owner; -}; - -/* name: - * this string will be used for logs - * - * minor: - * indicates minor device (/dev/lirc) number for registered driver - * if caller fills it with negative value, then the first free minor - * number will be used (if available) - * - * code_length: - * length of the remote control key code expressed in bits - * - * sample_rate: - * - * data: - * it may point to any driver data and this pointer will be passed to - * all callback functions - * - * add_to_buf: - * add_to_buf will be called after specified period of the time or - * triggered by the external event, this behavior depends on value of - * the sample_rate this function will be called in user context. This - * routine should return 0 if data was added to the buffer and - * -ENODATA if none was available. This should add some number of bits - * evenly divisible by code_length to the buffer - * - * rbuf: - * if not NULL, it will be used as a read buffer, you will have to - * write to the buffer by other means, like irq's (see also - * lirc_serial.c). - * - * set_use_inc: - * set_use_inc will be called after device is opened - * - * set_use_dec: - * set_use_dec will be called after device is closed - * - * fops: - * file_operations for drivers which don't fit the current driver model. - * - * Some ioctl's can be directly handled by lirc_dev if the driver's - * ioctl function is NULL or if it returns -ENOIOCTLCMD (see also - * lirc_serial.c). - * - * owner: - * the module owning this struct - * - */ - - -/* following functions can be called ONLY from user context - * - * returns negative value on error or minor number - * of the registered device if success - * contents of the structure pointed by p is copied - */ -extern int lirc_register_driver(struct lirc_driver *d); - -/* returns negative value on error or 0 if success -*/ -extern int lirc_unregister_driver(int minor); - -/* Returns the private data stored in the lirc_driver - * associated with the given device file pointer. - */ -void *lirc_get_pdata(struct file *file); - -/* default file operations - * used by drivers if they override only some operations - */ -int lirc_dev_fop_open(struct inode *inode, struct file *file); -int lirc_dev_fop_close(struct inode *inode, struct file *file); -unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait); -long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg); -ssize_t lirc_dev_fop_read(struct file *file, char *buffer, size_t length, - loff_t *ppos); -ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, size_t length, - loff_t *ppos); - -#endif diff --git a/include/media/lirc.h b/include/media/lirc.h index 8dffd4f47bf6..42c467c50519 100644 --- a/include/media/lirc.h +++ b/include/media/lirc.h @@ -1,6 +1,6 @@ /* * lirc.h - linux infrared remote control header file - * last modified 2010/06/03 by Jarod Wilson + * last modified 2010/07/13 by Jarod Wilson */ #ifndef _LINUX_LIRC_H @@ -33,6 +33,9 @@ #define LIRC_IS_FREQUENCY(val) (LIRC_MODE2(val) == LIRC_MODE2_FREQUENCY) #define LIRC_IS_TIMEOUT(val) (LIRC_MODE2(val) == LIRC_MODE2_TIMEOUT) +/* used heavily by lirc userspace */ +#define lirc_t int + /*** lirc compatible hardware features ***/ #define LIRC_MODE2SEND(x) (x) @@ -95,12 +98,10 @@ #define LIRC_GET_MIN_TIMEOUT _IOR('i', 0x00000008, __u32) #define LIRC_GET_MAX_TIMEOUT _IOR('i', 0x00000009, __u32) -#if 0 /* these ioctls are not used at the moment */ #define LIRC_GET_MIN_FILTER_PULSE _IOR('i', 0x0000000a, __u32) #define LIRC_GET_MAX_FILTER_PULSE _IOR('i', 0x0000000b, __u32) #define LIRC_GET_MIN_FILTER_SPACE _IOR('i', 0x0000000c, __u32) #define LIRC_GET_MAX_FILTER_SPACE _IOR('i', 0x0000000d, __u32) -#endif /* code length in bits, currently only for LIRC_MODE_LIRCCODE */ #define LIRC_GET_LENGTH _IOR('i', 0x0000000f, __u32) @@ -121,23 +122,30 @@ */ #define LIRC_SET_REC_TIMEOUT _IOW('i', 0x00000018, __u32) -#if 0 /* these ioctls are not used at the moment */ +/* 1 enables, 0 disables timeout reports in MODE2 */ +#define LIRC_SET_REC_TIMEOUT_REPORTS _IOW('i', 0x00000019, __u32) + /* * pulses shorter than this are filtered out by hardware (software * emulation in lirc_dev?) */ -#define LIRC_SET_REC_FILTER_PULSE _IOW('i', 0x00000019, __u32) +#define LIRC_SET_REC_FILTER_PULSE _IOW('i', 0x0000001a, __u32) /* * spaces shorter than this are filtered out by hardware (software * emulation in lirc_dev?) */ -#define LIRC_SET_REC_FILTER_SPACE _IOW('i', 0x0000001a, __u32) +#define LIRC_SET_REC_FILTER_SPACE _IOW('i', 0x0000001b, __u32) /* * if filter cannot be set independantly for pulse/space, this should * be used */ -#define LIRC_SET_REC_FILTER _IOW('i', 0x0000001b, __u32) -#endif +#define LIRC_SET_REC_FILTER _IOW('i', 0x0000001c, __u32) + +/* + * if enabled from the next key press on the driver will send + * LIRC_MODE2_FREQUENCY packets + */ +#define LIRC_SET_MEASURE_CARRIER_MODE _IOW('i', 0x0000001d, __u32) /* * to set a range use @@ -151,13 +159,7 @@ #define LIRC_NOTIFY_DECODE _IO('i', 0x00000020) -#if 0 /* these ioctls are not used at the moment */ -/* - * from the next key press on the driver will send - * LIRC_MODE2_FREQUENCY packets - */ -#define LIRC_MEASURE_CARRIER_ENABLE _IO('i', 0x00000021) -#define LIRC_MEASURE_CARRIER_DISABLE _IO('i', 0x00000022) -#endif +#define LIRC_SETUP_START _IO('i', 0x00000021) +#define LIRC_SETUP_END _IO('i', 0x00000022) #endif diff --git a/include/media/lirc_dev.h b/include/media/lirc_dev.h new file mode 100644 index 000000000000..b1f60663cb39 --- /dev/null +++ b/include/media/lirc_dev.h @@ -0,0 +1,225 @@ +/* + * LIRC base driver + * + * by Artur Lipowski + * This code is licensed under GNU GPL + * + */ + +#ifndef _LINUX_LIRC_DEV_H +#define _LINUX_LIRC_DEV_H + +#define MAX_IRCTL_DEVICES 4 +#define BUFLEN 16 + +#define mod(n, div) ((n) % (div)) + +#include +#include +#include +#include +#include +#include + +struct lirc_buffer { + wait_queue_head_t wait_poll; + spinlock_t fifo_lock; + unsigned int chunk_size; + unsigned int size; /* in chunks */ + /* Using chunks instead of bytes pretends to simplify boundary checking + * And should allow for some performance fine tunning later */ + struct kfifo fifo; + u8 fifo_initialized; +}; + +static inline void lirc_buffer_clear(struct lirc_buffer *buf) +{ + unsigned long flags; + + if (buf->fifo_initialized) { + spin_lock_irqsave(&buf->fifo_lock, flags); + kfifo_reset(&buf->fifo); + spin_unlock_irqrestore(&buf->fifo_lock, flags); + } else + WARN(1, "calling %s on an uninitialized lirc_buffer\n", + __func__); +} + +static inline int lirc_buffer_init(struct lirc_buffer *buf, + unsigned int chunk_size, + unsigned int size) +{ + int ret; + + init_waitqueue_head(&buf->wait_poll); + spin_lock_init(&buf->fifo_lock); + buf->chunk_size = chunk_size; + buf->size = size; + ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL); + if (ret == 0) + buf->fifo_initialized = 1; + + return ret; +} + +static inline void lirc_buffer_free(struct lirc_buffer *buf) +{ + if (buf->fifo_initialized) { + kfifo_free(&buf->fifo); + buf->fifo_initialized = 0; + } else + WARN(1, "calling %s on an uninitialized lirc_buffer\n", + __func__); +} + +static inline int lirc_buffer_len(struct lirc_buffer *buf) +{ + int len; + unsigned long flags; + + spin_lock_irqsave(&buf->fifo_lock, flags); + len = kfifo_len(&buf->fifo); + spin_unlock_irqrestore(&buf->fifo_lock, flags); + + return len; +} + +static inline int lirc_buffer_full(struct lirc_buffer *buf) +{ + return lirc_buffer_len(buf) == buf->size * buf->chunk_size; +} + +static inline int lirc_buffer_empty(struct lirc_buffer *buf) +{ + return !lirc_buffer_len(buf); +} + +static inline int lirc_buffer_available(struct lirc_buffer *buf) +{ + return buf->size - (lirc_buffer_len(buf) / buf->chunk_size); +} + +static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf, + unsigned char *dest) +{ + unsigned int ret = 0; + + if (lirc_buffer_len(buf) >= buf->chunk_size) + ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size, + &buf->fifo_lock); + return ret; + +} + +static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf, + unsigned char *orig) +{ + unsigned int ret; + + ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size, + &buf->fifo_lock); + + return ret; +} + +struct lirc_driver { + char name[40]; + int minor; + unsigned long code_length; + unsigned int buffer_size; /* in chunks holding one code each */ + int sample_rate; + unsigned long features; + + unsigned int chunk_size; + + void *data; + int min_timeout; + int max_timeout; + int (*add_to_buf) (void *data, struct lirc_buffer *buf); + struct lirc_buffer *rbuf; + int (*set_use_inc) (void *data); + void (*set_use_dec) (void *data); + struct file_operations *fops; + struct device *dev; + struct module *owner; +}; + +/* name: + * this string will be used for logs + * + * minor: + * indicates minor device (/dev/lirc) number for registered driver + * if caller fills it with negative value, then the first free minor + * number will be used (if available) + * + * code_length: + * length of the remote control key code expressed in bits + * + * sample_rate: + * + * data: + * it may point to any driver data and this pointer will be passed to + * all callback functions + * + * add_to_buf: + * add_to_buf will be called after specified period of the time or + * triggered by the external event, this behavior depends on value of + * the sample_rate this function will be called in user context. This + * routine should return 0 if data was added to the buffer and + * -ENODATA if none was available. This should add some number of bits + * evenly divisible by code_length to the buffer + * + * rbuf: + * if not NULL, it will be used as a read buffer, you will have to + * write to the buffer by other means, like irq's (see also + * lirc_serial.c). + * + * set_use_inc: + * set_use_inc will be called after device is opened + * + * set_use_dec: + * set_use_dec will be called after device is closed + * + * fops: + * file_operations for drivers which don't fit the current driver model. + * + * Some ioctl's can be directly handled by lirc_dev if the driver's + * ioctl function is NULL or if it returns -ENOIOCTLCMD (see also + * lirc_serial.c). + * + * owner: + * the module owning this struct + * + */ + + +/* following functions can be called ONLY from user context + * + * returns negative value on error or minor number + * of the registered device if success + * contents of the structure pointed by p is copied + */ +extern int lirc_register_driver(struct lirc_driver *d); + +/* returns negative value on error or 0 if success +*/ +extern int lirc_unregister_driver(int minor); + +/* Returns the private data stored in the lirc_driver + * associated with the given device file pointer. + */ +void *lirc_get_pdata(struct file *file); + +/* default file operations + * used by drivers if they override only some operations + */ +int lirc_dev_fop_open(struct inode *inode, struct file *file); +int lirc_dev_fop_close(struct inode *inode, struct file *file); +unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait); +long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg); +ssize_t lirc_dev_fop_read(struct file *file, char *buffer, size_t length, + loff_t *ppos); +ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, size_t length, + loff_t *ppos); + +#endif -- cgit v1.2.3 From 6efb870a115ac223ab578bc76699ba8591250568 Mon Sep 17 00:00:00 2001 From: Jarod Wilson Date: Fri, 16 Jul 2010 18:29:54 -0300 Subject: V4L/DVB: IR/lirc: use memdup_user instead of copy_from_user Signed-off-by: Jarod Wilson Signed-off-by: Mauro Carvalho Chehab --- drivers/media/IR/ir-lirc-codec.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'drivers/media/IR/ir-lirc-codec.c') diff --git a/drivers/media/IR/ir-lirc-codec.c b/drivers/media/IR/ir-lirc-codec.c index afb1ada36c78..ee1f2d443ab2 100644 --- a/drivers/media/IR/ir-lirc-codec.c +++ b/drivers/media/IR/ir-lirc-codec.c @@ -74,14 +74,9 @@ static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf, if (count > LIRCBUF_SIZE || count % 2 == 0) return -EINVAL; - txbuf = kzalloc(sizeof(int) * LIRCBUF_SIZE, GFP_KERNEL); - if (!txbuf) - return -ENOMEM; - - if (copy_from_user(txbuf, buf, n)) { - ret = -EFAULT; - goto out; - } + txbuf = memdup_user(buf, n); + if (IS_ERR(txbuf)) + return PTR_ERR(txbuf); ir_dev = lirc->ir_dev; if (!ir_dev) { -- cgit v1.2.3 From 49b7a12c0aa217c9fb163d330b5b80bafe55cb8b Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Fri, 23 Jul 2010 07:08:26 -0300 Subject: V4L/DVB: media/IR: testing the wrong variable There is a typo here. We meant to test "rbuf" instead of "drv". We already tested "drv" earlier. Signed-off-by: Dan Carpenter Acked-by: Jarod Wilson Signed-off-by: Mauro Carvalho Chehab --- drivers/media/IR/ir-lirc-codec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/media/IR/ir-lirc-codec.c') diff --git a/drivers/media/IR/ir-lirc-codec.c b/drivers/media/IR/ir-lirc-codec.c index ee1f2d443ab2..3ba482d96c4b 100644 --- a/drivers/media/IR/ir-lirc-codec.c +++ b/drivers/media/IR/ir-lirc-codec.c @@ -187,7 +187,7 @@ static int ir_lirc_register(struct input_dev *input_dev) return rc; rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL); - if (!drv) + if (!rbuf) goto rbuf_alloc_failed; rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE); -- cgit v1.2.3