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