1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * by Artur Lipowski <alipowski@interia.pl>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/file.h>
14 #include <linux/idr.h>
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/wait.h>
19 #include "rc-core-priv.h"
20 #include <uapi/linux/lirc.h>
22 #define LIRCBUF_SIZE 1024
24 static dev_t lirc_base_dev
;
26 /* Used to keep track of allocated lirc devices */
27 static DEFINE_IDA(lirc_ida
);
29 /* Only used for sysfs but defined to void otherwise */
30 static struct class *lirc_class
;
33 * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
35 * @dev: the struct rc_dev descriptor of the device
36 * @ev: the struct ir_raw_event descriptor of the pulse/space
38 void ir_lirc_raw_event(struct rc_dev
*dev
, struct ir_raw_event ev
)
47 * Userspace expects a long space event before the start of
48 * the signal to use as a sync. This may be done with repeat
49 * packets and normal samples. But if a reset has been sent
50 * then we assume that a long time has passed, so we send a
51 * space with the maximum time value.
53 sample
= LIRC_SPACE(LIRC_VALUE_MASK
);
54 dev_dbg(&dev
->dev
, "delivering reset sync space to lirc_dev\n");
57 } else if (ev
.carrier_report
) {
58 sample
= LIRC_FREQUENCY(ev
.carrier
);
59 dev_dbg(&dev
->dev
, "carrier report (freq: %d)\n", sample
);
62 } else if (ev
.timeout
) {
66 dev
->gap_start
= ktime_get();
68 dev
->gap_duration
= ev
.duration
;
70 sample
= LIRC_TIMEOUT(ev
.duration
/ 1000);
71 dev_dbg(&dev
->dev
, "timeout report (duration: %d)\n", sample
);
76 dev
->gap_duration
+= ktime_to_ns(ktime_sub(ktime_get(),
79 /* Convert to ms and cap by LIRC_VALUE_MASK */
80 do_div(dev
->gap_duration
, 1000);
81 dev
->gap_duration
= min_t(u64
, dev
->gap_duration
,
84 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
85 list_for_each_entry(fh
, &dev
->lirc_fh
, list
)
87 LIRC_SPACE(dev
->gap_duration
));
88 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
92 sample
= ev
.pulse
? LIRC_PULSE(ev
.duration
/ 1000) :
93 LIRC_SPACE(ev
.duration
/ 1000);
94 dev_dbg(&dev
->dev
, "delivering %uus %s to lirc_dev\n",
95 TO_US(ev
.duration
), TO_STR(ev
.pulse
));
99 * bpf does not care about the gap generated above; that exists
100 * for backwards compatibility
102 lirc_bpf_run(dev
, sample
);
104 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
105 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
106 if (LIRC_IS_TIMEOUT(sample
) && !fh
->send_timeout_reports
)
108 if (kfifo_put(&fh
->rawir
, sample
))
109 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
111 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
115 * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
116 * userspace. This can be called in atomic context.
117 * @dev: the struct rc_dev descriptor of the device
118 * @lsc: the struct lirc_scancode describing the decoded scancode
120 void ir_lirc_scancode_event(struct rc_dev
*dev
, struct lirc_scancode
*lsc
)
125 lsc
->timestamp
= ktime_get_ns();
127 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
128 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
129 if (kfifo_put(&fh
->scancodes
, *lsc
))
130 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
132 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
134 EXPORT_SYMBOL_GPL(ir_lirc_scancode_event
);
136 static int ir_lirc_open(struct inode
*inode
, struct file
*file
)
138 struct rc_dev
*dev
= container_of(inode
->i_cdev
, struct rc_dev
,
140 struct lirc_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
147 get_device(&dev
->dev
);
149 if (!dev
->registered
) {
154 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
155 if (kfifo_alloc(&fh
->rawir
, MAX_IR_EVENT_SIZE
, GFP_KERNEL
)) {
161 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
162 if (kfifo_alloc(&fh
->scancodes
, 32, GFP_KERNEL
)) {
168 fh
->send_mode
= LIRC_MODE_PULSE
;
170 fh
->send_timeout_reports
= true;
172 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
173 fh
->rec_mode
= LIRC_MODE_SCANCODE
;
175 fh
->rec_mode
= LIRC_MODE_MODE2
;
177 retval
= rc_open(dev
);
181 init_waitqueue_head(&fh
->wait_poll
);
183 file
->private_data
= fh
;
184 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
185 list_add(&fh
->list
, &dev
->lirc_fh
);
186 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
188 stream_open(inode
, file
);
192 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
193 kfifo_free(&fh
->scancodes
);
195 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
196 kfifo_free(&fh
->rawir
);
199 put_device(&dev
->dev
);
204 static int ir_lirc_close(struct inode
*inode
, struct file
*file
)
206 struct lirc_fh
*fh
= file
->private_data
;
207 struct rc_dev
*dev
= fh
->rc
;
210 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
212 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
214 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
215 kfifo_free(&fh
->rawir
);
216 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
217 kfifo_free(&fh
->scancodes
);
221 put_device(&dev
->dev
);
226 static ssize_t
ir_lirc_transmit_ir(struct file
*file
, const char __user
*buf
,
227 size_t n
, loff_t
*ppos
)
229 struct lirc_fh
*fh
= file
->private_data
;
230 struct rc_dev
*dev
= fh
->rc
;
232 struct ir_raw_event
*raw
= NULL
;
237 unsigned int duration
= 0; /* signal duration in us */
240 ret
= mutex_lock_interruptible(&dev
->lock
);
244 if (!dev
->registered
) {
254 if (fh
->send_mode
== LIRC_MODE_SCANCODE
) {
255 struct lirc_scancode scan
;
257 if (n
!= sizeof(scan
)) {
262 if (copy_from_user(&scan
, buf
, sizeof(scan
))) {
267 if (scan
.flags
|| scan
.keycode
|| scan
.timestamp
) {
273 * The scancode field in lirc_scancode is 64-bit simply
274 * to future-proof it, since there are IR protocols encode
275 * use more than 32 bits. For now only 32-bit protocols
278 if (scan
.scancode
> U32_MAX
||
279 !rc_validate_scancode(scan
.rc_proto
, scan
.scancode
)) {
284 raw
= kmalloc_array(LIRCBUF_SIZE
, sizeof(*raw
), GFP_KERNEL
);
290 ret
= ir_raw_encode_scancode(scan
.rc_proto
, scan
.scancode
,
297 txbuf
= kmalloc_array(count
, sizeof(unsigned int), GFP_KERNEL
);
303 for (i
= 0; i
< count
; i
++)
304 /* Convert from NS to US */
305 txbuf
[i
] = DIV_ROUND_UP(raw
[i
].duration
, 1000);
307 if (dev
->s_tx_carrier
) {
308 int carrier
= ir_raw_encode_carrier(scan
.rc_proto
);
311 dev
->s_tx_carrier(dev
, carrier
);
314 if (n
< sizeof(unsigned int) || n
% sizeof(unsigned int)) {
319 count
= n
/ sizeof(unsigned int);
320 if (count
> LIRCBUF_SIZE
|| count
% 2 == 0) {
325 txbuf
= memdup_user(buf
, n
);
327 ret
= PTR_ERR(txbuf
);
332 for (i
= 0; i
< count
; i
++) {
333 if (txbuf
[i
] > IR_MAX_DURATION
/ 1000 - duration
|| !txbuf
[i
]) {
338 duration
+= txbuf
[i
];
343 ret
= dev
->tx_ir(dev
, txbuf
, count
);
349 mutex_unlock(&dev
->lock
);
352 * The lircd gap calculation expects the write function to
353 * wait for the actual IR signal to be transmitted before
356 towait
= ktime_us_delta(ktime_add_us(start
, duration
),
359 set_current_state(TASK_INTERRUPTIBLE
);
360 schedule_timeout(usecs_to_jiffies(towait
));
369 mutex_unlock(&dev
->lock
);
373 static long ir_lirc_ioctl(struct file
*file
, unsigned int cmd
,
376 struct lirc_fh
*fh
= file
->private_data
;
377 struct rc_dev
*dev
= fh
->rc
;
378 u32 __user
*argp
= (u32 __user
*)(arg
);
382 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
383 ret
= get_user(val
, argp
);
388 ret
= mutex_lock_interruptible(&dev
->lock
);
392 if (!dev
->registered
) {
398 case LIRC_GET_FEATURES
:
399 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
400 val
|= LIRC_CAN_REC_SCANCODE
;
402 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
403 val
|= LIRC_CAN_REC_MODE2
;
404 if (dev
->rx_resolution
)
405 val
|= LIRC_CAN_GET_REC_RESOLUTION
;
409 val
|= LIRC_CAN_SEND_PULSE
;
411 val
|= LIRC_CAN_SET_TRANSMITTER_MASK
;
412 if (dev
->s_tx_carrier
)
413 val
|= LIRC_CAN_SET_SEND_CARRIER
;
414 if (dev
->s_tx_duty_cycle
)
415 val
|= LIRC_CAN_SET_SEND_DUTY_CYCLE
;
418 if (dev
->s_rx_carrier_range
)
419 val
|= LIRC_CAN_SET_REC_CARRIER
|
420 LIRC_CAN_SET_REC_CARRIER_RANGE
;
422 if (dev
->s_learning_mode
)
423 val
|= LIRC_CAN_USE_WIDEBAND_RECEIVER
;
425 if (dev
->s_carrier_report
)
426 val
|= LIRC_CAN_MEASURE_CARRIER
;
428 if (dev
->max_timeout
)
429 val
|= LIRC_CAN_SET_REC_TIMEOUT
;
434 case LIRC_GET_REC_MODE
:
435 if (dev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
441 case LIRC_SET_REC_MODE
:
442 switch (dev
->driver_type
) {
443 case RC_DRIVER_IR_RAW_TX
:
446 case RC_DRIVER_SCANCODE
:
447 if (val
!= LIRC_MODE_SCANCODE
)
450 case RC_DRIVER_IR_RAW
:
451 if (!(val
== LIRC_MODE_MODE2
||
452 val
== LIRC_MODE_SCANCODE
))
461 case LIRC_GET_SEND_MODE
:
468 case LIRC_SET_SEND_MODE
:
471 else if (!(val
== LIRC_MODE_PULSE
|| val
== LIRC_MODE_SCANCODE
))
478 case LIRC_SET_TRANSMITTER_MASK
:
482 ret
= dev
->s_tx_mask(dev
, val
);
485 case LIRC_SET_SEND_CARRIER
:
486 if (!dev
->s_tx_carrier
)
489 ret
= dev
->s_tx_carrier(dev
, val
);
492 case LIRC_SET_SEND_DUTY_CYCLE
:
493 if (!dev
->s_tx_duty_cycle
)
495 else if (val
<= 0 || val
>= 100)
498 ret
= dev
->s_tx_duty_cycle(dev
, val
);
502 case LIRC_SET_REC_CARRIER
:
503 if (!dev
->s_rx_carrier_range
)
508 ret
= dev
->s_rx_carrier_range(dev
, fh
->carrier_low
,
512 case LIRC_SET_REC_CARRIER_RANGE
:
513 if (!dev
->s_rx_carrier_range
)
518 fh
->carrier_low
= val
;
521 case LIRC_GET_REC_RESOLUTION
:
522 if (!dev
->rx_resolution
)
525 val
= dev
->rx_resolution
/ 1000;
528 case LIRC_SET_WIDEBAND_RECEIVER
:
529 if (!dev
->s_learning_mode
)
532 ret
= dev
->s_learning_mode(dev
, !!val
);
535 case LIRC_SET_MEASURE_CARRIER_MODE
:
536 if (!dev
->s_carrier_report
)
539 ret
= dev
->s_carrier_report(dev
, !!val
);
542 /* Generic timeout support */
543 case LIRC_GET_MIN_TIMEOUT
:
544 if (!dev
->max_timeout
)
547 val
= DIV_ROUND_UP(dev
->min_timeout
, 1000);
550 case LIRC_GET_MAX_TIMEOUT
:
551 if (!dev
->max_timeout
)
554 val
= dev
->max_timeout
/ 1000;
557 case LIRC_SET_REC_TIMEOUT
:
558 if (!dev
->max_timeout
) {
560 } else if (val
> U32_MAX
/ 1000) {
561 /* Check for multiply overflow */
564 u32 tmp
= val
* 1000;
566 if (tmp
< dev
->min_timeout
|| tmp
> dev
->max_timeout
)
568 else if (dev
->s_timeout
)
569 ret
= dev
->s_timeout(dev
, tmp
);
575 case LIRC_GET_REC_TIMEOUT
:
579 val
= DIV_ROUND_UP(dev
->timeout
, 1000);
582 case LIRC_SET_REC_TIMEOUT_REPORTS
:
583 if (dev
->driver_type
!= RC_DRIVER_IR_RAW
)
586 fh
->send_timeout_reports
= !!val
;
593 if (!ret
&& _IOC_DIR(cmd
) & _IOC_READ
)
594 ret
= put_user(val
, argp
);
597 mutex_unlock(&dev
->lock
);
601 static __poll_t
ir_lirc_poll(struct file
*file
, struct poll_table_struct
*wait
)
603 struct lirc_fh
*fh
= file
->private_data
;
604 struct rc_dev
*rcdev
= fh
->rc
;
607 poll_wait(file
, &fh
->wait_poll
, wait
);
609 if (!rcdev
->registered
) {
610 events
= EPOLLHUP
| EPOLLERR
;
611 } else if (rcdev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
612 if (fh
->rec_mode
== LIRC_MODE_SCANCODE
&&
613 !kfifo_is_empty(&fh
->scancodes
))
614 events
= EPOLLIN
| EPOLLRDNORM
;
616 if (fh
->rec_mode
== LIRC_MODE_MODE2
&&
617 !kfifo_is_empty(&fh
->rawir
))
618 events
= EPOLLIN
| EPOLLRDNORM
;
624 static ssize_t
ir_lirc_read_mode2(struct file
*file
, char __user
*buffer
,
627 struct lirc_fh
*fh
= file
->private_data
;
628 struct rc_dev
*rcdev
= fh
->rc
;
632 if (length
< sizeof(unsigned int) || length
% sizeof(unsigned int))
636 if (kfifo_is_empty(&fh
->rawir
)) {
637 if (file
->f_flags
& O_NONBLOCK
)
640 ret
= wait_event_interruptible(fh
->wait_poll
,
641 !kfifo_is_empty(&fh
->rawir
) ||
647 if (!rcdev
->registered
)
650 ret
= mutex_lock_interruptible(&rcdev
->lock
);
653 ret
= kfifo_to_user(&fh
->rawir
, buffer
, length
, &copied
);
654 mutex_unlock(&rcdev
->lock
);
657 } while (copied
== 0);
662 static ssize_t
ir_lirc_read_scancode(struct file
*file
, char __user
*buffer
,
665 struct lirc_fh
*fh
= file
->private_data
;
666 struct rc_dev
*rcdev
= fh
->rc
;
670 if (length
< sizeof(struct lirc_scancode
) ||
671 length
% sizeof(struct lirc_scancode
))
675 if (kfifo_is_empty(&fh
->scancodes
)) {
676 if (file
->f_flags
& O_NONBLOCK
)
679 ret
= wait_event_interruptible(fh
->wait_poll
,
680 !kfifo_is_empty(&fh
->scancodes
) ||
686 if (!rcdev
->registered
)
689 ret
= mutex_lock_interruptible(&rcdev
->lock
);
692 ret
= kfifo_to_user(&fh
->scancodes
, buffer
, length
, &copied
);
693 mutex_unlock(&rcdev
->lock
);
696 } while (copied
== 0);
701 static ssize_t
ir_lirc_read(struct file
*file
, char __user
*buffer
,
702 size_t length
, loff_t
*ppos
)
704 struct lirc_fh
*fh
= file
->private_data
;
705 struct rc_dev
*rcdev
= fh
->rc
;
707 if (rcdev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
710 if (!rcdev
->registered
)
713 if (fh
->rec_mode
== LIRC_MODE_MODE2
)
714 return ir_lirc_read_mode2(file
, buffer
, length
);
715 else /* LIRC_MODE_SCANCODE */
716 return ir_lirc_read_scancode(file
, buffer
, length
);
719 static const struct file_operations lirc_fops
= {
720 .owner
= THIS_MODULE
,
721 .write
= ir_lirc_transmit_ir
,
722 .unlocked_ioctl
= ir_lirc_ioctl
,
723 .compat_ioctl
= compat_ptr_ioctl
,
724 .read
= ir_lirc_read
,
725 .poll
= ir_lirc_poll
,
726 .open
= ir_lirc_open
,
727 .release
= ir_lirc_close
,
731 static void lirc_release_device(struct device
*ld
)
733 struct rc_dev
*rcdev
= container_of(ld
, struct rc_dev
, lirc_dev
);
735 put_device(&rcdev
->dev
);
738 int ir_lirc_register(struct rc_dev
*dev
)
740 const char *rx_type
, *tx_type
;
743 minor
= ida_simple_get(&lirc_ida
, 0, RC_DEV_MAX
, GFP_KERNEL
);
747 device_initialize(&dev
->lirc_dev
);
748 dev
->lirc_dev
.class = lirc_class
;
749 dev
->lirc_dev
.parent
= &dev
->dev
;
750 dev
->lirc_dev
.release
= lirc_release_device
;
751 dev
->lirc_dev
.devt
= MKDEV(MAJOR(lirc_base_dev
), minor
);
752 dev_set_name(&dev
->lirc_dev
, "lirc%d", minor
);
754 INIT_LIST_HEAD(&dev
->lirc_fh
);
755 spin_lock_init(&dev
->lirc_fh_lock
);
757 cdev_init(&dev
->lirc_cdev
, &lirc_fops
);
759 err
= cdev_device_add(&dev
->lirc_cdev
, &dev
->lirc_dev
);
763 get_device(&dev
->dev
);
765 switch (dev
->driver_type
) {
766 case RC_DRIVER_SCANCODE
:
767 rx_type
= "scancode";
769 case RC_DRIVER_IR_RAW
:
782 dev_info(&dev
->dev
, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
783 dev
->driver_name
, minor
, rx_type
, tx_type
);
788 ida_simple_remove(&lirc_ida
, minor
);
792 void ir_lirc_unregister(struct rc_dev
*dev
)
797 dev_dbg(&dev
->dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
798 dev
->driver_name
, MINOR(dev
->lirc_dev
.devt
));
800 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
801 list_for_each_entry(fh
, &dev
->lirc_fh
, list
)
802 wake_up_poll(&fh
->wait_poll
, EPOLLHUP
| EPOLLERR
);
803 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
805 cdev_device_del(&dev
->lirc_cdev
, &dev
->lirc_dev
);
806 ida_simple_remove(&lirc_ida
, MINOR(dev
->lirc_dev
.devt
));
809 int __init
lirc_dev_init(void)
813 lirc_class
= class_create(THIS_MODULE
, "lirc");
814 if (IS_ERR(lirc_class
)) {
815 pr_err("class_create failed\n");
816 return PTR_ERR(lirc_class
);
819 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, RC_DEV_MAX
,
822 class_destroy(lirc_class
);
823 pr_err("alloc_chrdev_region failed\n");
827 pr_debug("IR Remote Control driver registered, major %d\n",
828 MAJOR(lirc_base_dev
));
833 void __exit
lirc_dev_exit(void)
835 class_destroy(lirc_class
);
836 unregister_chrdev_region(lirc_base_dev
, RC_DEV_MAX
);
839 struct rc_dev
*rc_dev_get_from_fd(int fd
)
841 struct fd f
= fdget(fd
);
846 return ERR_PTR(-EBADF
);
848 if (f
.file
->f_op
!= &lirc_fops
) {
850 return ERR_PTR(-EINVAL
);
853 fh
= f
.file
->private_data
;
856 get_device(&dev
->dev
);
862 MODULE_ALIAS("lirc_dev");