summary refs log tree commit diff
path: root/usb_util
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 /usb_util
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 'usb_util')
-rw-r--r--usb_util/src/bindings.rs16
-rw-r--r--usb_util/src/device_handle.rs13
-rw-r--r--usb_util/src/libusb_context.rs32
-rw-r--r--usb_util/src/libusb_device.rs12
4 files changed, 26 insertions, 47 deletions
diff --git a/usb_util/src/bindings.rs b/usb_util/src/bindings.rs
index a3e7152..8f62a01 100644
--- a/usb_util/src/bindings.rs
+++ b/usb_util/src/bindings.rs
@@ -4012,16 +4012,13 @@ extern "C" {
     pub fn libusb_init(ctx: *mut *mut libusb_context) -> ::std::os::raw::c_int;
 }
 extern "C" {
-    pub fn libusb_init_jailed(ctx: *mut *mut libusb_context) -> ::std::os::raw::c_int;
-}
-extern "C" {
     pub fn libusb_exit(ctx: *mut libusb_context);
 }
 extern "C" {
-    pub fn libusb_get_device_from_fd(
+    pub fn libusb_wrap_sys_device(
         ctx: *mut libusb_context,
-        fd: ::std::os::raw::c_int,
-        device: *mut *mut libusb_device,
+        fd: __intptr_t,
+        handle: *mut *mut libusb_device_handle,
     ) -> ::std::os::raw::c_int;
 }
 extern "C" {
@@ -4199,13 +4196,6 @@ extern "C" {
     ) -> ::std::os::raw::c_int;
 }
 extern "C" {
-    pub fn libusb_open_fd(
-        dev: *mut libusb_device,
-        fd: ::std::os::raw::c_int,
-        handle: *mut *mut libusb_device_handle,
-    ) -> ::std::os::raw::c_int;
-}
-extern "C" {
     pub fn libusb_close(dev_handle: *mut libusb_device_handle);
 }
 extern "C" {
diff --git a/usb_util/src/device_handle.rs b/usb_util/src/device_handle.rs
index 32a963f..77a91be 100644
--- a/usb_util/src/device_handle.rs
+++ b/usb_util/src/device_handle.rs
@@ -8,6 +8,7 @@ use std::sync::Arc;
 use crate::bindings;
 use crate::error::{Error, Result};
 use crate::libusb_context::LibUsbContextInner;
+use crate::libusb_device::LibUsbDevice;
 use crate::usb_transfer::{UsbTransfer, UsbTransferBuffer};
 
 /// DeviceHandle wraps libusb_device_handle.
@@ -39,6 +40,18 @@ impl DeviceHandle {
         }
     }
 
+    /// Get corresponding usb device.
+    pub fn get_device(&self) -> LibUsbDevice {
+        // Safe because 'self.handle' is a valid pointer to device handle and libusb_get_device()
+        // always returns a valid device.
+        unsafe {
+            LibUsbDevice::new(
+                self._context.clone(),
+                bindings::libusb_get_device(self.handle),
+            )
+        }
+    }
+
     /// Reset this usb device.
     pub fn reset(&self) -> Result<()> {
         // Safe because 'self.handle' is a valid pointer to device handle.
diff --git a/usb_util/src/libusb_context.rs b/usb_util/src/libusb_context.rs
index a1bab32..17a77f0 100644
--- a/usb_util/src/libusb_context.rs
+++ b/usb_util/src/libusb_context.rs
@@ -3,11 +3,14 @@
 // found in the LICENSE file.
 
 use std;
-use std::os::raw::{c_short, c_void};
+#[allow(unused_imports)]
+use std::os::raw::{c_long, c_short, c_void};
 use std::os::unix::io::RawFd;
 use std::sync::Arc;
 
 use crate::bindings;
+#[cfg(feature = "sandboxed-libusb")]
+use crate::device_handle::DeviceHandle;
 use crate::error::{Error, Result};
 use crate::hotplug::{hotplug_cb, UsbHotplugHandler, UsbHotplugHandlerHolder};
 use crate::libusb_device::LibUsbDevice;
@@ -66,32 +69,17 @@ impl LibUsbContext {
         })
     }
 
-    /// Create a new jailed LibUsbContext.
     #[cfg(feature = "sandboxed-libusb")]
-    pub fn new_jailed() -> Result<LibUsbContext> {
-        let mut ctx: *mut bindings::libusb_context = std::ptr::null_mut();
-        // Safe because '&mut ctx' points to a valid memory (on stack).
-        try_libusb!(unsafe { bindings::libusb_init_jailed(&mut ctx) });
-        Ok(LibUsbContext {
-            inner: Arc::new(LibUsbContextInner {
-                context: ctx,
-                pollfd_change_handler: Mutex::new(None),
-            }),
-        })
-    }
-
-    /// Build device from File.
-    #[cfg(feature = "sandboxed-libusb")]
-    pub fn get_device_from_fd(&self, fd: std::fs::File) -> Result<LibUsbDevice> {
+    pub fn handle_from_file(&self, f: std::fs::File) -> Result<DeviceHandle> {
         use std::os::unix::io::IntoRawFd;
 
-        let fd = fd.into_raw_fd();
-        let mut device: *mut bindings::libusb_device = std::ptr::null_mut();
-        // Safe because fd is valid and owned, and '&mut device' points to valid memory.
+        let fd = f.into_raw_fd();
+        let mut handle: *mut bindings::libusb_device_handle = std::ptr::null_mut();
+        // Safe because fd is valid and owned, and '&mut handle' points to valid memory.
         try_libusb!(unsafe {
-            bindings::libusb_get_device_from_fd(self.inner.context, fd, &mut device)
+            bindings::libusb_wrap_sys_device(self.inner.context, fd as c_long, &mut handle)
         });
-        unsafe { Ok(LibUsbDevice::new(self.inner.clone(), device)) }
+        unsafe { Ok(DeviceHandle::new(self.inner.clone(), handle)) }
     }
 
     /// Returns a list of USB devices currently attached to the system.
diff --git a/usb_util/src/libusb_device.rs b/usb_util/src/libusb_device.rs
index e8f5120..ad87b46 100644
--- a/usb_util/src/libusb_device.rs
+++ b/usb_util/src/libusb_device.rs
@@ -3,8 +3,6 @@
 // found in the LICENSE file.
 
 use std;
-#[cfg(feature = "sandboxed-libusb")]
-use std::os::unix::io::RawFd;
 use std::sync::Arc;
 
 use crate::bindings;
@@ -107,14 +105,4 @@ impl LibUsbDevice {
         // Safe because handle points to valid memory.
         Ok(unsafe { DeviceHandle::new(self._context.clone(), handle) })
     }
-
-    /// Get device handle of this device. Take an external fd. This function is only safe when fd
-    /// is an fd of this usb device.
-    #[cfg(feature = "sandboxed-libusb")]
-    pub unsafe fn open_fd(&self, fd: RawFd) -> Result<DeviceHandle> {
-        let mut handle: *mut bindings::libusb_device_handle = std::ptr::null_mut();
-        // Safe when 'self.device' is constructed from libusb device list and handle is on stack.
-        try_libusb!(bindings::libusb_open_fd(self.device, fd, &mut handle));
-        Ok(DeviceHandle::new(self._context.clone(), handle))
-    }
 }