summary refs log tree commit diff
path: root/usb_util/src/libusb_context.rs
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/src/libusb_context.rs
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/src/libusb_context.rs')
-rw-r--r--usb_util/src/libusb_context.rs32
1 files changed, 10 insertions, 22 deletions
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.