blob: 1e87ee8e5c990ce6d6867cad8e09d18e3a0fdafd [file] [log] [blame]
Jarod Wilsonca414692010-07-03 01:07:53 -03001/* ir-lirc-codec.c - ir-core to classic lirc interface bridge
2 *
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 Wilsonca414692010-07-03 01:07:53 -030050
Maxim Levitsky46519182010-10-16 19:56:28 -030051 /* Packet end */
52 } else if (ev.timeout) {
53
54 if (lirc->gap)
55 return 0;
56
57 lirc->gap_start = ktime_get();
58 lirc->gap = true;
59 lirc->gap_duration = ev.duration;
60
61 if (!lirc->send_timeout_reports)
62 return 0;
63
64 sample = LIRC_TIMEOUT(ev.duration / 1000);
65
66 /* Normal sample */
67 } else {
68
69 if (lirc->gap) {
70 int gap_sample;
71
72 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
73 lirc->gap_start));
74
75 /* Convert to ms and cap by LIRC_VALUE_MASK */
76 do_div(lirc->gap_duration, 1000);
77 lirc->gap_duration = min(lirc->gap_duration,
78 (u64)LIRC_VALUE_MASK);
79
80 gap_sample = LIRC_SPACE(lirc->gap_duration);
David Härdemand8b4b582010-10-29 16:08:23 -030081 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
Maxim Levitsky46519182010-10-16 19:56:28 -030082 (unsigned char *) &gap_sample);
83 lirc->gap = false;
84 }
85
86 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
87 LIRC_SPACE(ev.duration / 1000);
88 }
Jarod Wilsonca414692010-07-03 01:07:53 -030089
David Härdemand8b4b582010-10-29 16:08:23 -030090 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
Maxim Levitsky510fcb72010-07-31 11:59:15 -030091 (unsigned char *) &sample);
David Härdemand8b4b582010-10-29 16:08:23 -030092 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
Jarod Wilsonca414692010-07-03 01:07:53 -030093
Jarod Wilsonca414692010-07-03 01:07:53 -030094 return 0;
95}
96
97static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
98 size_t n, loff_t *ppos)
99{
100 struct lirc_codec *lirc;
David Härdemand8b4b582010-10-29 16:08:23 -0300101 struct rc_dev *dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300102 int *txbuf; /* buffer with values to transmit */
103 int ret = 0, count;
104
105 lirc = lirc_get_pdata(file);
106 if (!lirc)
107 return -EFAULT;
108
109 if (n % sizeof(int))
110 return -EINVAL;
111
112 count = n / sizeof(int);
113 if (count > LIRCBUF_SIZE || count % 2 == 0)
114 return -EINVAL;
115
Jarod Wilson6efb8702010-07-16 18:29:54 -0300116 txbuf = memdup_user(buf, n);
117 if (IS_ERR(txbuf))
118 return PTR_ERR(txbuf);
Jarod Wilsonca414692010-07-03 01:07:53 -0300119
David Härdemand8b4b582010-10-29 16:08:23 -0300120 dev = lirc->dev;
121 if (!dev) {
Jarod Wilsonca414692010-07-03 01:07:53 -0300122 ret = -EFAULT;
123 goto out;
124 }
125
David Härdemand8b4b582010-10-29 16:08:23 -0300126 if (dev->tx_ir)
127 ret = dev->tx_ir(dev, txbuf, (u32)n);
Jarod Wilsonca414692010-07-03 01:07:53 -0300128
129out:
130 kfree(txbuf);
131 return ret;
132}
133
Maxim Levitskye5893332010-07-31 11:59:23 -0300134static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
135 unsigned long __user arg)
Jarod Wilsonca414692010-07-03 01:07:53 -0300136{
137 struct lirc_codec *lirc;
David Härdemand8b4b582010-10-29 16:08:23 -0300138 struct rc_dev *dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300139 int ret = 0;
Maxim Levitsky46519182010-10-16 19:56:28 -0300140 __u32 val = 0, tmp;
Jarod Wilsonca414692010-07-03 01:07:53 -0300141
142 lirc = lirc_get_pdata(filep);
143 if (!lirc)
144 return -EFAULT;
145
David Härdemand8b4b582010-10-29 16:08:23 -0300146 dev = lirc->dev;
147 if (!dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300148 return -EFAULT;
149
Maxim Levitskye5893332010-07-31 11:59:23 -0300150 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300151 ret = get_user(val, (__u32 *)arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300152 if (ret)
153 return ret;
Maxim Levitskye5893332010-07-31 11:59:23 -0300154 }
Jarod Wilsonca414692010-07-03 01:07:53 -0300155
Maxim Levitskye5893332010-07-31 11:59:23 -0300156 switch (cmd) {
157
158 /* legacy support */
159 case LIRC_GET_SEND_MODE:
160 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
161 break;
162
163 case LIRC_SET_SEND_MODE:
164 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
165 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300166 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300167
168 /* TX settings */
169 case LIRC_SET_TRANSMITTER_MASK:
David Härdemand8b4b582010-10-29 16:08:23 -0300170 if (!dev->s_tx_mask)
Jarod Wilsonca414692010-07-03 01:07:53 -0300171 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300172
David Härdemand8b4b582010-10-29 16:08:23 -0300173 return dev->s_tx_mask(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300174
175 case LIRC_SET_SEND_CARRIER:
David Härdemand8b4b582010-10-29 16:08:23 -0300176 if (!dev->s_tx_carrier)
Jarod Wilsonca414692010-07-03 01:07:53 -0300177 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300178
David Härdemand8b4b582010-10-29 16:08:23 -0300179 return dev->s_tx_carrier(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300180
Maxim Levitskye5893332010-07-31 11:59:23 -0300181 case LIRC_SET_SEND_DUTY_CYCLE:
David Härdemand8b4b582010-10-29 16:08:23 -0300182 if (!dev->s_tx_duty_cycle)
Maxim Levitskye5893332010-07-31 11:59:23 -0300183 return -ENOSYS;
184
185 if (val <= 0 || val >= 100)
186 return -EINVAL;
187
David Härdemand8b4b582010-10-29 16:08:23 -0300188 return dev->s_tx_duty_cycle(dev, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300189
Maxim Levitskye5893332010-07-31 11:59:23 -0300190 /* RX settings */
191 case LIRC_SET_REC_CARRIER:
David Härdemand8b4b582010-10-29 16:08:23 -0300192 if (!dev->s_rx_carrier_range)
Maxim Levitskye5893332010-07-31 11:59:23 -0300193 return -ENOSYS;
Jarod Wilsonca414692010-07-03 01:07:53 -0300194
Maxim Levitsky46519182010-10-16 19:56:28 -0300195 if (val <= 0)
196 return -EINVAL;
197
David Härdemand8b4b582010-10-29 16:08:23 -0300198 return dev->s_rx_carrier_range(dev,
199 dev->raw->lirc.carrier_low,
200 val);
Maxim Levitskye5893332010-07-31 11:59:23 -0300201
202 case LIRC_SET_REC_CARRIER_RANGE:
Maxim Levitsky46519182010-10-16 19:56:28 -0300203 if (val <= 0)
204 return -EINVAL;
Maxim Levitskye5893332010-07-31 11:59:23 -0300205
David Härdemand8b4b582010-10-29 16:08:23 -0300206 dev->raw->lirc.carrier_low = val;
Maxim Levitsky46519182010-10-16 19:56:28 -0300207 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300208
209 case LIRC_GET_REC_RESOLUTION:
David Härdemand8b4b582010-10-29 16:08:23 -0300210 val = dev->rx_resolution;
Maxim Levitskye5893332010-07-31 11:59:23 -0300211 break;
212
213 case LIRC_SET_WIDEBAND_RECEIVER:
David Härdemand8b4b582010-10-29 16:08:23 -0300214 if (!dev->s_learning_mode)
Maxim Levitskye5893332010-07-31 11:59:23 -0300215 return -ENOSYS;
216
David Härdemand8b4b582010-10-29 16:08:23 -0300217 return dev->s_learning_mode(dev, !!val);
Maxim Levitsky46519182010-10-16 19:56:28 -0300218
219 case LIRC_SET_MEASURE_CARRIER_MODE:
David Härdemand8b4b582010-10-29 16:08:23 -0300220 if (!dev->s_carrier_report)
Maxim Levitsky46519182010-10-16 19:56:28 -0300221 return -ENOSYS;
222
David Härdemand8b4b582010-10-29 16:08:23 -0300223 return dev->s_carrier_report(dev, !!val);
Maxim Levitsky46519182010-10-16 19:56:28 -0300224
Maxim Levitskye5893332010-07-31 11:59:23 -0300225 /* Generic timeout support */
226 case LIRC_GET_MIN_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300227 if (!dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300228 return -ENOSYS;
David Härdemand8b4b582010-10-29 16:08:23 -0300229 val = dev->min_timeout / 1000;
Maxim Levitskye5893332010-07-31 11:59:23 -0300230 break;
231
232 case LIRC_GET_MAX_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300233 if (!dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300234 return -ENOSYS;
David Härdemand8b4b582010-10-29 16:08:23 -0300235 val = dev->max_timeout / 1000;
Maxim Levitskye5893332010-07-31 11:59:23 -0300236 break;
237
238 case LIRC_SET_REC_TIMEOUT:
David Härdemand8b4b582010-10-29 16:08:23 -0300239 if (!dev->max_timeout)
Maxim Levitsky46519182010-10-16 19:56:28 -0300240 return -ENOSYS;
241
242 tmp = val * 1000;
243
David Härdemand8b4b582010-10-29 16:08:23 -0300244 if (tmp < dev->min_timeout ||
245 tmp > dev->max_timeout)
Maxim Levitsky46519182010-10-16 19:56:28 -0300246 return -EINVAL;
247
David Härdemand8b4b582010-10-29 16:08:23 -0300248 dev->timeout = tmp;
Maxim Levitsky46519182010-10-16 19:56:28 -0300249 break;
250
251 case LIRC_SET_REC_TIMEOUT_REPORTS:
252 lirc->send_timeout_reports = !!val;
Jarod Wilsonca414692010-07-03 01:07:53 -0300253 break;
254
255 default:
Arnd Bergmann044e5872010-08-02 15:43:35 -0300256 return lirc_dev_fop_ioctl(filep, cmd, arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300257 }
258
Maxim Levitskye5893332010-07-31 11:59:23 -0300259 if (_IOC_DIR(cmd) & _IOC_READ)
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300260 ret = put_user(val, (__u32 *)arg);
Maxim Levitskye5893332010-07-31 11:59:23 -0300261
Jarod Wilsonca414692010-07-03 01:07:53 -0300262 return ret;
263}
264
265static int ir_lirc_open(void *data)
266{
267 return 0;
268}
269
270static void ir_lirc_close(void *data)
271{
272 return;
273}
274
275static struct file_operations lirc_fops = {
276 .owner = THIS_MODULE,
277 .write = ir_lirc_transmit_ir,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300278 .unlocked_ioctl = ir_lirc_ioctl,
Jarod Wilson8be292c2010-10-09 15:07:06 -0300279#ifdef CONFIG_COMPAT
280 .compat_ioctl = ir_lirc_ioctl,
281#endif
Jarod Wilsonca414692010-07-03 01:07:53 -0300282 .read = lirc_dev_fop_read,
283 .poll = lirc_dev_fop_poll,
284 .open = lirc_dev_fop_open,
285 .release = lirc_dev_fop_close,
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200286 .llseek = no_llseek,
Jarod Wilsonca414692010-07-03 01:07:53 -0300287};
288
David Härdemand8b4b582010-10-29 16:08:23 -0300289static int ir_lirc_register(struct rc_dev *dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300290{
Jarod Wilsonca414692010-07-03 01:07:53 -0300291 struct lirc_driver *drv;
292 struct lirc_buffer *rbuf;
293 int rc = -ENOMEM;
294 unsigned long features;
295
296 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
297 if (!drv)
298 return rc;
299
300 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
Dan Carpenter49b7a122010-07-23 07:08:26 -0300301 if (!rbuf)
Jarod Wilsonca414692010-07-03 01:07:53 -0300302 goto rbuf_alloc_failed;
303
304 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
305 if (rc)
306 goto rbuf_init_failed;
307
308 features = LIRC_CAN_REC_MODE2;
David Härdemand8b4b582010-10-29 16:08:23 -0300309 if (dev->tx_ir) {
Jarod Wilsonca414692010-07-03 01:07:53 -0300310 features |= LIRC_CAN_SEND_PULSE;
David Härdemand8b4b582010-10-29 16:08:23 -0300311 if (dev->s_tx_mask)
Jarod Wilsonca414692010-07-03 01:07:53 -0300312 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
David Härdemand8b4b582010-10-29 16:08:23 -0300313 if (dev->s_tx_carrier)
Jarod Wilsonca414692010-07-03 01:07:53 -0300314 features |= LIRC_CAN_SET_SEND_CARRIER;
David Härdemand8b4b582010-10-29 16:08:23 -0300315 if (dev->s_tx_duty_cycle)
Maxim Levitsky67332ba2010-09-06 18:26:08 -0300316 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
Jarod Wilsonca414692010-07-03 01:07:53 -0300317 }
318
David Härdemand8b4b582010-10-29 16:08:23 -0300319 if (dev->s_rx_carrier_range)
Maxim Levitskye5893332010-07-31 11:59:23 -0300320 features |= LIRC_CAN_SET_REC_CARRIER |
321 LIRC_CAN_SET_REC_CARRIER_RANGE;
322
David Härdemand8b4b582010-10-29 16:08:23 -0300323 if (dev->s_learning_mode)
Maxim Levitskye5893332010-07-31 11:59:23 -0300324 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
325
David Härdemand8b4b582010-10-29 16:08:23 -0300326 if (dev->s_carrier_report)
Maxim Levitsky46519182010-10-16 19:56:28 -0300327 features |= LIRC_CAN_MEASURE_CARRIER;
328
David Härdemand8b4b582010-10-29 16:08:23 -0300329 if (dev->max_timeout)
Maxim Levitskye5893332010-07-31 11:59:23 -0300330 features |= LIRC_CAN_SET_REC_TIMEOUT;
331
Jarod Wilsonca414692010-07-03 01:07:53 -0300332 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
David Härdemand8b4b582010-10-29 16:08:23 -0300333 dev->driver_name);
Jarod Wilsonca414692010-07-03 01:07:53 -0300334 drv->minor = -1;
335 drv->features = features;
David Härdemand8b4b582010-10-29 16:08:23 -0300336 drv->data = &dev->raw->lirc;
Jarod Wilsonca414692010-07-03 01:07:53 -0300337 drv->rbuf = rbuf;
338 drv->set_use_inc = &ir_lirc_open;
339 drv->set_use_dec = &ir_lirc_close;
340 drv->code_length = sizeof(struct ir_raw_event) * 8;
341 drv->fops = &lirc_fops;
David Härdemand8b4b582010-10-29 16:08:23 -0300342 drv->dev = &dev->dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300343 drv->owner = THIS_MODULE;
344
345 drv->minor = lirc_register_driver(drv);
346 if (drv->minor < 0) {
347 rc = -ENODEV;
348 goto lirc_register_failed;
349 }
350
David Härdemand8b4b582010-10-29 16:08:23 -0300351 dev->raw->lirc.drv = drv;
352 dev->raw->lirc.dev = dev;
Jarod Wilsonca414692010-07-03 01:07:53 -0300353 return 0;
354
355lirc_register_failed:
356rbuf_init_failed:
357 kfree(rbuf);
358rbuf_alloc_failed:
359 kfree(drv);
360
361 return rc;
362}
363
David Härdemand8b4b582010-10-29 16:08:23 -0300364static int ir_lirc_unregister(struct rc_dev *dev)
Jarod Wilsonca414692010-07-03 01:07:53 -0300365{
David Härdemand8b4b582010-10-29 16:08:23 -0300366 struct lirc_codec *lirc = &dev->raw->lirc;
Jarod Wilsonca414692010-07-03 01:07:53 -0300367
368 lirc_unregister_driver(lirc->drv->minor);
369 lirc_buffer_free(lirc->drv->rbuf);
370 kfree(lirc->drv);
371
372 return 0;
373}
374
375static struct ir_raw_handler lirc_handler = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300376 .protocols = RC_TYPE_LIRC,
Jarod Wilsonca414692010-07-03 01:07:53 -0300377 .decode = ir_lirc_decode,
378 .raw_register = ir_lirc_register,
379 .raw_unregister = ir_lirc_unregister,
380};
381
382static int __init ir_lirc_codec_init(void)
383{
384 ir_raw_handler_register(&lirc_handler);
385
386 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
387 return 0;
388}
389
390static void __exit ir_lirc_codec_exit(void)
391{
392 ir_raw_handler_unregister(&lirc_handler);
393}
394
395module_init(ir_lirc_codec_init);
396module_exit(ir_lirc_codec_exit);
397
398MODULE_LICENSE("GPL");
399MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
400MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
401MODULE_DESCRIPTION("LIRC IR handler bridge");