170 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#include "vmlinux.h"9#include <bpf/bpf_helpers.h>10#include <linux/errno.h>11 12extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,13 unsigned int offset,14 const size_t __sz) __ksym;15extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;16extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;17extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,18 __u8 *data,19 size_t buf__sz,20 enum hid_report_type type,21 enum hid_class_request reqtype) __ksym;22 23#define HID_MAX_DESCRIPTOR_SIZE 409624#define HID_IGNORE_EVENT -125 26/* extracted from <linux/input.h> */27#define BUS_ANY 0x0028#define BUS_PCI 0x0129#define BUS_ISAPNP 0x0230#define BUS_USB 0x0331#define BUS_HIL 0x0432#define BUS_BLUETOOTH 0x0533#define BUS_VIRTUAL 0x0634#define BUS_ISA 0x1035#define BUS_I8042 0x1136#define BUS_XTKBD 0x1237#define BUS_RS232 0x1338#define BUS_GAMEPORT 0x1439#define BUS_PARPORT 0x1540#define BUS_AMIGA 0x1641#define BUS_ADB 0x1742#define BUS_I2C 0x1843#define BUS_HOST 0x1944#define BUS_GSC 0x1A45#define BUS_ATARI 0x1B46#define BUS_SPI 0x1C47#define BUS_RMI 0x1D48#define BUS_CEC 0x1E49#define BUS_INTEL_ISHTP 0x1F50#define BUS_AMD_SFH 0x2051 52/* extracted from <linux/hid.h> */53#define HID_GROUP_ANY 0x000054#define HID_GROUP_GENERIC 0x000155#define HID_GROUP_MULTITOUCH 0x000256#define HID_GROUP_SENSOR_HUB 0x000357#define HID_GROUP_MULTITOUCH_WIN_8 0x000458#define HID_GROUP_RMI 0x010059#define HID_GROUP_WACOM 0x010160#define HID_GROUP_LOGITECH_DJ_DEVICE 0x010261#define HID_GROUP_STEAM 0x010362#define HID_GROUP_LOGITECH_27MHZ_DEVICE 0x010463#define HID_GROUP_VIVALDI 0x010564 65/* include/linux/mod_devicetable.h defines as (~0), but that gives us negative size arrays */66#define HID_VID_ANY 0x000067#define HID_PID_ANY 0x000068 69#define BIT(n) (1UL << (n))70#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))71 72/* Helper macro to convert (foo, __LINE__) into foo134 so we can use __LINE__ for73 * field/variable names74 */75#define COMBINE1(X, Y) X ## Y76#define COMBINE(X, Y) COMBINE1(X, Y)77 78/* Macro magic:79 * __uint(foo, 123) creates a int (*foo)[1234]80 *81 * We use that macro to declare an anonymous struct with several82 * fields, each is the declaration of an pointer to an array of size83 * bus/group/vid/pid. (Because it's a pointer to such an array, actual storage84 * would be sizeof(pointer) rather than sizeof(array). Not that we ever85 * instantiate it anyway).86 *87 * This is only used for BTF introspection, we can later check "what size88 * is the bus array" in the introspection data and thus extract the bus ID89 * again.90 *91 * And we use the __LINE__ to give each of our structs a unique name so the92 * BPF program writer doesn't have to.93 *94 * $ bpftool btf dump file target/bpf/HP_Elite_Presenter.bpf.o95 * shows the inspection data, start by searching for .hid_bpf_config96 * and working backwards from that (each entry references the type_id of the97 * content).98 */99 100#define HID_DEVICE(b, g, ven, prod) \101 struct { \102 __uint(name, 0); \103 __uint(bus, (b)); \104 __uint(group, (g)); \105 __uint(vid, (ven)); \106 __uint(pid, (prod)); \107 } COMBINE(_entry, __LINE__)108 109/* Macro magic below is to make HID_BPF_CONFIG() look like a function call that110 * we can pass multiple HID_DEVICE() invocations in.111 *112 * For up to 16 arguments, HID_BPF_CONFIG(one, two) resolves to113 *114 * union {115 * HID_DEVICE(...);116 * HID_DEVICE(...);117 * } _device_ids SEC(".hid_bpf_config")118 *119 */120 121/* Returns the number of macro arguments, this expands122 * NARGS(a, b, c) to NTH_ARG(a, b, c, 15, 14, 13, .... 4, 3, 2, 1).123 * NTH_ARG always returns the 16th argument which in our case is 3.124 *125 * If we want more than 16 values _COUNTDOWN and _NTH_ARG both need to be126 * updated.127 */128#define _NARGS(...) _NARGS1(__VA_ARGS__, _COUNTDOWN)129#define _NARGS1(...) _NTH_ARG(__VA_ARGS__)130 131/* Add to this if we need more than 16 args */132#define _COUNTDOWN \133 15, 14, 13, 12, 11, 10, 9, 8, \134 7, 6, 5, 4, 3, 2, 1, 0135 136/* Return the 16 argument passed in. See _NARGS above for usage. Note this is137 * 1-indexed.138 */139#define _NTH_ARG( \140 _1, _2, _3, _4, _5, _6, _7, _8, \141 _9, _10, _11, _12, _13, _14, _15,\142 N, ...) N143 144/* Turns EXPAND(_ARG, a, b, c) into _ARG3(a, b, c) */145#define _EXPAND(func, ...) COMBINE(func, _NARGS(__VA_ARGS__)) (__VA_ARGS__)146 147/* And now define all the ARG macros for each number of args we want to accept */148#define _ARG1(_1) _1;149#define _ARG2(_1, _2) _1; _2;150#define _ARG3(_1, _2, _3) _1; _2; _3;151#define _ARG4(_1, _2, _3, _4) _1; _2; _3; _4;152#define _ARG5(_1, _2, _3, _4, _5) _1; _2; _3; _4; _5;153#define _ARG6(_1, _2, _3, _4, _5, _6) _1; _2; _3; _4; _5; _6;154#define _ARG7(_1, _2, _3, _4, _5, _6, _7) _1; _2; _3; _4; _5; _6; _7;155#define _ARG8(_1, _2, _3, _4, _5, _6, _7, _8) _1; _2; _3; _4; _5; _6; _7; _8;156#define _ARG9(_1, _2, _3, _4, _5, _6, _7, _8, _9) _1; _2; _3; _4; _5; _6; _7; _8; _9;157#define _ARG10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a;158#define _ARG11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b;159#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;160#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;161#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;162#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;163 164 165#define HID_BPF_CONFIG(...) union { \166 _EXPAND(_ARG, __VA_ARGS__) \167} _device_ids SEC(".hid_bpf_config")168 169#endif /* __HID_BPF_HELPERS_H */170