lirc-dev-intro.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. .. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later
  2. .. _lirc_dev_intro:
  3. ************
  4. Introduction
  5. ************
  6. LIRC stands for Linux Infrared Remote Control. The LIRC device interface is
  7. a bi-directional interface for transporting raw IR and decoded scancodes
  8. data between userspace and kernelspace. Fundamentally, it is just a chardev
  9. (/dev/lircX, for X = 0, 1, 2, ...), with a number of standard struct
  10. file_operations defined on it. With respect to transporting raw IR and
  11. decoded scancodes to and fro, the essential fops are read, write and ioctl.
  12. It is also possible to attach a BPF program to a LIRC device for decoding
  13. raw IR into scancodes.
  14. Example dmesg output upon a driver registering w/LIRC:
  15. .. code-block:: none
  16. $ dmesg |grep lirc_dev
  17. rc rc0: lirc_dev: driver mceusb registered at minor = 0, raw IR receiver, raw IR transmitter
  18. What you should see for a chardev:
  19. .. code-block:: none
  20. $ ls -l /dev/lirc*
  21. crw-rw---- 1 root root 248, 0 Jul 2 22:20 /dev/lirc0
  22. Note that the package `v4l-utils <https://git.linuxtv.org/v4l-utils.git/>`_
  23. contains tools for working with LIRC devices:
  24. - ir-ctl: can receive raw IR and transmit IR, as well as query LIRC
  25. device features.
  26. - ir-keytable: can load keymaps; allows you to set IR kernel protocols; load
  27. BPF IR decoders and test IR decoding. Some BPF IR decoders are also
  28. provided.
  29. .. _lirc_modes:
  30. **********
  31. LIRC modes
  32. **********
  33. LIRC supports some modes of receiving and sending IR codes, as shown
  34. on the following table.
  35. .. _lirc-mode-scancode:
  36. .. _lirc-scancode-flag-toggle:
  37. .. _lirc-scancode-flag-repeat:
  38. ``LIRC_MODE_SCANCODE``
  39. This mode is for both sending and receiving IR.
  40. For transmitting (aka sending), create a struct lirc_scancode with
  41. the desired scancode set in the ``scancode`` member, :c:type:`rc_proto`
  42. set to the :ref:`IR protocol <Remote_controllers_Protocols>`, and all other
  43. members set to 0. Write this struct to the lirc device.
  44. For receiving, you read struct lirc_scancode from the LIRC device.
  45. The ``scancode`` field is set to the received scancode and the
  46. :ref:`IR protocol <Remote_controllers_Protocols>` is set in
  47. :c:type:`rc_proto`. If the scancode maps to a valid key code, this is set
  48. in the ``keycode`` field, else it is set to ``KEY_RESERVED``.
  49. The ``flags`` can have ``LIRC_SCANCODE_FLAG_TOGGLE`` set if the toggle
  50. bit is set in protocols that support it (e.g. rc-5 and rc-6), or
  51. ``LIRC_SCANCODE_FLAG_REPEAT`` for when a repeat is received for protocols
  52. that support it (e.g. nec).
  53. In the Sanyo and NEC protocol, if you hold a button on remote, rather than
  54. repeating the entire scancode, the remote sends a shorter message with
  55. no scancode, which just means button is held, a "repeat". When this is
  56. received, the ``LIRC_SCANCODE_FLAG_REPEAT`` is set and the scancode and
  57. keycode is repeated.
  58. With nec, there is no way to distinguish "button hold" from "repeatedly
  59. pressing the same button". The rc-5 and rc-6 protocols have a toggle bit.
  60. When a button is released and pressed again, the toggle bit is inverted.
  61. If the toggle bit is set, the ``LIRC_SCANCODE_FLAG_TOGGLE`` is set.
  62. The ``timestamp`` field is filled with the time nanoseconds
  63. (in ``CLOCK_MONOTONIC``) when the scancode was decoded.
  64. .. _lirc-mode-mode2:
  65. ``LIRC_MODE_MODE2``
  66. The driver returns a sequence of pulse and space codes to userspace,
  67. as a series of u32 values.
  68. This mode is used only for IR receive.
  69. The upper 8 bits determine the packet type, and the lower 24 bits
  70. the payload. Use ``LIRC_VALUE()`` macro to get the payload, and
  71. the macro ``LIRC_MODE2()`` will give you the type, which
  72. is one of:
  73. ``LIRC_MODE2_PULSE``
  74. Signifies the presence of IR in microseconds, also known as *flash*.
  75. ``LIRC_MODE2_SPACE``
  76. Signifies absence of IR in microseconds, also known as *gap*.
  77. ``LIRC_MODE2_FREQUENCY``
  78. If measurement of the carrier frequency was enabled with
  79. :ref:`lirc_set_measure_carrier_mode` then this packet gives you
  80. the carrier frequency in Hertz.
  81. ``LIRC_MODE2_TIMEOUT``
  82. When the timeout set with :ref:`lirc_set_rec_timeout` expires due
  83. to no IR being detected, this packet will be sent, with the number
  84. of microseconds with no IR.
  85. ``LIRC_MODE2_OVERFLOW``
  86. Signifies that the IR receiver encounter an overflow, and some IR
  87. is missing. The IR data after this should be correct again. The
  88. actual value is not important, but this is set to 0xffffff by the
  89. kernel for compatibility with lircd.
  90. .. _lirc-mode-pulse:
  91. ``LIRC_MODE_PULSE``
  92. In pulse mode, a sequence of pulse/space integer values are written to the
  93. lirc device using :ref:`lirc-write`.
  94. The values are alternating pulse and space lengths, in microseconds. The
  95. first and last entry must be a pulse, so there must be an odd number
  96. of entries.
  97. This mode is used only for IR send.
  98. *************************************
  99. Data types used by LIRC_MODE_SCANCODE
  100. *************************************
  101. .. kernel-doc:: include/uapi/linux/lirc.h
  102. :identifiers: lirc_scancode rc_proto
  103. ********************
  104. BPF based IR decoder
  105. ********************
  106. The kernel has support for decoding the most common
  107. :ref:`IR protocols <Remote_controllers_Protocols>`, but there
  108. are many protocols which are not supported. To support these, it is possible
  109. to load an BPF program which does the decoding. This can only be done on
  110. LIRC devices which support reading raw IR.
  111. First, using the `bpf(2)`_ syscall with the ``BPF_LOAD_PROG`` argument,
  112. program must be loaded of type ``BPF_PROG_TYPE_LIRC_MODE2``. Once attached
  113. to the LIRC device, this program will be called for each pulse, space or
  114. timeout event on the LIRC device. The context for the BPF program is a
  115. pointer to a unsigned int, which is a :ref:`LIRC_MODE_MODE2 <lirc-mode-mode2>`
  116. value. When the program has decoded the scancode, it can be submitted using
  117. the BPF functions ``bpf_rc_keydown()`` or ``bpf_rc_repeat()``. Mouse or pointer
  118. movements can be reported using ``bpf_rc_pointer_rel()``.
  119. Once you have the file descriptor for the ``BPF_PROG_TYPE_LIRC_MODE2`` BPF
  120. program, it can be attached to the LIRC device using the `bpf(2)`_ syscall.
  121. The target must be the file descriptor for the LIRC device, and the
  122. attach type must be ``BPF_LIRC_MODE2``. No more than 64 BPF programs can be
  123. attached to a single LIRC device at a time.
  124. .. _bpf(2): http://man7.org/linux/man-pages/man2/bpf.2.html