xref: /linux/drivers/hid/bpf/progs/hid_bpf_helpers.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
104b3e5abSBenjamin Tissoires /* SPDX-License-Identifier: GPL-2.0-only */
204b3e5abSBenjamin Tissoires /* Copyright (c) 2022 Benjamin Tissoires
304b3e5abSBenjamin Tissoires  */
404b3e5abSBenjamin Tissoires 
504b3e5abSBenjamin Tissoires #ifndef __HID_BPF_HELPERS_H
604b3e5abSBenjamin Tissoires #define __HID_BPF_HELPERS_H
704b3e5abSBenjamin Tissoires 
804b3e5abSBenjamin Tissoires #include "vmlinux.h"
904b3e5abSBenjamin Tissoires #include <bpf/bpf_helpers.h>
1004b3e5abSBenjamin Tissoires #include <linux/errno.h>
1104b3e5abSBenjamin Tissoires 
1204b3e5abSBenjamin Tissoires extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
1304b3e5abSBenjamin Tissoires 			      unsigned int offset,
1404b3e5abSBenjamin Tissoires 			      const size_t __sz) __ksym;
1504b3e5abSBenjamin Tissoires extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;
1604b3e5abSBenjamin Tissoires extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;
1704b3e5abSBenjamin Tissoires extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
1804b3e5abSBenjamin Tissoires 			      __u8 *data,
1904b3e5abSBenjamin Tissoires 			      size_t buf__sz,
2004b3e5abSBenjamin Tissoires 			      enum hid_report_type type,
2104b3e5abSBenjamin Tissoires 			      enum hid_class_request reqtype) __ksym;
2204b3e5abSBenjamin Tissoires 
2304b3e5abSBenjamin Tissoires #define HID_MAX_DESCRIPTOR_SIZE	4096
2404b3e5abSBenjamin Tissoires #define HID_IGNORE_EVENT	-1
2504b3e5abSBenjamin Tissoires 
2604b3e5abSBenjamin Tissoires /* extracted from <linux/input.h> */
2704b3e5abSBenjamin Tissoires #define BUS_ANY			0x00
2804b3e5abSBenjamin Tissoires #define BUS_PCI			0x01
2904b3e5abSBenjamin Tissoires #define BUS_ISAPNP		0x02
3004b3e5abSBenjamin Tissoires #define BUS_USB			0x03
3104b3e5abSBenjamin Tissoires #define BUS_HIL			0x04
3204b3e5abSBenjamin Tissoires #define BUS_BLUETOOTH		0x05
3304b3e5abSBenjamin Tissoires #define BUS_VIRTUAL		0x06
3404b3e5abSBenjamin Tissoires #define BUS_ISA			0x10
3504b3e5abSBenjamin Tissoires #define BUS_I8042		0x11
3604b3e5abSBenjamin Tissoires #define BUS_XTKBD		0x12
3704b3e5abSBenjamin Tissoires #define BUS_RS232		0x13
3804b3e5abSBenjamin Tissoires #define BUS_GAMEPORT		0x14
3904b3e5abSBenjamin Tissoires #define BUS_PARPORT		0x15
4004b3e5abSBenjamin Tissoires #define BUS_AMIGA		0x16
4104b3e5abSBenjamin Tissoires #define BUS_ADB			0x17
4204b3e5abSBenjamin Tissoires #define BUS_I2C			0x18
4304b3e5abSBenjamin Tissoires #define BUS_HOST		0x19
4404b3e5abSBenjamin Tissoires #define BUS_GSC			0x1A
4504b3e5abSBenjamin Tissoires #define BUS_ATARI		0x1B
4604b3e5abSBenjamin Tissoires #define BUS_SPI			0x1C
4704b3e5abSBenjamin Tissoires #define BUS_RMI			0x1D
4804b3e5abSBenjamin Tissoires #define BUS_CEC			0x1E
4904b3e5abSBenjamin Tissoires #define BUS_INTEL_ISHTP		0x1F
5004b3e5abSBenjamin Tissoires #define BUS_AMD_SFH		0x20
5104b3e5abSBenjamin Tissoires 
5204b3e5abSBenjamin Tissoires /* extracted from <linux/hid.h> */
5304b3e5abSBenjamin Tissoires #define HID_GROUP_ANY				0x0000
5404b3e5abSBenjamin Tissoires #define HID_GROUP_GENERIC			0x0001
5504b3e5abSBenjamin Tissoires #define HID_GROUP_MULTITOUCH			0x0002
5604b3e5abSBenjamin Tissoires #define HID_GROUP_SENSOR_HUB			0x0003
5704b3e5abSBenjamin Tissoires #define HID_GROUP_MULTITOUCH_WIN_8		0x0004
5804b3e5abSBenjamin Tissoires #define HID_GROUP_RMI				0x0100
5904b3e5abSBenjamin Tissoires #define HID_GROUP_WACOM				0x0101
6004b3e5abSBenjamin Tissoires #define HID_GROUP_LOGITECH_DJ_DEVICE		0x0102
6104b3e5abSBenjamin Tissoires #define HID_GROUP_STEAM				0x0103
6204b3e5abSBenjamin Tissoires #define HID_GROUP_LOGITECH_27MHZ_DEVICE		0x0104
6304b3e5abSBenjamin Tissoires #define HID_GROUP_VIVALDI			0x0105
6404b3e5abSBenjamin Tissoires 
6504b3e5abSBenjamin Tissoires /* include/linux/mod_devicetable.h defines as (~0), but that gives us negative size arrays */
6604b3e5abSBenjamin Tissoires #define HID_VID_ANY				0x0000
6704b3e5abSBenjamin Tissoires #define HID_PID_ANY				0x0000
6804b3e5abSBenjamin Tissoires 
69*c4015aa7SBenjamin Tissoires #define BIT(n) (1UL << (n))
7004b3e5abSBenjamin Tissoires #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
7104b3e5abSBenjamin Tissoires 
7204b3e5abSBenjamin Tissoires /* Helper macro to convert (foo, __LINE__)  into foo134 so we can use __LINE__ for
7304b3e5abSBenjamin Tissoires  * field/variable names
7404b3e5abSBenjamin Tissoires  */
7504b3e5abSBenjamin Tissoires #define COMBINE1(X, Y) X ## Y
7604b3e5abSBenjamin Tissoires #define COMBINE(X, Y) COMBINE1(X, Y)
7704b3e5abSBenjamin Tissoires 
7804b3e5abSBenjamin Tissoires /* Macro magic:
7904b3e5abSBenjamin Tissoires  * __uint(foo, 123) creates a int (*foo)[1234]
8004b3e5abSBenjamin Tissoires  *
8104b3e5abSBenjamin Tissoires  * We use that macro to declare an anonymous struct with several
8204b3e5abSBenjamin Tissoires  * fields, each is the declaration of an pointer to an array of size
8304b3e5abSBenjamin Tissoires  * bus/group/vid/pid. (Because it's a pointer to such an array, actual storage
8404b3e5abSBenjamin Tissoires  * would be sizeof(pointer) rather than sizeof(array). Not that we ever
8504b3e5abSBenjamin Tissoires  * instantiate it anyway).
8604b3e5abSBenjamin Tissoires  *
8704b3e5abSBenjamin Tissoires  * This is only used for BTF introspection, we can later check "what size
8804b3e5abSBenjamin Tissoires  * is the bus array" in the introspection data and thus extract the bus ID
8904b3e5abSBenjamin Tissoires  * again.
9004b3e5abSBenjamin Tissoires  *
9104b3e5abSBenjamin Tissoires  * And we use the __LINE__ to give each of our structs a unique name so the
9204b3e5abSBenjamin Tissoires  * BPF program writer doesn't have to.
9304b3e5abSBenjamin Tissoires  *
9404b3e5abSBenjamin Tissoires  * $ bpftool btf dump file target/bpf/HP_Elite_Presenter.bpf.o
9504b3e5abSBenjamin Tissoires  * shows the inspection data, start by searching for .hid_bpf_config
9604b3e5abSBenjamin Tissoires  * and working backwards from that (each entry references the type_id of the
9704b3e5abSBenjamin Tissoires  * content).
9804b3e5abSBenjamin Tissoires  */
9904b3e5abSBenjamin Tissoires 
10004b3e5abSBenjamin Tissoires #define HID_DEVICE(b, g, ven, prod)	\
10104b3e5abSBenjamin Tissoires 	struct {			\
10204b3e5abSBenjamin Tissoires 		__uint(name, 0);	\
10304b3e5abSBenjamin Tissoires 		__uint(bus, (b));	\
10404b3e5abSBenjamin Tissoires 		__uint(group, (g));	\
10504b3e5abSBenjamin Tissoires 		__uint(vid, (ven));	\
10604b3e5abSBenjamin Tissoires 		__uint(pid, (prod));	\
10704b3e5abSBenjamin Tissoires 	} COMBINE(_entry, __LINE__)
10804b3e5abSBenjamin Tissoires 
10904b3e5abSBenjamin Tissoires /* Macro magic below is to make HID_BPF_CONFIG() look like a function call that
11004b3e5abSBenjamin Tissoires  * we can pass multiple HID_DEVICE() invocations in.
11104b3e5abSBenjamin Tissoires  *
11204b3e5abSBenjamin Tissoires  * For up to 16 arguments, HID_BPF_CONFIG(one, two) resolves to
11304b3e5abSBenjamin Tissoires  *
11404b3e5abSBenjamin Tissoires  * union {
11504b3e5abSBenjamin Tissoires  *    HID_DEVICE(...);
11604b3e5abSBenjamin Tissoires  *    HID_DEVICE(...);
11704b3e5abSBenjamin Tissoires  * } _device_ids SEC(".hid_bpf_config")
11804b3e5abSBenjamin Tissoires  *
11904b3e5abSBenjamin Tissoires  */
12004b3e5abSBenjamin Tissoires 
12104b3e5abSBenjamin Tissoires /* Returns the number of macro arguments, this expands
12204b3e5abSBenjamin Tissoires  * NARGS(a, b, c) to NTH_ARG(a, b, c, 15, 14, 13, .... 4, 3, 2, 1).
12304b3e5abSBenjamin Tissoires  * NTH_ARG always returns the 16th argument which in our case is 3.
12404b3e5abSBenjamin Tissoires  *
12504b3e5abSBenjamin Tissoires  * If we want more than 16 values _COUNTDOWN and _NTH_ARG both need to be
12604b3e5abSBenjamin Tissoires  * updated.
12704b3e5abSBenjamin Tissoires  */
12804b3e5abSBenjamin Tissoires #define _NARGS(...)  _NARGS1(__VA_ARGS__, _COUNTDOWN)
12904b3e5abSBenjamin Tissoires #define _NARGS1(...) _NTH_ARG(__VA_ARGS__)
13004b3e5abSBenjamin Tissoires 
13104b3e5abSBenjamin Tissoires /* Add to this if we need more than 16 args */
13204b3e5abSBenjamin Tissoires #define _COUNTDOWN \
13304b3e5abSBenjamin Tissoires 	15, 14, 13, 12, 11, 10, 9, 8,  \
13404b3e5abSBenjamin Tissoires 	 7,  6,  5,  4,  3,  2, 1, 0
13504b3e5abSBenjamin Tissoires 
13604b3e5abSBenjamin Tissoires /* Return the 16 argument passed in. See _NARGS above for usage. Note this is
13704b3e5abSBenjamin Tissoires  * 1-indexed.
13804b3e5abSBenjamin Tissoires  */
13904b3e5abSBenjamin Tissoires #define _NTH_ARG( \
14004b3e5abSBenjamin Tissoires 	_1,  _2,  _3,  _4,  _5,  _6,  _7, _8, \
14104b3e5abSBenjamin Tissoires 	_9, _10, _11, _12, _13, _14, _15,\
14204b3e5abSBenjamin Tissoires 	 N, ...) N
14304b3e5abSBenjamin Tissoires 
14404b3e5abSBenjamin Tissoires /* Turns EXPAND(_ARG, a, b, c) into _ARG3(a, b, c) */
14504b3e5abSBenjamin Tissoires #define _EXPAND(func, ...) COMBINE(func, _NARGS(__VA_ARGS__)) (__VA_ARGS__)
14604b3e5abSBenjamin Tissoires 
14704b3e5abSBenjamin Tissoires /* And now define all the ARG macros for each number of args we want to accept */
14804b3e5abSBenjamin Tissoires #define _ARG1(_1)                                                         _1;
14904b3e5abSBenjamin Tissoires #define _ARG2(_1, _2)                                                     _1; _2;
15004b3e5abSBenjamin Tissoires #define _ARG3(_1, _2, _3)                                                 _1; _2; _3;
15104b3e5abSBenjamin Tissoires #define _ARG4(_1, _2, _3, _4)                                             _1; _2; _3; _4;
15204b3e5abSBenjamin Tissoires #define _ARG5(_1, _2, _3, _4, _5)                                         _1; _2; _3; _4; _5;
15304b3e5abSBenjamin Tissoires #define _ARG6(_1, _2, _3, _4, _5, _6)                                     _1; _2; _3; _4; _5; _6;
15404b3e5abSBenjamin Tissoires #define _ARG7(_1, _2, _3, _4, _5, _6, _7)                                 _1; _2; _3; _4; _5; _6; _7;
15504b3e5abSBenjamin Tissoires #define _ARG8(_1, _2, _3, _4, _5, _6, _7, _8)                             _1; _2; _3; _4; _5; _6; _7; _8;
15604b3e5abSBenjamin Tissoires #define _ARG9(_1, _2, _3, _4, _5, _6, _7, _8, _9)                         _1; _2; _3; _4; _5; _6; _7; _8; _9;
15704b3e5abSBenjamin Tissoires #define _ARG10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a)                     _1; _2; _3; _4; _5; _6; _7; _8; _9; _a;
15804b3e5abSBenjamin Tissoires #define _ARG11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b)                 _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b;
15904b3e5abSBenjamin Tissoires #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;
16004b3e5abSBenjamin Tissoires #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;
16104b3e5abSBenjamin Tissoires #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;
16204b3e5abSBenjamin Tissoires #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;
16304b3e5abSBenjamin Tissoires 
16404b3e5abSBenjamin Tissoires 
16504b3e5abSBenjamin Tissoires #define HID_BPF_CONFIG(...)  union { \
16604b3e5abSBenjamin Tissoires 	_EXPAND(_ARG, __VA_ARGS__) \
16704b3e5abSBenjamin Tissoires } _device_ids SEC(".hid_bpf_config")
16804b3e5abSBenjamin Tissoires 
16904b3e5abSBenjamin Tissoires #endif /* __HID_BPF_HELPERS_H */
170