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/file.h>
24 #include <linux/idr.h>
25 #include <linux/poll.h>
26 #include <linux/sched.h>
27 #include <linux/wait.h>
29 #include "rc-core-priv.h"
30 #include <uapi/linux/lirc.h>
32 #define LIRCBUF_SIZE 256
34 static dev_t lirc_base_dev
;
36 /* Used to keep track of allocated lirc devices */
37 static DEFINE_IDA(lirc_ida
);
39 /* Only used for sysfs but defined to void otherwise */
40 static struct class *lirc_class
;
43 * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
45 * @dev: the struct rc_dev descriptor of the device
46 * @ev: the struct ir_raw_event descriptor of the pulse/space
48 void ir_lirc_raw_event(struct rc_dev
*dev
, struct ir_raw_event ev
)
57 * Userspace expects a long space event before the start of
58 * the signal to use as a sync. This may be done with repeat
59 * packets and normal samples. But if a reset has been sent
60 * then we assume that a long time has passed, so we send a
61 * space with the maximum time value.
63 sample
= LIRC_SPACE(LIRC_VALUE_MASK
);
64 dev_dbg(&dev
->dev
, "delivering reset sync space to lirc_dev\n");
67 } else if (ev
.carrier_report
) {
68 sample
= LIRC_FREQUENCY(ev
.carrier
);
69 dev_dbg(&dev
->dev
, "carrier report (freq: %d)\n", sample
);
72 } else if (ev
.timeout
) {
76 dev
->gap_start
= ktime_get();
78 dev
->gap_duration
= ev
.duration
;
80 sample
= LIRC_TIMEOUT(ev
.duration
/ 1000);
81 dev_dbg(&dev
->dev
, "timeout report (duration: %d)\n", sample
);
86 dev
->gap_duration
+= ktime_to_ns(ktime_sub(ktime_get(),
89 /* Convert to ms and cap by LIRC_VALUE_MASK */
90 do_div(dev
->gap_duration
, 1000);
91 dev
->gap_duration
= min_t(u64
, dev
->gap_duration
,
94 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
95 list_for_each_entry(fh
, &dev
->lirc_fh
, list
)
97 LIRC_SPACE(dev
->gap_duration
));
98 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
102 sample
= ev
.pulse
? LIRC_PULSE(ev
.duration
/ 1000) :
103 LIRC_SPACE(ev
.duration
/ 1000);
104 dev_dbg(&dev
->dev
, "delivering %uus %s to lirc_dev\n",
105 TO_US(ev
.duration
), TO_STR(ev
.pulse
));
109 * bpf does not care about the gap generated above; that exists
110 * for backwards compatibility
112 lirc_bpf_run(dev
, sample
);
114 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
115 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
116 if (LIRC_IS_TIMEOUT(sample
) && !fh
->send_timeout_reports
)
118 if (kfifo_put(&fh
->rawir
, sample
))
119 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
121 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
125 * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
126 * userspace. This can be called in atomic context.
127 * @dev: the struct rc_dev descriptor of the device
128 * @lsc: the struct lirc_scancode describing the decoded scancode
130 void ir_lirc_scancode_event(struct rc_dev
*dev
, struct lirc_scancode
*lsc
)
135 lsc
->timestamp
= ktime_get_ns();
137 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
138 list_for_each_entry(fh
, &dev
->lirc_fh
, list
) {
139 if (kfifo_put(&fh
->scancodes
, *lsc
))
140 wake_up_poll(&fh
->wait_poll
, EPOLLIN
| EPOLLRDNORM
);
142 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
144 EXPORT_SYMBOL_GPL(ir_lirc_scancode_event
);
146 static int ir_lirc_open(struct inode
*inode
, struct file
*file
)
148 struct rc_dev
*dev
= container_of(inode
->i_cdev
, struct rc_dev
,
150 struct lirc_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
157 get_device(&dev
->dev
);
159 if (!dev
->registered
) {
164 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
165 if (kfifo_alloc(&fh
->rawir
, MAX_IR_EVENT_SIZE
, GFP_KERNEL
)) {
171 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
172 if (kfifo_alloc(&fh
->scancodes
, 32, GFP_KERNEL
)) {
178 fh
->send_mode
= LIRC_MODE_PULSE
;
180 fh
->send_timeout_reports
= true;
182 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
183 fh
->rec_mode
= LIRC_MODE_SCANCODE
;
185 fh
->rec_mode
= LIRC_MODE_MODE2
;
187 retval
= rc_open(dev
);
191 init_waitqueue_head(&fh
->wait_poll
);
193 file
->private_data
= fh
;
194 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
195 list_add(&fh
->list
, &dev
->lirc_fh
);
196 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
198 nonseekable_open(inode
, file
);
202 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
203 kfifo_free(&fh
->scancodes
);
205 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
206 kfifo_free(&fh
->rawir
);
209 put_device(&dev
->dev
);
214 static int ir_lirc_close(struct inode
*inode
, struct file
*file
)
216 struct lirc_fh
*fh
= file
->private_data
;
217 struct rc_dev
*dev
= fh
->rc
;
220 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
222 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
224 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
225 kfifo_free(&fh
->rawir
);
226 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
227 kfifo_free(&fh
->scancodes
);
231 put_device(&dev
->dev
);
236 static ssize_t
ir_lirc_transmit_ir(struct file
*file
, const char __user
*buf
,
237 size_t n
, loff_t
*ppos
)
239 struct lirc_fh
*fh
= file
->private_data
;
240 struct rc_dev
*dev
= fh
->rc
;
242 struct ir_raw_event
*raw
= NULL
;
247 unsigned int duration
= 0; /* signal duration in us */
250 ret
= mutex_lock_interruptible(&dev
->lock
);
254 if (!dev
->registered
) {
264 if (fh
->send_mode
== LIRC_MODE_SCANCODE
) {
265 struct lirc_scancode scan
;
267 if (n
!= sizeof(scan
)) {
272 if (copy_from_user(&scan
, buf
, sizeof(scan
))) {
277 if (scan
.flags
|| scan
.keycode
|| scan
.timestamp
) {
283 * The scancode field in lirc_scancode is 64-bit simply
284 * to future-proof it, since there are IR protocols encode
285 * use more than 32 bits. For now only 32-bit protocols
288 if (scan
.scancode
> U32_MAX
||
289 !rc_validate_scancode(scan
.rc_proto
, scan
.scancode
)) {
294 raw
= kmalloc_array(LIRCBUF_SIZE
, sizeof(*raw
), GFP_KERNEL
);
300 ret
= ir_raw_encode_scancode(scan
.rc_proto
, scan
.scancode
,
307 txbuf
= kmalloc_array(count
, sizeof(unsigned int), GFP_KERNEL
);
313 for (i
= 0; i
< count
; i
++)
314 /* Convert from NS to US */
315 txbuf
[i
] = DIV_ROUND_UP(raw
[i
].duration
, 1000);
317 if (dev
->s_tx_carrier
) {
318 int carrier
= ir_raw_encode_carrier(scan
.rc_proto
);
321 dev
->s_tx_carrier(dev
, carrier
);
324 if (n
< sizeof(unsigned int) || n
% sizeof(unsigned int)) {
329 count
= n
/ sizeof(unsigned int);
330 if (count
> LIRCBUF_SIZE
|| count
% 2 == 0) {
335 txbuf
= memdup_user(buf
, n
);
337 ret
= PTR_ERR(txbuf
);
342 for (i
= 0; i
< count
; i
++) {
343 if (txbuf
[i
] > IR_MAX_DURATION
/ 1000 - duration
|| !txbuf
[i
]) {
348 duration
+= txbuf
[i
];
353 ret
= dev
->tx_ir(dev
, txbuf
, count
);
359 mutex_unlock(&dev
->lock
);
362 * The lircd gap calculation expects the write function to
363 * wait for the actual IR signal to be transmitted before
366 towait
= ktime_us_delta(ktime_add_us(start
, duration
),
369 set_current_state(TASK_INTERRUPTIBLE
);
370 schedule_timeout(usecs_to_jiffies(towait
));
379 mutex_unlock(&dev
->lock
);
383 static long ir_lirc_ioctl(struct file
*file
, unsigned int cmd
,
386 struct lirc_fh
*fh
= file
->private_data
;
387 struct rc_dev
*dev
= fh
->rc
;
388 u32 __user
*argp
= (u32 __user
*)(arg
);
392 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
393 ret
= get_user(val
, argp
);
398 ret
= mutex_lock_interruptible(&dev
->lock
);
402 if (!dev
->registered
) {
408 case LIRC_GET_FEATURES
:
409 if (dev
->driver_type
== RC_DRIVER_SCANCODE
)
410 val
|= LIRC_CAN_REC_SCANCODE
;
412 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
413 val
|= LIRC_CAN_REC_MODE2
;
414 if (dev
->rx_resolution
)
415 val
|= LIRC_CAN_GET_REC_RESOLUTION
;
419 val
|= LIRC_CAN_SEND_PULSE
;
421 val
|= LIRC_CAN_SET_TRANSMITTER_MASK
;
422 if (dev
->s_tx_carrier
)
423 val
|= LIRC_CAN_SET_SEND_CARRIER
;
424 if (dev
->s_tx_duty_cycle
)
425 val
|= LIRC_CAN_SET_SEND_DUTY_CYCLE
;
428 if (dev
->s_rx_carrier_range
)
429 val
|= LIRC_CAN_SET_REC_CARRIER
|
430 LIRC_CAN_SET_REC_CARRIER_RANGE
;
432 if (dev
->s_learning_mode
)
433 val
|= LIRC_CAN_USE_WIDEBAND_RECEIVER
;
435 if (dev
->s_carrier_report
)
436 val
|= LIRC_CAN_MEASURE_CARRIER
;
438 if (dev
->max_timeout
)
439 val
|= LIRC_CAN_SET_REC_TIMEOUT
;
444 case LIRC_GET_REC_MODE
:
445 if (dev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
451 case LIRC_SET_REC_MODE
:
452 switch (dev
->driver_type
) {
453 case RC_DRIVER_IR_RAW_TX
:
456 case RC_DRIVER_SCANCODE
:
457 if (val
!= LIRC_MODE_SCANCODE
)
460 case RC_DRIVER_IR_RAW
:
461 if (!(val
== LIRC_MODE_MODE2
||
462 val
== LIRC_MODE_SCANCODE
))
471 case LIRC_GET_SEND_MODE
:
478 case LIRC_SET_SEND_MODE
:
481 else if (!(val
== LIRC_MODE_PULSE
|| val
== LIRC_MODE_SCANCODE
))
488 case LIRC_SET_TRANSMITTER_MASK
:
492 ret
= dev
->s_tx_mask(dev
, val
);
495 case LIRC_SET_SEND_CARRIER
:
496 if (!dev
->s_tx_carrier
)
499 ret
= dev
->s_tx_carrier(dev
, val
);
502 case LIRC_SET_SEND_DUTY_CYCLE
:
503 if (!dev
->s_tx_duty_cycle
)
505 else if (val
<= 0 || val
>= 100)
508 ret
= dev
->s_tx_duty_cycle(dev
, val
);
512 case LIRC_SET_REC_CARRIER
:
513 if (!dev
->s_rx_carrier_range
)
518 ret
= dev
->s_rx_carrier_range(dev
, fh
->carrier_low
,
522 case LIRC_SET_REC_CARRIER_RANGE
:
523 if (!dev
->s_rx_carrier_range
)
528 fh
->carrier_low
= val
;
531 case LIRC_GET_REC_RESOLUTION
:
532 if (!dev
->rx_resolution
)
535 val
= dev
->rx_resolution
/ 1000;
538 case LIRC_SET_WIDEBAND_RECEIVER
:
539 if (!dev
->s_learning_mode
)
542 ret
= dev
->s_learning_mode(dev
, !!val
);
545 case LIRC_SET_MEASURE_CARRIER_MODE
:
546 if (!dev
->s_carrier_report
)
549 ret
= dev
->s_carrier_report(dev
, !!val
);
552 /* Generic timeout support */
553 case LIRC_GET_MIN_TIMEOUT
:
554 if (!dev
->max_timeout
)
557 val
= DIV_ROUND_UP(dev
->min_timeout
, 1000);
560 case LIRC_GET_MAX_TIMEOUT
:
561 if (!dev
->max_timeout
)
564 val
= dev
->max_timeout
/ 1000;
567 case LIRC_SET_REC_TIMEOUT
:
568 if (!dev
->max_timeout
) {
570 } else if (val
> U32_MAX
/ 1000) {
571 /* Check for multiply overflow */
574 u32 tmp
= val
* 1000;
576 if (tmp
< dev
->min_timeout
|| tmp
> dev
->max_timeout
)
578 else if (dev
->s_timeout
)
579 ret
= dev
->s_timeout(dev
, tmp
);
585 case LIRC_GET_REC_TIMEOUT
:
589 val
= DIV_ROUND_UP(dev
->timeout
, 1000);
592 case LIRC_SET_REC_TIMEOUT_REPORTS
:
593 if (dev
->driver_type
!= RC_DRIVER_IR_RAW
)
596 fh
->send_timeout_reports
= !!val
;
603 if (!ret
&& _IOC_DIR(cmd
) & _IOC_READ
)
604 ret
= put_user(val
, argp
);
607 mutex_unlock(&dev
->lock
);
611 static __poll_t
ir_lirc_poll(struct file
*file
, struct poll_table_struct
*wait
)
613 struct lirc_fh
*fh
= file
->private_data
;
614 struct rc_dev
*rcdev
= fh
->rc
;
617 poll_wait(file
, &fh
->wait_poll
, wait
);
619 if (!rcdev
->registered
) {
620 events
= EPOLLHUP
| EPOLLERR
;
621 } else if (rcdev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
622 if (fh
->rec_mode
== LIRC_MODE_SCANCODE
&&
623 !kfifo_is_empty(&fh
->scancodes
))
624 events
= EPOLLIN
| EPOLLRDNORM
;
626 if (fh
->rec_mode
== LIRC_MODE_MODE2
&&
627 !kfifo_is_empty(&fh
->rawir
))
628 events
= EPOLLIN
| EPOLLRDNORM
;
634 static ssize_t
ir_lirc_read_mode2(struct file
*file
, char __user
*buffer
,
637 struct lirc_fh
*fh
= file
->private_data
;
638 struct rc_dev
*rcdev
= fh
->rc
;
642 if (length
< sizeof(unsigned int) || length
% sizeof(unsigned int))
646 if (kfifo_is_empty(&fh
->rawir
)) {
647 if (file
->f_flags
& O_NONBLOCK
)
650 ret
= wait_event_interruptible(fh
->wait_poll
,
651 !kfifo_is_empty(&fh
->rawir
) ||
657 if (!rcdev
->registered
)
660 ret
= mutex_lock_interruptible(&rcdev
->lock
);
663 ret
= kfifo_to_user(&fh
->rawir
, buffer
, length
, &copied
);
664 mutex_unlock(&rcdev
->lock
);
667 } while (copied
== 0);
672 static ssize_t
ir_lirc_read_scancode(struct file
*file
, char __user
*buffer
,
675 struct lirc_fh
*fh
= file
->private_data
;
676 struct rc_dev
*rcdev
= fh
->rc
;
680 if (length
< sizeof(struct lirc_scancode
) ||
681 length
% sizeof(struct lirc_scancode
))
685 if (kfifo_is_empty(&fh
->scancodes
)) {
686 if (file
->f_flags
& O_NONBLOCK
)
689 ret
= wait_event_interruptible(fh
->wait_poll
,
690 !kfifo_is_empty(&fh
->scancodes
) ||
696 if (!rcdev
->registered
)
699 ret
= mutex_lock_interruptible(&rcdev
->lock
);
702 ret
= kfifo_to_user(&fh
->scancodes
, buffer
, length
, &copied
);
703 mutex_unlock(&rcdev
->lock
);
706 } while (copied
== 0);
711 static ssize_t
ir_lirc_read(struct file
*file
, char __user
*buffer
,
712 size_t length
, loff_t
*ppos
)
714 struct lirc_fh
*fh
= file
->private_data
;
715 struct rc_dev
*rcdev
= fh
->rc
;
717 if (rcdev
->driver_type
== RC_DRIVER_IR_RAW_TX
)
720 if (!rcdev
->registered
)
723 if (fh
->rec_mode
== LIRC_MODE_MODE2
)
724 return ir_lirc_read_mode2(file
, buffer
, length
);
725 else /* LIRC_MODE_SCANCODE */
726 return ir_lirc_read_scancode(file
, buffer
, length
);
729 static const struct file_operations lirc_fops
= {
730 .owner
= THIS_MODULE
,
731 .write
= ir_lirc_transmit_ir
,
732 .unlocked_ioctl
= ir_lirc_ioctl
,
734 .compat_ioctl
= ir_lirc_ioctl
,
736 .read
= ir_lirc_read
,
737 .poll
= ir_lirc_poll
,
738 .open
= ir_lirc_open
,
739 .release
= ir_lirc_close
,
743 static void lirc_release_device(struct device
*ld
)
745 struct rc_dev
*rcdev
= container_of(ld
, struct rc_dev
, lirc_dev
);
747 put_device(&rcdev
->dev
);
750 int ir_lirc_register(struct rc_dev
*dev
)
752 const char *rx_type
, *tx_type
;
755 minor
= ida_simple_get(&lirc_ida
, 0, RC_DEV_MAX
, GFP_KERNEL
);
759 device_initialize(&dev
->lirc_dev
);
760 dev
->lirc_dev
.class = lirc_class
;
761 dev
->lirc_dev
.parent
= &dev
->dev
;
762 dev
->lirc_dev
.release
= lirc_release_device
;
763 dev
->lirc_dev
.devt
= MKDEV(MAJOR(lirc_base_dev
), minor
);
764 dev_set_name(&dev
->lirc_dev
, "lirc%d", minor
);
766 INIT_LIST_HEAD(&dev
->lirc_fh
);
767 spin_lock_init(&dev
->lirc_fh_lock
);
769 cdev_init(&dev
->lirc_cdev
, &lirc_fops
);
771 err
= cdev_device_add(&dev
->lirc_cdev
, &dev
->lirc_dev
);
775 get_device(&dev
->dev
);
777 switch (dev
->driver_type
) {
778 case RC_DRIVER_SCANCODE
:
779 rx_type
= "scancode";
781 case RC_DRIVER_IR_RAW
:
794 dev_info(&dev
->dev
, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
795 dev
->driver_name
, minor
, rx_type
, tx_type
);
800 ida_simple_remove(&lirc_ida
, minor
);
804 void ir_lirc_unregister(struct rc_dev
*dev
)
809 dev_dbg(&dev
->dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
810 dev
->driver_name
, MINOR(dev
->lirc_dev
.devt
));
812 spin_lock_irqsave(&dev
->lirc_fh_lock
, flags
);
813 list_for_each_entry(fh
, &dev
->lirc_fh
, list
)
814 wake_up_poll(&fh
->wait_poll
, EPOLLHUP
| EPOLLERR
);
815 spin_unlock_irqrestore(&dev
->lirc_fh_lock
, flags
);
817 cdev_device_del(&dev
->lirc_cdev
, &dev
->lirc_dev
);
818 ida_simple_remove(&lirc_ida
, MINOR(dev
->lirc_dev
.devt
));
821 int __init
lirc_dev_init(void)
825 lirc_class
= class_create(THIS_MODULE
, "lirc");
826 if (IS_ERR(lirc_class
)) {
827 pr_err("class_create failed\n");
828 return PTR_ERR(lirc_class
);
831 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, RC_DEV_MAX
,
834 class_destroy(lirc_class
);
835 pr_err("alloc_chrdev_region failed\n");
839 pr_debug("IR Remote Control driver registered, major %d\n",
840 MAJOR(lirc_base_dev
));
845 void __exit
lirc_dev_exit(void)
847 class_destroy(lirc_class
);
848 unregister_chrdev_region(lirc_base_dev
, RC_DEV_MAX
);
851 struct rc_dev
*rc_dev_get_from_fd(int fd
)
853 struct fd f
= fdget(fd
);
858 return ERR_PTR(-EBADF
);
860 if (f
.file
->f_op
!= &lirc_fops
) {
862 return ERR_PTR(-EINVAL
);
865 fh
= f
.file
->private_data
;
868 get_device(&dev
->dev
);
874 MODULE_ALIAS("lirc_dev");