Skip to main content

viva_u3v/
descriptor.rs

1//! USB descriptor parsing for USB3 Vision devices.
2//!
3//! USB3 Vision devices are identified by their interfaces' class, subclass
4//! and protocol. This module extracts endpoint addresses and interface
5//! numbers from USB descriptors.
6
7#[cfg(any(feature = "usb", test))]
8/// USB3 Vision interface class code: Miscellaneous.
9pub(crate) const U3V_INTERFACE_CLASS: u8 = 0xEF;
10#[cfg(any(feature = "usb", test))]
11/// USB3 Vision interface subclass, the same on all three of its interfaces.
12pub(crate) const U3V_INTERFACE_SUBCLASS: u8 = 0x05;
13
14#[cfg(any(feature = "usb", test))]
15/// Interface protocol of the control interface.
16pub(crate) const U3V_CONTROL_PROTOCOL: u8 = 0x00;
17#[cfg(any(feature = "usb", test))]
18/// Interface protocol of the event interface.
19pub(crate) const U3V_EVENT_PROTOCOL: u8 = 0x01;
20#[cfg(any(feature = "usb", test))]
21/// Interface protocol of the streaming interface.
22pub(crate) const U3V_STREAM_PROTOCOL: u8 = 0x02;
23
24/// What a USB3 Vision interface carries.
25#[cfg(any(feature = "usb", test))]
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub(crate) enum U3vInterfaceKind {
28    /// The control interface: GenCP commands and acknowledges.
29    Control,
30    /// The event interface.
31    Event,
32    /// The streaming interface.
33    Stream,
34}
35
36#[cfg(any(feature = "usb", test))]
37/// Which USB3 Vision interface, if any, an interface descriptor describes.
38///
39/// All three are class `0xEF`, subclass `0x05`; the protocol tells them apart
40/// (aravis `ARV_UV_INTERFACE_*_PROTOCOL`, Wireshark `IF_SUBCLASS_MISC_U3V`).
41/// Until backlog TC-27 discovery matched the *subclass* against 0, 1 and 2,
42/// so it found no interface on a conformant camera.
43pub(crate) fn classify_interface(
44    class: u8,
45    subclass: u8,
46    protocol: u8,
47) -> Option<U3vInterfaceKind> {
48    if class != U3V_INTERFACE_CLASS || subclass != U3V_INTERFACE_SUBCLASS {
49        return None;
50    }
51    match protocol {
52        U3V_CONTROL_PROTOCOL => Some(U3vInterfaceKind::Control),
53        U3V_EVENT_PROTOCOL => Some(U3vInterfaceKind::Event),
54        U3V_STREAM_PROTOCOL => Some(U3vInterfaceKind::Stream),
55        _ => None,
56    }
57}
58
59/// Information about a USB3 Vision device's interfaces and endpoints,
60/// extracted from USB descriptors.
61#[derive(Debug, Clone)]
62pub struct U3vInterfaceInfo {
63    /// Interface number for the control channel.
64    pub control_iface: u8,
65    /// Bulk IN endpoint for control acks.
66    pub control_ep_in: u8,
67    /// Bulk OUT endpoint for control commands.
68    pub control_ep_out: u8,
69    /// Interface number for the streaming channel (if present).
70    pub stream_iface: Option<u8>,
71    /// Bulk IN endpoint for stream data (if present).
72    pub stream_ep_in: Option<u8>,
73    /// Interface number for the event channel (if present).
74    pub event_iface: Option<u8>,
75    /// Bulk IN endpoint for events (if present).
76    pub event_ep_in: Option<u8>,
77}
78
79/// The USB3 Vision interfaces and endpoints in a device's active
80/// configuration, and each bulk endpoint's maximum packet size; `None` if
81/// it has no USB3 Vision control interface with both bulk endpoints.
82#[cfg(feature = "usb")]
83pub(crate) fn parse_u3v_interfaces(
84    config: &nusb::descriptors::ConfigurationDescriptor<'_>,
85) -> Option<(U3vInterfaceInfo, Vec<(u8, usize)>)> {
86    use nusb::descriptors::TransferType;
87    use nusb::transfer::Direction;
88
89    let mut control_iface = None;
90    let mut control_ep_in = None;
91    let mut control_ep_out = None;
92    let mut stream_iface = None;
93    let mut stream_ep_in = None;
94    let mut event_iface = None;
95    let mut event_ep_in = None;
96    let mut packet_sizes = Vec::new();
97
98    for desc in config.interface_alt_settings() {
99        let Some(kind) = classify_interface(desc.class(), desc.subclass(), desc.protocol()) else {
100            continue;
101        };
102        let number = desc.interface_number();
103        for ep in desc
104            .endpoints()
105            .filter(|ep| ep.transfer_type() == TransferType::Bulk)
106        {
107            packet_sizes.push((ep.address(), ep.max_packet_size()));
108            match (kind, ep.direction()) {
109                (U3vInterfaceKind::Control, Direction::In) => control_ep_in = Some(ep.address()),
110                (U3vInterfaceKind::Control, Direction::Out) => control_ep_out = Some(ep.address()),
111                (U3vInterfaceKind::Event, Direction::In) => event_ep_in = Some(ep.address()),
112                (U3vInterfaceKind::Stream, Direction::In) => stream_ep_in = Some(ep.address()),
113                _ => {}
114            }
115        }
116        match kind {
117            U3vInterfaceKind::Control => control_iface = Some(number),
118            U3vInterfaceKind::Event => event_iface = Some(number),
119            U3vInterfaceKind::Stream => stream_iface = Some(number),
120        }
121    }
122
123    Some((
124        U3vInterfaceInfo {
125            control_iface: control_iface?,
126            control_ep_in: control_ep_in?,
127            control_ep_out: control_ep_out?,
128            stream_iface,
129            stream_ep_in,
130            event_iface,
131            event_ep_in,
132        },
133        packet_sizes,
134    ))
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140
141    #[test]
142    fn interfaces_are_told_apart_by_protocol() {
143        assert_eq!(
144            classify_interface(0xEF, 0x05, 0x00),
145            Some(U3vInterfaceKind::Control)
146        );
147        assert_eq!(
148            classify_interface(0xEF, 0x05, 0x01),
149            Some(U3vInterfaceKind::Event)
150        );
151        assert_eq!(
152            classify_interface(0xEF, 0x05, 0x02),
153            Some(U3vInterfaceKind::Stream)
154        );
155        // What discovery used to look for: subclass 0, 1, 2.
156        assert_eq!(classify_interface(0xEF, 0x00, 0x00), None);
157        assert_eq!(classify_interface(0xEF, 0x02, 0x00), None);
158        assert_eq!(classify_interface(0xFF, 0x05, 0x00), None);
159    }
160}