| 1 |
use std::{ |
| 2 |
ffi::c_void, |
| 3 |
io, |
| 4 |
mem::{self, ManuallyDrop}, |
| 5 |
ptr::{addr_of_mut, null_mut}, |
| 6 |
sync::Arc, |
| 7 |
}; |
| 8 |
|
| 9 |
use log::{debug, error, warn}; |
| 10 |
use windows_sys::Win32::{ |
| 11 |
Devices::Usb::{ |
| 12 |
WinUsb_ControlTransfer, WinUsb_GetOverlappedResult, WinUsb_ReadPipe, WinUsb_WritePipe, |
| 13 |
WINUSB_SETUP_PACKET, |
| 14 |
}, |
| 15 |
Foundation::{ |
| 16 |
GetLastError, ERROR_DEVICE_NOT_CONNECTED, ERROR_FILE_NOT_FOUND, ERROR_GEN_FAILURE, |
| 17 |
ERROR_IO_PENDING, ERROR_NOT_FOUND, ERROR_NO_SUCH_DEVICE, ERROR_OPERATION_ABORTED, |
| 18 |
ERROR_REQUEST_ABORTED, ERROR_SEM_TIMEOUT, ERROR_TIMEOUT, FALSE, TRUE, WIN32_ERROR, |
| 19 |
}, |
| 20 |
System::IO::{CancelIoEx, OVERLAPPED}, |
| 21 |
}; |
| 22 |
|
| 23 |
use crate::transfer::{ |
| 24 |
notify_completion, Completion, ControlIn, ControlOut, EndpointType, PlatformSubmit, |
| 25 |
PlatformTransfer, Recipient, RequestBuffer, ResponseBuffer, TransferError, |
| 26 |
}; |
| 27 |
|
| 28 |
use super::util::raw_handle; |
| 29 |
|
| 30 |
#[repr(C)] |
| 31 |
pub(crate) struct EventNotify { |
| 32 |
|
| 33 |
overlapped: OVERLAPPED, |
| 34 |
ptr: *mut c_void, |
| 35 |
} |
| 36 |
|
| 37 |
pub struct TransferData { |
| 38 |
interface: Arc<super::Interface>, |
| 39 |
event: *mut EventNotify, |
| 40 |
buf: *mut u8, |
| 41 |
capacity: usize, |
| 42 |
endpoint: u8, |
| 43 |
ep_type: EndpointType, |
| 44 |
submit_error: Option<WIN32_ERROR>, |
| 45 |
} |
| 46 |
|
| 47 |
unsafe impl Send for TransferData {} |
| 48 |
|
| 49 |
impl TransferData { |
| 50 |
pub(crate) fn new( |
| 51 |
interface: std::sync::Arc<super::Interface>, |
| 52 |
endpoint: u8, |
| 53 |
ep_type: EndpointType, |
| 54 |
) -> TransferData { |
| 55 |
TransferData { |
| 56 |
interface, |
| 57 |
event: Box::into_raw(Box::new(unsafe { mem::zeroed() })), |
| 58 |
buf: null_mut(), |
| 59 |
capacity: 0, |
| 60 |
endpoint, |
| 61 |
ep_type, |
| 62 |
submit_error: None, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
unsafe fn take_buf(&mut self, length: usize) -> Vec<u8> { |
| 68 |
let v = Vec::from_raw_parts(self.buf, length, self.capacity); |
| 69 |
self.buf = null_mut(); |
| 70 |
self.capacity = 0; |
| 71 |
v |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
unsafe fn post_submit(&mut self, r: i32, func: &str, user_data: *mut c_void) { |
| 76 |
if r == TRUE { |
| 77 |
error!("{func} completed synchronously") |
| 78 |
} |
| 79 |
|
| 80 |
let err = GetLastError(); |
| 81 |
|
| 82 |
if err != ERROR_IO_PENDING { |
| 83 |
self.submit_error = Some(err); |
| 84 |
error!("{func} failed: {}", io::Error::from_raw_os_error(err as _)); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
notify_completion::<TransferData>(user_data); |
| 89 |
} else { |
| 90 |
self.submit_error = None; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
unsafe fn get_status(&mut self) -> (usize, Result<(), TransferError>) { |
| 96 |
if let Some(err) = self.submit_error { |
| 97 |
debug!( |
| 98 |
"Transfer {:?} on endpoint {:02x} failed on submit: {}", |
| 99 |
self.event, self.endpoint, err |
| 100 |
); |
| 101 |
return (0, Err(map_error(err))); |
| 102 |
} |
| 103 |
|
| 104 |
let mut actual_len = 0; |
| 105 |
let r = WinUsb_GetOverlappedResult( |
| 106 |
self.interface.winusb_handle, |
| 107 |
self.event as *mut OVERLAPPED, |
| 108 |
&mut actual_len, |
| 109 |
FALSE, |
| 110 |
); |
| 111 |
|
| 112 |
let status = if r != 0 { |
| 113 |
debug!( |
| 114 |
"Transfer {:?} on endpoint {:02x} complete: {} bytes transferred", |
| 115 |
self.event, self.endpoint, actual_len |
| 116 |
); |
| 117 |
Ok(()) |
| 118 |
} else { |
| 119 |
let err = GetLastError(); |
| 120 |
debug!( |
| 121 |
"Transfer {:?} on endpoint {:02x} failed: {}, {} bytes transferred", |
| 122 |
self.event, self.endpoint, err, actual_len |
| 123 |
); |
| 124 |
Err(map_error(err)) |
| 125 |
}; |
| 126 |
|
| 127 |
(actual_len as usize, status) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
impl Drop for TransferData { |
| 132 |
fn drop(&mut self) { |
| 133 |
if !self.buf.is_null() { |
| 134 |
unsafe { drop(Vec::from_raw_parts(self.buf, 0, self.capacity)) } |
| 135 |
} |
| 136 |
unsafe { drop(Box::from_raw(self.event)) } |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl PlatformTransfer for TransferData { |
| 141 |
fn cancel(&self) { |
| 142 |
debug!("Cancelling transfer {:?}", self.event); |
| 143 |
unsafe { |
| 144 |
let r = CancelIoEx( |
| 145 |
raw_handle(&self.interface.handle), |
| 146 |
self.event as *mut OVERLAPPED, |
| 147 |
); |
| 148 |
if r == 0 { |
| 149 |
let err = GetLastError(); |
| 150 |
if err != ERROR_NOT_FOUND { |
| 151 |
error!( |
| 152 |
"CancelIoEx failed: {}", |
| 153 |
io::Error::from_raw_os_error(err as i32) |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl PlatformSubmit<Vec<u8>> for TransferData { |
| 162 |
unsafe fn submit(&mut self, data: Vec<u8>, user_data: *mut c_void) { |
| 163 |
addr_of_mut!((*self.event).ptr).write(user_data); |
| 164 |
|
| 165 |
let mut data = ManuallyDrop::new(data); |
| 166 |
self.buf = data.as_mut_ptr(); |
| 167 |
self.capacity = data.capacity(); |
| 168 |
let len = data.len(); |
| 169 |
|
| 170 |
debug!( |
| 171 |
"Submit transfer {:?} on endpoint {:02X} for {} bytes OUT", |
| 172 |
self.event, self.endpoint, len |
| 173 |
); |
| 174 |
|
| 175 |
let r = WinUsb_WritePipe( |
| 176 |
self.interface.winusb_handle, |
| 177 |
self.endpoint, |
| 178 |
self.buf, |
| 179 |
len.try_into().expect("transfer size should fit in u32"), |
| 180 |
null_mut(), |
| 181 |
self.event as *mut OVERLAPPED, |
| 182 |
); |
| 183 |
self.post_submit(r, "WinUsb_WritePipe", user_data); |
| 184 |
} |
| 185 |
|
| 186 |
unsafe fn take_completed(&mut self) -> Completion<ResponseBuffer> { |
| 187 |
let (actual_len, status) = self.get_status(); |
| 188 |
let data = ResponseBuffer::from_vec(self.take_buf(0), actual_len); |
| 189 |
Completion { data, status } |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
impl PlatformSubmit<RequestBuffer> for TransferData { |
| 194 |
unsafe fn submit(&mut self, data: RequestBuffer, user_data: *mut c_void) { |
| 195 |
addr_of_mut!((*self.event).ptr).write(user_data); |
| 196 |
|
| 197 |
let (buf, request_len) = data.into_vec(); |
| 198 |
let mut buf = ManuallyDrop::new(buf); |
| 199 |
self.buf = buf.as_mut_ptr(); |
| 200 |
self.capacity = buf.capacity(); |
| 201 |
|
| 202 |
debug!( |
| 203 |
"Submit transfer {:?} on endpoint {:02X} for {} bytes IN", |
| 204 |
self.event, self.endpoint, request_len |
| 205 |
); |
| 206 |
|
| 207 |
let r = WinUsb_ReadPipe( |
| 208 |
self.interface.winusb_handle, |
| 209 |
self.endpoint, |
| 210 |
self.buf, |
| 211 |
request_len |
| 212 |
.try_into() |
| 213 |
.expect("transfer size should fit in u32"), |
| 214 |
null_mut(), |
| 215 |
self.event as *mut OVERLAPPED, |
| 216 |
); |
| 217 |
self.post_submit(r, "WinUsb_ReadPipe", user_data); |
| 218 |
} |
| 219 |
|
| 220 |
unsafe fn take_completed(&mut self) -> Completion<Vec<u8>> { |
| 221 |
let (actual_len, status) = self.get_status(); |
| 222 |
let data = self.take_buf(actual_len); |
| 223 |
Completion { data, status } |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
impl PlatformSubmit<ControlIn> for TransferData { |
| 228 |
unsafe fn submit(&mut self, data: ControlIn, user_data: *mut c_void) { |
| 229 |
assert_eq!(self.endpoint, 0); |
| 230 |
assert_eq!(self.ep_type, EndpointType::Control); |
| 231 |
|
| 232 |
if data.recipient == Recipient::Interface |
| 233 |
&& data.index as u8 != self.interface.interface_number |
| 234 |
{ |
| 235 |
warn!("WinUSB sends interface number instead of passed `index` when performing a control transfer with `Recipient::Interface`"); |
| 236 |
} |
| 237 |
|
| 238 |
addr_of_mut!((*self.event).ptr).write(user_data); |
| 239 |
|
| 240 |
let mut buf = ManuallyDrop::new(Vec::with_capacity(data.length as usize)); |
| 241 |
self.buf = buf.as_mut_ptr(); |
| 242 |
self.capacity = buf.capacity(); |
| 243 |
|
| 244 |
debug!( |
| 245 |
"Submit transfer {:?} on endpoint {:02X} for {} bytes ControlIN", |
| 246 |
self.event, self.endpoint, data.length |
| 247 |
); |
| 248 |
|
| 249 |
let pkt = WINUSB_SETUP_PACKET { |
| 250 |
RequestType: data.request_type(), |
| 251 |
Request: data.request, |
| 252 |
Value: data.value, |
| 253 |
Index: data.index, |
| 254 |
Length: data.length, |
| 255 |
}; |
| 256 |
|
| 257 |
let r = WinUsb_ControlTransfer( |
| 258 |
self.interface.winusb_handle, |
| 259 |
pkt, |
| 260 |
self.buf, |
| 261 |
data.length as u32, |
| 262 |
null_mut(), |
| 263 |
self.event as *mut OVERLAPPED, |
| 264 |
); |
| 265 |
|
| 266 |
self.post_submit(r, "WinUsb_ControlTransfer", user_data); |
| 267 |
} |
| 268 |
|
| 269 |
unsafe fn take_completed(&mut self) -> Completion<Vec<u8>> { |
| 270 |
let (actual_len, status) = self.get_status(); |
| 271 |
let data = self.take_buf(actual_len); |
| 272 |
Completion { data, status } |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
impl PlatformSubmit<ControlOut<'_>> for TransferData { |
| 277 |
unsafe fn submit(&mut self, data: ControlOut, user_data: *mut c_void) { |
| 278 |
assert_eq!(self.endpoint, 0); |
| 279 |
assert_eq!(self.ep_type, EndpointType::Control); |
| 280 |
|
| 281 |
if data.recipient == Recipient::Interface |
| 282 |
&& data.index as u8 != self.interface.interface_number |
| 283 |
{ |
| 284 |
warn!("WinUSB sends interface number instead of passed `index` when performing a control transfer with `Recipient::Interface`"); |
| 285 |
} |
| 286 |
|
| 287 |
addr_of_mut!((*self.event).ptr).write(user_data); |
| 288 |
|
| 289 |
let mut buf = ManuallyDrop::new(data.data.to_vec()); |
| 290 |
self.buf = buf.as_mut_ptr(); |
| 291 |
self.capacity = buf.capacity(); |
| 292 |
let len: u16 = buf |
| 293 |
.len() |
| 294 |
.try_into() |
| 295 |
.expect("transfer size should fit in u16"); |
| 296 |
|
| 297 |
debug!( |
| 298 |
"Submit transfer {:?} on endpoint {:02X} for {} bytes ControlOUT", |
| 299 |
self.event, self.endpoint, len |
| 300 |
); |
| 301 |
|
| 302 |
let pkt = WINUSB_SETUP_PACKET { |
| 303 |
RequestType: data.request_type(), |
| 304 |
Request: data.request, |
| 305 |
Value: data.value, |
| 306 |
Index: data.index, |
| 307 |
Length: len as u16, |
| 308 |
}; |
| 309 |
|
| 310 |
let r = WinUsb_ControlTransfer( |
| 311 |
self.interface.winusb_handle, |
| 312 |
pkt, |
| 313 |
self.buf, |
| 314 |
len as u32, |
| 315 |
null_mut(), |
| 316 |
self.event as *mut OVERLAPPED, |
| 317 |
); |
| 318 |
|
| 319 |
self.post_submit(r, "WinUsb_ControlTransfer", user_data); |
| 320 |
} |
| 321 |
|
| 322 |
unsafe fn take_completed(&mut self) -> Completion<ResponseBuffer> { |
| 323 |
let (actual_len, status) = self.get_status(); |
| 324 |
let data = ResponseBuffer::from_vec(self.take_buf(0), actual_len); |
| 325 |
Completion { data, status } |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
pub(super) fn handle_event(completion: *mut OVERLAPPED) { |
| 330 |
let completion = completion as *mut EventNotify; |
| 331 |
debug!("Handling completion for transfer {completion:?}"); |
| 332 |
unsafe { |
| 333 |
let p = addr_of_mut!((*completion).ptr).read(); |
| 334 |
notify_completion::<TransferData>(p) |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
pub(crate) fn map_error(err: WIN32_ERROR) -> TransferError { |
| 339 |
match err { |
| 340 |
ERROR_GEN_FAILURE => TransferError::Stall, |
| 341 |
ERROR_REQUEST_ABORTED | ERROR_TIMEOUT | ERROR_SEM_TIMEOUT | ERROR_OPERATION_ABORTED => { |
| 342 |
TransferError::Cancelled |
| 343 |
} |
| 344 |
ERROR_FILE_NOT_FOUND | ERROR_DEVICE_NOT_CONNECTED | ERROR_NO_SUCH_DEVICE => { |
| 345 |
TransferError::Disconnected |
| 346 |
} |
| 347 |
_ => TransferError::Unknown, |
| 348 |
} |
| 349 |
} |