From 9b75e703df88db6b314acca4fd7d34919cdf2c0b Mon Sep 17 00:00:00 2001
From: Lin Huang <hl@rock-chips.com>
Date: Fri, 26 Apr 2019 17:33:18 +0800
Subject: [PATCH] Support usb3.0 adbd

Signed-off-by: Lin Huang <hl@rock-chips.com>
---
 core/adbd/functionfs.h       | 12 +++++++--
 core/adbd/usb_linux_client.c | 63 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/core/adbd/functionfs.h b/core/adbd/functionfs.h
index 53a3f30..42993f2 100644
--- a/core/adbd/functionfs.h
+++ b/core/adbd/functionfs.h
@@ -10,10 +10,18 @@
 
 enum {
 	FUNCTIONFS_DESCRIPTORS_MAGIC = 1,
-	FUNCTIONFS_STRINGS_MAGIC     = 2
+	FUNCTIONFS_STRINGS_MAGIC     = 2,
+	FUNCTIONFS_DESCRIPTORS_MAGIC_V2 = 3,
 };
 
-
+enum functionfs_flags {
+	FUNCTIONFS_HAS_FS_DESC = 1,
+	FUNCTIONFS_HAS_HS_DESC = 2,
+	FUNCTIONFS_HAS_SS_DESC = 4,
+	FUNCTIONFS_HAS_MS_OS_DESC = 8,
+	FUNCTIONFS_VIRTUAL_ADDR = 16,
+	FUNCTIONFS_EVENTFD = 32,
+};
 
 /* Descriptor of an non-audio endpoint */
 struct usb_endpoint_descriptor_no_audio {
diff --git a/core/adbd/usb_linux_client.c b/core/adbd/usb_linux_client.c
index 0e4d200..a437ad1 100644
--- a/core/adbd/usb_linux_client.c
+++ b/core/adbd/usb_linux_client.c
@@ -33,6 +33,7 @@
 
 #define MAX_PACKET_SIZE_FS	64
 #define MAX_PACKET_SIZE_HS	512
+#define MAX_PACKET_SIZE_SS	1024
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 # define cpu_to_le16(x) (x)
@@ -63,19 +64,33 @@ struct usb_handle
 };
 
 static const struct {
-    struct usb_functionfs_descs_head header;
+    __le32 magic;
+    __le32 length;
+    __le32 flags;
+    __le32 fs_count;
+    __le32 hs_count;
+    __le32 ss_count;
     struct {
         struct usb_interface_descriptor intf;
         struct usb_endpoint_descriptor_no_audio source;
         struct usb_endpoint_descriptor_no_audio sink;
     } __attribute__((packed)) fs_descs, hs_descs;
+    struct {
+        struct usb_interface_descriptor intf;
+	struct usb_endpoint_descriptor_no_audio source;
+	struct usb_ss_ep_comp_descriptor source_comp;
+	struct usb_endpoint_descriptor_no_audio sink;
+	struct usb_ss_ep_comp_descriptor sink_comp;
+    } __attribute__((packed)) ss_descs;
 } __attribute__((packed)) descriptors = {
-    .header = {
-        .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
-        .length = cpu_to_le32(sizeof(descriptors)),
-        .fs_count = 3,
-        .hs_count = 3,
-    },
+    .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
+    .length = cpu_to_le32(sizeof(descriptors)),
+    .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
+                         FUNCTIONFS_HAS_HS_DESC |
+                         FUNCTIONFS_HAS_SS_DESC),
+    .fs_count = 3,
+    .hs_count = 3,
+    .ss_count = 5,
     .fs_descs = {
         .intf = {
             .bLength = sizeof(descriptors.fs_descs.intf),
@@ -128,6 +143,40 @@ static const struct {
             .wMaxPacketSize = MAX_PACKET_SIZE_HS,
         },
     },
+    .ss_descs = {
+        .intf = {
+            .bLength = sizeof(descriptors.ss_descs.intf),
+            .bDescriptorType = USB_DT_INTERFACE,
+            .bInterfaceNumber = 0,
+            .bNumEndpoints = 2,
+            .bInterfaceClass = ADB_CLASS,
+            .bInterfaceSubClass = ADB_SUBCLASS,
+            .bInterfaceProtocol = ADB_PROTOCOL,
+            .iInterface = 1, /* first string from the provided table */
+        },
+        .source = {
+            .bLength = sizeof(descriptors.ss_descs.source),
+            .bDescriptorType = USB_DT_ENDPOINT,
+            .bEndpointAddress = 1 | USB_DIR_OUT,
+            .bmAttributes = USB_ENDPOINT_XFER_BULK,
+            .wMaxPacketSize = MAX_PACKET_SIZE_SS,
+        },
+        .source_comp = {
+            .bLength = sizeof(descriptors.ss_descs.source_comp),
+            .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
+        },
+        .sink = {
+            .bLength = sizeof(descriptors.ss_descs.sink),
+            .bDescriptorType = USB_DT_ENDPOINT,
+            .bEndpointAddress = 2 | USB_DIR_IN,
+            .bmAttributes = USB_ENDPOINT_XFER_BULK,
+            .wMaxPacketSize = MAX_PACKET_SIZE_SS,
+        },
+        .sink_comp = {
+            .bLength = sizeof(descriptors.ss_descs.sink_comp),
+            .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
+        },
+    },
 };
 
 #define STR_INTERFACE_ "ADB Interface"
-- 
2.7.4

