blob: e5eeec4da76ea776972cc202ac4367605dd999bb [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>
17#include <media/lirc.h>
Jarod Wilson56900852010-07-16 14:25:33 -030018#include <media/lirc_dev.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030019#include <media/rc-core.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030020#include "rc-core-priv.h"
Jarod Wilsonca414692010-07-03 01:07:53 -030021
22#define LIRCBUF_SIZE 256
23
24/**
25 * ir_lirc_decode() - Send raw IR data to lirc_dev to be relayed to the
26 * lircd userspace daemon for decoding.
David Härdemand8b4b582010-10-29 16:08:23 -030027 * @input_dev: the struct rc_dev descriptor of the device
Jarod Wilsonca414692010-07-03 01:07:53 -030028 * @duration: the struct ir_raw_event descriptor of the pulse/space
29 *
30 * This function returns -EINVAL if the lirc interfaces aren't wired up.
31 */
David Härdemand8b4b582010-10-29 16:08:23 -030032static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev)
Jarod Wilsonca414692010-07-03 01:07:53 -030033{
David Härdemand8b4b582010-10-29 16:08:23 -030034 struct lirc_codec *lirc = &dev->raw->lirc;
Maxim Levitsky510fcb72010-07-31 11:59:15 -030035 int sample;
Jarod Wilsonca414692010-07-03 01:07:53 -030036
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -030037 if (!(dev->raw->enabled_protocols & RC_TYPE_LIRC))
Jarod Wilsonca414692010-07-03 01:07:53 -030038 return 0;
39
David Härdemand8b4b582010-10-29 16:08:23 -030040 if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
Jarod Wilsonca414692010-07-03 01:07:53 -030041 return -EINVAL;
42
Maxim Levitsky46519182010-10-16 19:56:28 -030043 /* Packet start */
44 if (ev.reset)
Maxim Levitsky510fcb72010-07-31 11:59:15 -030045 return 0;
46
Maxim Levitsky46519182010-10-16 19:56:28 -030047 /* Carrier reports */
48 if (ev.carrier_report) {
49 sample = LIRC_FREQUENCY(ev.carrier);
Jarod Wilson457e2ff2011-01-18 02:33:08 -030050 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
Jarod Wilsonca414692010-07-03 01:07:53 -030051
Maxim Levitsky46519182010-10-16 19:56:28 -030052 /* Packet end */
53 } else if (ev.timeout) {
54
55 if (lirc->gap)
56 return 0;
57
58 lirc->gap_start = ktime_get();
59 lirc->gap = true;
60 lirc->gap_duration = ev.duration;
61
62 if (!lirc->send_timeout_reports)
63 return 0;
64
65 sample = LIRC_TIMEOUT(ev.duration / 1000);
Jarod Wilson457e2ff2011-01-18 02:33:08 -030066 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
Maxim Levitsky46519182010-10-16 19:56:28 -030067
68 /* Normal sample */
69 } else {
70
71 if (lirc->gap) {
72 int gap_sample;
73
74 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
75 lirc->gap_start));
76
77 /* Convert to ms and cap by LIRC_VALUE_MASK */
78 do_div(lirc->gap_duration, 1000);
79 lirc->gap_duration = min(lirc->gap_duration,
80 (u64)LIRC_VALUE_MASK);
81
82 gap_sample = LIRC_SPACE(lirc->gap_duration);
David Härdemand8b4b582010-10-29 16:08:23 -030083 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
Maxim Levitsky46519182010-10-16 19:56:28 -030084 (unsigned char *) &gap_sample);
85 lirc->gap = false;
86 }
87
88 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
89 LIRC_SPACE(ev.duration / 1000);
Jarod Wilson457e2ff2011-01-18 02:33:08 -030090 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
91 TO_US(ev.duration), TO_STR(ev.pulse));
Maxim Levitsky46519182010-10-16 19:56:28 -030092 }
Jarod Wilsonca414692010-07-03 01:07:53 -030093
David Härdemand8b4b582010-10-29 16:08:23 -030094 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
Maxim Levitsky510fcb72010-07-31 11:59:15 -030095 (unsigned char *) &sample);
David Härdemand8b4b582010-10-29 16:08:23 -030096 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
Jarod Wilsonca414692010-07-03 01:07:53 -030097
Jarod Wilsonca414692010-07-03 01:07:53 -030098 return 0;
99}
100
101static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
102 size_t n, loff_t *ppos)
103{
104 struct lirc_codec *lirc;
David Härdemand8b4b582010-10-29 16:08:23 -0300105 struct rc_dev *dev;
David Härdeman5588dc22011-04-28 12:13:58 -0300106 unsigned int *txbuf; /* buffer with values to transmit */
107 ssize_t ret = 0;
Vasiliy Kulikov4c8b8692010-11-26 14:06:35 -0300108 size_t count;
Jarod Wilsonca414692010-07-03 01:07:53 -0300109
110 lirc = lirc_get_pdata(file);
111 if (!lirc)
112 return -EFAULT;
113
David Härdeman5588dc22011-04-28 12:13:58 -0300114 if (n < sizeof(unsigned) || n % sizeof(unsigned))
Jarod Wilsonca414692010-07-03 01:07:53 -0300115 return -EINVAL;
116
David Härdeman5588dc22011-04-28 12:13:58 -0300117 count = n / sizeof(unsigned);
118 if (count > LIRCBUF_SIZE || count % 2 == 0)
Jarod Wilsonca414692010-07-03 01:07:53 -0300119 return -EINVAL;
120
Jarod Wilson6efb8702010-07-16 18:29:54 -0300121 txbuf = memdup_user(buf, n);
122 if (IS_ERR(txbuf))
123 return PTR_ERR(txbuf);
Jarod Wilsonca414692010-07-03 01:07:53 -0300124
David Härdemand8b4b582010-10-29 16:08:23 -0300125 dev = lirc->dev;
126 if (!dev) {
Jarod Wilsonca414692010-07-03 01:07:53 -0300127 ret = -EFAULT;
128 goto out;
129 }
130
David Härdemand8b4b582010-10-29 16:08:23 -0300131 if (dev->tx_ir)
David Härdeman5588dc22011-04-28 12:13:58 -0300132 ret = dev->tx_ir(dev, txbuf, count);
133
134 if (ret > 0)
135 ret *= sizeof(unsigned);
Jarod Wilsonca414692010-07-03 01:07:53 -0300136
137out:
138 kfree(txbuf);
139 return ret;
140}
141
Maxim Levitskye5893332010-07-31 11:59:23 -0300142static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
143 unsigned long __user arg)
Jarod Wilsonca414692010-07-03 01:07:53 -0300144{
145 struct lirc_codec *lirc;
David Härdemand8b4b582010-10-29 16:08:23 -0300146 struct rc_dev *dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300147 int ret = 0;
Maxim Levitsky46519182010-10-16 19:56:28 -0300148 __u32 val = 0, tmp;
Jarod Wilsonca414692010-07-03 01:07:53 -0300149
150 lirc = lirc_get_pdata(filep);
151 if (!lirc)
152 return -EFAULT;
153
David Härdemand8b4b582010-10-29 16:08:23 -0300154 dev = lirc->dev;
155 if (!dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300156 return -EFAULT;
157
Maxim Levitskye5893332010-07-31 11:59:23 -0300158 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300159 ret = get_user(val, (__u32 *)arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300160 if (ret)
161 return ret;
Maxim Levitskye5893332010-07-31 11:59:23 -0300162 }
Jarod Wilsonca414692010-07-03 01:07:53 -0300163
Maxim Levitskye5893332010-07-31 11:59:23 -0300164 switch (cmd) {
165
166 /* legacy support */
167 case LIRC_GET_SEND_MODE:
168 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
169 break;
170
171 case LIRC_SET_SEND_MODE:
172 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
173 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300174 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300175
176 /* TX settings */
177 case LIRC_SET_TRANSMITTER_MASK:
David Härdemand8b4b582010-10-29 16:08:23 -0300178 if (!dev->s_tx_mask)
Jarod Wilsonca414692010-07-03 01:07:53 -0300179 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300180
David Härdemand8b4b582010-10-29 16:08:23 -0300181 return dev->s_tx_mask(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300182
183 case LIRC_SET_SEND_CARRIER:
David Härdemand8b4b582010-10-29 16:08:23 -0300184 if (!dev->s_tx_carrier)
Jarod Wilsonca414692010-07-03 01:07:53 -0300185 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300186
David Härdemand8b4b582010-10-29 16:08:23 -0300187 return dev->s_tx_carrier(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300188
Maxim Levitskye5893332010-07-31 11:59:23 -0300189 case LIRC_SET_SEND_DUTY_CYCLE:
David Härdemand8b4b582010-10-29 16:08:23 -0300190 if (!dev->s_tx_duty_cycle)
Maxim Levitskye5893332010-07-31 11:59:23 -0300191 return -ENOSYS;
192
193 if (val <= 0 || val >= 100)
194 return -EINVAL;
195
David Härdemand8b4b582010-10-29 16:08:23 -0300196 return dev->s_tx_duty_cycle(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300197
Maxim Levitskye5893332010-07-31 11:59:23 -0300198 /* RX settings */
199 case LIRC_SET_REC_CARRIER:
David Härdemand8b4b582010-10-29 16:08:23 -0300200 if (!dev->s_rx_carrier_range)
Maxim Levitskye5893332010-07-31 11:59:23 -0300201 return -ENOSYS;
Jarod Wilsonca414692010-07-03 01:07:53 -0300202
Maxim Levitsky46519182010-10-16 19:56:28 -0300203 if (val <= 0)
204 return -EINVAL;
205
David Härdemand8b4b582010-10-29 16:08:23 -0300206 return dev->s_rx_carrier_range(dev,
207 dev->raw->lirc.carrier_low,
208 val);
Maxim Levitskye5893332010-07-31 11:59:23 -0300209
210 case LIRC_SET_REC_CARRIER_RANGE:
Maxim Levitsky46519182010-10-16 19:56:28 -0300211 if (val <= 0)
212 return -EINVAL;
Maxim Levitskye5893332010-07-31 11:59:23 -0300213
David Härdemand8b4b582010-10-29 16:08:23 -0300214 dev->raw->lirc.carrier_low = val;
Maxim Levitsky46519182010-10-16 19:56:28 -0300215 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300216
217 case LIRC_GET_REC_RESOLUTION:
David Härdemand8b4b582010-10-29 16:08:23 -0300218 val = dev->rx_resolution;
Maxim Levitskye5893332010-07-31 11:59:23 -0300219 break;
220
221 case LIRC_SET_WIDEBAND_RECEIVER:
David Härdemand8b4b582010-10-29 16:08:23 -0300222 if (!dev->s_learning_mode)
Maxim Levitskye5893332010-07-31 11:59:23 -0300223 return -ENOSYS;
224
David Härdemand8b4b582010-10-29 16:08:23 -0300225 return dev->s_learning_mode(dev, !!val);
Maxim Levitsky46519182010-10-16 19:56:28 -0300226
227 case LIRC_SET_MEASURE_CARRIER_MODE:
David Härdemand8b4b582010-10-29 16:08:23 -0300228 if (!dev->s_carrier_report)
Maxim Levitsky46519182010-10-16 19:56:28 -0300229 return -ENOSYS;
230
David Härdemand8b4b582010-10-29 16:08:23 -0300231 return dev->s_carrier_report(dev, !!val);
Maxim Levitsky46519182010-10-16 19:56:28 -0300232
Maxim Levitskye5893332010-07-31 11:59:23 -0300233 /* Generic timeout support */
234 case LIRC_GET_MIN_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300235 if (!dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300236 return -ENOSYS;
David Härdemand8b4b582010-10-29 16:08:23 -0300237 val = dev->min_timeout / 1000;
Maxim Levitskye5893332010-07-31 11:59:23 -0300238 break;
239
240 case LIRC_GET_MAX_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300241 if (!dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300242 return -ENOSYS;
David Härdemand8b4b582010-10-29 16:08:23 -0300243 val = dev->max_timeout / 1000;
Maxim Levitskye5893332010-07-31 11:59:23 -0300244 break;
245
246 case LIRC_SET_REC_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300247 if (!dev->max_timeout)
Maxim Levitsky46519182010-10-16 19:56:28 -0300248 return -ENOSYS;
249
250 tmp = val * 1000;
251
David Härdemand8b4b582010-10-29 16:08:23 -0300252 if (tmp < dev->min_timeout ||
253 tmp > dev->max_timeout)
Maxim Levitsky46519182010-10-16 19:56:28 -0300254 return -EINVAL;
255
David Härdemand8b4b582010-10-29 16:08:23 -0300256 dev->timeout = tmp;
Maxim Levitsky46519182010-10-16 19:56:28 -0300257 break;
258
259 case LIRC_SET_REC_TIMEOUT_REPORTS:
260 lirc->send_timeout_reports = !!val;
Jarod Wilsonca414692010-07-03 01:07:53 -0300261 break;
262
263 default:
Arnd Bergmann044e5872010-08-02 15:43:35 -0300264 return lirc_dev_fop_ioctl(filep, cmd, arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300265 }
266
Maxim Levitskye5893332010-07-31 11:59:23 -0300267 if (_IOC_DIR(cmd) & _IOC_READ)
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300268 ret = put_user(val, (__u32 *)arg);
Maxim Levitskye5893332010-07-31 11:59:23 -0300269
Jarod Wilsonca414692010-07-03 01:07:53 -0300270 return ret;
271}
272
273static int ir_lirc_open(void *data)
274{
275 return 0;
276}
277
278static void ir_lirc_close(void *data)
279{
280 return;
281}
282
283static struct file_operations lirc_fops = {
284 .owner = THIS_MODULE,
285 .write = ir_lirc_transmit_ir,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300286 .unlocked_ioctl = ir_lirc_ioctl,
Jarod Wilson8be292c2010-10-09 15:07:06 -0300287#ifdef CONFIG_COMPAT
288 .compat_ioctl = ir_lirc_ioctl,
289#endif
Jarod Wilsonca414692010-07-03 01:07:53 -0300290 .read = lirc_dev_fop_read,
291 .poll = lirc_dev_fop_poll,
292 .open = lirc_dev_fop_open,
293 .release = lirc_dev_fop_close,
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200294 .llseek = no_llseek,
Jarod Wilsonca414692010-07-03 01:07:53 -0300295};
296
David Härdemand8b4b582010-10-29 16:08:23 -0300297static int ir_lirc_register(struct rc_dev *dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300298{
Jarod Wilsonca414692010-07-03 01:07:53 -0300299 struct lirc_driver *drv;
300 struct lirc_buffer *rbuf;
301 int rc = -ENOMEM;
302 unsigned long features;
303
304 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
305 if (!drv)
306 return rc;
307
308 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
Dan Carpenter49b7a122010-07-23 07:08:26 -0300309 if (!rbuf)
Jarod Wilsonca414692010-07-03 01:07:53 -0300310 goto rbuf_alloc_failed;
311
312 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
313 if (rc)
314 goto rbuf_init_failed;
315
316 features = LIRC_CAN_REC_MODE2;
David Härdemand8b4b582010-10-29 16:08:23 -0300317 if (dev->tx_ir) {
Jarod Wilsonca414692010-07-03 01:07:53 -0300318 features |= LIRC_CAN_SEND_PULSE;
David Härdemand8b4b582010-10-29 16:08:23 -0300319 if (dev->s_tx_mask)
Jarod Wilsonca414692010-07-03 01:07:53 -0300320 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
David Härdemand8b4b582010-10-29 16:08:23 -0300321 if (dev->s_tx_carrier)
Jarod Wilsonca414692010-07-03 01:07:53 -0300322 features |= LIRC_CAN_SET_SEND_CARRIER;
David Härdemand8b4b582010-10-29 16:08:23 -0300323 if (dev->s_tx_duty_cycle)
Maxim Levitsky67332ba2010-09-06 18:26:08 -0300324 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
Jarod Wilsonca414692010-07-03 01:07:53 -0300325 }
326
David Härdemand8b4b582010-10-29 16:08:23 -0300327 if (dev->s_rx_carrier_range)
Maxim Levitskye5893332010-07-31 11:59:23 -0300328 features |= LIRC_CAN_SET_REC_CARRIER |
329 LIRC_CAN_SET_REC_CARRIER_RANGE;
330
David Härdemand8b4b582010-10-29 16:08:23 -0300331 if (dev->s_learning_mode)
Maxim Levitskye5893332010-07-31 11:59:23 -0300332 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
333
David Härdemand8b4b582010-10-29 16:08:23 -0300334 if (dev->s_carrier_report)
Maxim Levitsky46519182010-10-16 19:56:28 -0300335 features |= LIRC_CAN_MEASURE_CARRIER;
336
David Härdemand8b4b582010-10-29 16:08:23 -0300337 if (dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300338 features |= LIRC_CAN_SET_REC_TIMEOUT;
339
Jarod Wilsonca414692010-07-03 01:07:53 -0300340 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
David Härdemand8b4b582010-10-29 16:08:23 -0300341 dev->driver_name);
Jarod Wilsonca414692010-07-03 01:07:53 -0300342 drv->minor = -1;
343 drv->features = features;
David Härdemand8b4b582010-10-29 16:08:23 -0300344 drv->data = &dev->raw->lirc;
Jarod Wilsonca414692010-07-03 01:07:53 -0300345 drv->rbuf = rbuf;
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;
David Härdemand8b4b582010-10-29 16:08:23 -0300350 drv->dev = &dev->dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300351 drv->owner = THIS_MODULE;
352
353 drv->minor = lirc_register_driver(drv);
354 if (drv->minor < 0) {
355 rc = -ENODEV;
356 goto lirc_register_failed;
357 }
358
David Härdemand8b4b582010-10-29 16:08:23 -0300359 dev->raw->lirc.drv = drv;
360 dev->raw->lirc.dev = dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300361 return 0;
362
363lirc_register_failed:
364rbuf_init_failed:
365 kfree(rbuf);
366rbuf_alloc_failed:
367 kfree(drv);
368
369 return rc;
370}
371
David Härdemand8b4b582010-10-29 16:08:23 -0300372static int ir_lirc_unregister(struct rc_dev *dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300373{
David Härdemand8b4b582010-10-29 16:08:23 -0300374 struct lirc_codec *lirc = &dev->raw->lirc;
Jarod Wilsonca414692010-07-03 01:07:53 -0300375
376 lirc_unregister_driver(lirc->drv->minor);
377 lirc_buffer_free(lirc->drv->rbuf);
378 kfree(lirc->drv);
379
380 return 0;
381}
382
383static struct ir_raw_handler lirc_handler = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300384 .protocols = RC_TYPE_LIRC,
Jarod Wilsonca414692010-07-03 01:07:53 -0300385 .decode = ir_lirc_decode,
386 .raw_register = ir_lirc_register,
387 .raw_unregister = ir_lirc_unregister,
388};
389
390static int __init ir_lirc_codec_init(void)
391{
392 ir_raw_handler_register(&lirc_handler);
393
394 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
395 return 0;
396}
397
398static void __exit ir_lirc_codec_exit(void)
399{
400 ir_raw_handler_unregister(&lirc_handler);
401}
402
403module_init(ir_lirc_codec_init);
404module_exit(ir_lirc_codec_exit);
405
406MODULE_LICENSE("GPL");
407MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
408MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
409MODULE_DESCRIPTION("LIRC IR handler bridge");