xref: /linux/tools/testing/selftests/hid/progs/hid.c (revision 4f7153cf461ed0f78d8da8c9fd02d38728a76b90)
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 
56 struct hid_hw_request_syscall_args {
57 	/* data needs to come at offset 0 so we can use it in calls */
58 	__u8 data[10];
59 	unsigned int hid;
60 	int retval;
61 	size_t size;
62 	enum hid_report_type type;
63 	__u8 request_type;
64 };
65 
66 SEC("syscall")
67 int hid_user_raw_request(struct hid_hw_request_syscall_args *args)
68 {
69 	struct hid_bpf_ctx *ctx;
70 	const size_t size = args->size;
71 	int i, ret = 0;
72 
73 	if (size > sizeof(args->data))
74 		return -7; /* -E2BIG */
75 
76 	ctx = hid_bpf_allocate_context(args->hid);
77 	if (!ctx)
78 		return -1; /* EPERM check */
79 
80 	ret = hid_bpf_hw_request(ctx,
81 				 args->data,
82 				 size,
83 				 args->type,
84 				 args->request_type);
85 	args->retval = ret;
86 
87 	hid_bpf_release_context(ctx);
88 
89 	return 0;
90 }
91