1 // SPDX-License-Identifier: GPL-2.0-or-later
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 * 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 lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
44 /* Receiver overflow, data missing */
47 * Send lirc overflow message. This message is unknown to
48 * lircd, but it will interpret this as a long space as
49 * long as the value is set to high value. This resets its
52 sample = LIRC_OVERFLOW(LIRC_VALUE_MASK);
53 dev_dbg(&dev->dev, "delivering overflow to lirc_dev\n");
56 } else if (ev.carrier_report) {
57 sample = LIRC_FREQUENCY(ev.carrier);
58 dev_dbg(&dev->dev, "carrier report (freq: %d)\n", sample);
61 } else if (ev.timeout) {
62 dev->gap_start = ktime_get();
64 sample = LIRC_TIMEOUT(ev.duration);
65 dev_dbg(&dev->dev, "timeout report (duration: %d)\n", sample);
70 u64 duration = ktime_us_delta(ktime_get(),
73 /* Cap by LIRC_VALUE_MASK */
74 duration = min_t(u64, duration, LIRC_VALUE_MASK);
76 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
77 list_for_each_entry(fh, &dev->lirc_fh, list)
78 kfifo_put(&fh->rawir, LIRC_SPACE(duration));
79 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
83 sample = ev.pulse ? LIRC_PULSE(ev.duration) :
84 LIRC_SPACE(ev.duration);
85 dev_dbg(&dev->dev, "delivering %uus %s to lirc_dev\n",
86 ev.duration, TO_STR(ev.pulse));
90 * bpf does not care about the gap generated above; that exists
91 * for backwards compatibility
93 lirc_bpf_run(dev, sample);
95 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
96 list_for_each_entry(fh, &dev->lirc_fh, list) {
97 if (kfifo_put(&fh->rawir, sample))
98 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
100 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
104 * lirc_scancode_event() - Send scancode data to lirc to be relayed to
105 * userspace. This can be called in atomic context.
106 * @dev: the struct rc_dev descriptor of the device
107 * @lsc: the struct lirc_scancode describing the decoded scancode
109 void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
114 lsc->timestamp = ktime_get_ns();
116 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
117 list_for_each_entry(fh, &dev->lirc_fh, list) {
118 if (kfifo_put(&fh->scancodes, *lsc))
119 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
121 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
123 EXPORT_SYMBOL_GPL(lirc_scancode_event);
125 static int lirc_open(struct inode *inode, struct file *file)
127 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
129 struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
136 get_device(&dev->dev);
138 if (!dev->registered) {
143 if (dev->driver_type == RC_DRIVER_IR_RAW) {
144 if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
150 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
151 if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
157 fh->send_mode = LIRC_MODE_PULSE;
160 if (dev->driver_type == RC_DRIVER_SCANCODE)
161 fh->rec_mode = LIRC_MODE_SCANCODE;
163 fh->rec_mode = LIRC_MODE_MODE2;
165 retval = rc_open(dev);
169 init_waitqueue_head(&fh->wait_poll);
171 file->private_data = fh;
172 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
173 list_add(&fh->list, &dev->lirc_fh);
174 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
176 stream_open(inode, file);
180 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
181 kfifo_free(&fh->scancodes);
183 if (dev->driver_type == RC_DRIVER_IR_RAW)
184 kfifo_free(&fh->rawir);
187 put_device(&dev->dev);
192 static int lirc_close(struct inode *inode, struct file *file)
194 struct lirc_fh *fh = file->private_data;
195 struct rc_dev *dev = fh->rc;
198 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
200 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
202 if (dev->driver_type == RC_DRIVER_IR_RAW)
203 kfifo_free(&fh->rawir);
204 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
205 kfifo_free(&fh->scancodes);
209 put_device(&dev->dev);
214 static ssize_t lirc_transmit(struct file *file, const char __user *buf,
215 size_t n, loff_t *ppos)
217 struct lirc_fh *fh = file->private_data;
218 struct rc_dev *dev = fh->rc;
220 struct ir_raw_event *raw = NULL;
225 unsigned int duration = 0; /* signal duration in us */
228 ret = mutex_lock_interruptible(&dev->lock);
232 if (!dev->registered) {
242 if (fh->send_mode == LIRC_MODE_SCANCODE) {
243 struct lirc_scancode scan;
245 if (n != sizeof(scan)) {
250 if (copy_from_user(&scan, buf, sizeof(scan))) {
255 if (scan.flags || scan.keycode || scan.timestamp ||
256 scan.rc_proto > RC_PROTO_MAX) {
261 /* We only have encoders for 32-bit protocols. */
262 if (scan.scancode > U32_MAX ||
263 !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
268 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
274 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
281 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
287 for (i = 0; i < count; i++)
288 txbuf[i] = raw[i].duration;
290 if (dev->s_tx_carrier) {
291 int carrier = ir_raw_encode_carrier(scan.rc_proto);
294 dev->s_tx_carrier(dev, carrier);
297 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
302 count = n / sizeof(unsigned int);
303 if (count > LIRCBUF_SIZE || count % 2 == 0) {
308 txbuf = memdup_user(buf, n);
310 ret = PTR_ERR(txbuf);
315 for (i = 0; i < count; i++) {
316 if (txbuf[i] > IR_MAX_DURATION - duration || !txbuf[i]) {
321 duration += txbuf[i];
326 ret = dev->tx_ir(dev, txbuf, count);
332 mutex_unlock(&dev->lock);
335 * The lircd gap calculation expects the write function to
336 * wait for the actual IR signal to be transmitted before
339 towait = ktime_us_delta(ktime_add_us(start, duration),
342 set_current_state(TASK_INTERRUPTIBLE);
343 schedule_timeout(usecs_to_jiffies(towait));
352 mutex_unlock(&dev->lock);
356 static long lirc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
358 struct lirc_fh *fh = file->private_data;
359 struct rc_dev *dev = fh->rc;
360 u32 __user *argp = (u32 __user *)(arg);
364 if (_IOC_DIR(cmd) & _IOC_WRITE) {
365 ret = get_user(val, argp);
370 ret = mutex_lock_interruptible(&dev->lock);
374 if (!dev->registered) {
380 case LIRC_GET_FEATURES:
381 if (dev->driver_type == RC_DRIVER_SCANCODE)
382 val |= LIRC_CAN_REC_SCANCODE;
384 if (dev->driver_type == RC_DRIVER_IR_RAW) {
385 val |= LIRC_CAN_REC_MODE2;
386 if (dev->rx_resolution)
387 val |= LIRC_CAN_GET_REC_RESOLUTION;
391 val |= LIRC_CAN_SEND_PULSE;
393 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
394 if (dev->s_tx_carrier)
395 val |= LIRC_CAN_SET_SEND_CARRIER;
396 if (dev->s_tx_duty_cycle)
397 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
400 if (dev->s_rx_carrier_range)
401 val |= LIRC_CAN_SET_REC_CARRIER |
402 LIRC_CAN_SET_REC_CARRIER_RANGE;
404 if (dev->s_wideband_receiver)
405 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
407 if (dev->s_carrier_report)
408 val |= LIRC_CAN_MEASURE_CARRIER;
410 if (dev->max_timeout)
411 val |= LIRC_CAN_SET_REC_TIMEOUT;
416 case LIRC_GET_REC_MODE:
417 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
423 case LIRC_SET_REC_MODE:
424 switch (dev->driver_type) {
425 case RC_DRIVER_IR_RAW_TX:
428 case RC_DRIVER_SCANCODE:
429 if (val != LIRC_MODE_SCANCODE)
432 case RC_DRIVER_IR_RAW:
433 if (!(val == LIRC_MODE_MODE2 ||
434 val == LIRC_MODE_SCANCODE))
443 case LIRC_GET_SEND_MODE:
450 case LIRC_SET_SEND_MODE:
453 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
460 case LIRC_SET_TRANSMITTER_MASK:
464 ret = dev->s_tx_mask(dev, val);
467 case LIRC_SET_SEND_CARRIER:
468 if (!dev->s_tx_carrier)
471 ret = dev->s_tx_carrier(dev, val);
474 case LIRC_SET_SEND_DUTY_CYCLE:
475 if (!dev->s_tx_duty_cycle)
477 else if (val <= 0 || val >= 100)
480 ret = dev->s_tx_duty_cycle(dev, val);
484 case LIRC_SET_REC_CARRIER:
485 if (!dev->s_rx_carrier_range)
490 ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
494 case LIRC_SET_REC_CARRIER_RANGE:
495 if (!dev->s_rx_carrier_range)
500 fh->carrier_low = val;
503 case LIRC_GET_REC_RESOLUTION:
504 if (!dev->rx_resolution)
507 val = dev->rx_resolution;
510 case LIRC_SET_WIDEBAND_RECEIVER:
511 if (!dev->s_wideband_receiver)
514 ret = dev->s_wideband_receiver(dev, !!val);
517 case LIRC_SET_MEASURE_CARRIER_MODE:
518 if (!dev->s_carrier_report)
521 ret = dev->s_carrier_report(dev, !!val);
524 /* Generic timeout support */
525 case LIRC_GET_MIN_TIMEOUT:
526 if (!dev->max_timeout)
529 val = dev->min_timeout;
532 case LIRC_GET_MAX_TIMEOUT:
533 if (!dev->max_timeout)
536 val = dev->max_timeout;
539 case LIRC_SET_REC_TIMEOUT:
540 if (!dev->max_timeout) {
543 if (val < dev->min_timeout || val > dev->max_timeout)
545 else if (dev->s_timeout)
546 ret = dev->s_timeout(dev, val);
552 case LIRC_GET_REC_TIMEOUT:
559 case LIRC_SET_REC_TIMEOUT_REPORTS:
560 if (dev->driver_type != RC_DRIVER_IR_RAW)
568 if (!ret && _IOC_DIR(cmd) & _IOC_READ)
569 ret = put_user(val, argp);
572 mutex_unlock(&dev->lock);
576 static __poll_t lirc_poll(struct file *file, struct poll_table_struct *wait)
578 struct lirc_fh *fh = file->private_data;
579 struct rc_dev *rcdev = fh->rc;
582 poll_wait(file, &fh->wait_poll, wait);
584 if (!rcdev->registered) {
585 events = EPOLLHUP | EPOLLERR;
586 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
587 if (fh->rec_mode == LIRC_MODE_SCANCODE &&
588 !kfifo_is_empty(&fh->scancodes))
589 events = EPOLLIN | EPOLLRDNORM;
591 if (fh->rec_mode == LIRC_MODE_MODE2 &&
592 !kfifo_is_empty(&fh->rawir))
593 events = EPOLLIN | EPOLLRDNORM;
599 static ssize_t lirc_read_mode2(struct file *file, char __user *buffer,
602 struct lirc_fh *fh = file->private_data;
603 struct rc_dev *rcdev = fh->rc;
607 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
611 if (kfifo_is_empty(&fh->rawir)) {
612 if (file->f_flags & O_NONBLOCK)
615 ret = wait_event_interruptible(fh->wait_poll,
616 !kfifo_is_empty(&fh->rawir) ||
622 if (!rcdev->registered)
625 ret = mutex_lock_interruptible(&rcdev->lock);
628 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
629 mutex_unlock(&rcdev->lock);
632 } while (copied == 0);
637 static ssize_t lirc_read_scancode(struct file *file, char __user *buffer,
640 struct lirc_fh *fh = file->private_data;
641 struct rc_dev *rcdev = fh->rc;
645 if (length < sizeof(struct lirc_scancode) ||
646 length % sizeof(struct lirc_scancode))
650 if (kfifo_is_empty(&fh->scancodes)) {
651 if (file->f_flags & O_NONBLOCK)
654 ret = wait_event_interruptible(fh->wait_poll,
655 !kfifo_is_empty(&fh->scancodes) ||
661 if (!rcdev->registered)
664 ret = mutex_lock_interruptible(&rcdev->lock);
667 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
668 mutex_unlock(&rcdev->lock);
671 } while (copied == 0);
676 static ssize_t lirc_read(struct file *file, char __user *buffer, size_t length,
679 struct lirc_fh *fh = file->private_data;
680 struct rc_dev *rcdev = fh->rc;
682 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
685 if (!rcdev->registered)
688 if (fh->rec_mode == LIRC_MODE_MODE2)
689 return lirc_read_mode2(file, buffer, length);
690 else /* LIRC_MODE_SCANCODE */
691 return lirc_read_scancode(file, buffer, length);
694 static const struct file_operations lirc_fops = {
695 .owner = THIS_MODULE,
696 .write = lirc_transmit,
697 .unlocked_ioctl = lirc_ioctl,
698 .compat_ioctl = compat_ptr_ioctl,
702 .release = lirc_close,
706 static void lirc_release_device(struct device *ld)
708 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
710 put_device(&rcdev->dev);
713 int lirc_register(struct rc_dev *dev)
715 const char *rx_type, *tx_type;
718 minor = ida_alloc_max(&lirc_ida, RC_DEV_MAX - 1, GFP_KERNEL);
722 device_initialize(&dev->lirc_dev);
723 dev->lirc_dev.class = lirc_class;
724 dev->lirc_dev.parent = &dev->dev;
725 dev->lirc_dev.release = lirc_release_device;
726 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
727 dev_set_name(&dev->lirc_dev, "lirc%d", minor);
729 INIT_LIST_HEAD(&dev->lirc_fh);
730 spin_lock_init(&dev->lirc_fh_lock);
732 cdev_init(&dev->lirc_cdev, &lirc_fops);
734 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
738 get_device(&dev->dev);
740 switch (dev->driver_type) {
741 case RC_DRIVER_SCANCODE:
742 rx_type = "scancode";
744 case RC_DRIVER_IR_RAW:
757 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
758 dev->driver_name, minor, rx_type, tx_type);
763 ida_free(&lirc_ida, minor);
767 void lirc_unregister(struct rc_dev *dev)
772 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
773 dev->driver_name, MINOR(dev->lirc_dev.devt));
775 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
776 list_for_each_entry(fh, &dev->lirc_fh, list)
777 wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
778 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
780 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
781 ida_free(&lirc_ida, MINOR(dev->lirc_dev.devt));
784 int __init lirc_dev_init(void)
788 lirc_class = class_create(THIS_MODULE, "lirc");
789 if (IS_ERR(lirc_class)) {
790 pr_err("class_create failed\n");
791 return PTR_ERR(lirc_class);
794 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, "lirc");
796 class_destroy(lirc_class);
797 pr_err("alloc_chrdev_region failed\n");
801 pr_debug("IR Remote Control driver registered, major %d\n",
802 MAJOR(lirc_base_dev));
807 void __exit lirc_dev_exit(void)
809 class_destroy(lirc_class);
810 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
813 struct rc_dev *rc_dev_get_from_fd(int fd)
815 struct fd f = fdget(fd);
820 return ERR_PTR(-EBADF);
822 if (f.file->f_op != &lirc_fops) {
824 return ERR_PTR(-EINVAL);
827 fh = f.file->private_data;
830 get_device(&dev->dev);
836 MODULE_ALIAS("lirc_dev");