Debian Codegraph @

utils/keytable/bpf_protocols/samsung36.c

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