perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / drivers / media / rc / bpf-lirc.c
blobee657003c1a1efbe135187c49f3e202643f72dd5
1 // SPDX-License-Identifier: GPL-2.0
2 // bpf-lirc.c - handles bpf
3 //
4 // Copyright (C) 2018 Sean Young <sean@mess.org>
6 #include <linux/bpf.h>
7 #include <linux/filter.h>
8 #include <linux/bpf_lirc.h>
9 #include "rc-core-priv.h"
12 * BPF interface for raw IR
14 const struct bpf_prog_ops lirc_mode2_prog_ops = {
17 BPF_CALL_1(bpf_rc_repeat, u32*, sample)
19 struct ir_raw_event_ctrl *ctrl;
21 ctrl = container_of(sample, struct ir_raw_event_ctrl, bpf_sample);
23 rc_repeat(ctrl->dev);
25 return 0;
28 static const struct bpf_func_proto rc_repeat_proto = {
29 .func = bpf_rc_repeat,
30 .gpl_only = true, /* rc_repeat is EXPORT_SYMBOL_GPL */
31 .ret_type = RET_INTEGER,
32 .arg1_type = ARG_PTR_TO_CTX,
36 * Currently rc-core does not support 64-bit scancodes, but there are many
37 * known protocols with more than 32 bits. So, define the interface as u64
38 * as a future-proof.
40 BPF_CALL_4(bpf_rc_keydown, u32*, sample, u32, protocol, u64, scancode,
41 u32, toggle)
43 struct ir_raw_event_ctrl *ctrl;
45 ctrl = container_of(sample, struct ir_raw_event_ctrl, bpf_sample);
47 rc_keydown(ctrl->dev, protocol, scancode, toggle != 0);
49 return 0;
52 static const struct bpf_func_proto rc_keydown_proto = {
53 .func = bpf_rc_keydown,
54 .gpl_only = true, /* rc_keydown is EXPORT_SYMBOL_GPL */
55 .ret_type = RET_INTEGER,
56 .arg1_type = ARG_PTR_TO_CTX,
57 .arg2_type = ARG_ANYTHING,
58 .arg3_type = ARG_ANYTHING,
59 .arg4_type = ARG_ANYTHING,
62 BPF_CALL_3(bpf_rc_pointer_rel, u32*, sample, s32, rel_x, s32, rel_y)
64 struct ir_raw_event_ctrl *ctrl;
66 ctrl = container_of(sample, struct ir_raw_event_ctrl, bpf_sample);
68 input_report_rel(ctrl->dev->input_dev, REL_X, rel_x);
69 input_report_rel(ctrl->dev->input_dev, REL_Y, rel_y);
70 input_sync(ctrl->dev->input_dev);
72 return 0;
75 static const struct bpf_func_proto rc_pointer_rel_proto = {
76 .func = bpf_rc_pointer_rel,
77 .gpl_only = true,
78 .ret_type = RET_INTEGER,
79 .arg1_type = ARG_PTR_TO_CTX,
80 .arg2_type = ARG_ANYTHING,
81 .arg3_type = ARG_ANYTHING,
84 static const struct bpf_func_proto *
85 lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
87 switch (func_id) {
88 case BPF_FUNC_rc_repeat:
89 return &rc_repeat_proto;
90 case BPF_FUNC_rc_keydown:
91 return &rc_keydown_proto;
92 case BPF_FUNC_rc_pointer_rel:
93 return &rc_pointer_rel_proto;
94 case BPF_FUNC_map_lookup_elem:
95 return &bpf_map_lookup_elem_proto;
96 case BPF_FUNC_map_update_elem:
97 return &bpf_map_update_elem_proto;
98 case BPF_FUNC_map_delete_elem:
99 return &bpf_map_delete_elem_proto;
100 case BPF_FUNC_map_push_elem:
101 return &bpf_map_push_elem_proto;
102 case BPF_FUNC_map_pop_elem:
103 return &bpf_map_pop_elem_proto;
104 case BPF_FUNC_map_peek_elem:
105 return &bpf_map_peek_elem_proto;
106 case BPF_FUNC_ktime_get_ns:
107 return &bpf_ktime_get_ns_proto;
108 case BPF_FUNC_tail_call:
109 return &bpf_tail_call_proto;
110 case BPF_FUNC_get_prandom_u32:
111 return &bpf_get_prandom_u32_proto;
112 case BPF_FUNC_trace_printk:
113 if (capable(CAP_SYS_ADMIN))
114 return bpf_get_trace_printk_proto();
115 /* fall through */
116 default:
117 return NULL;
121 static bool lirc_mode2_is_valid_access(int off, int size,
122 enum bpf_access_type type,
123 const struct bpf_prog *prog,
124 struct bpf_insn_access_aux *info)
126 /* We have one field of u32 */
127 return type == BPF_READ && off == 0 && size == sizeof(u32);
130 const struct bpf_verifier_ops lirc_mode2_verifier_ops = {
131 .get_func_proto = lirc_mode2_func_proto,
132 .is_valid_access = lirc_mode2_is_valid_access
135 #define BPF_MAX_PROGS 64
137 static int lirc_bpf_attach(struct rc_dev *rcdev, struct bpf_prog *prog)
139 struct bpf_prog_array __rcu *old_array;
140 struct bpf_prog_array *new_array;
141 struct ir_raw_event_ctrl *raw;
142 int ret;
144 if (rcdev->driver_type != RC_DRIVER_IR_RAW)
145 return -EINVAL;
147 ret = mutex_lock_interruptible(&ir_raw_handler_lock);
148 if (ret)
149 return ret;
151 raw = rcdev->raw;
152 if (!raw) {
153 ret = -ENODEV;
154 goto unlock;
157 if (raw->progs && bpf_prog_array_length(raw->progs) >= BPF_MAX_PROGS) {
158 ret = -E2BIG;
159 goto unlock;
162 old_array = raw->progs;
163 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
164 if (ret < 0)
165 goto unlock;
167 rcu_assign_pointer(raw->progs, new_array);
168 bpf_prog_array_free(old_array);
170 unlock:
171 mutex_unlock(&ir_raw_handler_lock);
172 return ret;
175 static int lirc_bpf_detach(struct rc_dev *rcdev, struct bpf_prog *prog)
177 struct bpf_prog_array __rcu *old_array;
178 struct bpf_prog_array *new_array;
179 struct ir_raw_event_ctrl *raw;
180 int ret;
182 if (rcdev->driver_type != RC_DRIVER_IR_RAW)
183 return -EINVAL;
185 ret = mutex_lock_interruptible(&ir_raw_handler_lock);
186 if (ret)
187 return ret;
189 raw = rcdev->raw;
190 if (!raw) {
191 ret = -ENODEV;
192 goto unlock;
195 old_array = raw->progs;
196 ret = bpf_prog_array_copy(old_array, prog, NULL, &new_array);
198 * Do not use bpf_prog_array_delete_safe() as we would end up
199 * with a dummy entry in the array, and the we would free the
200 * dummy in lirc_bpf_free()
202 if (ret)
203 goto unlock;
205 rcu_assign_pointer(raw->progs, new_array);
206 bpf_prog_array_free(old_array);
207 bpf_prog_put(prog);
208 unlock:
209 mutex_unlock(&ir_raw_handler_lock);
210 return ret;
213 void lirc_bpf_run(struct rc_dev *rcdev, u32 sample)
215 struct ir_raw_event_ctrl *raw = rcdev->raw;
217 raw->bpf_sample = sample;
219 if (raw->progs)
220 BPF_PROG_RUN_ARRAY(raw->progs, &raw->bpf_sample, BPF_PROG_RUN);
224 * This should be called once the rc thread has been stopped, so there can be
225 * no concurrent bpf execution.
227 void lirc_bpf_free(struct rc_dev *rcdev)
229 struct bpf_prog_array_item *item;
231 if (!rcdev->raw->progs)
232 return;
234 item = rcu_dereference(rcdev->raw->progs)->items;
235 while (item->prog) {
236 bpf_prog_put(item->prog);
237 item++;
240 bpf_prog_array_free(rcdev->raw->progs);
243 int lirc_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
245 struct rc_dev *rcdev;
246 int ret;
248 if (attr->attach_flags)
249 return -EINVAL;
251 rcdev = rc_dev_get_from_fd(attr->target_fd);
252 if (IS_ERR(rcdev))
253 return PTR_ERR(rcdev);
255 ret = lirc_bpf_attach(rcdev, prog);
257 put_device(&rcdev->dev);
259 return ret;
262 int lirc_prog_detach(const union bpf_attr *attr)
264 struct bpf_prog *prog;
265 struct rc_dev *rcdev;
266 int ret;
268 if (attr->attach_flags)
269 return -EINVAL;
271 prog = bpf_prog_get_type(attr->attach_bpf_fd,
272 BPF_PROG_TYPE_LIRC_MODE2);
273 if (IS_ERR(prog))
274 return PTR_ERR(prog);
276 rcdev = rc_dev_get_from_fd(attr->target_fd);
277 if (IS_ERR(rcdev)) {
278 bpf_prog_put(prog);
279 return PTR_ERR(rcdev);
282 ret = lirc_bpf_detach(rcdev, prog);
284 bpf_prog_put(prog);
285 put_device(&rcdev->dev);
287 return ret;
290 int lirc_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
292 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
293 struct bpf_prog_array __rcu *progs;
294 struct rc_dev *rcdev;
295 u32 cnt, flags = 0;
296 int ret;
298 if (attr->query.query_flags)
299 return -EINVAL;
301 rcdev = rc_dev_get_from_fd(attr->query.target_fd);
302 if (IS_ERR(rcdev))
303 return PTR_ERR(rcdev);
305 if (rcdev->driver_type != RC_DRIVER_IR_RAW) {
306 ret = -EINVAL;
307 goto put;
310 ret = mutex_lock_interruptible(&ir_raw_handler_lock);
311 if (ret)
312 goto put;
314 progs = rcdev->raw->progs;
315 cnt = progs ? bpf_prog_array_length(progs) : 0;
317 if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt))) {
318 ret = -EFAULT;
319 goto unlock;
322 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags))) {
323 ret = -EFAULT;
324 goto unlock;
327 if (attr->query.prog_cnt != 0 && prog_ids && cnt)
328 ret = bpf_prog_array_copy_to_user(progs, prog_ids, cnt);
330 unlock:
331 mutex_unlock(&ir_raw_handler_lock);
332 put:
333 put_device(&rcdev->dev);
335 return ret;