Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / rc / ir-lirc-codec.c
blob165ea8f3fa307f7d26a60a0368925ab2e921a30c
1 /* ir-lirc-codec.c - rc-core to classic lirc interface bridge
3 * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
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.
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.
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"
23 #define LIRCBUF_SIZE 256
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
31 * This function returns -EINVAL if the lirc interfaces aren't wired up.
33 static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev)
35 struct lirc_codec *lirc = &dev->raw->lirc;
36 int sample;
38 if (!(dev->raw->enabled_protocols & RC_TYPE_LIRC))
39 return 0;
41 if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
42 return -EINVAL;
44 /* Packet start */
45 if (ev.reset)
46 return 0;
48 /* Carrier reports */
49 if (ev.carrier_report) {
50 sample = LIRC_FREQUENCY(ev.carrier);
51 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
53 /* Packet end */
54 } else if (ev.timeout) {
56 if (lirc->gap)
57 return 0;
59 lirc->gap_start = ktime_get();
60 lirc->gap = true;
61 lirc->gap_duration = ev.duration;
63 if (!lirc->send_timeout_reports)
64 return 0;
66 sample = LIRC_TIMEOUT(ev.duration / 1000);
67 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
69 /* Normal sample */
70 } else {
72 if (lirc->gap) {
73 int gap_sample;
75 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
76 lirc->gap_start));
78 /* Convert to ms and cap by LIRC_VALUE_MASK */
79 do_div(lirc->gap_duration, 1000);
80 lirc->gap_duration = min(lirc->gap_duration,
81 (u64)LIRC_VALUE_MASK);
83 gap_sample = LIRC_SPACE(lirc->gap_duration);
84 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
85 (unsigned char *) &gap_sample);
86 lirc->gap = false;
89 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
90 LIRC_SPACE(ev.duration / 1000);
91 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
92 TO_US(ev.duration), TO_STR(ev.pulse));
95 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
96 (unsigned char *) &sample);
97 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
99 return 0;
102 static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
103 size_t n, loff_t *ppos)
105 struct lirc_codec *lirc;
106 struct rc_dev *dev;
107 unsigned int *txbuf; /* buffer with values to transmit */
108 ssize_t ret = 0;
109 size_t count;
111 lirc = lirc_get_pdata(file);
112 if (!lirc)
113 return -EFAULT;
115 if (n < sizeof(unsigned) || n % sizeof(unsigned))
116 return -EINVAL;
118 count = n / sizeof(unsigned);
119 if (count > LIRCBUF_SIZE || count % 2 == 0)
120 return -EINVAL;
122 txbuf = memdup_user(buf, n);
123 if (IS_ERR(txbuf))
124 return PTR_ERR(txbuf);
126 dev = lirc->dev;
127 if (!dev) {
128 ret = -EFAULT;
129 goto out;
132 if (dev->tx_ir)
133 ret = dev->tx_ir(dev, txbuf, count);
135 if (ret > 0)
136 ret *= sizeof(unsigned);
138 out:
139 kfree(txbuf);
140 return ret;
143 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
144 unsigned long __user arg)
146 struct lirc_codec *lirc;
147 struct rc_dev *dev;
148 int ret = 0;
149 __u32 val = 0, tmp;
151 lirc = lirc_get_pdata(filep);
152 if (!lirc)
153 return -EFAULT;
155 dev = lirc->dev;
156 if (!dev)
157 return -EFAULT;
159 if (_IOC_DIR(cmd) & _IOC_WRITE) {
160 ret = get_user(val, (__u32 *)arg);
161 if (ret)
162 return ret;
165 switch (cmd) {
167 /* legacy support */
168 case LIRC_GET_SEND_MODE:
169 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
170 break;
172 case LIRC_SET_SEND_MODE:
173 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
174 return -EINVAL;
175 return 0;
177 /* TX settings */
178 case LIRC_SET_TRANSMITTER_MASK:
179 if (!dev->s_tx_mask)
180 return -EINVAL;
182 return dev->s_tx_mask(dev, val);
184 case LIRC_SET_SEND_CARRIER:
185 if (!dev->s_tx_carrier)
186 return -EINVAL;
188 return dev->s_tx_carrier(dev, val);
190 case LIRC_SET_SEND_DUTY_CYCLE:
191 if (!dev->s_tx_duty_cycle)
192 return -ENOSYS;
194 if (val <= 0 || val >= 100)
195 return -EINVAL;
197 return dev->s_tx_duty_cycle(dev, val);
199 /* RX settings */
200 case LIRC_SET_REC_CARRIER:
201 if (!dev->s_rx_carrier_range)
202 return -ENOSYS;
204 if (val <= 0)
205 return -EINVAL;
207 return dev->s_rx_carrier_range(dev,
208 dev->raw->lirc.carrier_low,
209 val);
211 case LIRC_SET_REC_CARRIER_RANGE:
212 if (val <= 0)
213 return -EINVAL;
215 dev->raw->lirc.carrier_low = val;
216 return 0;
218 case LIRC_GET_REC_RESOLUTION:
219 val = dev->rx_resolution;
220 break;
222 case LIRC_SET_WIDEBAND_RECEIVER:
223 if (!dev->s_learning_mode)
224 return -ENOSYS;
226 return dev->s_learning_mode(dev, !!val);
228 case LIRC_SET_MEASURE_CARRIER_MODE:
229 if (!dev->s_carrier_report)
230 return -ENOSYS;
232 return dev->s_carrier_report(dev, !!val);
234 /* Generic timeout support */
235 case LIRC_GET_MIN_TIMEOUT:
236 if (!dev->max_timeout)
237 return -ENOSYS;
238 val = dev->min_timeout / 1000;
239 break;
241 case LIRC_GET_MAX_TIMEOUT:
242 if (!dev->max_timeout)
243 return -ENOSYS;
244 val = dev->max_timeout / 1000;
245 break;
247 case LIRC_SET_REC_TIMEOUT:
248 if (!dev->max_timeout)
249 return -ENOSYS;
251 tmp = val * 1000;
253 if (tmp < dev->min_timeout ||
254 tmp > dev->max_timeout)
255 return -EINVAL;
257 dev->timeout = tmp;
258 break;
260 case LIRC_SET_REC_TIMEOUT_REPORTS:
261 lirc->send_timeout_reports = !!val;
262 break;
264 default:
265 return lirc_dev_fop_ioctl(filep, cmd, arg);
268 if (_IOC_DIR(cmd) & _IOC_READ)
269 ret = put_user(val, (__u32 *)arg);
271 return ret;
274 static int ir_lirc_open(void *data)
276 return 0;
279 static void ir_lirc_close(void *data)
281 return;
284 static struct file_operations lirc_fops = {
285 .owner = THIS_MODULE,
286 .write = ir_lirc_transmit_ir,
287 .unlocked_ioctl = ir_lirc_ioctl,
288 #ifdef CONFIG_COMPAT
289 .compat_ioctl = ir_lirc_ioctl,
290 #endif
291 .read = lirc_dev_fop_read,
292 .poll = lirc_dev_fop_poll,
293 .open = lirc_dev_fop_open,
294 .release = lirc_dev_fop_close,
295 .llseek = no_llseek,
298 static int ir_lirc_register(struct rc_dev *dev)
300 struct lirc_driver *drv;
301 struct lirc_buffer *rbuf;
302 int rc = -ENOMEM;
303 unsigned long features;
305 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
306 if (!drv)
307 return rc;
309 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
310 if (!rbuf)
311 goto rbuf_alloc_failed;
313 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
314 if (rc)
315 goto rbuf_init_failed;
317 features = LIRC_CAN_REC_MODE2;
318 if (dev->tx_ir) {
319 features |= LIRC_CAN_SEND_PULSE;
320 if (dev->s_tx_mask)
321 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
322 if (dev->s_tx_carrier)
323 features |= LIRC_CAN_SET_SEND_CARRIER;
324 if (dev->s_tx_duty_cycle)
325 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
328 if (dev->s_rx_carrier_range)
329 features |= LIRC_CAN_SET_REC_CARRIER |
330 LIRC_CAN_SET_REC_CARRIER_RANGE;
332 if (dev->s_learning_mode)
333 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
335 if (dev->s_carrier_report)
336 features |= LIRC_CAN_MEASURE_CARRIER;
338 if (dev->max_timeout)
339 features |= LIRC_CAN_SET_REC_TIMEOUT;
341 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
342 dev->driver_name);
343 drv->minor = -1;
344 drv->features = features;
345 drv->data = &dev->raw->lirc;
346 drv->rbuf = rbuf;
347 drv->set_use_inc = &ir_lirc_open;
348 drv->set_use_dec = &ir_lirc_close;
349 drv->code_length = sizeof(struct ir_raw_event) * 8;
350 drv->fops = &lirc_fops;
351 drv->dev = &dev->dev;
352 drv->owner = THIS_MODULE;
354 drv->minor = lirc_register_driver(drv);
355 if (drv->minor < 0) {
356 rc = -ENODEV;
357 goto lirc_register_failed;
360 dev->raw->lirc.drv = drv;
361 dev->raw->lirc.dev = dev;
362 return 0;
364 lirc_register_failed:
365 rbuf_init_failed:
366 kfree(rbuf);
367 rbuf_alloc_failed:
368 kfree(drv);
370 return rc;
373 static int ir_lirc_unregister(struct rc_dev *dev)
375 struct lirc_codec *lirc = &dev->raw->lirc;
377 lirc_unregister_driver(lirc->drv->minor);
378 lirc_buffer_free(lirc->drv->rbuf);
379 kfree(lirc->drv);
381 return 0;
384 static struct ir_raw_handler lirc_handler = {
385 .protocols = RC_TYPE_LIRC,
386 .decode = ir_lirc_decode,
387 .raw_register = ir_lirc_register,
388 .raw_unregister = ir_lirc_unregister,
391 static int __init ir_lirc_codec_init(void)
393 ir_raw_handler_register(&lirc_handler);
395 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
396 return 0;
399 static void __exit ir_lirc_codec_exit(void)
401 ir_raw_handler_unregister(&lirc_handler);
404 module_init(ir_lirc_codec_init);
405 module_exit(ir_lirc_codec_exit);
407 MODULE_LICENSE("GPL");
408 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
409 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
410 MODULE_DESCRIPTION("LIRC IR handler bridge");