1 // SPDX-License-Identifier: GPL-2.0
4 // Copyright (C) 2018 Sean Young <sean@mess.org>
6 // When sending LIRC_MODE_SCANCODE, the IR will be encoded. rc-loopback
7 // will send this IR to the receiver side, where we try to read the decoded
8 // IR. Decoding happens in a separate kernel thread, so we will need to
9 // wait until that is scheduled, hence we use poll to check for read
12 #include <linux/lirc.h>
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
26 #include "../kselftest.h"
28 #define TEST_SCANCODES 10
29 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
30 #define SYSFS_PATH_MAX 256
31 #define DNAME_PATH_MAX 256
39 { RC_PROTO_RC5
, "rc-5", 0x1f7f, "rc-5" },
40 { RC_PROTO_RC5X_20
, "rc-5x-20", 0x1f7f3f, "rc-5" },
41 { RC_PROTO_RC5_SZ
, "rc-5-sz", 0x2fff, "rc-5-sz" },
42 { RC_PROTO_JVC
, "jvc", 0xffff, "jvc" },
43 { RC_PROTO_SONY12
, "sony-12", 0x1f007f, "sony" },
44 { RC_PROTO_SONY15
, "sony-15", 0xff007f, "sony" },
45 { RC_PROTO_SONY20
, "sony-20", 0x1fff7f, "sony" },
46 { RC_PROTO_NEC
, "nec", 0xffff, "nec" },
47 { RC_PROTO_NECX
, "nec-x", 0xffffff, "nec" },
48 { RC_PROTO_NEC32
, "nec-32", 0xffffffff, "nec" },
49 { RC_PROTO_SANYO
, "sanyo", 0x1fffff, "sanyo" },
50 { RC_PROTO_RC6_0
, "rc-6-0", 0xffff, "rc-6" },
51 { RC_PROTO_RC6_6A_20
, "rc-6-6a-20", 0xfffff, "rc-6" },
52 { RC_PROTO_RC6_6A_24
, "rc-6-6a-24", 0xffffff, "rc-6" },
53 { RC_PROTO_RC6_6A_32
, "rc-6-6a-32", 0xffffffff, "rc-6" },
54 { RC_PROTO_RC6_MCE
, "rc-6-mce", 0x00007fff, "rc-6" },
55 { RC_PROTO_SHARP
, "sharp", 0x1fff, "sharp" },
56 { RC_PROTO_IMON
, "imon", 0x7fffffff, "imon" },
57 { RC_PROTO_RCMM12
, "rcmm-12", 0x00000fff, "rc-mm" },
58 { RC_PROTO_RCMM24
, "rcmm-24", 0x00ffffff, "rc-mm" },
59 { RC_PROTO_RCMM32
, "rcmm-32", 0xffffffff, "rc-mm" },
62 int lirc_open(const char *rc
)
65 char buf
[SYSFS_PATH_MAX
+ DNAME_PATH_MAX
];
69 snprintf(buf
, sizeof(buf
), "/sys/class/rc/%s", rc
);
73 ksft_exit_fail_msg("cannot open %s: %m\n", buf
);
75 while ((dent
= readdir(d
)) != NULL
) {
76 if (!strncmp(dent
->d_name
, "lirc", 4)) {
77 snprintf(buf
, sizeof(buf
), "/dev/%s", dent
->d_name
);
83 ksft_exit_skip("cannot find lirc device for %s\n", rc
);
87 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
89 ksft_exit_fail_msg("cannot open: %s: %m\n", buf
);
94 int main(int argc
, char **argv
)
98 int rlircfd
, wlircfd
, protocolfd
, i
, n
;
103 ksft_exit_fail_msg("Usage: %s <write rcN> <read rcN>\n",
106 rlircfd
= lirc_open(argv
[2]);
107 mode
= LIRC_MODE_SCANCODE
;
108 if (ioctl(rlircfd
, LIRC_SET_REC_MODE
, &mode
))
109 ksft_exit_fail_msg("failed to set scancode rec mode %s: %m\n",
112 wlircfd
= lirc_open(argv
[1]);
113 if (ioctl(wlircfd
, LIRC_SET_SEND_MODE
, &mode
))
114 ksft_exit_fail_msg("failed to set scancode send mode %s: %m\n",
117 snprintf(buf
, sizeof(buf
), "/sys/class/rc/%s/protocols", argv
[2]);
118 protocolfd
= open(buf
, O_WRONLY
);
119 if (protocolfd
== -1)
120 ksft_exit_fail_msg("failed to open %s: %m\n", buf
);
122 printf("Sending IR on %s and receiving IR on %s.\n", argv
[1], argv
[2]);
124 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
125 if (write(protocolfd
, protocols
[i
].decoder
,
126 strlen(protocols
[i
].decoder
)) == -1)
127 ksft_exit_fail_msg("failed to set write decoder\n");
129 printf("Testing protocol %s for decoder %s (%d/%d)...\n",
130 protocols
[i
].name
, protocols
[i
].decoder
,
131 i
+ 1, (int)ARRAY_SIZE(protocols
));
133 for (n
= 0; n
< TEST_SCANCODES
; n
++) {
134 unsigned int scancode
= rand() & protocols
[i
].mask
;
135 unsigned int rc_proto
= protocols
[i
].proto
;
137 if (rc_proto
== RC_PROTO_RC6_MCE
)
138 scancode
|= 0x800f0000;
140 if (rc_proto
== RC_PROTO_NECX
&&
141 (((scancode
>> 16) ^ ~(scancode
>> 8)) & 0xff) == 0)
144 if (rc_proto
== RC_PROTO_NEC32
&&
145 (((scancode
>> 8) ^ ~scancode
) & 0xff) == 0)
148 if (rc_proto
== RC_PROTO_RCMM32
&&
149 (scancode
& 0x000c0000) != 0x000c0000 &&
150 scancode
& 0x00008000)
153 struct lirc_scancode lsc
= {
154 .rc_proto
= rc_proto
,
158 printf("Testing scancode:%x\n", scancode
);
160 while (write(wlircfd
, &lsc
, sizeof(lsc
)) < 0) {
164 ksft_exit_fail_msg("failed to send ir: %m\n");
167 struct pollfd pfd
= { .fd
= rlircfd
, .events
= POLLIN
};
168 struct lirc_scancode lsc2
;
174 while (read(rlircfd
, &lsc2
, sizeof(lsc2
)) < 0) {
178 ksft_test_result_error("no scancode decoded: %m\n");
186 if (lsc
.rc_proto
!= lsc2
.rc_proto
)
187 ksft_test_result_error("decoded protocol is different: %d\n",
190 else if (lsc
.scancode
!= lsc2
.scancode
)
191 ksft_test_result_error("decoded scancode is different: %llx\n",
204 if (ksft_get_fail_cnt() > 0)