Commit | Line | Data |
---|---|---|
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> | |
18 | #include <media/ir-core.h> | |
19 | #include "ir-core-priv.h" | |
20 | #include "lirc_dev.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 | ||
36 | if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC)) | |
37 | return 0; | |
38 | ||
39 | if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf) | |
40 | return -EINVAL; | |
41 | ||
42 | IR_dprintk(2, "LIRC data transfer started (%uus %s)\n", | |
43 | TO_US(ev.duration), TO_STR(ev.pulse)); | |
44 | ||
45 | ir_dev->raw->lirc.lircdata += ev.duration / 1000; | |
46 | if (ev.pulse) | |
47 | ir_dev->raw->lirc.lircdata |= PULSE_BIT; | |
48 | ||
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); | |
52 | ||
53 | ir_dev->raw->lirc.lircdata = 0; | |
54 | ||
55 | return 0; | |
56 | } | |
57 | ||
58 | static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf, | |
59 | size_t n, loff_t *ppos) | |
60 | { | |
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; | |
65 | ||
66 | lirc = lirc_get_pdata(file); | |
67 | if (!lirc) | |
68 | return -EFAULT; | |
69 | ||
70 | if (n % sizeof(int)) | |
71 | return -EINVAL; | |
72 | ||
73 | count = n / sizeof(int); | |
74 | if (count > LIRCBUF_SIZE || count % 2 == 0) | |
75 | return -EINVAL; | |
76 | ||
77 | txbuf = kzalloc(sizeof(int) * LIRCBUF_SIZE, GFP_KERNEL); | |
78 | if (!txbuf) | |
79 | return -ENOMEM; | |
80 | ||
81 | if (copy_from_user(txbuf, buf, n)) { | |
82 | ret = -EFAULT; | |
83 | goto out; | |
84 | } | |
85 | ||
86 | ir_dev = lirc->ir_dev; | |
87 | if (!ir_dev) { | |
88 | ret = -EFAULT; | |
89 | goto out; | |
90 | } | |
91 | ||
92 | if (ir_dev->props && ir_dev->props->tx_ir) | |
93 | ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n); | |
94 | ||
95 | out: | |
96 | kfree(txbuf); | |
97 | return ret; | |
98 | } | |
99 | ||
044e5878 | 100 | static long ir_lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
ca414698 JW |
101 | { |
102 | struct lirc_codec *lirc; | |
103 | struct ir_input_dev *ir_dev; | |
104 | int ret = 0; | |
105 | void *drv_data; | |
106 | unsigned long val; | |
107 | ||
108 | lirc = lirc_get_pdata(filep); | |
109 | if (!lirc) | |
110 | return -EFAULT; | |
111 | ||
112 | ir_dev = lirc->ir_dev; | |
113 | if (!ir_dev || !ir_dev->props || !ir_dev->props->priv) | |
114 | return -EFAULT; | |
115 | ||
116 | drv_data = ir_dev->props->priv; | |
117 | ||
118 | switch (cmd) { | |
119 | case LIRC_SET_TRANSMITTER_MASK: | |
120 | ret = get_user(val, (unsigned long *)arg); | |
121 | if (ret) | |
122 | return ret; | |
123 | ||
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; | |
129 | ||
130 | case LIRC_SET_SEND_CARRIER: | |
131 | ret = get_user(val, (unsigned long *)arg); | |
132 | if (ret) | |
133 | return ret; | |
134 | ||
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; | |
140 | ||
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; | |
145 | ||
146 | case LIRC_SET_SEND_MODE: | |
147 | ret = get_user(val, (unsigned long *)arg); | |
148 | if (ret) | |
149 | return ret; | |
150 | ||
151 | if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK)) | |
152 | return -EINVAL; | |
153 | break; | |
154 | ||
155 | default: | |
044e5878 | 156 | return lirc_dev_fop_ioctl(filep, cmd, arg); |
ca414698 JW |
157 | } |
158 | ||
159 | return ret; | |
160 | } | |
161 | ||
162 | static int ir_lirc_open(void *data) | |
163 | { | |
164 | return 0; | |
165 | } | |
166 | ||
167 | static void ir_lirc_close(void *data) | |
168 | { | |
169 | return; | |
170 | } | |
171 | ||
172 | static struct file_operations lirc_fops = { | |
173 | .owner = THIS_MODULE, | |
174 | .write = ir_lirc_transmit_ir, | |
044e5878 | 175 | .unlocked_ioctl = ir_lirc_ioctl, |
ca414698 JW |
176 | .read = lirc_dev_fop_read, |
177 | .poll = lirc_dev_fop_poll, | |
178 | .open = lirc_dev_fop_open, | |
179 | .release = lirc_dev_fop_close, | |
180 | }; | |
181 | ||
182 | static int ir_lirc_register(struct input_dev *input_dev) | |
183 | { | |
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; | |
189 | ||
190 | drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL); | |
191 | if (!drv) | |
192 | return rc; | |
193 | ||
194 | rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL); | |
195 | if (!drv) | |
196 | goto rbuf_alloc_failed; | |
197 | ||
198 | rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE); | |
199 | if (rc) | |
200 | goto rbuf_init_failed; | |
201 | ||
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; | |
209 | } | |
210 | ||
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; | |
223 | ||
224 | drv->minor = lirc_register_driver(drv); | |
225 | if (drv->minor < 0) { | |
226 | rc = -ENODEV; | |
227 | goto lirc_register_failed; | |
228 | } | |
229 | ||
230 | ir_dev->raw->lirc.drv = drv; | |
231 | ir_dev->raw->lirc.ir_dev = ir_dev; | |
232 | ir_dev->raw->lirc.lircdata = PULSE_MASK; | |
233 | ||
234 | return 0; | |
235 | ||
236 | lirc_register_failed: | |
237 | rbuf_init_failed: | |
238 | kfree(rbuf); | |
239 | rbuf_alloc_failed: | |
240 | kfree(drv); | |
241 | ||
242 | return rc; | |
243 | } | |
244 | ||
245 | static int ir_lirc_unregister(struct input_dev *input_dev) | |
246 | { | |
247 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); | |
248 | struct lirc_codec *lirc = &ir_dev->raw->lirc; | |
249 | ||
250 | lirc_unregister_driver(lirc->drv->minor); | |
251 | lirc_buffer_free(lirc->drv->rbuf); | |
252 | kfree(lirc->drv); | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
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, | |
262 | }; | |
263 | ||
264 | static int __init ir_lirc_codec_init(void) | |
265 | { | |
266 | ir_raw_handler_register(&lirc_handler); | |
267 | ||
268 | printk(KERN_INFO "IR LIRC bridge handler initialized\n"); | |
269 | return 0; | |
270 | } | |
271 | ||
272 | static void __exit ir_lirc_codec_exit(void) | |
273 | { | |
274 | ir_raw_handler_unregister(&lirc_handler); | |
275 | } | |
276 | ||
277 | module_init(ir_lirc_codec_init); | |
278 | module_exit(ir_lirc_codec_exit); | |
279 | ||
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"); |