4 * by Artur Lipowski <alipowski@interia.pl>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/device.h>
23 #include <linux/idr.h>
24 #include <linux/poll.h>
25 #include <linux/sched.h>
26 #include <linux/wait.h>
28 #include "rc-core-priv.h"
29 #include <uapi/linux/lirc.h>
31 #define LIRCBUF_SIZE 256
33 static dev_t lirc_base_dev
;
35 /* Used to keep track of allocated lirc devices */
36 static DEFINE_IDA(lirc_ida
);
38 /* Only used for sysfs but defined to void otherwise */
39 static struct class *lirc_class
;
42 * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
44 * @dev: the struct rc_dev descriptor of the device
45 * @ev: the struct ir_raw_event descriptor of the pulse/space
47 void ir_lirc_raw_event(struct rc_dev
*dev
, struct ir_raw_event ev
)
56 * Userspace expects a long space event before the start of
57 * the signal to use as a sync. This may be done with repeat
58 * packets and normal samples. But if a reset has been sent
59 * then we assume that a long time has passed, so we send a
60 * space with the maximum time value.
62 sample
= LIRC_SPACE(LIRC_VALUE_MASK
);
63 IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
66 } else if (ev
.carrier_report
) {
67 sample
= LIRC_FREQUENCY(ev
.carrier
);
68 IR_dprintk(2, "carrier report (freq: %d)\n", sample
);
71 } else if (ev
.timeout
) {
75 dev
->gap_start
= ktime_get();
77 dev
->gap_duration
= ev
.duration
;
79 sample
= LIRC_TIMEOUT(ev
.duration
/ 1000);
80 IR_dprintk(2, "timeout report (duration: %d)\n", sample
);
85 dev
->gap_duration
+= ktime_to_ns(ktime_sub(ktime_get(),
88 /* Convert to ms and cap by LIRC_VALUE_MASK */
89 do_div(dev
->gap_duration
, 1000);
90 dev
->gap_duration
= min_t(u64
, dev
->gap_duration
,
93 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
94 list_for_each_entry(fh
, &dev
->lirc_fh
, list
)
96 LIRC_SPACE(dev
->gap_duration
));
97 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
101 sample
= ev
.pulse
? LIRC_PULSE(ev
.duration
/ 1000) :
102 LIRC_SPACE(ev
.duration
/ 1000);
103 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
104 TO_US(ev
.duration
), TO_STR(ev
.pulse
));
107 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
108 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
109 if (LIRC_IS_TIMEOUT(sample
) && !fh
->send_timeout_reports
)
111 if (kfifo_put(&fh
->rawir
, sample
))
112 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
114 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
118 * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
119 * userspace. This can be called in atomic context.
120 * @dev: the struct rc_dev descriptor of the device
121 * @lsc: the struct lirc_scancode describing the decoded scancode
123 void ir_lirc_scancode_event(struct rc_dev
*dev
, struct lirc_scancode
*lsc
)
128 lsc
->timestamp
= ktime_get_ns();
130 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
131 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
132 if (kfifo_put(&fh
->scancodes
, *lsc
))
133 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
135 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
137 EXPORT_SYMBOL_GPL(ir_lirc_scancode_event
);
139 static int ir_lirc_open(struct inode
*inode
, struct file
*file
)
141 struct rc_dev
*dev
= container_of(inode
->i_cdev
, struct rc_dev
,
143 struct lirc_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
150 get_device(&dev
->dev
);
152 if (!dev
->registered
) {
157 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
158 if (kfifo_alloc(&fh
->rawir
, MAX_IR_EVENT_SIZE
, GFP_KERNEL
)) {
164 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
165 if (kfifo_alloc(&fh
->scancodes
, 32, GFP_KERNEL
)) {
171 fh
->send_mode
= LIRC_MODE_PULSE
;
173 fh
->send_timeout_reports
= true;
175 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
176 fh
->rec_mode
= LIRC_MODE_SCANCODE
;
178 fh
->rec_mode
= LIRC_MODE_MODE2
;
180 retval
= rc_open(dev
);
184 init_waitqueue_head(&fh
->wait_poll
);
186 file
->private_data
= fh
;
187 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
188 list_add(&fh
->list
, &dev
->lirc_fh
);
189 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
191 nonseekable_open(inode
, file
);
195 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
196 kfifo_free(&fh
->scancodes
);
198 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
199 kfifo_free(&fh
->rawir
);
202 put_device(&dev
->dev
);
207 static int ir_lirc_close(struct inode
*inode
, struct file
*file
)
209 struct lirc_fh
*fh
= file
->private_data
;
210 struct rc_dev
*dev
= fh
->rc
;
213 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
215 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
217 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
218 kfifo_free(&fh
->rawir
);
219 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
220 kfifo_free(&fh
->scancodes
);
224 put_device(&dev
->dev
);
229 static ssize_t
ir_lirc_transmit_ir(struct file
*file
, const char __user
*buf
,
230 size_t n
, loff_t
*ppos
)
232 struct lirc_fh
*fh
= file
->private_data
;
233 struct rc_dev
*dev
= fh
->rc
;
235 struct ir_raw_event
*raw
= NULL
;
240 unsigned int duration
= 0; /* signal duration in us */
243 ret
= mutex_lock_interruptible(&dev
->lock
);
247 if (!dev
->registered
) {
259 if (fh
->send_mode
== LIRC_MODE_SCANCODE
) {
260 struct lirc_scancode scan
;
262 if (n
!= sizeof(scan
)) {
267 if (copy_from_user(&scan
, buf
, sizeof(scan
))) {
272 if (scan
.flags
|| scan
.keycode
|| scan
.timestamp
) {
278 * The scancode field in lirc_scancode is 64-bit simply
279 * to future-proof it, since there are IR protocols encode
280 * use more than 32 bits. For now only 32-bit protocols
283 if (scan
.scancode
> U32_MAX
||
284 !rc_validate_scancode(scan
.rc_proto
, scan
.scancode
)) {
289 raw
= kmalloc_array(LIRCBUF_SIZE
, sizeof(*raw
), GFP_KERNEL
);
295 ret
= ir_raw_encode_scancode(scan
.rc_proto
, scan
.scancode
,
302 txbuf
= kmalloc_array(count
, sizeof(unsigned int), GFP_KERNEL
);
308 for (i
= 0; i
< count
; i
++)
309 /* Convert from NS to US */
310 txbuf
[i
] = DIV_ROUND_UP(raw
[i
].duration
, 1000);
312 if (dev
->s_tx_carrier
) {
313 int carrier
= ir_raw_encode_carrier(scan
.rc_proto
);
316 dev
->s_tx_carrier(dev
, carrier
);
319 if (n
< sizeof(unsigned int) || n
% sizeof(unsigned int)) {
324 count
= n
/ sizeof(unsigned int);
325 if (count
> LIRCBUF_SIZE
|| count
% 2 == 0) {
330 txbuf
= memdup_user(buf
, n
);
332 ret
= PTR_ERR(txbuf
);
337 for (i
= 0; i
< count
; i
++) {
338 if (txbuf
[i
] > IR_MAX_DURATION
/ 1000 - duration
|| !txbuf
[i
]) {
343 duration
+= txbuf
[i
];
346 ret
= dev
->tx_ir(dev
, txbuf
, count
);
352 mutex_unlock(&dev
->lock
);
355 * The lircd gap calculation expects the write function to
356 * wait for the actual IR signal to be transmitted before
359 towait
= ktime_us_delta(ktime_add_us(start
, duration
),
362 set_current_state(TASK_INTERRUPTIBLE
);
363 schedule_timeout(usecs_to_jiffies(towait
));
372 mutex_unlock(&dev
->lock
);
376 static long ir_lirc_ioctl(struct file
*file
, unsigned int cmd
,
379 struct lirc_fh
*fh
= file
->private_data
;
380 struct rc_dev
*dev
= fh
->rc
;
381 u32 __user
*argp
= (u32 __user
*)(arg
);
385 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
386 ret
= get_user(val
, argp
);
391 ret
= mutex_lock_interruptible(&dev
->lock
);
395 if (!dev
->registered
) {
401 case LIRC_GET_FEATURES
:
402 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
403 val
|= LIRC_CAN_REC_SCANCODE
;
405 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
406 val
|= LIRC_CAN_REC_MODE2
;
407 if (dev
->rx_resolution
)
408 val
|= LIRC_CAN_GET_REC_RESOLUTION
;
412 val
|= LIRC_CAN_SEND_PULSE
;
414 val
|= LIRC_CAN_SET_TRANSMITTER_MASK
;
415 if (dev
->s_tx_carrier
)
416 val
|= LIRC_CAN_SET_SEND_CARRIER
;
417 if (dev
->s_tx_duty_cycle
)
418 val
|= LIRC_CAN_SET_SEND_DUTY_CYCLE
;
421 if (dev
->s_rx_carrier_range
)
422 val
|= LIRC_CAN_SET_REC_CARRIER
|
423 LIRC_CAN_SET_REC_CARRIER_RANGE
;
425 if (dev
->s_learning_mode
)
426 val
|= LIRC_CAN_USE_WIDEBAND_RECEIVER
;
428 if (dev
->s_carrier_report
)
429 val
|= LIRC_CAN_MEASURE_CARRIER
;
431 if (dev
->max_timeout
)
432 val
|= LIRC_CAN_SET_REC_TIMEOUT
;
437 case LIRC_GET_REC_MODE
:
438 if (dev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
444 case LIRC_SET_REC_MODE
:
445 switch (dev
->driver_type
) {
446 case RC_DRIVER_IR_RAW_TX
:
449 case RC_DRIVER_SCANCODE
:
450 if (val
!= LIRC_MODE_SCANCODE
)
453 case RC_DRIVER_IR_RAW
:
454 if (!(val
== LIRC_MODE_MODE2
||
455 val
== LIRC_MODE_SCANCODE
))
464 case LIRC_GET_SEND_MODE
:
471 case LIRC_SET_SEND_MODE
:
474 else if (!(val
== LIRC_MODE_PULSE
|| val
== LIRC_MODE_SCANCODE
))
481 case LIRC_SET_TRANSMITTER_MASK
:
485 ret
= dev
->s_tx_mask(dev
, val
);
488 case LIRC_SET_SEND_CARRIER
:
489 if (!dev
->s_tx_carrier
)
492 ret
= dev
->s_tx_carrier(dev
, val
);
495 case LIRC_SET_SEND_DUTY_CYCLE
:
496 if (!dev
->s_tx_duty_cycle
)
498 else if (val
<= 0 || val
>= 100)
501 ret
= dev
->s_tx_duty_cycle(dev
, val
);
505 case LIRC_SET_REC_CARRIER
:
506 if (!dev
->s_rx_carrier_range
)
511 ret
= dev
->s_rx_carrier_range(dev
, fh
->carrier_low
,
515 case LIRC_SET_REC_CARRIER_RANGE
:
516 if (!dev
->s_rx_carrier_range
)
521 fh
->carrier_low
= val
;
524 case LIRC_GET_REC_RESOLUTION
:
525 if (!dev
->rx_resolution
)
528 val
= dev
->rx_resolution
/ 1000;
531 case LIRC_SET_WIDEBAND_RECEIVER
:
532 if (!dev
->s_learning_mode
)
535 ret
= dev
->s_learning_mode(dev
, !!val
);
538 case LIRC_SET_MEASURE_CARRIER_MODE
:
539 if (!dev
->s_carrier_report
)
542 ret
= dev
->s_carrier_report(dev
, !!val
);
545 /* Generic timeout support */
546 case LIRC_GET_MIN_TIMEOUT
:
547 if (!dev
->max_timeout
)
550 val
= DIV_ROUND_UP(dev
->min_timeout
, 1000);
553 case LIRC_GET_MAX_TIMEOUT
:
554 if (!dev
->max_timeout
)
557 val
= dev
->max_timeout
/ 1000;
560 case LIRC_SET_REC_TIMEOUT
:
561 if (!dev
->max_timeout
) {
563 } else if (val
> U32_MAX
/ 1000) {
564 /* Check for multiply overflow */
567 u32 tmp
= val
* 1000;
569 if (tmp
< dev
->min_timeout
|| tmp
> dev
->max_timeout
)
571 else if (dev
->s_timeout
)
572 ret
= dev
->s_timeout(dev
, tmp
);
578 case LIRC_SET_REC_TIMEOUT_REPORTS
:
582 fh
->send_timeout_reports
= !!val
;
589 if (!ret
&& _IOC_DIR(cmd
) & _IOC_READ
)
590 ret
= put_user(val
, argp
);
593 mutex_unlock(&dev
->lock
);
597 static __poll_t
ir_lirc_poll(struct file
*file
, struct poll_table_struct
*wait
)
599 struct lirc_fh
*fh
= file
->private_data
;
600 struct rc_dev
*rcdev
= fh
->rc
;
603 poll_wait(file
, &fh
->wait_poll
, wait
);
605 if (!rcdev
->registered
) {
606 events
= EPOLLHUP
| EPOLLERR
;
607 } else if (rcdev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
608 if (fh
->rec_mode
== LIRC_MODE_SCANCODE
&&
609 !kfifo_is_empty(&fh
->scancodes
))
610 events
= EPOLLIN
| EPOLLRDNORM
;
612 if (fh
->rec_mode
== LIRC_MODE_MODE2
&&
613 !kfifo_is_empty(&fh
->rawir
))
614 events
= EPOLLIN
| EPOLLRDNORM
;
620 static ssize_t
ir_lirc_read_mode2(struct file
*file
, char __user
*buffer
,
623 struct lirc_fh
*fh
= file
->private_data
;
624 struct rc_dev
*rcdev
= fh
->rc
;
628 if (length
< sizeof(unsigned int) || length
% sizeof(unsigned int))
632 if (kfifo_is_empty(&fh
->rawir
)) {
633 if (file
->f_flags
& O_NONBLOCK
)
636 ret
= wait_event_interruptible(fh
->wait_poll
,
637 !kfifo_is_empty(&fh
->rawir
) ||
643 if (!rcdev
->registered
)
646 ret
= mutex_lock_interruptible(&rcdev
->lock
);
649 ret
= kfifo_to_user(&fh
->rawir
, buffer
, length
, &copied
);
650 mutex_unlock(&rcdev
->lock
);
653 } while (copied
== 0);
658 static ssize_t
ir_lirc_read_scancode(struct file
*file
, char __user
*buffer
,
661 struct lirc_fh
*fh
= file
->private_data
;
662 struct rc_dev
*rcdev
= fh
->rc
;
666 if (length
< sizeof(struct lirc_scancode
) ||
667 length
% sizeof(struct lirc_scancode
))
671 if (kfifo_is_empty(&fh
->scancodes
)) {
672 if (file
->f_flags
& O_NONBLOCK
)
675 ret
= wait_event_interruptible(fh
->wait_poll
,
676 !kfifo_is_empty(&fh
->scancodes
) ||
682 if (!rcdev
->registered
)
685 ret
= mutex_lock_interruptible(&rcdev
->lock
);
688 ret
= kfifo_to_user(&fh
->scancodes
, buffer
, length
, &copied
);
689 mutex_unlock(&rcdev
->lock
);
692 } while (copied
== 0);
697 static ssize_t
ir_lirc_read(struct file
*file
, char __user
*buffer
,
698 size_t length
, loff_t
*ppos
)
700 struct lirc_fh
*fh
= file
->private_data
;
701 struct rc_dev
*rcdev
= fh
->rc
;
703 if (rcdev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
706 if (!rcdev
->registered
)
709 if (fh
->rec_mode
== LIRC_MODE_MODE2
)
710 return ir_lirc_read_mode2(file
, buffer
, length
);
711 else /* LIRC_MODE_SCANCODE */
712 return ir_lirc_read_scancode(file
, buffer
, length
);
715 static const struct file_operations lirc_fops
= {
716 .owner
= THIS_MODULE
,
717 .write
= ir_lirc_transmit_ir
,
718 .unlocked_ioctl
= ir_lirc_ioctl
,
720 .compat_ioctl
= ir_lirc_ioctl
,
722 .read
= ir_lirc_read
,
723 .poll
= ir_lirc_poll
,
724 .open
= ir_lirc_open
,
725 .release
= ir_lirc_close
,
729 static void lirc_release_device(struct device
*ld
)
731 struct rc_dev
*rcdev
= container_of(ld
, struct rc_dev
, lirc_dev
);
733 put_device(&rcdev
->dev
);
736 int ir_lirc_register(struct rc_dev
*dev
)
740 minor
= ida_simple_get(&lirc_ida
, 0, RC_DEV_MAX
, GFP_KERNEL
);
744 device_initialize(&dev
->lirc_dev
);
745 dev
->lirc_dev
.class = lirc_class
;
746 dev
->lirc_dev
.parent
= &dev
->dev
;
747 dev
->lirc_dev
.release
= lirc_release_device
;
748 dev
->lirc_dev
.devt
= MKDEV(MAJOR(lirc_base_dev
), minor
);
749 dev_set_name(&dev
->lirc_dev
, "lirc%d", minor
);
751 INIT_LIST_HEAD(&dev
->lirc_fh
);
752 spin_lock_init(&dev
->lirc_fh_lock
);
754 cdev_init(&dev
->lirc_cdev
, &lirc_fops
);
756 err
= cdev_device_add(&dev
->lirc_cdev
, &dev
->lirc_dev
);
760 get_device(&dev
->dev
);
762 dev_info(&dev
->dev
, "lirc_dev: driver %s registered at minor = %d",
763 dev
->driver_name
, minor
);
768 ida_simple_remove(&lirc_ida
, minor
);
772 void ir_lirc_unregister(struct rc_dev
*dev
)
777 dev_dbg(&dev
->dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
778 dev
->driver_name
, MINOR(dev
->lirc_dev
.devt
));
780 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
781 list_for_each_entry(fh
, &dev
->lirc_fh
, list
)
782 wake_up_poll(&fh
->wait_poll
, EPOLLHUP
| EPOLLERR
);
783 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
785 cdev_device_del(&dev
->lirc_cdev
, &dev
->lirc_dev
);
786 ida_simple_remove(&lirc_ida
, MINOR(dev
->lirc_dev
.devt
));
789 int __init
lirc_dev_init(void)
793 lirc_class
= class_create(THIS_MODULE
, "lirc");
794 if (IS_ERR(lirc_class
)) {
795 pr_err("class_create failed\n");
796 return PTR_ERR(lirc_class
);
799 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, RC_DEV_MAX
,
802 class_destroy(lirc_class
);
803 pr_err("alloc_chrdev_region failed\n");
807 pr_info("IR Remote Control driver registered, major %d\n",
808 MAJOR(lirc_base_dev
));
813 void __exit
lirc_dev_exit(void)
815 class_destroy(lirc_class
);
816 unregister_chrdev_region(lirc_base_dev
, RC_DEV_MAX
);
819 MODULE_ALIAS("lirc_dev");