# Driver Model

      |  +---------------------------------------------------------------+
    S |  | 'gpm -t msc -m /var/run/lirc/lircm' or a well configured X    |
    O |  | (see section 'Configuring lircmd') for details                |
    F |  +--------------+------------------------------------------------+
    T |                 |
    W |   /var/run/lirc/lircm (named pipe (FIFO) ==> one connection)
    A |                 |
    R |     +-----------+-----------+   +----------------------------------+
    E |     | mouse daemon (lircmd) |   | tools (irexec, irxevent, ...)    |
      |     | configured with       |   | configured with ~/.config/lircrc |
      |     | lircmd.conf           |   |                                  |
      |     +-----------+-----------+   +-----------+----------------------+
      |                 |                           |
      |                 +-------------+-------------+
      |                               |
      |              /var/run/lirc/lircd (socket ==> multiple connections)
      |                               |
    S |              +----------------+--------------------------+
    O |              | decoder daemon (lircd), irrecord or mode2 |    TCP/IP
    F |              | lircd is configured through lircd.conf    +---  port
    T | User space   |                                           |     8765
    W |              +------------------+------------------------+
    A |                                 |            |
    R |                                 |            |
    E |                                 |            |
      |                                 |   /dev/uinput (Linux input layer)
      |                                 |
      |                                 |
      +---------------------------------+----------------------------------
      | Kernel space                    |              (character device
      |                                 |                  driver ==>
      |                    +------------+----------+    one connection)
      |                    |                       |
      |                /dev/lirc               /dev/ttySx
      |                    |                       |
      |       +------------+-------------+   +-----+---------------+
      |       |  LIRC device driver      |   | Linux serial driver |
      |       | (with ioctl-interface)   |   |                     |
      |       +------------+-------------+   +----------+----------+
      |                    |                            |
    --+--------------------+----------------------------+------------------
      |                    |                            |
      |         +----------+------------+               |
      |         |                       |               |
      | +-------+----------------+ +----+-----+ +-------+-----------------+
    H | | serial / parallel port | | TV cards | | Irman/RemoteMaster/etc. |
    W | +------------------------+ +----------+ +-------------------------+
      |

# Formats

- /dev/lirc:

  highly depends on the mode selected with ioctls:

  LIRC_MODE_MODE2

    outputs packets containing an int value describing a IR signal

    - bits 0-23 contain the length of the pulse/space in microseconds
    - bits 31-24 can be:
      - 0: space
      - 1: pulse
      - 2: timeout (has to be enabled using LIRC_SET_REC_TIMEOUT ioctl and is not supported by all receivers)
    - all other values are reserved

    Lengths greater than or equal to 16 seconds are clamped to 0xffffff.

    References:

    - drivers/lirc_serial/lirc_serial.c
    - drivers/lirc_parallel/lirc_parallel.c
    - tools/mode2.c (dumps the output from the driver to stdout)
  LIRC_MODE_LIRCCODE

    outputs codes of configurable length in big endian byte order

- /var/run/lirc/lircd:

  outputs strings containing all information about the remote and the pressed button.

  References:

  - daemons/lircd.c
  - tools/irw.c
- /var/run/lirc/lircm:
  - MouseSystems
    5 byte packets:
    - byte 1: button information
    - byte 2: change on X axis
    - byte 3: change on Y axis
    - byte 4,5: 0
  - IMPS/2
    4 byte packets: check the source code for details
  - IntelliMouse
    4 byte packets: check the source code for details

  References:

  - daemons/lircmd.c

# Writing TV card drivers using lirc_dev

The lirc_dev module is a helper and abstraction layer for other modules. It registers /dev/lirc device in a system (including support for devfs) and waits for plugin registration. After that it serves device requests (open, read, poll, ioctl, close) and if needed calls callback functions from plugin(s) to communicate with the physical device.

Plugins can be registered and unregistered many times. The current implementation allows two concurrent plugins, but can be easily changed by increasing the MAX_IRCTL_DEVICES definition. It also allows receiving of scan codes, which have more than 8 bits. Current limit for a scan code is 16*8 bits and also can be changed by increasing the BUFLEN definition.

For an API description see lirc_dev.h. The lirc_gpio module can be treated as examples of using this API.
This code contains many lines with debug messages (activated by debug option) and they will sustain until more tests will be performed.

Warning: Due to the used kernel API it requires kernel 2.2.4 or higher.
Any suggestions and questions are welcome. Artur Lipowski

# Writing Applications for LIRC

As LIRC is able to both receive and send IR commands there are two possible types of applications. Programs that send IR commands like xrc and irsend or programs that receive commands like irexec, irxevent and irpty. Both types of applications will have to connect to the lircd daemon using the socket interface usually located in /var/run/lirc/lircd. Communication on the socket uses human readable format. The end of a line is indicated by a newline character.

Whenever lircd receives a IR signal it will broadcast the following string to each client:

      <code> <repeat count> <button name> <remote control name>

code is a 64-bit encoding (in hexadecimal representation) of the IR signal. It's usage in applications is deprecated and should be ignored. The repeat count shows how long the user has been holding down a button. The counter will start at 0 and increment each time a new IR signal has been received. The button name and remote control name are defined in the lircd config file. Their purpose should be quite self-explanatory. They must not contain any whitespace.
The only other situation when lircd broadcasts to all clients is when it receives the SIGHUP signal and successfully re-reads its config file. Then it will send a SIGHUP packet to its clients indicating that its configuration might have changed. This feature is e.g. used in xrc to rebuild the list of supported remote controls each time lircd's configuration changes. The format of the packet will be explained later.

NOTE: As of 0.9.2+, this way of sending data is obsolete since sending is supported directly in the API. See the API documentation, Applications that want to send out IR commands can use the following commands:

      SEND_ONCE <remote control name> <button name> [<repeat count>]
      SEND_START <remote control name> <button name>
      SEND_STOP <remote control name> <button name>

The SEND_ONCE directive tells lircd to send the IR signal associated with the given remote control and button name, and then repeat it repeat count times. repeat count is a decimal number between 0 and repeat_max. The latter can be given as a command line argument to lircd, and defaults to 600. If repeat count is not specified or is less than the minimum number of repeats for the selected remote control, the minimum value will be used. SEND_START tells lircd to start repeating the given button until it receives a SEND_STOP command. However, the number of repeats is limited to repeat_max. lircd won't accept any new send commands while it is repeating.

lircd also understands the following commands:

      VERSION
      LIST [<remote control name>]

The response to the VERSION command will be a packet containing lircd's version.
The LIST command without further arguments can be used to get a list of all remote controls known to lircd. If a name of a supported remote control is given as argument all buttons of the given remote control are listed in the reply packet. Have a look at xrc for an example how this can be used.

There still remains to explain the format of lircd's reply packets. Here is a formal description of the packets:

      BEGIN
      <command>
      [SUCCESS|ERROR]
      [DATA
      n
      n lines of data]
      END

The protocol guarantees that broadcasted messages won't interfere with reply packets. But broadcasts may appear at any point between packets. command is the command lircd is currently processing. Its an exact copy of the command the client application has sent. The only exception are SIGHUP packages where command is substituted with SIGHUP. Note that SIGHUP packages may appear just after you have sent a command to lircd, so you have to make sure you don't confuse them with replies. SIGHUP packages come without any further data while each reply to a command contains either SUCCESS or ERROR indicating the result of processing the command. In case of an error the following data is a message explaining the problem. This message can be used to create an error message for the user.
If the command was successful, data is only sent for the commands that return some information. Note that a packet containing 0 lines of data can be a valid reply.

## The lirc_client library

If you only want to make your application to send or receive IR commands and if you don't want to mess with all the protocol stuff you can use the lirc_client api.

# lircrcd protocol

lircrcd syntactically uses the same protocol as lircd described in the last section. It supports the following commands:

IDENT ident

  Each program connecting to lircrcd identifies itself using this program. ident is the string that is used in the prog token inside the .lircrc file.

CODE code

  When the client receives the code string from lircd it will send it to lircrcd and will receive back the applicable config string from the .lircrc config file. It should resend the CODE command until nothing is returned back which means that nothing (more) should happen in response to code. This command is used each time the lirc_code2char() function is called by a client.

GETMODE

  lircrcd will return the current mode string.

# Hardware Abstraction Layer (HAL) integration

If your system support the FreeDesktop Hardware Abstraction Layer (HAL), you will want to install the file contrib/hal/20-ircontrol-lirc.fdi in ${datarootdir}/hal/fdi/information/20thirdparty

This file can be generated at will using contrib/hal/gen-hal-fdi.pl.
Installing 20-ircontrol-lirc.fdi allow applications developers to detect USB remotes easily by searching for HAL devices with info.capabilities=input.remote_control

This is a work in progress. We will also provide more information in the future, such as the driver/manufacturer/device name(s), and possibly event reporting.

# Note for configuration application developers

If you want to make a configuration application, lirc provides a parseable list of LIRC supported devices.
This file is generated at compilation time, along with its HTML equivalent and is available as doc/lirc.hwdb in the source tree. It should also be installed by the binary packages of your prefered distribution (bug report otherwise!).
The format is:
[remote controls type]
description;driver;lirc driver;HW_DEFAULT;lircd_conf;
The HW_DEFAULT entry is always empty.

# Known bugs

- The general lirc bugtracker is at the sourceforge website.

  If you use the lirc_serial or lirc_parallel driver regularly to transmit infra-red signals you might notice that your system clock will slow down. During transmit the driver turns off interrupts and hence some clock interrupts might get lost causing system clock inaccuracy. Unfortunately in order to ensure a good signal timing interrupts have to be disabled. Currently no work-around is known for this problem except using a program like netdate to synchronize your system clock regularly.

- The lirc_serial and lirc_parallel drivers measure the time between interrupts on the serial resp. parallel port to get a pulse and space representation of the incoming infra-red signal. If interrupts are disabled by the CPU for a rather long time (>100 µs, which happens often e.g. during heavy IDE disk activity) some interrupts might get lost and the incoming data stream becomes disturbed. In this case decoding of the infra-red signal will fail. This is the downside of the really simple receiver circuits and can't be addressed in software except keeping the time where interrupts are disabled to a minimum.

  If you are using an IDE system you might want to try calling hdparm -u1 -d1 for all of your drives. This enables DMA for the drive and allows the driver to unmask other interrupts during handling of a disk interrupt. But be aware that this can be dangerous for some (buggy) IDE chipsets. Consult the hdparm man page for further information.

[LIRC homepage]
The LIRC Manual, last update: 10-June-2014