Skip to content

Commit 6447a39

Browse files
committed
uefi: Begin safe EFI_USB_IO_PROTOCOL bindings
Add the UsbIo wrapper and implement methods for the informative functions provided by the protocol.
1 parent 4248ee8 commit 6447a39

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

uefi/src/proto/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod shell_params;
3131
pub mod shim;
3232
pub mod string;
3333
pub mod tcg;
34+
pub mod usb;
3435

3536
mod boot_policy;
3637

uefi/src/proto/usb/io.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
3+
//! USB I/O protocol.
4+
5+
use uefi_macros::unsafe_protocol;
6+
use uefi_raw::protocol::usb::io::UsbIoProtocol;
7+
8+
use crate::data_types::PoolString;
9+
use crate::{Char16, Result, StatusExt};
10+
11+
pub use uefi_raw::protocol::usb::io::{
12+
ConfigDescriptor, DeviceDescriptor, EndpointDescriptor, InterfaceDescriptor,
13+
};
14+
15+
/// USB I/O protocol.
16+
#[derive(Debug)]
17+
#[repr(transparent)]
18+
#[unsafe_protocol(UsbIoProtocol::GUID)]
19+
pub struct UsbIo(UsbIoProtocol);
20+
21+
impl UsbIo {
22+
/// Returns information about USB devices, including the device's class, subclass, and number
23+
/// of configurations.
24+
pub fn device_descriptor(&mut self) -> Result<DeviceDescriptor> {
25+
let mut device_descriptor = unsafe { core::mem::zeroed() };
26+
27+
unsafe { (self.0.get_device_descriptor)(&mut self.0, &mut device_descriptor) }
28+
.to_result_with_val(|| device_descriptor)
29+
}
30+
31+
/// Returns information about the active configuration of the USB device.
32+
pub fn config_descriptor(&mut self) -> Result<ConfigDescriptor> {
33+
let mut config_descriptor = unsafe { core::mem::zeroed() };
34+
35+
unsafe { (self.0.get_config_descriptor)(&mut self.0, &mut config_descriptor) }
36+
.to_result_with_val(|| config_descriptor)
37+
}
38+
39+
/// Returns information about the interface of the USB device.
40+
pub fn interface_descriptor(&mut self) -> Result<InterfaceDescriptor> {
41+
let mut interface_descriptor = unsafe { core::mem::zeroed() };
42+
43+
unsafe { (self.0.get_interface_descriptor)(&mut self.0, &mut interface_descriptor) }
44+
.to_result_with_val(|| interface_descriptor)
45+
}
46+
47+
/// Returns information about the interface of the USB device.
48+
pub fn endpoint_descriptor(&mut self, endpoint: u8) -> Result<EndpointDescriptor> {
49+
let mut endpoint_descriptor = unsafe { core::mem::zeroed() };
50+
51+
unsafe { (self.0.get_endpoint_descriptor)(&mut self.0, endpoint, &mut endpoint_descriptor) }
52+
.to_result_with_val(|| endpoint_descriptor)
53+
}
54+
55+
/// Returns the string associated with `string_id` in the language associated with `lang_id`.
56+
pub fn string_descriptor(&mut self, lang_id: u16, string_id: u8) -> Result<PoolString> {
57+
let mut string_ptr = core::ptr::null_mut();
58+
59+
unsafe { (self.0.get_string_descriptor)(&mut self.0, lang_id, string_id, &mut string_ptr) }
60+
.to_result()?;
61+
unsafe { PoolString::new(string_ptr.cast::<Char16>()) }
62+
}
63+
64+
/// Returns all of the language ID codes that the USB device supports.
65+
pub fn supported_languages(&mut self) -> Result<&[u16]> {
66+
let mut lang_id_table_ptr = core::ptr::null_mut();
67+
let mut lang_id_table_size = 0;
68+
69+
unsafe {
70+
(self.0.get_supported_languages)(
71+
&mut self.0,
72+
&mut lang_id_table_ptr,
73+
&mut lang_id_table_size,
74+
)
75+
}
76+
.to_result_with_val(|| unsafe {
77+
core::slice::from_raw_parts(lang_id_table_ptr, usize::from(lang_id_table_size))
78+
})
79+
}
80+
81+
/// Resets and reconfigures the USB controller.
82+
///
83+
/// This function should work for all USB devices except USB Hub Controllers.
84+
pub fn port_reset(&mut self) -> Result {
85+
unsafe { (self.0.port_reset)(&mut self.0) }.to_result()
86+
}
87+
}

uefi/src/proto/usb/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
3+
//! USB I/O protocols.
4+
//!
5+
//! These protocols can be used to interact with and configure USB devices.
6+
7+
pub mod io;

0 commit comments

Comments
 (0)