Path: blob/master/drivers/hid/bpf/progs/hid_bpf_helpers.h
26285 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#include "vmlinux.h"8#include <bpf/bpf_helpers.h>9#include <linux/errno.h>1011extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,12unsigned int offset,13const size_t __sz) __ksym;14extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;15extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;16extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,17__u8 *data,18size_t buf__sz,19enum hid_report_type type,20enum hid_class_request reqtype) __ksym;21extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,22__u8 *buf, size_t buf__sz) __weak __ksym;23extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,24enum hid_report_type type,25__u8 *data,26size_t buf__sz) __weak __ksym;27extern int hid_bpf_try_input_report(struct hid_bpf_ctx *ctx,28enum hid_report_type type,29__u8 *data,30size_t buf__sz) __weak __ksym;3132/* bpf_wq implementation */33extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;34extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;35extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,36int (callback_fn)(void *map, int *key, void *value),37unsigned int flags__k, void *aux__ign) __ksym;38#define bpf_wq_set_callback(wq, cb, flags) \39bpf_wq_set_callback_impl(wq, cb, flags, NULL)4041#define HID_MAX_DESCRIPTOR_SIZE 409642#define HID_IGNORE_EVENT -14344/* extracted from <linux/input.h> */45#define BUS_ANY 0x0046#define BUS_PCI 0x0147#define BUS_ISAPNP 0x0248#define BUS_USB 0x0349#define BUS_HIL 0x0450#define BUS_BLUETOOTH 0x0551#define BUS_VIRTUAL 0x0652#define BUS_ISA 0x1053#define BUS_I8042 0x1154#define BUS_XTKBD 0x1255#define BUS_RS232 0x1356#define BUS_GAMEPORT 0x1457#define BUS_PARPORT 0x1558#define BUS_AMIGA 0x1659#define BUS_ADB 0x1760#define BUS_I2C 0x1861#define BUS_HOST 0x1962#define BUS_GSC 0x1A63#define BUS_ATARI 0x1B64#define BUS_SPI 0x1C65#define BUS_RMI 0x1D66#define BUS_CEC 0x1E67#define BUS_INTEL_ISHTP 0x1F68#define BUS_AMD_SFH 0x206970/* extracted from <linux/hid.h> */71#define HID_GROUP_ANY 0x000072#define HID_GROUP_GENERIC 0x000173#define HID_GROUP_MULTITOUCH 0x000274#define HID_GROUP_SENSOR_HUB 0x000375#define HID_GROUP_MULTITOUCH_WIN_8 0x000476#define HID_GROUP_RMI 0x010077#define HID_GROUP_WACOM 0x010178#define HID_GROUP_LOGITECH_DJ_DEVICE 0x010279#define HID_GROUP_STEAM 0x010380#define HID_GROUP_LOGITECH_27MHZ_DEVICE 0x010481#define HID_GROUP_VIVALDI 0x01058283/* include/linux/mod_devicetable.h defines as (~0), but that gives us negative size arrays */84#define HID_VID_ANY 0x000085#define HID_PID_ANY 0x00008687#define BIT(n) (1UL << (n))88#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))8990/* Helper macro to convert (foo, __LINE__) into foo134 so we can use __LINE__ for91* field/variable names92*/93#define COMBINE1(X, Y) X ## Y94#define COMBINE(X, Y) COMBINE1(X, Y)9596/* Macro magic:97* __uint(foo, 123) creates a int (*foo)[1234]98*99* We use that macro to declare an anonymous struct with several100* fields, each is the declaration of an pointer to an array of size101* bus/group/vid/pid. (Because it's a pointer to such an array, actual storage102* would be sizeof(pointer) rather than sizeof(array). Not that we ever103* instantiate it anyway).104*105* This is only used for BTF introspection, we can later check "what size106* is the bus array" in the introspection data and thus extract the bus ID107* again.108*109* And we use the __LINE__ to give each of our structs a unique name so the110* BPF program writer doesn't have to.111*112* $ bpftool btf dump file target/bpf/HP_Elite_Presenter.bpf.o113* shows the inspection data, start by searching for .hid_bpf_config114* and working backwards from that (each entry references the type_id of the115* content).116*/117118#define HID_DEVICE(b, g, ven, prod) \119struct { \120__uint(name, 0); \121__uint(bus, (b)); \122__uint(group, (g)); \123__uint(vid, (ven)); \124__uint(pid, (prod)); \125} COMBINE(_entry, __LINE__)126127/* Macro magic below is to make HID_BPF_CONFIG() look like a function call that128* we can pass multiple HID_DEVICE() invocations in.129*130* For up to 16 arguments, HID_BPF_CONFIG(one, two) resolves to131*132* union {133* HID_DEVICE(...);134* HID_DEVICE(...);135* } _device_ids SEC(".hid_bpf_config")136*137*/138139/* Returns the number of macro arguments, this expands140* NARGS(a, b, c) to NTH_ARG(a, b, c, 15, 14, 13, .... 4, 3, 2, 1).141* NTH_ARG always returns the 16th argument which in our case is 3.142*143* If we want more than 16 values _COUNTDOWN and _NTH_ARG both need to be144* updated.145*/146#define _NARGS(...) _NARGS1(__VA_ARGS__, _COUNTDOWN)147#define _NARGS1(...) _NTH_ARG(__VA_ARGS__)148149/* Add to this if we need more than 16 args */150#define _COUNTDOWN \15115, 14, 13, 12, 11, 10, 9, 8, \1527, 6, 5, 4, 3, 2, 1, 0153154/* Return the 16 argument passed in. See _NARGS above for usage. Note this is155* 1-indexed.156*/157#define _NTH_ARG( \158_1, _2, _3, _4, _5, _6, _7, _8, \159_9, _10, _11, _12, _13, _14, _15,\160N, ...) N161162/* Turns EXPAND(_ARG, a, b, c) into _ARG3(a, b, c) */163#define _EXPAND(func, ...) COMBINE(func, _NARGS(__VA_ARGS__)) (__VA_ARGS__)164165/* And now define all the ARG macros for each number of args we want to accept */166#define _ARG1(_1) _1;167#define _ARG2(_1, _2) _1; _2;168#define _ARG3(_1, _2, _3) _1; _2; _3;169#define _ARG4(_1, _2, _3, _4) _1; _2; _3; _4;170#define _ARG5(_1, _2, _3, _4, _5) _1; _2; _3; _4; _5;171#define _ARG6(_1, _2, _3, _4, _5, _6) _1; _2; _3; _4; _5; _6;172#define _ARG7(_1, _2, _3, _4, _5, _6, _7) _1; _2; _3; _4; _5; _6; _7;173#define _ARG8(_1, _2, _3, _4, _5, _6, _7, _8) _1; _2; _3; _4; _5; _6; _7; _8;174#define _ARG9(_1, _2, _3, _4, _5, _6, _7, _8, _9) _1; _2; _3; _4; _5; _6; _7; _8; _9;175#define _ARG10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a;176#define _ARG11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b;177#define _ARG12(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b; _c;178#define _ARG13(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, _d) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b; _c; _d;179#define _ARG14(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, _d, _e) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b; _c; _d; _e;180#define _ARG15(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, _d, _e, _f) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b; _c; _d; _e; _f;181182183#define HID_BPF_CONFIG(...) union { \184_EXPAND(_ARG, __VA_ARGS__) \185} _device_ids SEC(".hid_bpf_config")186187#endif /* __HID_BPF_HELPERS_H */188189190