xref: /linux/tools/testing/selftests/hid/progs/hid_bpf_helpers.h (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 	char name[128];
65 	char phys[64];
66 	char uniq[64];
67 } __attribute__((preserve_access_index));
68 
69 struct bpf_wq {
70 	__u64 __opaque[2];
71 };
72 
73 struct hid_bpf_ctx {
74 	struct hid_device *hid;
75 	__u32 allocated_size;
76 	union {
77 		__s32 retval;
78 		__s32 size;
79 	};
80 } __attribute__((preserve_access_index));
81 
82 enum hid_class_request {
83 	HID_REQ_GET_REPORT		= 0x01,
84 	HID_REQ_GET_IDLE		= 0x02,
85 	HID_REQ_GET_PROTOCOL		= 0x03,
86 	HID_REQ_SET_REPORT		= 0x09,
87 	HID_REQ_SET_IDLE		= 0x0A,
88 	HID_REQ_SET_PROTOCOL		= 0x0B,
89 };
90 
91 struct hid_bpf_ops {
92 	int			hid_id;
93 	u32			flags;
94 	struct list_head	list;
95 	int (*hid_device_event)(struct hid_bpf_ctx *ctx, enum hid_report_type report_type,
96 				u64 source);
97 	int (*hid_rdesc_fixup)(struct hid_bpf_ctx *ctx);
98 	int (*hid_hw_request)(struct hid_bpf_ctx *ctx, unsigned char reportnum,
99 			       enum hid_report_type rtype, enum hid_class_request reqtype,
100 			       u64 source);
101 	int (*hid_hw_output_report)(struct hid_bpf_ctx *ctx, u64 source);
102 	struct hid_device *hdev;
103 };
104 
105 #ifndef BPF_F_BEFORE
106 #define BPF_F_BEFORE (1U << 3)
107 #endif
108 
109 /* following are kfuncs exported by HID for HID-BPF */
110 extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
111 			      unsigned int offset,
112 			      const size_t __sz) __weak __ksym;
113 extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __weak __ksym;
114 extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __weak __ksym;
115 extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
116 			      __u8 *data,
117 			      size_t buf__sz,
118 			      enum hid_report_type type,
119 			      enum hid_class_request reqtype) __weak __ksym;
120 extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,
121 				    __u8 *buf, size_t buf__sz) __weak __ksym;
122 extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,
123 				enum hid_report_type type,
124 				__u8 *data,
125 				size_t buf__sz) __weak __ksym;
126 extern int hid_bpf_try_input_report(struct hid_bpf_ctx *ctx,
127 				    enum hid_report_type type,
128 				    __u8 *data,
129 				    size_t buf__sz) __weak __ksym;
130 
131 /* bpf_wq implementation */
132 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
133 extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
134 extern int bpf_wq_set_callback(struct bpf_wq *wq,
135 		int (*callback_fn)(void *, int *, void *),
136 		unsigned int flags) __weak __ksym;
137 
138 #endif /* __HID_BPF_HELPERS_H */
139