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