blob: 9fc0db9d344d6458f6297cc4e015bc12d9486332 [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>
Jarod Wilsonca414692010-07-03 01:07:53 -030019#include <media/ir-core.h>
20#include "ir-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.
27 * @input_dev: the struct input_dev descriptor of the device
28 * @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 */
32static int ir_lirc_decode(struct input_dev *input_dev, struct ir_raw_event ev)
33{
34 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
Maxim Levitsky46519182010-10-16 19:56:28 -030035 struct lirc_codec *lirc = &ir_dev->raw->lirc;
Maxim Levitsky510fcb72010-07-31 11:59:15 -030036 int sample;
Jarod Wilsonca414692010-07-03 01:07:53 -030037
38 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC))
39 return 0;
40
41 if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf)
42 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 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);
66
67 /* Normal sample */
68 } else {
69
70 if (lirc->gap) {
71 int gap_sample;
72
73 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
74 lirc->gap_start));
75
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);
80
81 gap_sample = LIRC_SPACE(lirc->gap_duration);
82 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
83 (unsigned char *) &gap_sample);
84 lirc->gap = false;
85 }
86
87 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
88 LIRC_SPACE(ev.duration / 1000);
89 }
Jarod Wilsonca414692010-07-03 01:07:53 -030090
91 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
Maxim Levitsky510fcb72010-07-31 11:59:15 -030092 (unsigned char *) &sample);
Jarod Wilsonca414692010-07-03 01:07:53 -030093 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
94
Jarod Wilsonca414692010-07-03 01:07:53 -030095 return 0;
96}
97
98static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
99 size_t n, loff_t *ppos)
100{
101 struct lirc_codec *lirc;
102 struct ir_input_dev *ir_dev;
103 int *txbuf; /* buffer with values to transmit */
104 int ret = 0, count;
105
106 lirc = lirc_get_pdata(file);
107 if (!lirc)
108 return -EFAULT;
109
110 if (n % sizeof(int))
111 return -EINVAL;
112
113 count = n / sizeof(int);
114 if (count > LIRCBUF_SIZE || count % 2 == 0)
115 return -EINVAL;
116
Jarod Wilson6efb8702010-07-16 18:29:54 -0300117 txbuf = memdup_user(buf, n);
118 if (IS_ERR(txbuf))
119 return PTR_ERR(txbuf);
Jarod Wilsonca414692010-07-03 01:07:53 -0300120
121 ir_dev = lirc->ir_dev;
122 if (!ir_dev) {
123 ret = -EFAULT;
124 goto out;
125 }
126
127 if (ir_dev->props && ir_dev->props->tx_ir)
128 ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n);
129
130out:
131 kfree(txbuf);
132 return ret;
133}
134
Maxim Levitskye5893332010-07-31 11:59:23 -0300135static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
136 unsigned long __user arg)
Jarod Wilsonca414692010-07-03 01:07:53 -0300137{
138 struct lirc_codec *lirc;
139 struct ir_input_dev *ir_dev;
140 int ret = 0;
141 void *drv_data;
Maxim Levitsky46519182010-10-16 19:56:28 -0300142 __u32 val = 0, tmp;
Jarod Wilsonca414692010-07-03 01:07:53 -0300143
144 lirc = lirc_get_pdata(filep);
145 if (!lirc)
146 return -EFAULT;
147
148 ir_dev = lirc->ir_dev;
149 if (!ir_dev || !ir_dev->props || !ir_dev->props->priv)
150 return -EFAULT;
151
152 drv_data = ir_dev->props->priv;
153
Maxim Levitskye5893332010-07-31 11:59:23 -0300154 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300155 ret = get_user(val, (__u32 *)arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300156 if (ret)
157 return ret;
Maxim Levitskye5893332010-07-31 11:59:23 -0300158 }
Jarod Wilsonca414692010-07-03 01:07:53 -0300159
Maxim Levitskye5893332010-07-31 11:59:23 -0300160 switch (cmd) {
161
162 /* legacy support */
163 case LIRC_GET_SEND_MODE:
164 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
165 break;
166
167 case LIRC_SET_SEND_MODE:
168 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
169 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300170 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300171
172 /* TX settings */
173 case LIRC_SET_TRANSMITTER_MASK:
Maxim Levitsky46519182010-10-16 19:56:28 -0300174 if (!ir_dev->props->s_tx_mask)
Jarod Wilsonca414692010-07-03 01:07:53 -0300175 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300176
177 return ir_dev->props->s_tx_mask(drv_data, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300178
179 case LIRC_SET_SEND_CARRIER:
Maxim Levitsky46519182010-10-16 19:56:28 -0300180 if (!ir_dev->props->s_tx_carrier)
Jarod Wilsonca414692010-07-03 01:07:53 -0300181 return -EINVAL;
Maxim Levitsky46519182010-10-16 19:56:28 -0300182
183 return ir_dev->props->s_tx_carrier(drv_data, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300184
Maxim Levitskye5893332010-07-31 11:59:23 -0300185 case LIRC_SET_SEND_DUTY_CYCLE:
186 if (!ir_dev->props->s_tx_duty_cycle)
187 return -ENOSYS;
188
189 if (val <= 0 || val >= 100)
190 return -EINVAL;
191
Maxim Levitsky46519182010-10-16 19:56:28 -0300192 return ir_dev->props->s_tx_duty_cycle(drv_data, val);
Jarod Wilsonca414692010-07-03 01:07:53 -0300193
Maxim Levitskye5893332010-07-31 11:59:23 -0300194 /* RX settings */
195 case LIRC_SET_REC_CARRIER:
Maxim Levitsky46519182010-10-16 19:56:28 -0300196 if (!ir_dev->props->s_rx_carrier_range)
Maxim Levitskye5893332010-07-31 11:59:23 -0300197 return -ENOSYS;
Jarod Wilsonca414692010-07-03 01:07:53 -0300198
Maxim Levitsky46519182010-10-16 19:56:28 -0300199 if (val <= 0)
200 return -EINVAL;
201
202 return ir_dev->props->s_rx_carrier_range(drv_data,
203 ir_dev->raw->lirc.carrier_low, val);
Maxim Levitskye5893332010-07-31 11:59:23 -0300204
205 case LIRC_SET_REC_CARRIER_RANGE:
Maxim Levitsky46519182010-10-16 19:56:28 -0300206 if (val <= 0)
207 return -EINVAL;
Maxim Levitskye5893332010-07-31 11:59:23 -0300208
Maxim Levitsky46519182010-10-16 19:56:28 -0300209 ir_dev->raw->lirc.carrier_low = val;
210 return 0;
Maxim Levitskye5893332010-07-31 11:59:23 -0300211
212 case LIRC_GET_REC_RESOLUTION:
213 val = ir_dev->props->rx_resolution;
214 break;
215
216 case LIRC_SET_WIDEBAND_RECEIVER:
Maxim Levitsky46519182010-10-16 19:56:28 -0300217 if (!ir_dev->props->s_learning_mode)
Maxim Levitskye5893332010-07-31 11:59:23 -0300218 return -ENOSYS;
219
Maxim Levitsky46519182010-10-16 19:56:28 -0300220 return ir_dev->props->s_learning_mode(drv_data, !!val);
221
222 case LIRC_SET_MEASURE_CARRIER_MODE:
223 if (!ir_dev->props->s_carrier_report)
224 return -ENOSYS;
225
226 return ir_dev->props->s_carrier_report(drv_data, !!val);
227
Maxim Levitskye5893332010-07-31 11:59:23 -0300228 /* Generic timeout support */
229 case LIRC_GET_MIN_TIMEOUT:
230 if (!ir_dev->props->max_timeout)
231 return -ENOSYS;
232 val = ir_dev->props->min_timeout / 1000;
233 break;
234
235 case LIRC_GET_MAX_TIMEOUT:
236 if (!ir_dev->props->max_timeout)
237 return -ENOSYS;
238 val = ir_dev->props->max_timeout / 1000;
239 break;
240
241 case LIRC_SET_REC_TIMEOUT:
Maxim Levitsky46519182010-10-16 19:56:28 -0300242 if (!ir_dev->props->max_timeout)
243 return -ENOSYS;
244
245 tmp = val * 1000;
246
247 if (tmp < ir_dev->props->min_timeout ||
248 tmp > ir_dev->props->max_timeout)
249 return -EINVAL;
250
251 ir_dev->props->timeout = tmp;
252 break;
253
254 case LIRC_SET_REC_TIMEOUT_REPORTS:
255 lirc->send_timeout_reports = !!val;
Jarod Wilsonca414692010-07-03 01:07:53 -0300256 break;
257
258 default:
Arnd Bergmann044e5872010-08-02 15:43:35 -0300259 return lirc_dev_fop_ioctl(filep, cmd, arg);
Jarod Wilsonca414692010-07-03 01:07:53 -0300260 }
261
Maxim Levitskye5893332010-07-31 11:59:23 -0300262 if (_IOC_DIR(cmd) & _IOC_READ)
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300263 ret = put_user(val, (__u32 *)arg);
Maxim Levitskye5893332010-07-31 11:59:23 -0300264
Jarod Wilsonca414692010-07-03 01:07:53 -0300265 return ret;
266}
267
268static int ir_lirc_open(void *data)
269{
270 return 0;
271}
272
273static void ir_lirc_close(void *data)
274{
275 return;
276}
277
278static struct file_operations lirc_fops = {
279 .owner = THIS_MODULE,
280 .write = ir_lirc_transmit_ir,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300281 .unlocked_ioctl = ir_lirc_ioctl,
Jarod Wilson8be292c2010-10-09 15:07:06 -0300282#ifdef CONFIG_COMPAT
283 .compat_ioctl = ir_lirc_ioctl,
284#endif
Jarod Wilsonca414692010-07-03 01:07:53 -0300285 .read = lirc_dev_fop_read,
286 .poll = lirc_dev_fop_poll,
287 .open = lirc_dev_fop_open,
288 .release = lirc_dev_fop_close,
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200289 .llseek = no_llseek,
Jarod Wilsonca414692010-07-03 01:07:53 -0300290};
291
292static int ir_lirc_register(struct input_dev *input_dev)
293{
294 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
295 struct lirc_driver *drv;
296 struct lirc_buffer *rbuf;
297 int rc = -ENOMEM;
298 unsigned long features;
299
300 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
301 if (!drv)
302 return rc;
303
304 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
Dan Carpenter49b7a122010-07-23 07:08:26 -0300305 if (!rbuf)
Jarod Wilsonca414692010-07-03 01:07:53 -0300306 goto rbuf_alloc_failed;
307
308 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
309 if (rc)
310 goto rbuf_init_failed;
311
312 features = LIRC_CAN_REC_MODE2;
313 if (ir_dev->props->tx_ir) {
Maxim Levitskye5893332010-07-31 11:59:23 -0300314
Jarod Wilsonca414692010-07-03 01:07:53 -0300315 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;
Maxim Levitskye5893332010-07-31 11:59:23 -0300320
321 if (ir_dev->props->s_tx_duty_cycle)
Maxim Levitsky67332ba2010-09-06 18:26:08 -0300322 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
Jarod Wilsonca414692010-07-03 01:07:53 -0300323 }
324
Maxim Levitskye5893332010-07-31 11:59:23 -0300325 if (ir_dev->props->s_rx_carrier_range)
326 features |= LIRC_CAN_SET_REC_CARRIER |
327 LIRC_CAN_SET_REC_CARRIER_RANGE;
328
329 if (ir_dev->props->s_learning_mode)
330 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
331
Maxim Levitsky46519182010-10-16 19:56:28 -0300332 if (ir_dev->props->s_carrier_report)
333 features |= LIRC_CAN_MEASURE_CARRIER;
334
335
Maxim Levitskye5893332010-07-31 11:59:23 -0300336 if (ir_dev->props->max_timeout)
337 features |= LIRC_CAN_SET_REC_TIMEOUT;
338
339
Jarod Wilsonca414692010-07-03 01:07:53 -0300340 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
341 ir_dev->driver_name);
342 drv->minor = -1;
343 drv->features = features;
344 drv->data = &ir_dev->raw->lirc;
345 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;
350 drv->dev = &ir_dev->dev;
351 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
359 ir_dev->raw->lirc.drv = drv;
360 ir_dev->raw->lirc.ir_dev = ir_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
372static int ir_lirc_unregister(struct input_dev *input_dev)
373{
374 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
375 struct lirc_codec *lirc = &ir_dev->raw->lirc;
376
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 = {
385 .protocols = IR_TYPE_LIRC,
386 .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");