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