blob: 165ea8f3fa307f7d26a60a0368925ab2e921a30c [file] [log] [blame]
Jarod Wilson457e2ff2011-01-18 02:33:08 -03001/* ir-lirc-codec.c - rc-core to classic lirc interface bridge
Jarod Wilsonca414692010-07-03 01:07:53 -03002 *
3 * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
4 *
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.
8 *
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.
13 */
14
15#include <linux/sched.h>
16#include <linux/wait.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040017#include <linux/module.h>
Jarod Wilsonca414692010-07-03 01:07:53 -030018#include <media/lirc.h>
Jarod Wilson56900852010-07-16 14:25:33 -030019#include <media/lirc_dev.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030020#include <media/rc-core.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030021#include "rc-core-priv.h"
Jarod Wilsonca414692010-07-03 01:07:53 -030022
23#define LIRCBUF_SIZE 256
24
25/**
26 * ir_lirc_decode() - Send raw IR data to lirc_dev to be relayed to the
27 * lircd userspace daemon for decoding.
David Härdemand8b4b582010-10-29 16:08:23 -030028 * @input_dev: the struct rc_dev descriptor of the device
Jarod Wilsonca414692010-07-03 01:07:53 -030029 * @duration: the struct ir_raw_event descriptor of the pulse/space
30 *
31 * This function returns -EINVAL if the lirc interfaces aren't wired up.
32 */
David Härdemand8b4b582010-10-29 16:08:23 -030033static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev)
Jarod Wilsonca414692010-07-03 01:07:53 -030034{
David Härdemand8b4b582010-10-29 16:08:23 -030035 struct lirc_codec *lirc = &dev->raw->lirc;
Maxim Levitsky510fcb72010-07-31 11:59:15 -030036 int sample;
Jarod Wilsonca414692010-07-03 01:07:53 -030037
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -030038 if (!(dev->raw->enabled_protocols & RC_TYPE_LIRC))
Jarod Wilsonca414692010-07-03 01:07:53 -030039 return 0;
40
David Härdemand8b4b582010-10-29 16:08:23 -030041 if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
Jarod Wilsonca414692010-07-03 01:07:53 -030042 return -EINVAL;
43
Maxim Levitsky46519182010-10-16 19:56:28 -030044 /* Packet start */
45 if (ev.reset)
Maxim Levitsky510fcb72010-07-31 11:59:15 -030046 return 0;
47
Maxim Levitsky46519182010-10-16 19:56:28 -030048 /* Carrier reports */
49 if (ev.carrier_report) {
50 sample = LIRC_FREQUENCY(ev.carrier);
Jarod Wilson457e2ff2011-01-18 02:33:08 -030051 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
Jarod Wilsonca414692010-07-03 01:07:53 -030052
Maxim Levitsky46519182010-10-16 19:56:28 -030053 /* Packet end */
54 } else if (ev.timeout) {
55
56 if (lirc->gap)
57 return 0;
58
59 lirc->gap_start = ktime_get();
60 lirc->gap = true;
61 lirc->gap_duration = ev.duration;
62
63 if (!lirc->send_timeout_reports)
64 return 0;
65
66 sample = LIRC_TIMEOUT(ev.duration / 1000);
Jarod Wilson457e2ff2011-01-18 02:33:08 -030067 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
Maxim Levitsky46519182010-10-16 19:56:28 -030068
69 /* Normal sample */
70 } else {
71
72 if (lirc->gap) {
73 int gap_sample;
74
75 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
76 lirc->gap_start));
77
78 /* Convert to ms and cap by LIRC_VALUE_MASK */
79 do_div(lirc->gap_duration, 1000);
80 lirc->gap_duration = min(lirc->gap_duration,
81 (u64)LIRC_VALUE_MASK);
82
83 gap_sample = LIRC_SPACE(lirc->gap_duration);
David Härdemand8b4b582010-10-29 16:08:23 -030084 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
Maxim Levitsky46519182010-10-16 19:56:28 -030085 (unsigned char *) &gap_sample);
86 lirc->gap = false;
87 }
88
89 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
90 LIRC_SPACE(ev.duration / 1000);
Jarod Wilson457e2ff2011-01-18 02:33:08 -030091 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
92 TO_US(ev.duration), TO_STR(ev.pulse));
Maxim Levitsky46519182010-10-16 19:56:28 -030093 }
Jarod Wilsonca414692010-07-03 01:07:53 -030094
David Härdemand8b4b582010-10-29 16:08:23 -030095 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
Maxim Levitsky510fcb72010-07-31 11:59:15 -030096 (unsigned char *) &sample);
David Härdemand8b4b582010-10-29 16:08:23 -030097 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
Jarod Wilsonca414692010-07-03 01:07:53 -030098
Jarod Wilsonca414692010-07-03 01:07:53 -030099 return 0;
100}
101
102static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
103 size_t n, loff_t *ppos)
104{
105 struct lirc_codec *lirc;
David Härdemand8b4b582010-10-29 16:08:23 -0300106 struct rc_dev *dev;
David Härdeman5588dc22011-04-28 12:13:58 -0300107 unsigned int *txbuf; /* buffer with values to transmit */
108 ssize_t ret = 0;
Vasiliy Kulikov4c8b8692010-11-26 14:06:35 -0300109 size_t count;
Jarod Wilsonca414692010-07-03 01:07:53 -0300110
111 lirc = lirc_get_pdata(file);
112 if (!lirc)
113 return -EFAULT;
114
David Härdeman5588dc22011-04-28 12:13:58 -0300115 if (n < sizeof(unsigned) || n % sizeof(unsigned))
Jarod Wilsonca414692010-07-03 01:07:53 -0300116 return -EINVAL;
117
David Härdeman5588dc22011-04-28 12:13:58 -0300118 count = n / sizeof(unsigned);
119 if (count > LIRCBUF_SIZE || count % 2 == 0)
Jarod Wilsonca414692010-07-03 01:07:53 -0300120 return -EINVAL;
121
Jarod Wilson6efb8702010-07-16 18:29:54 -0300122 txbuf = memdup_user(buf, n);
123 if (IS_ERR(txbuf))
124 return PTR_ERR(txbuf);
Jarod Wilsonca414692010-07-03 01:07:53 -0300125
David Härdemand8b4b582010-10-29 16:08:23 -0300126 dev = lirc->dev;
127 if (!dev) {
Jarod Wilsonca414692010-07-03 01:07:53 -0300128 ret = -EFAULT;
129 goto out;
130 }
131
David Härdemand8b4b582010-10-29 16:08:23 -0300132 if (dev->tx_ir)
David Härdeman5588dc22011-04-28 12:13:58 -0300133 ret = dev->tx_ir(dev, txbuf, count);
134
135 if (ret > 0)
136 ret *= sizeof(unsigned);
Jarod Wilsonca414692010-07-03 01:07:53 -0300137
138out:
139 kfree(txbuf);
140 return ret;
141}
142
Maxim Levitskye5893332010-07-31 11:59:23 -0300143static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
144 unsigned long __user arg)
Jarod Wilsonca414692010-07-03 01:07:53 -0300145{
146 struct lirc_codec *lirc;
David Härdemand8b4b582010-10-29 16:08:23 -0300147 struct rc_dev *dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300148 int ret = 0;
Maxim Levitsky46519182010-10-16 19:56:28 -0300149 __u32 val = 0, tmp;
Jarod Wilsonca414692010-07-03 01:07:53 -0300150
151 lirc = lirc_get_pdata(filep);
152 if (!lirc)
153 return -EFAULT;
154
David Härdemand8b4b582010-10-29 16:08:23 -0300155 dev = lirc->dev;
156 if (!dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300157 return -EFAULT;
158
Maxim Levitskye5893332010-07-31 11:59:23 -0300159 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300160 ret = get_user(val, (__u32 *)arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300161 if (ret)
162 return ret;
Maxim Levitskye5893332010-07-31 11:59:23 -0300163 }
Jarod Wilsonca414692010-07-03 01:07:53 -0300164
Maxim Levitskye5893332010-07-31 11:59:23 -0300165 switch (cmd) {
166
167 /* legacy support */
168 case LIRC_GET_SEND_MODE:
169 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
170 break;
171
172 case LIRC_SET_SEND_MODE:
173 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
174 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300175 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300176
177 /* TX settings */
178 case LIRC_SET_TRANSMITTER_MASK:
David Härdemand8b4b582010-10-29 16:08:23 -0300179 if (!dev->s_tx_mask)
Jarod Wilsonca414692010-07-03 01:07:53 -0300180 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300181
David Härdemand8b4b582010-10-29 16:08:23 -0300182 return dev->s_tx_mask(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300183
184 case LIRC_SET_SEND_CARRIER:
David Härdemand8b4b582010-10-29 16:08:23 -0300185 if (!dev->s_tx_carrier)
Jarod Wilsonca414692010-07-03 01:07:53 -0300186 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300187
David Härdemand8b4b582010-10-29 16:08:23 -0300188 return dev->s_tx_carrier(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300189
Maxim Levitskye5893332010-07-31 11:59:23 -0300190 case LIRC_SET_SEND_DUTY_CYCLE:
David Härdemand8b4b582010-10-29 16:08:23 -0300191 if (!dev->s_tx_duty_cycle)
Maxim Levitskye5893332010-07-31 11:59:23 -0300192 return -ENOSYS;
193
194 if (val <= 0 || val >= 100)
195 return -EINVAL;
196
David Härdemand8b4b582010-10-29 16:08:23 -0300197 return dev->s_tx_duty_cycle(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300198
Maxim Levitskye5893332010-07-31 11:59:23 -0300199 /* RX settings */
200 case LIRC_SET_REC_CARRIER:
David Härdemand8b4b582010-10-29 16:08:23 -0300201 if (!dev->s_rx_carrier_range)
Maxim Levitskye5893332010-07-31 11:59:23 -0300202 return -ENOSYS;
Jarod Wilsonca414692010-07-03 01:07:53 -0300203
Maxim Levitsky46519182010-10-16 19:56:28 -0300204 if (val <= 0)
205 return -EINVAL;
206
David Härdemand8b4b582010-10-29 16:08:23 -0300207 return dev->s_rx_carrier_range(dev,
208 dev->raw->lirc.carrier_low,
209 val);
Maxim Levitskye5893332010-07-31 11:59:23 -0300210
211 case LIRC_SET_REC_CARRIER_RANGE:
Maxim Levitsky46519182010-10-16 19:56:28 -0300212 if (val <= 0)
213 return -EINVAL;
Maxim Levitskye5893332010-07-31 11:59:23 -0300214
David Härdemand8b4b582010-10-29 16:08:23 -0300215 dev->raw->lirc.carrier_low = val;
Maxim Levitsky46519182010-10-16 19:56:28 -0300216 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300217
218 case LIRC_GET_REC_RESOLUTION:
David Härdemand8b4b582010-10-29 16:08:23 -0300219 val = dev->rx_resolution;
Maxim Levitskye5893332010-07-31 11:59:23 -0300220 break;
221
222 case LIRC_SET_WIDEBAND_RECEIVER:
David Härdemand8b4b582010-10-29 16:08:23 -0300223 if (!dev->s_learning_mode)
Maxim Levitskye5893332010-07-31 11:59:23 -0300224 return -ENOSYS;
225
David Härdemand8b4b582010-10-29 16:08:23 -0300226 return dev->s_learning_mode(dev, !!val);
Maxim Levitsky46519182010-10-16 19:56:28 -0300227
228 case LIRC_SET_MEASURE_CARRIER_MODE:
David Härdemand8b4b582010-10-29 16:08:23 -0300229 if (!dev->s_carrier_report)
Maxim Levitsky46519182010-10-16 19:56:28 -0300230 return -ENOSYS;
231
David Härdemand8b4b582010-10-29 16:08:23 -0300232 return dev->s_carrier_report(dev, !!val);
Maxim Levitsky46519182010-10-16 19:56:28 -0300233
Maxim Levitskye5893332010-07-31 11:59:23 -0300234 /* Generic timeout support */
235 case LIRC_GET_MIN_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300236 if (!dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300237 return -ENOSYS;
David Härdemand8b4b582010-10-29 16:08:23 -0300238 val = dev->min_timeout / 1000;
Maxim Levitskye5893332010-07-31 11:59:23 -0300239 break;
240
241 case LIRC_GET_MAX_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300242 if (!dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300243 return -ENOSYS;
David Härdemand8b4b582010-10-29 16:08:23 -0300244 val = dev->max_timeout / 1000;
Maxim Levitskye5893332010-07-31 11:59:23 -0300245 break;
246
247 case LIRC_SET_REC_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300248 if (!dev->max_timeout)
Maxim Levitsky46519182010-10-16 19:56:28 -0300249 return -ENOSYS;
250
251 tmp = val * 1000;
252
David Härdemand8b4b582010-10-29 16:08:23 -0300253 if (tmp < dev->min_timeout ||
254 tmp > dev->max_timeout)
Maxim Levitsky46519182010-10-16 19:56:28 -0300255 return -EINVAL;
256
David Härdemand8b4b582010-10-29 16:08:23 -0300257 dev->timeout = tmp;
Maxim Levitsky46519182010-10-16 19:56:28 -0300258 break;
259
260 case LIRC_SET_REC_TIMEOUT_REPORTS:
261 lirc->send_timeout_reports = !!val;
Jarod Wilsonca414692010-07-03 01:07:53 -0300262 break;
263
264 default:
Arnd Bergmann044e5872010-08-02 15:43:35 -0300265 return lirc_dev_fop_ioctl(filep, cmd, arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300266 }
267
Maxim Levitskye5893332010-07-31 11:59:23 -0300268 if (_IOC_DIR(cmd) & _IOC_READ)
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300269 ret = put_user(val, (__u32 *)arg);
Maxim Levitskye5893332010-07-31 11:59:23 -0300270
Jarod Wilsonca414692010-07-03 01:07:53 -0300271 return ret;
272}
273
274static int ir_lirc_open(void *data)
275{
276 return 0;
277}
278
279static void ir_lirc_close(void *data)
280{
281 return;
282}
283
284static struct file_operations lirc_fops = {
285 .owner = THIS_MODULE,
286 .write = ir_lirc_transmit_ir,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300287 .unlocked_ioctl = ir_lirc_ioctl,
Jarod Wilson8be292c2010-10-09 15:07:06 -0300288#ifdef CONFIG_COMPAT
289 .compat_ioctl = ir_lirc_ioctl,
290#endif
Jarod Wilsonca414692010-07-03 01:07:53 -0300291 .read = lirc_dev_fop_read,
292 .poll = lirc_dev_fop_poll,
293 .open = lirc_dev_fop_open,
294 .release = lirc_dev_fop_close,
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200295 .llseek = no_llseek,
Jarod Wilsonca414692010-07-03 01:07:53 -0300296};
297
David Härdemand8b4b582010-10-29 16:08:23 -0300298static int ir_lirc_register(struct rc_dev *dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300299{
Jarod Wilsonca414692010-07-03 01:07:53 -0300300 struct lirc_driver *drv;
301 struct lirc_buffer *rbuf;
302 int rc = -ENOMEM;
303 unsigned long features;
304
305 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
306 if (!drv)
307 return rc;
308
309 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
Dan Carpenter49b7a122010-07-23 07:08:26 -0300310 if (!rbuf)
Jarod Wilsonca414692010-07-03 01:07:53 -0300311 goto rbuf_alloc_failed;
312
313 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
314 if (rc)
315 goto rbuf_init_failed;
316
317 features = LIRC_CAN_REC_MODE2;
David Härdemand8b4b582010-10-29 16:08:23 -0300318 if (dev->tx_ir) {
Jarod Wilsonca414692010-07-03 01:07:53 -0300319 features |= LIRC_CAN_SEND_PULSE;
David Härdemand8b4b582010-10-29 16:08:23 -0300320 if (dev->s_tx_mask)
Jarod Wilsonca414692010-07-03 01:07:53 -0300321 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
David Härdemand8b4b582010-10-29 16:08:23 -0300322 if (dev->s_tx_carrier)
Jarod Wilsonca414692010-07-03 01:07:53 -0300323 features |= LIRC_CAN_SET_SEND_CARRIER;
David Härdemand8b4b582010-10-29 16:08:23 -0300324 if (dev->s_tx_duty_cycle)
Maxim Levitsky67332ba2010-09-06 18:26:08 -0300325 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
Jarod Wilsonca414692010-07-03 01:07:53 -0300326 }
327
David Härdemand8b4b582010-10-29 16:08:23 -0300328 if (dev->s_rx_carrier_range)
Maxim Levitskye5893332010-07-31 11:59:23 -0300329 features |= LIRC_CAN_SET_REC_CARRIER |
330 LIRC_CAN_SET_REC_CARRIER_RANGE;
331
David Härdemand8b4b582010-10-29 16:08:23 -0300332 if (dev->s_learning_mode)
Maxim Levitskye5893332010-07-31 11:59:23 -0300333 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
334
David Härdemand8b4b582010-10-29 16:08:23 -0300335 if (dev->s_carrier_report)
Maxim Levitsky46519182010-10-16 19:56:28 -0300336 features |= LIRC_CAN_MEASURE_CARRIER;
337
David Härdemand8b4b582010-10-29 16:08:23 -0300338 if (dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300339 features |= LIRC_CAN_SET_REC_TIMEOUT;
340
Jarod Wilsonca414692010-07-03 01:07:53 -0300341 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
David Härdemand8b4b582010-10-29 16:08:23 -0300342 dev->driver_name);
Jarod Wilsonca414692010-07-03 01:07:53 -0300343 drv->minor = -1;
344 drv->features = features;
David Härdemand8b4b582010-10-29 16:08:23 -0300345 drv->data = &dev->raw->lirc;
Jarod Wilsonca414692010-07-03 01:07:53 -0300346 drv->rbuf = rbuf;
347 drv->set_use_inc = &ir_lirc_open;
348 drv->set_use_dec = &ir_lirc_close;
349 drv->code_length = sizeof(struct ir_raw_event) * 8;
350 drv->fops = &lirc_fops;
David Härdemand8b4b582010-10-29 16:08:23 -0300351 drv->dev = &dev->dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300352 drv->owner = THIS_MODULE;
353
354 drv->minor = lirc_register_driver(drv);
355 if (drv->minor < 0) {
356 rc = -ENODEV;
357 goto lirc_register_failed;
358 }
359
David Härdemand8b4b582010-10-29 16:08:23 -0300360 dev->raw->lirc.drv = drv;
361 dev->raw->lirc.dev = dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300362 return 0;
363
364lirc_register_failed:
365rbuf_init_failed:
366 kfree(rbuf);
367rbuf_alloc_failed:
368 kfree(drv);
369
370 return rc;
371}
372
David Härdemand8b4b582010-10-29 16:08:23 -0300373static int ir_lirc_unregister(struct rc_dev *dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300374{
David Härdemand8b4b582010-10-29 16:08:23 -0300375 struct lirc_codec *lirc = &dev->raw->lirc;
Jarod Wilsonca414692010-07-03 01:07:53 -0300376
377 lirc_unregister_driver(lirc->drv->minor);
378 lirc_buffer_free(lirc->drv->rbuf);
379 kfree(lirc->drv);
380
381 return 0;
382}
383
384static struct ir_raw_handler lirc_handler = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300385 .protocols = RC_TYPE_LIRC,
Jarod Wilsonca414692010-07-03 01:07:53 -0300386 .decode = ir_lirc_decode,
387 .raw_register = ir_lirc_register,
388 .raw_unregister = ir_lirc_unregister,
389};
390
391static int __init ir_lirc_codec_init(void)
392{
393 ir_raw_handler_register(&lirc_handler);
394
395 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
396 return 0;
397}
398
399static void __exit ir_lirc_codec_exit(void)
400{
401 ir_raw_handler_unregister(&lirc_handler);
402}
403
404module_init(ir_lirc_codec_init);
405module_exit(ir_lirc_codec_exit);
406
407MODULE_LICENSE("GPL");
408MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
409MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
410MODULE_DESCRIPTION("LIRC IR handler bridge");