xref: /linux/tools/testing/selftests/hid/progs/hid_bpf_helpers.h (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2022 Benjamin Tissoires
3  */
4 
5 #ifndef __HID_BPF_HELPERS_H
6 #define __HID_BPF_HELPERS_H
7 
8 /* "undefine" structs and enums in vmlinux.h, because we "override" them below */
9 #define hid_bpf_ctx hid_bpf_ctx___not_used
10 #define hid_report_type hid_report_type___not_used
11 #define hid_class_request hid_class_request___not_used
12 #define hid_bpf_attach_flags hid_bpf_attach_flags___not_used
13 #define HID_INPUT_REPORT         HID_INPUT_REPORT___not_used
14 #define HID_OUTPUT_REPORT        HID_OUTPUT_REPORT___not_used
15 #define HID_FEATURE_REPORT       HID_FEATURE_REPORT___not_used
16 #define HID_REPORT_TYPES         HID_REPORT_TYPES___not_used
17 #define HID_REQ_GET_REPORT       HID_REQ_GET_REPORT___not_used
18 #define HID_REQ_GET_IDLE         HID_REQ_GET_IDLE___not_used
19 #define HID_REQ_GET_PROTOCOL     HID_REQ_GET_PROTOCOL___not_used
20 #define HID_REQ_SET_REPORT       HID_REQ_SET_REPORT___not_used
21 #define HID_REQ_SET_IDLE         HID_REQ_SET_IDLE___not_used
22 #define HID_REQ_SET_PROTOCOL     HID_REQ_SET_PROTOCOL___not_used
23 #define HID_BPF_FLAG_NONE        HID_BPF_FLAG_NONE___not_used
24 #define HID_BPF_FLAG_INSERT_HEAD HID_BPF_FLAG_INSERT_HEAD___not_used
25 #define HID_BPF_FLAG_MAX         HID_BPF_FLAG_MAX___not_used
26 
27 #include "vmlinux.h"
28 
29 #undef hid_bpf_ctx
30 #undef hid_report_type
31 #undef hid_class_request
32 #undef hid_bpf_attach_flags
33 #undef HID_INPUT_REPORT
34 #undef HID_OUTPUT_REPORT
35 #undef HID_FEATURE_REPORT
36 #undef HID_REPORT_TYPES
37 #undef HID_REQ_GET_REPORT
38 #undef HID_REQ_GET_IDLE
39 #undef HID_REQ_GET_PROTOCOL
40 #undef HID_REQ_SET_REPORT
41 #undef HID_REQ_SET_IDLE
42 #undef HID_REQ_SET_PROTOCOL
43 #undef HID_BPF_FLAG_NONE
44 #undef HID_BPF_FLAG_INSERT_HEAD
45 #undef HID_BPF_FLAG_MAX
46 
47 #include <bpf/bpf_helpers.h>
48 #include <bpf/bpf_tracing.h>
49 #include <linux/const.h>
50 
51 enum hid_report_type {
52 	HID_INPUT_REPORT		= 0,
53 	HID_OUTPUT_REPORT		= 1,
54 	HID_FEATURE_REPORT		= 2,
55 
56 	HID_REPORT_TYPES,
57 };
58 
59 struct hid_bpf_ctx {
60 	__u32 index;
61 	const struct hid_device *hid;
62 	__u32 allocated_size;
63 	enum hid_report_type report_type;
64 	union {
65 		__s32 retval;
66 		__s32 size;
67 	};
68 } __attribute__((preserve_access_index));
69 
70 enum hid_class_request {
71 	HID_REQ_GET_REPORT		= 0x01,
72 	HID_REQ_GET_IDLE		= 0x02,
73 	HID_REQ_GET_PROTOCOL		= 0x03,
74 	HID_REQ_SET_REPORT		= 0x09,
75 	HID_REQ_SET_IDLE		= 0x0A,
76 	HID_REQ_SET_PROTOCOL		= 0x0B,
77 };
78 
79 enum hid_bpf_attach_flags {
80 	HID_BPF_FLAG_NONE = 0,
81 	HID_BPF_FLAG_INSERT_HEAD = _BITUL(0),
82 	HID_BPF_FLAG_MAX,
83 };
84 
85 /* following are kfuncs exported by HID for HID-BPF */
86 extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
87 			      unsigned int offset,
88 			      const size_t __sz) __ksym;
89 extern int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, u32 flags) __ksym;
90 extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;
91 extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;
92 extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
93 			      __u8 *data,
94 			      size_t buf__sz,
95 			      enum hid_report_type type,
96 			      enum hid_class_request reqtype) __ksym;
97 extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,
98 				    __u8 *buf, size_t buf__sz) __ksym;
99 extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,
100 				enum hid_report_type type,
101 				__u8 *data,
102 				size_t buf__sz) __ksym;
103 
104 #endif /* __HID_BPF_HELPERS_H */
105