0001 .. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later
0002
0003 .. _lirc_dev_intro:
0004
0005 ************
0006 Introduction
0007 ************
0008
0009 LIRC stands for Linux Infrared Remote Control. The LIRC device interface is
0010 a bi-directional interface for transporting raw IR and decoded scancodes
0011 data between userspace and kernelspace. Fundamentally, it is just a chardev
0012 (/dev/lircX, for X = 0, 1, 2, ...), with a number of standard struct
0013 file_operations defined on it. With respect to transporting raw IR and
0014 decoded scancodes to and fro, the essential fops are read, write and ioctl.
0015
0016 It is also possible to attach a BPF program to a LIRC device for decoding
0017 raw IR into scancodes.
0018
0019 Example dmesg output upon a driver registering w/LIRC:
0020
0021 .. code-block:: none
0022
0023 $ dmesg |grep lirc_dev
0024 rc rc0: lirc_dev: driver mceusb registered at minor = 0, raw IR receiver, raw IR transmitter
0025
0026 What you should see for a chardev:
0027
0028 .. code-block:: none
0029
0030 $ ls -l /dev/lirc*
0031 crw-rw---- 1 root root 248, 0 Jul 2 22:20 /dev/lirc0
0032
0033 Note that the package `v4l-utils <https://git.linuxtv.org/v4l-utils.git/>`_
0034 contains tools for working with LIRC devices:
0035
0036 - ir-ctl: can receive raw IR and transmit IR, as well as query LIRC
0037 device features.
0038
0039 - ir-keytable: can load keymaps; allows you to set IR kernel protocols; load
0040 BPF IR decoders and test IR decoding. Some BPF IR decoders are also
0041 provided.
0042
0043 .. _lirc_modes:
0044
0045 **********
0046 LIRC modes
0047 **********
0048
0049 LIRC supports some modes of receiving and sending IR codes, as shown
0050 on the following table.
0051
0052 .. _lirc-mode-scancode:
0053 .. _lirc-scancode-flag-toggle:
0054 .. _lirc-scancode-flag-repeat:
0055
0056 ``LIRC_MODE_SCANCODE``
0057
0058 This mode is for both sending and receiving IR.
0059
0060 For transmitting (aka sending), create a struct lirc_scancode with
0061 the desired scancode set in the ``scancode`` member, :c:type:`rc_proto`
0062 set to the :ref:`IR protocol <Remote_controllers_Protocols>`, and all other
0063 members set to 0. Write this struct to the lirc device.
0064
0065 For receiving, you read struct lirc_scancode from the LIRC device.
0066 The ``scancode`` field is set to the received scancode and the
0067 :ref:`IR protocol <Remote_controllers_Protocols>` is set in
0068 :c:type:`rc_proto`. If the scancode maps to a valid key code, this is set
0069 in the ``keycode`` field, else it is set to ``KEY_RESERVED``.
0070
0071 The ``flags`` can have ``LIRC_SCANCODE_FLAG_TOGGLE`` set if the toggle
0072 bit is set in protocols that support it (e.g. rc-5 and rc-6), or
0073 ``LIRC_SCANCODE_FLAG_REPEAT`` for when a repeat is received for protocols
0074 that support it (e.g. nec).
0075
0076 In the Sanyo and NEC protocol, if you hold a button on remote, rather than
0077 repeating the entire scancode, the remote sends a shorter message with
0078 no scancode, which just means button is held, a "repeat". When this is
0079 received, the ``LIRC_SCANCODE_FLAG_REPEAT`` is set and the scancode and
0080 keycode is repeated.
0081
0082 With nec, there is no way to distinguish "button hold" from "repeatedly
0083 pressing the same button". The rc-5 and rc-6 protocols have a toggle bit.
0084 When a button is released and pressed again, the toggle bit is inverted.
0085 If the toggle bit is set, the ``LIRC_SCANCODE_FLAG_TOGGLE`` is set.
0086
0087 The ``timestamp`` field is filled with the time nanoseconds
0088 (in ``CLOCK_MONOTONIC``) when the scancode was decoded.
0089
0090 .. _lirc-mode-mode2:
0091
0092 ``LIRC_MODE_MODE2``
0093
0094 The driver returns a sequence of pulse and space codes to userspace,
0095 as a series of u32 values.
0096
0097 This mode is used only for IR receive.
0098
0099 The upper 8 bits determine the packet type, and the lower 24 bits
0100 the payload. Use ``LIRC_VALUE()`` macro to get the payload, and
0101 the macro ``LIRC_MODE2()`` will give you the type, which
0102 is one of:
0103
0104 ``LIRC_MODE2_PULSE``
0105
0106 Signifies the presence of IR in microseconds, also known as *flash*.
0107
0108 ``LIRC_MODE2_SPACE``
0109
0110 Signifies absence of IR in microseconds, also known as *gap*.
0111
0112 ``LIRC_MODE2_FREQUENCY``
0113
0114 If measurement of the carrier frequency was enabled with
0115 :ref:`lirc_set_measure_carrier_mode` then this packet gives you
0116 the carrier frequency in Hertz.
0117
0118 ``LIRC_MODE2_TIMEOUT``
0119
0120 When the timeout set with :ref:`lirc_set_rec_timeout` expires due
0121 to no IR being detected, this packet will be sent, with the number
0122 of microseconds with no IR.
0123
0124 ``LIRC_MODE2_OVERFLOW``
0125
0126 Signifies that the IR receiver encounter an overflow, and some IR
0127 is missing. The IR data after this should be correct again. The
0128 actual value is not important, but this is set to 0xffffff by the
0129 kernel for compatibility with lircd.
0130
0131 .. _lirc-mode-pulse:
0132
0133 ``LIRC_MODE_PULSE``
0134
0135 In pulse mode, a sequence of pulse/space integer values are written to the
0136 lirc device using :ref:`lirc-write`.
0137
0138 The values are alternating pulse and space lengths, in microseconds. The
0139 first and last entry must be a pulse, so there must be an odd number
0140 of entries.
0141
0142 This mode is used only for IR send.
0143
0144 *************************************
0145 Data types used by LIRC_MODE_SCANCODE
0146 *************************************
0147
0148 .. kernel-doc:: include/uapi/linux/lirc.h
0149 :identifiers: lirc_scancode rc_proto
0150
0151 ********************
0152 BPF based IR decoder
0153 ********************
0154
0155 The kernel has support for decoding the most common
0156 :ref:`IR protocols <Remote_controllers_Protocols>`, but there
0157 are many protocols which are not supported. To support these, it is possible
0158 to load an BPF program which does the decoding. This can only be done on
0159 LIRC devices which support reading raw IR.
0160
0161 First, using the `bpf(2)`_ syscall with the ``BPF_LOAD_PROG`` argument,
0162 program must be loaded of type ``BPF_PROG_TYPE_LIRC_MODE2``. Once attached
0163 to the LIRC device, this program will be called for each pulse, space or
0164 timeout event on the LIRC device. The context for the BPF program is a
0165 pointer to a unsigned int, which is a :ref:`LIRC_MODE_MODE2 <lirc-mode-mode2>`
0166 value. When the program has decoded the scancode, it can be submitted using
0167 the BPF functions ``bpf_rc_keydown()`` or ``bpf_rc_repeat()``. Mouse or pointer
0168 movements can be reported using ``bpf_rc_pointer_rel()``.
0169
0170 Once you have the file descriptor for the ``BPF_PROG_TYPE_LIRC_MODE2`` BPF
0171 program, it can be attached to the LIRC device using the `bpf(2)`_ syscall.
0172 The target must be the file descriptor for the LIRC device, and the
0173 attach type must be ``BPF_LIRC_MODE2``. No more than 64 BPF programs can be
0174 attached to a single LIRC device at a time.
0175
0176 .. _bpf(2): http://man7.org/linux/man-pages/man2/bpf.2.html