1 // SPDX-License-Identifier: GPL-2.0
2 // test ir decoder
3 //
4 // Copyright (C) 2018 Sean Young <sean@mess.org>
5
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.
8 //
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.
12 //
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).
16 //
17 // The following CONFIG options must be enabled for the test to succeed:
18 // CONFIG_RC_CORE=y
19 // CONFIG_BPF_RAWIR_EVENT=y
20 // CONFIG_RC_LOOPBACK=y
21
22 // Steps:
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
29
30 #include <linux/bpf.h>
31 #include <linux/lirc.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <poll.h>
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42
43 #include "bpf_util.h"
44 #include <bpf/bpf.h>
45 #include <bpf/libbpf.h>
46
main(int argc,char ** argv)47 int main(int argc, char **argv)
48 {
49 struct bpf_object *obj;
50 int ret, lircfd, progfd, mode;
51 int testir = 0x1dead;
52 u32 prog_ids[10], prog_flags[10], prog_cnt;
53
54 if (argc != 2) {
55 printf("Usage: %s /dev/lircN\n", argv[0]);
56 return 2;
57 }
58
59 ret = bpf_prog_load("test_lirc_mode2_kern.o",
60 BPF_PROG_TYPE_LIRC_MODE2, &obj, &progfd);
61 if (ret) {
62 printf("Failed to load bpf program\n");
63 return 1;
64 }
65
66 lircfd = open(argv[1], O_RDWR | O_NONBLOCK);
67 if (lircfd == -1) {
68 printf("failed to open lirc device %s: %m\n", argv[1]);
69 return 1;
70 }
71
72 /* Let's try detach it before it was ever attached */
73 ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
74 if (ret != -1 || errno != ENOENT) {
75 printf("bpf_prog_detach2 not attached should fail: %m\n");
76 return 1;
77 }
78
79 mode = LIRC_MODE_SCANCODE;
80 if (ioctl(lircfd, LIRC_SET_REC_MODE, &mode)) {
81 printf("failed to set rec mode: %m\n");
82 return 1;
83 }
84
85 prog_cnt = 10;
86 ret = bpf_prog_query(lircfd, BPF_LIRC_MODE2, 0, prog_flags, prog_ids,
87 &prog_cnt);
88 if (ret) {
89 printf("Failed to query bpf programs on lirc device: %m\n");
90 return 1;
91 }
92
93 if (prog_cnt != 0) {
94 printf("Expected nothing to be attached\n");
95 return 1;
96 }
97
98 ret = bpf_prog_attach(progfd, lircfd, BPF_LIRC_MODE2, 0);
99 if (ret) {
100 printf("Failed to attach bpf to lirc device: %m\n");
101 return 1;
102 }
103
104 /* Write raw IR */
105 ret = write(lircfd, &testir, sizeof(testir));
106 if (ret != sizeof(testir)) {
107 printf("Failed to send test IR message: %m\n");
108 return 1;
109 }
110
111 struct pollfd pfd = { .fd = lircfd, .events = POLLIN };
112 struct lirc_scancode lsc;
113
114 poll(&pfd, 1, 100);
115
116 /* Read decoded IR */
117 ret = read(lircfd, &lsc, sizeof(lsc));
118 if (ret != sizeof(lsc)) {
119 printf("Failed to read decoded IR: %m\n");
120 return 1;
121 }
122
123 if (lsc.scancode != 0xdead || lsc.rc_proto != 64) {
124 printf("Incorrect scancode decoded\n");
125 return 1;
126 }
127
128 prog_cnt = 10;
129 ret = bpf_prog_query(lircfd, BPF_LIRC_MODE2, 0, prog_flags, prog_ids,
130 &prog_cnt);
131 if (ret) {
132 printf("Failed to query bpf programs on lirc device: %m\n");
133 return 1;
134 }
135
136 if (prog_cnt != 1) {
137 printf("Expected one program to be attached\n");
138 return 1;
139 }
140
141 /* Let's try detaching it now it is actually attached */
142 ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
143 if (ret) {
144 printf("bpf_prog_detach2: returned %m\n");
145 return 1;
146 }
147
148 return 0;
149 }
150