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 * 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)
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);
71 dev_dbg(&dev->dev, "timeout report (duration: %d)\n", sample);
76 dev->gap_duration += ktime_to_us(ktime_sub(ktime_get(),
79 /* Cap by LIRC_VALUE_MASK */
80 dev->gap_duration = min_t(u64, dev->gap_duration,
83 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
84 list_for_each_entry(fh, &dev->lirc_fh, list)
86 LIRC_SPACE(dev->gap_duration));
87 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
91 sample = ev.pulse ? LIRC_PULSE(ev.duration) :
92 LIRC_SPACE(ev.duration);
93 dev_dbg(&dev->dev, "delivering %uus %s to lirc_dev\n",
94 ev.duration, TO_STR(ev.pulse));
98 * bpf does not care about the gap generated above; that exists
99 * for backwards compatibility
101 lirc_bpf_run(dev, sample);
103 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
104 list_for_each_entry(fh, &dev->lirc_fh, list) {
105 if (LIRC_IS_TIMEOUT(sample) && !fh->send_timeout_reports)
107 if (kfifo_put(&fh->rawir, sample))
108 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
110 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
114 * lirc_scancode_event() - Send scancode data to lirc to be relayed to
115 * userspace. This can be called in atomic context.
116 * @dev: the struct rc_dev descriptor of the device
117 * @lsc: the struct lirc_scancode describing the decoded scancode
119 void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
124 lsc->timestamp = ktime_get_ns();
126 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
127 list_for_each_entry(fh, &dev->lirc_fh, list) {
128 if (kfifo_put(&fh->scancodes, *lsc))
129 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
131 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
133 EXPORT_SYMBOL_GPL(lirc_scancode_event);
135 static int lirc_open(struct inode *inode, struct file *file)
137 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
139 struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
146 get_device(&dev->dev);
148 if (!dev->registered) {
153 if (dev->driver_type == RC_DRIVER_IR_RAW) {
154 if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
160 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
161 if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
167 fh->send_mode = LIRC_MODE_PULSE;
169 fh->send_timeout_reports = true;
171 if (dev->driver_type == RC_DRIVER_SCANCODE)
172 fh->rec_mode = LIRC_MODE_SCANCODE;
174 fh->rec_mode = LIRC_MODE_MODE2;
176 retval = rc_open(dev);
180 init_waitqueue_head(&fh->wait_poll);
182 file->private_data = fh;
183 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
184 list_add(&fh->list, &dev->lirc_fh);
185 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
187 stream_open(inode, file);
191 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
192 kfifo_free(&fh->scancodes);
194 if (dev->driver_type == RC_DRIVER_IR_RAW)
195 kfifo_free(&fh->rawir);
198 put_device(&dev->dev);
203 static int lirc_close(struct inode *inode, struct file *file)
205 struct lirc_fh *fh = file->private_data;
206 struct rc_dev *dev = fh->rc;
209 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
211 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
213 if (dev->driver_type == RC_DRIVER_IR_RAW)
214 kfifo_free(&fh->rawir);
215 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
216 kfifo_free(&fh->scancodes);
220 put_device(&dev->dev);
225 static ssize_t lirc_transmit(struct file *file, const char __user *buf,
226 size_t n, loff_t *ppos)
228 struct lirc_fh *fh = file->private_data;
229 struct rc_dev *dev = fh->rc;
231 struct ir_raw_event *raw = NULL;
236 unsigned int duration = 0; /* signal duration in us */
239 ret = mutex_lock_interruptible(&dev->lock);
243 if (!dev->registered) {
253 if (fh->send_mode == LIRC_MODE_SCANCODE) {
254 struct lirc_scancode scan;
256 if (n != sizeof(scan)) {
261 if (copy_from_user(&scan, buf, sizeof(scan))) {
266 if (scan.flags || scan.keycode || scan.timestamp) {
271 /* We only have encoders for 32-bit protocols. */
272 if (scan.scancode > U32_MAX ||
273 !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
278 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
284 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
289 /* drop trailing space */
295 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
301 for (i = 0; i < count; i++)
302 txbuf[i] = raw[i].duration;
304 if (dev->s_tx_carrier) {
305 int carrier = ir_raw_encode_carrier(scan.rc_proto);
308 dev->s_tx_carrier(dev, carrier);
311 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
316 count = n / sizeof(unsigned int);
317 if (count > LIRCBUF_SIZE || count % 2 == 0) {
322 txbuf = memdup_user(buf, n);
324 ret = PTR_ERR(txbuf);
329 for (i = 0; i < count; i++) {
330 if (txbuf[i] > IR_MAX_DURATION - duration || !txbuf[i]) {
335 duration += txbuf[i];
340 ret = dev->tx_ir(dev, txbuf, count);
346 mutex_unlock(&dev->lock);
349 * The lircd gap calculation expects the write function to
350 * wait for the actual IR signal to be transmitted before
353 towait = ktime_us_delta(ktime_add_us(start, duration),
356 set_current_state(TASK_INTERRUPTIBLE);
357 schedule_timeout(usecs_to_jiffies(towait));
366 mutex_unlock(&dev->lock);
370 static long lirc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
372 struct lirc_fh *fh = file->private_data;
373 struct rc_dev *dev = fh->rc;
374 u32 __user *argp = (u32 __user *)(arg);
378 if (_IOC_DIR(cmd) & _IOC_WRITE) {
379 ret = get_user(val, argp);
384 ret = mutex_lock_interruptible(&dev->lock);
388 if (!dev->registered) {
394 case LIRC_GET_FEATURES:
395 if (dev->driver_type == RC_DRIVER_SCANCODE)
396 val |= LIRC_CAN_REC_SCANCODE;
398 if (dev->driver_type == RC_DRIVER_IR_RAW) {
399 val |= LIRC_CAN_REC_MODE2;
400 if (dev->rx_resolution)
401 val |= LIRC_CAN_GET_REC_RESOLUTION;
405 val |= LIRC_CAN_SEND_PULSE;
407 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
408 if (dev->s_tx_carrier)
409 val |= LIRC_CAN_SET_SEND_CARRIER;
410 if (dev->s_tx_duty_cycle)
411 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
414 if (dev->s_rx_carrier_range)
415 val |= LIRC_CAN_SET_REC_CARRIER |
416 LIRC_CAN_SET_REC_CARRIER_RANGE;
418 if (dev->s_learning_mode)
419 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
421 if (dev->s_carrier_report)
422 val |= LIRC_CAN_MEASURE_CARRIER;
424 if (dev->max_timeout)
425 val |= LIRC_CAN_SET_REC_TIMEOUT;
430 case LIRC_GET_REC_MODE:
431 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
437 case LIRC_SET_REC_MODE:
438 switch (dev->driver_type) {
439 case RC_DRIVER_IR_RAW_TX:
442 case RC_DRIVER_SCANCODE:
443 if (val != LIRC_MODE_SCANCODE)
446 case RC_DRIVER_IR_RAW:
447 if (!(val == LIRC_MODE_MODE2 ||
448 val == LIRC_MODE_SCANCODE))
457 case LIRC_GET_SEND_MODE:
464 case LIRC_SET_SEND_MODE:
467 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
474 case LIRC_SET_TRANSMITTER_MASK:
478 ret = dev->s_tx_mask(dev, val);
481 case LIRC_SET_SEND_CARRIER:
482 if (!dev->s_tx_carrier)
485 ret = dev->s_tx_carrier(dev, val);
488 case LIRC_SET_SEND_DUTY_CYCLE:
489 if (!dev->s_tx_duty_cycle)
491 else if (val <= 0 || val >= 100)
494 ret = dev->s_tx_duty_cycle(dev, val);
498 case LIRC_SET_REC_CARRIER:
499 if (!dev->s_rx_carrier_range)
504 ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
508 case LIRC_SET_REC_CARRIER_RANGE:
509 if (!dev->s_rx_carrier_range)
514 fh->carrier_low = val;
517 case LIRC_GET_REC_RESOLUTION:
518 if (!dev->rx_resolution)
521 val = dev->rx_resolution;
524 case LIRC_SET_WIDEBAND_RECEIVER:
525 if (!dev->s_learning_mode)
528 ret = dev->s_learning_mode(dev, !!val);
531 case LIRC_SET_MEASURE_CARRIER_MODE:
532 if (!dev->s_carrier_report)
535 ret = dev->s_carrier_report(dev, !!val);
538 /* Generic timeout support */
539 case LIRC_GET_MIN_TIMEOUT:
540 if (!dev->max_timeout)
543 val = dev->min_timeout;
546 case LIRC_GET_MAX_TIMEOUT:
547 if (!dev->max_timeout)
550 val = dev->max_timeout;
553 case LIRC_SET_REC_TIMEOUT:
554 if (!dev->max_timeout) {
557 if (val < dev->min_timeout || val > dev->max_timeout)
559 else if (dev->s_timeout)
560 ret = dev->s_timeout(dev, val);
566 case LIRC_GET_REC_TIMEOUT:
573 case LIRC_SET_REC_TIMEOUT_REPORTS:
574 if (dev->driver_type != RC_DRIVER_IR_RAW)
577 fh->send_timeout_reports = !!val;
584 if (!ret && _IOC_DIR(cmd) & _IOC_READ)
585 ret = put_user(val, argp);
588 mutex_unlock(&dev->lock);
592 static __poll_t lirc_poll(struct file *file, struct poll_table_struct *wait)
594 struct lirc_fh *fh = file->private_data;
595 struct rc_dev *rcdev = fh->rc;
598 poll_wait(file, &fh->wait_poll, wait);
600 if (!rcdev->registered) {
601 events = EPOLLHUP | EPOLLERR;
602 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
603 if (fh->rec_mode == LIRC_MODE_SCANCODE &&
604 !kfifo_is_empty(&fh->scancodes))
605 events = EPOLLIN | EPOLLRDNORM;
607 if (fh->rec_mode == LIRC_MODE_MODE2 &&
608 !kfifo_is_empty(&fh->rawir))
609 events = EPOLLIN | EPOLLRDNORM;
615 static ssize_t lirc_read_mode2(struct file *file, char __user *buffer,
618 struct lirc_fh *fh = file->private_data;
619 struct rc_dev *rcdev = fh->rc;
623 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
627 if (kfifo_is_empty(&fh->rawir)) {
628 if (file->f_flags & O_NONBLOCK)
631 ret = wait_event_interruptible(fh->wait_poll,
632 !kfifo_is_empty(&fh->rawir) ||
638 if (!rcdev->registered)
641 ret = mutex_lock_interruptible(&rcdev->lock);
644 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
645 mutex_unlock(&rcdev->lock);
648 } while (copied == 0);
653 static ssize_t lirc_read_scancode(struct file *file, char __user *buffer,
656 struct lirc_fh *fh = file->private_data;
657 struct rc_dev *rcdev = fh->rc;
661 if (length < sizeof(struct lirc_scancode) ||
662 length % sizeof(struct lirc_scancode))
666 if (kfifo_is_empty(&fh->scancodes)) {
667 if (file->f_flags & O_NONBLOCK)
670 ret = wait_event_interruptible(fh->wait_poll,
671 !kfifo_is_empty(&fh->scancodes) ||
677 if (!rcdev->registered)
680 ret = mutex_lock_interruptible(&rcdev->lock);
683 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
684 mutex_unlock(&rcdev->lock);
687 } while (copied == 0);
692 static ssize_t lirc_read(struct file *file, char __user *buffer, size_t length,
695 struct lirc_fh *fh = file->private_data;
696 struct rc_dev *rcdev = fh->rc;
698 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
701 if (!rcdev->registered)
704 if (fh->rec_mode == LIRC_MODE_MODE2)
705 return lirc_read_mode2(file, buffer, length);
706 else /* LIRC_MODE_SCANCODE */
707 return lirc_read_scancode(file, buffer, length);
710 static const struct file_operations lirc_fops = {
711 .owner = THIS_MODULE,
712 .write = lirc_transmit,
713 .unlocked_ioctl = lirc_ioctl,
714 .compat_ioctl = compat_ptr_ioctl,
718 .release = lirc_close,
722 static void lirc_release_device(struct device *ld)
724 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
726 put_device(&rcdev->dev);
729 int lirc_register(struct rc_dev *dev)
731 const char *rx_type, *tx_type;
734 minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
738 device_initialize(&dev->lirc_dev);
739 dev->lirc_dev.class = lirc_class;
740 dev->lirc_dev.parent = &dev->dev;
741 dev->lirc_dev.release = lirc_release_device;
742 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
743 dev_set_name(&dev->lirc_dev, "lirc%d", minor);
745 INIT_LIST_HEAD(&dev->lirc_fh);
746 spin_lock_init(&dev->lirc_fh_lock);
748 cdev_init(&dev->lirc_cdev, &lirc_fops);
750 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
754 get_device(&dev->dev);
756 switch (dev->driver_type) {
757 case RC_DRIVER_SCANCODE:
758 rx_type = "scancode";
760 case RC_DRIVER_IR_RAW:
773 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
774 dev->driver_name, minor, rx_type, tx_type);
779 ida_simple_remove(&lirc_ida, minor);
783 void lirc_unregister(struct rc_dev *dev)
788 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
789 dev->driver_name, MINOR(dev->lirc_dev.devt));
791 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
792 list_for_each_entry(fh, &dev->lirc_fh, list)
793 wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
794 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
796 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
797 ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
800 int __init lirc_dev_init(void)
804 lirc_class = class_create(THIS_MODULE, "lirc");
805 if (IS_ERR(lirc_class)) {
806 pr_err("class_create failed\n");
807 return PTR_ERR(lirc_class);
810 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, "lirc");
812 class_destroy(lirc_class);
813 pr_err("alloc_chrdev_region failed\n");
817 pr_debug("IR Remote Control driver registered, major %d\n",
818 MAJOR(lirc_base_dev));
823 void __exit lirc_dev_exit(void)
825 class_destroy(lirc_class);
826 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
829 struct rc_dev *rc_dev_get_from_fd(int fd, bool write)
831 struct fd f = fdget(fd);
836 return ERR_PTR(-EBADF);
838 if (f.file->f_op != &lirc_fops) {
840 return ERR_PTR(-EINVAL);
843 if (write && !(f.file->f_mode & FMODE_WRITE))
844 return ERR_PTR(-EPERM);
846 fh = f.file->private_data;
849 get_device(&dev->dev);
855 MODULE_ALIAS("lirc_dev");