Skip to content

Commit 01d3240

Browse files
seanyoungAlexei Starovoitov
authored and
Alexei Starovoitov
committed
media: bpf: add bpf function to report mouse movement
Some IR remotes have a directional pad or other pointer-like thing that can be used as a mouse. Make it possible to decode these types of IR protocols in BPF. Cc: [email protected] Signed-off-by: Sean Young <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ca5d1a7 commit 01d3240

File tree

7 files changed

+109
-23
lines changed

7 files changed

+109
-23
lines changed

drivers/media/rc/bpf-lirc.c

+24
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ static const struct bpf_func_proto rc_keydown_proto = {
5959
.arg4_type = ARG_ANYTHING,
6060
};
6161

62+
BPF_CALL_3(bpf_rc_pointer_rel, u32*, sample, s32, rel_x, s32, rel_y)
63+
{
64+
struct ir_raw_event_ctrl *ctrl;
65+
66+
ctrl = container_of(sample, struct ir_raw_event_ctrl, bpf_sample);
67+
68+
input_report_rel(ctrl->dev->input_dev, REL_X, rel_x);
69+
input_report_rel(ctrl->dev->input_dev, REL_Y, rel_y);
70+
input_sync(ctrl->dev->input_dev);
71+
72+
return 0;
73+
}
74+
75+
static const struct bpf_func_proto rc_pointer_rel_proto = {
76+
.func = bpf_rc_pointer_rel,
77+
.gpl_only = true,
78+
.ret_type = RET_INTEGER,
79+
.arg1_type = ARG_PTR_TO_CTX,
80+
.arg2_type = ARG_ANYTHING,
81+
.arg3_type = ARG_ANYTHING,
82+
};
83+
6284
static const struct bpf_func_proto *
6385
lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6486
{
@@ -67,6 +89,8 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6789
return &rc_repeat_proto;
6890
case BPF_FUNC_rc_keydown:
6991
return &rc_keydown_proto;
92+
case BPF_FUNC_rc_pointer_rel:
93+
return &rc_pointer_rel_proto;
7094
case BPF_FUNC_map_lookup_elem:
7195
return &bpf_map_lookup_elem_proto;
7296
case BPF_FUNC_map_update_elem:

include/uapi/linux/bpf.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,20 @@ union bpf_attr {
23012301
* payload and/or *pop* value being to large.
23022302
* Return
23032303
* 0 on success, or a negative error in case of failure.
2304+
*
2305+
* int bpf_rc_pointer_rel(void *ctx, s32 rel_x, s32 rel_y)
2306+
* Description
2307+
* This helper is used in programs implementing IR decoding, to
2308+
* report a successfully decoded pointer movement.
2309+
*
2310+
* The *ctx* should point to the lirc sample as passed into
2311+
* the program.
2312+
*
2313+
* This helper is only available is the kernel was compiled with
2314+
* the **CONFIG_BPF_LIRC_MODE2** configuration option set to
2315+
* "**y**".
2316+
* Return
2317+
* 0
23042318
*/
23052319
#define __BPF_FUNC_MAPPER(FN) \
23062320
FN(unspec), \
@@ -2394,7 +2408,8 @@ union bpf_attr {
23942408
FN(map_pop_elem), \
23952409
FN(map_peek_elem), \
23962410
FN(msg_push_data), \
2397-
FN(msg_pop_data),
2411+
FN(msg_pop_data), \
2412+
FN(rc_pointer_rel),
23982413

23992414
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
24002415
* function eBPF program intends to call

tools/include/uapi/linux/bpf.h

+16-2
Original file line numberDiff line numberDiff line change
@@ -2298,9 +2298,22 @@ union bpf_attr {
22982298
* if possible. Other errors can occur if input parameters are
22992299
* invalid either due to *start* byte not being valid part of msg
23002300
* payload and/or *pop* value being to large.
2301+
* Return
2302+
* 0 on success, or a negative error in case of failure.
2303+
*
2304+
* int bpf_rc_pointer_rel(void *ctx, s32 rel_x, s32 rel_y)
2305+
* Description
2306+
* This helper is used in programs implementing IR decoding, to
2307+
* report a successfully decoded pointer movement.
2308+
*
2309+
* The *ctx* should point to the lirc sample as passed into
2310+
* the program.
23012311
*
2312+
* This helper is only available is the kernel was compiled with
2313+
* the **CONFIG_BPF_LIRC_MODE2** configuration option set to
2314+
* "**y**".
23022315
* Return
2303-
* 0 on success, or a negative erro in case of failure.
2316+
* 0
23042317
*/
23052318
#define __BPF_FUNC_MAPPER(FN) \
23062319
FN(unspec), \
@@ -2394,7 +2407,8 @@ union bpf_attr {
23942407
FN(map_pop_elem), \
23952408
FN(map_peek_elem), \
23962409
FN(msg_push_data), \
2397-
FN(msg_pop_data),
2410+
FN(msg_pop_data), \
2411+
FN(rc_pointer_rel),
23982412

23992413
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
24002414
* function eBPF program intends to call

tools/testing/selftests/bpf/bpf_helpers.h

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ static int (*bpf_skb_vlan_push)(void *ctx, __be16 vlan_proto, __u16 vlan_tci) =
170170
(void *) BPF_FUNC_skb_vlan_push;
171171
static int (*bpf_skb_vlan_pop)(void *ctx) =
172172
(void *) BPF_FUNC_skb_vlan_pop;
173+
static int (*bpf_rc_pointer_rel)(void *ctx, int rel_x, int rel_y) =
174+
(void *) BPF_FUNC_rc_pointer_rel;
173175

174176
/* llvm builtin functions that eBPF C program may use to
175177
* emit BPF_LD_ABS and BPF_LD_IND instructions

tools/testing/selftests/bpf/test_lirc_mode2.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ do
2121
if grep -q DRV_NAME=rc-loopback $i/uevent
2222
then
2323
LIRCDEV=$(grep DEVNAME= $i/lirc*/uevent | sed sQDEVNAME=Q/dev/Q)
24+
INPUTDEV=$(grep DEVNAME= $i/input*/event*/uevent | sed sQDEVNAME=Q/dev/Q)
2425
fi
2526
done
2627

2728
if [ -n $LIRCDEV ];
2829
then
2930
TYPE=lirc_mode2
30-
./test_lirc_mode2_user $LIRCDEV
31+
./test_lirc_mode2_user $LIRCDEV $INPUTDEV
3132
ret=$?
3233
if [ $ret -ne 0 ]; then
3334
echo -e ${RED}"FAIL: $TYPE"${NC}

tools/testing/selftests/bpf/test_lirc_mode2_kern.c

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ int bpf_decoder(unsigned int *sample)
1515

1616
if (duration & 0x10000)
1717
bpf_rc_keydown(sample, 0x40, duration & 0xffff, 0);
18+
if (duration & 0x20000)
19+
bpf_rc_pointer_rel(sample, (duration >> 8) & 0xff,
20+
duration & 0xff);
1821
}
1922

2023
return 0;

tools/testing/selftests/bpf/test_lirc_mode2_user.c

+46-19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <linux/bpf.h>
3131
#include <linux/lirc.h>
32+
#include <linux/input.h>
3233
#include <errno.h>
3334
#include <stdio.h>
3435
#include <stdlib.h>
@@ -47,12 +48,13 @@
4748
int main(int argc, char **argv)
4849
{
4950
struct bpf_object *obj;
50-
int ret, lircfd, progfd, mode;
51-
int testir = 0x1dead;
51+
int ret, lircfd, progfd, inputfd;
52+
int testir1 = 0x1dead;
53+
int testir2 = 0x20101;
5254
u32 prog_ids[10], prog_flags[10], prog_cnt;
5355

54-
if (argc != 2) {
55-
printf("Usage: %s /dev/lircN\n", argv[0]);
56+
if (argc != 3) {
57+
printf("Usage: %s /dev/lircN /dev/input/eventM\n", argv[0]);
5658
return 2;
5759
}
5860

@@ -76,9 +78,9 @@ int main(int argc, char **argv)
7678
return 1;
7779
}
7880

79-
mode = LIRC_MODE_SCANCODE;
80-
if (ioctl(lircfd, LIRC_SET_REC_MODE, &mode)) {
81-
printf("failed to set rec mode: %m\n");
81+
inputfd = open(argv[2], O_RDONLY | O_NONBLOCK);
82+
if (inputfd == -1) {
83+
printf("failed to open input device %s: %m\n", argv[1]);
8284
return 1;
8385
}
8486

@@ -102,29 +104,54 @@ int main(int argc, char **argv)
102104
}
103105

104106
/* Write raw IR */
105-
ret = write(lircfd, &testir, sizeof(testir));
106-
if (ret != sizeof(testir)) {
107+
ret = write(lircfd, &testir1, sizeof(testir1));
108+
if (ret != sizeof(testir1)) {
107109
printf("Failed to send test IR message: %m\n");
108110
return 1;
109111
}
110112

111-
struct pollfd pfd = { .fd = lircfd, .events = POLLIN };
112-
struct lirc_scancode lsc;
113+
struct pollfd pfd = { .fd = inputfd, .events = POLLIN };
114+
struct input_event event;
113115

114-
poll(&pfd, 1, 100);
116+
for (;;) {
117+
poll(&pfd, 1, 100);
115118

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;
119+
/* Read decoded IR */
120+
ret = read(inputfd, &event, sizeof(event));
121+
if (ret != sizeof(event)) {
122+
printf("Failed to read decoded IR: %m\n");
123+
return 1;
124+
}
125+
126+
if (event.type == EV_MSC && event.code == MSC_SCAN &&
127+
event.value == 0xdead) {
128+
break;
129+
}
121130
}
122131

123-
if (lsc.scancode != 0xdead || lsc.rc_proto != 64) {
124-
printf("Incorrect scancode decoded\n");
132+
/* Write raw IR */
133+
ret = write(lircfd, &testir2, sizeof(testir2));
134+
if (ret != sizeof(testir2)) {
135+
printf("Failed to send test IR message: %m\n");
125136
return 1;
126137
}
127138

139+
for (;;) {
140+
poll(&pfd, 1, 100);
141+
142+
/* Read decoded IR */
143+
ret = read(inputfd, &event, sizeof(event));
144+
if (ret != sizeof(event)) {
145+
printf("Failed to read decoded IR: %m\n");
146+
return 1;
147+
}
148+
149+
if (event.type == EV_REL && event.code == REL_Y &&
150+
event.value == 1 ) {
151+
break;
152+
}
153+
}
154+
128155
prog_cnt = 10;
129156
ret = bpf_prog_query(lircfd, BPF_LIRC_MODE2, 0, prog_flags, prog_ids,
130157
&prog_cnt);

0 commit comments

Comments
 (0)