|
| 1 | +# USB Device Framework (usb::df) |
| 2 | + |
| 3 | +`usb::df` is a high-level USB device framework that separates: |
| 4 | + |
| 5 | +1. Device policy and configuration modeling |
| 6 | +2. USB class/function logic (HID, CDC-ACM, vendor functions) |
| 7 | +3. Hardware/RTOS USB controller integration (MAC implementations) |
| 8 | + |
| 9 | +The key design idea is that a USB configuration is defined manually at a high level |
| 10 | +as a static composition of: |
| 11 | + |
| 12 | +1. Power properties |
| 13 | +2. Interfaces bound to function objects |
| 14 | +3. Endpoints bound to those interfaces |
| 15 | + |
| 16 | +The framework then uses those definitions to build descriptors and route control/data |
| 17 | +transfers without requiring descriptor blobs handwritten by the application. |
| 18 | +This manual definition allows for a device framework with multiple number of configurations |
| 19 | +for each bus speed (and even alternative configurations for MS Windows OS), |
| 20 | +that can be easily modified at runtime as well. |
| 21 | + |
| 22 | +## Design goals |
| 23 | + |
| 24 | +- Portable high-level USB function logic across platforms |
| 25 | +- Explicit, static configuration composition (no hidden dynamic behavior) |
| 26 | +- No dynamic memory requirement in the framework core |
| 27 | +- Runtime selection of configuration sets (including per-speed lists) |
| 28 | +- Support for vendor/device extensions without forking core device logic |
| 29 | + |
| 30 | +## Core concepts |
| 31 | + |
| 32 | +### 1) Function objects |
| 33 | + |
| 34 | +A `usb::df::function` is the base class for USB functions. Subclasses implement class |
| 35 | +behavior and descriptor contribution. |
| 36 | + |
| 37 | +Responsibilities of a function: |
| 38 | + |
| 39 | +- Contribute to configuration descriptor via `describe_config(...)` |
| 40 | +- Handle control requests routed to its interfaces/endpoints |
| 41 | +- Manage its assigned endpoints while it is active |
| 42 | +- Optionally own string descriptor indices |
| 43 | + |
| 44 | +Examples in-tree: |
| 45 | + |
| 46 | +- `usb::df::hid::function` |
| 47 | +- `usb::df::cdc::acm::function` |
| 48 | +- `usb::df::microsoft::xfunction` |
| 49 | + |
| 50 | +### 2) Configuration model |
| 51 | + |
| 52 | +The `usb::df::config` configuration model is built around fixed-size elements: |
| 53 | + |
| 54 | +- `power`: bus/self/shared power + remote wakeup + max current |
| 55 | +- `header`: configuration metadata (`power` + optional name + computed size) |
| 56 | +- `interface`: binds one interface entry to a function object |
| 57 | +- `endpoint`: endpoint descriptor plus internal flags |
| 58 | + |
| 59 | +The tests in `test/usb/df/config.test.cpp` demonstrate intended behavior, |
| 60 | +including reverse iteration, endpoint lookup, interface endpoint views, |
| 61 | +active-vs-unused endpoint filtering, and list handling. |
| 62 | + |
| 63 | +### 3) Device controller |
| 64 | + |
| 65 | +`usb::df::device` handles standard device-level control flow: |
| 66 | + |
| 67 | +- standard requests (`GET_DESCRIPTOR`, `SET_CONFIGURATION`, etc.) |
| 68 | +- interface and endpoint recipient request routing |
| 69 | +- active configuration transitions |
| 70 | +- string descriptor ownership and dispatch |
| 71 | +- BOS descriptor assembly |
| 72 | +- power/state event signaling to application |
| 73 | + |
| 74 | +`usb::df::device_instance<SPEEDS, MAX_CONFIG_LIST_SIZE>` stores and serves |
| 75 | +configuration lists per speed and provides convenience APIs for single-config devices. |
| 76 | + |
| 77 | +### 4) MAC abstraction |
| 78 | + |
| 79 | +`usb::df::mac` is the hardware/driver abstraction used by `device` and `function` objects. |
| 80 | +It provides: |
| 81 | + |
| 82 | +- bus attach/detach and reset integration |
| 83 | +- control transfer staging |
| 84 | +- endpoint open/close/send/receive/stall operations |
| 85 | +- active endpoint mapping helpers |
| 86 | + |
| 87 | +Platform ports implement concrete behavior (for example Zephyr UDC and NXP MCUX). |
| 88 | + |
| 89 | +### 5) Extension mechanism |
| 90 | + |
| 91 | +`usb::df::device::extension` extensions can hook device behavior without modifying core classes: |
| 92 | + |
| 93 | +- bus reset reaction |
| 94 | +- extra string ownership |
| 95 | +- descriptor/control request augmentation |
| 96 | +- speed-specific config override |
| 97 | +- BOS capability contribution |
| 98 | + |
| 99 | +Microsoft OS 2.0 support is implemented through this extension model. |
| 100 | + |
| 101 | +## Architecture at a glance |
| 102 | + |
| 103 | +Typical flow: |
| 104 | + |
| 105 | +1. Application creates function objects |
| 106 | +2. Application builds one or more `config::view` definitions |
| 107 | +3. Application registers configs in `device_instance` |
| 108 | +4. Application opens device (`device.open()`) |
| 109 | +5. Host enumerates; `device` builds descriptors by asking each function |
| 110 | +6. Host selects configuration; functions are initialized and endpoints opened |
| 111 | +7. Class/data traffic is routed between `device`, `function`, and `mac` |
| 112 | + |
| 113 | +Layering: |
| 114 | + |
| 115 | +- Application: owns function instances and selected configuration sets |
| 116 | +- `usb::df` core: descriptor generation, control routing, lifecycle orchestration |
| 117 | +- Port MAC: interacts with USB controller driver/RTOS |
| 118 | + |
| 119 | +## Practical extension points |
| 120 | + |
| 121 | +For new class/vendor development: |
| 122 | + |
| 123 | +1. Derive from `usb::df::function` or `usb::df::named_function` |
| 124 | +2. Implement `describe_config(...)` |
| 125 | +3. Implement control request handlers as needed |
| 126 | +4. Implement enable/disable and transfer callbacks |
| 127 | +5. Provide a `config(...)` helper to simplify integration |
| 128 | + |
| 129 | +For device-wide customization: |
| 130 | + |
| 131 | +1. Derive from `usb::df::device::extension` |
| 132 | +2. Override only the hooks needed |
| 133 | +3. Inject extension instance into `device_instance` constructor |
0 commit comments