Debian Codegraph @

utils/cec-ctl/cec-pin.cpp

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4 */
5
6 #include <string>
7
8 #include <linux/cec.h>
9 #include "cec-htng.h"
10
11 #include "cec-ctl.h"
12 #include "cec-log.h"
13
14 static std::string find_opcode_name(__u8 opcode)
15 {
16 const char *name = cec_opcode2s(opcode);
17
18 return name ? std::string(": ") + name : "";
19 }
20
21 static std::string find_cdc_opcode_name(__u8 opcode)
22 {
23 const char *name = cec_cdc_opcode2s(opcode);
24
25 return name ? std::string(": ") + name : "";
26 }
27
28 enum cec_state {
29 CEC_ST_IDLE,
30 CEC_ST_RECEIVE_START_BIT,
31 CEC_ST_RECEIVING_DATA,
32 };
33
34 /* All timings are in microseconds */
35 #define CEC_TIM_MARGIN 100
36
37 #define CEC_TIM_START_BIT_LOW 3700
38 #define CEC_TIM_START_BIT_LOW_MIN 3500
39 #define CEC_TIM_START_BIT_LOW_MAX 3900
40 #define CEC_TIM_START_BIT_TOTAL 4500
41 #define CEC_TIM_START_BIT_TOTAL_MIN 4300
42 #define CEC_TIM_START_BIT_TOTAL_MAX 4700
43
44 #define CEC_TIM_DATA_BIT_0_LOW 1500
45 #define CEC_TIM_DATA_BIT_0_LOW_MIN 1300
46 #define CEC_TIM_DATA_BIT_0_LOW_MAX 1700
47 #define CEC_TIM_DATA_BIT_1_LOW 600
48 #define CEC_TIM_DATA_BIT_1_LOW_MIN 400
49 #define CEC_TIM_DATA_BIT_1_LOW_MAX 800
50 #define CEC_TIM_DATA_BIT_TOTAL 2400
51 #define CEC_TIM_DATA_BIT_TOTAL_MIN 2050
52 #define CEC_TIM_DATA_BIT_TOTAL_MAX 2750
53 #define CEC_TIM_DATA_BIT_SAMPLE 1050
54 #define CEC_TIM_DATA_BIT_SAMPLE_MIN 850
55 #define CEC_TIM_DATA_BIT_SAMPLE_MAX 1250
56
57 #define CEC_TIM_IDLE_SAMPLE 1000
58 #define CEC_TIM_IDLE_SAMPLE_MIN 500
59 #define CEC_TIM_IDLE_SAMPLE_MAX 1500
60 #define CEC_TIM_START_BIT_SAMPLE 500
61 #define CEC_TIM_START_BIT_SAMPLE_MIN 300
62 #define CEC_TIM_START_BIT_SAMPLE_MAX 700
63
64 #define CEC_TIM_LOW_DRIVE_ERROR (1.5 * CEC_TIM_DATA_BIT_TOTAL)
65 #define CEC_TIM_LOW_DRIVE_ERROR_MIN (1.4 * CEC_TIM_DATA_BIT_TOTAL)
66 #define CEC_TIM_LOW_DRIVE_ERROR_MAX (1.6 * CEC_TIM_DATA_BIT_TOTAL)
67
68 /*
69 * Start and data bit error injection 'long' bit periods.
70 * The CEC PIN framework can insert too-long bit periods
71 * for either start or data bits. The period is stretched
72 * to these values.
73 *
74 * We warn but accept bit periods up to these values in the
75 * analyzer as this makes the analyzer results more readable.
76 */
77 #define CEC_TIM_START_BIT_TOTAL_LONG (5000 + CEC_TIM_MARGIN)
78 #define CEC_TIM_DATA_BIT_TOTAL_LONG (2900 + CEC_TIM_MARGIN)
79
80 __u64 eob_ts;
81 __u64 eob_ts_max;
82
83 // Global CEC state
84 static enum cec_state state;
85 static double ts;
86 static __u64 low_usecs;
87 static unsigned int rx_bit;
88 static __u8 byte;
89 static bool eom;
90 static bool eom_reached;
91 static __u8 byte_cnt;
92 static bool bcast;
93 static bool cdc;
94 static struct cec_msg msg;
95
96 static void cec_pin_rx_start_bit_was_high(bool is_high, __u64 usecs, __u64 usecs_min, bool show)
97 {
98 bool period_too_long = low_usecs + usecs > CEC_TIM_START_BIT_TOTAL_LONG;
99
100 if (is_high && show)
101 printf("%s: warn: start bit: total period too long\n", ts2s(ts).c_str());
102 else if (low_usecs + usecs > CEC_TIM_START_BIT_TOTAL_MAX && show)
103 printf("%s: warn: start bit: total period too long (%.2f > %.2f ms)\n",
104 ts2s(ts).c_str(), (low_usecs + usecs) / 1000.0,
105 CEC_TIM_START_BIT_TOTAL_MAX / 1000.0);
106 if (is_high || period_too_long) {
107 if (show)
108 printf("\n");
109 state = CEC_ST_IDLE;
110 return;
111 }
112 if (low_usecs + usecs < CEC_TIM_START_BIT_TOTAL_MIN - CEC_TIM_MARGIN && show)
113 printf("%s: warn: start bit: total period too short (%.2f < %.2f ms)\n",
114 ts2s(ts).c_str(), (low_usecs + usecs) / 1000.0,
115 CEC_TIM_START_BIT_TOTAL_MIN / 1000.0);
116 state = CEC_ST_RECEIVING_DATA;
117 rx_bit = 0;
118 byte = 0;
119 eom = false;
120 eom_reached = false;
121 byte_cnt = 0;
122 bcast = false;
123 cdc = false;
124 msg.len = 0;
125 }
126
127 static void cec_pin_rx_start_bit_was_low(__u64 ev_ts, __u64 usecs, __u64 usecs_min, bool show)
128 {
129 if (usecs_min > CEC_TIM_START_BIT_LOW_MAX && show)
130 printf("%s: warn: start bit: low time too long (%.2f > %.2f ms)\n",
131 ts2s(ts).c_str(), usecs / 1000.0,
132 CEC_TIM_START_BIT_LOW_MAX / 1000.0);
133 if (usecs_min > CEC_TIM_START_BIT_LOW_MAX + CEC_TIM_MARGIN * 5) {
134 if (show)
135 printf("\n");
136 state = CEC_ST_IDLE;
137 return;
138 }
139 if (usecs_min < CEC_TIM_START_BIT_LOW_MIN - CEC_TIM_MARGIN * 6) {
140 state = CEC_ST_IDLE;
141 return;
142 }
143 low_usecs = usecs;
144 eob_ts = ev_ts + 1000 * (CEC_TIM_START_BIT_TOTAL - low_usecs);
145 eob_ts_max = ev_ts + 1000 * (CEC_TIM_START_BIT_TOTAL_LONG - low_usecs);
146 }
147
148 static void cec_pin_rx_data_bit_was_high(bool is_high, __u64 ev_ts,
149 __u64 usecs, __u64 usecs_min, bool show)
150 {
151 bool period_too_long = low_usecs + usecs > CEC_TIM_DATA_BIT_TOTAL_LONG;
152 bool bit;
153
154 if (is_high && rx_bit < 9 && show)
155 printf("%s: warn: data bit %d: total period too long\n", ts2s(ts).c_str(), rx_bit);
156 else if (rx_bit < 9 && show &&
157 low_usecs + usecs > CEC_TIM_DATA_BIT_TOTAL_MAX + CEC_TIM_MARGIN)
158 printf("%s: warn: data bit %d: total period too long (%.2f ms)\n",
159 ts2s(ts).c_str(), rx_bit, (low_usecs + usecs) / 1000.0);
160 if (low_usecs + usecs < CEC_TIM_DATA_BIT_TOTAL_MIN - CEC_TIM_MARGIN && show)
161 printf("%s: warn: data bit %d: total period too short (%.2f ms)\n",
162 ts2s(ts).c_str(), rx_bit, (low_usecs + usecs) / 1000.0);
163
164 bit = low_usecs < CEC_TIM_DATA_BIT_1_LOW_MAX + CEC_TIM_MARGIN;
165 if (rx_bit <= 7) {
166 byte |= bit << (7 - rx_bit);
167 } else if (rx_bit == 8) {
168 eom = bit;
169 } else {
170 std::string s;
171
172 if (byte_cnt == 0) {
173 bcast = (byte & 0xf) == 0xf;
174 s = ": " + std::string(cec_la2s(byte >> 4)) +
175 " to " + (bcast ? "All" : cec_la2s(byte & 0xf));
176 } else if (byte_cnt == 1) {
177 s = find_opcode_name(byte);
178 } else if (cdc && byte_cnt == 4) {
179 s = find_cdc_opcode_name(byte);
180 }
181
182 bool ack = !(bcast ^ bit);
183
184 if (msg.len < CEC_MAX_MSG_SIZE)
185 msg.msg[msg.len++] = byte;
186 if (show)
187 printf("%s: rx 0x%02x%s%s%s%s%s\n", ts2s(ts).c_str(), byte,
188 eom ? " EOM" : "", ack ? " ACK" : " NACK",
189 bcast ? " (broadcast)" : "",
190 eom_reached ? " (warn: spurious byte)" : "",
191 s.c_str());
192 if (!eom_reached && is_high && !eom && ack && show)
193 printf("%s: warn: missing EOM\n", ts2s(ts).c_str());
194 else if (!is_high && !period_too_long && verbose && show)
195 printf("\n");
196 if (byte_cnt == 1 && byte == CEC_MSG_CDC_MESSAGE)
197 cdc = true;
198 byte_cnt++;
199 if (byte_cnt >= CEC_MAX_MSG_SIZE)
200 eom_reached = true;
201 if (show && eom && msg.len > 2) {
202 msg.rx_status = CEC_RX_STATUS_OK;
203 msg.rx_ts = ev_ts;
204 printf("\nTransmit from %s to %s (%d to %d):\n",
205 cec_la2s(cec_msg_initiator(&msg)),
206 cec_msg_is_broadcast(&msg) ? "all" : cec_la2s(cec_msg_destination(&msg)),
207 cec_msg_initiator(&msg), cec_msg_destination(&msg));
208 cec_log_msg(&msg);
209 }
210 }
211 rx_bit++;
212 if ((is_high || period_too_long) && !eom) {
213 eom_reached = false;
214 if (show)
215 printf("\n");
216 state = is_high ? CEC_ST_IDLE : CEC_ST_RECEIVE_START_BIT;
217 return;
218 }
219 if (rx_bit == 10) {
220 if (eom) {
221 eom_reached = true;
222 if (is_high) {
223 if (show)
224 printf("\n");
225 state = CEC_ST_IDLE;
226 }
227 }
228 rx_bit = 0;
229 byte = 0;
230 eom = false;
231 }
232 }
233
234 static void cec_pin_rx_data_bit_was_low(__u64 ev_ts, __u64 usecs, __u64 usecs_min, bool show)
235 {
236 /*
237 * If the low drive starts at the end of a 0 bit, then the actual
238 * maximum time that the bus can be low is the two summed.
239 */
240 const unsigned max_low_drive = static_cast<unsigned>(CEC_TIM_LOW_DRIVE_ERROR_MAX) +
241 CEC_TIM_DATA_BIT_0_LOW_MAX + CEC_TIM_MARGIN;
242
243 low_usecs = usecs;
244 if (usecs >= CEC_TIM_LOW_DRIVE_ERROR_MIN - CEC_TIM_MARGIN) {
245 if (usecs >= max_low_drive && show)
246 printf("%s: warn: low drive too long (%.2f > %.2f ms)\n\n",
247 ts2s(ts).c_str(), usecs / 1000.0,
248 CEC_TIM_LOW_DRIVE_ERROR_MAX / 1000.0);
249 if (show)
250 printf("\n");
251 state = CEC_ST_IDLE;
252 return;
253 }
254
255 if (rx_bit == 0 && byte_cnt &&
256 usecs >= CEC_TIM_START_BIT_LOW_MIN - CEC_TIM_MARGIN) {
257 if (show)
258 printf("%s: warn: unexpected start bit\n", ts2s(ts).c_str());
259 cec_pin_rx_start_bit_was_low(ev_ts, usecs, usecs_min, show);
260 state = CEC_ST_RECEIVE_START_BIT;
261 return;
262 }
263
264 if (usecs_min > CEC_TIM_DATA_BIT_0_LOW_MAX) {
265 if (show)
266 printf("%s: warn: data bit %d: low time too long (%.2f ms)\n",
267 ts2s(ts).c_str(), rx_bit, usecs / 1000.0);
268 if (usecs_min > CEC_TIM_DATA_BIT_TOTAL_MAX) {
269 if (show)
270 printf("\n");
271 state = CEC_ST_IDLE;
272 }
273 return;
274 }
275 if (usecs_min > CEC_TIM_DATA_BIT_1_LOW_MAX &&
276 usecs < CEC_TIM_DATA_BIT_0_LOW_MIN - CEC_TIM_MARGIN && show) {
277 printf("%s: warn: data bit %d: invalid 0->1 transition (%.2f ms)\n",
278 ts2s(ts).c_str(), rx_bit, usecs / 1000.0);
279 }
280 if (usecs < CEC_TIM_DATA_BIT_1_LOW_MIN - CEC_TIM_MARGIN && show) {
281 printf("%s: warn: data bit %d: low time too short (%.2f ms)\n",
282 ts2s(ts).c_str(), rx_bit, usecs / 1000.0);
283 }
284
285 eob_ts = ev_ts + 1000 * (CEC_TIM_DATA_BIT_TOTAL - low_usecs);
286 eob_ts_max = ev_ts + 1000 * (CEC_TIM_DATA_BIT_TOTAL_LONG - low_usecs);
287 }
288
289 static void cec_pin_debug(__u64 ev_ts, __u64 usecs, bool was_high, bool is_high, bool show)
290 {
291 __u64 usecs_min = usecs > CEC_TIM_MARGIN ? usecs - CEC_TIM_MARGIN : 0;
292
293 switch (state) {
294 case CEC_ST_RECEIVE_START_BIT:
295 eom_reached = false;
296 if (was_high)
297 cec_pin_rx_start_bit_was_high(is_high, usecs, usecs_min, show);
298 else
299 cec_pin_rx_start_bit_was_low(ev_ts, usecs, usecs_min, show);
300 break;
301
302 case CEC_ST_RECEIVING_DATA:
303 if (was_high)
304 cec_pin_rx_data_bit_was_high(is_high, ev_ts, usecs, usecs_min, show);
305 else
306 cec_pin_rx_data_bit_was_low(ev_ts, usecs, usecs_min, show);
307 break;
308
309 case CEC_ST_IDLE:
310 eom_reached = false;
311 if (!is_high)
312 state = CEC_ST_RECEIVE_START_BIT;
313 break;
314 }
315 }
316
317 #define verb_printf(fmt, args...) \
318 do { \
319 if (verbose) \
320 printf(fmt, ##args); \
321 } while (0)
322
323 void log_event_pin(bool is_high, __u64 ev_ts, bool show)
324 {
325 static __u64 last_ts;
326 static __u64 last_change_ts;
327 static __u64 last_1_to_0_ts;
328 static bool was_high = true;
329 double bit_periods = ((ev_ts - last_ts) / 1000.0) / CEC_TIM_DATA_BIT_TOTAL;
330
331 eob_ts = eob_ts_max = 0;
332
333 ts = ev_ts / 1000000000.0;
334 if (last_change_ts == 0) {
335 last_ts = last_change_ts = last_1_to_0_ts = ev_ts - CEC_TIM_DATA_BIT_TOTAL * 16000;
336 if (is_high)
337 return;
338 }
339 if (show) {
340 double delta = (ev_ts - last_change_ts) / 1000000.0;
341
342 if (!was_high && last_change_ts && state == CEC_ST_RECEIVE_START_BIT &&
343 delta * 1000 >= CEC_TIM_START_BIT_LOW_MIN - CEC_TIM_MARGIN)
344 verb_printf("\n");
345 verb_printf("%s: ", ts2s(ts).c_str());
346 if (last_change_ts && is_high && was_high &&
347 (ev_ts - last_1_to_0_ts) / 1000000 <= 10) {
348 verb_printf("1 -> 1 (was 1 for %.2f ms, period of previous %spulse %.2f ms)\n",
349 delta, state == CEC_ST_RECEIVE_START_BIT ? "start " : "",
350 (ev_ts - last_1_to_0_ts) / 1000000.0);
351 } else if (last_change_ts && is_high && was_high) {
352 verb_printf("1 -> 1 (%.2f ms)\n", delta);
353 } else if (was_high && state == CEC_ST_IDLE) {
354 if (bit_periods > 1 && bit_periods < 10)
355 verb_printf("1 -> 0 (was 1 for %.2f ms, signal free time = %.1f bit periods)\n",
356 delta, bit_periods);
357 else
358 verb_printf("1 -> 0 (was 1 for %.2f ms)\n", delta);
359 } else if (was_high && (ev_ts - last_1_to_0_ts) / 1000000 <= 10) {
360 verb_printf("1 -> 0 (was 1 for %.2f ms, period of previous %spulse %.2f ms)\n",
361 delta, state == CEC_ST_RECEIVE_START_BIT ? "start " : "",
362 (ev_ts - last_1_to_0_ts) / 1000000.0);
363 } else if (was_high) {
364 verb_printf("1 -> 0 (was 1 for %.2f ms)\n", delta);
365 } else if (last_change_ts && state == CEC_ST_RECEIVE_START_BIT &&
366 delta * 1000 < CEC_TIM_START_BIT_LOW_MIN - CEC_TIM_MARGIN) {
367 verb_printf("0 -> 1 (was 0 for %.2f ms, might indicate %d bit)\n", delta,
368 delta * 1000 < CEC_TIM_DATA_BIT_1_LOW_MAX + CEC_TIM_MARGIN);
369 } else if (last_change_ts && state == CEC_ST_RECEIVE_START_BIT) {
370 verb_printf("0 -> 1 (was 0 for %.2f ms)\n", delta);
371 } else if (last_change_ts &&
372 delta * 1000 >= CEC_TIM_LOW_DRIVE_ERROR_MIN - CEC_TIM_MARGIN) {
373 if (verbose)
374 printf("0 -> 1 (was 0 for %.2f ms, warn: indicates low drive)\n", delta);
375 else
376 printf("\n%s: warn: low drive for %.2f ms\n", ts2s(ts).c_str(), delta);
377 } else if (last_change_ts) {
378 verb_printf("0 -> 1 (was 0 for %.2f ms, indicates %d bit)\n", delta,
379 delta * 1000 < CEC_TIM_DATA_BIT_1_LOW_MAX + CEC_TIM_MARGIN);
380 } else {
381 verb_printf("0 -> 1\n");
382 }
383
384 if (!verbose && !is_high && bit_periods > 1 && bit_periods < 10)
385 printf("%s: signal free time = %.1f bit periods\n",
386 ts2s(ts).c_str(), bit_periods);
387 }
388 cec_pin_debug(ev_ts, (ev_ts - last_ts) / 1000, was_high, is_high, show);
389 last_change_ts = ev_ts;
390 if (!is_high)
391 last_1_to_0_ts = ev_ts;
392 last_ts = ev_ts;
393 was_high = is_high;
394 }