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);
35 struct lirc_codec *lirc = &ir_dev->raw->lirc;
38 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC))
41 if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf)
49 if (ev.carrier_report) {
50 sample = LIRC_FREQUENCY(ev.carrier);
53 } else if (ev.timeout) {
58 lirc->gap_start = ktime_get();
60 lirc->gap_duration = ev.duration;
62 if (!lirc->send_timeout_reports)
65 sample = LIRC_TIMEOUT(ev.duration / 1000);
73 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
76 /* Convert to ms and cap by LIRC_VALUE_MASK */
77 do_div(lirc->gap_duration, 1000);
78 lirc->gap_duration = min(lirc->gap_duration,
79 (u64)LIRC_VALUE_MASK);
81 gap_sample = LIRC_SPACE(lirc->gap_duration);
82 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
83 (unsigned char *) &gap_sample);
87 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
88 LIRC_SPACE(ev.duration / 1000);
91 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
92 (unsigned char *) &sample);
93 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
98 static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
99 size_t n, loff_t *ppos)
101 struct lirc_codec *lirc;
102 struct ir_input_dev *ir_dev;
103 int *txbuf; /* buffer with values to transmit */
106 lirc = lirc_get_pdata(file);
113 count = n / sizeof(int);
114 if (count > LIRCBUF_SIZE || count % 2 == 0)
117 txbuf = memdup_user(buf, n);
119 return PTR_ERR(txbuf);
121 ir_dev = lirc->ir_dev;
127 if (ir_dev->props && ir_dev->props->tx_ir)
128 ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n);
135 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
136 unsigned long __user arg)
138 struct lirc_codec *lirc;
139 struct ir_input_dev *ir_dev;
144 lirc = lirc_get_pdata(filep);
148 ir_dev = lirc->ir_dev;
149 if (!ir_dev || !ir_dev->props || !ir_dev->props->priv)
152 drv_data = ir_dev->props->priv;
154 if (_IOC_DIR(cmd) & _IOC_WRITE) {
155 ret = get_user(val, (__u32 *)arg);
163 case LIRC_GET_SEND_MODE:
164 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
167 case LIRC_SET_SEND_MODE:
168 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
173 case LIRC_SET_TRANSMITTER_MASK:
174 if (!ir_dev->props->s_tx_mask)
177 return ir_dev->props->s_tx_mask(drv_data, val);
179 case LIRC_SET_SEND_CARRIER:
180 if (!ir_dev->props->s_tx_carrier)
183 return ir_dev->props->s_tx_carrier(drv_data, val);
185 case LIRC_SET_SEND_DUTY_CYCLE:
186 if (!ir_dev->props->s_tx_duty_cycle)
189 if (val <= 0 || val >= 100)
192 return ir_dev->props->s_tx_duty_cycle(drv_data, val);
195 case LIRC_SET_REC_CARRIER:
196 if (!ir_dev->props->s_rx_carrier_range)
202 return ir_dev->props->s_rx_carrier_range(drv_data,
203 ir_dev->raw->lirc.carrier_low, val);
205 case LIRC_SET_REC_CARRIER_RANGE:
209 ir_dev->raw->lirc.carrier_low = val;
212 case LIRC_GET_REC_RESOLUTION:
213 val = ir_dev->props->rx_resolution;
216 case LIRC_SET_WIDEBAND_RECEIVER:
217 if (!ir_dev->props->s_learning_mode)
220 return ir_dev->props->s_learning_mode(drv_data, !!val);
222 case LIRC_SET_MEASURE_CARRIER_MODE:
223 if (!ir_dev->props->s_carrier_report)
226 return ir_dev->props->s_carrier_report(drv_data, !!val);
228 /* Generic timeout support */
229 case LIRC_GET_MIN_TIMEOUT:
230 if (!ir_dev->props->max_timeout)
232 val = ir_dev->props->min_timeout / 1000;
235 case LIRC_GET_MAX_TIMEOUT:
236 if (!ir_dev->props->max_timeout)
238 val = ir_dev->props->max_timeout / 1000;
241 case LIRC_SET_REC_TIMEOUT:
242 if (!ir_dev->props->max_timeout)
247 if (tmp < ir_dev->props->min_timeout ||
248 tmp > ir_dev->props->max_timeout)
251 ir_dev->props->timeout = tmp;
254 case LIRC_SET_REC_TIMEOUT_REPORTS:
255 lirc->send_timeout_reports = !!val;
259 return lirc_dev_fop_ioctl(filep, cmd, arg);
262 if (_IOC_DIR(cmd) & _IOC_READ)
263 ret = put_user(val, (__u32 *)arg);
268 static int ir_lirc_open(void *data)
273 static void ir_lirc_close(void *data)
278 static struct file_operations lirc_fops = {
279 .owner = THIS_MODULE,
280 .write = ir_lirc_transmit_ir,
281 .unlocked_ioctl = ir_lirc_ioctl,
283 .compat_ioctl = ir_lirc_ioctl,
285 .read = lirc_dev_fop_read,
286 .poll = lirc_dev_fop_poll,
287 .open = lirc_dev_fop_open,
288 .release = lirc_dev_fop_close,
292 static int ir_lirc_register(struct input_dev *input_dev)
294 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
295 struct lirc_driver *drv;
296 struct lirc_buffer *rbuf;
298 unsigned long features;
300 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
304 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
306 goto rbuf_alloc_failed;
308 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
310 goto rbuf_init_failed;
312 features = LIRC_CAN_REC_MODE2;
313 if (ir_dev->props->tx_ir) {
315 features |= LIRC_CAN_SEND_PULSE;
316 if (ir_dev->props->s_tx_mask)
317 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
318 if (ir_dev->props->s_tx_carrier)
319 features |= LIRC_CAN_SET_SEND_CARRIER;
321 if (ir_dev->props->s_tx_duty_cycle)
322 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
325 if (ir_dev->props->s_rx_carrier_range)
326 features |= LIRC_CAN_SET_REC_CARRIER |
327 LIRC_CAN_SET_REC_CARRIER_RANGE;
329 if (ir_dev->props->s_learning_mode)
330 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
332 if (ir_dev->props->s_carrier_report)
333 features |= LIRC_CAN_MEASURE_CARRIER;
336 if (ir_dev->props->max_timeout)
337 features |= LIRC_CAN_SET_REC_TIMEOUT;
340 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
341 ir_dev->driver_name);
343 drv->features = features;
344 drv->data = &ir_dev->raw->lirc;
346 drv->set_use_inc = &ir_lirc_open;
347 drv->set_use_dec = &ir_lirc_close;
348 drv->code_length = sizeof(struct ir_raw_event) * 8;
349 drv->fops = &lirc_fops;
350 drv->dev = &ir_dev->dev;
351 drv->owner = THIS_MODULE;
353 drv->minor = lirc_register_driver(drv);
354 if (drv->minor < 0) {
356 goto lirc_register_failed;
359 ir_dev->raw->lirc.drv = drv;
360 ir_dev->raw->lirc.ir_dev = ir_dev;
363 lirc_register_failed:
372 static int ir_lirc_unregister(struct input_dev *input_dev)
374 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
375 struct lirc_codec *lirc = &ir_dev->raw->lirc;
377 lirc_unregister_driver(lirc->drv->minor);
378 lirc_buffer_free(lirc->drv->rbuf);
384 static struct ir_raw_handler lirc_handler = {
385 .protocols = IR_TYPE_LIRC,
386 .decode = ir_lirc_decode,
387 .raw_register = ir_lirc_register,
388 .raw_unregister = ir_lirc_unregister,
391 static int __init ir_lirc_codec_init(void)
393 ir_raw_handler_register(&lirc_handler);
395 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
399 static void __exit ir_lirc_codec_exit(void)
401 ir_raw_handler_unregister(&lirc_handler);
404 module_init(ir_lirc_codec_init);
405 module_exit(ir_lirc_codec_exit);
407 MODULE_LICENSE("GPL");
408 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
409 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
410 MODULE_DESCRIPTION("LIRC IR handler bridge");