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 #define BIT(n) (1U << n) 88 89 #ifndef BPF_F_BEFORE 90 #define BPF_F_BEFORE BIT(3) 91 #endif 92 93 #define HID_QUIRK_IGNORE_SPECIAL_DRIVER BIT(22) 94 95 /* following are kfuncs exported by HID for HID-BPF */ 96 extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx, 97 unsigned int offset, 98 const size_t __sz) __ksym; 99 extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym; 100 extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym; 101 extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, 102 __u8 *data, 103 size_t buf__sz, 104 enum hid_report_type type, 105 enum hid_class_request reqtype) __ksym; 106 extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, 107 __u8 *buf, size_t buf__sz) __ksym; 108 extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx, 109 enum hid_report_type type, 110 __u8 *data, 111 size_t buf__sz) __ksym; 112 extern int hid_bpf_try_input_report(struct hid_bpf_ctx *ctx, 113 enum hid_report_type type, 114 __u8 *data, 115 size_t buf__sz) __ksym; 116 117 /* bpf_wq implementation */ 118 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym; 119 extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym; 120 extern int bpf_wq_set_callback_impl(struct bpf_wq *wq, 121 int (callback_fn)(void *map, int *key, void *wq), 122 unsigned int flags__k, void *aux__ign) __ksym; 123 #define bpf_wq_set_callback(timer, cb, flags) \ 124 bpf_wq_set_callback_impl(timer, cb, flags, NULL) 125 126 #endif /* __HID_BPF_HELPERS_H */ 127