[media] dvb-usb-gp8psk: Fix tuner timeout (against git)
[deliverable/linux.git] / drivers / media / IR / ir-lirc-codec.c
CommitLineData
ca414698
JW
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>
5690085e 18#include <media/lirc_dev.h>
ca414698
JW
19#include <media/ir-core.h>
20#include "ir-core-priv.h"
ca414698
JW
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 */
32static 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);
510fcb70 35 int sample;
ca414698
JW
36
37 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC))
38 return 0;
39
40 if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf)
41 return -EINVAL;
42
510fcb70
ML
43 if (IS_RESET(ev))
44 return 0;
45
ca414698
JW
46 IR_dprintk(2, "LIRC data transfer started (%uus %s)\n",
47 TO_US(ev.duration), TO_STR(ev.pulse));
48
510fcb70 49 sample = ev.duration / 1000;
ca414698 50 if (ev.pulse)
510fcb70 51 sample |= PULSE_BIT;
ca414698
JW
52
53 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
510fcb70 54 (unsigned char *) &sample);
ca414698
JW
55 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
56
ca414698
JW
57
58 return 0;
59}
60
61static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
62 size_t n, loff_t *ppos)
63{
64 struct lirc_codec *lirc;
65 struct ir_input_dev *ir_dev;
66 int *txbuf; /* buffer with values to transmit */
67 int ret = 0, count;
68
69 lirc = lirc_get_pdata(file);
70 if (!lirc)
71 return -EFAULT;
72
73 if (n % sizeof(int))
74 return -EINVAL;
75
76 count = n / sizeof(int);
77 if (count > LIRCBUF_SIZE || count % 2 == 0)
78 return -EINVAL;
79
6efb870a
JW
80 txbuf = memdup_user(buf, n);
81 if (IS_ERR(txbuf))
82 return PTR_ERR(txbuf);
ca414698
JW
83
84 ir_dev = lirc->ir_dev;
85 if (!ir_dev) {
86 ret = -EFAULT;
87 goto out;
88 }
89
90 if (ir_dev->props && ir_dev->props->tx_ir)
91 ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n);
92
93out:
94 kfree(txbuf);
95 return ret;
96}
97
e589333f
ML
98static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
99 unsigned long __user arg)
ca414698
JW
100{
101 struct lirc_codec *lirc;
102 struct ir_input_dev *ir_dev;
103 int ret = 0;
104 void *drv_data;
be1f985f 105 __u32 val = 0;
ca414698
JW
106
107 lirc = lirc_get_pdata(filep);
108 if (!lirc)
109 return -EFAULT;
110
111 ir_dev = lirc->ir_dev;
112 if (!ir_dev || !ir_dev->props || !ir_dev->props->priv)
113 return -EFAULT;
114
115 drv_data = ir_dev->props->priv;
116
e589333f 117 if (_IOC_DIR(cmd) & _IOC_WRITE) {
be1f985f 118 ret = get_user(val, (__u32 *)arg);
ca414698
JW
119 if (ret)
120 return ret;
e589333f
ML
121 }
122
123 switch (cmd) {
124
125 /* legacy support */
126 case LIRC_GET_SEND_MODE:
127 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
128 break;
129
130 case LIRC_SET_SEND_MODE:
131 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
132 return -EINVAL;
133 break;
ca414698 134
e589333f
ML
135 /* TX settings */
136 case LIRC_SET_TRANSMITTER_MASK:
137 if (ir_dev->props->s_tx_mask)
be1f985f 138 ret = ir_dev->props->s_tx_mask(drv_data, val);
ca414698
JW
139 else
140 return -EINVAL;
141 break;
142
143 case LIRC_SET_SEND_CARRIER:
e589333f 144 if (ir_dev->props->s_tx_carrier)
be1f985f 145 ir_dev->props->s_tx_carrier(drv_data, val);
ca414698
JW
146 else
147 return -EINVAL;
148 break;
149
e589333f
ML
150 case LIRC_SET_SEND_DUTY_CYCLE:
151 if (!ir_dev->props->s_tx_duty_cycle)
152 return -ENOSYS;
153
154 if (val <= 0 || val >= 100)
155 return -EINVAL;
156
157 ir_dev->props->s_tx_duty_cycle(ir_dev->props->priv, val);
ca414698
JW
158 break;
159
e589333f
ML
160 /* RX settings */
161 case LIRC_SET_REC_CARRIER:
162 if (ir_dev->props->s_rx_carrier_range)
163 ret = ir_dev->props->s_rx_carrier_range(
164 ir_dev->props->priv,
165 ir_dev->raw->lirc.carrier_low, val);
166 else
167 return -ENOSYS;
ca414698 168
e589333f
ML
169 if (!ret)
170 ir_dev->raw->lirc.carrier_low = 0;
171 break;
172
173 case LIRC_SET_REC_CARRIER_RANGE:
174 if (val >= 0)
175 ir_dev->raw->lirc.carrier_low = val;
176 break;
177
178
179 case LIRC_GET_REC_RESOLUTION:
180 val = ir_dev->props->rx_resolution;
181 break;
182
183 case LIRC_SET_WIDEBAND_RECEIVER:
184 if (ir_dev->props->s_learning_mode)
185 return ir_dev->props->s_learning_mode(
186 ir_dev->props->priv, !!val);
187 else
188 return -ENOSYS;
189
190 /* Generic timeout support */
191 case LIRC_GET_MIN_TIMEOUT:
192 if (!ir_dev->props->max_timeout)
193 return -ENOSYS;
194 val = ir_dev->props->min_timeout / 1000;
195 break;
196
197 case LIRC_GET_MAX_TIMEOUT:
198 if (!ir_dev->props->max_timeout)
199 return -ENOSYS;
200 val = ir_dev->props->max_timeout / 1000;
201 break;
202
203 case LIRC_SET_REC_TIMEOUT:
204 if (val < ir_dev->props->min_timeout ||
205 val > ir_dev->props->max_timeout)
ca414698 206 return -EINVAL;
e589333f 207 ir_dev->props->timeout = val * 1000;
ca414698
JW
208 break;
209
210 default:
044e5878 211 return lirc_dev_fop_ioctl(filep, cmd, arg);
ca414698
JW
212 }
213
e589333f 214 if (_IOC_DIR(cmd) & _IOC_READ)
be1f985f 215 ret = put_user(val, (__u32 *)arg);
e589333f 216
ca414698
JW
217 return ret;
218}
219
220static int ir_lirc_open(void *data)
221{
222 return 0;
223}
224
225static void ir_lirc_close(void *data)
226{
227 return;
228}
229
230static struct file_operations lirc_fops = {
231 .owner = THIS_MODULE,
232 .write = ir_lirc_transmit_ir,
044e5878 233 .unlocked_ioctl = ir_lirc_ioctl,
8be292cc
JW
234#ifdef CONFIG_COMPAT
235 .compat_ioctl = ir_lirc_ioctl,
236#endif
ca414698
JW
237 .read = lirc_dev_fop_read,
238 .poll = lirc_dev_fop_poll,
239 .open = lirc_dev_fop_open,
240 .release = lirc_dev_fop_close,
241};
242
243static int ir_lirc_register(struct input_dev *input_dev)
244{
245 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
246 struct lirc_driver *drv;
247 struct lirc_buffer *rbuf;
248 int rc = -ENOMEM;
249 unsigned long features;
250
251 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
252 if (!drv)
253 return rc;
254
255 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
49b7a12c 256 if (!rbuf)
ca414698
JW
257 goto rbuf_alloc_failed;
258
259 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
260 if (rc)
261 goto rbuf_init_failed;
262
263 features = LIRC_CAN_REC_MODE2;
264 if (ir_dev->props->tx_ir) {
e589333f 265
ca414698
JW
266 features |= LIRC_CAN_SEND_PULSE;
267 if (ir_dev->props->s_tx_mask)
268 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
269 if (ir_dev->props->s_tx_carrier)
270 features |= LIRC_CAN_SET_SEND_CARRIER;
e589333f
ML
271
272 if (ir_dev->props->s_tx_duty_cycle)
67332ba8 273 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
ca414698
JW
274 }
275
e589333f
ML
276 if (ir_dev->props->s_rx_carrier_range)
277 features |= LIRC_CAN_SET_REC_CARRIER |
278 LIRC_CAN_SET_REC_CARRIER_RANGE;
279
280 if (ir_dev->props->s_learning_mode)
281 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
282
283 if (ir_dev->props->max_timeout)
284 features |= LIRC_CAN_SET_REC_TIMEOUT;
285
286
ca414698
JW
287 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
288 ir_dev->driver_name);
289 drv->minor = -1;
290 drv->features = features;
291 drv->data = &ir_dev->raw->lirc;
292 drv->rbuf = rbuf;
293 drv->set_use_inc = &ir_lirc_open;
294 drv->set_use_dec = &ir_lirc_close;
295 drv->code_length = sizeof(struct ir_raw_event) * 8;
296 drv->fops = &lirc_fops;
297 drv->dev = &ir_dev->dev;
298 drv->owner = THIS_MODULE;
299
300 drv->minor = lirc_register_driver(drv);
301 if (drv->minor < 0) {
302 rc = -ENODEV;
303 goto lirc_register_failed;
304 }
305
306 ir_dev->raw->lirc.drv = drv;
307 ir_dev->raw->lirc.ir_dev = ir_dev;
ca414698
JW
308 return 0;
309
310lirc_register_failed:
311rbuf_init_failed:
312 kfree(rbuf);
313rbuf_alloc_failed:
314 kfree(drv);
315
316 return rc;
317}
318
319static int ir_lirc_unregister(struct input_dev *input_dev)
320{
321 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
322 struct lirc_codec *lirc = &ir_dev->raw->lirc;
323
324 lirc_unregister_driver(lirc->drv->minor);
325 lirc_buffer_free(lirc->drv->rbuf);
326 kfree(lirc->drv);
327
328 return 0;
329}
330
331static struct ir_raw_handler lirc_handler = {
332 .protocols = IR_TYPE_LIRC,
333 .decode = ir_lirc_decode,
334 .raw_register = ir_lirc_register,
335 .raw_unregister = ir_lirc_unregister,
336};
337
338static int __init ir_lirc_codec_init(void)
339{
340 ir_raw_handler_register(&lirc_handler);
341
342 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
343 return 0;
344}
345
346static void __exit ir_lirc_codec_exit(void)
347{
348 ir_raw_handler_unregister(&lirc_handler);
349}
350
351module_init(ir_lirc_codec_init);
352module_exit(ir_lirc_codec_exit);
353
354MODULE_LICENSE("GPL");
355MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
356MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
357MODULE_DESCRIPTION("LIRC IR handler bridge");
This page took 0.109587 seconds and 5 git commands to generate.