V4L/DVB: IR/lirc: make lirc userspace and staging modules buildable
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / IR / ir-lirc-codec.c
blobafb1ada36c78445217abb017d2943aa85e6a8a4e
1 /* ir-lirc-codec.c - ir-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/ir-core.h>
20 #include "ir-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 input_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 input_dev *input_dev, struct ir_raw_event ev)
34 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
36 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC))
37 return 0;
39 if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf)
40 return -EINVAL;
42 IR_dprintk(2, "LIRC data transfer started (%uus %s)\n",
43 TO_US(ev.duration), TO_STR(ev.pulse));
45 ir_dev->raw->lirc.lircdata += ev.duration / 1000;
46 if (ev.pulse)
47 ir_dev->raw->lirc.lircdata |= PULSE_BIT;
49 lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
50 (unsigned char *) &ir_dev->raw->lirc.lircdata);
51 wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
53 ir_dev->raw->lirc.lircdata = 0;
55 return 0;
58 static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
59 size_t n, loff_t *ppos)
61 struct lirc_codec *lirc;
62 struct ir_input_dev *ir_dev;
63 int *txbuf; /* buffer with values to transmit */
64 int ret = 0, count;
66 lirc = lirc_get_pdata(file);
67 if (!lirc)
68 return -EFAULT;
70 if (n % sizeof(int))
71 return -EINVAL;
73 count = n / sizeof(int);
74 if (count > LIRCBUF_SIZE || count % 2 == 0)
75 return -EINVAL;
77 txbuf = kzalloc(sizeof(int) * LIRCBUF_SIZE, GFP_KERNEL);
78 if (!txbuf)
79 return -ENOMEM;
81 if (copy_from_user(txbuf, buf, n)) {
82 ret = -EFAULT;
83 goto out;
86 ir_dev = lirc->ir_dev;
87 if (!ir_dev) {
88 ret = -EFAULT;
89 goto out;
92 if (ir_dev->props && ir_dev->props->tx_ir)
93 ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n);
95 out:
96 kfree(txbuf);
97 return ret;
100 static long ir_lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
102 struct lirc_codec *lirc;
103 struct ir_input_dev *ir_dev;
104 int ret = 0;
105 void *drv_data;
106 unsigned long val;
108 lirc = lirc_get_pdata(filep);
109 if (!lirc)
110 return -EFAULT;
112 ir_dev = lirc->ir_dev;
113 if (!ir_dev || !ir_dev->props || !ir_dev->props->priv)
114 return -EFAULT;
116 drv_data = ir_dev->props->priv;
118 switch (cmd) {
119 case LIRC_SET_TRANSMITTER_MASK:
120 ret = get_user(val, (unsigned long *)arg);
121 if (ret)
122 return ret;
124 if (ir_dev->props && ir_dev->props->s_tx_mask)
125 ret = ir_dev->props->s_tx_mask(drv_data, (u32)val);
126 else
127 return -EINVAL;
128 break;
130 case LIRC_SET_SEND_CARRIER:
131 ret = get_user(val, (unsigned long *)arg);
132 if (ret)
133 return ret;
135 if (ir_dev->props && ir_dev->props->s_tx_carrier)
136 ir_dev->props->s_tx_carrier(drv_data, (u32)val);
137 else
138 return -EINVAL;
139 break;
141 case LIRC_GET_SEND_MODE:
142 val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
143 ret = put_user(val, (unsigned long *)arg);
144 break;
146 case LIRC_SET_SEND_MODE:
147 ret = get_user(val, (unsigned long *)arg);
148 if (ret)
149 return ret;
151 if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
152 return -EINVAL;
153 break;
155 default:
156 return lirc_dev_fop_ioctl(filep, cmd, arg);
159 return ret;
162 static int ir_lirc_open(void *data)
164 return 0;
167 static void ir_lirc_close(void *data)
169 return;
172 static struct file_operations lirc_fops = {
173 .owner = THIS_MODULE,
174 .write = ir_lirc_transmit_ir,
175 .unlocked_ioctl = ir_lirc_ioctl,
176 .read = lirc_dev_fop_read,
177 .poll = lirc_dev_fop_poll,
178 .open = lirc_dev_fop_open,
179 .release = lirc_dev_fop_close,
182 static int ir_lirc_register(struct input_dev *input_dev)
184 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
185 struct lirc_driver *drv;
186 struct lirc_buffer *rbuf;
187 int rc = -ENOMEM;
188 unsigned long features;
190 drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
191 if (!drv)
192 return rc;
194 rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
195 if (!drv)
196 goto rbuf_alloc_failed;
198 rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
199 if (rc)
200 goto rbuf_init_failed;
202 features = LIRC_CAN_REC_MODE2;
203 if (ir_dev->props->tx_ir) {
204 features |= LIRC_CAN_SEND_PULSE;
205 if (ir_dev->props->s_tx_mask)
206 features |= LIRC_CAN_SET_TRANSMITTER_MASK;
207 if (ir_dev->props->s_tx_carrier)
208 features |= LIRC_CAN_SET_SEND_CARRIER;
211 snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
212 ir_dev->driver_name);
213 drv->minor = -1;
214 drv->features = features;
215 drv->data = &ir_dev->raw->lirc;
216 drv->rbuf = rbuf;
217 drv->set_use_inc = &ir_lirc_open;
218 drv->set_use_dec = &ir_lirc_close;
219 drv->code_length = sizeof(struct ir_raw_event) * 8;
220 drv->fops = &lirc_fops;
221 drv->dev = &ir_dev->dev;
222 drv->owner = THIS_MODULE;
224 drv->minor = lirc_register_driver(drv);
225 if (drv->minor < 0) {
226 rc = -ENODEV;
227 goto lirc_register_failed;
230 ir_dev->raw->lirc.drv = drv;
231 ir_dev->raw->lirc.ir_dev = ir_dev;
232 ir_dev->raw->lirc.lircdata = PULSE_MASK;
234 return 0;
236 lirc_register_failed:
237 rbuf_init_failed:
238 kfree(rbuf);
239 rbuf_alloc_failed:
240 kfree(drv);
242 return rc;
245 static int ir_lirc_unregister(struct input_dev *input_dev)
247 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
248 struct lirc_codec *lirc = &ir_dev->raw->lirc;
250 lirc_unregister_driver(lirc->drv->minor);
251 lirc_buffer_free(lirc->drv->rbuf);
252 kfree(lirc->drv);
254 return 0;
257 static struct ir_raw_handler lirc_handler = {
258 .protocols = IR_TYPE_LIRC,
259 .decode = ir_lirc_decode,
260 .raw_register = ir_lirc_register,
261 .raw_unregister = ir_lirc_unregister,
264 static int __init ir_lirc_codec_init(void)
266 ir_raw_handler_register(&lirc_handler);
268 printk(KERN_INFO "IR LIRC bridge handler initialized\n");
269 return 0;
272 static void __exit ir_lirc_codec_exit(void)
274 ir_raw_handler_unregister(&lirc_handler);
277 module_init(ir_lirc_codec_init);
278 module_exit(ir_lirc_codec_exit);
280 MODULE_LICENSE("GPL");
281 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
282 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
283 MODULE_DESCRIPTION("LIRC IR handler bridge");