summary refs log tree commit diff
path: root/devices/src/usb
diff options
context:
space:
mode:
authorDaniel Verkamp <dverkamp@chromium.org>2019-06-25 12:47:31 -0700
committerCommit Bot <commit-bot@chromium.org>2019-06-27 17:51:06 +0000
commit6160e479f6a5aad9cabf7d831ff63f4dd14e9704 (patch)
treef7f451a5ad82a3fb80680db4a833df9632e908e8 /devices/src/usb
parent6a8cd101b2e81e0478fb0fe6659759bad470e213 (diff)
downloadcrosvm-6160e479f6a5aad9cabf7d831ff63f4dd14e9704.tar
crosvm-6160e479f6a5aad9cabf7d831ff63f4dd14e9704.tar.gz
crosvm-6160e479f6a5aad9cabf7d831ff63f4dd14e9704.tar.bz2
crosvm-6160e479f6a5aad9cabf7d831ff63f4dd14e9704.tar.lz
crosvm-6160e479f6a5aad9cabf7d831ff63f4dd14e9704.tar.xz
crosvm-6160e479f6a5aad9cabf7d831ff63f4dd14e9704.tar.zst
crosvm-6160e479f6a5aad9cabf7d831ff63f4dd14e9704.zip
usb: switch to new libusb_wrap_sys_device API
Replace use of our custom, patched libusb APIs with the new
libusb_wrap_sys_device() function, which has been submitted to libusb
upstream. This allows us to drop the bindings for the custom APIs (and
will also allow us to drop the libusb patch that introduces them).

For now, keep this path behind the sandboxed-libusb feature to allow
crosvm to build against older libusb versions that do not have the new
API. This should be cleaned up eventually once we are comfortable with
raising the minimum libusb version required.

BUG=b:133773289
TEST=Attach Android device to Linux VM; deploy app via adb

Change-Id: Ie249c6f3f3b4c63210dd163ca7ad03e2de8a8872
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1676601
Tested-by: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'devices/src/usb')
-rw-r--r--devices/src/usb/host_backend/context.rs24
-rw-r--r--devices/src/usb/host_backend/host_backend_device_provider.rs21
2 files changed, 8 insertions, 37 deletions
diff --git a/devices/src/usb/host_backend/context.rs b/devices/src/usb/host_backend/context.rs
index 960d066..76ad4a4 100644
--- a/devices/src/usb/host_backend/context.rs
+++ b/devices/src/usb/host_backend/context.rs
@@ -8,8 +8,11 @@ use std::os::raw::c_short;
 use std::os::unix::io::RawFd;
 use std::sync::{Arc, Weak};
 use sys_util::{error, WatchingEvents};
+#[cfg(feature = "sandboxed-libusb")]
+use usb_util::device_handle::DeviceHandle;
 use usb_util::hotplug::UsbHotplugHandler;
 use usb_util::libusb_context::{LibUsbContext, LibUsbPollfdChangeHandler};
+#[cfg(not(feature = "sandboxed-libusb"))]
 use usb_util::libusb_device::LibUsbDevice;
 use vm_control::MaybeOwnedFd;
 
@@ -22,7 +25,6 @@ pub struct Context {
 
 impl Context {
     /// Create a new context.
-    #[cfg(not(feature = "sandboxed-libusb"))]
     pub fn new(event_loop: Arc<EventLoop>) -> Result<Context> {
         let context = LibUsbContext::new().map_err(Error::CreateLibUsbContext)?;
         let ctx = Context {
@@ -36,20 +38,6 @@ impl Context {
         Ok(ctx)
     }
 
-    #[cfg(feature = "sandboxed-libusb")]
-    pub fn new(event_loop: Arc<EventLoop>) -> Result<Context> {
-        let context = LibUsbContext::new_jailed().map_err(Error::CreateLibUsbContext)?;
-        let ctx = Context {
-            context: context.clone(),
-            event_loop,
-            event_handler: Arc::new(LibUsbEventHandler {
-                context: context.clone(),
-            }),
-        };
-        ctx.init_event_handler()?;
-        Ok(ctx)
-    }
-
     pub fn set_hotplug_handler<H: UsbHotplugHandler + Sized>(&self, handler: H) {
         if let Err(e) = self.context.set_hotplug_cb(handler) {
             error!("cannot set hotplug handler: {:?}", e);
@@ -100,9 +88,9 @@ impl Context {
     }
 
     #[cfg(feature = "sandboxed-libusb")]
-    pub fn get_device(&self, fd: std::fs::File) -> Option<LibUsbDevice> {
-        match self.context.get_device_from_fd(fd) {
-            Ok(dev) => Some(dev),
+    pub fn get_device_handle(&self, fd: std::fs::File) -> Option<DeviceHandle> {
+        match self.context.handle_from_file(fd) {
+            Ok(handle) => Some(handle),
             Err(e) => {
                 error!("could not build device from fd: {:?}", e);
                 None
diff --git a/devices/src/usb/host_backend/host_backend_device_provider.rs b/devices/src/usb/host_backend/host_backend_device_provider.rs
index 01fd84c..f479bcb 100644
--- a/devices/src/usb/host_backend/host_backend_device_provider.rs
+++ b/devices/src/usb/host_backend/host_backend_device_provider.rs
@@ -209,9 +209,7 @@ impl ProviderInner {
                         }
                     };
 
-                    let device_fd = usb_file.as_raw_fd();
-
-                    let device = match self.ctx.get_device(usb_file) {
+                    let device_handle = match self.ctx.get_device_handle(usb_file) {
                         Some(d) => d,
                         None => {
                             error!(
@@ -228,22 +226,7 @@ impl ProviderInner {
                         }
                     };
 
-                    let device_handle = {
-                        // This is safe only when fd is an fd of the current device.
-                        match unsafe { device.open_fd(device_fd) } {
-                            Ok(handle) => handle,
-                            Err(e) => {
-                                error!("fail to open device: {:?}", e);
-                                // The send failure will be logged, but event loop still think
-                                // the event is handled.
-                                let _ = self
-                                    .sock
-                                    .send(&UsbControlResult::FailedToOpenDevice)
-                                    .map_err(Error::WriteControlSock);
-                                return Ok(());
-                            }
-                        }
-                    };
+                    let device = device_handle.get_device();
 
                     // Resetting the device is used to make sure it is in a known state, but it may
                     // still function if the reset fails.