1 /* ir-lirc-codec.c - ir-core to classic lirc interface bridge
3 * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
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.
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.
15 #include <linux/sched.h>
16 #include <linux/wait.h>
17 #include <media/lirc.h>
18 #include <media/lirc_dev.h>
19 #include <media/rc-core.h>
20 #include "rc-core-priv.h"
22 #define LIRCBUF_SIZE 256
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 rc_dev descriptor of the device
28 * @duration: the struct ir_raw_event descriptor of the pulse/space
30 * This function returns -EINVAL if the lirc interfaces aren't wired up.
32 static int ir_lirc_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
34 struct lirc_codec
*lirc
= &dev
->raw
->lirc
;
37 if (!(dev
->raw
->enabled_protocols
& RC_TYPE_LIRC
))
40 if (!dev
->raw
->lirc
.drv
|| !dev
->raw
->lirc
.drv
->rbuf
)
48 if (ev
.carrier_report
) {
49 sample
= LIRC_FREQUENCY(ev
.carrier
);
52 } else if (ev
.timeout
) {
57 lirc
->gap_start
= ktime_get();
59 lirc
->gap_duration
= ev
.duration
;
61 if (!lirc
->send_timeout_reports
)
64 sample
= LIRC_TIMEOUT(ev
.duration
/ 1000);
72 lirc
->gap_duration
+= ktime_to_ns(ktime_sub(ktime_get(),
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
);
80 gap_sample
= LIRC_SPACE(lirc
->gap_duration
);
81 lirc_buffer_write(dev
->raw
->lirc
.drv
->rbuf
,
82 (unsigned char *) &gap_sample
);
86 sample
= ev
.pulse
? LIRC_PULSE(ev
.duration
/ 1000) :
87 LIRC_SPACE(ev
.duration
/ 1000);
90 lirc_buffer_write(dev
->raw
->lirc
.drv
->rbuf
,
91 (unsigned char *) &sample
);
92 wake_up(&dev
->raw
->lirc
.drv
->rbuf
->wait_poll
);
97 static ssize_t
ir_lirc_transmit_ir(struct file
*file
, const char *buf
,
98 size_t n
, loff_t
*ppos
)
100 struct lirc_codec
*lirc
;
102 int *txbuf
; /* buffer with values to transmit */
106 lirc
= lirc_get_pdata(file
);
113 count
= n
/ sizeof(int);
114 if (count
> LIRCBUF_SIZE
|| count
% 2 == 0 || n
% sizeof(int) != 0)
117 txbuf
= memdup_user(buf
, n
);
119 return PTR_ERR(txbuf
);
128 ret
= dev
->tx_ir(dev
, txbuf
, (u32
)n
);
135 static long ir_lirc_ioctl(struct file
*filep
, unsigned int cmd
,
136 unsigned long __user arg
)
138 struct lirc_codec
*lirc
;
143 lirc
= lirc_get_pdata(filep
);
151 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
152 ret
= get_user(val
, (__u32
*)arg
);
160 case LIRC_GET_SEND_MODE
:
161 val
= LIRC_CAN_SEND_PULSE
& LIRC_CAN_SEND_MASK
;
164 case LIRC_SET_SEND_MODE
:
165 if (val
!= (LIRC_MODE_PULSE
& LIRC_CAN_SEND_MASK
))
170 case LIRC_SET_TRANSMITTER_MASK
:
174 return dev
->s_tx_mask(dev
, val
);
176 case LIRC_SET_SEND_CARRIER
:
177 if (!dev
->s_tx_carrier
)
180 return dev
->s_tx_carrier(dev
, val
);
182 case LIRC_SET_SEND_DUTY_CYCLE
:
183 if (!dev
->s_tx_duty_cycle
)
186 if (val
<= 0 || val
>= 100)
189 return dev
->s_tx_duty_cycle(dev
, val
);
192 case LIRC_SET_REC_CARRIER
:
193 if (!dev
->s_rx_carrier_range
)
199 return dev
->s_rx_carrier_range(dev
,
200 dev
->raw
->lirc
.carrier_low
,
203 case LIRC_SET_REC_CARRIER_RANGE
:
207 dev
->raw
->lirc
.carrier_low
= val
;
210 case LIRC_GET_REC_RESOLUTION
:
211 val
= dev
->rx_resolution
;
214 case LIRC_SET_WIDEBAND_RECEIVER
:
215 if (!dev
->s_learning_mode
)
218 return dev
->s_learning_mode(dev
, !!val
);
220 case LIRC_SET_MEASURE_CARRIER_MODE
:
221 if (!dev
->s_carrier_report
)
224 return dev
->s_carrier_report(dev
, !!val
);
226 /* Generic timeout support */
227 case LIRC_GET_MIN_TIMEOUT
:
228 if (!dev
->max_timeout
)
230 val
= dev
->min_timeout
/ 1000;
233 case LIRC_GET_MAX_TIMEOUT
:
234 if (!dev
->max_timeout
)
236 val
= dev
->max_timeout
/ 1000;
239 case LIRC_SET_REC_TIMEOUT
:
240 if (!dev
->max_timeout
)
245 if (tmp
< dev
->min_timeout
||
246 tmp
> dev
->max_timeout
)
252 case LIRC_SET_REC_TIMEOUT_REPORTS
:
253 lirc
->send_timeout_reports
= !!val
;
257 return lirc_dev_fop_ioctl(filep
, cmd
, arg
);
260 if (_IOC_DIR(cmd
) & _IOC_READ
)
261 ret
= put_user(val
, (__u32
*)arg
);
266 static int ir_lirc_open(void *data
)
271 static void ir_lirc_close(void *data
)
276 static struct file_operations lirc_fops
= {
277 .owner
= THIS_MODULE
,
278 .write
= ir_lirc_transmit_ir
,
279 .unlocked_ioctl
= ir_lirc_ioctl
,
281 .compat_ioctl
= ir_lirc_ioctl
,
283 .read
= lirc_dev_fop_read
,
284 .poll
= lirc_dev_fop_poll
,
285 .open
= lirc_dev_fop_open
,
286 .release
= lirc_dev_fop_close
,
290 static int ir_lirc_register(struct rc_dev
*dev
)
292 struct lirc_driver
*drv
;
293 struct lirc_buffer
*rbuf
;
295 unsigned long features
;
297 drv
= kzalloc(sizeof(struct lirc_driver
), GFP_KERNEL
);
301 rbuf
= kzalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
303 goto rbuf_alloc_failed
;
305 rc
= lirc_buffer_init(rbuf
, sizeof(int), LIRCBUF_SIZE
);
307 goto rbuf_init_failed
;
309 features
= LIRC_CAN_REC_MODE2
;
311 features
|= LIRC_CAN_SEND_PULSE
;
313 features
|= LIRC_CAN_SET_TRANSMITTER_MASK
;
314 if (dev
->s_tx_carrier
)
315 features
|= LIRC_CAN_SET_SEND_CARRIER
;
316 if (dev
->s_tx_duty_cycle
)
317 features
|= LIRC_CAN_SET_SEND_DUTY_CYCLE
;
320 if (dev
->s_rx_carrier_range
)
321 features
|= LIRC_CAN_SET_REC_CARRIER
|
322 LIRC_CAN_SET_REC_CARRIER_RANGE
;
324 if (dev
->s_learning_mode
)
325 features
|= LIRC_CAN_USE_WIDEBAND_RECEIVER
;
327 if (dev
->s_carrier_report
)
328 features
|= LIRC_CAN_MEASURE_CARRIER
;
330 if (dev
->max_timeout
)
331 features
|= LIRC_CAN_SET_REC_TIMEOUT
;
333 snprintf(drv
->name
, sizeof(drv
->name
), "ir-lirc-codec (%s)",
336 drv
->features
= features
;
337 drv
->data
= &dev
->raw
->lirc
;
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
;
343 drv
->dev
= &dev
->dev
;
344 drv
->owner
= THIS_MODULE
;
346 drv
->minor
= lirc_register_driver(drv
);
347 if (drv
->minor
< 0) {
349 goto lirc_register_failed
;
352 dev
->raw
->lirc
.drv
= drv
;
353 dev
->raw
->lirc
.dev
= dev
;
356 lirc_register_failed
:
365 static int ir_lirc_unregister(struct rc_dev
*dev
)
367 struct lirc_codec
*lirc
= &dev
->raw
->lirc
;
369 lirc_unregister_driver(lirc
->drv
->minor
);
370 lirc_buffer_free(lirc
->drv
->rbuf
);
376 static struct ir_raw_handler lirc_handler
= {
377 .protocols
= RC_TYPE_LIRC
,
378 .decode
= ir_lirc_decode
,
379 .raw_register
= ir_lirc_register
,
380 .raw_unregister
= ir_lirc_unregister
,
383 static int __init
ir_lirc_codec_init(void)
385 ir_raw_handler_register(&lirc_handler
);
387 printk(KERN_INFO
"IR LIRC bridge handler initialized\n");
391 static void __exit
ir_lirc_codec_exit(void)
393 ir_raw_handler_unregister(&lirc_handler
);
396 module_init(ir_lirc_codec_init
);
397 module_exit(ir_lirc_codec_exit
);
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");