Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 1 | From dae9a11f3a158357966399aef97c48b5f16934d9 Mon Sep 17 00:00:00 2001 |
| 2 | From: Jiacheng Liu <jiacheng.liu@mediatek.com> |
| 3 | Date: Sat, 24 Jul 2021 11:01:18 +0800 |
| 4 | Subject: [PATCH] android-tools: adb: add u3 ss descriptor support |
| 5 | |
| 6 | Porting u3 Superspeed descriptor support to open-embedded android-tools package. |
| 7 | This patch origins from the the patch in android project [1], but has been |
| 8 | modified for backporting to android-tools_5.1.1.r37. |
| 9 | |
| 10 | [1] https://android.googlesource.com/platform/system/core/+/d6ee9f26a5163af4121f4380264fcbd4e6851a17%5E%21 |
| 11 | |
| 12 | Signed-off-by: Macpaul Lin <macpaul.lin@mediatek.com> |
| 13 | Signed-off-by: Jiacheng Liu <jiacheng.liu@mediatek.com> |
| 14 | --- |
| 15 | adb/usb_linux_client.c | 275 +++++++++++++++++++++++++++++++---------- |
| 16 | 1 file changed, 207 insertions(+), 68 deletions(-) |
| 17 | |
| 18 | diff --git a/adb/usb_linux_client.c b/adb/usb_linux_client.c |
| 19 | index 6e8b5bb..884e85e 100644 |
| 20 | --- a/adb/usb_linux_client.c |
| 21 | +++ b/adb/usb_linux_client.c |
| 22 | @@ -31,8 +31,10 @@ |
| 23 | #define TRACE_TAG TRACE_USB |
| 24 | #include "adb.h" |
| 25 | |
| 26 | +#define USB_EXT_PROP_UNICODE 1 |
| 27 | #define MAX_PACKET_SIZE_FS 64 |
| 28 | #define MAX_PACKET_SIZE_HS 512 |
| 29 | +#define MAX_PACKET_SIZE_SS 1024 |
| 30 | |
| 31 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 32 | # define cpu_to_le16(x) (x) |
| 33 | @@ -62,74 +64,185 @@ struct usb_handle |
| 34 | int bulk_in; /* "in" from the host's perspective => sink for adbd */ |
| 35 | }; |
| 36 | |
| 37 | -static const struct { |
| 38 | - struct usb_functionfs_descs_head header; |
| 39 | - struct { |
| 40 | - struct usb_interface_descriptor intf; |
| 41 | - struct usb_endpoint_descriptor_no_audio source; |
| 42 | - struct usb_endpoint_descriptor_no_audio sink; |
| 43 | - } __attribute__((packed)) fs_descs, hs_descs; |
| 44 | -} __attribute__((packed)) descriptors = { |
| 45 | - .header = { |
| 46 | - .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC), |
| 47 | - .length = cpu_to_le32(sizeof(descriptors)), |
| 48 | - .fs_count = 3, |
| 49 | - .hs_count = 3, |
| 50 | +struct func_desc { |
| 51 | + struct usb_interface_descriptor intf; |
| 52 | + struct usb_endpoint_descriptor_no_audio source; |
| 53 | + struct usb_endpoint_descriptor_no_audio sink; |
| 54 | +} __attribute__((packed)); |
| 55 | + |
| 56 | +struct ss_func_desc { |
| 57 | + struct usb_interface_descriptor intf; |
| 58 | + struct usb_endpoint_descriptor_no_audio source; |
| 59 | + struct usb_ss_ep_comp_descriptor source_comp; |
| 60 | + struct usb_endpoint_descriptor_no_audio sink; |
| 61 | + struct usb_ss_ep_comp_descriptor sink_comp; |
| 62 | +} __attribute__((packed)); |
| 63 | + |
| 64 | +struct desc_v1 { |
| 65 | + struct usb_functionfs_descs_head_v1 { |
| 66 | + __le32 magic; |
| 67 | + __le32 length; |
| 68 | + __le32 fs_count; |
| 69 | + __le32 hs_count; |
| 70 | + } __attribute__((packed)) header; |
| 71 | + struct func_desc fs_descs, hs_descs; |
| 72 | +} __attribute__((packed)); |
| 73 | + |
| 74 | +struct usb_os_desc_ext_prop { |
| 75 | + uint32_t dwSize; |
| 76 | + uint32_t dwPropertyDataType; |
| 77 | + |
| 78 | + // Property name and value are transmitted as UTF-16, but the kernel only |
| 79 | + // accepts ASCII values and performs the conversion for us. |
| 80 | + uint16_t wPropertyNameLength; |
| 81 | + char bPropertyName[20]; |
| 82 | + |
| 83 | + uint32_t dwPropertyDataLength; |
| 84 | + char bProperty[39]; |
| 85 | +} __attribute__((packed)) os_desc_guid = { |
| 86 | + .dwSize = sizeof(struct usb_os_desc_ext_prop), |
| 87 | + .dwPropertyDataType = cpu_to_le32(USB_EXT_PROP_UNICODE), |
| 88 | + .wPropertyNameLength = cpu_to_le16(20), |
| 89 | + .bPropertyName = "DeviceInterfaceGUID", |
| 90 | + .dwPropertyDataLength = cpu_to_le32(39), |
| 91 | + .bProperty = "{F72FE0D4-CBCB-407D-8814-9ED673D0DD6B}", |
| 92 | +}; |
| 93 | + |
| 94 | +struct usb_ext_prop_values { |
| 95 | + struct usb_os_desc_ext_prop guid; |
| 96 | +} __attribute__((packed)); |
| 97 | + |
| 98 | +struct desc_v2 { |
| 99 | + struct usb_functionfs_descs_head_v2 header; |
| 100 | + // The rest of the structure depends on the flags in the header. |
| 101 | + __le32 fs_count; |
| 102 | + __le32 hs_count; |
| 103 | + __le32 ss_count; |
| 104 | + __le32 os_count; |
| 105 | + struct func_desc fs_descs, hs_descs; |
| 106 | + struct ss_func_desc ss_descs; |
| 107 | + struct usb_os_desc_header os_header; |
| 108 | + struct usb_ext_compat_desc os_desc; |
| 109 | + struct usb_os_desc_header os_prop_header; |
| 110 | + struct usb_ext_prop_values os_prop_values; |
| 111 | +} __attribute__((packed)); |
| 112 | + |
| 113 | +static struct func_desc fs_descriptors = { |
| 114 | + .intf = { |
| 115 | + .bLength = sizeof(fs_descriptors.intf), |
| 116 | + .bDescriptorType = USB_DT_INTERFACE, |
| 117 | + .bInterfaceNumber = 0, |
| 118 | + .bNumEndpoints = 2, |
| 119 | + .bInterfaceClass = ADB_CLASS, |
| 120 | + .bInterfaceSubClass = ADB_SUBCLASS, |
| 121 | + .bInterfaceProtocol = ADB_PROTOCOL, |
| 122 | + .iInterface = 1, /* first string from the provided table */ |
| 123 | + }, |
| 124 | + .source = { |
| 125 | + .bLength = sizeof(fs_descriptors.source), |
| 126 | + .bDescriptorType = USB_DT_ENDPOINT, |
| 127 | + .bEndpointAddress = 1 | USB_DIR_OUT, |
| 128 | + .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 129 | + .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 130 | + }, |
| 131 | + .sink = { |
| 132 | + .bLength = sizeof(fs_descriptors.sink), |
| 133 | + .bDescriptorType = USB_DT_ENDPOINT, |
| 134 | + .bEndpointAddress = 2 | USB_DIR_IN, |
| 135 | + .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 136 | + .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 137 | + }, |
| 138 | +}; |
| 139 | + |
| 140 | +static struct func_desc hs_descriptors = { |
| 141 | + .intf = { |
| 142 | + .bLength = sizeof(hs_descriptors.intf), |
| 143 | + .bDescriptorType = USB_DT_INTERFACE, |
| 144 | + .bInterfaceNumber = 0, |
| 145 | + .bNumEndpoints = 2, |
| 146 | + .bInterfaceClass = ADB_CLASS, |
| 147 | + .bInterfaceSubClass = ADB_SUBCLASS, |
| 148 | + .bInterfaceProtocol = ADB_PROTOCOL, |
| 149 | + .iInterface = 1, /* first string from the provided table */ |
| 150 | + }, |
| 151 | + .source = { |
| 152 | + .bLength = sizeof(hs_descriptors.source), |
| 153 | + .bDescriptorType = USB_DT_ENDPOINT, |
| 154 | + .bEndpointAddress = 1 | USB_DIR_OUT, |
| 155 | + .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 156 | + .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 157 | + }, |
| 158 | + .sink = { |
| 159 | + .bLength = sizeof(hs_descriptors.sink), |
| 160 | + .bDescriptorType = USB_DT_ENDPOINT, |
| 161 | + .bEndpointAddress = 2 | USB_DIR_IN, |
| 162 | + .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 163 | + .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 164 | + }, |
| 165 | +}; |
| 166 | + |
| 167 | +static struct ss_func_desc ss_descriptors = { |
| 168 | + .intf = { |
| 169 | + .bLength = sizeof(ss_descriptors.intf), |
| 170 | + .bDescriptorType = USB_DT_INTERFACE, |
| 171 | + .bInterfaceNumber = 0, |
| 172 | + .bNumEndpoints = 2, |
| 173 | + .bInterfaceClass = ADB_CLASS, |
| 174 | + .bInterfaceSubClass = ADB_SUBCLASS, |
| 175 | + .bInterfaceProtocol = ADB_PROTOCOL, |
| 176 | + .iInterface = 1, /* first string from the provided table */ |
| 177 | + }, |
| 178 | + .source = { |
| 179 | + .bLength = sizeof(ss_descriptors.source), |
| 180 | + .bDescriptorType = USB_DT_ENDPOINT, |
| 181 | + .bEndpointAddress = 1 | USB_DIR_OUT, |
| 182 | + .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 183 | + .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 184 | + }, |
| 185 | + .source_comp = { |
| 186 | + .bLength = sizeof(ss_descriptors.source_comp), |
| 187 | + .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 188 | + .bMaxBurst = 4, |
| 189 | }, |
| 190 | - .fs_descs = { |
| 191 | - .intf = { |
| 192 | - .bLength = sizeof(descriptors.fs_descs.intf), |
| 193 | - .bDescriptorType = USB_DT_INTERFACE, |
| 194 | - .bInterfaceNumber = 0, |
| 195 | - .bNumEndpoints = 2, |
| 196 | - .bInterfaceClass = ADB_CLASS, |
| 197 | - .bInterfaceSubClass = ADB_SUBCLASS, |
| 198 | - .bInterfaceProtocol = ADB_PROTOCOL, |
| 199 | - .iInterface = 1, /* first string from the provided table */ |
| 200 | - }, |
| 201 | - .source = { |
| 202 | - .bLength = sizeof(descriptors.fs_descs.source), |
| 203 | - .bDescriptorType = USB_DT_ENDPOINT, |
| 204 | - .bEndpointAddress = 1 | USB_DIR_OUT, |
| 205 | - .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 206 | - .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 207 | - }, |
| 208 | - .sink = { |
| 209 | - .bLength = sizeof(descriptors.fs_descs.sink), |
| 210 | - .bDescriptorType = USB_DT_ENDPOINT, |
| 211 | - .bEndpointAddress = 2 | USB_DIR_IN, |
| 212 | - .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 213 | - .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 214 | - }, |
| 215 | + .sink = { |
| 216 | + .bLength = sizeof(ss_descriptors.sink), |
| 217 | + .bDescriptorType = USB_DT_ENDPOINT, |
| 218 | + .bEndpointAddress = 2 | USB_DIR_IN, |
| 219 | + .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 220 | + .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 221 | }, |
| 222 | - .hs_descs = { |
| 223 | - .intf = { |
| 224 | - .bLength = sizeof(descriptors.hs_descs.intf), |
| 225 | - .bDescriptorType = USB_DT_INTERFACE, |
| 226 | - .bInterfaceNumber = 0, |
| 227 | - .bNumEndpoints = 2, |
| 228 | - .bInterfaceClass = ADB_CLASS, |
| 229 | - .bInterfaceSubClass = ADB_SUBCLASS, |
| 230 | - .bInterfaceProtocol = ADB_PROTOCOL, |
| 231 | - .iInterface = 1, /* first string from the provided table */ |
| 232 | - }, |
| 233 | - .source = { |
| 234 | - .bLength = sizeof(descriptors.hs_descs.source), |
| 235 | - .bDescriptorType = USB_DT_ENDPOINT, |
| 236 | - .bEndpointAddress = 1 | USB_DIR_OUT, |
| 237 | - .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 238 | - .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 239 | - }, |
| 240 | - .sink = { |
| 241 | - .bLength = sizeof(descriptors.hs_descs.sink), |
| 242 | - .bDescriptorType = USB_DT_ENDPOINT, |
| 243 | - .bEndpointAddress = 2 | USB_DIR_IN, |
| 244 | - .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 245 | - .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 246 | - }, |
| 247 | + .sink_comp = { |
| 248 | + .bLength = sizeof(ss_descriptors.sink_comp), |
| 249 | + .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 250 | + .bMaxBurst = 4, |
| 251 | }, |
| 252 | }; |
| 253 | |
| 254 | +struct usb_ext_compat_desc os_desc_compat = { |
| 255 | + .bFirstInterfaceNumber = 0, |
| 256 | + .Reserved1 = cpu_to_le32(1), |
| 257 | + .CompatibleID = { 'W', 'I', 'N', 'U', 'S', 'B', '\0', '\0'}, |
| 258 | + .SubCompatibleID = {0}, |
| 259 | + .Reserved2 = {0}, |
| 260 | +}; |
| 261 | + |
| 262 | +static struct usb_os_desc_header os_desc_header = { |
| 263 | + .interface = cpu_to_le32(0), |
| 264 | + .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)), |
| 265 | + .bcdVersion = cpu_to_le32(1), |
| 266 | + .wIndex = cpu_to_le32(4), |
| 267 | + .bCount = cpu_to_le32(1), |
| 268 | + .Reserved = cpu_to_le32(0), |
| 269 | +}; |
| 270 | + |
| 271 | +static struct usb_os_desc_header os_prop_header = { |
| 272 | + .interface = cpu_to_le32(0), |
| 273 | + .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(struct usb_ext_prop_values)), |
| 274 | + .bcdVersion = cpu_to_le32(1), |
| 275 | + .wIndex = cpu_to_le32(5), |
| 276 | + .wCount = cpu_to_le16(1), |
| 277 | +}; |
| 278 | + |
| 279 | #define STR_INTERFACE_ "ADB Interface" |
| 280 | |
| 281 | static const struct { |
| 282 | @@ -151,8 +264,6 @@ static const struct { |
| 283 | }, |
| 284 | }; |
| 285 | |
| 286 | - |
| 287 | - |
| 288 | static void *usb_adb_open_thread(void *x) |
| 289 | { |
| 290 | struct usb_handle *usb = (struct usb_handle *)x; |
| 291 | @@ -270,6 +381,24 @@ static void usb_adb_init() |
| 292 | static void init_functionfs(struct usb_handle *h) |
| 293 | { |
| 294 | ssize_t ret; |
| 295 | + struct desc_v1 v1_descriptor = {}; |
| 296 | + struct desc_v2 v2_descriptor = {}; |
| 297 | + |
| 298 | + v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); |
| 299 | + v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); |
| 300 | + v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC | |
| 301 | + FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC; |
| 302 | + v2_descriptor.fs_count = 3; |
| 303 | + v2_descriptor.hs_count = 3; |
| 304 | + v2_descriptor.ss_count = 5; |
| 305 | + v2_descriptor.os_count = 2; |
| 306 | + v2_descriptor.fs_descs = fs_descriptors; |
| 307 | + v2_descriptor.hs_descs = hs_descriptors; |
| 308 | + v2_descriptor.ss_descs = ss_descriptors; |
| 309 | + v2_descriptor.os_header = os_desc_header; |
| 310 | + v2_descriptor.os_desc = os_desc_compat; |
| 311 | + v2_descriptor.os_prop_header = os_prop_header; |
| 312 | + v2_descriptor.os_prop_values.guid = os_desc_guid; |
| 313 | |
| 314 | if (h->control < 0) { // might have already done this before |
| 315 | D("OPENING %s\n", USB_FFS_ADB_EP0); |
| 316 | @@ -279,10 +408,20 @@ static void init_functionfs(struct usb_handle *h) |
| 317 | goto err; |
| 318 | } |
| 319 | |
| 320 | - ret = adb_write(h->control, &descriptors, sizeof(descriptors)); |
| 321 | + ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); |
| 322 | if (ret < 0) { |
| 323 | - D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); |
| 324 | - goto err; |
| 325 | + D("[ %s: write v2_descriptor failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); |
| 326 | + v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); |
| 327 | + v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); |
| 328 | + v1_descriptor.header.fs_count = 3; |
| 329 | + v1_descriptor.header.hs_count = 3; |
| 330 | + v1_descriptor.fs_descs = fs_descriptors; |
| 331 | + v1_descriptor.hs_descs = hs_descriptors; |
| 332 | + ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); |
| 333 | + if (ret < 0) { |
| 334 | + D("[ %s: failed to write USB descriptors]\n", USB_FFS_ADB_EP0); |
| 335 | + goto err; |
| 336 | + } |
| 337 | } |
| 338 | |
| 339 | ret = adb_write(h->control, &strings, sizeof(strings)); |
| 340 | -- |
| 341 | 2.18.0 |
| 342 | |