summary refs log tree commit diff
path: root/usb_util/src/device_handle.rs
diff options
context:
space:
mode:
authorDaniel Verkamp <dverkamp@chromium.org>2019-07-09 17:21:54 -0700
committerCommit Bot <commit-bot@chromium.org>2019-10-17 00:20:24 +0000
commit6494117e1766337f5d688b98bfc3df999932c3ac (patch)
tree0c8b6fd24a615ae146bb0a2d6f4d8ce332f5cc42 /usb_util/src/device_handle.rs
parentbed8b0017d2cb283c20dc50241adb4f5b2668489 (diff)
downloadcrosvm-6494117e1766337f5d688b98bfc3df999932c3ac.tar
crosvm-6494117e1766337f5d688b98bfc3df999932c3ac.tar.gz
crosvm-6494117e1766337f5d688b98bfc3df999932c3ac.tar.bz2
crosvm-6494117e1766337f5d688b98bfc3df999932c3ac.tar.lz
crosvm-6494117e1766337f5d688b98bfc3df999932c3ac.tar.xz
crosvm-6494117e1766337f5d688b98bfc3df999932c3ac.tar.zst
crosvm-6494117e1766337f5d688b98bfc3df999932c3ac.zip
usb: replace libusb with Rust usb_util library
Drop the dependency on libusb and reimplement the host USB backend using
usb_sys to wrap the Linux usbdevfs ioctls.

This allows sandboxing to work without any dependency on libusb patches,
and it gives us the flexibility to modify and update the USB backend
without depending on an external third-party library.

BUG=chromium:987833
TEST=`adb logcat` on nami with Nexus 5 attached
TEST=deploy app to phone with Android Studio
TEST=Run EdgeTPU USB accelerator demo (including DFU mode transition)

Cq-Depend: chromium:1773695
Change-Id: I4321c2b6142caac15f48f197795a37d59d268831
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1783601
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'usb_util/src/device_handle.rs')
-rw-r--r--usb_util/src/device_handle.rs161
1 files changed, 0 insertions, 161 deletions
diff --git a/usb_util/src/device_handle.rs b/usb_util/src/device_handle.rs
deleted file mode 100644
index 17b0f68..0000000
--- a/usb_util/src/device_handle.rs
+++ /dev/null
@@ -1,161 +0,0 @@
-// Copyright 2018 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-use std::os::raw::c_int;
-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::{TransferCanceller, UsbTransfer, UsbTransferBuffer};
-
-/// DeviceHandle wraps libusb_device_handle.
-pub struct DeviceHandle {
-    _context: Arc<LibUsbContextInner>,
-    handle: *mut bindings::libusb_device_handle,
-}
-
-unsafe impl Send for DeviceHandle {}
-
-impl Drop for DeviceHandle {
-    fn drop(&mut self) {
-        // Safe because self.handle is a valid pointer to libusb_device_handle.
-        unsafe {
-            bindings::libusb_close(self.handle);
-        }
-    }
-}
-
-impl DeviceHandle {
-    /// Create a new DeviceHande. 'handle' should be a valid pointer to libusb_device_handle.
-    pub unsafe fn new(
-        ctx: Arc<LibUsbContextInner>,
-        handle: *mut bindings::libusb_device_handle,
-    ) -> DeviceHandle {
-        DeviceHandle {
-            _context: ctx,
-            handle,
-        }
-    }
-
-    /// 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.
-        try_libusb!(unsafe { bindings::libusb_reset_device(self.handle) });
-        Ok(())
-    }
-    /// Get bConfigurationValue of the currently active configuration.
-    pub fn get_active_configuration(&self) -> Result<i32> {
-        let mut config: c_int = 0;
-        // Safe because 'self.handle' is a valid pointer to device handle and '&mut config' is a
-        // valid output location.
-        try_libusb!(unsafe { bindings::libusb_get_configuration(self.handle, &mut config) });
-        Ok(config as i32)
-    }
-
-    /// Set active configuration for a device.
-    pub fn set_active_configuration(&mut self, config: i32) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe { bindings::libusb_set_configuration(self.handle, config as c_int) });
-        Ok(())
-    }
-
-    /// Claim an interface on this deivce handle.
-    pub fn claim_interface(&self, interface_number: i32) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe { bindings::libusb_claim_interface(self.handle, interface_number) });
-        Ok(())
-    }
-
-    /// Release an interface previously claimed with libusb_claim_interface.
-    pub fn release_interface(&self, interface_number: i32) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe { bindings::libusb_release_interface(self.handle, interface_number) });
-        Ok(())
-    }
-
-    /// Perform a USB port reset to reinitialize a device.
-    pub fn reset_device(&self) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe { bindings::libusb_reset_device(self.handle) });
-        Ok(())
-    }
-
-    /// Determine if a kernel driver is active on an interface.
-    pub fn kernel_driver_active(&self, interface_number: i32) -> Result<bool> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        let v = try_libusb!(unsafe {
-            bindings::libusb_kernel_driver_active(self.handle, interface_number)
-        });
-        Ok(v != 0)
-    }
-
-    /// Detach a kernel driver from an interface.
-    pub fn detach_kernel_driver(&self, interface_number: i32) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe {
-            bindings::libusb_detach_kernel_driver(self.handle, interface_number)
-        });
-        Ok(())
-    }
-
-    /// Re-attach an interfae's kernel driver, which was previously detached using
-    /// detach_kernel_driver.
-    pub fn attach_kernel_driver(&self, interface_number: i32) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe {
-            bindings::libusb_attach_kernel_driver(self.handle, interface_number)
-        });
-        Ok(())
-    }
-
-    /// Active an alternate setting for an interface.
-    pub fn set_interface_alt_setting(
-        &self,
-        interface_number: i32,
-        alternative_setting: i32,
-    ) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe {
-            bindings::libusb_set_interface_alt_setting(
-                self.handle,
-                interface_number,
-                alternative_setting,
-            )
-        });
-        Ok(())
-    }
-
-    /// Clear the halt/stall condition for an endpoint.
-    pub fn clear_halt(&self, endpoint: u8) -> Result<()> {
-        // Safe because 'self.handle' is a valid pointer to device handle.
-        try_libusb!(unsafe { bindings::libusb_clear_halt(self.handle, endpoint) });
-        Ok(())
-    }
-
-    /// Libusb asynchronous I/O interface has a 5 step process. It gives lots of
-    /// flexibility but makes it hard to manage object life cycle and easy to
-    /// write unsafe code. We wrap this interface to a simple "transfer" and "cancel"
-    /// interface. Resubmission is not supported and deallocation is handled safely
-    /// here.
-    pub fn submit_async_transfer<T: UsbTransferBuffer>(
-        &self,
-        transfer: UsbTransfer<T>,
-    ) -> Result<TransferCanceller> {
-        unsafe { transfer.submit(self.handle) }
-    }
-}