Debian Codegraph @

utils/keytable/bpf_protocols/pulse_length.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_REPEAT_SPACE,
14 STATE_BITS_SPACE,
15 STATE_BITS_PULSE,
16 STATE_TRAILER,
17 };
18
19 struct decoder_state {
20 unsigned long bits;
21 enum state state;
22 unsigned int count;
23 };
24
25 struct bpf_map_def SEC("lirc_mode2/maps") decoder_state_map = {
26 .type = BPF_MAP_TYPE_ARRAY,
27 .key_size = sizeof(unsigned int),
28 .value_size = sizeof(struct decoder_state),
29 .max_entries = 1,
30 };
31
32 // These values can be overridden in the rc_keymap toml
33 //
34 // We abuse elf relocations. We cast the address of these variables to
35 // an int, so that the compiler emits a mov immediate for the address
36 // but uses it as an int. The bpf loader replaces the relocation with the
37 // actual value (either overridden or taken from the data segment).
38 int margin = 200;
39 int header_pulse = 2125;
40 int header_space = 1875;
41 int repeat_pulse = 0;
42 int repeat_space = 0;
43 int bit_space = 625;
44 int bit_0_pulse = 375;
45 int bit_1_pulse = 1625;
46 int trailer_pulse = 0;
47 int bits = 4;
48 int reverse = 0;
49 int header_optional = 0;
50 int rc_protocol = 67;
51
52 #define BPF_PARAM(x) (int)(long)(&(x))
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/pulse_length")
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
65 if (!s)
66 return 0;
67
68 switch (*sample & LIRC_MODE2_MASK) {
69 case LIRC_MODE2_SPACE:
70 case LIRC_MODE2_PULSE:
71 case LIRC_MODE2_TIMEOUT:
72 break;
73 default:
74 // not a timing events
75 return 0;
76 }
77
78 int duration = LIRC_VALUE(*sample);
79 int pulse = LIRC_IS_PULSE(*sample);
80
81 switch (s->state) {
82 case STATE_HEADER_SPACE:
83 if (!pulse && eq_margin(BPF_PARAM(header_space), duration))
84 s->state = STATE_BITS_PULSE;
85 else
86 s->state = STATE_INACTIVE;
87 break;
88 case STATE_REPEAT_SPACE:
89 if (!pulse && eq_margin(BPF_PARAM(repeat_space), duration))
90 s->state = STATE_TRAILER;
91 else
92 s->state = STATE_INACTIVE;
93 break;
94 case STATE_INACTIVE:
95 if (pulse && eq_margin(BPF_PARAM(header_pulse), duration)) {
96 s->bits = 0;
97 s->state = STATE_HEADER_SPACE;
98 s->count = 0;
99 break;
100 }
101 if (pulse && BPF_PARAM(repeat_pulse) > 0 &&
102 eq_margin(BPF_PARAM(repeat_pulse), duration)) {
103 s->state = STATE_REPEAT_SPACE;
104 s->count = 0;
105 break;
106 }
107 if (!BPF_PARAM(header_optional))
108 break;
109 /* pass through */
110 case STATE_BITS_PULSE:
111 if (!pulse)
112 break;
113
114 unsigned long set_bit = 1;
115
116 if (BPF_PARAM(reverse))
117 set_bit <<= s->count;
118 else
119 s->bits <<= 1;
120
121 if (eq_margin(BPF_PARAM(bit_1_pulse), duration))
122 s->bits |= set_bit;
123 else if (!eq_margin(BPF_PARAM(bit_0_pulse), duration)) {
124 s->state = STATE_INACTIVE;
125 break;
126 }
127
128 s->count++;
129 if (s->count == BPF_PARAM(bits)) {
130 bpf_rc_keydown(sample, BPF_PARAM(rc_protocol), s->bits, 0);
131 s->state = STATE_INACTIVE;
132 } else {
133 s->state = STATE_BITS_SPACE;
134 }
135 break;
136 case STATE_BITS_SPACE:
137 if (!pulse && eq_margin(BPF_PARAM(bit_space), duration))
138 s->state = STATE_BITS_PULSE;
139 else
140 s->state = STATE_INACTIVE;
141 break;
142 case STATE_TRAILER:
143 if (pulse && eq_margin(BPF_PARAM(trailer_pulse), duration))
144 bpf_rc_repeat(sample);
145
146 s->state = STATE_INACTIVE;
147 }
148
149 return 0;
150 }
151
152 char _license[] SEC("license") = "GPL";