From 425e4aad1e944b79e820dbe696a4a9fc0360b595 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Mon, 5 Apr 2021 14:40:01 -0700 Subject: [PATCH] devices: usb: clean up intercepted_control_transfer Rearrange the control request type match to check the request and recipient together in a tuple (along with direction, which will be different for a Get Descriptor request, to be added in the next commit). No functional change. BUG=b:180238956 BUG=chromium:1030778 TEST=Share USB device with Crostini Change-Id: I51badd8f8b067a5e19216a92257c2b75c2041663 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2811860 Tested-by: kokoro Commit-Queue: Daniel Verkamp Reviewed-by: Zach Reizner --- devices/src/usb/host_backend/host_device.rs | 59 +++++++++++---------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/devices/src/usb/host_backend/host_device.rs b/devices/src/usb/host_backend/host_device.rs index daed9c5747..3aa15c0b00 100644 --- a/devices/src/usb/host_backend/host_device.rs +++ b/devices/src/usb/host_backend/host_device.rs @@ -95,43 +95,48 @@ impl HostDevice { fn intercepted_control_transfer(&mut self, xhci_transfer: &XhciTransfer) -> Result { let direction = self.control_request_setup.get_direction(); let recipient = self.control_request_setup.get_recipient(); - let standard_request = self.control_request_setup.get_standard_request(); - - if direction != ControlRequestDataPhaseTransferDirection::HostToDevice { - // Only host to device requests are intercepted currently. + let standard_request = if let Some(req) = self.control_request_setup.get_standard_request() + { + req + } else { + // Unknown control requests will be passed through to the device. return Ok(false); - } + }; - let status = match standard_request { - Some(StandardControlRequest::SetAddress) => { - if recipient != ControlRequestRecipient::Device { - return Ok(false); - } + let (status, bytes_transferred) = match (standard_request, recipient, direction) { + ( + StandardControlRequest::SetAddress, + ControlRequestRecipient::Device, + ControlRequestDataPhaseTransferDirection::HostToDevice, + ) => { usb_debug!("host device handling set address"); let addr = self.control_request_setup.value as u32; self.set_address(addr); - TransferStatus::Completed + (TransferStatus::Completed, 0) } - Some(StandardControlRequest::SetConfiguration) => { - if recipient != ControlRequestRecipient::Device { - return Ok(false); - } + ( + StandardControlRequest::SetConfiguration, + ControlRequestRecipient::Device, + ControlRequestDataPhaseTransferDirection::HostToDevice, + ) => { usb_debug!("host device handling set config"); - self.set_config()? + (self.set_config()?, 0) } - Some(StandardControlRequest::SetInterface) => { - if recipient != ControlRequestRecipient::Interface { - return Ok(false); - } + ( + StandardControlRequest::SetInterface, + ControlRequestRecipient::Interface, + ControlRequestDataPhaseTransferDirection::HostToDevice, + ) => { usb_debug!("host device handling set interface"); - self.set_interface()? + (self.set_interface()?, 0) } - Some(StandardControlRequest::ClearFeature) => { - if recipient != ControlRequestRecipient::Endpoint { - return Ok(false); - } + ( + StandardControlRequest::ClearFeature, + ControlRequestRecipient::Endpoint, + ControlRequestDataPhaseTransferDirection::HostToDevice, + ) => { usb_debug!("host device handling clear feature"); - self.clear_feature()? + (self.clear_feature()?, 0) } _ => { // Other requests will be passed through to the device. @@ -140,7 +145,7 @@ impl HostDevice { }; xhci_transfer - .on_transfer_complete(&status, 0) + .on_transfer_complete(&status, bytes_transferred) .map_err(Error::TransferComplete)?; Ok(true) }