xref: /linux/tools/testing/selftests/hid/progs/hid_bpf_helpers.h (revision f0caa1d49cc07b30a7e2f104d3853ec6dc1c3cad)
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 bpf_wq bpf_wq___not_used
10 #define hid_bpf_ctx hid_bpf_ctx___not_used
11 #define hid_bpf_ops hid_bpf_ops___not_used
12 #define hid_device hid_device___not_used
13 #define hid_report_type hid_report_type___not_used
14 #define hid_class_request hid_class_request___not_used
15 #define hid_bpf_attach_flags hid_bpf_attach_flags___not_used
16 #define HID_INPUT_REPORT         HID_INPUT_REPORT___not_used
17 #define HID_OUTPUT_REPORT        HID_OUTPUT_REPORT___not_used
18 #define HID_FEATURE_REPORT       HID_FEATURE_REPORT___not_used
19 #define HID_REPORT_TYPES         HID_REPORT_TYPES___not_used
20 #define HID_REQ_GET_REPORT       HID_REQ_GET_REPORT___not_used
21 #define HID_REQ_GET_IDLE         HID_REQ_GET_IDLE___not_used
22 #define HID_REQ_GET_PROTOCOL     HID_REQ_GET_PROTOCOL___not_used
23 #define HID_REQ_SET_REPORT       HID_REQ_SET_REPORT___not_used
24 #define HID_REQ_SET_IDLE         HID_REQ_SET_IDLE___not_used
25 #define HID_REQ_SET_PROTOCOL     HID_REQ_SET_PROTOCOL___not_used
26 
27 /* do not define kfunc through vmlinux.h as this messes up our custom hack */
28 #define BPF_NO_KFUNC_PROTOTYPES
29 
30 #include "vmlinux.h"
31 
32 #undef bpf_wq
33 #undef hid_bpf_ctx
34 #undef hid_bpf_ops
35 #undef hid_device
36 #undef hid_report_type
37 #undef hid_class_request
38 #undef hid_bpf_attach_flags
39 #undef HID_INPUT_REPORT
40 #undef HID_OUTPUT_REPORT
41 #undef HID_FEATURE_REPORT
42 #undef HID_REPORT_TYPES
43 #undef HID_REQ_GET_REPORT
44 #undef HID_REQ_GET_IDLE
45 #undef HID_REQ_GET_PROTOCOL
46 #undef HID_REQ_SET_REPORT
47 #undef HID_REQ_SET_IDLE
48 #undef HID_REQ_SET_PROTOCOL
49 
50 #include <bpf/bpf_helpers.h>
51 #include <bpf/bpf_tracing.h>
52 #include <linux/const.h>
53 
54 enum hid_report_type {
55 	HID_INPUT_REPORT		= 0,
56 	HID_OUTPUT_REPORT		= 1,
57 	HID_FEATURE_REPORT		= 2,
58 
59 	HID_REPORT_TYPES,
60 };
61 
62 struct hid_device {
63 	unsigned int id;
64 } __attribute__((preserve_access_index));
65 
66 struct bpf_wq {
67 	__u64 __opaque[2];
68 };
69 
70 struct hid_bpf_ctx {
71 	struct hid_device *hid;
72 	__u32 allocated_size;
73 	union {
74 		__s32 retval;
75 		__s32 size;
76 	};
77 } __attribute__((preserve_access_index));
78 
79 enum hid_class_request {
80 	HID_REQ_GET_REPORT		= 0x01,
81 	HID_REQ_GET_IDLE		= 0x02,
82 	HID_REQ_GET_PROTOCOL		= 0x03,
83 	HID_REQ_SET_REPORT		= 0x09,
84 	HID_REQ_SET_IDLE		= 0x0A,
85 	HID_REQ_SET_PROTOCOL		= 0x0B,
86 };
87 
88 struct hid_bpf_ops {
89 	int			hid_id;
90 	u32			flags;
91 	struct list_head	list;
92 	int (*hid_device_event)(struct hid_bpf_ctx *ctx, enum hid_report_type report_type,
93 				u64 source);
94 	int (*hid_rdesc_fixup)(struct hid_bpf_ctx *ctx);
95 	int (*hid_hw_request)(struct hid_bpf_ctx *ctx, unsigned char reportnum,
96 			       enum hid_report_type rtype, enum hid_class_request reqtype,
97 			       u64 source);
98 	int (*hid_hw_output_report)(struct hid_bpf_ctx *ctx, u64 source);
99 	struct hid_device *hdev;
100 };
101 
102 #ifndef BPF_F_BEFORE
103 #define BPF_F_BEFORE (1U << 3)
104 #endif
105 
106 /* following are kfuncs exported by HID for HID-BPF */
107 extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
108 			      unsigned int offset,
109 			      const size_t __sz) __weak __ksym;
110 extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __weak __ksym;
111 extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __weak __ksym;
112 extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
113 			      __u8 *data,
114 			      size_t buf__sz,
115 			      enum hid_report_type type,
116 			      enum hid_class_request reqtype) __weak __ksym;
117 extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,
118 				    __u8 *buf, size_t buf__sz) __weak __ksym;
119 extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,
120 				enum hid_report_type type,
121 				__u8 *data,
122 				size_t buf__sz) __weak __ksym;
123 extern int hid_bpf_try_input_report(struct hid_bpf_ctx *ctx,
124 				    enum hid_report_type type,
125 				    __u8 *data,
126 				    size_t buf__sz) __weak __ksym;
127 
128 /* bpf_wq implementation */
129 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
130 extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
131 extern int bpf_wq_set_callback(struct bpf_wq *wq,
132 		int (*callback_fn)(void *, int *, void *),
133 		unsigned int flags) __weak __ksym;
134 
135 #endif /* __HID_BPF_HELPERS_H */
136