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 const struct class lirc_class
= {
35 * lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
37 * @dev: the struct rc_dev descriptor of the device
38 * @ev: the struct ir_raw_event descriptor of the pulse/space
40 void lirc_raw_event(struct rc_dev
*dev
, struct ir_raw_event ev
)
46 /* Receiver overflow, data missing */
49 * Send lirc overflow message. This message is unknown to
50 * lircd, but it will interpret this as a long space as
51 * long as the value is set to high value. This resets its
54 sample
= LIRC_OVERFLOW(LIRC_VALUE_MASK
);
55 dev_dbg(&dev
->dev
, "delivering overflow to lirc_dev\n");
58 } else if (ev
.carrier_report
) {
59 sample
= LIRC_FREQUENCY(ev
.carrier
);
60 dev_dbg(&dev
->dev
, "carrier report (freq: %d)\n", sample
);
63 } else if (ev
.timeout
) {
64 dev
->gap_start
= ktime_get();
66 sample
= LIRC_TIMEOUT(ev
.duration
);
67 dev_dbg(&dev
->dev
, "timeout report (duration: %d)\n", sample
);
72 u64 duration
= ktime_us_delta(ktime_get(),
75 /* Cap by LIRC_VALUE_MASK */
76 duration
= min_t(u64
, duration
, LIRC_VALUE_MASK
);
78 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
79 list_for_each_entry(fh
, &dev
->lirc_fh
, list
)
80 kfifo_put(&fh
->rawir
, LIRC_SPACE(duration
));
81 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
85 sample
= ev
.pulse
? LIRC_PULSE(ev
.duration
) :
86 LIRC_SPACE(ev
.duration
);
87 dev_dbg(&dev
->dev
, "delivering %uus %s to lirc_dev\n",
88 ev
.duration
, TO_STR(ev
.pulse
));
92 * bpf does not care about the gap generated above; that exists
93 * for backwards compatibility
95 lirc_bpf_run(dev
, sample
);
97 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
98 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
99 if (kfifo_put(&fh
->rawir
, sample
))
100 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
102 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
106 * lirc_scancode_event() - Send scancode data to lirc to be relayed to
107 * userspace. This can be called in atomic context.
108 * @dev: the struct rc_dev descriptor of the device
109 * @lsc: the struct lirc_scancode describing the decoded scancode
111 void lirc_scancode_event(struct rc_dev
*dev
, struct lirc_scancode
*lsc
)
116 lsc
->timestamp
= ktime_get_ns();
118 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
119 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
120 if (kfifo_put(&fh
->scancodes
, *lsc
))
121 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
123 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
125 EXPORT_SYMBOL_GPL(lirc_scancode_event
);
127 static int lirc_open(struct inode
*inode
, struct file
*file
)
129 struct rc_dev
*dev
= container_of(inode
->i_cdev
, struct rc_dev
,
131 struct lirc_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
138 get_device(&dev
->dev
);
140 if (!dev
->registered
) {
145 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
146 if (kfifo_alloc(&fh
->rawir
, MAX_IR_EVENT_SIZE
, GFP_KERNEL
)) {
152 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
153 if (kfifo_alloc(&fh
->scancodes
, 32, GFP_KERNEL
)) {
159 fh
->send_mode
= LIRC_MODE_PULSE
;
162 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
163 fh
->rec_mode
= LIRC_MODE_SCANCODE
;
165 fh
->rec_mode
= LIRC_MODE_MODE2
;
167 retval
= rc_open(dev
);
171 init_waitqueue_head(&fh
->wait_poll
);
173 file
->private_data
= fh
;
174 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
175 list_add(&fh
->list
, &dev
->lirc_fh
);
176 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
178 stream_open(inode
, file
);
182 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
183 kfifo_free(&fh
->scancodes
);
185 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
186 kfifo_free(&fh
->rawir
);
189 put_device(&dev
->dev
);
194 static int lirc_close(struct inode
*inode
, struct file
*file
)
196 struct lirc_fh
*fh
= file
->private_data
;
197 struct rc_dev
*dev
= fh
->rc
;
200 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
202 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
204 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
205 kfifo_free(&fh
->rawir
);
206 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
207 kfifo_free(&fh
->scancodes
);
211 put_device(&dev
->dev
);
216 static ssize_t
lirc_transmit(struct file
*file
, const char __user
*buf
,
217 size_t n
, loff_t
*ppos
)
219 struct lirc_fh
*fh
= file
->private_data
;
220 struct rc_dev
*dev
= fh
->rc
;
222 struct ir_raw_event
*raw
= NULL
;
227 unsigned int duration
= 0; /* signal duration in us */
230 ret
= mutex_lock_interruptible(&dev
->lock
);
234 if (!dev
->registered
) {
244 if (fh
->send_mode
== LIRC_MODE_SCANCODE
) {
245 struct lirc_scancode scan
;
247 if (n
!= sizeof(scan
)) {
252 if (copy_from_user(&scan
, buf
, sizeof(scan
))) {
257 if (scan
.flags
|| scan
.keycode
|| scan
.timestamp
||
258 scan
.rc_proto
> RC_PROTO_MAX
) {
263 /* We only have encoders for 32-bit protocols. */
264 if (scan
.scancode
> U32_MAX
||
265 !rc_validate_scancode(scan
.rc_proto
, scan
.scancode
)) {
270 raw
= kmalloc_array(LIRCBUF_SIZE
, sizeof(*raw
), GFP_KERNEL
);
276 ret
= ir_raw_encode_scancode(scan
.rc_proto
, scan
.scancode
,
281 /* drop trailing space */
287 txbuf
= kmalloc_array(count
, sizeof(unsigned int), GFP_KERNEL
);
293 for (i
= 0; i
< count
; i
++)
294 txbuf
[i
] = raw
[i
].duration
;
296 if (dev
->s_tx_carrier
) {
297 int carrier
= ir_raw_encode_carrier(scan
.rc_proto
);
300 dev
->s_tx_carrier(dev
, carrier
);
303 if (n
< sizeof(unsigned int) || n
% sizeof(unsigned int)) {
308 count
= n
/ sizeof(unsigned int);
309 if (count
> LIRCBUF_SIZE
|| count
% 2 == 0) {
314 txbuf
= memdup_user(buf
, n
);
316 ret
= PTR_ERR(txbuf
);
321 for (i
= 0; i
< count
; i
++) {
322 if (txbuf
[i
] > IR_MAX_DURATION
- duration
|| !txbuf
[i
]) {
327 duration
+= txbuf
[i
];
332 ret
= dev
->tx_ir(dev
, txbuf
, count
);
338 mutex_unlock(&dev
->lock
);
341 * The lircd gap calculation expects the write function to
342 * wait for the actual IR signal to be transmitted before
345 towait
= ktime_us_delta(ktime_add_us(start
, duration
),
348 set_current_state(TASK_INTERRUPTIBLE
);
349 schedule_timeout(usecs_to_jiffies(towait
));
358 mutex_unlock(&dev
->lock
);
362 static long lirc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
364 struct lirc_fh
*fh
= file
->private_data
;
365 struct rc_dev
*dev
= fh
->rc
;
366 u32 __user
*argp
= (u32 __user
*)(arg
);
370 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
371 ret
= get_user(val
, argp
);
376 ret
= mutex_lock_interruptible(&dev
->lock
);
380 if (!dev
->registered
) {
386 case LIRC_GET_FEATURES
:
387 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
388 val
|= LIRC_CAN_REC_SCANCODE
;
390 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
391 val
|= LIRC_CAN_REC_MODE2
;
392 if (dev
->rx_resolution
)
393 val
|= LIRC_CAN_GET_REC_RESOLUTION
;
397 val
|= LIRC_CAN_SEND_PULSE
;
399 val
|= LIRC_CAN_SET_TRANSMITTER_MASK
;
400 if (dev
->s_tx_carrier
)
401 val
|= LIRC_CAN_SET_SEND_CARRIER
;
402 if (dev
->s_tx_duty_cycle
)
403 val
|= LIRC_CAN_SET_SEND_DUTY_CYCLE
;
406 if (dev
->s_rx_carrier_range
)
407 val
|= LIRC_CAN_SET_REC_CARRIER
|
408 LIRC_CAN_SET_REC_CARRIER_RANGE
;
410 if (dev
->s_wideband_receiver
)
411 val
|= LIRC_CAN_USE_WIDEBAND_RECEIVER
;
413 if (dev
->s_carrier_report
)
414 val
|= LIRC_CAN_MEASURE_CARRIER
;
416 if (dev
->max_timeout
)
417 val
|= LIRC_CAN_SET_REC_TIMEOUT
;
422 case LIRC_GET_REC_MODE
:
423 if (dev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
429 case LIRC_SET_REC_MODE
:
430 switch (dev
->driver_type
) {
431 case RC_DRIVER_IR_RAW_TX
:
434 case RC_DRIVER_SCANCODE
:
435 if (val
!= LIRC_MODE_SCANCODE
)
438 case RC_DRIVER_IR_RAW
:
439 if (!(val
== LIRC_MODE_MODE2
||
440 val
== LIRC_MODE_SCANCODE
))
449 case LIRC_GET_SEND_MODE
:
456 case LIRC_SET_SEND_MODE
:
459 else if (!(val
== LIRC_MODE_PULSE
|| val
== LIRC_MODE_SCANCODE
))
466 case LIRC_SET_TRANSMITTER_MASK
:
470 ret
= dev
->s_tx_mask(dev
, val
);
473 case LIRC_SET_SEND_CARRIER
:
474 if (!dev
->s_tx_carrier
)
477 ret
= dev
->s_tx_carrier(dev
, val
);
480 case LIRC_SET_SEND_DUTY_CYCLE
:
481 if (!dev
->s_tx_duty_cycle
)
483 else if (val
<= 0 || val
>= 100)
486 ret
= dev
->s_tx_duty_cycle(dev
, val
);
490 case LIRC_SET_REC_CARRIER
:
491 if (!dev
->s_rx_carrier_range
)
496 ret
= dev
->s_rx_carrier_range(dev
, fh
->carrier_low
,
500 case LIRC_SET_REC_CARRIER_RANGE
:
501 if (!dev
->s_rx_carrier_range
)
506 fh
->carrier_low
= val
;
509 case LIRC_GET_REC_RESOLUTION
:
510 if (!dev
->rx_resolution
)
513 val
= dev
->rx_resolution
;
516 case LIRC_SET_WIDEBAND_RECEIVER
:
517 if (!dev
->s_wideband_receiver
)
520 ret
= dev
->s_wideband_receiver(dev
, !!val
);
523 case LIRC_SET_MEASURE_CARRIER_MODE
:
524 if (!dev
->s_carrier_report
)
527 ret
= dev
->s_carrier_report(dev
, !!val
);
530 /* Generic timeout support */
531 case LIRC_GET_MIN_TIMEOUT
:
532 if (!dev
->max_timeout
)
535 val
= dev
->min_timeout
;
538 case LIRC_GET_MAX_TIMEOUT
:
539 if (!dev
->max_timeout
)
542 val
= dev
->max_timeout
;
545 case LIRC_SET_REC_TIMEOUT
:
546 if (!dev
->max_timeout
) {
549 if (val
< dev
->min_timeout
|| val
> dev
->max_timeout
)
551 else if (dev
->s_timeout
)
552 ret
= dev
->s_timeout(dev
, val
);
558 case LIRC_GET_REC_TIMEOUT
:
565 case LIRC_SET_REC_TIMEOUT_REPORTS
:
566 if (dev
->driver_type
!= RC_DRIVER_IR_RAW
)
574 if (!ret
&& _IOC_DIR(cmd
) & _IOC_READ
)
575 ret
= put_user(val
, argp
);
578 mutex_unlock(&dev
->lock
);
582 static __poll_t
lirc_poll(struct file
*file
, struct poll_table_struct
*wait
)
584 struct lirc_fh
*fh
= file
->private_data
;
585 struct rc_dev
*rcdev
= fh
->rc
;
588 poll_wait(file
, &fh
->wait_poll
, wait
);
590 if (!rcdev
->registered
) {
591 events
= EPOLLHUP
| EPOLLERR
;
592 } else if (rcdev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
593 if (fh
->rec_mode
== LIRC_MODE_SCANCODE
&&
594 !kfifo_is_empty(&fh
->scancodes
))
595 events
= EPOLLIN
| EPOLLRDNORM
;
597 if (fh
->rec_mode
== LIRC_MODE_MODE2
&&
598 !kfifo_is_empty(&fh
->rawir
))
599 events
= EPOLLIN
| EPOLLRDNORM
;
605 static ssize_t
lirc_read_mode2(struct file
*file
, char __user
*buffer
,
608 struct lirc_fh
*fh
= file
->private_data
;
609 struct rc_dev
*rcdev
= fh
->rc
;
613 if (length
< sizeof(unsigned int) || length
% sizeof(unsigned int))
617 if (kfifo_is_empty(&fh
->rawir
)) {
618 if (file
->f_flags
& O_NONBLOCK
)
621 ret
= wait_event_interruptible(fh
->wait_poll
,
622 !kfifo_is_empty(&fh
->rawir
) ||
628 if (!rcdev
->registered
)
631 ret
= mutex_lock_interruptible(&rcdev
->lock
);
634 ret
= kfifo_to_user(&fh
->rawir
, buffer
, length
, &copied
);
635 mutex_unlock(&rcdev
->lock
);
638 } while (copied
== 0);
643 static ssize_t
lirc_read_scancode(struct file
*file
, char __user
*buffer
,
646 struct lirc_fh
*fh
= file
->private_data
;
647 struct rc_dev
*rcdev
= fh
->rc
;
651 if (length
< sizeof(struct lirc_scancode
) ||
652 length
% sizeof(struct lirc_scancode
))
656 if (kfifo_is_empty(&fh
->scancodes
)) {
657 if (file
->f_flags
& O_NONBLOCK
)
660 ret
= wait_event_interruptible(fh
->wait_poll
,
661 !kfifo_is_empty(&fh
->scancodes
) ||
667 if (!rcdev
->registered
)
670 ret
= mutex_lock_interruptible(&rcdev
->lock
);
673 ret
= kfifo_to_user(&fh
->scancodes
, buffer
, length
, &copied
);
674 mutex_unlock(&rcdev
->lock
);
677 } while (copied
== 0);
682 static ssize_t
lirc_read(struct file
*file
, char __user
*buffer
, size_t length
,
685 struct lirc_fh
*fh
= file
->private_data
;
686 struct rc_dev
*rcdev
= fh
->rc
;
688 if (rcdev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
691 if (!rcdev
->registered
)
694 if (fh
->rec_mode
== LIRC_MODE_MODE2
)
695 return lirc_read_mode2(file
, buffer
, length
);
696 else /* LIRC_MODE_SCANCODE */
697 return lirc_read_scancode(file
, buffer
, length
);
700 static const struct file_operations lirc_fops
= {
701 .owner
= THIS_MODULE
,
702 .write
= lirc_transmit
,
703 .unlocked_ioctl
= lirc_ioctl
,
704 .compat_ioctl
= compat_ptr_ioctl
,
708 .release
= lirc_close
,
711 static void lirc_release_device(struct device
*ld
)
713 struct rc_dev
*rcdev
= container_of(ld
, struct rc_dev
, lirc_dev
);
715 put_device(&rcdev
->dev
);
718 int lirc_register(struct rc_dev
*dev
)
720 const char *rx_type
, *tx_type
;
723 minor
= ida_alloc_max(&lirc_ida
, RC_DEV_MAX
- 1, GFP_KERNEL
);
727 device_initialize(&dev
->lirc_dev
);
728 dev
->lirc_dev
.class = &lirc_class
;
729 dev
->lirc_dev
.parent
= &dev
->dev
;
730 dev
->lirc_dev
.release
= lirc_release_device
;
731 dev
->lirc_dev
.devt
= MKDEV(MAJOR(lirc_base_dev
), minor
);
732 dev_set_name(&dev
->lirc_dev
, "lirc%d", minor
);
734 INIT_LIST_HEAD(&dev
->lirc_fh
);
735 spin_lock_init(&dev
->lirc_fh_lock
);
737 cdev_init(&dev
->lirc_cdev
, &lirc_fops
);
739 err
= cdev_device_add(&dev
->lirc_cdev
, &dev
->lirc_dev
);
743 get_device(&dev
->dev
);
745 switch (dev
->driver_type
) {
746 case RC_DRIVER_SCANCODE
:
747 rx_type
= "scancode";
749 case RC_DRIVER_IR_RAW
:
762 dev_info(&dev
->dev
, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
763 dev
->driver_name
, minor
, rx_type
, tx_type
);
768 ida_free(&lirc_ida
, minor
);
772 void 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_free(&lirc_ida
, MINOR(dev
->lirc_dev
.devt
));
789 int __init
lirc_dev_init(void)
793 retval
= class_register(&lirc_class
);
797 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, RC_DEV_MAX
, "lirc");
799 class_unregister(&lirc_class
);
800 pr_err("alloc_chrdev_region failed\n");
804 pr_debug("IR Remote Control driver registered, major %d\n",
805 MAJOR(lirc_base_dev
));
810 void __exit
lirc_dev_exit(void)
812 class_unregister(&lirc_class
);
813 unregister_chrdev_region(lirc_base_dev
, RC_DEV_MAX
);
816 struct rc_dev
*rc_dev_get_from_fd(int fd
, bool write
)
823 return ERR_PTR(-EBADF
);
825 if (fd_file(f
)->f_op
!= &lirc_fops
)
826 return ERR_PTR(-EINVAL
);
828 if (write
&& !(fd_file(f
)->f_mode
& FMODE_WRITE
))
829 return ERR_PTR(-EPERM
);
831 fh
= fd_file(f
)->private_data
;
834 get_device(&dev
->dev
);
839 MODULE_ALIAS("lirc_dev");