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->lirc.drv || !dev->raw->lirc.drv->rbuf)
43 /* Userspace expects a long space event before the start of
44 * the signal to use as a sync. This may be done with repeat
45 * packets and normal samples. But if a reset has been sent
46 * then we assume that a long time has passed, so we send a
47 * space with the maximum time value. */
48 sample = LIRC_SPACE(LIRC_VALUE_MASK);
49 IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
52 } else if (ev.carrier_report) {
53 sample = LIRC_FREQUENCY(ev.carrier);
54 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
57 } else if (ev.timeout) {
62 lirc->gap_start = ktime_get();
64 lirc->gap_duration = ev.duration;
66 if (!lirc->send_timeout_reports)
69 sample = LIRC_TIMEOUT(ev.duration / 1000);
70 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
78 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
81 /* Convert to ms and cap by LIRC_VALUE_MASK */
82 do_div(lirc->gap_duration, 1000);
83 lirc->gap_duration = min(lirc->gap_duration,
84 (u64)LIRC_VALUE_MASK);
86 gap_sample = LIRC_SPACE(lirc->gap_duration);
87 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
88 (unsigned char *) &gap_sample);
92 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
93 LIRC_SPACE(ev.duration / 1000);
94 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
95 TO_US(ev.duration), TO_STR(ev.pulse));
98 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
99 (unsigned char *) &sample);
100 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
105 static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
106 size_t n, loff_t *ppos)
108 struct lirc_codec *lirc;
110 unsigned int *txbuf; /* buffer with values to transmit */
111 ssize_t ret = -EINVAL;
115 unsigned int duration = 0; /* signal duration in us */
120 lirc = lirc_get_pdata(file);
124 if (n < sizeof(unsigned) || n % sizeof(unsigned))
127 count = n / sizeof(unsigned);
128 if (count > LIRCBUF_SIZE || count % 2 == 0)
131 txbuf = memdup_user(buf, n);
133 return PTR_ERR(txbuf);
146 for (i = 0; i < count; i++) {
147 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
152 duration += txbuf[i];
155 ret = dev->tx_ir(dev, txbuf, count);
159 for (duration = i = 0; i < ret; i++)
160 duration += txbuf[i];
162 ret *= sizeof(unsigned int);
165 * The lircd gap calculation expects the write function to
166 * wait for the actual IR signal to be transmitted before
169 towait = ktime_us_delta(ktime_add_us(start, duration), ktime_get());
171 set_current_state(TASK_INTERRUPTIBLE);
172 schedule_timeout(usecs_to_jiffies(towait));
180 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
183 struct lirc_codec *lirc;
185 u32 __user *argp = (u32 __user *)(arg);
189 lirc = lirc_get_pdata(filep);
197 if (_IOC_DIR(cmd) & _IOC_WRITE) {
198 ret = get_user(val, argp);
206 case LIRC_GET_SEND_MODE:
210 val = LIRC_MODE_PULSE;
213 case LIRC_SET_SEND_MODE:
217 if (val != LIRC_MODE_PULSE)
222 case LIRC_SET_TRANSMITTER_MASK:
226 return dev->s_tx_mask(dev, val);
228 case LIRC_SET_SEND_CARRIER:
229 if (!dev->s_tx_carrier)
232 return dev->s_tx_carrier(dev, val);
234 case LIRC_SET_SEND_DUTY_CYCLE:
235 if (!dev->s_tx_duty_cycle)
238 if (val <= 0 || val >= 100)
241 return dev->s_tx_duty_cycle(dev, val);
244 case LIRC_SET_REC_CARRIER:
245 if (!dev->s_rx_carrier_range)
251 return dev->s_rx_carrier_range(dev,
252 dev->raw->lirc.carrier_low,
255 case LIRC_SET_REC_CARRIER_RANGE:
256 if (!dev->s_rx_carrier_range)
262 dev->raw->lirc.carrier_low = val;
265 case LIRC_GET_REC_RESOLUTION:
266 if (!dev->rx_resolution)
269 val = dev->rx_resolution;
272 case LIRC_SET_WIDEBAND_RECEIVER:
273 if (!dev->s_learning_mode)
276 return dev->s_learning_mode(dev, !!val);
278 case LIRC_SET_MEASURE_CARRIER_MODE:
279 if (!dev->s_carrier_report)
282 return dev->s_carrier_report(dev, !!val);
284 /* Generic timeout support */
285 case LIRC_GET_MIN_TIMEOUT:
286 if (!dev->max_timeout)
288 val = DIV_ROUND_UP(dev->min_timeout, 1000);
291 case LIRC_GET_MAX_TIMEOUT:
292 if (!dev->max_timeout)
294 val = dev->max_timeout / 1000;
297 case LIRC_SET_REC_TIMEOUT:
298 if (!dev->max_timeout)
303 if (tmp < dev->min_timeout ||
304 tmp > dev->max_timeout)
308 ret = dev->s_timeout(dev, tmp);
313 case LIRC_SET_REC_TIMEOUT_REPORTS:
317 lirc->send_timeout_reports = !!val;
321 return lirc_dev_fop_ioctl(filep, cmd, arg);
324 if (_IOC_DIR(cmd) & _IOC_READ)
325 ret = put_user(val, argp);
330 static const struct file_operations lirc_fops = {
331 .owner = THIS_MODULE,
332 .write = ir_lirc_transmit_ir,
333 .unlocked_ioctl = ir_lirc_ioctl,
335 .compat_ioctl = ir_lirc_ioctl,
337 .read = lirc_dev_fop_read,
338 .poll = lirc_dev_fop_poll,
339 .open = lirc_dev_fop_open,
340 .release = lirc_dev_fop_close,
344 static int ir_lirc_register(struct rc_dev *dev)
346 struct lirc_driver *drv;
348 unsigned long features = 0;
350 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
354 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
355 features |= LIRC_CAN_REC_MODE2;
356 if (dev->rx_resolution)
357 features |= LIRC_CAN_GET_REC_RESOLUTION;
361 features |= LIRC_CAN_SEND_PULSE;
363 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
364 if (dev->s_tx_carrier)
365 features |= LIRC_CAN_SET_SEND_CARRIER;
366 if (dev->s_tx_duty_cycle)
367 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
370 if (dev->s_rx_carrier_range)
371 features |= LIRC_CAN_SET_REC_CARRIER |
372 LIRC_CAN_SET_REC_CARRIER_RANGE;
374 if (dev->s_learning_mode)
375 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
377 if (dev->s_carrier_report)
378 features |= LIRC_CAN_MEASURE_CARRIER;
380 if (dev->max_timeout)
381 features |= LIRC_CAN_SET_REC_TIMEOUT;
383 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
386 drv->features = features;
387 drv->data = &dev->raw->lirc;
389 drv->code_length = sizeof(struct ir_raw_event) * 8;
390 drv->chunk_size = sizeof(int);
391 drv->buffer_size = LIRCBUF_SIZE;
392 drv->fops = &lirc_fops;
393 drv->dev = &dev->dev;
395 drv->owner = THIS_MODULE;
397 drv->minor = lirc_register_driver(drv);
398 if (drv->minor < 0) {
403 dev->raw->lirc.drv = drv;
404 dev->raw->lirc.dev = dev;
412 static int ir_lirc_unregister(struct rc_dev *dev)
414 struct lirc_codec *lirc = &dev->raw->lirc;
416 lirc_unregister_driver(lirc->drv->minor);
423 static struct ir_raw_handler lirc_handler = {
425 .decode = ir_lirc_decode,
426 .raw_register = ir_lirc_register,
427 .raw_unregister = ir_lirc_unregister,
430 static int __init ir_lirc_codec_init(void)
432 ir_raw_handler_register(&lirc_handler);
434 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
438 static void __exit ir_lirc_codec_exit(void)
440 ir_raw_handler_unregister(&lirc_handler);
443 module_init(ir_lirc_codec_init);
444 module_exit(ir_lirc_codec_exit);
446 MODULE_LICENSE("GPL");
447 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
448 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
449 MODULE_DESCRIPTION("LIRC IR handler bridge");