backlight: max8925_bl: Fix Device Tree node lookup
[linux/fpc-iii.git] / drivers / media / rc / ir-lirc-codec.c
blob080130582303a1d009136f3ed8f569b0f4463486
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->enabled_protocols & RC_BIT_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 /* 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");
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);
59 /* Packet end */
60 } else if (ev.timeout) {
62 if (lirc->gap)
63 return 0;
65 lirc->gap_start = ktime_get();
66 lirc->gap = true;
67 lirc->gap_duration = ev.duration;
69 if (!lirc->send_timeout_reports)
70 return 0;
72 sample = LIRC_TIMEOUT(ev.duration / 1000);
73 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
75 /* Normal sample */
76 } else {
78 if (lirc->gap) {
79 int gap_sample;
81 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
82 lirc->gap_start));
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);
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;
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));
101 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
102 (unsigned char *) &sample);
103 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
105 return 0;
108 static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
109 size_t n, loff_t *ppos)
111 struct lirc_codec *lirc;
112 struct rc_dev *dev;
113 unsigned int *txbuf; /* buffer with values to transmit */
114 ssize_t ret = -EINVAL;
115 size_t count;
116 ktime_t start;
117 s64 towait;
118 unsigned int duration = 0; /* signal duration in us */
119 int i;
121 start = ktime_get();
123 lirc = lirc_get_pdata(file);
124 if (!lirc)
125 return -EFAULT;
127 if (n < sizeof(unsigned) || n % sizeof(unsigned))
128 return -EINVAL;
130 count = n / sizeof(unsigned);
131 if (count > LIRCBUF_SIZE || count % 2 == 0)
132 return -EINVAL;
134 txbuf = memdup_user(buf, n);
135 if (IS_ERR(txbuf))
136 return PTR_ERR(txbuf);
138 dev = lirc->dev;
139 if (!dev) {
140 ret = -EFAULT;
141 goto out;
144 if (!dev->tx_ir) {
145 ret = -ENOSYS;
146 goto out;
149 for (i = 0; i < count; i++) {
150 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
151 ret = -EINVAL;
152 goto out;
155 duration += txbuf[i];
158 ret = dev->tx_ir(dev, txbuf, count);
159 if (ret < 0)
160 goto out;
162 for (duration = i = 0; i < ret; i++)
163 duration += txbuf[i];
165 ret *= sizeof(unsigned int);
168 * The lircd gap calculation expects the write function to
169 * wait for the actual IR signal to be transmitted before
170 * returning.
172 towait = ktime_us_delta(ktime_add_us(start, duration), ktime_get());
173 if (towait > 0) {
174 set_current_state(TASK_INTERRUPTIBLE);
175 schedule_timeout(usecs_to_jiffies(towait));
178 out:
179 kfree(txbuf);
180 return ret;
183 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
184 unsigned long arg)
186 struct lirc_codec *lirc;
187 struct rc_dev *dev;
188 u32 __user *argp = (u32 __user *)(arg);
189 int ret = 0;
190 __u32 val = 0, tmp;
192 lirc = lirc_get_pdata(filep);
193 if (!lirc)
194 return -EFAULT;
196 dev = lirc->dev;
197 if (!dev)
198 return -EFAULT;
200 if (_IOC_DIR(cmd) & _IOC_WRITE) {
201 ret = get_user(val, argp);
202 if (ret)
203 return ret;
206 switch (cmd) {
208 /* legacy support */
209 case LIRC_GET_SEND_MODE:
210 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
211 break;
213 case LIRC_SET_SEND_MODE:
214 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
215 return -EINVAL;
216 return 0;
218 /* TX settings */
219 case LIRC_SET_TRANSMITTER_MASK:
220 if (!dev->s_tx_mask)
221 return -ENOSYS;
223 return dev->s_tx_mask(dev, val);
225 case LIRC_SET_SEND_CARRIER:
226 if (!dev->s_tx_carrier)
227 return -ENOSYS;
229 return dev->s_tx_carrier(dev, val);
231 case LIRC_SET_SEND_DUTY_CYCLE:
232 if (!dev->s_tx_duty_cycle)
233 return -ENOSYS;
235 if (val <= 0 || val >= 100)
236 return -EINVAL;
238 return dev->s_tx_duty_cycle(dev, val);
240 /* RX settings */
241 case LIRC_SET_REC_CARRIER:
242 if (!dev->s_rx_carrier_range)
243 return -ENOSYS;
245 if (val <= 0)
246 return -EINVAL;
248 return dev->s_rx_carrier_range(dev,
249 dev->raw->lirc.carrier_low,
250 val);
252 case LIRC_SET_REC_CARRIER_RANGE:
253 if (val <= 0)
254 return -EINVAL;
256 dev->raw->lirc.carrier_low = val;
257 return 0;
259 case LIRC_GET_REC_RESOLUTION:
260 val = dev->rx_resolution;
261 break;
263 case LIRC_SET_WIDEBAND_RECEIVER:
264 if (!dev->s_learning_mode)
265 return -ENOSYS;
267 return dev->s_learning_mode(dev, !!val);
269 case LIRC_SET_MEASURE_CARRIER_MODE:
270 if (!dev->s_carrier_report)
271 return -ENOSYS;
273 return dev->s_carrier_report(dev, !!val);
275 /* Generic timeout support */
276 case LIRC_GET_MIN_TIMEOUT:
277 if (!dev->max_timeout)
278 return -ENOSYS;
279 val = dev->min_timeout / 1000;
280 break;
282 case LIRC_GET_MAX_TIMEOUT:
283 if (!dev->max_timeout)
284 return -ENOSYS;
285 val = dev->max_timeout / 1000;
286 break;
288 case LIRC_SET_REC_TIMEOUT:
289 if (!dev->max_timeout)
290 return -ENOSYS;
292 /* Check for multiply overflow */
293 if (val > U32_MAX / 1000)
294 return -EINVAL;
296 tmp = val * 1000;
298 if (tmp < dev->min_timeout || tmp > dev->max_timeout)
299 return -EINVAL;
301 dev->timeout = tmp;
302 break;
304 case LIRC_SET_REC_TIMEOUT_REPORTS:
305 lirc->send_timeout_reports = !!val;
306 break;
308 default:
309 return lirc_dev_fop_ioctl(filep, cmd, arg);
312 if (_IOC_DIR(cmd) & _IOC_READ)
313 ret = put_user(val, argp);
315 return ret;
318 static int ir_lirc_open(void *data)
320 return 0;
323 static void ir_lirc_close(void *data)
325 return;
328 static const struct file_operations lirc_fops = {
329 .owner = THIS_MODULE,
330 .write = ir_lirc_transmit_ir,
331 .unlocked_ioctl = ir_lirc_ioctl,
332 #ifdef CONFIG_COMPAT
333 .compat_ioctl = ir_lirc_ioctl,
334 #endif
335 .read = lirc_dev_fop_read,
336 .poll = lirc_dev_fop_poll,
337 .open = lirc_dev_fop_open,
338 .release = lirc_dev_fop_close,
339 .llseek = no_llseek,
342 static int ir_lirc_register(struct rc_dev *dev)
344 struct lirc_driver *drv;
345 struct lirc_buffer *rbuf;
346 int rc = -ENOMEM;
347 unsigned long features;
349 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
350 if (!drv)
351 return rc;
353 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
354 if (!rbuf)
355 goto rbuf_alloc_failed;
357 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
358 if (rc)
359 goto rbuf_init_failed;
361 features = LIRC_CAN_REC_MODE2;
362 if (dev->tx_ir) {
363 features |= LIRC_CAN_SEND_PULSE;
364 if (dev->s_tx_mask)
365 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
366 if (dev->s_tx_carrier)
367 features |= LIRC_CAN_SET_SEND_CARRIER;
368 if (dev->s_tx_duty_cycle)
369 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
372 if (dev->s_rx_carrier_range)
373 features |= LIRC_CAN_SET_REC_CARRIER |
374 LIRC_CAN_SET_REC_CARRIER_RANGE;
376 if (dev->s_learning_mode)
377 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
379 if (dev->s_carrier_report)
380 features |= LIRC_CAN_MEASURE_CARRIER;
382 if (dev->max_timeout)
383 features |= LIRC_CAN_SET_REC_TIMEOUT;
385 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
386 dev->driver_name);
387 drv->minor = -1;
388 drv->features = features;
389 drv->data = &dev->raw->lirc;
390 drv->rbuf = rbuf;
391 drv->set_use_inc = &ir_lirc_open;
392 drv->set_use_dec = &ir_lirc_close;
393 drv->code_length = sizeof(struct ir_raw_event) * 8;
394 drv->fops = &lirc_fops;
395 drv->dev = &dev->dev;
396 drv->rdev = dev;
397 drv->owner = THIS_MODULE;
399 drv->minor = lirc_register_driver(drv);
400 if (drv->minor < 0) {
401 rc = -ENODEV;
402 goto lirc_register_failed;
405 dev->raw->lirc.drv = drv;
406 dev->raw->lirc.dev = dev;
407 return 0;
409 lirc_register_failed:
410 rbuf_init_failed:
411 kfree(rbuf);
412 rbuf_alloc_failed:
413 kfree(drv);
415 return rc;
418 static int ir_lirc_unregister(struct rc_dev *dev)
420 struct lirc_codec *lirc = &dev->raw->lirc;
422 lirc_unregister_driver(lirc->drv->minor);
423 lirc_buffer_free(lirc->drv->rbuf);
424 kfree(lirc->drv);
426 return 0;
429 static struct ir_raw_handler lirc_handler = {
430 .protocols = RC_BIT_LIRC,
431 .decode = ir_lirc_decode,
432 .raw_register = ir_lirc_register,
433 .raw_unregister = ir_lirc_unregister,
436 static int __init ir_lirc_codec_init(void)
438 ir_raw_handler_register(&lirc_handler);
440 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
441 return 0;
444 static void __exit ir_lirc_codec_exit(void)
446 ir_raw_handler_unregister(&lirc_handler);
449 module_init(ir_lirc_codec_init);
450 module_exit(ir_lirc_codec_exit);
452 MODULE_LICENSE("GPL");
453 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
454 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
455 MODULE_DESCRIPTION("LIRC IR handler bridge");