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