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