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);
81 * Below is HID internal
84 /* internal function to call eBPF programs, not to be used by anybody */
85 int __hid_bpf_tail_call(struct hid_bpf_ctx *ctx);
87 #define HID_BPF_MAX_PROGS_PER_DEV 64
88 #define HID_BPF_FLAG_MASK (((HID_BPF_FLAG_MAX - 1) << 1) - 1)
90 /* types of HID programs to attach to */
91 enum hid_bpf_prog_type {
92 HID_BPF_PROG_TYPE_UNDEF = -1,
93 HID_BPF_PROG_TYPE_DEVICE_EVENT, /* an event is emitted from the device */
94 HID_BPF_PROG_TYPE_RDESC_FIXUP,
95 HID_BPF_PROG_TYPE_MAX,
98 struct hid_report_enum;
101 struct hid_report *(*hid_get_report)(struct hid_report_enum *report_enum, const u8 *data);
102 int (*hid_hw_raw_request)(struct hid_device *hdev,
103 unsigned char reportnum, __u8 *buf,
104 size_t len, enum hid_report_type rtype,
105 enum hid_class_request reqtype);
106 int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len);
107 int (*hid_input_report)(struct hid_device *hid, enum hid_report_type type,
108 u8 *data, u32 size, int interrupt);
109 struct module *owner;
110 const struct bus_type *bus_type;
113 extern struct hid_bpf_ops *hid_bpf_ops;
115 struct hid_bpf_prog_list {
116 u16 prog_idx[HID_BPF_MAX_PROGS_PER_DEV];
120 /* stored in each device */
122 u8 *device_data; /* allocated when a bpf program of type
123 * SEC(f.../hid_bpf_device_event) has been attached
128 struct hid_bpf_prog_list __rcu *progs[HID_BPF_PROG_TYPE_MAX]; /* attached BPF progs */
129 bool destroyed; /* prevents the assignment of any progs */
131 spinlock_t progs_lock; /* protects RCU update of progs */
134 /* specific HID-BPF link when a program is attached to a device */
135 struct hid_bpf_link {
136 struct bpf_link link;
140 #ifdef CONFIG_HID_BPF
141 u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type, u8 *data,
142 u32 *size, int interrupt);
143 int hid_bpf_connect_device(struct hid_device *hdev);
144 void hid_bpf_disconnect_device(struct hid_device *hdev);
145 void hid_bpf_destroy_device(struct hid_device *hid);
146 void hid_bpf_device_init(struct hid_device *hid);
147 u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *size);
148 #else /* CONFIG_HID_BPF */
149 static inline u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type,
150 u8 *data, u32 *size, int interrupt) { return data; }
151 static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; }
152 static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {}
153 static inline void hid_bpf_destroy_device(struct hid_device *hid) {}
154 static inline void hid_bpf_device_init(struct hid_device *hid) {}
155 #define call_hid_bpf_rdesc_fixup(_hdev, _rdesc, _size) \
156 ((u8 *)kmemdup(_rdesc, *(_size), GFP_KERNEL))
158 #endif /* CONFIG_HID_BPF */
160 #endif /* __HID_BPF_H */