1 /* SPDX-License-Identifier: GPL-2.0+ */
7 #include <linux/spinlock.h>
8 #include <uapi/linux/hid.h>
13 * The following is the user facing HID BPF API.
15 * Extra care should be taken when editing this part, as
16 * it might break existing out of the tree bpf programs.
20 * struct hid_bpf_ctx - User accessible data for all HID programs
22 * ``data`` is not directly accessible from the context. We need to issue
23 * a call to ``hid_bpf_get_data()`` in order to get a pointer to that field.
25 * All of these fields are currently read-only.
27 * @index: program index in the jump table. No special meaning (a smaller index
28 * doesn't mean the program will be executed before another program with
30 * @hid: the ``struct hid_device`` representing the device itself
31 * @report_type: used for ``hid_bpf_device_event()``
32 * @allocated_size: Allocated size of data.
34 * This is how much memory is available and can be requested
36 * Note that for ``HID_BPF_RDESC_FIXUP``, that memory is set to
38 * @size: Valid data in the data field.
40 * Programs can get the available valid size in data by fetching this field.
41 * Programs can also change this value by returning a positive number in the
43 * To discard the event, return a negative error code.
45 * ``size`` must always be less or equal than ``allocated_size`` (it is enforced
46 * once all BPF programs have been run).
47 * @retval: Return value of the previous program.
51 const struct hid_device *hid;
53 enum hid_report_type report_type;
61 * enum hid_bpf_attach_flags - flags used when attaching a HIF-BPF program
63 * @HID_BPF_FLAG_NONE: no specific flag is used, the kernel choses where to
65 * @HID_BPF_FLAG_INSERT_HEAD: insert the given program before any other program
66 * currently attached to the device. This doesn't
67 * guarantee that this program will always be first
68 * @HID_BPF_FLAG_MAX: sentinel value, not to be used by the callers
70 enum hid_bpf_attach_flags {
71 HID_BPF_FLAG_NONE = 0,
72 HID_BPF_FLAG_INSERT_HEAD = _BITUL(0),
76 /* Following functions are tracepoints that BPF programs can attach to */
77 int hid_bpf_device_event(struct hid_bpf_ctx *ctx);
78 int hid_bpf_rdesc_fixup(struct hid_bpf_ctx *ctx);
80 /* Following functions are kfunc that we export to BPF programs */
81 /* available everywhere in HID-BPF */
82 __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t __sz);
84 /* only available in syscall */
85 int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags);
86 int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz,
87 enum hid_report_type rtype, enum hid_class_request reqtype);
88 struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id);
89 void hid_bpf_release_context(struct hid_bpf_ctx *ctx);
92 * Below is HID internal
95 /* internal function to call eBPF programs, not to be used by anybody */
96 int __hid_bpf_tail_call(struct hid_bpf_ctx *ctx);
98 #define HID_BPF_MAX_PROGS_PER_DEV 64
99 #define HID_BPF_FLAG_MASK (((HID_BPF_FLAG_MAX - 1) << 1) - 1)
101 /* types of HID programs to attach to */
102 enum hid_bpf_prog_type {
103 HID_BPF_PROG_TYPE_UNDEF = -1,
104 HID_BPF_PROG_TYPE_DEVICE_EVENT, /* an event is emitted from the device */
105 HID_BPF_PROG_TYPE_RDESC_FIXUP,
106 HID_BPF_PROG_TYPE_MAX,
109 struct hid_report_enum;
112 struct hid_report *(*hid_get_report)(struct hid_report_enum *report_enum, const u8 *data);
113 int (*hid_hw_raw_request)(struct hid_device *hdev,
114 unsigned char reportnum, __u8 *buf,
115 size_t len, enum hid_report_type rtype,
116 enum hid_class_request reqtype);
117 struct module *owner;
118 struct bus_type *bus_type;
121 extern struct hid_bpf_ops *hid_bpf_ops;
123 struct hid_bpf_prog_list {
124 u16 prog_idx[HID_BPF_MAX_PROGS_PER_DEV];
128 /* stored in each device */
130 u8 *device_data; /* allocated when a bpf program of type
131 * SEC(f.../hid_bpf_device_event) has been attached
136 struct hid_bpf_prog_list __rcu *progs[HID_BPF_PROG_TYPE_MAX]; /* attached BPF progs */
137 bool destroyed; /* prevents the assignment of any progs */
139 spinlock_t progs_lock; /* protects RCU update of progs */
142 /* specific HID-BPF link when a program is attached to a device */
143 struct hid_bpf_link {
144 struct bpf_link link;
148 #ifdef CONFIG_HID_BPF
149 u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type, u8 *data,
150 u32 *size, int interrupt);
151 int hid_bpf_connect_device(struct hid_device *hdev);
152 void hid_bpf_disconnect_device(struct hid_device *hdev);
153 void hid_bpf_destroy_device(struct hid_device *hid);
154 void hid_bpf_device_init(struct hid_device *hid);
155 u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *size);
156 #else /* CONFIG_HID_BPF */
157 static inline u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type,
158 u8 *data, u32 *size, int interrupt) { return data; }
159 static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; }
160 static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {}
161 static inline void hid_bpf_destroy_device(struct hid_device *hid) {}
162 static inline void hid_bpf_device_init(struct hid_device *hid) {}
163 static inline u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *size)
165 return kmemdup(rdesc, *size, GFP_KERNEL);
168 #endif /* CONFIG_HID_BPF */
170 #endif /* __HID_BPF_H */