spiritlevel 0.2.0

TMDS link bring-up for HDMI sources: clock ratio, scrambling and sink configuration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Tear-down: stop the TMDS link a bring-up started.

use hdmi_hal::ddc::DdcError;

use crate::config::{ScramblerOff, SinkClearTiming, SinkPresence, TearDownConfig};
use crate::event::{SinkClearSkip, TmdsEvent, TmdsEventKind, TmdsStep};
use crate::outcome::{SinkClear, TearDownError, TornDown};
use crate::params::TmdsLinkParams;
use crate::types::SinkTmdsConfig;

use super::TmdsIo;

/// The parts of a tear-down, in the order they run.
#[derive(Clone, Copy)]
enum Part {
    Sink,
    Scrambler,
    Output,
}

/// The order of the parts with clearing on, from the two settings: the sink before or after
/// the output stops, the scrambler before or after the sink.
const fn order(config: &TearDownConfig) -> [Part; 3] {
    match (config.sink_clear_timing, config.scrambler_off) {
        (SinkClearTiming::BeforeOutputStops, ScramblerOff::AfterSink) => {
            [Part::Sink, Part::Scrambler, Part::Output]
        }
        (SinkClearTiming::BeforeOutputStops, ScramblerOff::BeforeSink) => {
            [Part::Scrambler, Part::Sink, Part::Output]
        }
        (SinkClearTiming::AfterOutputStops, ScramblerOff::AfterSink) => {
            [Part::Output, Part::Sink, Part::Scrambler]
        }
        (SinkClearTiming::AfterOutputStops, ScramblerOff::BeforeSink) => {
            [Part::Scrambler, Part::Output, Part::Sink]
        }
    }
}

/// Stops the TMDS link `link` describes.
///
/// With `clear_sink` off (the default), only the output stops: the next bring-up writes the
/// sink's `TMDS_Config` and the source's scrambler again. With it on, the sink's
/// `TMDS_Config` is written to 0 and the scrambler turned off, in the order the settings give,
/// and the output stops. The sink is written only when the link set something (not on a
/// plain link or a sink without SCDC) and the caller says it is present.
///
/// Every step is attempted whatever failed before: a failed sink write is a warning
/// ([`SinkClear::Failed`]), and a failed PHY call still lets the output stop. Not cancelled:
/// tear-down is the recovery. Powering the PHY down is the caller's, not part of it.
///
/// Records at most [`MAX_TEAR_DOWN_EVENTS`](crate::MAX_TEAR_DOWN_EVENTS) events.
///
/// # Errors
///
/// [`TearDownError`] when a PHY step failed, with the first error of each failed step.
pub async fn tear_down<Io: TmdsIo, F: FnMut(TmdsEvent)>(
    io: &mut Io,
    link: &TmdsLinkParams,
    config: &TearDownConfig,
    presence: SinkPresence,
    record: &mut F,
) -> Result<TornDown<Io::ScdcError>, TearDownError<Io::ScdcError, Io::PhyError>> {
    let mut emit = |io: &Io, kind| record(TmdsEvent::new(io.now(), kind));
    emit(
        io,
        TmdsEventKind::TearDownStarted {
            link: *link,
            config: *config,
            presence,
        },
    );

    let set_by_link = link
        .sink_config()
        .is_some_and(|c| c.scrambling_enable || c.high_tmds_clock_ratio);
    let mut skip = if !config.clear_sink {
        Some((SinkClearSkip::NotRequested, SinkClear::NotRequested))
    } else if !set_by_link {
        Some((SinkClearSkip::NothingToClear, SinkClear::NothingToClear))
    } else if presence == SinkPresence::Gone {
        Some((SinkClearSkip::SinkGone, SinkClear::SkippedSinkGone))
    } else {
        None
    };

    let ordered = order(config);
    let parts: &[Part] = if config.clear_sink {
        &ordered
    } else {
        // Clearing off: only the output stops. The sink part records why it was skipped.
        &[Part::Sink, Part::Output]
    };

    let mut sink = SinkClear::NotRequested;
    let mut scrambler = None;
    let mut output = None;
    for &part in parts {
        match part {
            Part::Sink => match skip.take() {
                Some((reason, result)) => {
                    emit(io, TmdsEventKind::SinkClearSkipped { reason });
                    sink = result;
                }
                None => {
                    let cleared = SinkTmdsConfig::new(false, false);
                    match io.write_tmds_config(cleared).await {
                        Ok(()) => {
                            emit(io, TmdsEventKind::TmdsConfigWritten { config: cleared });
                            sink = SinkClear::Cleared;
                        }
                        Err(error) => {
                            emit(
                                io,
                                TmdsEventKind::ScdcStepFailed {
                                    step: TmdsStep::WriteTmdsConfig,
                                    kind: error.kind(),
                                    ends: false,
                                },
                            );
                            sink = SinkClear::Failed(error);
                        }
                    }
                }
            },
            Part::Scrambler => match io.set_scrambling(false).await {
                Ok(()) => emit(io, TmdsEventKind::ScramblerSet { enabled: false }),
                Err(error) => {
                    emit(
                        io,
                        TmdsEventKind::PhyStepFailed {
                            step: TmdsStep::SetScrambling,
                            ends: false,
                        },
                    );
                    scrambler = Some(error);
                }
            },
            Part::Output => match io.stop_output().await {
                Ok(()) => emit(io, TmdsEventKind::OutputStopped),
                Err(error) => {
                    emit(
                        io,
                        TmdsEventKind::PhyStepFailed {
                            step: TmdsStep::StopOutput,
                            ends: false,
                        },
                    );
                    output = Some(error);
                }
            },
        }
    }

    emit(
        io,
        TmdsEventKind::TearDownEnded {
            sink: sink.kinds(),
            scrambler_failed: scrambler.is_some(),
            output_failed: output.is_some(),
        },
    );
    if scrambler.is_none() && output.is_none() {
        Ok(TornDown { sink })
    } else {
        Err(TearDownError {
            sink,
            scrambler,
            output,
        })
    }
}

#[cfg(test)]
mod tests {
    use hdmi_hal::ddc::DdcErrorKind;

    use super::*;
    use crate::MAX_TEAR_DOWN_EVENTS;
    use crate::procedure::test_io::{Op, TestIo, block_on};
    use crate::types::{SinkTmdsFacts, SourceTmdsFacts, TmdsPolicy};

    /// 4K60 RGB 8 bpc: scrambled at 1/40.
    fn scrambled() -> TmdsLinkParams {
        TmdsLinkParams::choose(
            594_000,
            SinkTmdsFacts::new(true, true, false),
            SourceTmdsFacts::new(false),
            TmdsPolicy::DEFAULT,
        )
        .unwrap()
    }

    /// 1080p60 on a sink with SCDC that does not declare low-rate scrambling: plain.
    fn plain() -> TmdsLinkParams {
        TmdsLinkParams::choose(
            148_500,
            SinkTmdsFacts::new(true, true, false),
            SourceTmdsFacts::new(false),
            TmdsPolicy::DEFAULT,
        )
        .unwrap()
    }

    /// 1080p60 scrambled at a low rate: `Scrambling_Enable` alone, ratio 1/10.
    fn low_rate() -> TmdsLinkParams {
        TmdsLinkParams::choose(
            148_500,
            SinkTmdsFacts::new(true, true, true),
            SourceTmdsFacts::new(true),
            TmdsPolicy::DEFAULT,
        )
        .unwrap()
    }

    const CLEARING: TearDownConfig = TearDownConfig::DEFAULT.with_clear_sink(true);

    fn run(
        io: &mut TestIo,
        link: &TmdsLinkParams,
        config: &TearDownConfig,
        presence: SinkPresence,
    ) -> (
        Result<TornDown<DdcErrorKind>, TearDownError<DdcErrorKind, ()>>,
        heapless_events::Events,
    ) {
        let mut events = heapless_events::Events::default();
        let result = block_on(tear_down(io, link, config, presence, &mut |e| {
            events.push(e)
        }));
        (result, events)
    }

    /// A fixed buffer of events, as a caller without `alloc` keeps them.
    mod heapless_events {
        use crate::event::{TmdsEvent, TmdsEventKind};

        #[derive(Default)]
        pub struct Events {
            pub list: [Option<TmdsEvent>; 8],
            pub len: usize,
        }

        impl Events {
            pub fn push(&mut self, event: TmdsEvent) {
                self.list[self.len] = Some(event);
                self.len += 1;
            }

            pub fn kinds(&self) -> impl Iterator<Item = TmdsEventKind> + '_ {
                self.list[..self.len].iter().map(|e| e.unwrap().kind)
            }
        }
    }

    #[test]
    fn by_default_only_the_output_stops() {
        let mut io = TestIo::new();
        let (result, events) = run(
            &mut io,
            &scrambled(),
            &TearDownConfig::DEFAULT,
            SinkPresence::Present,
        );
        assert_eq!(
            result,
            Ok(TornDown {
                sink: SinkClear::NotRequested
            })
        );
        assert_eq!(io.ops(), [Op::StopOutput]);
        let kinds: [TmdsEventKind; 4] = core::array::from_fn(|i| events.kinds().nth(i).unwrap());
        assert!(matches!(kinds[0], TmdsEventKind::TearDownStarted { .. }));
        assert_eq!(
            kinds[1],
            TmdsEventKind::SinkClearSkipped {
                reason: SinkClearSkip::NotRequested
            }
        );
        assert_eq!(kinds[2], TmdsEventKind::OutputStopped);
        assert_eq!(
            kinds[3],
            TmdsEventKind::TearDownEnded {
                sink: SinkClear::NotRequested,
                scrambler_failed: false,
                output_failed: false,
            }
        );
        assert_eq!(events.len, 4);
    }

    #[test]
    fn clearing_follows_the_order_the_settings_give() {
        let cleared = Op::WriteTmdsConfig(SinkTmdsConfig::new(false, false));
        let cases = [
            (
                SinkClearTiming::BeforeOutputStops,
                ScramblerOff::AfterSink,
                [cleared, Op::SetScrambling(false), Op::StopOutput],
            ),
            (
                SinkClearTiming::BeforeOutputStops,
                ScramblerOff::BeforeSink,
                [Op::SetScrambling(false), cleared, Op::StopOutput],
            ),
            (
                SinkClearTiming::AfterOutputStops,
                ScramblerOff::AfterSink,
                [Op::StopOutput, cleared, Op::SetScrambling(false)],
            ),
            (
                SinkClearTiming::AfterOutputStops,
                ScramblerOff::BeforeSink,
                [Op::SetScrambling(false), Op::StopOutput, cleared],
            ),
        ];
        for (timing, scrambler_off, expected) in cases {
            let config = CLEARING
                .with_sink_clear_timing(timing)
                .with_scrambler_off(scrambler_off);
            let mut io = TestIo::new();
            let (result, events) = run(&mut io, &scrambled(), &config, SinkPresence::Present);
            assert_eq!(
                result,
                Ok(TornDown {
                    sink: SinkClear::Cleared
                })
            );
            assert_eq!(io.ops(), expected, "{timing:?} {scrambler_off:?}");
            assert_eq!(events.len, MAX_TEAR_DOWN_EVENTS);
        }
    }

    #[test]
    fn nothing_is_written_to_a_plain_link_a_sink_without_scdc_or_a_gone_sink() {
        let no_scdc = TmdsLinkParams::choose(
            148_500,
            SinkTmdsFacts::NONE,
            SourceTmdsFacts::new(false),
            TmdsPolicy::DEFAULT,
        )
        .unwrap();
        for (link, presence, expected, reason) in [
            (
                plain(),
                SinkPresence::Present,
                SinkClear::NothingToClear,
                SinkClearSkip::NothingToClear,
            ),
            (
                no_scdc,
                SinkPresence::Present,
                SinkClear::NothingToClear,
                SinkClearSkip::NothingToClear,
            ),
            (
                scrambled(),
                SinkPresence::Gone,
                SinkClear::SkippedSinkGone,
                SinkClearSkip::SinkGone,
            ),
        ] {
            let mut io = TestIo::new();
            let (result, events) = run(&mut io, &link, &CLEARING, presence);
            assert_eq!(result, Ok(TornDown { sink: expected }));
            assert_eq!(io.ops(), [Op::SetScrambling(false), Op::StopOutput]);
            assert!(
                events
                    .kinds()
                    .any(|k| k == TmdsEventKind::SinkClearSkipped { reason })
            );
        }
    }

    #[test]
    fn a_failed_sink_write_is_a_warning() {
        let mut io = TestIo::new();
        io.fail_scdc = Some(DdcErrorKind::Busy);
        let (result, events) = run(&mut io, &scrambled(), &CLEARING, SinkPresence::Present);
        assert_eq!(
            result,
            Ok(TornDown {
                sink: SinkClear::Failed(DdcErrorKind::Busy)
            })
        );
        // The write was attempted and failed; the rest went on.
        assert_eq!(
            io.ops(),
            [
                Op::WriteTmdsConfig(SinkTmdsConfig::new(false, false)),
                Op::SetScrambling(false),
                Op::StopOutput
            ]
        );
        assert!(events.kinds().any(|k| k
            == TmdsEventKind::ScdcStepFailed {
                step: TmdsStep::WriteTmdsConfig,
                kind: DdcErrorKind::Busy,
                ends: false,
            }));
    }

    #[test]
    fn a_failed_phy_step_does_not_keep_the_output_running() {
        let mut io = TestIo::new();
        io.fail_set_scrambling = true;
        let (result, events) = run(&mut io, &scrambled(), &CLEARING, SinkPresence::Present);
        assert_eq!(
            result,
            Err(TearDownError {
                sink: SinkClear::Cleared,
                scrambler: Some(()),
                output: None,
            })
        );
        assert_eq!(io.ops().last(), Some(&Op::StopOutput));
        assert!(events.kinds().any(|k| k
            == TmdsEventKind::TearDownEnded {
                sink: SinkClear::Cleared,
                scrambler_failed: true,
                output_failed: false,
            }));

        let mut io = TestIo::new();
        io.fail_stop_output = true;
        let (result, events) = run(&mut io, &scrambled(), &CLEARING, SinkPresence::Present);
        assert_eq!(
            result,
            Err(TearDownError {
                sink: SinkClear::Cleared,
                scrambler: None,
                output: Some(()),
            })
        );
        assert!(events.kinds().any(|k| k
            == TmdsEventKind::PhyStepFailed {
                step: TmdsStep::StopOutput,
                ends: false,
            }));
    }

    #[test]
    fn a_low_rate_scrambled_link_is_cleared_too() {
        let link = low_rate();
        assert_eq!(link.sink_config(), Some(SinkTmdsConfig::new(true, false)));
        let mut io = TestIo::new();
        io.tmds_config = 0x01;
        let (result, _) = run(&mut io, &link, &CLEARING, SinkPresence::Present);
        assert_eq!(
            result,
            Ok(TornDown {
                sink: SinkClear::Cleared
            })
        );
        assert!(
            io.ops()
                .contains(&Op::WriteTmdsConfig(SinkTmdsConfig::new(false, false)))
        );
        assert_eq!(io.tmds_config, 0);
    }

    #[test]
    fn no_combination_records_more_than_the_bound() {
        for clear_sink in [false, true] {
            for timing in [
                SinkClearTiming::BeforeOutputStops,
                SinkClearTiming::AfterOutputStops,
            ] {
                for off in [ScramblerOff::AfterSink, ScramblerOff::BeforeSink] {
                    for presence in [SinkPresence::Present, SinkPresence::Gone] {
                        for link in [scrambled(), low_rate(), plain()] {
                            for fail in [false, true] {
                                let config = TearDownConfig::DEFAULT
                                    .with_clear_sink(clear_sink)
                                    .with_sink_clear_timing(timing)
                                    .with_scrambler_off(off);
                                let mut io = TestIo::new();
                                if fail {
                                    io.fail_scdc = Some(DdcErrorKind::Other);
                                    io.fail_set_scrambling = true;
                                    io.fail_stop_output = true;
                                }
                                let (_, events) = run(&mut io, &link, &config, presence);
                                assert!(events.len <= MAX_TEAR_DOWN_EVENTS);
                            }
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn events_are_stamped_from_the_io_clock() {
        let mut io = TestIo::new();
        let (_, events) = run(&mut io, &scrambled(), &CLEARING, SinkPresence::Present);
        let stamps: [u64; 5] = core::array::from_fn(|i| events.list[i].unwrap().at.as_micros());
        assert!(stamps.windows(2).all(|w| w[0] < w[1]), "{stamps:?}");
    }
}