1 /* ir-lirc-codec.c - ir-core to classic lirc interface bridge
3 * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/sched.h>
16 #include <linux/wait.h>
17 #include <media/lirc.h>
18 #include <media/lirc_dev.h>
19 #include <media/ir-core.h>
20 #include "ir-core-priv.h"
22 #define LIRCBUF_SIZE 256
25 * ir_lirc_decode() - Send raw IR data to lirc_dev to be relayed to the
26 * lircd userspace daemon for decoding.
27 * @input_dev: the struct input_dev descriptor of the device
28 * @duration: the struct ir_raw_event descriptor of the pulse/space
30 * This function returns -EINVAL if the lirc interfaces aren't wired up.
32 static int ir_lirc_decode(struct input_dev *input_dev, struct ir_raw_event ev)
34 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
37 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC))
40 if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf)
46 IR_dprintk(2, "LIRC data transfer started (%uus %s)\n",
47 TO_US(ev.duration), TO_STR(ev.pulse));
49 sample = ev.duration / 1000;
53 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
54 (unsigned char *) &sample);
55 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
61 static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
62 size_t n, loff_t *ppos)
64 struct lirc_codec *lirc;
65 struct ir_input_dev *ir_dev;
66 int *txbuf; /* buffer with values to transmit */
69 lirc = lirc_get_pdata(file);
76 count = n / sizeof(int);
77 if (count > LIRCBUF_SIZE || count % 2 == 0)
80 txbuf = memdup_user(buf, n);
82 return PTR_ERR(txbuf);
84 ir_dev = lirc->ir_dev;
90 if (ir_dev->props && ir_dev->props->tx_ir)
91 ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n);
98 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
99 unsigned long __user arg)
101 struct lirc_codec *lirc;
102 struct ir_input_dev *ir_dev;
105 unsigned long val = 0;
107 lirc = lirc_get_pdata(filep);
111 ir_dev = lirc->ir_dev;
112 if (!ir_dev || !ir_dev->props || !ir_dev->props->priv)
115 drv_data = ir_dev->props->priv;
117 if (_IOC_DIR(cmd) & _IOC_WRITE) {
118 ret = get_user(val, (unsigned long *)arg);
126 case LIRC_GET_SEND_MODE:
127 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
130 case LIRC_SET_SEND_MODE:
131 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
136 case LIRC_SET_TRANSMITTER_MASK:
137 if (ir_dev->props->s_tx_mask)
138 ret = ir_dev->props->s_tx_mask(drv_data, (u32)val);
143 case LIRC_SET_SEND_CARRIER:
144 if (ir_dev->props->s_tx_carrier)
145 ir_dev->props->s_tx_carrier(drv_data, (u32)val);
150 case LIRC_SET_SEND_DUTY_CYCLE:
151 if (!ir_dev->props->s_tx_duty_cycle)
154 if (val <= 0 || val >= 100)
157 ir_dev->props->s_tx_duty_cycle(ir_dev->props->priv, val);
161 case LIRC_SET_REC_CARRIER:
162 if (ir_dev->props->s_rx_carrier_range)
163 ret = ir_dev->props->s_rx_carrier_range(
165 ir_dev->raw->lirc.carrier_low, val);
170 ir_dev->raw->lirc.carrier_low = 0;
173 case LIRC_SET_REC_CARRIER_RANGE:
175 ir_dev->raw->lirc.carrier_low = val;
179 case LIRC_GET_REC_RESOLUTION:
180 val = ir_dev->props->rx_resolution;
183 case LIRC_SET_WIDEBAND_RECEIVER:
184 if (ir_dev->props->s_learning_mode)
185 return ir_dev->props->s_learning_mode(
186 ir_dev->props->priv, !!val);
190 /* Generic timeout support */
191 case LIRC_GET_MIN_TIMEOUT:
192 if (!ir_dev->props->max_timeout)
194 val = ir_dev->props->min_timeout / 1000;
197 case LIRC_GET_MAX_TIMEOUT:
198 if (!ir_dev->props->max_timeout)
200 val = ir_dev->props->max_timeout / 1000;
203 case LIRC_SET_REC_TIMEOUT:
204 if (val < ir_dev->props->min_timeout ||
205 val > ir_dev->props->max_timeout)
207 ir_dev->props->timeout = val * 1000;
211 return lirc_dev_fop_ioctl(filep, cmd, arg);
214 if (_IOC_DIR(cmd) & _IOC_READ)
215 ret = put_user(val, (unsigned long *)arg);
220 static int ir_lirc_open(void *data)
225 static void ir_lirc_close(void *data)
230 static struct file_operations lirc_fops = {
231 .owner = THIS_MODULE,
232 .write = ir_lirc_transmit_ir,
233 .unlocked_ioctl = ir_lirc_ioctl,
234 .read = lirc_dev_fop_read,
235 .poll = lirc_dev_fop_poll,
236 .open = lirc_dev_fop_open,
237 .release = lirc_dev_fop_close,
241 static int ir_lirc_register(struct input_dev *input_dev)
243 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
244 struct lirc_driver *drv;
245 struct lirc_buffer *rbuf;
247 unsigned long features;
249 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
253 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
255 goto rbuf_alloc_failed;
257 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
259 goto rbuf_init_failed;
261 features = LIRC_CAN_REC_MODE2;
262 if (ir_dev->props->tx_ir) {
264 features |= LIRC_CAN_SEND_PULSE;
265 if (ir_dev->props->s_tx_mask)
266 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
267 if (ir_dev->props->s_tx_carrier)
268 features |= LIRC_CAN_SET_SEND_CARRIER;
270 if (ir_dev->props->s_tx_duty_cycle)
271 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
274 if (ir_dev->props->s_rx_carrier_range)
275 features |= LIRC_CAN_SET_REC_CARRIER |
276 LIRC_CAN_SET_REC_CARRIER_RANGE;
278 if (ir_dev->props->s_learning_mode)
279 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
281 if (ir_dev->props->max_timeout)
282 features |= LIRC_CAN_SET_REC_TIMEOUT;
285 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
286 ir_dev->driver_name);
288 drv->features = features;
289 drv->data = &ir_dev->raw->lirc;
291 drv->set_use_inc = &ir_lirc_open;
292 drv->set_use_dec = &ir_lirc_close;
293 drv->code_length = sizeof(struct ir_raw_event) * 8;
294 drv->fops = &lirc_fops;
295 drv->dev = &ir_dev->dev;
296 drv->owner = THIS_MODULE;
298 drv->minor = lirc_register_driver(drv);
299 if (drv->minor < 0) {
301 goto lirc_register_failed;
304 ir_dev->raw->lirc.drv = drv;
305 ir_dev->raw->lirc.ir_dev = ir_dev;
308 lirc_register_failed:
317 static int ir_lirc_unregister(struct input_dev *input_dev)
319 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
320 struct lirc_codec *lirc = &ir_dev->raw->lirc;
322 lirc_unregister_driver(lirc->drv->minor);
323 lirc_buffer_free(lirc->drv->rbuf);
329 static struct ir_raw_handler lirc_handler = {
330 .protocols = IR_TYPE_LIRC,
331 .decode = ir_lirc_decode,
332 .raw_register = ir_lirc_register,
333 .raw_unregister = ir_lirc_unregister,
336 static int __init ir_lirc_codec_init(void)
338 ir_raw_handler_register(&lirc_handler);
340 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
344 static void __exit ir_lirc_codec_exit(void)
346 ir_raw_handler_unregister(&lirc_handler);
349 module_init(ir_lirc_codec_init);
350 module_exit(ir_lirc_codec_exit);
352 MODULE_LICENSE("GPL");
353 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
354 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
355 MODULE_DESCRIPTION("LIRC IR handler bridge");