Debian Codegraph @

utils/keytable/bpf_protocols/rc_mm.c

raw
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (C) 2018 Sean Young <[email protected]>
4
5 #include <linux/lirc.h>
6 #include <linux/bpf.h>
7
8 #include "bpf_helpers.h"
9
10 enum state {
11 STATE_INACTIVE,
12 STATE_HEADER_SPACE,
13 STATE_BITS_SPACE,
14 STATE_BITS_PULSE,
15 STATE_TRAILER,
16 };
17
18 struct decoder_state {
19 unsigned int bits;
20 enum state state;
21 unsigned int count;
22 };
23
24 struct bpf_map_def SEC("lirc_mode2/maps") decoder_state_map = {
25 .type = BPF_MAP_TYPE_ARRAY,
26 .key_size = sizeof(unsigned int),
27 .value_size = sizeof(struct decoder_state),
28 .max_entries = 1,
29 };
30
31 // These values can be overridden in the rc_keymap toml
32 //
33 // We abuse elf relocations. We cast the address of these variables to
34 // an int, so that the compiler emits a mov immediate for the address
35 // but uses it as an int. The bpf loader replaces the relocation with the
36 // actual value (either overridden or taken from the data segment).
37 //
38 // This is why they should be accessed through the BPF_PARAM() macro.
39
40 #define BPF_PARAM(x) (int)(long)(&(x))
41
42 int margin = 100;
43 int header_pulse = 417;
44 int header_space = 278;
45 int bit_pulse = 167;
46 int bit_0_space = 278;
47 int bit_1_space = 444;
48 int bit_2_space = 611;
49 int bit_3_space = 778;
50 int trailer = 167;
51 int bits = 8;
52 int rc_protocol = 65;
53
54 static inline int eq_margin(unsigned d1, unsigned d2)
55 {
56 return ((d1 > (d2 - BPF_PARAM(margin))) && (d1 < (d2 + BPF_PARAM(margin))));
57 }
58
59 SEC("lirc_mode2/rc_mm")
60 int bpf_decoder(unsigned int *sample)
61 {
62 unsigned int key = 0;
63 struct decoder_state *s = bpf_map_lookup_elem(&decoder_state_map, &key);
64 if (!s)
65 return 0;
66
67 switch (*sample & LIRC_MODE2_MASK) {
68 case LIRC_MODE2_SPACE:
69 case LIRC_MODE2_PULSE:
70 case LIRC_MODE2_TIMEOUT:
71 break;
72 default:
73 // not a timing events
74 return 0;
75 }
76
77 int duration = LIRC_VALUE(*sample);
78
79 switch (s->state) {
80 case STATE_INACTIVE:
81 if (LIRC_IS_PULSE(*sample) && eq_margin(BPF_PARAM(header_pulse), duration)) {
82 s->state = STATE_HEADER_SPACE;
83 s->bits = 0;
84 s->count = 0;
85 }
86 break;
87 case STATE_HEADER_SPACE:
88 if (LIRC_IS_SPACE(*sample) && eq_margin(BPF_PARAM(header_space), duration))
89 s->state = STATE_BITS_PULSE;
90 else
91 s->state = STATE_INACTIVE;
92 break;
93 case STATE_BITS_PULSE:
94 if (LIRC_IS_PULSE(*sample) && eq_margin(BPF_PARAM(bit_pulse), duration)) {
95 s->state = STATE_BITS_SPACE;
96 } else {
97 s->state = STATE_INACTIVE;
98 }
99 break;
100 case STATE_BITS_SPACE:
101 if (!LIRC_IS_SPACE(*sample)) {
102 s->state = STATE_INACTIVE;
103 break;
104 }
105
106 s->bits <<= 2;
107
108 if (eq_margin(BPF_PARAM(bit_0_space), duration)) {
109 s->bits |= 0;
110 } else if (eq_margin(BPF_PARAM(bit_1_space), duration)) {
111 s->bits |= 1;
112 } else if (eq_margin(BPF_PARAM(bit_2_space), duration)) {
113 s->bits |= 2;
114 } else if (eq_margin(BPF_PARAM(bit_3_space), duration)) {
115 s->bits |= 3;
116 } else {
117 s->state = STATE_INACTIVE;
118 break;
119 }
120
121 s->count += 2;
122 if (s->count >= BPF_PARAM(bits)) {
123 s->state = STATE_TRAILER;
124 } else {
125 s->state = STATE_BITS_PULSE;
126 }
127 break;
128 case STATE_TRAILER:
129 if (LIRC_IS_PULSE(*sample) && eq_margin(BPF_PARAM(trailer), duration))
130 bpf_rc_keydown(sample, BPF_PARAM(rc_protocol), s->bits, 0);
131
132 s->state = STATE_INACTIVE;
133 }
134
135 return 0;
136 }
137
138 char _license[] SEC("license") = "GPL";
139