summary refs log tree commit diff
path: root/usb_util/src/endpoint_descriptor.rs
diff options
context:
space:
mode:
authorJingkui Wang <jkwang@google.com>2018-11-02 00:09:31 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-12-01 01:08:36 -0800
commit81066162c240f45fbed96ca868d69c91a40bfb0e (patch)
treec8f79b405e7cd93506402aeac26afa086ca99a09 /usb_util/src/endpoint_descriptor.rs
parent457ebc9d93253cc44d81baacda889ff604fb0319 (diff)
downloadcrosvm-81066162c240f45fbed96ca868d69c91a40bfb0e.tar
crosvm-81066162c240f45fbed96ca868d69c91a40bfb0e.tar.gz
crosvm-81066162c240f45fbed96ca868d69c91a40bfb0e.tar.bz2
crosvm-81066162c240f45fbed96ca868d69c91a40bfb0e.tar.lz
crosvm-81066162c240f45fbed96ca868d69c91a40bfb0e.tar.xz
crosvm-81066162c240f45fbed96ca868d69c91a40bfb0e.tar.zst
crosvm-81066162c240f45fbed96ca868d69c91a40bfb0e.zip
usb_util: Add descriptors
add config descriptors, endpoint descriptors, interface descriptors.

BUG=chromium:831850
TEST=cargo test
CQ-DEPEND=CL:1135783

Change-Id: If74c407f198725bdc6a3096b03d6fe02dcd29ec8
Reviewed-on: https://chromium-review.googlesource.com/1299716
Commit-Ready: Jingkui Wang <jkwang@google.com>
Tested-by: Jingkui Wang <jkwang@google.com>
Reviewed-by: Jingkui Wang <jkwang@google.com>
Diffstat (limited to 'usb_util/src/endpoint_descriptor.rs')
-rw-r--r--usb_util/src/endpoint_descriptor.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/usb_util/src/endpoint_descriptor.rs b/usb_util/src/endpoint_descriptor.rs
new file mode 100644
index 0000000..e8d328a
--- /dev/null
+++ b/usb_util/src/endpoint_descriptor.rs
@@ -0,0 +1,56 @@
+// 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 super::types::{EndpointDirection, EndpointType};
+use bindings::libusb_endpoint_descriptor;
+use std::ops::Deref;
+
+/// ConfigDescriptor wraps libusb_interface_descriptor.
+pub struct EndpointDescriptor<'a>(&'a libusb_endpoint_descriptor);
+
+const ENDPOINT_DESCRIPTOR_DIRECTION_MASK: u8 = 1 << 7;
+const ENDPOINT_DESCRIPTOR_NUMBER_MASK: u8 = 0xf;
+const ENDPOINT_DESCRIPTOR_ATTRIBUTES_TYPE_MASK: u8 = 0x3;
+
+impl<'a> EndpointDescriptor<'a> {
+    // Create new endpoint descriptor.
+    pub fn new(descriptor: &libusb_endpoint_descriptor) -> EndpointDescriptor {
+        EndpointDescriptor(descriptor)
+    }
+
+    // Get direction of this endpoint.
+    pub fn get_direction(&self) -> EndpointDirection {
+        let direction = self.0.bEndpointAddress & ENDPOINT_DESCRIPTOR_DIRECTION_MASK;
+        if direction > 0 {
+            EndpointDirection::DeviceToHost
+        } else {
+            EndpointDirection::HostToDevice
+        }
+    }
+
+    // Get endpoint number.
+    pub fn get_endpoint_number(&self) -> u8 {
+        self.0.bEndpointAddress & ENDPOINT_DESCRIPTOR_NUMBER_MASK
+    }
+
+    // Get endpoint type.
+    pub fn get_endpoint_type(&self) -> Option<EndpointType> {
+        let ep_type = self.0.bmAttributes & ENDPOINT_DESCRIPTOR_ATTRIBUTES_TYPE_MASK;
+        match ep_type {
+            0 => Some(EndpointType::Control),
+            1 => Some(EndpointType::Isochronous),
+            2 => Some(EndpointType::Bulk),
+            3 => Some(EndpointType::Interrupt),
+            _ => None,
+        }
+    }
+}
+
+impl<'a> Deref for EndpointDescriptor<'a> {
+    type Target = libusb_endpoint_descriptor;
+
+    fn deref(&self) -> &libusb_endpoint_descriptor {
+        self.0
+    }
+}