1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Red hat */
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 #include "hid_bpf_helpers.h"
7 
8 char _license[] SEC("license") = "GPL";
9 
10 struct attach_prog_args {
11 	int prog_fd;
12 	unsigned int hid;
13 	int retval;
14 };
15 
16 __u64 callback_check = 52;
17 __u64 callback2_check = 52;
18 
19 SEC("?fmod_ret/hid_bpf_device_event")
20 int BPF_PROG(hid_first_event, struct hid_bpf_ctx *hid_ctx)
21 {
22 	__u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
23 
24 	if (!rw_data)
25 		return 0; /* EPERM check */
26 
27 	callback_check = rw_data[1];
28 
29 	rw_data[2] = rw_data[1] + 5;
30 
31 	return hid_ctx->size;
32 }
33 
34 SEC("?fmod_ret/hid_bpf_device_event")
35 int BPF_PROG(hid_change_report_id, struct hid_bpf_ctx *hid_ctx)
36 {
37 	__u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
38 
39 	if (!rw_data)
40 		return 0; /* EPERM check */
41 
42 	rw_data[0] = 2;
43 
44 	return 9;
45 }
46 
47 SEC("syscall")
48 int attach_prog(struct attach_prog_args *ctx)
49 {
50 	ctx->retval = hid_bpf_attach_prog(ctx->hid,
51 					  ctx->prog_fd,
52 					  0);
53 	return 0;
54 }
55