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