123 lines · c
1/* SPDX-License-Identifier: GPL-2.0-only */2/* Copyright (c) 2022 Benjamin Tissoires3 */4 5#ifndef __HID_BPF_HELPERS_H6#define __HID_BPF_HELPERS_H7 8/* "undefine" structs and enums in vmlinux.h, because we "override" them below */9#define hid_bpf_ctx hid_bpf_ctx___not_used10#define hid_bpf_ops hid_bpf_ops___not_used11#define hid_report_type hid_report_type___not_used12#define hid_class_request hid_class_request___not_used13#define hid_bpf_attach_flags hid_bpf_attach_flags___not_used14#define HID_INPUT_REPORT HID_INPUT_REPORT___not_used15#define HID_OUTPUT_REPORT HID_OUTPUT_REPORT___not_used16#define HID_FEATURE_REPORT HID_FEATURE_REPORT___not_used17#define HID_REPORT_TYPES HID_REPORT_TYPES___not_used18#define HID_REQ_GET_REPORT HID_REQ_GET_REPORT___not_used19#define HID_REQ_GET_IDLE HID_REQ_GET_IDLE___not_used20#define HID_REQ_GET_PROTOCOL HID_REQ_GET_PROTOCOL___not_used21#define HID_REQ_SET_REPORT HID_REQ_SET_REPORT___not_used22#define HID_REQ_SET_IDLE HID_REQ_SET_IDLE___not_used23#define HID_REQ_SET_PROTOCOL HID_REQ_SET_PROTOCOL___not_used24 25#include "vmlinux.h"26 27#undef hid_bpf_ctx28#undef hid_bpf_ops29#undef hid_report_type30#undef hid_class_request31#undef hid_bpf_attach_flags32#undef HID_INPUT_REPORT33#undef HID_OUTPUT_REPORT34#undef HID_FEATURE_REPORT35#undef HID_REPORT_TYPES36#undef HID_REQ_GET_REPORT37#undef HID_REQ_GET_IDLE38#undef HID_REQ_GET_PROTOCOL39#undef HID_REQ_SET_REPORT40#undef HID_REQ_SET_IDLE41#undef HID_REQ_SET_PROTOCOL42 43#include <bpf/bpf_helpers.h>44#include <bpf/bpf_tracing.h>45#include <linux/const.h>46 47enum 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 55struct 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 64enum 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 73struct 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_BEFORE88#define BPF_F_BEFORE (1U << 3)89#endif90 91/* following are kfuncs exported by HID for HID-BPF */92extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,93 unsigned int offset,94 const size_t __sz) __ksym;95extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;96extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;97extern 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;102extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,103 __u8 *buf, size_t buf__sz) __ksym;104extern 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;108extern 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 */114extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;115extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;116extern 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