1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
45//! Interface to USB controller hardware
67use crate::utilities::cells::VolatileCell;
89/// USB controller interface
10pub trait UsbController<'a> {
11fn set_client(&self, client: &'a dyn Client<'a>);
1213// Should be called before `enable_as_device()`
14fn endpoint_set_ctrl_buffer(&self, buf: &'a [VolatileCell<u8>]);
15fn endpoint_set_in_buffer(&self, endpoint: usize, buf: &'a [VolatileCell<u8>]);
16fn endpoint_set_out_buffer(&self, endpoint: usize, buf: &'a [VolatileCell<u8>]);
1718// Must be called before `attach()`
19fn enable_as_device(&self, speed: DeviceSpeed);
2021fn attach(&self);
2223fn detach(&self);
2425fn set_address(&self, addr: u16);
2627fn enable_address(&self);
2829fn endpoint_in_enable(&self, transfer_type: TransferType, endpoint: usize);
3031fn endpoint_out_enable(&self, transfer_type: TransferType, endpoint: usize);
3233fn endpoint_in_out_enable(&self, transfer_type: TransferType, endpoint: usize);
3435fn endpoint_resume_in(&self, endpoint: usize);
3637fn endpoint_resume_out(&self, endpoint: usize);
38}
3940#[derive(Clone, Copy, Debug)]
41pub enum TransferType {
42 Control = 0,
43 Isochronous,
44 Bulk,
45 Interrupt,
46}
4748#[derive(Clone, Copy, Debug)]
49pub enum DeviceSpeed {
50 Full,
51 Low,
52}
5354/// USB controller client interface
55pub trait Client<'a> {
56fn enable(&'a self);
57fn attach(&'a self);
58fn bus_reset(&'a self);
5960fn ctrl_setup(&'a self, endpoint: usize) -> CtrlSetupResult;
61fn ctrl_in(&'a self, endpoint: usize) -> CtrlInResult;
62fn ctrl_out(&'a self, endpoint: usize, packet_bytes: u32) -> CtrlOutResult;
63fn ctrl_status(&'a self, endpoint: usize);
64fn ctrl_status_complete(&'a self, endpoint: usize);
6566fn packet_in(&'a self, transfer_type: TransferType, endpoint: usize) -> InResult;
67fn packet_out(
68&'a self,
69 transfer_type: TransferType,
70 endpoint: usize,
71 packet_bytes: u32,
72 ) -> OutResult;
7374fn packet_transmitted(&'a self, endpoint: usize);
75}
7677#[derive(Debug)]
78pub enum CtrlSetupResult {
79/// The Setup request was handled successfully
80Ok,
81 OkSetAddress,
8283// The Setup request cannot be handled; abort this transfer with STALL
84ErrBadLength,
85 ErrNoParse,
86 ErrNonstandardRequest,
87 ErrUnrecognizedDescriptorType,
88 ErrUnrecognizedRequestType,
89 ErrNoDeviceQualifier,
90 ErrInvalidDeviceIndex,
91 ErrInvalidConfigurationIndex,
92 ErrInvalidInterfaceIndex,
93 ErrInvalidStringIndex,
9495 ErrGeneric,
96}
9798pub enum CtrlInResult {
99/// A packet of the given size was written into the endpoint buffer
100Packet(usize, bool),
101102/// The client is not yet able to provide data to the host, but may
103 /// be able to in the future. This result causes the controller
104 /// to send a NAK token to the host.
105Delay,
106107/// The client does not support the request. This result causes the
108 /// controller to send a STALL token to the host.
109Error,
110}
111112pub enum CtrlOutResult {
113/// Data received (send ACK)
114Ok,
115116/// Not ready yet (send NAK)
117Delay,
118119/// In halt state (send STALL)
120Halted,
121}
122123/// Result for IN packets sent on bulk or interrupt endpoints.
124#[derive(Debug)]
125pub enum InResult {
126/// A packet of the given size was written into the endpoint buffer
127Packet(usize),
128129/// The client is not yet able to provide data to the host, but may
130 /// be able to in the future. This result causes the controller
131 /// to send a NAK token to the host.
132Delay,
133134/// The client does not support the request. This result causes the
135 /// controller to send a STALL token to the host.
136Error,
137}
138139/// Result for OUT packets sent on bulk or interrupt endpoints.
140#[derive(Debug)]
141pub enum OutResult {
142/// The OUT packet was consumed
143Ok,
144145/// The client is not yet able to consume data from the host, but may
146 /// be able to in the future. This result causes the controller
147 /// to send a NAK token to the host.
148Delay,
149150/// The client does not support the request. This result causes the
151 /// controller to send a STALL token to the host.
152Error,
153}