Path: blob/master/tools/testing/selftests/hid/progs/hid_bpf_helpers.h
26308 views
/* SPDX-License-Identifier: GPL-2.0-only */1/* Copyright (c) 2022 Benjamin Tissoires2*/34#ifndef __HID_BPF_HELPERS_H5#define __HID_BPF_HELPERS_H67/* "undefine" structs and enums in vmlinux.h, because we "override" them below */8#define hid_bpf_ctx hid_bpf_ctx___not_used9#define hid_bpf_ops hid_bpf_ops___not_used10#define hid_report_type hid_report_type___not_used11#define hid_class_request hid_class_request___not_used12#define hid_bpf_attach_flags hid_bpf_attach_flags___not_used13#define HID_INPUT_REPORT HID_INPUT_REPORT___not_used14#define HID_OUTPUT_REPORT HID_OUTPUT_REPORT___not_used15#define HID_FEATURE_REPORT HID_FEATURE_REPORT___not_used16#define HID_REPORT_TYPES HID_REPORT_TYPES___not_used17#define HID_REQ_GET_REPORT HID_REQ_GET_REPORT___not_used18#define HID_REQ_GET_IDLE HID_REQ_GET_IDLE___not_used19#define HID_REQ_GET_PROTOCOL HID_REQ_GET_PROTOCOL___not_used20#define HID_REQ_SET_REPORT HID_REQ_SET_REPORT___not_used21#define HID_REQ_SET_IDLE HID_REQ_SET_IDLE___not_used22#define HID_REQ_SET_PROTOCOL HID_REQ_SET_PROTOCOL___not_used2324/* do not define kfunc through vmlinux.h as this messes up our custom hack */25#define BPF_NO_KFUNC_PROTOTYPES2627#include "vmlinux.h"2829#undef hid_bpf_ctx30#undef hid_bpf_ops31#undef hid_report_type32#undef hid_class_request33#undef hid_bpf_attach_flags34#undef HID_INPUT_REPORT35#undef HID_OUTPUT_REPORT36#undef HID_FEATURE_REPORT37#undef HID_REPORT_TYPES38#undef HID_REQ_GET_REPORT39#undef HID_REQ_GET_IDLE40#undef HID_REQ_GET_PROTOCOL41#undef HID_REQ_SET_REPORT42#undef HID_REQ_SET_IDLE43#undef HID_REQ_SET_PROTOCOL4445#include <bpf/bpf_helpers.h>46#include <bpf/bpf_tracing.h>47#include <linux/const.h>4849enum hid_report_type {50HID_INPUT_REPORT = 0,51HID_OUTPUT_REPORT = 1,52HID_FEATURE_REPORT = 2,5354HID_REPORT_TYPES,55};5657struct hid_bpf_ctx {58struct hid_device *hid;59__u32 allocated_size;60union {61__s32 retval;62__s32 size;63};64} __attribute__((preserve_access_index));6566enum hid_class_request {67HID_REQ_GET_REPORT = 0x01,68HID_REQ_GET_IDLE = 0x02,69HID_REQ_GET_PROTOCOL = 0x03,70HID_REQ_SET_REPORT = 0x09,71HID_REQ_SET_IDLE = 0x0A,72HID_REQ_SET_PROTOCOL = 0x0B,73};7475struct hid_bpf_ops {76int hid_id;77u32 flags;78struct list_head list;79int (*hid_device_event)(struct hid_bpf_ctx *ctx, enum hid_report_type report_type,80u64 source);81int (*hid_rdesc_fixup)(struct hid_bpf_ctx *ctx);82int (*hid_hw_request)(struct hid_bpf_ctx *ctx, unsigned char reportnum,83enum hid_report_type rtype, enum hid_class_request reqtype,84u64 source);85int (*hid_hw_output_report)(struct hid_bpf_ctx *ctx, u64 source);86struct hid_device *hdev;87};8889#ifndef BPF_F_BEFORE90#define BPF_F_BEFORE (1U << 3)91#endif9293/* following are kfuncs exported by HID for HID-BPF */94extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,95unsigned int offset,96const size_t __sz) __weak __ksym;97extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __weak __ksym;98extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __weak __ksym;99extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,100__u8 *data,101size_t buf__sz,102enum hid_report_type type,103enum hid_class_request reqtype) __weak __ksym;104extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,105__u8 *buf, size_t buf__sz) __weak __ksym;106extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,107enum hid_report_type type,108__u8 *data,109size_t buf__sz) __weak __ksym;110extern int hid_bpf_try_input_report(struct hid_bpf_ctx *ctx,111enum hid_report_type type,112__u8 *data,113size_t buf__sz) __weak __ksym;114115/* bpf_wq implementation */116extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;117extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;118extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,119int (callback_fn)(void *map, int *key, void *wq),120unsigned int flags__k, void *aux__ign) __weak __ksym;121#define bpf_wq_set_callback(timer, cb, flags) \122bpf_wq_set_callback_impl(timer, cb, flags, NULL)123124#endif /* __HID_BPF_HELPERS_H */125126127