1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright (c) 2022 Benjamin Tissoires
3 */
4
5#ifndef __HID_BPF_HELPERS_H
6#define __HID_BPF_HELPERS_H
7
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;
22extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,
23 __u8 *buf, size_t buf__sz) __weak __ksym;
24extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,
25 enum hid_report_type type,
26 __u8 *data,
27 size_t buf__sz) __weak __ksym;
28extern int hid_bpf_try_input_report(struct hid_bpf_ctx *ctx,
29 enum hid_report_type type,
30 __u8 *data,
31 size_t buf__sz) __weak __ksym;
32
33/* bpf_wq implementation */
34extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
35extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
36extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,
37 int (callback_fn)(void *map, int *key, void *value),
38 unsigned int flags__k, void *aux__ign) __ksym;
39#define bpf_wq_set_callback(wq, cb, flags) \
40 bpf_wq_set_callback_impl(wq, cb, flags, NULL)
41
42#define HID_MAX_DESCRIPTOR_SIZE 4096
43#define HID_IGNORE_EVENT -1
44
45/* extracted from <linux/input.h> */
46#define BUS_ANY 0x00
47#define BUS_PCI 0x01
48#define BUS_ISAPNP 0x02
49#define BUS_USB 0x03
50#define BUS_HIL 0x04
51#define BUS_BLUETOOTH 0x05
52#define BUS_VIRTUAL 0x06
53#define BUS_ISA 0x10
54#define BUS_I8042 0x11
55#define BUS_XTKBD 0x12
56#define BUS_RS232 0x13
57#define BUS_GAMEPORT 0x14
58#define BUS_PARPORT 0x15
59#define BUS_AMIGA 0x16
60#define BUS_ADB 0x17
61#define BUS_I2C 0x18
62#define BUS_HOST 0x19
63#define BUS_GSC 0x1A
64#define BUS_ATARI 0x1B
65#define BUS_SPI 0x1C
66#define BUS_RMI 0x1D
67#define BUS_CEC 0x1E
68#define BUS_INTEL_ISHTP 0x1F
69#define BUS_AMD_SFH 0x20
70
71/* extracted from <linux/hid.h> */
72#define HID_GROUP_ANY 0x0000
73#define HID_GROUP_GENERIC 0x0001
74#define HID_GROUP_MULTITOUCH 0x0002
75#define HID_GROUP_SENSOR_HUB 0x0003
76#define HID_GROUP_MULTITOUCH_WIN_8 0x0004
77#define HID_GROUP_RMI 0x0100
78#define HID_GROUP_WACOM 0x0101
79#define HID_GROUP_LOGITECH_DJ_DEVICE 0x0102
80#define HID_GROUP_STEAM 0x0103
81#define HID_GROUP_LOGITECH_27MHZ_DEVICE 0x0104
82#define HID_GROUP_VIVALDI 0x0105
83
84/* include/linux/mod_devicetable.h defines as (~0), but that gives us negative size arrays */
85#define HID_VID_ANY 0x0000
86#define HID_PID_ANY 0x0000
87
88#define BIT(n) (1UL << (n))
89#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
90
91/* Helper macro to convert (foo, __LINE__) into foo134 so we can use __LINE__ for
92 * field/variable names
93 */
94#define COMBINE1(X, Y) X ## Y
95#define COMBINE(X, Y) COMBINE1(X, Y)
96
97/* Macro magic:
98 * __uint(foo, 123) creates a int (*foo)[1234]
99 *
100 * We use that macro to declare an anonymous struct with several
101 * fields, each is the declaration of an pointer to an array of size
102 * bus/group/vid/pid. (Because it's a pointer to such an array, actual storage
103 * would be sizeof(pointer) rather than sizeof(array). Not that we ever
104 * instantiate it anyway).
105 *
106 * This is only used for BTF introspection, we can later check "what size
107 * is the bus array" in the introspection data and thus extract the bus ID
108 * again.
109 *
110 * And we use the __LINE__ to give each of our structs a unique name so the
111 * BPF program writer doesn't have to.
112 *
113 * $ bpftool btf dump file target/bpf/HP_Elite_Presenter.bpf.o
114 * shows the inspection data, start by searching for .hid_bpf_config
115 * and working backwards from that (each entry references the type_id of the
116 * content).
117 */
118
119#define HID_DEVICE(b, g, ven, prod) \
120 struct { \
121 __uint(name, 0); \
122 __uint(bus, (b)); \
123 __uint(group, (g)); \
124 __uint(vid, (ven)); \
125 __uint(pid, (prod)); \
126 } COMBINE(_entry, __LINE__)
127
128/* Macro magic below is to make HID_BPF_CONFIG() look like a function call that
129 * we can pass multiple HID_DEVICE() invocations in.
130 *
131 * For up to 16 arguments, HID_BPF_CONFIG(one, two) resolves to
132 *
133 * union {
134 * HID_DEVICE(...);
135 * HID_DEVICE(...);
136 * } _device_ids SEC(".hid_bpf_config")
137 *
138 */
139
140/* Returns the number of macro arguments, this expands
141 * NARGS(a, b, c) to NTH_ARG(a, b, c, 15, 14, 13, .... 4, 3, 2, 1).
142 * NTH_ARG always returns the 16th argument which in our case is 3.
143 *
144 * If we want more than 16 values _COUNTDOWN and _NTH_ARG both need to be
145 * updated.
146 */
147#define _NARGS(...) _NARGS1(__VA_ARGS__, _COUNTDOWN)
148#define _NARGS1(...) _NTH_ARG(__VA_ARGS__)
149
150/* Add to this if we need more than 16 args */
151#define _COUNTDOWN \
152 15, 14, 13, 12, 11, 10, 9, 8, \
153 7, 6, 5, 4, 3, 2, 1, 0
154
155/* Return the 16 argument passed in. See _NARGS above for usage. Note this is
156 * 1-indexed.
157 */
158#define _NTH_ARG( \
159 _1, _2, _3, _4, _5, _6, _7, _8, \
160 _9, _10, _11, _12, _13, _14, _15,\
161 N, ...) N
162
163/* Turns EXPAND(_ARG, a, b, c) into _ARG3(a, b, c) */
164#define _EXPAND(func, ...) COMBINE(func, _NARGS(__VA_ARGS__)) (__VA_ARGS__)
165
166/* And now define all the ARG macros for each number of args we want to accept */
167#define _ARG1(_1) _1;
168#define _ARG2(_1, _2) _1; _2;
169#define _ARG3(_1, _2, _3) _1; _2; _3;
170#define _ARG4(_1, _2, _3, _4) _1; _2; _3; _4;
171#define _ARG5(_1, _2, _3, _4, _5) _1; _2; _3; _4; _5;
172#define _ARG6(_1, _2, _3, _4, _5, _6) _1; _2; _3; _4; _5; _6;
173#define _ARG7(_1, _2, _3, _4, _5, _6, _7) _1; _2; _3; _4; _5; _6; _7;
174#define _ARG8(_1, _2, _3, _4, _5, _6, _7, _8) _1; _2; _3; _4; _5; _6; _7; _8;
175#define _ARG9(_1, _2, _3, _4, _5, _6, _7, _8, _9) _1; _2; _3; _4; _5; _6; _7; _8; _9;
176#define _ARG10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a;
177#define _ARG11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b) _1; _2; _3; _4; _5; _6; _7; _8; _9; _a; _b;
178#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;
179#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;
180#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;
181#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;
182
183
184#define HID_BPF_CONFIG(...) union { \
185 _EXPAND(_ARG, __VA_ARGS__) \
186} _device_ids SEC(".hid_bpf_config")
187
188#endif /* __HID_BPF_HELPERS_H */
189

source code of linux/drivers/hid/bpf/progs/hid_bpf_helpers.h