sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / media / rc / ir-lirc-codec.c
blob1c5cc65ea1e11463c9ba6998f48c1947988e842a
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 <media/lirc.h>
18 #include <media/lirc_dev.h>
19 #include <media/rc-core.h>
20 #include "rc-core-priv.h"
22 #define LIRCBUF_SIZE 256
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 rc_dev descriptor of the device
28 * @duration: the struct ir_raw_event descriptor of the pulse/space
30 * This function returns -EINVAL if the lirc interfaces aren't wired up.
32 static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev)
34 struct lirc_codec *lirc = &dev->raw->lirc;
35 int sample;
37 if (!(dev->raw->enabled_protocols & RC_TYPE_LIRC))
38 return 0;
40 if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
41 return -EINVAL;
43 /* Packet start */
44 if (ev.reset)
45 return 0;
47 /* Carrier reports */
48 if (ev.carrier_report) {
49 sample = LIRC_FREQUENCY(ev.carrier);
50 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
52 /* Packet end */
53 } else if (ev.timeout) {
55 if (lirc->gap)
56 return 0;
58 lirc->gap_start = ktime_get();
59 lirc->gap = true;
60 lirc->gap_duration = ev.duration;
62 if (!lirc->send_timeout_reports)
63 return 0;
65 sample = LIRC_TIMEOUT(ev.duration / 1000);
66 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
68 /* Normal sample */
69 } else {
71 if (lirc->gap) {
72 int gap_sample;
74 lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
75 lirc->gap_start));
77 /* Convert to ms and cap by LIRC_VALUE_MASK */
78 do_div(lirc->gap_duration, 1000);
79 lirc->gap_duration = min(lirc->gap_duration,
80 (u64)LIRC_VALUE_MASK);
82 gap_sample = LIRC_SPACE(lirc->gap_duration);
83 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
84 (unsigned char *) &gap_sample);
85 lirc->gap = false;
88 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
89 LIRC_SPACE(ev.duration / 1000);
90 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
91 TO_US(ev.duration), TO_STR(ev.pulse));
94 lirc_buffer_write(dev->raw->lirc.drv->rbuf,
95 (unsigned char *) &sample);
96 wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
98 return 0;
101 static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
102 size_t n, loff_t *ppos)
104 struct lirc_codec *lirc;
105 struct rc_dev *dev;
106 int *txbuf; /* buffer with values to transmit */
107 int ret = 0;
108 size_t count;
110 lirc = lirc_get_pdata(file);
111 if (!lirc)
112 return -EFAULT;
114 if (n % sizeof(int))
115 return -EINVAL;
117 count = n / sizeof(int);
118 if (count > LIRCBUF_SIZE || count % 2 == 0 || n % sizeof(int) != 0)
119 return -EINVAL;
121 txbuf = memdup_user(buf, n);
122 if (IS_ERR(txbuf))
123 return PTR_ERR(txbuf);
125 dev = lirc->dev;
126 if (!dev) {
127 ret = -EFAULT;
128 goto out;
131 if (dev->tx_ir)
132 ret = dev->tx_ir(dev, txbuf, (u32)n);
134 out:
135 kfree(txbuf);
136 return ret;
139 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
140 unsigned long __user arg)
142 struct lirc_codec *lirc;
143 struct rc_dev *dev;
144 int ret = 0;
145 __u32 val = 0, tmp;
147 lirc = lirc_get_pdata(filep);
148 if (!lirc)
149 return -EFAULT;
151 dev = lirc->dev;
152 if (!dev)
153 return -EFAULT;
155 if (_IOC_DIR(cmd) & _IOC_WRITE) {
156 ret = get_user(val, (__u32 *)arg);
157 if (ret)
158 return ret;
161 switch (cmd) {
163 /* legacy support */
164 case LIRC_GET_SEND_MODE:
165 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
166 break;
168 case LIRC_SET_SEND_MODE:
169 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
170 return -EINVAL;
171 return 0;
173 /* TX settings */
174 case LIRC_SET_TRANSMITTER_MASK:
175 if (!dev->s_tx_mask)
176 return -EINVAL;
178 return dev->s_tx_mask(dev, val);
180 case LIRC_SET_SEND_CARRIER:
181 if (!dev->s_tx_carrier)
182 return -EINVAL;
184 return dev->s_tx_carrier(dev, val);
186 case LIRC_SET_SEND_DUTY_CYCLE:
187 if (!dev->s_tx_duty_cycle)
188 return -ENOSYS;
190 if (val <= 0 || val >= 100)
191 return -EINVAL;
193 return dev->s_tx_duty_cycle(dev, val);
195 /* RX settings */
196 case LIRC_SET_REC_CARRIER:
197 if (!dev->s_rx_carrier_range)
198 return -ENOSYS;
200 if (val <= 0)
201 return -EINVAL;
203 return dev->s_rx_carrier_range(dev,
204 dev->raw->lirc.carrier_low,
205 val);
207 case LIRC_SET_REC_CARRIER_RANGE:
208 if (val <= 0)
209 return -EINVAL;
211 dev->raw->lirc.carrier_low = val;
212 return 0;
214 case LIRC_GET_REC_RESOLUTION:
215 val = dev->rx_resolution;
216 break;
218 case LIRC_SET_WIDEBAND_RECEIVER:
219 if (!dev->s_learning_mode)
220 return -ENOSYS;
222 return dev->s_learning_mode(dev, !!val);
224 case LIRC_SET_MEASURE_CARRIER_MODE:
225 if (!dev->s_carrier_report)
226 return -ENOSYS;
228 return dev->s_carrier_report(dev, !!val);
230 /* Generic timeout support */
231 case LIRC_GET_MIN_TIMEOUT:
232 if (!dev->max_timeout)
233 return -ENOSYS;
234 val = dev->min_timeout / 1000;
235 break;
237 case LIRC_GET_MAX_TIMEOUT:
238 if (!dev->max_timeout)
239 return -ENOSYS;
240 val = dev->max_timeout / 1000;
241 break;
243 case LIRC_SET_REC_TIMEOUT:
244 if (!dev->max_timeout)
245 return -ENOSYS;
247 tmp = val * 1000;
249 if (tmp < dev->min_timeout ||
250 tmp > dev->max_timeout)
251 return -EINVAL;
253 dev->timeout = tmp;
254 break;
256 case LIRC_SET_REC_TIMEOUT_REPORTS:
257 lirc->send_timeout_reports = !!val;
258 break;
260 default:
261 return lirc_dev_fop_ioctl(filep, cmd, arg);
264 if (_IOC_DIR(cmd) & _IOC_READ)
265 ret = put_user(val, (__u32 *)arg);
267 return ret;
270 static int ir_lirc_open(void *data)
272 return 0;
275 static void ir_lirc_close(void *data)
277 return;
280 static struct file_operations lirc_fops = {
281 .owner = THIS_MODULE,
282 .write = ir_lirc_transmit_ir,
283 .unlocked_ioctl = ir_lirc_ioctl,
284 #ifdef CONFIG_COMPAT
285 .compat_ioctl = ir_lirc_ioctl,
286 #endif
287 .read = lirc_dev_fop_read,
288 .poll = lirc_dev_fop_poll,
289 .open = lirc_dev_fop_open,
290 .release = lirc_dev_fop_close,
291 .llseek = no_llseek,
294 static int ir_lirc_register(struct rc_dev *dev)
296 struct lirc_driver *drv;
297 struct lirc_buffer *rbuf;
298 int rc = -ENOMEM;
299 unsigned long features;
301 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
302 if (!drv)
303 return rc;
305 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
306 if (!rbuf)
307 goto rbuf_alloc_failed;
309 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
310 if (rc)
311 goto rbuf_init_failed;
313 features = LIRC_CAN_REC_MODE2;
314 if (dev->tx_ir) {
315 features |= LIRC_CAN_SEND_PULSE;
316 if (dev->s_tx_mask)
317 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
318 if (dev->s_tx_carrier)
319 features |= LIRC_CAN_SET_SEND_CARRIER;
320 if (dev->s_tx_duty_cycle)
321 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
324 if (dev->s_rx_carrier_range)
325 features |= LIRC_CAN_SET_REC_CARRIER |
326 LIRC_CAN_SET_REC_CARRIER_RANGE;
328 if (dev->s_learning_mode)
329 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
331 if (dev->s_carrier_report)
332 features |= LIRC_CAN_MEASURE_CARRIER;
334 if (dev->max_timeout)
335 features |= LIRC_CAN_SET_REC_TIMEOUT;
337 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
338 dev->driver_name);
339 drv->minor = -1;
340 drv->features = features;
341 drv->data = &dev->raw->lirc;
342 drv->rbuf = rbuf;
343 drv->set_use_inc = &ir_lirc_open;
344 drv->set_use_dec = &ir_lirc_close;
345 drv->code_length = sizeof(struct ir_raw_event) * 8;
346 drv->fops = &lirc_fops;
347 drv->dev = &dev->dev;
348 drv->owner = THIS_MODULE;
350 drv->minor = lirc_register_driver(drv);
351 if (drv->minor < 0) {
352 rc = -ENODEV;
353 goto lirc_register_failed;
356 dev->raw->lirc.drv = drv;
357 dev->raw->lirc.dev = dev;
358 return 0;
360 lirc_register_failed:
361 rbuf_init_failed:
362 kfree(rbuf);
363 rbuf_alloc_failed:
364 kfree(drv);
366 return rc;
369 static int ir_lirc_unregister(struct rc_dev *dev)
371 struct lirc_codec *lirc = &dev->raw->lirc;
373 lirc_unregister_driver(lirc->drv->minor);
374 lirc_buffer_free(lirc->drv->rbuf);
375 kfree(lirc->drv);
377 return 0;
380 static struct ir_raw_handler lirc_handler = {
381 .protocols = RC_TYPE_LIRC,
382 .decode = ir_lirc_decode,
383 .raw_register = ir_lirc_register,
384 .raw_unregister = ir_lirc_unregister,
387 static int __init ir_lirc_codec_init(void)
389 ir_raw_handler_register(&lirc_handler);
391 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
392 return 0;
395 static void __exit ir_lirc_codec_exit(void)
397 ir_raw_handler_unregister(&lirc_handler);
400 module_init(ir_lirc_codec_init);
401 module_exit(ir_lirc_codec_exit);
403 MODULE_LICENSE("GPL");
404 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
405 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
406 MODULE_DESCRIPTION("LIRC IR handler bridge");