Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[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 int sample;
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
43 if (IS_RESET(ev))
44 return 0;
45
46 IR_dprintk(2, "LIRC data transfer started (%uus %s)\n",
47 TO_US(ev.duration), TO_STR(ev.pulse));
48
49 sample = ev.duration / 1000;
50 if (ev.pulse)
51 sample |= PULSE_BIT;
52
53 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
54 (unsigned char *) &sample);
55 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
56
57
58 return 0;
59 }
60
61 static 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
80 txbuf = memdup_user(buf, n);
81 if (IS_ERR(txbuf))
82 return PTR_ERR(txbuf);
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
93 out:
94 kfree(txbuf);
95 return ret;
96 }
97
98 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
99 unsigned long __user arg)
100 {
101 struct lirc_codec *lirc;
102 struct ir_input_dev *ir_dev;
103 int ret = 0;
104 void *drv_data;
105 unsigned long val = 0;
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
117 if (_IOC_DIR(cmd) & _IOC_WRITE) {
118 ret = get_user(val, (unsigned long *)arg);
119 if (ret)
120 return ret;
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;
134
135 /* TX settings */
136 case LIRC_SET_TRANSMITTER_MASK:
137 if (ir_dev->props->s_tx_mask)
138 ret = ir_dev->props->s_tx_mask(drv_data, (u32)val);
139 else
140 return -EINVAL;
141 break;
142
143 case LIRC_SET_SEND_CARRIER:
144 if (ir_dev->props->s_tx_carrier)
145 ir_dev->props->s_tx_carrier(drv_data, (u32)val);
146 else
147 return -EINVAL;
148 break;
149
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);
158 break;
159
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;
168
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)
206 return -EINVAL;
207 ir_dev->props->timeout = val * 1000;
208 break;
209
210 default:
211 return lirc_dev_fop_ioctl(filep, cmd, arg);
212 }
213
214 if (_IOC_DIR(cmd) & _IOC_READ)
215 ret = put_user(val, (unsigned long *)arg);
216
217 return ret;
218 }
219
220 static int ir_lirc_open(void *data)
221 {
222 return 0;
223 }
224
225 static void ir_lirc_close(void *data)
226 {
227 return;
228 }
229
230 static struct file_operations lirc_fops = {
231 .owner = THIS_MODULE,
232 .write = ir_lirc_transmit_ir,
233 .unlocked_ioctl = ir_lirc_ioctl,
234 .read = lirc_dev_fop_read,
235 .poll = lirc_dev_fop_poll,
236 .open = lirc_dev_fop_open,
237 .release = lirc_dev_fop_close,
238 .llseek = no_llseek,
239 };
240
241 static int ir_lirc_register(struct input_dev *input_dev)
242 {
243 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
244 struct lirc_driver *drv;
245 struct lirc_buffer *rbuf;
246 int rc = -ENOMEM;
247 unsigned long features;
248
249 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
250 if (!drv)
251 return rc;
252
253 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
254 if (!rbuf)
255 goto rbuf_alloc_failed;
256
257 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
258 if (rc)
259 goto rbuf_init_failed;
260
261 features = LIRC_CAN_REC_MODE2;
262 if (ir_dev->props->tx_ir) {
263
264 features |= LIRC_CAN_SEND_PULSE;
265 if (ir_dev->props->s_tx_mask)
266 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
267 if (ir_dev->props->s_tx_carrier)
268 features |= LIRC_CAN_SET_SEND_CARRIER;
269
270 if (ir_dev->props->s_tx_duty_cycle)
271 features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
272 }
273
274 if (ir_dev->props->s_rx_carrier_range)
275 features |= LIRC_CAN_SET_REC_CARRIER |
276 LIRC_CAN_SET_REC_CARRIER_RANGE;
277
278 if (ir_dev->props->s_learning_mode)
279 features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
280
281 if (ir_dev->props->max_timeout)
282 features |= LIRC_CAN_SET_REC_TIMEOUT;
283
284
285 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
286 ir_dev->driver_name);
287 drv->minor = -1;
288 drv->features = features;
289 drv->data = &ir_dev->raw->lirc;
290 drv->rbuf = rbuf;
291 drv->set_use_inc = &ir_lirc_open;
292 drv->set_use_dec = &ir_lirc_close;
293 drv->code_length = sizeof(struct ir_raw_event) * 8;
294 drv->fops = &lirc_fops;
295 drv->dev = &ir_dev->dev;
296 drv->owner = THIS_MODULE;
297
298 drv->minor = lirc_register_driver(drv);
299 if (drv->minor < 0) {
300 rc = -ENODEV;
301 goto lirc_register_failed;
302 }
303
304 ir_dev->raw->lirc.drv = drv;
305 ir_dev->raw->lirc.ir_dev = ir_dev;
306 return 0;
307
308 lirc_register_failed:
309 rbuf_init_failed:
310 kfree(rbuf);
311 rbuf_alloc_failed:
312 kfree(drv);
313
314 return rc;
315 }
316
317 static int ir_lirc_unregister(struct input_dev *input_dev)
318 {
319 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
320 struct lirc_codec *lirc = &ir_dev->raw->lirc;
321
322 lirc_unregister_driver(lirc->drv->minor);
323 lirc_buffer_free(lirc->drv->rbuf);
324 kfree(lirc->drv);
325
326 return 0;
327 }
328
329 static struct ir_raw_handler lirc_handler = {
330 .protocols = IR_TYPE_LIRC,
331 .decode = ir_lirc_decode,
332 .raw_register = ir_lirc_register,
333 .raw_unregister = ir_lirc_unregister,
334 };
335
336 static int __init ir_lirc_codec_init(void)
337 {
338 ir_raw_handler_register(&lirc_handler);
339
340 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
341 return 0;
342 }
343
344 static void __exit ir_lirc_codec_exit(void)
345 {
346 ir_raw_handler_unregister(&lirc_handler);
347 }
348
349 module_init(ir_lirc_codec_init);
350 module_exit(ir_lirc_codec_exit);
351
352 MODULE_LICENSE("GPL");
353 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
354 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
355 MODULE_DESCRIPTION("LIRC IR handler bridge");
This page took 0.204934 seconds and 6 git commands to generate.