]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/rc/ir-lirc-codec.c
[media] lirc: advertise LIRC_CAN_GET_REC_RESOLUTION and improve
[karo-tx-linux.git] / drivers / media / rc / ir-lirc-codec.c
1 /* ir-lirc-codec.c - rc-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 <linux/module.h>
18 #include <media/lirc.h>
19 #include <media/lirc_dev.h>
20 #include <media/rc-core.h>
21 #include "rc-core-priv.h"
22
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.
28  * @input_dev:  the struct rc_dev descriptor of the device
29  * @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  */
33 static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev)
34 {
35         struct lirc_codec *lirc = &dev->raw->lirc;
36         int sample;
37
38         if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
39                 return -EINVAL;
40
41         /* Packet start */
42         if (ev.reset) {
43                 /* Userspace expects a long space event before the start of
44                  * the signal to use as a sync.  This may be done with repeat
45                  * packets and normal samples.  But if a reset has been sent
46                  * then we assume that a long time has passed, so we send a
47                  * space with the maximum time value. */
48                 sample = LIRC_SPACE(LIRC_VALUE_MASK);
49                 IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
50
51         /* Carrier reports */
52         } else if (ev.carrier_report) {
53                 sample = LIRC_FREQUENCY(ev.carrier);
54                 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
55
56         /* Packet end */
57         } else if (ev.timeout) {
58
59                 if (lirc->gap)
60                         return 0;
61
62                 lirc->gap_start = ktime_get();
63                 lirc->gap = true;
64                 lirc->gap_duration = ev.duration;
65
66                 if (!lirc->send_timeout_reports)
67                         return 0;
68
69                 sample = LIRC_TIMEOUT(ev.duration / 1000);
70                 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
71
72         /* Normal sample */
73         } else {
74
75                 if (lirc->gap) {
76                         int gap_sample;
77
78                         lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
79                                 lirc->gap_start));
80
81                         /* Convert to ms and cap by LIRC_VALUE_MASK */
82                         do_div(lirc->gap_duration, 1000);
83                         lirc->gap_duration = min(lirc->gap_duration,
84                                                         (u64)LIRC_VALUE_MASK);
85
86                         gap_sample = LIRC_SPACE(lirc->gap_duration);
87                         lirc_buffer_write(dev->raw->lirc.drv->rbuf,
88                                                 (unsigned char *) &gap_sample);
89                         lirc->gap = false;
90                 }
91
92                 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
93                                         LIRC_SPACE(ev.duration / 1000);
94                 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
95                            TO_US(ev.duration), TO_STR(ev.pulse));
96         }
97
98         lirc_buffer_write(dev->raw->lirc.drv->rbuf,
99                           (unsigned char *) &sample);
100         wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
101
102         return 0;
103 }
104
105 static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
106                                    size_t n, loff_t *ppos)
107 {
108         struct lirc_codec *lirc;
109         struct rc_dev *dev;
110         unsigned int *txbuf; /* buffer with values to transmit */
111         ssize_t ret = -EINVAL;
112         size_t count;
113         ktime_t start;
114         s64 towait;
115         unsigned int duration = 0; /* signal duration in us */
116         int i;
117
118         start = ktime_get();
119
120         lirc = lirc_get_pdata(file);
121         if (!lirc)
122                 return -EFAULT;
123
124         if (n < sizeof(unsigned) || n % sizeof(unsigned))
125                 return -EINVAL;
126
127         count = n / sizeof(unsigned);
128         if (count > LIRCBUF_SIZE || count % 2 == 0)
129                 return -EINVAL;
130
131         txbuf = memdup_user(buf, n);
132         if (IS_ERR(txbuf))
133                 return PTR_ERR(txbuf);
134
135         dev = lirc->dev;
136         if (!dev) {
137                 ret = -EFAULT;
138                 goto out;
139         }
140
141         if (!dev->tx_ir) {
142                 ret = -EINVAL;
143                 goto out;
144         }
145
146         for (i = 0; i < count; i++) {
147                 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
148                         ret = -EINVAL;
149                         goto out;
150                 }
151
152                 duration += txbuf[i];
153         }
154
155         ret = dev->tx_ir(dev, txbuf, count);
156         if (ret < 0)
157                 goto out;
158
159         for (duration = i = 0; i < ret; i++)
160                 duration += txbuf[i];
161
162         ret *= sizeof(unsigned int);
163
164         /*
165          * The lircd gap calculation expects the write function to
166          * wait for the actual IR signal to be transmitted before
167          * returning.
168          */
169         towait = ktime_us_delta(ktime_add_us(start, duration), ktime_get());
170         if (towait > 0) {
171                 set_current_state(TASK_INTERRUPTIBLE);
172                 schedule_timeout(usecs_to_jiffies(towait));
173         }
174
175 out:
176         kfree(txbuf);
177         return ret;
178 }
179
180 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
181                         unsigned long arg)
182 {
183         struct lirc_codec *lirc;
184         struct rc_dev *dev;
185         u32 __user *argp = (u32 __user *)(arg);
186         int ret = 0;
187         __u32 val = 0, tmp;
188
189         lirc = lirc_get_pdata(filep);
190         if (!lirc)
191                 return -EFAULT;
192
193         dev = lirc->dev;
194         if (!dev)
195                 return -EFAULT;
196
197         if (_IOC_DIR(cmd) & _IOC_WRITE) {
198                 ret = get_user(val, argp);
199                 if (ret)
200                         return ret;
201         }
202
203         switch (cmd) {
204
205         /* legacy support */
206         case LIRC_GET_SEND_MODE:
207                 if (!dev->tx_ir)
208                         return -ENOTTY;
209
210                 val = LIRC_MODE_PULSE;
211                 break;
212
213         case LIRC_SET_SEND_MODE:
214                 if (!dev->tx_ir)
215                         return -ENOTTY;
216
217                 if (val != LIRC_MODE_PULSE)
218                         return -EINVAL;
219                 return 0;
220
221         /* TX settings */
222         case LIRC_SET_TRANSMITTER_MASK:
223                 if (!dev->s_tx_mask)
224                         return -ENOTTY;
225
226                 return dev->s_tx_mask(dev, val);
227
228         case LIRC_SET_SEND_CARRIER:
229                 if (!dev->s_tx_carrier)
230                         return -ENOTTY;
231
232                 return dev->s_tx_carrier(dev, val);
233
234         case LIRC_SET_SEND_DUTY_CYCLE:
235                 if (!dev->s_tx_duty_cycle)
236                         return -ENOTTY;
237
238                 if (val <= 0 || val >= 100)
239                         return -EINVAL;
240
241                 return dev->s_tx_duty_cycle(dev, val);
242
243         /* RX settings */
244         case LIRC_SET_REC_CARRIER:
245                 if (!dev->s_rx_carrier_range)
246                         return -ENOTTY;
247
248                 if (val <= 0)
249                         return -EINVAL;
250
251                 return dev->s_rx_carrier_range(dev,
252                                                dev->raw->lirc.carrier_low,
253                                                val);
254
255         case LIRC_SET_REC_CARRIER_RANGE:
256                 if (!dev->s_rx_carrier_range)
257                         return -ENOTTY;
258
259                 if (val <= 0)
260                         return -EINVAL;
261
262                 dev->raw->lirc.carrier_low = val;
263                 return 0;
264
265         case LIRC_GET_REC_RESOLUTION:
266                 if (!dev->rx_resolution)
267                         return -ENOTTY;
268
269                 val = dev->rx_resolution;
270                 break;
271
272         case LIRC_SET_WIDEBAND_RECEIVER:
273                 if (!dev->s_learning_mode)
274                         return -ENOTTY;
275
276                 return dev->s_learning_mode(dev, !!val);
277
278         case LIRC_SET_MEASURE_CARRIER_MODE:
279                 if (!dev->s_carrier_report)
280                         return -ENOTTY;
281
282                 return dev->s_carrier_report(dev, !!val);
283
284         /* Generic timeout support */
285         case LIRC_GET_MIN_TIMEOUT:
286                 if (!dev->max_timeout)
287                         return -ENOTTY;
288                 val = DIV_ROUND_UP(dev->min_timeout, 1000);
289                 break;
290
291         case LIRC_GET_MAX_TIMEOUT:
292                 if (!dev->max_timeout)
293                         return -ENOTTY;
294                 val = dev->max_timeout / 1000;
295                 break;
296
297         case LIRC_SET_REC_TIMEOUT:
298                 if (!dev->max_timeout)
299                         return -ENOTTY;
300
301                 tmp = val * 1000;
302
303                 if (tmp < dev->min_timeout ||
304                     tmp > dev->max_timeout)
305                                 return -EINVAL;
306
307                 if (dev->s_timeout)
308                         ret = dev->s_timeout(dev, tmp);
309                 if (!ret)
310                         dev->timeout = tmp;
311                 break;
312
313         case LIRC_SET_REC_TIMEOUT_REPORTS:
314                 if (!dev->timeout)
315                         return -ENOTTY;
316
317                 lirc->send_timeout_reports = !!val;
318                 break;
319
320         default:
321                 return lirc_dev_fop_ioctl(filep, cmd, arg);
322         }
323
324         if (_IOC_DIR(cmd) & _IOC_READ)
325                 ret = put_user(val, argp);
326
327         return ret;
328 }
329
330 static int ir_lirc_open(void *data)
331 {
332         return 0;
333 }
334
335 static void ir_lirc_close(void *data)
336 {
337         return;
338 }
339
340 static const struct file_operations lirc_fops = {
341         .owner          = THIS_MODULE,
342         .write          = ir_lirc_transmit_ir,
343         .unlocked_ioctl = ir_lirc_ioctl,
344 #ifdef CONFIG_COMPAT
345         .compat_ioctl   = ir_lirc_ioctl,
346 #endif
347         .read           = lirc_dev_fop_read,
348         .poll           = lirc_dev_fop_poll,
349         .open           = lirc_dev_fop_open,
350         .release        = lirc_dev_fop_close,
351         .llseek         = no_llseek,
352 };
353
354 static int ir_lirc_register(struct rc_dev *dev)
355 {
356         struct lirc_driver *drv;
357         struct lirc_buffer *rbuf;
358         int rc = -ENOMEM;
359         unsigned long features = 0;
360
361         drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
362         if (!drv)
363                 return rc;
364
365         rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
366         if (!rbuf)
367                 goto rbuf_alloc_failed;
368
369         rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
370         if (rc)
371                 goto rbuf_init_failed;
372
373         if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
374                 features |= LIRC_CAN_REC_MODE2;
375                 if (dev->rx_resolution)
376                         features |= LIRC_CAN_GET_REC_RESOLUTION;
377         }
378         if (dev->tx_ir) {
379                 features |= LIRC_CAN_SEND_PULSE;
380                 if (dev->s_tx_mask)
381                         features |= LIRC_CAN_SET_TRANSMITTER_MASK;
382                 if (dev->s_tx_carrier)
383                         features |= LIRC_CAN_SET_SEND_CARRIER;
384                 if (dev->s_tx_duty_cycle)
385                         features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
386         }
387
388         if (dev->s_rx_carrier_range)
389                 features |= LIRC_CAN_SET_REC_CARRIER |
390                         LIRC_CAN_SET_REC_CARRIER_RANGE;
391
392         if (dev->s_learning_mode)
393                 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
394
395         if (dev->s_carrier_report)
396                 features |= LIRC_CAN_MEASURE_CARRIER;
397
398         if (dev->max_timeout)
399                 features |= LIRC_CAN_SET_REC_TIMEOUT;
400
401         snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
402                  dev->driver_name);
403         drv->minor = -1;
404         drv->features = features;
405         drv->data = &dev->raw->lirc;
406         drv->rbuf = rbuf;
407         drv->set_use_inc = &ir_lirc_open;
408         drv->set_use_dec = &ir_lirc_close;
409         drv->code_length = sizeof(struct ir_raw_event) * 8;
410         drv->fops = &lirc_fops;
411         drv->dev = &dev->dev;
412         drv->rdev = dev;
413         drv->owner = THIS_MODULE;
414
415         drv->minor = lirc_register_driver(drv);
416         if (drv->minor < 0) {
417                 rc = -ENODEV;
418                 goto lirc_register_failed;
419         }
420
421         dev->raw->lirc.drv = drv;
422         dev->raw->lirc.dev = dev;
423         return 0;
424
425 lirc_register_failed:
426 rbuf_init_failed:
427         kfree(rbuf);
428 rbuf_alloc_failed:
429         kfree(drv);
430
431         return rc;
432 }
433
434 static int ir_lirc_unregister(struct rc_dev *dev)
435 {
436         struct lirc_codec *lirc = &dev->raw->lirc;
437
438         lirc_unregister_driver(lirc->drv->minor);
439         lirc_buffer_free(lirc->drv->rbuf);
440         kfree(lirc->drv->rbuf);
441         kfree(lirc->drv);
442
443         return 0;
444 }
445
446 static struct ir_raw_handler lirc_handler = {
447         .protocols      = 0,
448         .decode         = ir_lirc_decode,
449         .raw_register   = ir_lirc_register,
450         .raw_unregister = ir_lirc_unregister,
451 };
452
453 static int __init ir_lirc_codec_init(void)
454 {
455         ir_raw_handler_register(&lirc_handler);
456
457         printk(KERN_INFO "IR LIRC bridge handler initialized\n");
458         return 0;
459 }
460
461 static void __exit ir_lirc_codec_exit(void)
462 {
463         ir_raw_handler_unregister(&lirc_handler);
464 }
465
466 module_init(ir_lirc_codec_init);
467 module_exit(ir_lirc_codec_exit);
468
469 MODULE_LICENSE("GPL");
470 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
471 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
472 MODULE_DESCRIPTION("LIRC IR handler bridge");