The Pedigree Project 0.1
UsbStartup.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef USB_STARTUP_H
3#define USB_STARTUP_H
4#include <stddef.h>
5
6namespace UsbStartup {
7enum class Outcome { Neutral, Failed, DeviceReady };
8
9// The controller serializes access. Nested hub enumeration and concurrent PnP
10// probes must settle before a failed takeover can return the whole controller.
11class State {
12 public:
13 void enableRecovery(bool enabled) {
14 m_Enabled = enabled;
15 }
16 bool begin() {
17 if (m_Closed)
18 return false;
19 ++m_Active;
20 return true;
21 }
22 bool finish(Outcome outcome) {
23 if (!m_Active)
24 return false;
25 m_Failed |= outcome == Outcome::Failed;
26 m_Committed |= outcome == Outcome::DeviceReady;
27 --m_Active;
28 if (m_Enabled && !m_Closed && !m_Active && m_Failed && !m_Committed) {
29 m_Closed = true;
30 return true;
31 }
32 return false;
33 }
34 void close() {
35 m_Closed = true;
36 }
37
38 private:
39 size_t m_Active = 0;
40 bool m_Enabled = false;
41 bool m_Failed = false;
42 bool m_Committed = false;
43 bool m_Closed = false;
44};
45} // namespace UsbStartup
46#endif