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