Skip to content

Commit a56a256

Browse files
bentissJiri Kosina
authored andcommitted
samples/hid: add Surface Dial example
Add a more complete HID-BPF example. Signed-off-by: Benjamin Tissoires <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 6008105 commit a56a256

File tree

5 files changed

+368
-1
lines changed

5 files changed

+368
-1
lines changed

samples/hid/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
hid_mouse
3+
hid_surface_dial
34
*.out
45
*.skel.h
56
/vmlinux.h

samples/hid/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pound := \#
77

88
# List of programs to build
99
tprogs-y += hid_mouse
10+
tprogs-y += hid_surface_dial
1011

1112
# Libbpf dependencies
1213
LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
@@ -19,6 +20,7 @@ EXTRA_HEADERS := hid_bpf_attach.h
1920
EXTRA_BPF_HEADERS := hid_bpf_helpers.h
2021

2122
hid_mouse-objs := hid_mouse.o
23+
hid_surface_dial-objs := hid_surface_dial.o
2224

2325
# Tell kbuild to always build the programs
2426
always-y := $(tprogs-y)
@@ -156,6 +158,7 @@ libbpf_hdrs: $(LIBBPF)
156158
.PHONY: libbpf_hdrs
157159

158160
$(obj)/hid_mouse.o: $(obj)/hid_mouse.skel.h
161+
$(obj)/hid_surface_dial.o: $(obj)/hid_surface_dial.skel.h
159162

160163
-include $(HID_SAMPLES_PATH)/Makefile.target
161164

@@ -201,10 +204,11 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(EXTRA_BPF_HEADERS_SRC) $(obj)/vmlinux.h
201204
-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
202205
-c $(filter %.bpf.c,$^) -o $@
203206

204-
LINKED_SKELS := hid_mouse.skel.h
207+
LINKED_SKELS := hid_mouse.skel.h hid_surface_dial.skel.h
205208
clean-files += $(LINKED_SKELS)
206209

207210
hid_mouse.skel.h-deps := hid_mouse.bpf.o hid_bpf_attach.bpf.o
211+
hid_surface_dial.skel.h-deps := hid_surface_dial.bpf.o hid_bpf_attach.bpf.o
208212

209213
LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
210214

samples/hid/hid_bpf_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
1010
unsigned int offset,
1111
const size_t __sz) __ksym;
1212
extern int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, u32 flags) __ksym;
13+
extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;
14+
extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;
1315
extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
1416
__u8 *data,
1517
size_t buf__sz,

samples/hid/hid_surface_dial.bpf.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (c) 2022 Benjamin Tissoires
3+
*/
4+
5+
#include "vmlinux.h"
6+
#include <bpf/bpf_helpers.h>
7+
#include <bpf/bpf_tracing.h>
8+
#include "hid_bpf_helpers.h"
9+
10+
#define HID_UP_BUTTON 0x0009
11+
#define HID_GD_WHEEL 0x0038
12+
13+
SEC("fmod_ret/hid_bpf_device_event")
14+
int BPF_PROG(hid_event, struct hid_bpf_ctx *hctx)
15+
{
16+
__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 9 /* size */);
17+
18+
if (!data)
19+
return 0; /* EPERM check */
20+
21+
/* Touch */
22+
data[1] &= 0xfd;
23+
24+
/* X */
25+
data[4] = 0;
26+
data[5] = 0;
27+
28+
/* Y */
29+
data[6] = 0;
30+
data[7] = 0;
31+
32+
return 0;
33+
}
34+
35+
/* 72 == 360 / 5 -> 1 report every 5 degrees */
36+
int resolution = 72;
37+
int physical = 5;
38+
39+
struct haptic_syscall_args {
40+
unsigned int hid;
41+
int retval;
42+
};
43+
44+
static __u8 haptic_data[8];
45+
46+
SEC("syscall")
47+
int set_haptic(struct haptic_syscall_args *args)
48+
{
49+
struct hid_bpf_ctx *ctx;
50+
const size_t size = sizeof(haptic_data);
51+
u16 *res;
52+
int ret;
53+
54+
if (size > sizeof(haptic_data))
55+
return -7; /* -E2BIG */
56+
57+
ctx = hid_bpf_allocate_context(args->hid);
58+
if (!ctx)
59+
return -1; /* EPERM check */
60+
61+
haptic_data[0] = 1; /* report ID */
62+
63+
ret = hid_bpf_hw_request(ctx, haptic_data, size, HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
64+
65+
bpf_printk("probed/remove event ret value: %d", ret);
66+
bpf_printk("buf: %02x %02x %02x",
67+
haptic_data[0],
68+
haptic_data[1],
69+
haptic_data[2]);
70+
bpf_printk(" %02x %02x %02x",
71+
haptic_data[3],
72+
haptic_data[4],
73+
haptic_data[5]);
74+
bpf_printk(" %02x %02x",
75+
haptic_data[6],
76+
haptic_data[7]);
77+
78+
/* whenever resolution multiplier is not 3600, we have the fixed report descriptor */
79+
res = (u16 *)&haptic_data[1];
80+
if (*res != 3600) {
81+
// haptic_data[1] = 72; /* resolution multiplier */
82+
// haptic_data[2] = 0; /* resolution multiplier */
83+
// haptic_data[3] = 0; /* Repeat Count */
84+
haptic_data[4] = 3; /* haptic Auto Trigger */
85+
// haptic_data[5] = 5; /* Waveform Cutoff Time */
86+
// haptic_data[6] = 80; /* Retrigger Period */
87+
// haptic_data[7] = 0; /* Retrigger Period */
88+
} else {
89+
haptic_data[4] = 0;
90+
}
91+
92+
ret = hid_bpf_hw_request(ctx, haptic_data, size, HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
93+
94+
bpf_printk("set haptic ret value: %d -> %d", ret, haptic_data[4]);
95+
96+
args->retval = ret;
97+
98+
hid_bpf_release_context(ctx);
99+
100+
return 0;
101+
}
102+
103+
/* Convert REL_DIAL into REL_WHEEL */
104+
SEC("fmod_ret/hid_bpf_rdesc_fixup")
105+
int BPF_PROG(hid_rdesc_fixup, struct hid_bpf_ctx *hctx)
106+
{
107+
__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 4096 /* size */);
108+
__u16 *res, *phys;
109+
110+
if (!data)
111+
return 0; /* EPERM check */
112+
113+
/* Convert TOUCH into a button */
114+
data[31] = HID_UP_BUTTON;
115+
data[33] = 2;
116+
117+
/* Convert REL_DIAL into REL_WHEEL */
118+
data[45] = HID_GD_WHEEL;
119+
120+
/* Change Resolution Multiplier */
121+
phys = (__u16 *)&data[61];
122+
*phys = physical;
123+
res = (__u16 *)&data[66];
124+
*res = resolution;
125+
126+
/* Convert X,Y from Abs to Rel */
127+
data[88] = 0x06;
128+
data[98] = 0x06;
129+
130+
return 0;
131+
}
132+
133+
char _license[] SEC("license") = "GPL";
134+
u32 _version SEC("version") = 1;

0 commit comments

Comments
 (0)