|
| 1 | +// Copyright lowRISC contributors (OpenTitan project). |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +use anyhow::Result; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use std::rc::Rc; |
| 8 | +use std::time::Duration; |
| 9 | +use thiserror::Error; |
| 10 | + |
| 11 | +use crate::impl_serializable_error; |
| 12 | + |
| 13 | +/// Errors related to the GPIO interface. |
| 14 | +#[derive(Debug, Error, Serialize, Deserialize)] |
| 15 | +pub enum UsbError { |
| 16 | + #[error("Generic error: {0}")] |
| 17 | + Generic(String), |
| 18 | +} |
| 19 | +impl_serializable_error!(UsbError); |
| 20 | + |
| 21 | +/// A trait which represents a USB device. |
| 22 | +/// |
| 23 | +/// Note: some of the methods use `rusb`'s datatypes to avoid redefining |
| 24 | +/// all USB structure but otherwise does not require rusb to be implemented. |
| 25 | +pub trait UsbDevice { |
| 26 | + /// Return the VID of the device. |
| 27 | + fn get_vendor_id(&self) -> u16; |
| 28 | + |
| 29 | + /// Return the PID of the device. |
| 30 | + fn get_product_id(&self) -> u16; |
| 31 | + |
| 32 | + /// Gets the serial number of the device. |
| 33 | + fn get_serial_number(&self) -> &str; |
| 34 | + |
| 35 | + /// Set the active configuration. |
| 36 | + fn set_active_configuration(&self, config: u8) -> Result<()>; |
| 37 | + |
| 38 | + /// Claim an interface for use with the kernel. |
| 39 | + fn claim_interface(&self, iface: u8) -> Result<()>; |
| 40 | + |
| 41 | + /// Release a previously claimed interface to the kernel. |
| 42 | + fn release_interface(&self, iface: u8) -> Result<()>; |
| 43 | + |
| 44 | + /// Set an interface alternate setting. |
| 45 | + fn set_alternate_setting(&self, iface: u8, setting: u8) -> Result<()>; |
| 46 | + |
| 47 | + /// Check whether a kernel driver currentl controls the device. |
| 48 | + fn kernel_driver_active(&self, iface: u8) -> Result<bool>; |
| 49 | + |
| 50 | + /// Detach the kernel driver from the device. |
| 51 | + fn detach_kernel_driver(&self, iface: u8) -> Result<()>; |
| 52 | + |
| 53 | + /// Attach the kernel driver to the device. |
| 54 | + fn attach_kernel_driver(&self, iface: u8) -> Result<()>; |
| 55 | + |
| 56 | + /// Return the currently active configuration's descriptor. |
| 57 | + fn active_config_descriptor(&self) -> Result<rusb::ConfigDescriptor>; |
| 58 | + |
| 59 | + /// Return the device's bus number. |
| 60 | + fn bus_number(&self) -> u8; |
| 61 | + |
| 62 | + /// Return the sequence of port numbers from the root down to the device. |
| 63 | + fn port_numbers(&self) -> Result<Vec<u8>>; |
| 64 | + |
| 65 | + /// Return a string descriptor in ASCII. |
| 66 | + fn read_string_descriptor_ascii(&self, idx: u8) -> Result<String>; |
| 67 | + |
| 68 | + /// Reset the device. |
| 69 | + /// |
| 70 | + /// Note that this UsbDevice handle will most likely become invalid |
| 71 | + /// after resetting the device and a new one has to be obtained. |
| 72 | + fn reset(&self) -> Result<()>; |
| 73 | + |
| 74 | + /// Get the default timeout for operations. |
| 75 | + fn get_timeout(&self) -> Duration; |
| 76 | + |
| 77 | + /// Issue a USB control request with optional host-to-device data. |
| 78 | + fn write_control_timeout( |
| 79 | + &self, |
| 80 | + request_type: u8, |
| 81 | + request: u8, |
| 82 | + value: u16, |
| 83 | + index: u16, |
| 84 | + buf: &[u8], |
| 85 | + timeout: Duration, |
| 86 | + ) -> Result<usize>; |
| 87 | + |
| 88 | + /// Issue a USB control request with optional host-to-device data. |
| 89 | + /// |
| 90 | + /// This function uses the default timeout set up by the context. |
| 91 | + fn write_control( |
| 92 | + &self, |
| 93 | + request_type: u8, |
| 94 | + request: u8, |
| 95 | + value: u16, |
| 96 | + index: u16, |
| 97 | + buf: &[u8], |
| 98 | + ) -> Result<usize> { |
| 99 | + self.write_control_timeout(request_type, request, value, index, buf, self.get_timeout()) |
| 100 | + } |
| 101 | + |
| 102 | + /// Issue a USB control request with optional device-to-host data. |
| 103 | + fn read_control_timeout( |
| 104 | + &self, |
| 105 | + request_type: u8, |
| 106 | + request: u8, |
| 107 | + value: u16, |
| 108 | + index: u16, |
| 109 | + buf: &mut [u8], |
| 110 | + timeout: Duration, |
| 111 | + ) -> Result<usize>; |
| 112 | + |
| 113 | + /// Issue a USB control request with optional device-to-host data. |
| 114 | + /// |
| 115 | + /// This function uses the default timeout set up by the context. |
| 116 | + fn read_control( |
| 117 | + &self, |
| 118 | + request_type: u8, |
| 119 | + request: u8, |
| 120 | + value: u16, |
| 121 | + index: u16, |
| 122 | + buf: &mut [u8], |
| 123 | + ) -> Result<usize> { |
| 124 | + self.read_control_timeout(request_type, request, value, index, buf, self.get_timeout()) |
| 125 | + } |
| 126 | + |
| 127 | + /// Read bulk data bytes to given USB endpoint. |
| 128 | + fn read_bulk_timeout(&self, endpoint: u8, data: &mut [u8], timeout: Duration) -> Result<usize>; |
| 129 | + |
| 130 | + /// Read bulk data bytes to given USB endpoint. |
| 131 | + /// |
| 132 | + /// This function uses the default timeout set up by the context. |
| 133 | + fn read_bulk(&self, endpoint: u8, data: &mut [u8]) -> Result<usize> { |
| 134 | + self.read_bulk_timeout(endpoint, data, self.get_timeout()) |
| 135 | + } |
| 136 | + |
| 137 | + /// Write bulk data bytes to given USB endpoint. |
| 138 | + fn write_bulk_timeout(&self, endpoint: u8, data: &[u8], timeout: Duration) -> Result<usize>; |
| 139 | + |
| 140 | + /// Write bulk data bytes to given USB endpoint. |
| 141 | + /// |
| 142 | + /// This function uses the default timeout set up by the context. |
| 143 | + fn write_bulk(&self, endpoint: u8, data: &[u8]) -> Result<usize> { |
| 144 | + self.write_bulk_timeout(endpoint, data, self.get_timeout()) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/// A trait which represents a USB context. |
| 149 | +pub trait UsbContext { |
| 150 | + /// Find a device by VID:PID, and optionally disambiguate by serial number. |
| 151 | + /// |
| 152 | + /// If no device matches, this function returns immediately and does not wait. |
| 153 | + fn device_by_id( |
| 154 | + &self, |
| 155 | + usb_vid: u16, |
| 156 | + usb_pid: u16, |
| 157 | + usb_serial: Option<&str>, |
| 158 | + ) -> Result<Rc<dyn UsbDevice>> { |
| 159 | + self.device_by_id_with_timeout(usb_vid, usb_pid, usb_serial, Duration::ZERO) |
| 160 | + } |
| 161 | + |
| 162 | + /// Find a device by VID:PID, and optionally disambiguate by serial number. |
| 163 | + fn device_by_id_with_timeout( |
| 164 | + &self, |
| 165 | + usb_vid: u16, |
| 166 | + usb_pid: u16, |
| 167 | + usb_serial: Option<&str>, |
| 168 | + timeout: Duration, |
| 169 | + ) -> Result<Rc<dyn UsbDevice>>; |
| 170 | + |
| 171 | + /// Find a device with a specific interface, and optionally disambiguate by serial number. |
| 172 | + /// |
| 173 | + /// If no device matches, this function returns immediately and does not wait. |
| 174 | + fn device_by_interface( |
| 175 | + &self, |
| 176 | + class: u8, |
| 177 | + subclass: u8, |
| 178 | + protocol: u8, |
| 179 | + usb_serial: Option<&str>, |
| 180 | + ) -> Result<Rc<dyn UsbDevice>> { |
| 181 | + self.device_by_interface_with_timeout(class, subclass, protocol, usb_serial, Duration::ZERO) |
| 182 | + } |
| 183 | + |
| 184 | + fn device_by_interface_with_timeout( |
| 185 | + &self, |
| 186 | + class: u8, |
| 187 | + subclass: u8, |
| 188 | + protocol: u8, |
| 189 | + usb_serial: Option<&str>, |
| 190 | + timeout: Duration, |
| 191 | + ) -> Result<Rc<dyn UsbDevice>>; |
| 192 | +} |
0 commit comments