-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathio.rs
296 lines (261 loc) · 9.46 KB
/
io.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: MIT OR Apache-2.0
//! USB I/O protocol.
use core::ffi;
use uefi_macros::unsafe_protocol;
use uefi_raw::protocol::usb::io::UsbIoProtocol;
use uefi_raw::protocol::usb::{
ConfigDescriptor, DataDirection, DeviceDescriptor, DeviceRequest, EndpointDescriptor,
InterfaceDescriptor, UsbTransferStatus,
};
use crate::data_types::PoolString;
use crate::{Char16, Result, StatusExt};
/// USB I/O protocol.
#[derive(Debug)]
#[repr(transparent)]
#[unsafe_protocol(UsbIoProtocol::GUID)]
pub struct UsbIo(UsbIoProtocol);
impl UsbIo {
/// Performs a USB Control transfer, allowing the driver to communicate with the USB device.
pub fn control_transfer(
&mut self,
request_type: u8,
request: u8,
value: u16,
index: u16,
transfer: ControlTransfer,
timeout: u32,
) -> Result<(), UsbTransferStatus> {
let (direction, buffer_ptr, length) = match transfer {
ControlTransfer::None => (DataDirection::NO_DATA, core::ptr::null_mut(), 0),
ControlTransfer::DataIn(buffer) => (
DataDirection::DATA_IN,
buffer.as_ptr().cast_mut(),
buffer.len(),
),
ControlTransfer::DataOut(buffer) => (
DataDirection::DATA_OUT,
buffer.as_ptr().cast_mut(),
buffer.len(),
),
};
let request_type = if direction == DataDirection::DATA_IN {
request_type | 0x80
} else if direction == DataDirection::DATA_OUT {
request_type & !0x80
} else {
request_type
};
let mut device_request = DeviceRequest {
request_type,
request,
value,
index,
length: length as u16,
};
let mut status = UsbTransferStatus::default();
unsafe {
(self.0.control_transfer)(
&mut self.0,
&mut device_request,
direction,
timeout,
buffer_ptr.cast::<ffi::c_void>(),
length,
&mut status,
)
}
.to_result_with_err(|_| status)
}
/// Sends the provided buffer to a USB device over a bulk transfer pipe.
///
/// Returns the number of bytes that were actually sent to the device.
pub fn sync_bulk_send(
&mut self,
endpoint: u8,
buffer: &[u8],
timeout: usize,
) -> Result<usize, UsbTransferStatus> {
let mut status = UsbTransferStatus::default();
let mut length = buffer.len();
unsafe {
(self.0.bulk_transfer)(
&mut self.0,
endpoint & !0x80,
buffer.as_ptr().cast_mut().cast::<ffi::c_void>(),
&mut length,
timeout,
&mut status,
)
}
.to_result_with_err(|_| status)
.map(|()| length)
}
/// Fills the provided buffer with data from a USB device over a bulk transfer pipe.
///
/// Returns the number of bytes that were actually received from the device.
pub fn sync_bulk_receive(
&mut self,
endpoint: u8,
buffer: &mut [u8],
timeout: usize,
) -> Result<usize, UsbTransferStatus> {
let mut status = UsbTransferStatus::default();
let mut length = buffer.len();
unsafe {
(self.0.bulk_transfer)(
&mut self.0,
endpoint | 0x80,
buffer.as_ptr().cast_mut().cast::<ffi::c_void>(),
&mut length,
timeout,
&mut status,
)
}
.to_result_with_err(|_| status)
.map(|()| length)
}
/// Sends the provided buffer to a USB device through a synchronous interrupt transfer.
pub fn sync_interrupt_send(
&mut self,
endpoint: u8,
buffer: &[u8],
timeout: usize,
) -> Result<usize, UsbTransferStatus> {
let mut status = UsbTransferStatus::default();
let mut length = buffer.len();
unsafe {
(self.0.sync_interrupt_transfer)(
&mut self.0,
endpoint & !0x80,
buffer.as_ptr().cast_mut().cast::<ffi::c_void>(),
&mut length,
timeout,
&mut status,
)
}
.to_result_with_err(|_| status)
.map(|()| length)
}
/// Fills the provided buffer with data from a USB device through a synchronous interrupt
/// transfer.
pub fn sync_interrupt_receive(
&mut self,
endpoint: u8,
buffer: &mut [u8],
timeout: usize,
) -> Result<usize, UsbTransferStatus> {
let mut status = UsbTransferStatus::default();
let mut length = buffer.len();
unsafe {
(self.0.sync_interrupt_transfer)(
&mut self.0,
endpoint | 0x80,
buffer.as_ptr().cast_mut().cast::<ffi::c_void>(),
&mut length,
timeout,
&mut status,
)
}
.to_result_with_err(|_| status)
.map(|()| length)
}
/// Sends the provided buffer to a USB device over an isochronous transfer pipe.
pub fn sync_isochronous_send(
&mut self,
endpoint: u8,
buffer: &[u8],
) -> Result<(), UsbTransferStatus> {
let mut status = UsbTransferStatus::default();
unsafe {
(self.0.isochronous_transfer)(
&mut self.0,
endpoint & !0x80,
buffer.as_ptr().cast_mut().cast::<ffi::c_void>(),
buffer.len(),
&mut status,
)
}
.to_result_with_err(|_| status)
}
/// Fills the provided buffer with data from a USB device over an isochronous transfer pipe.
pub fn sync_isochronous_receive(
&mut self,
endpoint: u8,
buffer: &mut [u8],
) -> Result<(), UsbTransferStatus> {
let mut status = UsbTransferStatus::default();
unsafe {
(self.0.isochronous_transfer)(
&mut self.0,
endpoint | 0x80,
buffer.as_mut_ptr().cast::<ffi::c_void>(),
buffer.len(),
&mut status,
)
}
.to_result_with_err(|_| status)
}
/// Returns information about USB devices, including the device's class, subclass, and number
/// of configurations.
pub fn device_descriptor(&mut self) -> Result<DeviceDescriptor> {
let mut device_descriptor = unsafe { core::mem::zeroed() };
unsafe { (self.0.get_device_descriptor)(&mut self.0, &mut device_descriptor) }
.to_result_with_val(|| device_descriptor)
}
/// Returns information about the active configuration of the USB device.
pub fn config_descriptor(&mut self) -> Result<ConfigDescriptor> {
let mut config_descriptor = unsafe { core::mem::zeroed() };
unsafe { (self.0.get_config_descriptor)(&mut self.0, &mut config_descriptor) }
.to_result_with_val(|| config_descriptor)
}
/// Returns information about the interface of the USB device.
pub fn interface_descriptor(&mut self) -> Result<InterfaceDescriptor> {
let mut interface_descriptor = unsafe { core::mem::zeroed() };
unsafe { (self.0.get_interface_descriptor)(&mut self.0, &mut interface_descriptor) }
.to_result_with_val(|| interface_descriptor)
}
/// Returns information about the interface of the USB device.
pub fn endpoint_descriptor(&mut self, endpoint: u8) -> Result<EndpointDescriptor> {
let mut endpoint_descriptor = unsafe { core::mem::zeroed() };
unsafe { (self.0.get_endpoint_descriptor)(&mut self.0, endpoint, &mut endpoint_descriptor) }
.to_result_with_val(|| endpoint_descriptor)
}
/// Returns the string associated with `string_id` in the language associated with `lang_id`.
pub fn string_descriptor(&mut self, lang_id: u16, string_id: u8) -> Result<PoolString> {
let mut string_ptr = core::ptr::null_mut();
unsafe { (self.0.get_string_descriptor)(&mut self.0, lang_id, string_id, &mut string_ptr) }
.to_result()?;
unsafe { PoolString::new(string_ptr.cast::<Char16>()) }
}
/// Returns all of the language ID codes that the USB device supports.
pub fn supported_languages(&mut self) -> Result<&[u16]> {
let mut lang_id_table_ptr = core::ptr::null_mut();
let mut lang_id_table_size = 0;
unsafe {
(self.0.get_supported_languages)(
&mut self.0,
&mut lang_id_table_ptr,
&mut lang_id_table_size,
)
}
.to_result_with_val(|| unsafe {
core::slice::from_raw_parts(lang_id_table_ptr, usize::from(lang_id_table_size))
})
}
/// Resets and reconfigures the USB controller.
///
/// This function should work for all USB devices except USB Hub Controllers.
pub fn port_reset(&mut self) -> Result {
unsafe { (self.0.port_reset)(&mut self.0) }.to_result()
}
}
/// Controls what type of USB control transfer operation should occur.
#[derive(Debug)]
pub enum ControlTransfer<'buffer> {
/// The USB control transfer has no data phase.
None,
/// The USB control transfer has an input data phase.
DataIn(&'buffer mut [u8]),
/// The USB control transfer has an output data phase.
DataOut(&'buffer [u8]),
}