1 /* ir-lirc-codec.c - rc-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 <linux/module.h>
18 #include <media/lirc.h>
19 #include <media/lirc_dev.h>
20 #include <media/rc-core.h>
21 #include "rc-core-priv.h"
23 #define LIRCBUF_SIZE 256
26 * ir_lirc_decode() - Send raw IR data to lirc_dev to be relayed to the
27 * lircd userspace daemon for decoding.
28 * @input_dev: the struct rc_dev descriptor of the device
29 * @duration: the struct ir_raw_event descriptor of the pulse/space
31 * This function returns -EINVAL if the lirc interfaces aren't wired up.
33 static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev)
35 struct lirc_codec *lirc = &dev->raw->lirc;
38 if (!(dev->raw->enabled_protocols & RC_TYPE_LIRC))
41 if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
46 /* Userspace expects a long space event before the start of
47 * the signal to use as a sync. This may be done with repeat
48 * packets and normal samples. But if a reset has been sent
49 * then we assume that a long time has passed, so we send a
50 * space with the maximum time value. */
51 sample = LIRC_SPACE(LIRC_VALUE_MASK);
52 IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
55 } else if (ev.carrier_report) {
56 sample = LIRC_FREQUENCY(ev.carrier);
57 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
60 } else if (ev.timeout) {
65 lirc->gap_start = ktime_get();
67 lirc->gap_duration = ev.duration;
69 if (!lirc->send_timeout_reports)
72 sample = LIRC_TIMEOUT(ev.duration / 1000);
73 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
81 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
84 /* Convert to ms and cap by LIRC_VALUE_MASK */
85 do_div(lirc->gap_duration, 1000);
86 lirc->gap_duration = min(lirc->gap_duration,
87 (u64)LIRC_VALUE_MASK);
89 gap_sample = LIRC_SPACE(lirc->gap_duration);
90 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
91 (unsigned char *) &gap_sample);
95 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
96 LIRC_SPACE(ev.duration / 1000);
97 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
98 TO_US(ev.duration), TO_STR(ev.pulse));
101 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
102 (unsigned char *) &sample);
103 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
108 static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
109 size_t n, loff_t *ppos)
111 struct lirc_codec *lirc;
113 unsigned int *txbuf; /* buffer with values to transmit */
117 lirc = lirc_get_pdata(file);
121 if (n < sizeof(unsigned) || n % sizeof(unsigned))
124 count = n / sizeof(unsigned);
125 if (count > LIRCBUF_SIZE || count % 2 == 0)
128 txbuf = memdup_user(buf, n);
130 return PTR_ERR(txbuf);
139 ret = dev->tx_ir(dev, txbuf, count);
142 ret *= sizeof(unsigned);
149 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
152 struct lirc_codec *lirc;
154 u32 __user *argp = (u32 __user *)(arg);
158 lirc = lirc_get_pdata(filep);
166 if (_IOC_DIR(cmd) & _IOC_WRITE) {
167 ret = get_user(val, argp);
175 case LIRC_GET_SEND_MODE:
176 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
179 case LIRC_SET_SEND_MODE:
180 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
185 case LIRC_SET_TRANSMITTER_MASK:
189 return dev->s_tx_mask(dev, val);
191 case LIRC_SET_SEND_CARRIER:
192 if (!dev->s_tx_carrier)
195 return dev->s_tx_carrier(dev, val);
197 case LIRC_SET_SEND_DUTY_CYCLE:
198 if (!dev->s_tx_duty_cycle)
201 if (val <= 0 || val >= 100)
204 return dev->s_tx_duty_cycle(dev, val);
207 case LIRC_SET_REC_CARRIER:
208 if (!dev->s_rx_carrier_range)
214 return dev->s_rx_carrier_range(dev,
215 dev->raw->lirc.carrier_low,
218 case LIRC_SET_REC_CARRIER_RANGE:
222 dev->raw->lirc.carrier_low = val;
225 case LIRC_GET_REC_RESOLUTION:
226 val = dev->rx_resolution;
229 case LIRC_SET_WIDEBAND_RECEIVER:
230 if (!dev->s_learning_mode)
233 return dev->s_learning_mode(dev, !!val);
235 case LIRC_SET_MEASURE_CARRIER_MODE:
236 if (!dev->s_carrier_report)
239 return dev->s_carrier_report(dev, !!val);
241 /* Generic timeout support */
242 case LIRC_GET_MIN_TIMEOUT:
243 if (!dev->max_timeout)
245 val = dev->min_timeout / 1000;
248 case LIRC_GET_MAX_TIMEOUT:
249 if (!dev->max_timeout)
251 val = dev->max_timeout / 1000;
254 case LIRC_SET_REC_TIMEOUT:
255 if (!dev->max_timeout)
260 if (tmp < dev->min_timeout ||
261 tmp > dev->max_timeout)
267 case LIRC_SET_REC_TIMEOUT_REPORTS:
268 lirc->send_timeout_reports = !!val;
272 return lirc_dev_fop_ioctl(filep, cmd, arg);
275 if (_IOC_DIR(cmd) & _IOC_READ)
276 ret = put_user(val, argp);
281 static int ir_lirc_open(void *data)
286 static void ir_lirc_close(void *data)
291 static struct file_operations lirc_fops = {
292 .owner = THIS_MODULE,
293 .write = ir_lirc_transmit_ir,
294 .unlocked_ioctl = ir_lirc_ioctl,
296 .compat_ioctl = ir_lirc_ioctl,
298 .read = lirc_dev_fop_read,
299 .poll = lirc_dev_fop_poll,
300 .open = lirc_dev_fop_open,
301 .release = lirc_dev_fop_close,
305 static int ir_lirc_register(struct rc_dev *dev)
307 struct lirc_driver *drv;
308 struct lirc_buffer *rbuf;
310 unsigned long features;
312 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
316 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
318 goto rbuf_alloc_failed;
320 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
322 goto rbuf_init_failed;
324 features = LIRC_CAN_REC_MODE2;
326 features |= LIRC_CAN_SEND_PULSE;
328 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
329 if (dev->s_tx_carrier)
330 features |= LIRC_CAN_SET_SEND_CARRIER;
331 if (dev->s_tx_duty_cycle)
332 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
335 if (dev->s_rx_carrier_range)
336 features |= LIRC_CAN_SET_REC_CARRIER |
337 LIRC_CAN_SET_REC_CARRIER_RANGE;
339 if (dev->s_learning_mode)
340 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
342 if (dev->s_carrier_report)
343 features |= LIRC_CAN_MEASURE_CARRIER;
345 if (dev->max_timeout)
346 features |= LIRC_CAN_SET_REC_TIMEOUT;
348 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
351 drv->features = features;
352 drv->data = &dev->raw->lirc;
354 drv->set_use_inc = &ir_lirc_open;
355 drv->set_use_dec = &ir_lirc_close;
356 drv->code_length = sizeof(struct ir_raw_event) * 8;
357 drv->fops = &lirc_fops;
358 drv->dev = &dev->dev;
359 drv->owner = THIS_MODULE;
361 drv->minor = lirc_register_driver(drv);
362 if (drv->minor < 0) {
364 goto lirc_register_failed;
367 dev->raw->lirc.drv = drv;
368 dev->raw->lirc.dev = dev;
371 lirc_register_failed:
380 static int ir_lirc_unregister(struct rc_dev *dev)
382 struct lirc_codec *lirc = &dev->raw->lirc;
384 lirc_unregister_driver(lirc->drv->minor);
385 lirc_buffer_free(lirc->drv->rbuf);
391 static struct ir_raw_handler lirc_handler = {
392 .protocols = RC_TYPE_LIRC,
393 .decode = ir_lirc_decode,
394 .raw_register = ir_lirc_register,
395 .raw_unregister = ir_lirc_unregister,
398 static int __init ir_lirc_codec_init(void)
400 ir_raw_handler_register(&lirc_handler);
402 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
406 static void __exit ir_lirc_codec_exit(void)
408 ir_raw_handler_unregister(&lirc_handler);
411 module_init(ir_lirc_codec_init);
412 module_exit(ir_lirc_codec_exit);
414 MODULE_LICENSE("GPL");
415 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
416 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
417 MODULE_DESCRIPTION("LIRC IR handler bridge");