1 // SPDX-License-Identifier: GPL-2.0
4 // Copyright (C) 2018 Sean Young <sean@mess.org>
6 // A lirc chardev is a device representing a consumer IR (cir) device which
7 // can receive infrared signals from remote control and/or transmit IR.
9 // IR is sent as a series of pulses and space somewhat like morse code. The
10 // BPF program can decode this into scancodes so that rc-core can translate
11 // this into input key codes using the rc keymap.
13 // This test works by sending IR over rc-loopback, so the IR is processed by
14 // BPF and then decoded into scancodes. The lirc chardev must be the one
15 // associated with rc-loopback, see the output of ir-keytable(1).
17 // The following CONFIG options must be enabled for the test to succeed:
19 // CONFIG_BPF_RAWIR_EVENT=y
20 // CONFIG_RC_LOOPBACK=y
23 // 1. Open the /dev/lircN device for rc-loopback (given on command line)
24 // 2. Attach bpf_lirc_mode2 program which decodes some IR.
25 // 3. Send some IR to the same IR device; since it is loopback, this will
26 // end up in the bpf program
27 // 4. bpf program should decode IR and report keycode
28 // 5. We can read keycode from same /dev/lirc device
30 #include <linux/bpf.h>
31 #include <linux/input.h>
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
45 #include <bpf/libbpf.h>
47 #include "testing_helpers.h"
49 int main(int argc, char **argv)
51 struct bpf_object *obj;
52 int ret, lircfd, progfd, inputfd;
53 int testir1 = 0x1dead;
54 int testir2 = 0x20101;
55 u32 prog_ids[10], prog_flags[10], prog_cnt;
58 printf("Usage: %s /dev/lircN /dev/input/eventM\n", argv[0]);
62 ret = bpf_prog_test_load("test_lirc_mode2_kern.o",
63 BPF_PROG_TYPE_LIRC_MODE2, &obj, &progfd);
65 printf("Failed to load bpf program\n");
69 lircfd = open(argv[1], O_RDWR | O_NONBLOCK);
71 printf("failed to open lirc device %s: %m\n", argv[1]);
75 /* Let's try detach it before it was ever attached */
76 ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
77 if (ret != -1 || errno != ENOENT) {
78 printf("bpf_prog_detach2 not attached should fail: %m\n");
82 inputfd = open(argv[2], O_RDONLY | O_NONBLOCK);
84 printf("failed to open input device %s: %m\n", argv[1]);
89 ret = bpf_prog_query(lircfd, BPF_LIRC_MODE2, 0, prog_flags, prog_ids,
92 printf("Failed to query bpf programs on lirc device: %m\n");
97 printf("Expected nothing to be attached\n");
101 ret = bpf_prog_attach(progfd, lircfd, BPF_LIRC_MODE2, 0);
103 printf("Failed to attach bpf to lirc device: %m\n");
108 ret = write(lircfd, &testir1, sizeof(testir1));
109 if (ret != sizeof(testir1)) {
110 printf("Failed to send test IR message: %m\n");
114 struct pollfd pfd = { .fd = inputfd, .events = POLLIN };
115 struct input_event event;
120 /* Read decoded IR */
121 ret = read(inputfd, &event, sizeof(event));
122 if (ret != sizeof(event)) {
123 printf("Failed to read decoded IR: %m\n");
127 if (event.type == EV_MSC && event.code == MSC_SCAN &&
128 event.value == 0xdead) {
134 ret = write(lircfd, &testir2, sizeof(testir2));
135 if (ret != sizeof(testir2)) {
136 printf("Failed to send test IR message: %m\n");
143 /* Read decoded IR */
144 ret = read(inputfd, &event, sizeof(event));
145 if (ret != sizeof(event)) {
146 printf("Failed to read decoded IR: %m\n");
150 if (event.type == EV_REL && event.code == REL_Y &&
157 ret = bpf_prog_query(lircfd, BPF_LIRC_MODE2, 0, prog_flags, prog_ids,
160 printf("Failed to query bpf programs on lirc device: %m\n");
165 printf("Expected one program to be attached\n");
169 /* Let's try detaching it now it is actually attached */
170 ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
172 printf("bpf_prog_detach2: returned %m\n");