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