Skip to main content

usb_stack/host/
pipe.rs

1//! Bulk and interrupt pipes.
2//!
3//! A pipe owns one host channel and the endpoint's data toggle. Reads and
4//! writes are one transfer each: they are split into packets of the endpoint's
5//! maximum packet size and stop at the first short packet, which is what the
6//! USB specification calls a transfer.
7
8use core::cell::Cell;
9
10use crate::endpoint::{EndpointAddress, EndpointType};
11use crate::host::controller::{Channel, EndpointConfig, HostController, Toggle};
12use crate::host::error::HostResult;
13
14/// An endpoint of an enumerated device.
15pub struct Pipe<'d, C: HostController> {
16    controller: &'d C,
17    channel: Channel,
18    endpoint: EndpointConfig,
19    /// Toggle the next packet is sent with.
20    toggle: Cell<Toggle>,
21}
22
23impl<'d, C: HostController> Pipe<'d, C> {
24    pub(crate) fn new(controller: &'d C, channel: Channel, endpoint: EndpointConfig) -> Self {
25        Self {
26            controller,
27            channel,
28            endpoint,
29            toggle: Cell::new(Toggle::Data0),
30        }
31    }
32
33    /// Endpoint number and direction.
34    #[inline]
35    pub fn address(&self) -> EndpointAddress {
36        self.endpoint.address
37    }
38
39    /// Maximum packet size.
40    #[inline]
41    pub fn max_packet(&self) -> u16 {
42        self.endpoint.max_packet
43    }
44
45    /// Bulk or interrupt.
46    #[inline]
47    pub fn transfer_type(&self) -> EndpointType {
48        self.endpoint.transfer
49    }
50
51    /// Restart the data toggle at DATA0, as `CLEAR_FEATURE(ENDPOINT_HALT)`
52    /// requires.
53    pub(crate) fn reset_toggle(&self) {
54        self.toggle.set(Toggle::Data0);
55    }
56
57    /// Receive one transfer, stopping at a short packet or when `buffer` is
58    /// full.
59    pub fn read(&self, buffer: &mut [u8]) -> HostResult<usize> {
60        let max_packet = self.max_packet() as usize;
61        let mut done = 0;
62
63        while done < buffer.len() {
64            let chunk = (buffer.len() - done).min(max_packet);
65            let received = self.controller.transfer_in(
66                self.channel,
67                self.toggle.get(),
68                &mut buffer[done..done + chunk],
69            )?;
70            self.advance();
71            done += received;
72            if received < chunk {
73                break;
74            }
75        }
76        Ok(done)
77    }
78
79    /// Send one transfer.
80    ///
81    /// An empty `data` sends a single zero-length packet, which is how a bulk
82    /// transfer is terminated early.
83    pub fn write(&self, data: &[u8]) -> HostResult<usize> {
84        let max_packet = self.max_packet() as usize;
85        let mut done = 0;
86
87        while done < data.len() {
88            let chunk = (data.len() - done).min(max_packet);
89            self.controller.transfer_out(
90                self.channel,
91                self.toggle.get(),
92                &data[done..done + chunk],
93            )?;
94            self.advance();
95            done += chunk;
96        }
97        if data.is_empty() {
98            self.controller
99                .transfer_out(self.channel, self.toggle.get(), &[])?;
100            self.advance();
101        }
102        Ok(done)
103    }
104
105    fn advance(&self) {
106        self.toggle.set(match self.toggle.get() {
107            Toggle::Data0 => Toggle::Data1,
108            Toggle::Data1 => Toggle::Data0,
109        });
110    }
111}
112
113impl<C: HostController> Drop for Pipe<'_, C> {
114    fn drop(&mut self) {
115        self.controller.free_channel(self.channel);
116    }
117}