Debian Codegraph @

utils/keytable/bpf_protocols/imon_rsc.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_BITS_SPACE,
13 STATE_BITS_PULSE,
14 };
15
16 struct decoder_state {
17 unsigned long bits;
18 enum state state;
19 unsigned int count;
20 };
21
22 struct bpf_map_def SEC("lirc_mode2/maps") decoder_state_map = {
23 .type = BPF_MAP_TYPE_ARRAY,
24 .key_size = sizeof(unsigned int),
25 .value_size = sizeof(struct decoder_state),
26 .max_entries = 1,
27 };
28
29 // These values can be overridden in the rc_keymap toml
30 //
31 // We abuse elf relocations. We cast the address of these variables to
32 // an int, so that the compiler emits a mov immediate for the address
33 // but uses it as an int. The bpf loader replaces the relocation with the
34 // actual value (either overridden or taken from the data segment).
35 int margin = 325;
36
37 #define BPF_PARAM(x) (int)(long)(&(x))
38
39 static inline int eq_margin(unsigned d1, unsigned d2)
40 {
41 return ((d1 > (d2 - BPF_PARAM(margin))) && (d1 < (d2 + BPF_PARAM(margin))));
42 }
43
44 SEC("lirc_mode2/imon_rsc")
45 int bpf_decoder(unsigned int *sample)
46 {
47 unsigned int key = 0;
48 struct decoder_state *s = bpf_map_lookup_elem(&decoder_state_map, &key);
49
50 if (!s)
51 return 0;
52
53 switch (*sample & LIRC_MODE2_MASK) {
54 case LIRC_MODE2_SPACE:
55 case LIRC_MODE2_PULSE:
56 case LIRC_MODE2_TIMEOUT:
57 break;
58 default:
59 // not a timing events
60 return 0;
61 }
62
63 int duration = LIRC_VALUE(*sample);
64 int pulse = LIRC_IS_PULSE(*sample);
65
66 switch (s->state) {
67 case STATE_INACTIVE:
68 if (pulse && (eq_margin(duration, 2000) ||
69 eq_margin(duration, 3250))) {
70 s->state = STATE_BITS_SPACE;
71 s->bits = 0;
72 s->count = 0;
73 }
74 break;
75
76 case STATE_BITS_PULSE:
77 if (pulse && eq_margin(duration, 625))
78 s->state = STATE_BITS_SPACE;
79 else
80 s->state = STATE_INACTIVE;
81 break;
82
83 case STATE_BITS_SPACE:
84 if (pulse) {
85 s->state = STATE_INACTIVE;
86 break;
87 }
88
89 if (duration > 2400) {
90 int x = 0, y = 0;
91
92 if (!(s->count == 5 || s->count == 4)) {
93 s->state = STATE_INACTIVE;
94 break;
95 }
96
97 switch (s->bits & 0x0f) {
98 case 0x0: x = 0; y = -4; break;
99 case 0x1: x = 0; y = 4; break;
100 case 0x2: x = 4; y = 0; break;
101 case 0x3: x = -4; y = 0; break;
102
103 case 0x4: x = 4; y = -4; break;
104 case 0x5: x = -4; y = 4; break;
105 case 0x6: x = 4; y = 4; break;
106 case 0x7: x = -4; y = -4; break;
107
108 case 0xc: x = 4; y = -2; break;
109 case 0xd: x = -4; y = 2; break;
110 case 0xe: x = 2; y = 4; break;
111 case 0xf: x = -2; y = -4; break;
112
113 case 0x8: x = 2; y = -4; break;
114 case 0x9: x = -2; y = 4; break;
115 case 0xa: x = 4; y = 2; break;
116 case 0xb: x = -4; y = -2; break;
117 }
118
119 bpf_rc_pointer_rel(sample, x, y);
120
121 s->state = STATE_INACTIVE;
122 break;
123 }
124
125 s->bits <<= 1;
126
127 if (eq_margin(duration, 1800))
128 s->bits |= 1;
129 else if (!eq_margin(duration, 625)) {
130 s->state = STATE_INACTIVE;
131 break;
132 }
133 s->count++;
134 s->state = STATE_BITS_PULSE;
135 break;
136 }
137
138 return 0;
139 }
140
141 char _license[] SEC("license") = "GPL";